summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArun Sharma <aruns@google.com>2009-03-15 11:24:43 -0700
committerArun Sharma <aruns@google.com>2009-03-16 21:34:48 -0700
commit576b59e4b15ddf2f2d3f17036b9c2f43c7504777 (patch)
treeae313c6d7b99b81d85d49a6b68475031d9e50a2f
parenta2c27a4ab724cf129cf956df4c6fb779bd2f5366 (diff)
downloadlibunwind-576b59e4b15ddf2f2d3f17036b9c2f43c7504777.tar.gz
Verify that we don't call malloc when unwinding locally.
-rw-r--r--tests/Gtest-nomalloc.c114
-rw-r--r--tests/Ltest-nomalloc.c5
-rw-r--r--tests/Makefile.am7
3 files changed, 125 insertions, 1 deletions
diff --git a/tests/Gtest-nomalloc.c b/tests/Gtest-nomalloc.c
new file mode 100644
index 00000000..70faa368
--- /dev/null
+++ b/tests/Gtest-nomalloc.c
@@ -0,0 +1,114 @@
+/* libunwind - a platform-independent unwind library
+ Copyright (C) 2009 Google, Inc
+ Contributed by Arun Sharma <arun.sharma@google.com>
+
+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 <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <libunwind.h>
+
+#define panic(args...) \
+ { fprintf (stderr, args); exit (-1); }
+
+int verbose;
+int num_errors;
+int in_unwind;
+
+void *
+malloc(size_t s)
+{
+ static void * (*func)();
+
+ if(!func)
+ func = (void *(*)()) dlsym(RTLD_NEXT, "malloc");
+
+ if (in_unwind) {
+ num_errors++;
+ return NULL;
+ } else {
+ return func(s);
+ }
+}
+
+static void
+do_backtrace (void)
+{
+ unw_word_t ip, sp;
+ unw_cursor_t cursor;
+ unw_context_t uc;
+ int ret;
+
+ in_unwind = 1;
+ unw_getcontext (&uc);
+ if (unw_init_local (&cursor, &uc) < 0)
+ panic ("unw_init_local failed!\n");
+
+ do
+ {
+ unw_get_reg (&cursor, UNW_REG_IP, &ip);
+ unw_get_reg (&cursor, UNW_REG_SP, &sp);
+
+ ret = unw_step (&cursor);
+ if (ret < 0)
+ {
+ ++num_errors;
+ }
+ }
+ while (ret > 0);
+ in_unwind = 0;
+}
+
+void
+foo3 ()
+{
+ do_backtrace ();
+}
+
+void
+foo2 ()
+{
+ foo3 ();
+}
+
+void
+foo1 ()
+{
+ foo2 ();
+}
+
+int
+main (int argc, char **argv)
+{
+ foo1();
+
+ if (num_errors > 0)
+ {
+ fprintf (stderr, "FAILURE: detected %d errors\n", num_errors);
+ exit (-1);
+ }
+ return 0;
+}
diff --git a/tests/Ltest-nomalloc.c b/tests/Ltest-nomalloc.c
new file mode 100644
index 00000000..74d63312
--- /dev/null
+++ b/tests/Ltest-nomalloc.c
@@ -0,0 +1,5 @@
+#define UNW_LOCAL_ONLY
+#include <libunwind.h>
+#if !defined(UNW_REMOTE_ONLY)
+#include "Gtest-nomalloc.c"
+#endif
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 073a33cd..f43dc609 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -40,7 +40,8 @@ endif #ARCH_IA64
Gtest-resume-sig Ltest-resume-sig \
Gtest-dyn1 Ltest-dyn1 \
test-async-sig test-flush-cache test-init-remote \
- test-mem test-setjmp test-ptrace
+ test-mem test-setjmp test-ptrace \
+ Ltest-nomalloc
noinst_PROGRAMS_cdep = forker mapper test-ptrace-misc test-varargs \
Gperf-simple Lperf-simple
@@ -98,3 +99,7 @@ test_ptrace_LDADD = ../src/libunwind-ptrace.a $(LIBUNWIND)
Ltest_concurrent_LDADD = $(LIBUNWIND) -lpthread
Gtest_concurrent_LDADD = $(LIBUNWIND) -lpthread
test_async_sig_LDADD = $(LIBUNWIND) -lpthread
+
+LDADD += -ldl
+Ltest_nomalloc_SOURCES = Ltest-nomalloc.c
+