Documentation on class header layouts? Specifically, x86

runtime
(Tanner Bennett) #1

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 :)

(Joe Groff) #2

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.

(Tanner Bennett) #3

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
(Tanner Bennett) #4

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?

(Joe Groff) #5

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
(Tanner Bennett) #6

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;
(Joe Groff) #7

I would just use void* instead of id.