From: Martin Quinson Date: Fri, 22 Jul 2022 21:24:27 +0000 (+0200) Subject: Another attempt at PRELOADing mmalloc (WIP) X-Git-Tag: v3.32~135 X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/8a3175c8d73996d77d1d06f85941bc60f5a9cf17 Another attempt at PRELOADing mmalloc (WIP) Try to have mmalloc both in the simgrid library (for the regular use) and also as an external library that can be LD_PRELOADed before sthread too. We'll have two copies of the library when actually preloading it in sthread, but in this case, we use dlsym(RTDL_NEXT) to find the first loaded symbol in memory to have only one mheap in the process. It still segfault when launching a sthread code within simgrid-mc as follows: bin/simgrid-mc --cfg=model-check/setenv:LD_PRELOAD=lib/libsgmalloc.so:lib/libsthread.soexamples/sthread/pthread-mutex-simple but at least it should fix the breakage found on FreeBSD. --- diff --git a/src/mc/api.cpp b/src/mc/api.cpp index ce024e6e62..e097a37de8 100644 --- a/src/mc/api.cpp +++ b/src/mc/api.cpp @@ -37,18 +37,10 @@ simgrid::mc::Exploration* Api::initialize(char** argv, const std::unordered_map< int i = 1; while (argv[i] != nullptr && argv[i][0] == '-') i++; - if (env.find("LD_PRELOAD") == env.end()) - setenv("LD_PRELOAD", "libsgmalloc.so", 1); for (auto const& [key, val] : env) { - if (key == "LD_PRELOAD") { - auto v2 = std::string("libsgmalloc.so:") + val; - XBT_INFO("setenv '%s'='%s'", key.c_str(), v2.c_str()); - setenv(key.c_str(), v2.c_str(), 1); - } else { - XBT_INFO("setenv '%s'='%s'", key.c_str(), val.c_str()); - setenv(key.c_str(), val.c_str(), 1); - } + XBT_INFO("setenv '%s'='%s'", key.c_str(), val.c_str()); + setenv(key.c_str(), val.c_str(), 1); } xbt_assert(argv[i] != nullptr, "Unable to find a binary to exec on the command line. Did you only pass config flags?"); diff --git a/src/xbt/mmalloc/mm_interface.c b/src/xbt/mmalloc/mm_interface.c index 28534b08fb..5f03243556 100644 --- a/src/xbt/mmalloc/mm_interface.c +++ b/src/xbt/mmalloc/mm_interface.c @@ -51,9 +51,3 @@ size_t mmalloc_get_bytes_used_remote(size_t heaplimit, const malloc_info* heapin } return bytes; } - -__attribute__((weak)) xbt_mheap_t mmalloc_get_current_heap(void) -{ - fprintf(stderr, "Fake mmalloc_get_current_heap()\n"); - return NULL; -} \ No newline at end of file diff --git a/src/xbt/mmalloc/mm_module.c b/src/xbt/mmalloc/mm_module.c index d8b53599ac..37f301b1d7 100644 --- a/src/xbt/mmalloc/mm_module.c +++ b/src/xbt/mmalloc/mm_module.c @@ -193,15 +193,23 @@ static void mmalloc_fork_child(void) xbt_mheap_t mmalloc_preinit(void) { if (__mmalloc_default_mdp == NULL) { - if (!mmalloc_pagesize) - mmalloc_pagesize = getpagesize(); - unsigned long mask = ~((unsigned long)mmalloc_pagesize - 1); - void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask); - __mmalloc_default_mdp = xbt_mheap_new(addr, XBT_MHEAP_OPTION_MEMSET); - - // atfork mandated at least on FreeBSD, or simgrid-mc will fail to fork the verified app - int res = pthread_atfork(mmalloc_fork_prepare, mmalloc_fork_parent, mmalloc_fork_child); - mmalloc_assert(res == 0, "pthread_atfork() failed: return value %d", res); + xbt_mheap_t (*other)(void) = dlsym(RTLD_NEXT, "mmalloc_preinit"); + if (other != NULL) { // This is the second time that this module is loaded, let's use the other one + __mmalloc_default_mdp = other(); + // fprintf(stderr, "Reuse the other mmalloc_init\n"); + } else { + // fprintf(stderr, "New mmalloc_init\n"); + + if (!mmalloc_pagesize) + mmalloc_pagesize = getpagesize(); + unsigned long mask = ~((unsigned long)mmalloc_pagesize - 1); + void* addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask); + __mmalloc_default_mdp = xbt_mheap_new(addr, XBT_MHEAP_OPTION_MEMSET); + + // atfork mandated at least on FreeBSD, or simgrid-mc will fail to fork the verified app + int res = pthread_atfork(mmalloc_fork_prepare, mmalloc_fork_parent, mmalloc_fork_child); + mmalloc_assert(res == 0, "pthread_atfork() failed: return value %d", res); + } } mmalloc_assert(__mmalloc_default_mdp != NULL, "__mmalloc_default_mdp cannot be NULL"); diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 813d8292de..ae045cefa4 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -289,6 +289,7 @@ set(XBT_SRC if(HAVE_MMALLOC) set(SGMALLOC_SRC src/xbt/mmalloc/mm.c) + set(XBT_SRC ${XBT_SRC} src/xbt/mmalloc/mm.c) else() set(EXTRA_DIST ${EXTRA_DIST} src/xbt/mmalloc/mm.c) endif()