Skip to content

Conversation

@Nirlah
Copy link
Contributor

@Nirlah Nirlah commented Jun 28, 2025

This fix adds support for tagged pointers, an internal optimization used by the Objective-C runtime for small objects like short NSStrings and NSNumbers. These “pointers” may not be aligned, as they encode data directly within the pointer value itself.

To support tagged pointers safely, the code now explicitly disables Zig’s runtime alignment safety checks when casting such values. This avoids runtime panics caused by misaligned pointer dereferencing.

The issue was observed when interacting with an Objective-C API that returned a NSString, which turned out to be a tagged pointer (and not a real heap pointer). The previous implementation assumed proper alignment and thus crashed during a cast operation safety check.

In some cases, Objective-C may use unaligned pointers.
This fix accommodates for such case.
@mitchellh
Copy link
Owner

mitchellh commented Jun 28, 2025

Is it possible to force a tagged pointer somehow so we can have a unit test for this? Calling the real ObjC APIs, our tests are macOS only anyways.

@Nirlah Nirlah marked this pull request as draft June 28, 2025 14:06
@Nirlah
Copy link
Contributor Author

Nirlah commented Jun 28, 2025

I'll look into it...
Turning the PR into a draft in the meanwhile.

@Nirlah
Copy link
Contributor Author

Nirlah commented Jun 28, 2025

This one is a bit tricky to test, but I think this should work...

Note that in the unlikely event that the perquisites for the test are unmet the test is skipped and a warning is logged.
@mitchellh Is this an acceptable behavior or should I change it?

@Nirlah Nirlah marked this pull request as ready for review June 28, 2025 17:08
@mitchellh
Copy link
Owner

No that’s absolutely excellent work thank you.

@mitchellh mitchellh merged commit 43d0758 into mitchellh:main Jun 28, 2025
1 check passed
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.

2 participants