diff options
author | Brock York <twunknown@gmail.com> | 2019-06-13 02:04:26 +1000 |
---|---|---|
committer | Dave Watson <dade.watson@gmail.com> | 2019-06-12 09:04:26 -0700 |
commit | 4b68214f1b1a6dd9cf09d8c0f6db7ebdb8ec3b8a (patch) | |
tree | 54c6c5cff26a220cd091309a017494c294768425 /tests | |
parent | edc427a9eccd6db583fd0cd920e2af23b4b544a9 (diff) | |
download | libunwind-4b68214f1b1a6dd9cf09d8c0f6db7ebdb8ec3b8a.tar.gz |
x86_64: Add fixup code if previous RIP was invalid (#120)
* x86_64: Add fixup code if previous RIP was invalid
Previous RIP could become invalid if a bad function pointer
was followed. Calling unwind from a signal frame in this case
skips the frame that called the bad pointer.
Check if the value at RSP looks to be valid in this case. It
should be the value of RIP pushed to the stack by the CALL instruction.
If it looks valid use this value as the next frames RIP else fallback
to the previous method.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 4 | ||||
-rw-r--r-- | tests/x64-unwind-badjmp-signal-frame.c | 121 |
2 files changed, 124 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index fee8a70b..61d1bf87 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -39,7 +39,7 @@ if USE_ALTIVEC endif #USE_ALTIVEC else #!ARCH_PPC64 if ARCH_X86_64 - check_PROGRAMS_arch += Gx64-test-dwarf-expressions Lx64-test-dwarf-expressions + check_PROGRAMS_arch += Gx64-test-dwarf-expressions Lx64-test-dwarf-expressions x64-unwind-badjmp-signal-frame endif #ARCH X86_64 endif #!ARCH_PPC64 endif #!ARCH_IA64 @@ -157,6 +157,7 @@ Ltest_cxx_exceptions_SOURCES = Ltest-cxx-exceptions.cxx Ltest_init_local_signal_SOURCES = Ltest-init-local-signal.c Ltest-init-local-signal-lib.c +x64_unwind_badjmp_signal_frame_SOURCES = x64-unwind-badjmp-signal-frame.c Gtest_dyn1_SOURCES = Gtest-dyn1.c flush-cache.S flush-cache.h Ltest_dyn1_SOURCES = Ltest-dyn1.c flush-cache.S flush-cache.h test_static_link_SOURCES = test-static-link-loc.c test-static-link-gen.c @@ -203,6 +204,7 @@ Ltest_init_local_signal_LDADD = $(LIBUNWIND) $(LIBUNWIND_local) Gtest_bt_LDADD = $(LIBUNWIND) $(LIBUNWIND_local) Gtest_concurrent_LDADD = $(LIBUNWIND) $(LIBUNWIND_local) -lpthread +x64_unwind_badjmp_signal_frame_LDADD = $(LIBUNWIND) $(LIBUNWIND_local) Gtest_dyn1_LDADD = $(LIBUNWIND) $(LIBUNWIND_local) Gtest_exc_LDADD = $(LIBUNWIND) $(LIBUNWIND_local) Gtest_init_LDADD = $(LIBUNWIND) $(LIBUNWIND_local) @BACKTRACELIB@ diff --git a/tests/x64-unwind-badjmp-signal-frame.c b/tests/x64-unwind-badjmp-signal-frame.c new file mode 100644 index 00000000..6b4ea3ac --- /dev/null +++ b/tests/x64-unwind-badjmp-signal-frame.c @@ -0,0 +1,121 @@ +/* libunwind - a platform-independent unwind library + Copyright (C) 2019 Brock York <twunknown AT gmail.com> + +This file is part of libunwind. + +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. */ + +#include <stdio.h> +#include <stdlib.h> +#include <signal.h> +#include <string.h> +#include <execinfo.h> +#include <sys/types.h> +#include <sys/ucontext.h> +#include <unistd.h> +#include <sys/ptrace.h> + +#define UNW_LOCAL_ONLY +#include <libunwind.h> + +/* + * unwind in the signal handler checking the backtrace is correct + * after a bad jump. + */ +void handle_sigsegv(int signal, siginfo_t *info, void *ucontext) +{ + /* + * 0 = success + * !0 = general failure + * 77 = test skipped + * 99 = complete failure + */ + int test_status = 0; + unw_cursor_t cursor; unw_context_t uc; + unw_word_t ip, sp, offset; + char name[1000]; + int found_signal_frame = 0; + int i = 0; + char *names[] = { + "", + "main", + }; + int names_count = sizeof(names) / sizeof(*names); + + unw_getcontext(&uc); + unw_init_local(&cursor, &uc); + + while (unw_step(&cursor) > 0 && !test_status) + { + if (unw_is_signal_frame(&cursor)) + { + found_signal_frame = 1; + } + if (found_signal_frame) + { + unw_get_reg(&cursor, UNW_REG_IP, &ip); + unw_get_reg(&cursor, UNW_REG_SP, &sp); + memset(name, 0, sizeof(char) * 1000); + unw_get_proc_name(&cursor, name, sizeof(char) * 1000, &offset); + printf("ip = %lx, sp = %lx offset = %lx name = %s\n", (long) ip, (long) sp, (long) offset, name); + if (i < names_count) + { + if (strcmp(names[i], name) != 0) + { + test_status = 1; + printf("frame %s doesn't match expected frame %s\n", name, names[i]); + } + else + { + i += 1; + } + } + } + } + + if (i != names_count) //Make sure we found all the frames! + { + printf("Failed to find all frames i:%d != names_count:%d\n", i, names_count); + test_status = 1; + } + + /*return test_status to test harness*/ + exit(test_status); +} + +void (*invalid_function)() = (void*)1; + +int main(int argc, char *argv[]) +{ + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = handle_sigsegv; + sa.sa_flags = SA_SIGINFO; + sigaction(SIGSEGV, &sa, NULL); + + invalid_function(); + + /* + * 99 is the hard error exit status for automake tests: + * https://www.gnu.org/software/automake/manual/html_node/Scripts_002dbased-Testsuites.html#Scripts_002dbased-Testsuites + * If we dont end up in the signal handler something went horribly wrong. + */ + return 99; +} |