stacktrace_riscv-inl.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 const uintptr_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 uintptr_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 ? high_char_ptr - low_char_ptr : kUnknownFrameSize;
  40. }
  41. // Given a pointer to a stack frame, locate and return the calling stackframe,
  42. // or return null if no stackframe can be found. Perform sanity checks (the
  43. // strictness of which is controlled by the boolean parameter
  44. // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
  45. template <bool STRICT_UNWINDING, bool WITH_CONTEXT>
  46. ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack.
  47. ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack.
  48. static void ** NextStackFrame(void **old_frame_pointer, const void *uc,
  49. const std::pair<size_t, size_t> range) {
  50. // .
  51. // .
  52. // .
  53. // +-> +----------------+
  54. // | | return address |
  55. // | | previous fp |
  56. // | | ... |
  57. // | +----------------+ <-+
  58. // | | return address | |
  59. // +---|- previous fp | |
  60. // | ... | |
  61. // $fp ->|----------------+ |
  62. // | return address | |
  63. // | previous fp -|---+
  64. // $sp ->| ... |
  65. // +----------------+
  66. void **new_frame_pointer = reinterpret_cast<void **>(old_frame_pointer[-2]);
  67. uintptr_t frame_pointer = reinterpret_cast<uintptr_t>(new_frame_pointer);
  68. // The RISCV ELF psABI mandates that the stack pointer is always 16-byte
  69. // aligned.
  70. // TODO(#1236) this doesn't hold for ILP32E which only mandates a 4-byte
  71. // alignment.
  72. if (frame_pointer & 15)
  73. return nullptr;
  74. // If the new frame pointer matches the signal context, avoid terminating
  75. // early to deal with alternate signal stacks.
  76. if (WITH_CONTEXT)
  77. if (const ucontext_t *ucv = static_cast<const ucontext_t *>(uc))
  78. // RISCV ELF psABI has the frame pointer at x8/fp/s0.
  79. // -- RISCV psABI Table 18.2
  80. if (ucv->uc_mcontext.__gregs[8] == frame_pointer)
  81. return new_frame_pointer;
  82. // Check frame size. In strict mode, we assume frames to be under 100,000
  83. // bytes. In non-strict mode, we relax the limit to 1MB.
  84. const uintptr_t max_size = STRICT_UNWINDING ? 100000 : 1000000;
  85. const uintptr_t frame_size =
  86. ComputeStackFrameSize(old_frame_pointer, new_frame_pointer);
  87. if (frame_size == kUnknownFrameSize) {
  88. if (STRICT_UNWINDING)
  89. return nullptr;
  90. // In non-strict mode permit non-contiguous stacks (e.g. alternate signal
  91. // frame handling).
  92. if (reinterpret_cast<uintptr_t>(new_frame_pointer) < range.first ||
  93. reinterpret_cast<uintptr_t>(new_frame_pointer) > range.second)
  94. return nullptr;
  95. }
  96. if (frame_size > max_size)
  97. return nullptr;
  98. return new_frame_pointer;
  99. }
  100. template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
  101. ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack.
  102. ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack.
  103. static int UnwindImpl(void **result, int *sizes, int max_depth, int skip_count,
  104. const void *ucp, int *min_dropped_frames) {
  105. // The `frame_pointer` that is computed here points to the top of the frame.
  106. // The two words preceding the address are the return address and the previous
  107. // frame pointer.
  108. #if defined(__GNUC__)
  109. void **frame_pointer = reinterpret_cast<void **>(__builtin_frame_address(0));
  110. #else
  111. #error reading stack pointer not yet supported on this platform
  112. #endif
  113. std::pair<size_t, size_t> stack = {
  114. // assume that the first page is not the stack.
  115. static_cast<size_t>(sysconf(_SC_PAGESIZE)),
  116. std::numeric_limits<size_t>::max() - sizeof(void *)
  117. };
  118. int n = 0;
  119. void *return_address = nullptr;
  120. while (frame_pointer && n < max_depth) {
  121. return_address = frame_pointer[-1];
  122. // The absl::GetStackFrames routine is called when we are in some
  123. // informational context (the failure signal handler for example). Use the
  124. // non-strict unwinding rules to produce a stack trace that is as complete
  125. // as possible (even if it contains a few bogus entries in some rare cases).
  126. void **next_frame_pointer =
  127. NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(frame_pointer, ucp,
  128. stack);
  129. if (skip_count > 0) {
  130. skip_count--;
  131. } else {
  132. result[n] = return_address;
  133. if (IS_STACK_FRAMES) {
  134. sizes[n] = ComputeStackFrameSize(frame_pointer, next_frame_pointer);
  135. }
  136. n++;
  137. }
  138. frame_pointer = next_frame_pointer;
  139. }
  140. if (min_dropped_frames != nullptr) {
  141. // Implementation detail: we clamp the max of frames we are willing to
  142. // count, so as not to spend too much time in the loop below.
  143. const int kMaxUnwind = 200;
  144. int num_dropped_frames = 0;
  145. for (int j = 0; frame_pointer != nullptr && j < kMaxUnwind; j++) {
  146. if (skip_count > 0) {
  147. skip_count--;
  148. } else {
  149. num_dropped_frames++;
  150. }
  151. frame_pointer =
  152. NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(frame_pointer, ucp,
  153. stack);
  154. }
  155. *min_dropped_frames = num_dropped_frames;
  156. }
  157. return n;
  158. }
  159. namespace absl {
  160. ABSL_NAMESPACE_BEGIN
  161. namespace debugging_internal {
  162. bool StackTraceWorksForTest() { return true; }
  163. } // namespace debugging_internal
  164. ABSL_NAMESPACE_END
  165. } // namespace absl
  166. #endif