Am Donnerstag, dem 04.09.2025 um 17:24 -0700 schrieb Kees Cook: > To support the KCFI type-id which needs to convert unique function > prototypes into unique 32-bit values, add a subset of the Itanium C++ > mangling ABI for C typeinfo of function prototypes, but then do > hashing, which is needed by KCFI to get a 32-bit hash value for a > given function prototype. Optionally report the mangled string > to the dumpfile. > > Trying to extract only the C portions of the gcc/cp/mangle.cc code > seemed infeasible after a few attempts. So this is the minimal subset > of the mangling ABI needed to generate unique KCFI type ids. > > I could not find a way to build a sensible selftest infrastructure for > this code. I wanted to do something like this: > > #ifdef CHECKING_P > const char code[] = " > typedef struct { int x, y } xy_t; > extern int func(xy_t *p); > "; > > ASSERT_MANGLE (code, "_ZTSPFiP4xy_tE"); > ... > #endif > > But I could not find any way to build a localized parser that could > parse the "code" string from which I could extract the "func" fndecl. > It would have been so much nicer to build the selftest directly into > mangle.cc here, but I couldn't figure it out. Instead, later patches > create a "kcfi" dump file, and the large kcfi testsuite validates > expected mangle strings as part of the type-id validation.
(BTW: I somewhere have a half-finished patch for two builtins, one that one parse a string and return a type and one which would serialize a type into a string. But it wasn't finished and I am not sure this would be helpful.) I have two more general questions about this code. Is this mangling be intended to be compatible with C's type system? Is it meant to be stable, or can it be changed later? If it is meant to be compatible with C's rules, you need to be careful when to encode the size of an array, as, char[] and char[5] are compatible types. So in many cases, one would have to remove the size to not have errors when calling a function via a compatible type that is slightly different. Martin