Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[registrar] Fix registration of stub Objective-C classes. Fixes #21546. #21593

Merged
merged 4 commits into from
Dec 30, 2024

Conversation

rolfbjarne
Copy link
Member

@rolfbjarne rolfbjarne commented Nov 7, 2024

Objective-C has a fairly new feature called stub (unrealized) classes, which don't exist until runtime. They were introduced to support Swift classes exposed to Objective-C, when the exact layout of the class isn't known at compile time, and the creation of the class itself is deferred until runtime.

An Objective-C class is marked as a stub class by adding the objc_class_stub attribute to the class definition:

__attribute__((objc_class_stub)
@interface ObjCStubClass : NSObject
@end

Sidenote: the class will also have the objc_subclassing_restricted attribute, because Clang requires the latter when the former is present.

When this attribute is present, Clang will generate code to load the class at runtime, instead of just fetching it, when doing [ObjCStubClass class].

Our immediate symptom of stub classes when using the static registrar is that the above would crash:

  • We don't have the original headers for any third-party Objective-C code, so we generate it based on the information we have available.
  • We don't add the objc_class_stub attribute to the generated Objective-C header (because we don't know that the class is a stub class).
  • Clang compiles the wrong code for [ObjCStubClass class], because of the missing attribute.

The first step in the fix is to add a new IsStubClass property to the Register attribute, so that we can let the registrars know about them.

Then update the static registrar to add the objc_class_stub attribute to the generated Objective-C header.

We also need a fix for the dynamic registrar, because we call the objc_getClass ("ObjCStubClass") there to get the Objective-C class, and that doesn't work unless the stub class has already been realized/used (objc_getClass returns NULL). It turns out that computing the total number of Objective-C classes in the process will also realize all stub Objective-C classes, so that's what we do when we find a stub Objective-C class.

References:

Fixes #21546.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🔥 [CI Build] Build failed 🔥

Build failed for the job 'Build macOS tests'

Pipeline on Agent
Hash: 7dba6e3c1dcdde830ded271c72152283718fc0b9 [PR build]

@rolfbjarne rolfbjarne force-pushed the dev/rolf/issue-21546-dynamic-class-fetch branch from 7dba6e3 to 3ebd719 Compare December 10, 2024 08:36
@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 rolfbjarne force-pushed the dev/rolf/issue-21546-dynamic-class-fetch branch from 3ebd719 to 597c77d Compare December 12, 2024 15:24
@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.

Copy link
Contributor

⚠️ Your code has been reformatted. ⚠️

If this is not desired, add the actions-disable-autoformat label, and revert the reformatting commit.

If files unrelated to your change were modified, try reverting the reformatting commit + merging with the target branch (and push those changes).

@github-actions github-actions bot force-pushed the dev/rolf/issue-21546-dynamic-class-fetch branch from 10febf8 to ec3143f Compare December 20, 2024 11:14
@rolfbjarne
Copy link
Member Author

/azp run xamarin-macios-pr

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@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 rolfbjarne marked this pull request as ready for review December 20, 2024 16:33
@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
Member Author

/azp run xamarin-macios-pr,xamarin-macios-pr-apidiff

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: 327969584f21b30013f47fd883440d7ab4fb14ca [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 327969584f21b30013f47fd883440d7ab4fb14ca [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 327969584f21b30013f47fd883440d7ab4fb14ca [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: 327969584f21b30013f47fd883440d7ab4fb14ca [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Tests on macOS X64 - Mac Sonoma (14) passed 💻

All tests on macOS X64 - Mac Sonoma (14) passed.

Pipeline on Agent
Hash: 327969584f21b30013f47fd883440d7ab4fb14ca [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Tests on macOS arm64 - Mac Sequoia (15) passed 💻

All tests on macOS arm64 - Mac Sequoia (15) passed.

Pipeline on Agent
Hash: 327969584f21b30013f47fd883440d7ab4fb14ca [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Tests on macOS M1 - Mac Ventura (13) passed 💻

All tests on macOS M1 - Mac Ventura (13) passed.

Pipeline on Agent
Hash: 327969584f21b30013f47fd883440d7ab4fb14ca [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

.NET (No breaking changes)

✅ API diff vs stable

.NET (No breaking changes)

ℹ️ Generator diff

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

Pipeline on Agent
Hash: 327969584f21b30013f47fd883440d7ab4fb14ca [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: 327969584f21b30013f47fd883440d7ab4fb14ca [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🔥 [CI Build] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 1 tests failed, 106 tests passed.

Failures

❌ generator tests

1 tests failed, 4 tests passed.
  • Roslyn Generator tests/.NET: Failed (Execution failed with exit code 1)

Html Report (VSDrops) Download

Successes

✅ 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
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker: All 40 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 8 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 10 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 9 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 8 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 327969584f21b30013f47fd883440d7ab4fb14ca [PR build]

@rolfbjarne
Copy link
Member Author

Test failure is unrelated (#21864).

@rolfbjarne rolfbjarne merged commit 913ffab into main Dec 30, 2024
17 checks passed
@rolfbjarne rolfbjarne deleted the dev/rolf/issue-21546-dynamic-class-fetch branch December 30, 2024 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

iOS binding library - Crash when app launches
3 participants