On Mon, Apr 4, 2011 at 9:26 PM, Paul Pluzhnikov <[email protected]> wrote:
> On Mon, Apr 4, 2011 at 9:01 PM, Arun Sharma <[email protected]> wrote:
>
>> Could you also add a test case to Ltest-nomalloc, so we catch any
>> mallocs in the fast unwind path? Thanks.
>
> That may not be easy: AFAICT glibc pthread_setspecific doesn't call
> calloc all the time; only when there are more than
> PTHREAD_KEY_2NDLEVEL_SIZE (== 32) keys, and even then only the first
> time for a block of keys.
>
> I'll try to make a test case anyway.

Attached patch makes a new Ltest-nocalloc test, and shows 1 calloc call
from Lassi's backtrace() on Ubuntu/glibc-2.11/x86_64 system:

FAILURE: detected 1 error (malloc: 0, calloc: 1)

Just as a sanity check, I ran it in GDB, and confirmed the expected
stack trace:


Breakpoint 2, calloc (n=32, s=16) at ../../tests/Gtest-nocalloc.c:60
60        if (in_unwind) {
(gdb) bt
#0  calloc (n=32, s=16) at ../../tests/Gtest-nocalloc.c:60
#1  0x00007ffff79c5ac9 in __pthread_setspecific (key=100,
      value=0x7ffff7fb91f0) at pthread_setspecific.c:73
#2  0x00007ffff7fe4638 in trace_cache_get (cursor=0x7fffffffc6e0,
      buffer=0x7fffffffd2d0, size=0x7fffffffd29c) at
      ../../src/x86_64/Gtrace.c:175
#3  _ULx86_64_tdep_trace (cursor=0x7fffffffc6e0, buffer=0x7fffffffd2d0,
      size=0x7fffffffd29c) at ../../src/x86_64/Gtrace.c:424
#4  0x00007ffff7fe250e in unw_backtrace (buffer=0x7fffffffd2d0, size=100)
      at ../../src/mi/backtrace.c:69
#5  0x0000000000400a28 in do_backtrace () at ../../tests/Gtest-nocalloc.c:91
#6  0x0000000000400b2d in foo3 (argc=<value optimized out>, argv=<value
      optimized out>) at ../../tests/Gtest-nocalloc.c:98
#7  foo2 (argc=<value optimized out>, argv=<value optimized out>) at
      ../../tests/Gtest-nocalloc.c:104
#8  foo1 (argc=<value optimized out>, argv=<value optimized out>) at
      ../../tests/Gtest-nocalloc.c:110
#9  main (argc=<value optimized out>, argv=<value optimized out>) at
      ../../tests/Gtest-nocalloc.c:126


Now all we have to do is figure out how to fix it ;-)

Thanks,
-- 
Paul Pluzhnikov
diff --git a/tests/Gtest-nocalloc.c b/tests/Gtest-nocalloc.c
new file mode 100644
index 0000000..6f61b96
--- /dev/null
+++ b/tests/Gtest-nocalloc.c
@@ -0,0 +1,137 @@
+/* libunwind - a platform-independent unwind library
+   Copyright (C) 2011 Google, Inc
+	Contributed by Paul Pluzhnikov <[email protected]>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <execinfo.h>  /* for backtrace  */
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <pthread.h>
+#include <libunwind.h>
+
+#define panic(args...)				\
+	{ fprintf (stderr, args); exit (-1); }
+
+int verbose;
+int num_mallocs;
+int num_callocs;
+int in_unwind;
+
+void *
+calloc(size_t n, size_t s)
+{
+  static void * (*func)();
+
+#ifdef __GLIBC__
+  /* In glibc, dlsym() calls calloc. Calling dlsym(RTLD_NEXT, "calloc") here
+     causes infinite recursion.  Instead, we simply use it by its other
+     name.  */
+  extern void *__libc_calloc();
+  func = &__libc_calloc;
+#else
+  if(!func)
+    func = (void *(*)()) dlsym(RTLD_NEXT, "calloc");
+#endif
+
+  if (in_unwind) {
+    num_callocs++;
+    return NULL;
+  } else {
+    return func(n, s);
+  }
+}
+
+void *
+malloc(size_t s)
+{
+  static void * (*func)();
+
+  if(!func)
+    func = (void *(*)()) dlsym(RTLD_NEXT, "malloc");
+
+  if (in_unwind) {
+    num_mallocs++;
+    return NULL;
+  } else {
+    return func(s);
+  }
+}
+
+static void
+do_backtrace (void)
+{
+  const int num_levels = 100;
+  void *pc[num_levels];
+
+  in_unwind = 1;
+  backtrace(pc, num_levels);
+  in_unwind = 0;
+}
+
+void
+foo3 ()
+{
+  do_backtrace ();
+}
+
+void
+foo2 ()
+{
+  foo3 ();
+}
+
+void
+foo1 (void)
+{
+  foo2 ();
+  return NULL;
+}
+
+int
+main (int argc, char **argv)
+{
+  int i, num_errors;
+
+  /* Create (and leak) 100 TSDs, then call backtrace()
+     and check that it doesn't call malloc()/calloc().  */
+  for (i = 0; i < 100; ++i) {
+    pthread_key_t key;
+    if (pthread_key_create (&key, NULL))
+      panic ("FAILURE: unable to create key %d\n", i);
+  }
+  foo1 ();
+  num_errors = num_mallocs + num_callocs;
+  if (num_errors > 0)
+    {
+      fprintf (stderr,
+	       "FAILURE: detected %d error%s (malloc: %d, calloc: %d)\n",
+	       num_errors, num_errors > 1 ? "s" : "",
+	       num_mallocs, num_callocs);
+      exit (-1);
+    }
+  return 0;
+}
diff --git a/tests/Ltest-nocalloc.c b/tests/Ltest-nocalloc.c
new file mode 100644
index 0000000..8463dcf
--- /dev/null
+++ b/tests/Ltest-nocalloc.c
@@ -0,0 +1,5 @@
+#define UNW_LOCAL_ONLY
+#include <libunwind.h>
+#if !defined(UNW_REMOTE_ONLY)
+#include "Gtest-nocalloc.c"
+#endif
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c724e43..d5e3ae2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,7 +1,8 @@
 AM_CPPFLAGS = -I$(top_srcdir)/include
 
 EXTRA_DIST =	run-ia64-test-dyn1 run-ptrace-mapper run-ptrace-misc	\
-		run-check-namespace check-namespace.sh.in Gtest-nomalloc.c
+		run-check-namespace check-namespace.sh.in Gtest-nomalloc.c \
+		Gtest-nocalloc.c
 
 MAINTAINERCLEANFILES = Makefile.in
 
@@ -44,7 +45,7 @@ endif #ARCH_IA64
 			Gtest-trace Ltest-trace				 \
 			test-async-sig test-flush-cache test-init-remote \
 			test-mem test-setjmp test-ptrace		 \
-			Ltest-nomalloc rs-race
+			Ltest-nomalloc Ltest-nocalloc rs-race
  noinst_PROGRAMS_cdep = forker mapper test-ptrace-misc test-varargs	\
 			Gperf-simple Lperf-simple			\
 			Gperf-trace Lperf-trace
@@ -102,6 +103,7 @@ Gtest_bt_SOURCES = Gtest-bt.c ident.c
 Ltest_bt_SOURCES = Ltest-bt.c ident.c
 test_ptrace_misc_SOURCES = test-ptrace-misc.c ident.c
 Ltest_nomalloc_SOURCES = Ltest-nomalloc.c
+Ltest_nocalloc_SOURCES = Ltest-nocalloc.c
 Gtest_trace_SOURCES = Gtest-trace.c ident.c
 Ltest_trace_SOURCES = Ltest-trace.c ident.c
 
@@ -148,6 +150,7 @@ Ltest_dyn1_LDADD = $(LIBUNWIND_local)
 Ltest_exc_LDADD = $(LIBUNWIND_local)
 Ltest_init_LDADD = $(LIBUNWIND_local)
 Ltest_nomalloc_LDADD = $(LIBUNWIND_local) @DLLIB@
+Ltest_nocalloc_LDADD = $(LIBUNWIND_local) @DLLIB@ -lpthread
 Ltest_resume_sig_LDADD = $(LIBUNWIND)
 Lperf_simple_LDADD = $(LIBUNWIND_local)
 Ltest_trace_LDADD = $(LIBUNWIND_local)
_______________________________________________
Libunwind-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/libunwind-devel

Reply via email to