sanitizer_stacktrace_sparc.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //===-- sanitizer_stacktrace_sparc.cpp ------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is shared between AddressSanitizer and ThreadSanitizer
  10. // run-time libraries.
  11. //
  12. // Implementation of fast stack unwinding for Sparc.
  13. //===----------------------------------------------------------------------===//
  14. #if defined(__sparc__)
  15. #if defined(__arch64__) || defined(__sparcv9)
  16. #define STACK_BIAS 2047
  17. #else
  18. #define STACK_BIAS 0
  19. #endif
  20. #include "sanitizer_common.h"
  21. #include "sanitizer_stacktrace.h"
  22. namespace __sanitizer {
  23. void BufferedStackTrace::UnwindFast(uptr pc, uptr bp, uptr stack_top,
  24. uptr stack_bottom, u32 max_depth) {
  25. // TODO(yln): add arg sanity check for stack_top/stack_bottom
  26. CHECK_GE(max_depth, 2);
  27. const uptr kPageSize = GetPageSizeCached();
  28. trace_buffer[0] = pc;
  29. size = 1;
  30. if (stack_top < 4096) return; // Sanity check for stack top.
  31. // Flush register windows to memory
  32. #if defined(__sparc_v9__) || defined(__sparcv9__) || defined(__sparcv9)
  33. asm volatile("flushw" ::: "memory");
  34. #else
  35. asm volatile("ta 3" ::: "memory");
  36. #endif
  37. // On the SPARC, the return address is not in the frame, it is in a
  38. // register. There is no way to access it off of the current frame
  39. // pointer, but it can be accessed off the previous frame pointer by
  40. // reading the value from the register window save area.
  41. uptr prev_bp = GET_CURRENT_FRAME();
  42. uptr next_bp = prev_bp;
  43. unsigned int i = 0;
  44. while (next_bp != bp && IsAligned(next_bp, sizeof(uhwptr)) && i++ < 8) {
  45. prev_bp = next_bp;
  46. next_bp = (uptr)((uhwptr *)next_bp)[14] + STACK_BIAS;
  47. }
  48. if (next_bp == bp)
  49. bp = prev_bp;
  50. // Lowest possible address that makes sense as the next frame pointer.
  51. // Goes up as we walk the stack.
  52. uptr bottom = stack_bottom;
  53. // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
  54. while (IsValidFrame(bp, stack_top, bottom) && IsAligned(bp, sizeof(uhwptr)) &&
  55. size < max_depth) {
  56. uhwptr pc1 = ((uhwptr *)bp)[15];
  57. // Let's assume that any pointer in the 0th page is invalid and
  58. // stop unwinding here. If we're adding support for a platform
  59. // where this isn't true, we need to reconsider this check.
  60. if (pc1 < kPageSize)
  61. break;
  62. if (pc1 != pc) {
  63. // %o7 contains the address of the call instruction and not the
  64. // return address, so we need to compensate.
  65. trace_buffer[size++] = GetNextInstructionPc((uptr)pc1);
  66. }
  67. bottom = bp;
  68. bp = (uptr)((uhwptr *)bp)[14] + STACK_BIAS;
  69. }
  70. }
  71. } // namespace __sanitizer
  72. #endif // !defined(__sparc__)