lhmouse / mcfgthread

Cornerstone of the MOST efficient std::thread on Windows for mingw-w64
https://gcc-mcf.lhmouse.com/
Other
269 stars 28 forks source link

Make global data shareable between static libraries #70

Closed lhmouse closed 1 year ago

lhmouse commented 1 year ago

At the moment, we have some global states as static variables:

https://github.com/lhmouse/mcfgthread/blob/3768a1eda78fa280fcd1f6f22a85882c24178fbc/src/xglobals.c#L146

This is not a problem if mcfgthread is always linked as a shared library. But, there are people who prefer static linking. In the latter case, all static instances in an EXE and all DLLs it has loaded get distinct copies of these variables, and it is dangerous to pass thread handles, TLS key handles, etc. between them.

Therefore, I think we can place these data into a named shared memory region, so individual modules always refer to the same region.

lhmouse commented 1 year ago

The name of mapping of global data looks like

Local\__MCF_crt_xglobals_<PID>_<RANDOM>

where <PID> is the process ID and <RANDOM> is the 64-bit result of EncodePointer(-42) * 0x9E3779B97F4A7C15ULL [^1]. This value should be unique to each process, and not exploitable by others.

[^1]: 0x9E3779B97F4A7C15 is the 64-bit fixed-point value of the Golden Ratio.