Repository navigation
[runtime] Replace stale object map entries when completing deferred registration - #26478
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the .NET for Apple platforms runtime and toolchain to make native-handle → managed-wrapper registration more robust in alloc/init edge cases (notably pointer reuse and deferred registration), while also introducing related build/linker/runtime improvements (trusted platform assemblies generation, runtimeconfig merging, and UI-thread-check control via a feature switch), plus new regression tests.
Changes:
- Fixes deferred
NSObjectregistration/handle transitions to avoid staleobject_mapentries and incorrect unregister behavior when native pointers are reused. - Reworks UI-thread-check behavior: removes the old “remove-uithread-checks” optimizer path and replaces it with an MSBuild property + ILLink feature switch/substitution approach.
- Adds/updates build-time/runtime infrastructure (TPA list generation, runtimeconfig.dev merge task) and expands coverage with new tests and expected baselines.
Reviewed changes
Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tools/linker/OptimizeGeneratedCode.cs | Removes EnsureUIThread optimizer. |
| tools/dotnet-linker/Steps/TrimmableRegistrarStep.cs | Emit lookup method conditionally. |
| tools/dotnet-linker/Steps/GenerateMainStep.cs | Populate TPA list for build. |
| tools/dotnet-linker/LinkerConfiguration.cs | Adds new linker config keys. |
| tools/dotnet-linker/DocumentionComments.cs | Adds name-only doc signature helper. |
| tools/dotnet-linker/AppBundleRewriter.cs | Avoid trimmer crash in DDA signatures. |
| tools/common/Target.cs | Emit TPA name array + escaping. |
| tools/common/StaticRegistrar.cs | Adds namespace validation call. |
| tools/common/Optimizations.cs | Deprecates remove-uithread-checks optimization path. |
| tools/common/ErrorHelper.tools.cs | Log warnings as warnings in MSBuild. |
| tools/common/Application.cs | Stores TPA generation state/list. |
| tools/api-tools/mono-api-html/MultiplexedFormatter.cs | Fix placeholder leakage in output. |
| tools/api-tools/mono-api-html-tests/mono-api-html-tests.csproj | New test project for mono-api-html. |
| tools/api-tools/mono-api-html-tests/ApiDiffTests.cs | Regression test for placeholder leakage. |
| tools/api-tools/api-tools.slnx | Adds slnx including new tests. |
| tests/xharness/Jenkins/TestVariationsFactory.cs | Fixes variations naming/selection. |
| tests/test-libraries/libtest.m | Adds native helpers for alloc/init scenarios. |
| tests/test-libraries/libtest.h | Exposes new native test helpers. |
| tests/msbuild/Xamarin.MacDev.Tasks.Tests/TaskTests/MergeRuntimeConfigFilesTaskTest.cs | Tests runtimeconfig merge task. |
| tests/monotouch-test/ObjCRuntime/InitCallbackProbeTest.cs | Probe tests for init-time behavior. |
| tests/monotouch-test/ObjCRuntime/AllocInitRaceTest.cs | Deterministic alloc/init reuse regression. |
| tests/linker/link all/PreserveTest.cs | Updates preserved signature expectation. |
| tests/dotnet/UnitTests/Extensions.cs | Updates filtered-warning strings. |
| tests/dotnet/UnitTests/expected/TVOS-NativeAOT-TrimmableStatic-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/TVOS-NativeAOT-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/TVOS-MonoVM-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/TVOS-MonoVM-preservedapis.txt | Updates expected preserved APIs. |
| tests/dotnet/UnitTests/expected/TVOS-MonoVM-interpreter-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/TVOS-MonoVM-interpreter-preservedapis.txt | Updates expected preserved APIs. |
| tests/dotnet/UnitTests/expected/MacOSX-NativeAOT-TrimmableStatic-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/MacOSX-NativeAOT-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/MacOSX-CoreCLR-Interpreter-TrimmableStatic-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/MacOSX-CoreCLR-Interpreter-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/MacCatalyst-NativeAOT-TrimmableStatic-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/MacCatalyst-NativeAOT-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/MacCatalyst-MonoVM-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/MacCatalyst-MonoVM-preservedapis.txt | Updates expected preserved APIs. |
| tests/dotnet/UnitTests/expected/MacCatalyst-MonoVM-interpreter-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/MacCatalyst-MonoVM-interpreter-preservedapis.txt | Updates expected preserved APIs. |
| tests/dotnet/UnitTests/expected/iOS-NativeAOT-TrimmableStatic-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/iOS-NativeAOT-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/iOS-MonoVM-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/iOS-MonoVM-preservedapis.txt | Updates expected preserved APIs. |
| tests/dotnet/UnitTests/expected/iOS-MonoVM-interpreter-size.txt | Updates expected app size. |
| tests/dotnet/UnitTests/expected/iOS-MonoVM-interpreter-preservedapis.txt | Updates expected preserved APIs. |
| tests/dotnet/UnitTests/EnsureUIThreadChecksTest.cs | New test for UI-thread-check switch. |
| tests/dotnet/EnsureUIThreadApp/tvOS/EnsureUIThreadApp.csproj | New app for UI-thread-check test. |
| tests/dotnet/EnsureUIThreadApp/shared.csproj | Shared test app settings. |
| tests/dotnet/EnsureUIThreadApp/macOS/EnsureUIThreadApp.csproj | New app for UI-thread-check test. |
| tests/dotnet/EnsureUIThreadApp/MacCatalyst/EnsureUIThreadApp.csproj | New app for UI-thread-check test. |
| tests/dotnet/EnsureUIThreadApp/iOS/EnsureUIThreadApp.csproj | New app for UI-thread-check test. |
| tests/dotnet/EnsureUIThreadApp/EnsureUIThreadApp.cs | References EnsureUIThread for linking. |
| tests/dotnet/EnsureUIThreadApp/Directory.Build.props | Imports test props. |
| tests/cecil-tests/Documentation.KnownFailures.txt | Updates known failures list. |
| tests/bindings-test/ApiDefinition.cs | Adds bindings for new test types. |
| tests/assembly-preparer/RelocateRegistrarTrampolinesTests.cs | Adds generic trampoline relocation coverage. |
| tests/assembly-preparer/PreserveSmartEnumConversionsTest.cs | Updates DDA signature expectation. |
| tests/assembly-preparer/PreserveBlockCodeHandlerTests.cs | Updates DDA signature expectation. |
| tests/assembly-preparer/OptimizeGeneratedCodeHandlerTests.cs | Removes/adjusts EnsureUIThread optimization tests. |
| tests/assembly-preparer/BaseClass.cs | Adds extra csproj injection parameter. |
| system-dependencies.sh | Adds retrying runtime download logic. |
| src/VideoToolbox/VTDefs.cs | Improves enum docs. |
| src/VideoSubscriberAccount/VSAccountProviderAuthenticationScheme.cs | Improves extension docs. |
| src/UIKit/UIApplication.cs | Moves UI-thread-check wiring to feature switch. |
| src/SceneKit/Defs.cs | Improves enum docs. |
| src/SafariServices/SSEnums.cs | Improves enum docs. |
| src/PdfKit/Enums.cs | Improves enum docs + adds missing member docs. |
| src/ObjCRuntime/TypeMaps.cs | Makes LookupUnmanagedFunction virtual default. |
| src/MapKit/MKEnums.cs | Fixes doc typo and removes noise. |
| src/ILLink.Substitutions.tvOS.xml | Adds feature switch + stubs EnsureUIThread. |
| src/ILLink.Substitutions.macOS.xml | Adds feature switch + stubs EnsureUIThread. |
| src/ILLink.Substitutions.MacCatalyst.xml | Adds feature switch + stubs EnsureUIThread. |
| src/ILLink.Substitutions.iOS.xml | Adds feature switch + stubs EnsureUIThread. |
| src/Foundation/NSObject2.cs | Deferred registration + ownership-aware unregister. |
| src/Foundation/Enum.cs | Improves enum/member docs. |
| src/EventKitUI/Defs.cs | Improves enum/member docs. |
| src/CoreVideo/CVEnums.cs | Improves enum/member docs. |
| src/CoreTelephony/CTEnums.cs | Adds enum member docs + new member doc. |
| src/CoreMotion/Defs.cs | Removes doc noise. |
| src/AVFoundation/Enums.cs | Improves enum/member docs. |
| src/AudioUnit/AUEnums.cs | Improves enum/member docs. |
| src/AppKit/NSApplication.cs | Moves UI-thread-check wiring to feature switch. |
| src/AppKit/Enums.cs | Improves enum/member docs. |
| scripts/changelog/README.md | Adds changelog tool README. |
| scripts/changelog/Program.cs | New changelog tool implementation. |
| scripts/changelog/fragment.mk | Adds script integration fragment. |
| scripts/changelog/changelog.csproj | Adds changelog tool project. |
| runtime/xamarin/main.h | Adds TPA/multi-RID runtime globals. |
| runtime/runtime.m | Uses build-time TPA list when present. |
| NuGet.config | Removes an old feed entry. |
| msbuild/Xamarin.Shared/Xamarin.Shared.targets | Includes .exe in assembly lists + inputs. |
| msbuild/Xamarin.MacDev.Tasks/Tasks/MergeRuntimeConfigFiles.cs | New task to merge runtimeconfig files. |
| msbuild/Xamarin.MacDev.Tasks/Tasks/DetectSdkLocation.cs | Updates deprecation warning text. |
| msbuild/Xamarin.Localization.MSBuild/MSBStrings.resx | Updates warnings + adds new errors. |
| macios/Localize/loc/zh-Hant/macios/tools/mtouch/Errors.resx.lcl | Localization change (generated file). |
| macios/Localize/loc/tr/macios/msbuild/Xamarin.Localization.MSBuild/MSBStrings.resx.lcl | Localization change (generated file). |
| macios/Localize/loc/ko/macios/msbuild/Xamarin.Localization.MSBuild/MSBStrings.resx.lcl | Localization change (generated file). |
| macios/Localize/loc/ja/macios/msbuild/Xamarin.Localization.MSBuild/MSBStrings.resx.lcl | Localization change (generated file). |
| eng/Version.Details.xml | Updates xharness dependency. |
| eng/Version.Details.props | Updates xharness dependency version. |
| dotnet/targets/Xamarin.Shared.Sdk.targets | New MSBuild properties/targets for features. |
| dotnet/targets/Xamarin.Shared.Sdk.props | Changes default registrar/prepare behavior. |
| docs/website/optimizations.md | Documents EnsureUIThread optimization change. |
| docs/building-apps/build-properties.md | Documents new MSBuild properties. |
| Directory.Build.props | Updates SystemSecurityCryptographyXml version. |
| .github/workflows/zizmor.yml | Updates zizmor action pin. |
| .github/workflows/maestro-changelog.yml | Switches to in-repo changelog tool. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
|
✅ Applied expected app size files from gist. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
🚀 [CI Build #046aa4a] Test results 🚀Test results✅ All tests passed on VSTS: test results. 🎉 All 264 tests passed 🎉 Tests counts✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download macOS tests✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download Linux Build VerificationPipeline on Agent |
* Benchmarks: write through the emulator's REST API; count byte-array copies; verify peer re-registration - #23: both benchmark apps seed and update through the Firestore emulator's REST API (benchmarks/Shared/ EmulatorWriter.cs: HttpClient over SocketsHttpHandler, :commit writes built with Utf8JsonWriter), not an in-process "seeder" Firestore instance. Writes create no Java or Objective-C objects, so the process-wide JNI global reference count and iOS peer census measure only the listener, without the manual disposal Android's PrepareUpdate needed. Updates keep Update's must-exist semantics (currentDocument.exists) and the write->callback timer still starts after the write is prepared. The seeder instances, SeedData converters and UpdateFields go. - #27: FrameSink.onFrame/onTimedFrame take the byte-array frame as Object, so the binding no longer copies it into managed memory before the receiver runs (and back after). FrameReceiver copies it, with one exact-size array and a bulk copy, after the benchmark observer's arrival probe, so byte-array allocation now includes the copy. Production (direct buffer, frame null) is unchanged. - #29: Microsoft.iOS 27.1's RegisterNSObject removes a pointer's stale entry before inserting, so the peer census's created count does count re-registered peers; PeerCensus's explanation said otherwise and is corrected, with the one upstream-fixed corner (dotnet/macios#26478). A new startup check re-wraps a stale entry and requires created +1. - Android peered mode crashed on the first snapshot: the entry set's enumerator now hands HashMap$Node entries over as plain Java.Lang.Object, so the IMapEntry cast threw. PlainValues falls back to JavaCast, as an app must. Closes #23 Closes #27 Closes #29 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Android peered baseline: restore the measured top-level map walk Round six (87b1731) routed each document's top-level map through Map()'s entrySet() walk, which no run ever measured: on this toolchain the entry set hands each HashMap$Node over as a plain Java.Lang.Object, so the IMapEntry cast threw on the first snapshot, and with JavaCast the process crashed (SIGSEGV) at 500+ documents. Fields goes back to enumerating the binding's JavaDictionary (keySet() plus get() per key), as an ordinary app's foreach does and as every published peered run measured. Map() keeps the JavaCast fallback, disposing the plain peer, for the rare java.util.Map value; it is off the measured path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Benchmarks: run the iOS benchmark on a physical iPhone scripts/run-ios-device-benchmark.sh builds the NativeAOT benchmark for ios-arm64, signs it (CODESIGN_KEY, CODESIGN_PROVISION), installs it over the existing app (so the phone keeps its local-network permission), empties the last run's result files on the phone, launches it with devicectl and pulls the results back. The phone reaches the Firestore emulator on the Mac over Wi-Fi: firebase.lan.json is firebase.json listening on all interfaces, and the app declares NSLocalNetworkUsageDescription, which iOS requires before it reaches the local network. Android devices were already supported (ALLOW_DEVICE=1, through adb reverse). This is also the first run of the iOS shim's device (ios-arm64) slice, which had only been linked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * run-android-benchmark.sh: force-stop the app before each launch; check the emulator it used On a physical Pixel (Android 17), `install -r` of an unchanged APK didn't always stop the running app. The app starts one run per process, so a process left from an earlier run ignored the next launch (its late PEERLESS_DONE line then ended the next run's wait) or a relaunch read an intent without --es host and fell back to 10.0.2.2, which a device can't reach. The script now force-stops the package (and waits for it to exit) before every launch, and requires the run file to name the emulator address this launch passed, as the iOS scripts already do. The fake adb in test-benchmark-scripts.sh records the launch's host and port in the run file it hands back, and two new checks cover the force-stop and the device's reversed address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Android benchmark: start each run from the script's own launch On a physical Pixel (Android 17), SystemUI relaunched the benchmark app the moment the script force-stopped it. The script's launch then only brought that task to the front, without creating an activity or delivering its intent, so the app either ran with no extras (falling back to the emulator's 10.0.2.2, which a phone can't reach) or never started a run. - run-android-benchmark.sh launches with --activity-clear-task, so the launch always creates a fresh activity from its own intent; the script tests check for it. - MainActivity starts a run only from an intent that carries the script's "mode" extra (from OnCreate or OnNewIntent), so a relaunch by the system or the launcher can't take the process's one run with default options. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Benchmarks: phone results on .NET 11, with findings Ten runs on a Pixel 9 Pro XL and an iPhone SE, four alternating emulator runs that check the REST writer against the in-process seeder on .NET 11, and FINDINGS-devices.md, including failed runs, unknowns and limits. The November 3 results and tables stay as they were. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Review of #71: iPhone run script robustness, compare commands, frame-release tests - run-ios-device-benchmark.sh: capture devicectl's process list and require three missed checks in a row; end the run if the last run's files can't be listed or emptied (files that don't exist are skipped, so a first run works); keep host.txt and the checks when the device description can't be read. - README: the documented compare commands name the emulator and phone runs separately, since runs are pooled by mode. - compare-android.py: the peerless-bytes caption follows each run's "byteArrayCopy: counted" line, with the two published phone runs that predate it listed. Regenerating every published table changes nothing. - Android tests: a frame after Stop and a frame with a bad length release their Java array; each fails when its release is broken. - Info.plist: the plain-HTTP comment is back above NSAppTransportSecurity. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * FINDINGS-devices: correct the emulator load range The four .NET 11 emulator runs record a 1-minute load of 15.54 to 23.69, not 14-24. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Addresses review feedback on PR #26424:
RegisterNSObject(..., onlyIfNeeded: true)previously bailed out onobject_map.ContainsKey(ptr)alone, without checking whether the existing entry's weakGCHandle.Targetwas still alive. If a native pointer was reused and the previous entry's target had already been collected (without the native object dying through the normal path), the stale entry would block registration of the new wrapper — leaving it unregistered and leaking the oldGCHandle.This change checks
existing.Target is not nullinstead of just key presence. A dead entry is now removed and its handle freed before the new object is registered, matching the behavior already used elsewhere (e.g.UnregisterNSObject(ptr, managed)andNativeObjectHasDied).🤖 Pull request created by Copilot