ObjFW is technically only valid C99, not C11 #125

Open
opened 2026-06-08 22:25:16 +02:00 by js · 3 comments
Owner

C11 made simply storing and reading a value from two threads undefined behavior. The runtime depends on this heavily, as for the dtable, it doesn't care if the old value is seen - the old, pre-initialized dtable will make the dispatch end up in the not found handler, which will try to initialize the class, which would then get a mutex and see it's already initialized, at this point making the store definitely visible to that thread. So this buys extremely fast dispatch for in the worst case trying to initialize a class from every thread once.

This all works fine and is safe, but C11 made this undefined behavior. So the compiler could decide to just break this in C11 mode, while it has to work in C99 mode. Options would be to always compile as C99 or to use the new C11 atomics to avoid it being UB - though that would require writing wrappers for C99 that for a simple store / load would do nothing extra there.

C11 made simply storing and reading a value from two threads undefined behavior. The runtime depends on this heavily, as for the dtable, it doesn't care if the old value is seen - the old, pre-initialized dtable will make the dispatch end up in the not found handler, which will try to initialize the class, which would then get a mutex and see it's already initialized, at this point making the store definitely visible to that thread. So this buys extremely fast dispatch for in the worst case trying to initialize a class from every thread once. This all works fine and is safe, but C11 made this undefined behavior. So the compiler could decide to just break this in C11 mode, while it has to work in C99 mode. Options would be to always compile as C99 or to use the new C11 atomics to avoid it being UB - though that would require writing wrappers for C99 that for a simple store / load would do nothing extra there.

Is it OK to use a library build as C99 like ObjFW in a C23 project?

I have always set my ObjC code to build with C23 (only because of auto, btw).

Is it OK to use a library build as C99 like ObjFW in a C23 project? I have always set my ObjC code to build with C23 (only because of `auto`, btw).
Author
Owner

This is only how ObjFW itself is compiled, this does not affect code using ObjFW.

This is only how ObjFW itself is compiled, this does not affect code using ObjFW.
Author
Owner

This might just be undefined behavior we have to live with that works in practice. Technically, we'd need to switch to C11 atomics, C11 mutexes etc. and only ever use those in order to not be undefined behavior.

ObjC always requires some undefined behavior anyway, such as casting function pointers to non-function pointers and vice versa. So this is probably just another UB we cannot easily get rid of.

I'll mark this as nice to have, as we could probably add a wrapper for atomics that gets defined to nothing on C99 and make sure we only ever use C11 threading - though I am not sure that is sufficient for everything we do.

This might just be undefined behavior we have to live with that works in practice. Technically, we'd need to switch to C11 atomics, C11 mutexes etc. and only ever use those in order to not be undefined behavior. ObjC always requires some undefined behavior anyway, such as casting function pointers to non-function pointers and vice versa. So this is probably just another UB we cannot easily get rid of. I'll mark this as nice to have, as we could probably add a wrapper for atomics that gets defined to nothing on C99 and make sure we only ever use C11 threading - though I am not sure that is sufficient for everything we do.
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ObjFW/ObjFW#125
No description provided.