[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [heaptrack/1.1] src/track: Call libc / libstdc++ freeres function in libheaptrack's atexit
From:       Milian Wolff <null () kde ! org>
Date:       2018-07-02 21:00:15
Message-ID: E1fa5vj-0003x7-6r () code ! kde ! org
[Download RAW message or body]

Git commit 5ff967ab12fda20834100ea3861d65d74221db89 by Milian Wolff.
Committed on 02/07/2018 at 20:58.
Pushed by mwolff into branch '1.1'.

Call libc / libstdc++ freeres function in libheaptrack's atexit

On one hand, this prevents an infinite recursion when the call to
atexit allocates, which happens e.g. with uClibc, since the
call from heaptrack_preload did not install the recursion guard yet.

On the other hand, this code move ensures that the freeres functions
get called properly before heaptrack_stop is called.

BUG: 395109

M  +0    -19   src/track/heaptrack_preload.cpp
M  +19   -0    src/track/libheaptrack.cpp

https://commits.kde.org/heaptrack/5ff967ab12fda20834100ea3861d65d74221db89

diff --git a/src/track/heaptrack_preload.cpp b/src/track/heaptrack_preload.cpp
index b776b18..054d50b 100644
--- a/src/track/heaptrack_preload.cpp
+++ b/src/track/heaptrack_preload.cpp
@@ -35,13 +35,6 @@ using namespace std;
 #define HAVE_ALIGNED_ALLOC 0
 #endif
 
-extern "C" {
-__attribute__((weak)) void __libc_freeres();
-}
-namespace __gnu_cxx {
-__attribute__((weak)) extern void __freeres();
-}
-
 namespace {
 
 namespace hooks {
@@ -123,18 +116,6 @@ void* dummy_calloc(size_t num, size_t size) noexcept
 
 void init()
 {
-    atexit([]() {
-        // free internal libstdc++ resources
-        // see also Valgrind's `--run-cxx-freeres` option
-        if (&__gnu_cxx::__freeres) {
-            __gnu_cxx::__freeres();
-        }
-        // free internal libc resources, cf: https://bugs.kde.org/show_bug.cgi?id=378765
-        // see also Valgrind's `--run-libc-freeres` option
-        if (&__libc_freeres) {
-            __libc_freeres();
-        }
-    });
     heaptrack_init(getenv("DUMP_HEAPTRACK_OUTPUT"),
                    [] {
                        hooks::calloc.original = &dummy_calloc;
diff --git a/src/track/libheaptrack.cpp b/src/track/libheaptrack.cpp
index 644a1ac..1c67a79 100644
--- a/src/track/libheaptrack.cpp
+++ b/src/track/libheaptrack.cpp
@@ -49,6 +49,13 @@
 #include "util/libunwind_config.h"
 #include "util/linewriter.h"
 
+extern "C" {
+__attribute__((weak)) void __libc_freeres();
+}
+namespace __gnu_cxx {
+__attribute__((weak)) extern void __freeres();
+}
+
 /**
  * uncomment this to get extended debug code for known pointers
  * there are still some malloc functions I'm missing apparently,
@@ -273,6 +280,18 @@ public:
                     return;
                 }
                 debugLog<MinimalOutput>("%s", "atexit()");
+
+                // free internal libstdc++ resources
+                // see also Valgrind's `--run-cxx-freeres` option
+                if (&__gnu_cxx::__freeres) {
+                    __gnu_cxx::__freeres();
+                }
+                // free internal libc resources, cf: https://bugs.kde.org/show_bug.cgi?id=378765
+                // see also Valgrind's `--run-libc-freeres` option
+                if (&__libc_freeres) {
+                    __libc_freeres();
+                }
+
                 s_atexit.store(true);
                 heaptrack_stop();
             });
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic