unwind_leaffunction.pass.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Ensure that leaf function can be unwund.
  10. // REQUIRES: linux && (target-aarch64 || target-x86_64)
  11. #include <assert.h>
  12. #include <dlfcn.h>
  13. #include <signal.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #include <unwind.h>
  20. _Unwind_Reason_Code frame_handler(struct _Unwind_Context* ctx, void* arg) {
  21. (void)arg;
  22. Dl_info info = { 0, 0, 0, 0 };
  23. // Unwind util the main is reached, above frames deeped on the platfrom and architecture.
  24. if (dladdr(reinterpret_cast<void *>(_Unwind_GetIP(ctx)), &info) &&
  25. info.dli_sname && !strcmp("main", info.dli_sname)) {
  26. _Exit(0);
  27. }
  28. return _URC_NO_REASON;
  29. }
  30. void signal_handler(int signum) {
  31. (void)signum;
  32. _Unwind_Backtrace(frame_handler, NULL);
  33. _Exit(-1);
  34. }
  35. int* faultyPointer = NULL;
  36. __attribute__((noinline)) void crashing_leaf_func(void) {
  37. *faultyPointer = 0;
  38. }
  39. int main(int, char**) {
  40. signal(SIGSEGV, signal_handler);
  41. crashing_leaf_func();
  42. return -2;
  43. }