Skip to content

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

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

[ObjCRuntime] Improve super calls - Fixes #25362#25376
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.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
Copy link
Copy Markdown
Member Author

/review

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 14, 2026

.NET for Apple Platforms PR Reviewer completed successfully!

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

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
Comment thread src/Foundation/NSObject2.cs Outdated
Comment thread src/bgen/Generator.cs
@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
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