All of lore.kernel.org
 help / color / mirror / Atom feed
* RFC: Fix crash in dlerror()
@ 2014-02-07 21:50 Stefan Seefeld
  0 siblings, 0 replies; 28+ messages in thread
From: Stefan Seefeld @ 2014-02-07 21:50 UTC (permalink / raw)
  To: lttng-dev

[-- Attachment #1: Type: text/plain, Size: 1216 bytes --]

I have been looking into an issue with the malloc wrapper, where an
unsuccessful call to dlopen(), followed by a call to dlerror(), would
result in a segmentation fault when the malloc wrapper is being used.

The problem is the following:

The functions dlopen() and dlsym() make use of a global (though
thread-local) "result" structure to hold the error state, to allow a
subsequent call to dlerror() to report it.

As it turns out, dlerror() itself may implicitly call realloc(), which,
if it hasn't been used before, triggers our wrapper to call dlsym(). So,
while dlerror() inspects said result structure, dlsym() re-initializes
it, causing the crash...

This is arguably a bug in the dlfcn functions. The attached patch
attempts to fix this by moving the initialization of the realloc()
wrapper (i.e., the loading of the symbol) into the constructor. This
fixes the crash that I'm observing, but since none of these dependencies
are specified or documented, this change may cause other issues elsewhere.

Are there any objections to this approach ? If not, I'll submit a formal
patch for this.

Thanks,
        Stefan

-- 
Stefan Seefeld
CodeSourcery / Mentor Graphics
http://www.mentor.com/embedded-software/


[-- Attachment #2: patch --]
[-- Type: text/plain, Size: 805 bytes --]

diff --git a/liblttng-ust-libc-wrapper/lttng-ust-malloc.c b/liblttng-ust-libc-wrapper/lttng-ust-malloc.c
index 33ed18b..3f391db 100644
--- a/liblttng-ust-libc-wrapper/lttng-ust-malloc.c
+++ b/liblttng-ust-libc-wrapper/lttng-ust-malloc.c
@@ -54,6 +54,14 @@ static void *static_calloc(size_t nmemb, size_t size)
 	return &static_calloc_buf[prev_offset];
 }
 
+static void *(*plibc_realloc)(void *ptr, size_t size);
+
+__attribute__((constructor))
+static void init()
+{
+	plibc_realloc = dlsym(RTLD_NEXT, "realloc");
+}
+
 void *malloc(size_t size)
 {
 	static void *(*plibc_malloc)(size_t size);
@@ -119,7 +127,6 @@ void *calloc(size_t nmemb, size_t size)
 
 void *realloc(void *ptr, size_t size)
 {
-	static void *(*plibc_realloc)(void *ptr, size_t size);
 	void *retval;
 
 	if (plibc_realloc == NULL) {

[-- Attachment #3: Type: text/plain, Size: 155 bytes --]

_______________________________________________
lttng-dev mailing list
lttng-dev@lists.lttng.org
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

^ permalink raw reply related	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2014-02-14 15:12 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <52F55511.2080309@mentor.com>
2014-02-08 16:06 ` RFC: Fix crash in dlerror() Mathieu Desnoyers
     [not found] ` <1380151240.21051.1391875592403.JavaMail.zimbra@efficios.com>
2014-02-08 16:53   ` Stefan Seefeld
     [not found]   ` <52F66118.5010003@mentor.com>
2014-02-08 22:22     ` Mathieu Desnoyers
     [not found]     ` <905376946.21151.1391898148330.JavaMail.zimbra@efficios.com>
2014-02-09 16:28       ` Stefan Seefeld
     [not found]       ` <52F7ACB6.4060907@mentor.com>
2014-02-11  0:31         ` Mathieu Desnoyers
2014-02-11  0:53       ` Stefan Seefeld
     [not found]       ` <52F9746F.5070302@mentor.com>
2014-02-11 20:51         ` Mathieu Desnoyers
     [not found]         ` <1494953964.23233.1392151900002.JavaMail.zimbra@efficios.com>
2014-02-11 20:55           ` Stefan Seefeld
     [not found]           ` <52FA8E5C.40203@mentor.com>
2014-02-12  3:39             ` Mathieu Desnoyers
     [not found]             ` <975853026.23546.1392176395828.JavaMail.zimbra@efficios.com>
2014-02-12 14:35               ` Mathieu Desnoyers
     [not found]               ` <2141704629.23782.1392215703555.JavaMail.zimbra@efficios.com>
2014-02-12 21:59                 ` Mathieu Desnoyers
     [not found] ` <52FCAA28.6020708@mentor.com>
     [not found]   ` <1026072798.24303.1392297452496.JavaMail.zimbra@efficios.com>
     [not found]     ` <52FCCCD2.9050302@mentor.com>
     [not found]       ` <610029715.24333.1392300717731.JavaMail.zimbra@efficios.com>
     [not found]         ` <1692042945.24342.1392301795996.JavaMail.zimbra@efficios.com>
     [not found]           ` <1119459836.24348.1392302831554.JavaMail.zimbra@efficios.com>
     [not found]             ` <52FCE732.9090508@mentor.com>
2014-02-13 16:40               ` Mathieu Desnoyers
     [not found]               ` <581364674.24417.1392309613306.JavaMail.zimbra@efficios.com>
2014-02-13 16:51                 ` Woegerer, Paul
     [not found]                 ` <52FCF7F5.9070908@mentor.com>
2014-02-13 18:52                   ` Mathieu Desnoyers
     [not found]                   ` <934762932.24558.1392317539645.JavaMail.zimbra@efficios.com>
2014-02-13 19:44                     ` Woegerer, Paul
     [not found]                     ` <A30AF42E15BD64459E202697468DFFA77E387F33@EU-MBX-04.mgc.mentorg.com>
2014-02-13 22:06                       ` Woegerer, Paul
     [not found]                       ` <A30AF42E15BD64459E202697468DFFA77E389F61@EU-MBX-04.mgc.mentorg.com>
2014-02-13 22:44                         ` Stefan Seefeld
     [not found]                         ` <52FD4AB0.4060203@mentor.com>
2014-02-14  6:59                           ` Woegerer, Paul
2014-02-14 10:30                           ` Alexander Monakov
     [not found]                           ` <alpine.LNX.2.00.1402141420420.24828@monopod.intra.ispras.ru>
2014-02-14 11:35                             ` Woegerer, Paul
2014-02-14 11:54                             ` Woegerer, Paul
2014-02-14 13:45                             ` [PATCH] Force static_alloc setup to be written into memory Paul Woegerer
     [not found]                             ` <1392385505-13405-1-git-send-email-paul_woegerer@mentor.com>
2014-02-14 14:08                               ` Mathieu Desnoyers
2014-02-14 14:23                               ` Alexander Monakov
     [not found]                               ` <alpine.LNX.2.00.1402141816441.24828@monopod.intra.ispras.ru>
2014-02-14 14:39                                 ` Woegerer, Paul
2014-02-14 14:46                                 ` Stefan Seefeld
2014-02-14 15:12                                 ` Mathieu Desnoyers
2014-02-07 21:50 RFC: Fix crash in dlerror() Stefan Seefeld

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.