Documentation on class header layouts? Specifically, x86

Background: this project aims to implement reflection on Swift Types. It provides mechanisms for dynamically creating instances of classes and structs, and support for KVC on them.

I'm trying to resolve this issue, a crash that only occurs on x86. It looks like class instances are being over or under-released, judging by the stack trace. They're initializing the retain count to 2 here. The class layout they're using can be found here.

I did find TypeLayout.rst, but it has left us with a mysterious TODO in the class layout section, and it doesn't provide any detailed/visual description of the layout.

Can anyone point me to a file that documents the layout of the class header on x86? Any help is appreciated :)

The object header layout is not public ABI and subject to arbitrary change, sorry. If you can, please use public entry points like swift_allocObject to set up your object instances.

Noted, thank you! I actually didn't know that, I thought it was fixed forever. But it makes sense when you think about it, classes are created by the runtime and nothing else touches the header but runtime functions.

I'll see if I can allocObject to work.

1 Like

I feel like the hardest part of this is going to be getting the symbol exposed to C. Advice on how to go about this without hard-coding the mangled name? Or is that the only option in a nutshell?

swift_allocObject should be exported as an unmangled symbol name:

$ nm /usr/lib/swift/libswiftCore.dylib |grep swift_allocObject
00000000002c9630 t 00000000002c9620 T _swift_allocObject

and it's using the C calling convention, so you can declare it with a compatible signature in your own C header.

2 Likes

What's the fix for id missing on linux? Would adding typedef void* id to a header do the trick? Or this?

typedef struct objc_object { } *id;

I would just use void* instead of id.