Skip to content

Double free error when mixing c++20 project with folly built using c++17 #2477

@chengji77

Description

@chengji77

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions