Skip to content

[ObjCRuntime] Add opt-in Runtime.InvokedSelector tracking. Fixes #23905 - #27220

Draft
rolfbjarne wants to merge 3 commits into
mainfrom
dev/rolf/invoked-selector-tracking
Draft

rolfbjarne wants to merge 3 commits into
mainfrom
dev/rolf/invoked-selector-tracking

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

Expose the actual selector of the innermost active Objective-C-to-managed invocation on the current thread. Save and restore the context across nested calls and Objective-C exception exits, without flowing it through the managed execution context.

Detect getter references in build inputs and persist the decision through assembly preparation, linking, and post-processing. EnableInvokedSelectorTracking provides an explicit override for reflection or dynamically loaded code, and explicitly disabling tracking rejects detected references instead of returning misleading results.

Keep unused invocation paths unchanged by selecting separate tracked dynamic trampolines at startup and conditionally instrumenting generated registrar methods. Partial-static registration falls back to dynamic registration when enabled, since its prebuilt platform registrar cannot be instrumented per application.

Add detection, registrar-generation, incremental-build, and runtime coverage, and document the property and its limitations.

This enables multiple selectors registered with Runtime.ConnectMethod to share a managed method and distinguish which selector invoked it. It does not add general support for overriding methodSignatureForSelector: or fix its existing dynamic-registrar recursion.

Fixes #23905

🤖 Pull request created by Copilot

rolfbjarne and others added 3 commits October 30, 2026 16:34
Expose the actual selector of the innermost active Objective-C-to-managed invocation on the current thread. Save and restore the context across nested calls and Objective-C exception exits, without flowing it through the managed execution context.

Detect getter references in build inputs and persist the decision through assembly preparation, linking, and post-processing. EnableInvokedSelectorTracking provides an explicit override for reflection or dynamically loaded code, and explicitly disabling tracking rejects detected references instead of returning misleading results.

Keep unused invocation paths unchanged by selecting separate tracked dynamic trampolines at startup and conditionally instrumenting generated registrar methods. Partial-static registration falls back to dynamic registration when enabled, since its prebuilt platform registrar cannot be instrumented per application.

Add detection, registrar-generation, incremental-build, and runtime coverage, and document the property and its limitations.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: e2ccbe90-e08b-4e00-b391-a795b08a3b6c
Keep dynamic registrar analysis owned by assembly preparation when the linker reruns selector detection. Avoid scanning unrelated modules or scanning again after tracking is already enabled, while preserving conservative detection and explicit-disable errors.

Handle missing preparation output without failing isolated or non-Mac target execution, and preserve explicit EnableInvokedSelectorTracking overrides. Cover automatic detection, explicit-disable failures, incremental reuse, and missing output across the supported preparation/post-processing combinations; document diagnostic MT2388.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: e2ccbe90-e08b-4e00-b391-a795b08a3b6c
Forward AdditionalDefineConstants to MySimpleApp so selector tracking integration tests compile the getter reference when requested. Without this test-fixture hook, the automatic-detection cases exercised an app with no reference and could not verify the intended build-time behavior.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: e2ccbe90-e08b-4e00-b391-a795b08a3b6c
@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 7ea689275da85e3d45033063e64e1cc07779a9a9 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [CI Build #7ea6892] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 5 tests failed, 212 tests passed.

Failures

❌ dotnettests tests (iOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.AppSizeTest.CoreCLR_Interpreter(iOS,"ios-arm64",Tr...: Multiple failures or warnings in test:
  1. Unexpected APIs were added to the preserved set. The updated expected file is avai...
    * Xamarin.Tests.AppSizeTest.CoreCLR_R2R(iOS,"ios-arm64",True): Multiple failures or warnings in test:
  2. Unexpected APIs were added to the preserved set. The updated expected file is avai...

Html Report (VSDrops) Download

❌ dotnettests tests (MacCatalyst)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.AppSizeTest.CoreCLR_Interpreter(MacCatalyst,"macca...: Multiple failures or warnings in test:
  1. Unexpected APIs were added to the preserved set. The updated expected file is avai...
    * Xamarin.Tests.AppSizeTest.CoreCLR_R2R(MacCatalyst,"maccatalyst-a...: Multiple failures or warnings in test:
  2. Unexpected APIs were added to the preserved set. The updated expected file is avai...

Html Report (VSDrops) Download

❌ dotnettests tests (tvOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.AppSizeTest.CoreCLR_Interpreter(TVOS,"tvos-arm64",...: Multiple failures or warnings in test:
  1. Unexpected APIs were added to the preserved set. The updated expected file is avai...
    * Xamarin.Tests.AppSizeTest.CoreCLR_R2R(TVOS,"tvos-arm64",True): Multiple failures or warnings in test:
  2. Unexpected APIs were added to the preserved set. The updated expected file is avai...

Html Report (VSDrops) Download

❌ Tests on macOS Sonoma (14) tests

1 tests failed, 4 tests passed.

Failed tests

  • macOS/osx-x64 monotouch-test: Failed (exit code 137)
    • No test failure details available. Output tail:
      • MonoTouchFixtures.Compression.ThoroughCompressionStreamTest : 7.5165 ms
      • MonoTouchFixtures.Compression : 71.6452 ms
      • MonoTouchFixtures.ConfigTest
      • [PASS] Existence
      • MonoTouchFixtures.ConfigTest : 1.2996 ms
      • MonoTouchFixtures.Contacts
      • MonoTouchFixtures.Contacts.ContactFetchRequestTest
      • Contacts permission denied (error: Access Denied)
      • Execution timed out after 600 seconds.
      • Execution completed with exit code 137
  • MacCatalyst/maccatalyst-x64 monotouch-test: Failed (exit code 137)
    • No test failure details available. Output tail:
      • at MonoTouch.NUnit.UI.BaseTouchRunner.Run(NUnit.Framework.Internal.Test)
      • at MonoTouch.NUnit.UI.BaseTouchRunner.Run()
      • at MonoTouch.NUnit.UI.BaseTouchRunner.<AutoRun>b__49_0()
      • at MonoTouch.NUnit.UI.TouchRunner+<>c__DisplayClass19_0.<ExecuteOnMainThread>b__0()
      • at Foundation.NSAsyncActionDispatcher.Apply()
      • at __Registrar_Callbacks__.callback_3037_Foundation_NSAsyncActionDispatcher_Apply(IntPtr, IntPtr, IntPtr*)
      • at UIKit.UIApplication.UIApplicationMain(Int32, System.String[], IntPtr, IntPtr)
      • at UIKit.UIApplication.Main(System.String[], System.Type, System.Type)
      • at MainClass.Main(System.String[])
      • Execution completed with exit code 137

Html Report (VSDrops) Download

❌ Tests on macOS Sequoia (15) tests

1 tests failed, 4 tests passed.

Failed tests

  • MacCatalyst/maccatalyst-x64 monotouch-test: Failed (exit code 137)
    • No test failure details available. Output tail:
      • at MonoTouch.NUnit.UI.BaseTouchRunner.Run(NUnit.Framework.Internal.Test)
      • at MonoTouch.NUnit.UI.BaseTouchRunner.Run()
      • at MonoTouch.NUnit.UI.BaseTouchRunner.<AutoRun>b__49_0()
      • at MonoTouch.NUnit.UI.TouchRunner+<>c__DisplayClass19_0.<ExecuteOnMainThread>b__0()
      • at Foundation.NSAsyncActionDispatcher.Apply()
      • at __Registrar_Callbacks__.callback_3037_Foundation_NSAsyncActionDispatcher_Apply(IntPtr, IntPtr, IntPtr*)
      • at UIKit.UIApplication.UIApplicationMain(Int32, System.String[], IntPtr, IntPtr)
      • at UIKit.UIApplication.Main(System.String[], System.Type, System.Type)
      • at MainClass.Main(System.String[])
      • Execution completed with exit code 137
  • MacCatalyst/maccatalyst-arm64 monotouch-test: Failed (exit code 137)
    • No test failure details available. Output tail:
      • at MonoTouch.NUnit.UI.BaseTouchRunner.Run(NUnit.Framework.Internal.Test)
      • at MonoTouch.NUnit.UI.BaseTouchRunner.Run()
      • at MonoTouch.NUnit.UI.BaseTouchRunner.<AutoRun>b__49_0()
      • at MonoTouch.NUnit.UI.TouchRunner+<>c__DisplayClass19_0.<ExecuteOnMainThread>b__0()
      • at Foundation.NSAsyncActionDispatcher.Apply()
      • at __Registrar_Callbacks__.callback_3037_Foundation_NSAsyncActionDispatcher_Apply(IntPtr, IntPtr, IntPtr*)
      • at UIKit.UIApplication.UIApplicationMain(Int32, System.String[], IntPtr, IntPtr)
      • at UIKit.UIApplication.Main(System.String[], System.Type, System.Type)
      • at MainClass.Main(System.String[])
      • Execution completed with exit code 137

Html Report (VSDrops) Download

Successes

✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 1 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker (iOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (tvOS): All 21 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 20 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 20 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 20 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 20 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Golden Gate (27): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

✅ Linux build succeeded

Pipeline on Agent
Hash: 7ea689275da85e3d45033063e64e1cc07779a9a9 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

⚠️ AppSizeTest expected files changed ⚠️

The AppSizeTest detected changes in the expected app size files.

To update the expected files, add a comment with the following command:

/apply-gist https://gist.github.com/vs-mobiletools-engineering-service2/b5c8fb6671a68afa2a8c321a7572bf4c
Updated files
  • iOS-CoreCLR-Interpreter-preservedapis.txt
  • iOS-CoreCLR-R2R-preservedapis.txt
  • MacCatalyst-CoreCLR-Interpreter-preservedapis.txt
  • MacCatalyst-CoreCLR-R2R-preservedapis.txt
  • TVOS-CoreCLR-Interpreter-preservedapis.txt
  • TVOS-CoreCLR-R2R-preservedapis.txt

Pipeline on Agent
Hash: 7ea689275da85e3d45033063e64e1cc07779a9a9 [PR build]

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add API to get the selector for an Exported method

2 participants