If folly is built with c++17, but the project using folly is built using c++20, when jemalloc is enabled, folly::usingJEMalloc() has double free error at folly/memory/Malloc.h#L192.
Compiler is clang 18.
Minimal code to reproduce:
int main(int argc, char **argv) {
const folly::Init init(&argc, &argv);
std::cout << "use jemalloc: " << folly::usingJEMalloc() << std::endl;
return 0;
}
folly::usingJEMalloc() should return true but actually it returns false.
This is because template class FastStaticBool<Initializer> is initialized twice using different definitions under c++17 and c++20 (this violates ODR and causes UB). As a result, Initializer::operator()() is called twice. For reasons I cannot understand, ptr is declared as static at folly/memory/Malloc.h#L186. It's allocated once but freed twice.
This section of code is excluded if FOLLY_SANITIZE defined, which makes it escaped from sanitizer checks.
Apart from that strange static declaration on ptr, I think we should avoid the template being defined differently. A possible fix is to move FOLLY_CPLUSPLUS macro from Portability.h to folly-config.h, and set its value to the C++ version used when compiling folly.
If folly is built with c++17, but the project using folly is built using c++20, when jemalloc is enabled,
folly::usingJEMalloc()has double free error at folly/memory/Malloc.h#L192.Compiler is clang 18.
Minimal code to reproduce:
folly::usingJEMalloc()should return true but actually it returns false.This is because template class
FastStaticBool<Initializer>is initialized twice using different definitions under c++17 and c++20 (this violates ODR and causes UB). As a result,Initializer::operator()()is called twice. For reasons I cannot understand,ptris declared as static at folly/memory/Malloc.h#L186. It's allocated once but freed twice.This section of code is excluded if
FOLLY_SANITIZEdefined, which makes it escaped from sanitizer checks.Apart from that strange
staticdeclaration onptr, I think we should avoid the template being defined differently. A possible fix is to moveFOLLY_CPLUSPLUSmacro fromPortability.htofolly-config.h, and set its value to the C++ version used when compiling folly.