sanitizer_stacktrace_sparc.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #if defined(__GNUC__)
  29. // __builtin_return_address returns the address of the call instruction
  30. // on the SPARC and not the return address, so we need to compensate.
  31. trace_buffer[0] = GetNextInstructionPc(pc);
  32. #else
  33. trace_buffer[0] = pc;
  34. #endif
  35. size = 1;
  36. if (stack_top < 4096) return; // Sanity check for stack top.
  37. // Flush register windows to memory
  38. #if defined(__sparc_v9__) || defined(__sparcv9__) || defined(__sparcv9)
  39. asm volatile("flushw" ::: "memory");
  40. #else
  41. asm volatile("ta 3" ::: "memory");
  42. #endif
  43. // On the SPARC, the return address is not in the frame, it is in a
  44. // register. There is no way to access it off of the current frame
  45. // pointer, but it can be accessed off the previous frame pointer by
  46. // reading the value from the register window save area.
  47. uptr prev_bp = GET_CURRENT_FRAME();
  48. uptr next_bp = prev_bp;
  49. unsigned int i = 0;
  50. while (next_bp != bp && IsAligned(next_bp, sizeof(uhwptr)) && i++ < 8) {
  51. prev_bp = next_bp;
  52. next_bp = (uptr)((uhwptr *)next_bp)[14] + STACK_BIAS;
  53. }
  54. if (next_bp == bp)
  55. bp = prev_bp;
  56. // Lowest possible address that makes sense as the next frame pointer.
  57. // Goes up as we walk the stack.
  58. uptr bottom = stack_bottom;
  59. // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
  60. while (IsValidFrame(bp, stack_top, bottom) && IsAligned(bp, sizeof(uhwptr)) &&
  61. size < max_depth) {
  62. uhwptr pc1 = ((uhwptr *)bp)[15];
  63. // Let's assume that any pointer in the 0th page is invalid and
  64. // stop unwinding here. If we're adding support for a platform
  65. // where this isn't true, we need to reconsider this check.
  66. if (pc1 < kPageSize)
  67. break;
  68. if (pc1 != pc) {
  69. // %o7 contains the address of the call instruction and not the
  70. // return address, so we need to compensate.
  71. trace_buffer[size++] = GetNextInstructionPc((uptr)pc1);
  72. }
  73. bottom = bp;
  74. bp = (uptr)((uhwptr *)bp)[14] + STACK_BIAS;
  75. }
  76. }
  77. } // namespace __sanitizer
  78. #endif // !defined(__sparc__)