From f7ce1e63676bc69240bda151ac0fda70b478aa5a Mon Sep 17 00:00:00 2001 From: Moritz Maxeiner Date: Sun, 17 Sep 2017 15:46:49 +0200 Subject: [PATCH 1/4] Fix leaking memory for storing DSO dependencies in shared druntime --- src/rt/sections_elf_shared.d | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/rt/sections_elf_shared.d b/src/rt/sections_elf_shared.d index eb1c752ccf..ad56b16186 100644 --- a/src/rt/sections_elf_shared.d +++ b/src/rt/sections_elf_shared.d @@ -456,6 +456,8 @@ extern(C) void _d_dso_registry(CompilerDSOData* data) unsetDSOForHandle(pdso, pdso._handle); pdso._handle = null; + + pdso._deps.reset(); } else { From 8384a458259a714ed61a80b37b1807300a355302 Mon Sep 17 00:00:00 2001 From: Moritz Maxeiner Date: Sun, 17 Sep 2017 16:55:25 +0200 Subject: [PATCH 2/4] Free _handleToDSO in shared druntime at shutdown --- src/rt/sections_elf_shared.d | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/rt/sections_elf_shared.d b/src/rt/sections_elf_shared.d index ad56b16186..dbb33e7676 100644 --- a/src/rt/sections_elf_shared.d +++ b/src/rt/sections_elf_shared.d @@ -468,7 +468,17 @@ extern(C) void _d_dso_registry(CompilerDSOData* data) freeDSO(pdso); - if (_loadedDSOs.empty) finiLocks(); // last DSO + if (_loadedDSOs.empty) + { + // last DSO + version (Shared) + { + !pthread_mutex_lock(&_handleToDSOMutex) || assert(0); + _handleToDSO.reset(); + !pthread_mutex_unlock(&_handleToDSOMutex) || assert(0); + } + finiLocks(); + } } } From 89ffe18dfdc29f359ad1b86bc149379bc9e640f6 Mon Sep 17 00:00:00 2001 From: Martin Nowak Date: Sat, 23 Sep 2017 13:50:01 +0200 Subject: [PATCH 3/4] move dso cleanup to freeDSO --- src/rt/sections_elf_shared.d | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/rt/sections_elf_shared.d b/src/rt/sections_elf_shared.d index dbb33e7676..b83ddde8b5 100644 --- a/src/rt/sections_elf_shared.d +++ b/src/rt/sections_elf_shared.d @@ -455,9 +455,6 @@ extern(C) void _d_dso_registry(CompilerDSOData* data) } unsetDSOForHandle(pdso, pdso._handle); - pdso._handle = null; - - pdso._deps.reset(); } else { @@ -611,7 +608,12 @@ version (Shared) void runFinalizers(DSO* pdso) void freeDSO(DSO* pdso) nothrow @nogc { pdso._gcRanges.reset(); - version (Shared) pdso._codeSegments.reset(); + version (Shared) + { + pdso._codeSegments.reset(); + pdso._deps.reset(); + pdso._handle = null; + } .free(pdso); } From c04a50624b072c4b76e3644b4951a617bfe7de8b Mon Sep 17 00:00:00 2001 From: Martin Nowak Date: Sat, 23 Sep 2017 13:50:10 +0200 Subject: [PATCH 4/4] no need for locking when runtime gets unloaded --- src/rt/sections_elf_shared.d | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/rt/sections_elf_shared.d b/src/rt/sections_elf_shared.d index b83ddde8b5..506ca29393 100644 --- a/src/rt/sections_elf_shared.d +++ b/src/rt/sections_elf_shared.d @@ -465,14 +465,13 @@ extern(C) void _d_dso_registry(CompilerDSOData* data) freeDSO(pdso); + // last DSO being unloaded => shutdown registry if (_loadedDSOs.empty) { - // last DSO version (Shared) { - !pthread_mutex_lock(&_handleToDSOMutex) || assert(0); + assert(_handleToDSO.empty); _handleToDSO.reset(); - !pthread_mutex_unlock(&_handleToDSOMutex) || assert(0); } finiLocks(); }