Skip to content

[HandleSafety] Add a number of dictionary APIs to make it easier to safely get and set values. - #26829

Open
rolfbjarne wants to merge 3 commits into
mainfrom
dev/rolf/handle-safety-dictionaries
Open

rolfbjarne wants to merge 3 commits into
mainfrom
dev/rolf/handle-safety-dictionaries

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

These new overloads take INativeObject instances instead of plain handles, and as such they can make sure the INativeObject instances aren't collected by the GC too early (instead of each caller having to ensure this).

  • CFDictionary gains GetValue and GetBooleanValue overloads.
  • CFMutableDictionary gains SetValue overloads.
  • NSDictionary gains LowlevelObjectForKey overloads.
  • NSMutableDictionary gains LowlevelSetObject overloads.

And then update consumers to use these to APIs to fix premature object collection by the GC.

Contributes towards #10146.

This also required a fix in the handle analyzer: recognize symbol references that occur after a handle access within the same containing statement. This prevents RBI0014 from incorrectly flagging pattern variables that are consumed in an if body after being used in the condition.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

Copilot-Session: 0eb8114a-c6b4-4dda-9cf6-2fd3f7e5f123

Recognize symbol references that occur after a handle access within the same containing statement. This prevents RBI0014 from incorrectly flagging pattern variables that are consumed in an if body after being used in the condition.

Add a regression case covering an INativeObject pattern variable used later in the same if statement.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 0eb8114a-c6b4-4dda-9cf6-2fd3f7e5f123
…afely get and set values.

These new overloads take INativeObject instances instead of plain handles, and as such they can make sure the INativeObject instances aren't collected by the GC too early (instead of each caller having to ensure this).

* CFDictionary gains GetValue and GetBooleanValue overloads.
* CFMutableDictionary gains SetValue overloads.
* NSDictionary gains LowlevelObjectForKey overloads.
* NSMutableDictionary gains LowlevelSetObject overloads.

And then update consumers to use these to APIs to fix premature object collection by the GC.

Contributes towards #10146.

Copilot-Session: dd86cf03-297a-43e8-ae1f-3c0b22bb67c4
@rolfbjarne
rolfbjarne requested a review from dalexsoto as a code owner October 2, 2026 17:13
Copilot AI balanced review requested due to automatic review settings October 2, 2026 17:13

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.

Copilot review overview

🟡 Changes recommended

The analyzer can miss real GC-safety violations, its regression test bypasses the new logic, and a nullable setter now rejects null.

Review effort: Balanced
Findings: 3 Medium severity

Open (3)
What changed in this PR

Adds GC-safe dictionary APIs and migrates consumers away from direct native-handle access. It also updates RBI0014 analysis for same-statement references.

Changes:

  • Added safe dictionary get/set overloads.
  • Migrated framework consumers to the new APIs.
  • Updated analyzer behavior, tests, and known failures.
File Description
tests/​rgen/​Microsoft.Macios.Bindings.Analyzer.Tests/​NativeObjectHandleAnalyzerTests.cs Adds analyzer regression coverage.
tests/​cecil-tests/​HandleSafety.KnownFailures.cs Removes resolved handle-safety failures.
src/​Security/​Certificate.cs Uses safe dictionary setters.
src/​rgen/​Microsoft.Macios.Bindings.Analyzer/​NativeObjectHandleAnalyzer.cs Detects later same-statement references.
src/​QuickLook/​Thumbnail.cs Safely retains dictionary keys.
src/​NaturalLanguage/​NLVectorDictionary.cs Uses safe key lookup.
src/​NaturalLanguage/​NLStrongDictionary.cs Uses safe key lookup.
src/​ImageIO/​CGImageDestination.cs Migrates background-color assignment.
src/​Foundation/​NSMutableDictionary.cs Adds safe setter overloads.
src/​Foundation/​NSLayoutConstraint.cs Retains native views during insertion.
src/​Foundation/​NSDictionary.cs Adds safe lookup overload.
src/​Foundation/​DictionaryContainer.cs Uses safe mutable dictionary APIs.
src/​CoreText/​CTTypesetter.cs Migrates option accessors.
src/​CoreText/​CTStringAttributes.cs Migrates attribute accessors.
src/​CoreText/​CTFont.cs Migrates feature accessors.
src/​CoreText/​Adapter.cs Uses safe dictionary helpers.
src/​CoreServices/​FSEvents.cs Safely retains event keys.
src/​CoreImage/​CISampler.cs Safely inserts color spaces.
src/​CoreImage/​CIImage.cs Migrates adjustment options.
src/​CoreFoundation/​CFDictionary.cs Adds GC-safe get/set overloads.
src/​AppKit/​NSStringAttributes.cs Uses safe key lookup.

Comment thread src/ImageIO/CGImageDestination.cs
@vs-mobiletools-engineering-service2

This comment has been minimized.

Comment thread src/QuickLook/Thumbnail.cs Outdated
Comment thread src/QuickLook/Thumbnail.cs Outdated
Co-authored-by: Rolf Bjarne Kvinge <rolf@xamarin.com>
Copilot AI balanced review requested due to automatic review settings October 2, 2026 17:57

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.

Copilot review overview

🔵 Needs a closer look

The analyzer can suppress valid diagnostics, and one removed known failure still has a native-object lifetime gap.

Review effort: Balanced
Findings: None

Resolved since last review (3)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Keep source wrappers alive through CFArray creation

src/​Foundation/​DictionaryContainer.cs:669

🤖 ❌ Memory management — This method still extracts raw handles from values before CFArray.FromIntPtrs; changing only the dictionary insertion does not keep the source wrappers alive while CFArrayCreate consumes those handles. Keep values alive through array creation/insertion before removing this method from the handle-safety known failures.

Rule: GC.KeepAlive after handle access

@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: 1d157d640bf685eb24f1f8346a83d2a123652ad4 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🚀 [CI Build #1d157d6] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 221 tests passed 🎉

Tests counts

✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): 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
✅ dotnettests (tvOS): 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 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 Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ 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: 1d157d640bf685eb24f1f8346a83d2a123652ad4 [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.

3 participants