ivmai / bdwgc

The Boehm-Demers-Weiser conservative C/C++ Garbage Collector (bdwgc, also known as bdw-gc, boehm-gc, libgc)
https://www.hboehm.info/gc/
Other
2.97k stars 407 forks source link

is using LD_PRELOAD safe? #481

Open srepmub opened 1 year ago

srepmub commented 1 year ago

hi,

I'm a long time user of the BDWGC, and use it as part of a restricted-Python-to-C++ compiler:

https://github.com/shedskin/shedskin

I was wondering if it is safe to LD_PRELOAD a malloc substitute (since BDWGC uses malloc in the end?). in a simple benchmark where lots of small objects are allocated, it does seem to help a lot to use for example libjemalloc.

ivmai commented 1 year ago

since BDWGC uses malloc in the end?

BDWGC uses mmap to get memory from OS (on Linux). It uses calloc (to get the memory from OS) on some rare targets only (where no other better way to).

ivmai commented 1 year ago

is safe to LD_PRELOAD a malloc substitute

If I understand you correctly you mean building the collector with --enable-redirect-malloc and passing libgc.so to LD_PRELOAD. I can't say about reliability, It works on some targets (e.g. Linux). You'd better to try on your platform. If your target should support multi-threaded apps, please check that pthread_create (and friends) are redirected to GC_pthread_create (it should be redirected but worth to check anyway because lack of the redirection cases random errors at runtime because the collector should know all threads manipulating with pointers to the heap).