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?");
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");