Open
Description
Example
type
X = object of RootObj
proc `=destroy`(x: var X) =
discard
proc main() =
var x: ref RootObj = (ref X)()
main()
Actual Output
Note that a recent clang version (>= 17) is required.
$ nim r --cc:clang --passC:-fsanitize=function --passL:-fsanitize=function test.nim
cache/nimskull/test_d/stdlib_system.nim.c:2046:3: runtime error: call to function eqdestroy___test_2 through pointer to incorrect function type 'void (*)(void *)'
Possible Solution
Generate generic thunks for hooks called via RTTI. For example
void eqdestroy_thunk___test_2(void* ptr) {
eqdestroy___test_2((X*) ptr);
}
Alternatively type erase eqdestroy
/eqtrace
parameters, but that spells trouble if anyone uses =destroy
/=trace
as function pointers.
References
Many other projects are also dealing with the fallout:
- UBSan: Calling a function through pointer to incorrect function type is undefined behavior python/cpython#111178
- systemd fails miserably with clang-17's
-fsanitize=function
systemd/systemd#29972 - Undefined behavior around calls to generic functions like OPENSSL_LH_HASHFUNC openssl/openssl#22896
Apparently this new UB warning has to do with CFI (Control Flow Integrity) protections.