Commit c0f5924
Scenario:
An object whose native `init` returns a different pointer than `alloc` (e.g. `CKRecordZoneID`) frees its alloc'd address. If another object (e.g. a `__MonoMac_NSAsyncActionDispatcher`) is allocated at that just-freed address on another thread and registered in `object_map`, the first object's `Handle` setter would then unconditionally `UnregisterNSObject` its stale alloc handle and clobber the second object's registration. A later native->managed marshal of that address then fails ("Could not find an existing managed instance", errors 8027/8034/8035).
Fix:
* Defer `object_map` registration until after `init` for user types. User types carry their gchandle in a native ivar (set at alloc time), which is self-cleaning when the address is freed/reused, so it's a safe authoritative fallback lookup during `init`. Only the final (post-`init`) handle is added to `object_map`. Direct bindings have no ivar, so they stay eagerly registered and are protected by the ownership-aware unregister below.
* Use an ownership-aware `UnregisterNSObject (handle, this)` in the `Handle` setter, which only removes the `object_map` entry if it still refers to `this` (mirroring the check `NativeObjectHasDied` already had).
* Add a native->managed ivar fallback to `Runtime.GetNSObject`/`GetNSObject<T>` so a user type whose object_map registration was deferred can still be resolved during `init`.
* Gate both behaviors behind a legacy `AppContext` switch (`ObjCRuntime.Runtime.RegisterObjectsBeforeInit`, default off) to restore the previous behavior if any existing binding relied on it.
Tests:
* AllocInitRaceTest deterministically reproduces the clobber with a tiny custom native allocator (ReuseSlotClassA/ReuseSlotClassB): one class' `init` frees its instance and forces the next allocation to reuse that exact address. ReusedAddressSurvivesAllocInitClobber verifies the reused address still resolves to the correct object; ReusedAddressClobberedWithLegacySwitch documents the pre-fix behavior with the legacy switch on.
* InitCallbackProbeTest exercises surfacing `self` to managed code during `init`.
* A failed-init + GC guard (InitReturnsNilClass) covers the #23679 shape (a native `init` that raises an Objective-C exception, followed by a forced GC).
Fixes #9478.
Fixes #23679.
Fixes #25861.
🤖 Pull request created by Copilot
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Rolf Bjarne Kvinge <rokvin@microsoft.com>
1 parent a26e2d4 commit c0f5924
21 files changed
Lines changed: 703 additions & 68 deletions
File tree
- src
- Foundation
- ObjCRuntime
- tests
- bindings-test
- dotnet/UnitTests/expected
- linker/link all
- monotouch-test/ObjCRuntime
- test-libraries
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
332 | 337 | | |
333 | 338 | | |
334 | 339 | | |
| |||
509 | 514 | | |
510 | 515 | | |
511 | 516 | | |
512 | | - | |
513 | 517 | | |
514 | 518 | | |
515 | | - | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
516 | 538 | | |
517 | 539 | | |
518 | 540 | | |
519 | 541 | | |
520 | 542 | | |
521 | | - | |
| 543 | + | |
522 | 544 | | |
523 | 545 | | |
524 | | - | |
525 | | - | |
526 | 546 | | |
527 | 547 | | |
528 | 548 | | |
| |||
543 | 563 | | |
544 | 564 | | |
545 | 565 | | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
546 | 597 | | |
547 | 598 | | |
548 | 599 | | |
| |||
800 | 851 | | |
801 | 852 | | |
802 | 853 | | |
803 | | - | |
804 | | - | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
805 | 863 | | |
806 | 864 | | |
807 | 865 | | |
| |||
843 | 901 | | |
844 | 902 | | |
845 | 903 | | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
846 | 907 | | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
847 | 922 | | |
848 | 923 | | |
849 | 924 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
290 | 290 | | |
291 | 291 | | |
292 | 292 | | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
293 | 301 | | |
294 | 302 | | |
295 | 303 | | |
| |||
1204 | 1212 | | |
1205 | 1213 | | |
1206 | 1214 | | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
1207 | 1230 | | |
1208 | 1231 | | |
1209 | 1232 | | |
| |||
1224 | 1247 | | |
1225 | 1248 | | |
1226 | 1249 | | |
1227 | | - | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
1228 | 1256 | | |
1229 | 1257 | | |
1230 | 1258 | | |
| |||
1234 | 1262 | | |
1235 | 1263 | | |
1236 | 1264 | | |
1237 | | - | |
1238 | | - | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
1239 | 1276 | | |
1240 | 1277 | | |
1241 | 1278 | | |
| |||
1865 | 1902 | | |
1866 | 1903 | | |
1867 | 1904 | | |
| 1905 | + | |
| 1906 | + | |
| 1907 | + | |
| 1908 | + | |
| 1909 | + | |
| 1910 | + | |
1868 | 1911 | | |
1869 | 1912 | | |
1870 | 1913 | | |
| |||
1921 | 1964 | | |
1922 | 1965 | | |
1923 | 1966 | | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
| 1971 | + | |
1924 | 1972 | | |
1925 | 1973 | | |
1926 | 1974 | | |
| |||
2794 | 2842 | | |
2795 | 2843 | | |
2796 | 2844 | | |
| 2845 | + | |
| 2846 | + | |
| 2847 | + | |
| 2848 | + | |
| 2849 | + | |
| 2850 | + | |
| 2851 | + | |
| 2852 | + | |
| 2853 | + | |
| 2854 | + | |
| 2855 | + | |
| 2856 | + | |
| 2857 | + | |
| 2858 | + | |
| 2859 | + | |
| 2860 | + | |
| 2861 | + | |
| 2862 | + | |
| 2863 | + | |
| 2864 | + | |
| 2865 | + | |
| 2866 | + | |
| 2867 | + | |
| 2868 | + | |
| 2869 | + | |
| 2870 | + | |
| 2871 | + | |
| 2872 | + | |
| 2873 | + | |
| 2874 | + | |
| 2875 | + | |
| 2876 | + | |
| 2877 | + | |
| 2878 | + | |
| 2879 | + | |
| 2880 | + | |
| 2881 | + | |
| 2882 | + | |
2797 | 2883 | | |
2798 | 2884 | | |
2799 | 2885 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
886 | 886 | | |
887 | 887 | | |
888 | 888 | | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
889 | 923 | | |
0 commit comments