stacktrace_riscv-inl.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2021 The Abseil Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_RISCV_INL_H_
  15. #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_RISCV_INL_H_
  16. // Generate stack trace for riscv
  17. #include <sys/ucontext.h>
  18. #include "absl/base/config.h"
  19. #if defined(__linux__)
  20. #include <sys/mman.h>
  21. #include <ucontext.h>
  22. #include <unistd.h>
  23. #endif
  24. #include <atomic>
  25. #include <cassert>
  26. #include <cstdint>
  27. #include <iostream>
  28. #include <limits>
  29. #include <utility>
  30. #include "absl/base/attributes.h"
  31. #include "absl/debugging/stacktrace.h"
  32. static constexpr ptrdiff_t kUnknownFrameSize = 0;
  33. // Compute the size of a stack frame in [low..high). We assume that low < high.
  34. // Return size of kUnknownFrameSize.
  35. template <typename T>
  36. static inline ptrdiff_t ComputeStackFrameSize(const T *low, const T *high) {
  37. const char *low_char_ptr = reinterpret_cast<const char *>(low);
  38. const char *high_char_ptr = reinterpret_cast<const char *>(high);
  39. return low < high ? static_cast<ptrdiff_t>(high_char_ptr - low_char_ptr)
  40. : kUnknownFrameSize;
  41. }
  42. // Given a pointer to a stack frame, locate and return the calling stackframe,
  43. // or return null if no stackframe can be found. Perform sanity checks (the
  44. // strictness of which is controlled by the boolean parameter
  45. // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
  46. template <bool STRICT_UNWINDING, bool WITH_CONTEXT>
  47. ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack.
  48. ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack.
  49. static void ** NextStackFrame(void **old_frame_pointer, const void *uc,
  50. const std::pair<size_t, size_t> range) {
  51. // .
  52. // .
  53. // .
  54. // +-> +----------------+
  55. // | | return address |
  56. // | | previous fp |
  57. // | | ... |
  58. // | +----------------+ <-+
  59. // | | return address | |
  60. // +---|- previous fp | |
  61. // | ... | |
  62. // $fp ->|----------------+ |
  63. // | return address | |
  64. // | previous fp -|---+
  65. // $sp ->| ... |
  66. // +----------------+
  67. void **new_frame_pointer = reinterpret_cast<void **>(old_frame_pointer[-2]);
  68. uintptr_t frame_pointer = reinterpret_cast<uintptr_t>(new_frame_pointer);
  69. // The RISCV ELF psABI mandates that the stack pointer is always 16-byte
  70. // aligned.
  71. // TODO(#1236) this doesn't hold for ILP32E which only mandates a 4-byte
  72. // alignment.
  73. if (frame_pointer & 15)
  74. return nullptr;
  75. // If the new frame pointer matches the signal context, avoid terminating
  76. // early to deal with alternate signal stacks.
  77. if (WITH_CONTEXT)
  78. if (const ucontext_t *ucv = static_cast<const ucontext_t *>(uc))
  79. // RISCV ELF psABI has the frame pointer at x8/fp/s0.
  80. // -- RISCV psABI Table 18.2
  81. if (ucv->uc_mcontext.__gregs[8] == frame_pointer)
  82. return new_frame_pointer;
  83. // Check frame size. In strict mode, we assume frames to be under 100,000
  84. // bytes. In non-strict mode, we relax the limit to 1MB.
  85. const ptrdiff_t max_size = STRICT_UNWINDING ? 100000 : 1000000;
  86. const ptrdiff_t frame_size =
  87. ComputeStackFrameSize(old_frame_pointer, new_frame_pointer);
  88. if (frame_size == kUnknownFrameSize) {
  89. if (STRICT_UNWINDING)
  90. return nullptr;
  91. // In non-strict mode permit non-contiguous stacks (e.g. alternate signal
  92. // frame handling).
  93. if (reinterpret_cast<uintptr_t>(new_frame_pointer) < range.first ||
  94. reinterpret_cast<uintptr_t>(new_frame_pointer) > range.second)
  95. return nullptr;
  96. }
  97. if (frame_size > max_size)
  98. return nullptr;
  99. return new_frame_pointer;
  100. }
  101. template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
  102. ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack.
  103. ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack.
  104. static int UnwindImpl(void **result, int *sizes, int max_depth, int skip_count,
  105. const void *ucp, int *min_dropped_frames) {
  106. // The `frame_pointer` that is computed here points to the top of the frame.
  107. // The two words preceding the address are the return address and the previous
  108. // frame pointer.
  109. #if defined(__GNUC__)
  110. void **frame_pointer = reinterpret_cast<void **>(__builtin_frame_address(0));
  111. #else
  112. #error reading stack pointer not yet supported on this platform
  113. #endif
  114. std::pair<size_t, size_t> stack = {
  115. // assume that the first page is not the stack.
  116. static_cast<size_t>(sysconf(_SC_PAGESIZE)),
  117. std::numeric_limits<size_t>::max() - sizeof(void *)
  118. };
  119. int n = 0;
  120. void *return_address = nullptr;
  121. while (frame_pointer && n < max_depth) {
  122. return_address = frame_pointer[-1];
  123. // The absl::GetStackFrames routine is called when we are in some
  124. // informational context (the failure signal handler for example). Use the
  125. // non-strict unwinding rules to produce a stack trace that is as complete
  126. // as possible (even if it contains a few bogus entries in some rare cases).
  127. void **next_frame_pointer =
  128. NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(frame_pointer, ucp,
  129. stack);
  130. if (skip_count > 0) {
  131. skip_count--;
  132. } else {
  133. result[n] = return_address;
  134. if (IS_STACK_FRAMES) {
  135. // NextStackFrame() has already checked that frame size fits to int
  136. sizes[n] = static_cast<int>(ComputeStackFrameSize(frame_pointer,
  137. next_frame_pointer));
  138. }
  139. n++;
  140. }
  141. frame_pointer = next_frame_pointer;
  142. }
  143. if (min_dropped_frames != nullptr) {
  144. // Implementation detail: we clamp the max of frames we are willing to
  145. // count, so as not to spend too much time in the loop below.
  146. const int kMaxUnwind = 200;
  147. int num_dropped_frames = 0;
  148. for (int j = 0; frame_pointer != nullptr && j < kMaxUnwind; j++) {
  149. if (skip_count > 0) {
  150. skip_count--;
  151. } else {
  152. num_dropped_frames++;
  153. }
  154. frame_pointer =
  155. NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(frame_pointer, ucp,
  156. stack);
  157. }
  158. *min_dropped_frames = num_dropped_frames;
  159. }
  160. return n;
  161. }
  162. namespace absl {
  163. ABSL_NAMESPACE_BEGIN
  164. namespace debugging_internal {
  165. bool StackTraceWorksForTest() { return true; }
  166. } // namespace debugging_internal
  167. ABSL_NAMESPACE_END
  168. } // namespace absl
  169. #endif