Skip to content

[HandleSafety] Recognize GC.KeepAlive with equivalent (non-identical) loads. - #26366

Open
rolfbjarne wants to merge 2 commits into
mainfrom
dev/rolf/handle-safety-equivalent-loads
Open

[HandleSafety] Recognize GC.KeepAlive with equivalent (non-identical) loads.#26366
rolfbjarne wants to merge 2 commits into
mainfrom
dev/rolf/handle-safety-equivalent-loads

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

Some members were already safe - they fetch a handle from a field and then call
GC.KeepAlive on that same field, e.g.:

DoSomethingWith (this.field.GetHandle ());
GC.KeepAlive (this.field);

The test failed to recognize this because field is a field, so the compiler
emits two separate ldfld field instructions, and IsKeptAlive only matched
the handle-fetch load instruction by reference identity (which worked for the
dup pattern the compiler emits for local variables, but not for fields).

Improve IsKeptAlive to also match a GC.KeepAlive argument that's loaded by a
different but provably equivalent instruction (same field of the same object,
same argument, same local, etc.). This is sound: it never claims two loads are
equivalent unless it's certain.

This resolves the following known failures:

  • AddressBook.ABMultiValueEntry`1.get_Identifier
  • AddressBook.ABMultiValueEntry`1.get_Label
  • AddressBook.ABMultiValueEntry`1.get_Value
  • AddressBook.ABMultiValueEntry`1.set_Label
  • AddressBook.ABMultiValueEntry`1.set_Value
  • Foundation.NSFastEnumerator`1.Fetch

Copilot-Session: dd86cf03-297a-43e8-ae1f-3c0b22bb67c4

… loads.

Some members were already safe - they fetch a handle from a field and then call
`GC.KeepAlive` on that same field, e.g.:

    DoSomethingWith (this.field.GetHandle ());
    GC.KeepAlive (this.field);

The test failed to recognize this because `field` is a field, so the compiler
emits two separate `ldfld field` instructions, and `IsKeptAlive` only matched
the handle-fetch load instruction by reference identity (which worked for the
`dup` pattern the compiler emits for local variables, but not for fields).

Improve `IsKeptAlive` to also match a `GC.KeepAlive` argument that's loaded by a
different but provably equivalent instruction (same field of the same object,
same argument, same local, etc.). This is sound: it never claims two loads are
equivalent unless it's certain.

This resolves the following known failures:

* AddressBook.ABMultiValueEntry`1.get_Identifier
* AddressBook.ABMultiValueEntry`1.get_Label
* AddressBook.ABMultiValueEntry`1.get_Value
* AddressBook.ABMultiValueEntry`1.set_Label
* AddressBook.ABMultiValueEntry`1.set_Value
* Foundation.NSFastEnumerator`1.Fetch

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: dd86cf03-297a-43e8-ae1f-3c0b22bb67c4
Copilot AI review requested due to automatic review settings July 30, 2026 10:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves the Cecil-based handle-safety analysis used by the test suite to recognize GC.KeepAlive patterns where the kept-alive object is reloaded via a different but equivalent IL load instruction (notably repeated ldfld for fields). This makes previously “known failing” members pass the HandleSafety check.

Changes:

  • Extend IsKeptAlive to match GC.KeepAlive arguments via a new “equivalent load” analysis (not just instruction identity).
  • Add equivalence logic for common load opcodes and for ldfld when both loads target the same field on equivalent source objects.
  • Remove several entries from HandleSafety.KnownFailures.cs that should now be recognized as safe.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/cecil-tests/HandleSafety.cs Enhances GC.KeepAlive detection by recognizing provably equivalent IL loads (incl. repeated ldfld patterns).
tests/cecil-tests/HandleSafety.KnownFailures.cs Drops known-failure entries that should now pass with the improved equivalence matching.

Comment thread tests/cecil-tests/HandleSafety.cs
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@rolfbjarne
rolfbjarne requested a review from dalexsoto as a code owner July 31, 2026 13:59
@rolfbjarne
rolfbjarne enabled auto-merge (squash) July 31, 2026 14:01
@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: 1573996988ae37d09ab20bbf550f342f74d7f8fa [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [CI Build #1573996] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 44 tests failed, 159 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.AppIconTest.AlternateAppIcon_Failure(iOS,"ios-arm6...: Failure when comparing error messages:
      Unexpected error message #0:
      Expected: Can't find the AlternateAppIcon 'InexistentAppI...
    • Xamarin.Tests.AppIconTest.AlternateAppIcon(iOS,"ios-arm64"): 'dotnet build' failed with exit code 1
      Full command: /Users/cloudtest/vss/_work/1/s/macios/builds/downloads/dotnet-sdk-10.0.400-...
    • Xamarin.Tests.AppIconTest.AlternateAppIcons(iOS,"ios-arm64"): 'dotnet build' failed with exit code 1
      Full command: /Users/cloudtest/vss/_work/1/s/macios/builds/downloads/dotnet-sdk-10.0.400-...
    • ... and 61 more

Html Report (VSDrops) Download

❌ dotnettests tests (macOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.ExtensionsTest.AdditionalAppExtensionTest(MacOSX,"...: 'dotnet build' failed with exit code 1
      Full command: /Users/cloudtest/vss/_work/1/s/macios/builds/downloads/dotnet-sdk-10.0.400-...

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.AppIconTest.AlternateAppIcon_Failure(TVOS,"tvos-ar...: Failure when comparing error messages:
      Unexpected error message #0:
      Expected: Can't find the AlternateAppIcon 'InexistentAppI...
    • Xamarin.Tests.AppIconTest.AlternateAppIcon(TVOS,"tvos-arm64"): 'dotnet build' failed with exit code 1
      Full command: /Users/cloudtest/vss/_work/1/s/macios/builds/downloads/dotnet-sdk-10.0.400-...
    • Xamarin.Tests.AppIconTest.AlternateAppIcons(TVOS,"tvos-arm64"): 'dotnet build' failed with exit code 1
      Full command: /Users/cloudtest/vss/_work/1/s/macios/builds/downloads/dotnet-sdk-10.0.400-...
    • ... and 29 more

Html Report (VSDrops) Download

❌ monotouch tests (iOS)

19 tests failed, 0 tests passed.

Failed tests

  • monotouch-test/iOS - simulator/Debug: BuildFailure
  • monotouch-test/iOS - simulator/Release (link sdk): BuildFailure
  • monotouch-test/iOS - simulator/Release (link all): BuildFailure
  • monotouch-test/iOS - simulator/Debug (PrepareAssemblies): BuildFailure
  • monotouch-test/iOS - simulator/Debug (PrepareAssemblies, inline dlfcn, dont link): BuildFailure
  • monotouch-test/iOS - simulator/Debug (LinkSdk): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/iOS - simulator/Debug (static registrar): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/iOS - simulator/Release (all optimizations): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/iOS - simulator/Release (NativeAOT): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/iOS - simulator/Release (trimmable static registrar, NativeAOT): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/iOS - simulator/Debug (managed static registrar): BuildFailure
  • monotouch-test/iOS - simulator/Release (managed static registrar, all optimizations): BuildFailure
  • monotouch-test/iOS - simulator/Debug (interpreter): BuildFailure
  • monotouch-test/iOS - simulator/Release (interpreter): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/iOS - simulator/Release (compat inline Class.GetHandle): BuildFailure
  • monotouch-test/iOS - simulator/Release (strict inline Class.GetHandle): BuildFailure
  • monotouch-test/iOS - simulator/Release (compat inline dlfcn): BuildFailure
  • monotouch-test/iOS - simulator/Release (strict inline dlfcn, link sdk): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/iOS - simulator/Release (NativeAOT, .NET 11 defaults): BuildFailure

Html Report (VSDrops) Download

❌ monotouch tests (tvOS)

19 tests failed, 0 tests passed.

Failed tests

  • monotouch-test/tvOS - simulator/Debug: BuildFailure
  • monotouch-test/tvOS - simulator/Release (link sdk): BuildFailure
  • monotouch-test/tvOS - simulator/Release (link all): BuildFailure
  • monotouch-test/tvOS - simulator/Debug (PrepareAssemblies): BuildFailure
  • monotouch-test/tvOS - simulator/Debug (PrepareAssemblies, inline dlfcn, dont link): BuildFailure
  • monotouch-test/tvOS - simulator/Debug (LinkSdk): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/tvOS - simulator/Debug (static registrar): BuildFailure
  • monotouch-test/tvOS - simulator/Release (all optimizations): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/tvOS - simulator/Release (NativeAOT): BuildFailure
  • monotouch-test/tvOS - simulator/Release (trimmable static registrar, NativeAOT): BuildFailure
  • monotouch-test/tvOS - simulator/Debug (managed static registrar): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/tvOS - simulator/Release (managed static registrar, all optimizations): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/tvOS - simulator/Debug (interpreter): BuildFailure
  • monotouch-test/tvOS - simulator/Release (interpreter): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/tvOS - simulator/Release (compat inline Class.GetHandle): BuildFailure
  • monotouch-test/tvOS - simulator/Release (strict inline Class.GetHandle): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/tvOS - simulator/Release (compat inline dlfcn): BuildFailure
  • monotouch-test/tvOS - simulator/Release (strict inline dlfcn, link sdk): BuildFailure ( (failed to parse the logs: The Writer is closed or in error state.))
  • monotouch-test/tvOS - simulator/Release (NativeAOT, .NET 11 defaults): BuildFailure

Html Report (VSDrops) Download

❌ msbuild tests

1 tests failed, 1 tests passed.

Failed tests

  • MSBuild tasks tests: Failed (Execution failed with exit code 1)
    • Xamarin.MacDev.Tasks.DetectSigningIdentityTaskTests.CustomEntitl...: #RunTask-ErrorCount
      : error: No valid iOS code signing keys found in keychain. You need to request a codesigning certificate ...
    • Xamarin.MacDev.Tasks.DetectSigningIdentityTaskTests.EmptyEntitle...: #RunTask-ErrorCount
      : error: No valid iOS code signing keys found in keychain. You need to request a codesigning certificate ...
    • Xamarin.MacDev.Tasks.DetectSigningIdentityTaskTests.EmptyEntitle...: #RunTask-ErrorCount
      : error: No valid iOS code signing keys found in keychain. You need to request a codesigning certificate ...
    • ... and 7 more

Html Report (VSDrops) Download

❌ windows tests

1 tests failed, 2 tests passed.

Failed tests

  • Remote .NET tests/Xamarin.Tests.WindowsTest.PluralRuntimeIdentifiersWithRemoteMac(iOS,"ios-arm64","Release"): Failed: 'dotnet build' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.WindowsTest.BundleStructureWithRemoteMac(iOS,"ios-arm64",All,"Debug"): Failed: 'dotnet build' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.PostBuildTest.BuildIpaAndArchiveOnRemoteWindowsTest(iOS,"ios-arm64"): Failed: 'dotnet build' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.WindowsTest.PluralRuntimeIdentifiersWithRemoteMac(iOS,"ios-arm64","Debug"): Failed: 'dotnet build' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.DotNetProjectTest.AppWithLibraryWithResourcesReferenceOnRemoteWindows(iOS,"ios-arm64",False): Failed: 'dotnet build' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.DotNetProjectTest.AppWithLibraryWithResourcesReferenceOnRemoteWindows(iOS,"ios-arm64",True): Failed: 'dotnet build' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.WindowsTest.AssemblyPreparerRemoteTest(iOS,"ios-arm64"): Failed: 'dotnet build' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.WindowsTest.RemoteTest(iOS,"ios-arm64"): Failed: 'dotnet build' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.WindowsTest.StripTest(iOS,"ios-arm64","Release"): Failed: 'dotnet build' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.DotNetProjectTest.BuildProjectsWithExtensionsOnRemoteWindows(iOS,"ios-arm64",False): Failed: 'dotnet build' failed with exit code 1

Html Report (VSDrops) Download

❌ Tests on macOS Tahoe (26) tests

1 tests failed, 4 tests passed.

Failed tests

  • macOS/osx-arm64 monotouch-test: Failed (exit code 137)
    • No test failure details available. Output tail:
      • 2026-07-31 07:45:41.897 monotouchtest[21579:5511663] CFSocketSetAddress listen failure: 102
      • 2026-07-31 07:45:41.914 monotouchtest[21579:5511663] CFSocketSetAddress listen failure: 102
      • 2026-07-31 07:45:41.930 monotouchtest[21579:5511663] CFSocketSetAddress listen failure: 102
      • 2026-07-31 07:45:41.946 monotouchtest[21579:5511663] CFSocketSetAddress listen failure: 102
      • 2026-07-31 07:45:41.962 monotouchtest[21579:5511663] CFSocketSetAddress listen failure: 102
      • 2026-07-31 07:45:41.978 monotouchtest[21579:5511663] CFSocketSetAddress listen failure: 102
      • 2026-07-31 07:45:41.995 monotouchtest[21579:5511663] CFSocketSetAddress listen failure: 102
      • 2026-07-31 07:45:42.043 monotouchtest[21579:5511244] CFSocketSetAddress listen failure: 102
      • Execution timed out after 600 seconds.
      • 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 (MacCatalyst): 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 5 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 15 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (tvOS): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 19 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 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 Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: 1573996988ae37d09ab20bbf550f342f74d7f8fa [PR build]

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.

3 participants