Skip to content

[ObjCRuntime] Improve super calls - Fixes #25362 - #25376

Merged
rolfbjarne merged 14 commits into
mainfrom
dev/rolf/objcmsgsendsuper
May 18, 2026
Merged

rolfbjarne merged 14 commits into
mainfrom
dev/rolf/objcmsgsendsuper

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

Summary

Introduces a stack-allocated ObjCSuper struct to replace the use of NSObject.SuperHandle for objc_msgSendSuper calls. This eliminates the need to allocate and manage unmanaged memory for the objc_super struct on every super call.

Changes

  • New ObjCSuper readonly ref struct (src/ObjCRuntime/ObjCSuper.cs): A [StructLayout(LayoutKind.Sequential)] struct with Receiver and ClassHandle fields matching the native objc_super layout.

  • Updated bgen code generation: P/Invoke declarations for objc_msgSendSuper variants now take ObjCSuper* instead of IntPtr as the first parameter. Generated call sites create a stack-allocated ObjCSuper and pass &__objc_super__ instead of this.SuperHandle.

  • Updated handwritten super calls in 11 source files (CIFilter, CIVector, CALayer, UIPasteboard, NSInputStream, NSUuid, NSThread, NSHttpCookie, GCMouse, NSOpenGLPixelFormat, NSBitmapImageRep) plus NSObject2.cs.

  • NSObject.SuperHandle is preserved for backward compatibility (can be simplified/removed in XAMCORE_5_0).

Fixes #25362

πŸ€– Pull request created by Copilot

rolfbjarne and others added 3 commits May 8, 2026 12:32
Replace this.SuperHandle with a stack-allocated ObjCSuper struct
passed by pointer to objc_msgSendSuper calls. Add GC.KeepAlive (this)
after each super call to prevent premature collection.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add unsafe { } blocks around code that takes the address of
__objc_super__ in files where the enclosing method is not already
marked unsafe.

Files modified:
- src/CoreImage/CIVector.cs
- src/CoreImage/CIFilter.cs (2 locations)
- src/UIKit/UIPasteboard.cs (2 locations)
- src/Foundation/NSObject2.cs (3 locations)
- src/Foundation/NSHttpCookie.cs
- src/GameController/GCMouse.cs (2 locations)
- src/AppKit/NSBitmapImageRep.cs
- src/CoreAnimation/CALayer.cs

Files already in unsafe context (no changes needed):
- src/Foundation/NSInputStream.cs
- src/Foundation/NSThread.cs
- src/Foundation/NSUuid.cs
- src/AppKit/NSOpenGLPixelFormat.cs

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

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

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

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

βœ… .NET for Apple Platforms PR Reviewer completed successfully!

@github-actions github-actions Bot 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.

Review Summary

This PR successfully modernizes super call handling by replacing heap-allocated objc_super structures (via NSObject.SuperHandle) with stack-allocated ObjCSuper structs. The changes are well-executed and consistent across both generated and handwritten code.

βœ… Strengths

  1. Correct memory management: All super call sites properly create ObjCSuper on the stack and include GC.KeepAlive (this) to prevent premature collection
  2. Consistent pattern: The bgen generator correctly emits the new pattern with unsafe blocks, stack allocation, and GC safety
  3. Binary layout correctness: The ObjCSuper struct uses [StructLayout(LayoutKind.Sequential)] with the correct field order (receiver, classHandle) matching the native objc_super layout
  4. Backward compatibility: NSObject.SuperHandle is preserved (mentioned in PR description as simplifiable in XAMCORE_5_0)
  5. Proper P/Invoke signatures: All objc_msgSendSuper variants correctly updated to take ObjCSuper* instead of IntPtr

🎯 Findings

  • 3 suggestions for minor improvements to API design, code organization, and documentation

Key Changes Validated

  • βœ… ObjCSuper struct correctly implements objc_super layout
  • βœ… All handwritten super calls (11 files) updated consistently
  • βœ… Generator correctly emits unsafe blocks with stack allocation
  • βœ… GC.KeepAlive (this) present after all super calls
  • βœ… Return values properly captured before GC.KeepAlive where needed
  • βœ… Test expectations updated for size changes
  • βœ… CI checks passing (many completed, remainder in progress)

The implementation is sound and ready for merge after considering the minor suggestions above.

Generated by .NET for Apple Platforms PR Reviewer for issue #25376 Β· ● 7.1M

Comment thread src/ObjCRuntime/ObjCSuper.cs Resolved
Comment thread src/Foundation/NSObject2.cs Resolved
Comment thread src/bgen/Generator.cs Resolved
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

rolfbjarne and others added 3 commits May 14, 2026 19:48
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

βœ… [PR Build #7966a34] Build passed (Detect API changes) βœ…

Pipeline on Agent
Hash: 7966a3435d7b0527fddd04a77b2d64b8db6ac9b4 [PR build]

@rolfbjarne
rolfbjarne enabled auto-merge (squash) May 15, 2026 06:28
@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

βœ… [PR Build #7966a34] Build passed (Build packages) βœ…

Pipeline on Agent
Hash: 7966a3435d7b0527fddd04a77b2d64b8db6ac9b4 [PR build]

@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) Unable to create gist: Response status code does not indicate success: 502 (Bad Gateway). (raw diff) - Please review changes)

Pipeline on Agent
Hash: 7966a3435d7b0527fddd04a77b2d64b8db6ac9b4 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

βœ… [PR Build #7966a34] Build passed (Build macOS tests) βœ…

Pipeline on Agent
Hash: 7966a3435d7b0527fddd04a77b2d64b8db6ac9b4 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

πŸš€ [CI Build #7966a34] Test results πŸš€

Test results

βœ… All tests passed on VSTS: test results.

πŸŽ‰ All 175 tests passed πŸŽ‰

Tests counts

βœ… 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. [attempt 2] 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 6 tests passed. [attempt 2] Html Report (VSDrops) Download
βœ… linker: All 44 tests passed. Html Report (VSDrops) Download
βœ… monotouch (iOS): All 16 tests passed. [attempt 2] Html Report (VSDrops) Download
βœ… monotouch (MacCatalyst): All 18 tests passed. Html Report (VSDrops) Download
βœ… monotouch (macOS): All 18 tests passed. Html Report (VSDrops) Download
βœ… monotouch (tvOS): All 16 tests passed. Html Report (VSDrops) Download
βœ… msbuild: All 2 tests passed. Html Report (VSDrops) Download
βœ… sharpie: All 1 tests passed. (⚠️ Html Report Publish failed ⚠️) 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 Publish failed ⚠️) 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
βœ… Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

βœ… Linux build succeeded

Pipeline on Agent
Hash: 7966a3435d7b0527fddd04a77b2d64b8db6ac9b4 [PR build]

@rolfbjarne
rolfbjarne merged commit 66ef21c into main May 18, 2026
49 checks passed
rolfbjarne added a commit that referenced this pull request May 22, 2026
Store the super handle in a ConditionalWeakTable.

* Allocate native memory for it.
* Cache that native memory in a ConditionalWeakTable.
* Free it when the NSObject is freed (but not before).
* Obsolete the SuperHandle property in .NET 11, and remove it in XAMCORE_5_0.

This is less performant, but any performance issues will be alleviated by
the fact that NSObject.GetSuper () will be called much less after #25376.

The great advantage is that NSObjectData is now smaller, and fits in the
tagged memory returned by `ObjectiveCMarshal.CreateReferenceTrackingHandle`,
which will simplify memory management _a lot_ for CoreCLR.

Contributes towards #25383.

This is a continuation/simplification of #25405.
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.

Improve super calls

4 participants