Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference to tcmalloc in leveldb's shared object causes crashes upon late loading leveldb #634

Open
Peaker opened this issue Nov 5, 2018 · 1 comment
Labels

Comments

@Peaker
Copy link

Peaker commented Nov 5, 2018

When dynamically loading leveldb's shared object, libtcmalloc (if enabled) is implicitly loaded too.
The loading of libtcmalloc overrides malloc, free and other libc functions.
This override is fine when it happens at the beginning of execution but causes 2 problems when it is late loaded (with dlopen after executable had started):

  • Memory previously allocated with malloc can be freed with tcmalloc, causing a crash
  • tcmalloc itself calls library code that may have already resolved its new/delete symbols to libc's whereas direct calls to new/delete in tcmalloc are always resolved to its own implementation.

To illustrate, compile&run this C++ program (fix the path to libleveldb)

#include <dlfcn.h>
#include <string>
int main() {
    std::string s; // This fixes libstdc++ new/delete to use malloc/free. No crash without it!
    dlopen("/usr/lib/x86_64-linux-gnu/libleveldb.so", RTLD_NOW); // directly loading tcmalloc causes the same problem
    return 0;
}

This bug had been hit many times, looks like:

[src/tcmalloc.cc:283] Attempt to free invalid pointer 0x562806a73fb0

commercialhaskell/stack#2926

https://stackoverflow.com/questions/50132801/debugging-a-failure-to-access-libleveldb-with-c-sharp-on-osx

junyanz/interactive-deep-colorization#26

lamdu/lamdu#104 (comment)

I suggest removing tcmalloc references in leveldb. Document that it is recommended for use in the user application, instead. This will allow safe late-loading of leveldb.

@yairchu
Copy link

yairchu commented Nov 5, 2018

In some more detail, the crash often happens inside MallocExtension::Initialize(), in the code inside #ifdef __GLIBC__, so reproduction with the example program above is possible under Linux, but on macOS, for example, it doesn't cause a crash.

@cmumford cmumford added the bug label Apr 13, 2019
tvh pushed a commit to tvh/homebrew-core that referenced this issue Nov 4, 2021
maochongxin pushed a commit to maochongxin/leveldb that referenced this issue Jul 21, 2022
* Update with instructions to build under Visual Studio

Fixes Issue google#634.

I spent 3 days trying to build this library under Visual Studio 2017, only to discover on has to link to `Shlwapi.lib`.

Became so frustrated with the docs that I added full build instructions for Visual Studio 2015, 2017 and Intel Comiler 2015 and 2019.

* Update headings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants