1
0
Fork 0
mirror of https://github.com/tobast/libunwind-eh_elf.git synced 2024-10-01 02:49:29 +02:00
Commit graph

164 commits

Author SHA1 Message Date
Arun Sharma
2f328202ee Fix a compiler warning
Signed-off-by: Paul Pluzhnikov <ppluzhnikov@google.com>
2011-12-01 22:08:08 -08:00
Arun Sharma
1010880548 Address x86_64 crashes when using sigaltstack
The crashes were tracked down to f->rpb_cfa_offset being incorrect.

The problem is that {rsp,rbp}_cfa_offset only have 15 bits, but for
SIGRETURN frame they are filled with:

// src/x86_64/Gstash_frame.c

   f->cfa_reg_offset = d->cfa - c->sigcontext_addr;
   f->rbp_cfa_offset = DWARF_GET_LOC(d->loc[RBP]) - d->cfa;
   f->rsp_cfa_offset = DWARF_GET_LOC(d->loc[RSP]) - d->cfa;

The problem is that the delta here can be arbitrarily large when
sigaltstack is used, and can easily overflow the 15 and 30-bit fields.

When signal handler starts running, the stack layout is:

 ... higher addresses ...
        ucontext
 CFA->
        __restore_rt (== pretcode in rt_sigframe from
                      linux-2.6/arch/x86/include/asm/sigframe.h)
 SP ->
       ... sighandler runs on this stack.

 ... lower addresses ...

This makes it very convenient to find ucontext from the CFA.

Attached patch re-tested on Linux/x86_64, no new failures.

Signed-off-by: Paul Pluzhnikov <ppluzhnikov@google.com>
Reviwed-by: Lassi Tuura <lat@cern.ch>
2011-11-27 18:34:38 -08:00
Arun Sharma
0a26727ea2 Fix TLS destructor ordering problems
Glibc calls thread-specific dtors in the order in which the keys were added,
so the first dtor is the trace_cache_free() one. Then thread-specific
data for some other key is free()d, which calls into unw_backtrace(),
which uses dangling cache and munmapped cache->frames.

[ Minor rename + compiler warning fix: asharma@fb.com ]
Signed-off-by: Paul Pluzhnikov <ppluzhnikov@google.com>
2011-10-29 17:12:36 -07:00
Arun Sharma
08077a4962 pthread_once() workaround for FreeBSD and Solaris
On FreeBSD, as well as on the Solaris < 10, weak pthread_once stub is
always exported from libc. But it does nothing, which means that if
threaded library is not loaded, then pthread_once() call do not actually
call the initializer finction. The construct
  if (likely (pthread_once != 0))
  {
    pthread_once(&trace_cache_once, &trace_cache_init_once);
then fails to initialize the trace cache on x86_64.

Work around by checking that the initializer was indeed called.
Note that this can break if libthr is loaded dynamically, but my belief
is that there is no platforms which allow dynamic loading of the threading
library.
2011-10-29 16:53:30 -07:00
Arun
f89fb17695 Fix incorrect calls to memset.
Found when compiling libunwind with clang.

Signed-off-by: Paul Pluzhnikov <ppluzhnikov@google.com>
2011-10-02 22:43:28 -07:00
Arun Sharma
e09f9701ff Handle register nums > 16 on x86_64
gcc generates them when using ms-abi. Support disabled by
default since it increases the cache footprint of the library.
2011-06-18 20:48:49 -07:00
Lassi Tuura
d2525ec936 Use single level hash table for fast trace. 2011-05-06 22:09:07 -07:00
Lassi Tuura
5c2cade264 Inline access to initial register values as it's known to be safe. 2011-05-06 20:19:36 -07:00
Lassi Tuura
ae5c1f2adf Performance optimisations for fast trace.
Insert static branch prediction predicates in useful places and avoid
unnecessary code in the hottest paths. Bypass unnecessary indirect
calls, in particular to access_mem(), when known to be safe.
2011-04-17 20:34:38 -07:00
Arun Sharma
e2962af9d3 Implement a cheaper getcontext()
Since the fast unwinding code path doesn't need the full context,
a faster target dependent getcontext is implemented.

Signed-off-by: Lassi Tuura <lat@cern.ch>
2011-04-05 22:07:05 -07:00
Arun Sharma
15f182828d Use __thread instead of pthread_getspecific() 2011-04-05 22:06:51 -07:00
Lassi Tuura
5f38f35d5d Drop a call frame in tdep_trace and avoid a call to unw_step.
Dropping the extra frame for unw_backtrace itself using unw_step is
approximately 15% slower than skipping the frame in tdep_trace.  So
drop the frame in the latter, and make the function a private
implementation detail for libunwind, not an exported interface.

Also moves unw_getcontext call back into unw_backtrace to avoid an
extra call frame in case slow_backtrace does not get inlined into
unw_backtrace.
2011-04-01 00:00:39 -07:00
Lassi Tuura
3b9fd99cb7 Assign copyright as requested by the author. 2011-03-25 00:20:49 -07:00
Lassi Tuura
f1ea02be58 Reset 'used' to zero after expanding frame cache hash table. 2011-03-25 00:20:48 -07:00
Lassi Tuura
44a14d1364 Integrate fast trace into backtrace(). 2011-03-24 22:33:55 -07:00
Lassi Tuura
9e98f15e9a Fast back-trace for x86_64 for only collecting the call stack.
Adds new function to perform a pure stack walk without unwinding,
functionally similar to backtrace() but accelerated by an address
attribute cache the caller maintains across calls.
2011-03-24 22:33:17 -07:00
Lassi Tuura
28f33c8ce0 Auto-detect whether to use msync() or mincore() for address validation. 2011-03-24 21:02:28 -07:00
Arun Sharma
99e60be5a4 Fix the page boundary crossing bug.
Signed-off-by: Jason Evans <jasone@canonware.com>
2010-06-08 14:44:07 -07:00
Konstantin Belousov
e1493dc011 Merge branch 'master' into freebsd 2010-05-03 17:59:50 +03:00
Arun Sharma
aa3bb307a3 Fix merge errors 2010-05-03 07:47:41 -07:00
Konstantin Belousov
b73c618a31 Merge branch 'master' into freebsd 2010-05-02 16:29:02 +03:00
Arun Sharma
0176c4f723 Merge commit 'fdc534ccb05d1e6c0438345d292203343a811701' 2010-04-28 17:27:38 -07:00
Arun Sharma
fdc534ccb0 Silence a compiler warning 2010-04-28 16:38:27 -07:00
Arun Sharma
215afa30b7 Add unwind descriptors to getcontext.S 2010-04-28 16:32:30 -07:00
Arun Sharma
c140d8598d Undo commit f252f5ff4e for now.
Seems to introduce a couple of test breakages.
2010-04-25 09:44:59 -07:00
Arun Sharma
d3c4bc4295 Fix a compiler warning 2010-04-25 09:24:40 -07:00
Lassi Tuura
045c55b2a2 Be conservative in all pointer derefrences by default.
Since most people can't completely control their compile or runtime
environment, it becomes hard to ensure that unwind data is perfect.
2010-04-24 19:24:49 -07:00
Lassi Tuura
d4fbc8326a Detect end of stack in x86-64 rbp-based walk. 2010-04-24 19:22:08 -07:00
Lassi Tuura
92cc7fd78a Remove obsolete code for 'old way' of handling x86-64 signal frames. 2010-04-24 19:21:52 -07:00
Lassi Tuura
f252f5ff4e Recognise and unwind through PLT. 2010-04-24 19:21:26 -07:00
Lassi Tuura
a9dce3c06e During the stack unwinding process, the return address points to
the instruction after the call for a normal frame. libunwind uses
IP-1 to lookup unwind information. However, this is not necessary for
interrupted frames such as signal frames (or interrupt frames) in
the kernel context.

This patch handles both cases correctly.

Based on work by Mark Wielaard <mwielaard@redhat.com>
2010-04-24 19:16:09 -07:00
Lassi Tuura
dac2d001af Identify signal frames by augmentation attribute. 2010-04-24 17:18:26 -07:00
Konstantin Belousov
58f290e1ce Add config.h. 2010-04-21 15:43:05 +03:00
Konstantin Belousov
2709abc883 msync() also may be called on unaligned address. 2010-04-21 15:33:37 +03:00
Konstantin Belousov
4454413242 Account for possible unaligned access. 2010-04-21 15:18:02 +03:00
Konstantin Belousov
ee99dbec87 Use mincore instead of msync when available 2010-04-20 17:45:18 +03:00
Konstantin Belousov
b1c68e060c Arrange the FreeBSD ucontext offset definitions in ascending order. 2010-04-17 00:17:58 +03:00
Arun Sharma
1d9c5a356d Fix compiler warnings on x86_64 Linux 2010-04-14 12:01:37 -07:00
Konstantin Belousov
1e77c66a51 Fix FreeBSD/x86_64 sigcontext restore 2010-04-14 12:04:28 +03:00
Konstantin Belousov
21f0e90ce8 Rework 69001646fa, store sigframe address into sigcontext_addr. 2010-04-11 22:01:08 +03:00
Konstantin Belousov
69001646fa Store the ucontext_t pointer to properly handle resume across signal frames. 2010-04-11 13:48:14 +03:00
Konstantin Belousov
f01a043712 Convince gcc that x86_64_sigreturn cannot return, as declared. 2010-04-11 13:47:28 +03:00
Konstantin Belousov
6f7b335e89 Add my copyrights to several files that contain essential modifications 2010-04-10 01:42:26 +03:00
Arun Sharma
caa6095aec Fix compile errors on Linux 2010-04-07 19:59:06 -07:00
Konstantin Belousov
979af4502f Move local_resume to os-specific file. 2010-04-05 23:00:27 +03:00
Konstantin Belousov
0dbeeeb08d Move uc_addr to os-specific file 2010-04-05 22:42:23 +03:00
Konstantin Belousov
aeee03dd74 Reduce diffs to master 2010-04-05 16:28:46 +03:00
Konstantin Belousov
ccc0ae665b More move of osdep code. 2010-04-05 16:25:36 +03:00
Konstantin Belousov
a40641f326 Trim spaces 2010-04-05 16:25:20 +03:00
Konstantin Belousov
fd88f41818 Merge remote branch 'origin/master' into freebsd
Conflicts:
	src/x86/Gis_signal_frame.c
	src/x86/Gstep.c
	src/x86_64/Gis_signal_frame.c
	src/x86_64/Gstep.c
2010-04-05 16:14:50 +03:00
Arun Sharma
8e53e62db9 Refactor os specific code for x86 (both 32 and 64 bit).
Move Linux specific code into ${arch}/Gos-linux.c
2010-04-04 16:46:13 -07:00
Konstantin Belousov
31ece3eaa1 Fix setcontext for amd64 freebsd 2010-04-03 18:28:31 +03:00
Konstantin Belousov
9bb9c972e6 Merge with origin/master 2010-03-31 16:01:38 +03:00
Arun Sharma
f8a15e9679 Fix a couple of test breakages on x86_64
setcontext() now restores the signal mask. Also remove a check
in the test that doesn't seem to be valid.
2010-03-10 22:52:12 -08:00
Konstantin Belousov
752ce15c4f Preliminary version of freebsd syscall unwinder 2010-03-08 19:45:54 +02:00
Konstantin Belousov
89e1df2d32 Fix _UI_siglongjmp_cont name 2010-03-08 15:55:29 +02:00
Konstantin Belousov
0f1c0f5f66 Fix resume. 2010-03-08 15:51:39 +02:00
Konstantin Belousov
43ed5b3e56 Add required assembler magic. 2010-03-08 00:57:50 +02:00
Konstantin Belousov
093855f12a Remove unneeded includes of sys/types.h 2010-03-08 00:25:22 +02:00
Konstantin Belousov
53095e6b3e Code for resume 2010-03-07 23:43:07 +02:00
Konstantin Belousov
c64723835c Support walk over freebsd sigframes 2010-03-07 21:53:01 +02:00
Konstantin Belousov
d0d5f9979b Correct rsp offset 2010-03-07 21:20:43 +02:00
Konstantin Belousov
200147d0f2 Handle fpu state 2010-03-06 23:22:46 +02:00
Konstantin Belousov
62218e10eb Provide proper offsets for gregs in freebsd ucontext. Note that fp regs are not handled. 2010-03-06 22:33:11 +02:00
Konstantin Belousov
81f2de0083 Add implementation of unw_is_signal_frame for freebsd. 2010-03-06 18:08:44 +02:00
Konstantin Belousov
1ba7599250 Add FreeBSD stub. 2010-03-06 17:54:51 +02:00
Konstantin Belousov
3eabce18b1 Include sys/types.h when needed. 2010-03-06 16:43:39 +02:00
Konstantin Belousov
c7ffca11ae FreeBSD way to name registers. 2010-03-06 16:35:32 +02:00
Arun Sharma
3468a6b33d Change MS_SYNC to MS_ASYNC
Hopefully this makes the address validation a bit cheaper.
2010-02-23 10:35:47 -08:00
Paul Pluzhnikov
0cf76ed0b5 Check for NULL when validating addresses
This is rather on the obvious side.

While doing strace on an executable using libunwind, I noticed a
lot of:

  msync(0, 1, MS_SYNC) = -1 ENOMEM (Cannot allocate memory)

Since we know that the first page isn't mapped (or at least doesn't
contain the data we are looking for), we can eliminate all such
msync calls.

Tested on Linux/x86_64 with no regressions.
2009-12-01 13:59:45 -08:00
Arun Sharma
491d576529 Fix compiler warnings on x86_64 2009-10-16 14:01:50 -07:00
Paul Pluzhnikov
84d4150668 Allow caller to block signals.
Greetings,

We use libunwind just for stack traces (I suspect many others do as well).

The use pattern is:

GetStackTrace(void** result, int max_depth)
{
...
  unw_getcontext(&uc);
  unw_init_local(&cursor, &uc);

  while (n < max_depth) {
    if (unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip) < 0) {
      break;
    }
    result[n++] = ip;
    if (unw_step(&cursor) <= 0) {
      break;
    }
  }

Given this usage, it is quite convenient for us to block signals (or
prevent signal handlers from re-entering libunwind by other means) at the
"top level", which makes most of the sigprocmask calls performed by
libunwind itself unneccessary.

The second patch in this series adds a configure option which removes most
of the sigprocmask calls.

Attached patch is a preliminary for it -- consolidating all of the
"sigprocmask; mutex_lock;" sequences into lock_acquire and "mutex_unlock;
sigprocmask;" sequences into lock_release.

Thanks,
--
Paul Pluzhnikov

commit 402d15b123d54a7669db7cf17a76dd315094e472
Author: Paul Pluzhnikov <ppluzhnikov@google.com>
Date:   Mon Sep 21 10:18:28 2009 -0700

    Replace "sigprocmask + mutext_lock" with a single lock_acquire.
    Likewise, replace "mutext_unlock + sigprocmask" with lock_release.
2009-09-25 09:35:31 -07:00
Arun Sharma
ef29eade44 This patch eliminates one system call per unwind by not using the
getcontext in libc.

Also cleanup the namespace (check-name-space passes on x86_64 now).
Replace uses of offsets.h with ucontext_i.h.
Rename _x86_64_setcontext to _Ux86_64_setcontext.

TBD: Add CFI annotations for get/setcontext.

Signed-off-by: Paul Pluzhnikov <ppluzhnikov@google.com>
Signed-off-by: Arun Sharma <arun.sharma@google.com>
2008-06-16 14:42:16 -06:00
Arun Sharma
649f1fb344 [X86-64] For local unwinding, we have a defence mechanism against
bad/missing unwind information, which could result in libunwind
dereferencing bad pointers. This mechanism is based on msync(2) system
call and significantly reduces the chances of a bad pointer
dereference in libunwind.

The original idea was to turn this mechanism on only when necessary
i.e. libunwind didn't find proper unwind information for a IP.

There are a couple of problems in the current implementation.

* The flag is global and is modified without locking
* The flag isn't reset when starting a new unwind

The attached patch makes ->validate a per-thread setting by moving it
into struct cursor from unw_local_addr_space and resets it to false
when starting a new unwind. As a result, cursor->as_arg points to the
cursor itself instead of the ucontext (for the local case).

This was found to reduce the number of msync() system calls from an
application using libunwind significantly.

Signed-off-by: Paul Pluzhnikov <ppluzhnikov@google.com>
Signed-off-by: Arun Sharma <arun.sharma@google.com>
2008-06-16 14:35:53 -06:00
Paul Pluzhnikov
bb9d3dc689 [X86-64] Fix the pattern used to match signal frames.
The current pattern is too restrictive and doesn't work well on
modern glibcs.

Signed-off-by: Paul Pluzhnikov <ppluzhnikov@google.com>
Signed-off-by: Arun Sharma <arun.sharma@google.com>
2008-04-16 08:05:49 -06:00
Jan Kratochvil
57e5696463 Mark Wielaard <mwielaard@redhat.com> writes:
On some systems executable stacks are denied. Since libunwind and the
tests don't actually need executable stacks this patch marks all
assembly files as not needing it.

The original patch comes from frysk:

2007-04-05  Jan Kratochvil  <jan.kratochvil@redhat.com>

    * src/hppa/getcontext.S, src/hppa/setcontext.S, src/hppa/siglongjmp.S,
    src/ia64/Ginstall_cursor.S, src/ia64/Linstall_cursor.S,
    src/ia64/dyn_info_list.S, src/ia64/getcontext.S, src/ia64/longjmp.S,
    src/ia64/setjmp.S, src/ia64/siglongjmp.S, src/ia64/sigsetjmp.S,
    src/ppc64/longjmp.S, src/ppc64/siglongjmp.S, src/x86/longjmp.S,
    src/x86/siglongjmp.S, src/x86_64/longjmp.S, src/x86_64/setcontext.S,
    src/x86_64/siglongjmp.S: Stack should be non-executable, for SELinux.

I added a couple more markers for new files in current libunwind.

Before this patch you would get the following on selinux enabled
systems without allow_exec_stack: error while loading shared
libraries:

libunwind.so.7: cannot enable executable stack as shared object
	requires: Permission denied

After the patch that error disappears and all test results are similar
to the results on systems without executable stack protection.
2007-10-01 08:35:01 -06:00
David Mosberger-Tang
e6b9f350f7 Introduce a tdep_get_func_addr_hook() in the ELF lookup_symbol()
routine and add address-space argument.  This is needed because on
PPC64, a the function-name symbol refers to a function descriptor
(unlike, for example, on ia64, where the @fptr() operator is needed to
refer to a function descriptor).  Thus, in order to look up the name
of a function, we need to dereference the function descriptor.  To
make matters more "interesting", the function descriptors are normally
resolved by the dynamic linker, so we can't get their values from the
ELF file.  Instead, we have to read them from the running image, hence
the need for the address-space argument.
2007-08-22 13:02:09 -06:00
David Mosberger-Tang
5c95d139df Cleanup some files from previous patch and rerurn autoconf tools. 2007-08-06 19:59:28 -06:00
Arun Sharma
3afd613545 [X86-64]: Instead of auto-generating _u_context_i.h, hardcode the
necessary offsets in offsets.h as is done for other
	  platforms.  This fixes cross-builds.
2007-08-06 19:58:12 -06:00
Arun Sharma
497b987043 Add gen-offsets to the Makefile
This is so that the source file gets distributed and _ucontext_i.h is
generated properly on the target machine.

Signed-off-by: Arun Sharma <arun.sharma@google.com>
2007-04-10 20:18:56 -06:00
Arun Sharma
31440e9796 Make libunwind pass C++ exceptions correctly
When libunwind is linked with a C++ program that throws exceptions,
the exception that's thrown is passed in %rax. However, libc's
setcontext clears %rax, causing problems.

This patch implements a setcontext that doesn't clobber rax.

TBD: Add dwarf CFI annotations

Signed-off-by: Arun Sharma <arun.sharma@google.com>
2007-04-05 20:40:41 -06:00
David Mosberger-Tang
62bacfcf1f [x86-64] Minor reformatting for GNU-style and use memset() in lieu of bzero(). 2006-08-10 21:34:31 -06:00
Arun Sharma (अरुण)
795529eee3 [x86-64] Add a known good address cache.
This improves the performance of Lperf-simple from 500ns to 195ns
on the avg.

Signed-off-by: Arun Sharma <arun.sharma@google.com>
2006-08-10 21:30:01 -06:00
Arun Sharma
8297866bd4 [x86-64] (unw_step): Further refine handling of functions without unwind-info.
CFA should be incremented by 16 in the hope that the previous frame
may have valid unwind info.

Also increase the default frame pointer recognition heuristic from 4k
to 16k.

Signed-off-by: Andrey Veskov <Andrey.Veskov@intel.com>
Signed-off-by: Arun Sharma <arun.sharma@google.com>
2006-07-26 22:06:48 -06:00
Arun Sharma
06d2ffa41a [x86-64] (unw_step): Improve handling of functions which lack unwind info.
If following the frame-chain leads to a frame >= 4KB away, we conclude that
the frame-pointer isn't really valid.
2006-07-26 22:04:40 -06:00
Arun Sharma
7c0907634d [x86-64] (unw_is_signal_frame): If access mem fails, it's not a signal frame. 2006-07-26 21:58:27 -06:00
Arun Sharma
527225e492 [x86-64] Make a best effort at validating pointers before dereferencing them.
Signed-off-by: Arun Sharma <arun.sharma@google.com>
2006-07-26 21:46:39 -06:00
Arun Sharma
00db7f752a Implement a ip -> dwarf_reg_state cache.
Signed-off-by: Arun Sharma <arun.sharma@google.com>
2006-07-26 21:18:49 -06:00
David Mosberger-Tang
6daeeac60a Revert "Initial revision"
This reverts e6446885f4 commit.
2006-07-26 14:53:49 -06:00
David Mosberger-Tang
ca69fee8bb Merge ../libunwind-v0.98
Conflicts:

	include/dwarf_i.h
	include/x86/jmpbuf.h
	include/x86_64/jmpbuf.h
	src/hppa/init.h
	src/mi/Gget_fpreg.c
	src/mi/Gset_fpreg.c
	src/mi/strerror.c
2006-07-25 21:41:43 -06:00
hp.com!davidm
d9e100753f Include "libunwind_i.h" instead of "tdep.h".
(Logical change 1.294)
2005-05-20 09:48:08 +00:00
hp.com!davidm
07b01ad205 Include "libunwind_i.h" instead of "tdep.h" and "internal.h".
(Logical change 1.294)
2005-05-20 09:48:08 +00:00
mostang.com!davidm
7fbb8a9593 Adjust for sigset_t to intrmask_t renaming.
(Logical change 1.290)
2005-05-03 09:13:17 +00:00
mostang.com!davidm
17bf4d0af8 (unw_step): If dwarf_step() fails and the frame doesn't look like
a signal-trampoline, assume that it's a PLT stub.
	If non-DWARF stepping fails to change IP and CFA, declare
	it a bad frame.

2004/11/23 16:59:56-08:00 mostang.com!davidm
(unw_step): Also print IP as part of the function-trace.

2004/11/23 16:17:37-08:00 mostang.com!davidm
(unw_step): When dwarf_step() fails on a signal-frame, fill in all
	the known locations because dwarf_step() fails on older
	kernels which don't export the kernel vDSO even though every-
	thing else may be providing proper DWARF unwind-info.

2004/10/25 17:43:57+02:00 homeip.net!davidm
Add Debug statement for return-value.

(Logical change 1.290)
2005-05-03 09:13:17 +00:00
mostang.com!davidm
7ac9665fd3 (unw_get_proc_info): Don't freak out if dwarf_make_proc_info() fails.
Unfortunately, it must fail for current versions of libc
	since they fail to provide unwind-info for _start() and
	_dl_start().

(Logical change 1.290)
2005-05-03 09:13:17 +00:00
mostang.com!davidm
b1b84400b9 (tdep_uc_addr): Define as HIDDEN.
(Logical change 1.290)
2005-05-03 09:13:17 +00:00
mostang.com!davidm
e3180d47a0 (UC_MCONTEXT_{R8-R15,RDI,RSI,RBX,RDX,RAX,RCX}: New macros.
(Logical change 1.290)
2005-05-03 09:13:17 +00:00
hp.com!davidm
e6446885f4 Initial revision 2005-05-03 09:13:17 +00:00
hp.com!davidm
516f759229 Fix two typos which kept things from building on x86-64 and other
platforms.

(Logical change 1.290)
2005-05-03 09:13:17 +00:00
hp.com!davidm
df5acdd24a Fix missing NELEMS -> ARRAY_SIZE adjustment.
(Logical change 1.290)
2005-05-03 09:13:17 +00:00