asan_debugging.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //===-- asan_debugging.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 a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // This file contains various functions that are generally useful to call when
  12. // using a debugger (LLDB, GDB).
  13. //===----------------------------------------------------------------------===//
  14. #include "asan_allocator.h"
  15. #include "asan_descriptions.h"
  16. #include "asan_flags.h"
  17. #include "asan_internal.h"
  18. #include "asan_mapping.h"
  19. #include "asan_report.h"
  20. #include "asan_thread.h"
  21. #include "sanitizer_common/sanitizer_stackdepot.h"
  22. namespace {
  23. using namespace __asan;
  24. static void FindInfoForStackVar(uptr addr, const char *frame_descr, uptr offset,
  25. char *name, uptr name_size,
  26. uptr *region_address, uptr *region_size) {
  27. InternalMmapVector<StackVarDescr> vars;
  28. vars.reserve(16);
  29. if (!ParseFrameDescription(frame_descr, &vars)) {
  30. return;
  31. }
  32. for (uptr i = 0; i < vars.size(); i++) {
  33. if (offset <= vars[i].beg + vars[i].size) {
  34. // We use name_len + 1 because strlcpy will guarantee a \0 at the end, so
  35. // if we're limiting the copy due to name_len, we add 1 to ensure we copy
  36. // the whole name and then terminate with '\0'.
  37. internal_strlcpy(name, vars[i].name_pos,
  38. Min(name_size, vars[i].name_len + 1));
  39. *region_address = addr - (offset - vars[i].beg);
  40. *region_size = vars[i].size;
  41. return;
  42. }
  43. }
  44. }
  45. uptr AsanGetStack(uptr addr, uptr *trace, u32 size, u32 *thread_id,
  46. bool alloc_stack) {
  47. AsanChunkView chunk = FindHeapChunkByAddress(addr);
  48. if (!chunk.IsValid()) return 0;
  49. StackTrace stack(nullptr, 0);
  50. if (alloc_stack) {
  51. if (chunk.AllocTid() == kInvalidTid) return 0;
  52. stack = StackDepotGet(chunk.GetAllocStackId());
  53. if (thread_id) *thread_id = chunk.AllocTid();
  54. } else {
  55. if (chunk.FreeTid() == kInvalidTid) return 0;
  56. stack = StackDepotGet(chunk.GetFreeStackId());
  57. if (thread_id) *thread_id = chunk.FreeTid();
  58. }
  59. if (trace && size) {
  60. size = Min(size, Min(stack.size, kStackTraceMax));
  61. for (uptr i = 0; i < size; i++)
  62. trace[i] = StackTrace::GetPreviousInstructionPc(stack.trace[i]);
  63. return size;
  64. }
  65. return 0;
  66. }
  67. } // namespace
  68. SANITIZER_INTERFACE_ATTRIBUTE
  69. const char *__asan_locate_address(uptr addr, char *name, uptr name_size,
  70. uptr *region_address_ptr,
  71. uptr *region_size_ptr) {
  72. AddressDescription descr(addr);
  73. uptr region_address = 0;
  74. uptr region_size = 0;
  75. const char *region_kind = nullptr;
  76. if (name && name_size > 0) name[0] = 0;
  77. if (auto shadow = descr.AsShadow()) {
  78. // region_{address,size} are already 0
  79. switch (shadow->kind) {
  80. case kShadowKindLow:
  81. region_kind = "low shadow";
  82. break;
  83. case kShadowKindGap:
  84. region_kind = "shadow gap";
  85. break;
  86. case kShadowKindHigh:
  87. region_kind = "high shadow";
  88. break;
  89. }
  90. } else if (auto heap = descr.AsHeap()) {
  91. region_kind = "heap";
  92. region_address = heap->chunk_access.chunk_begin;
  93. region_size = heap->chunk_access.chunk_size;
  94. } else if (auto stack = descr.AsStack()) {
  95. region_kind = "stack";
  96. if (!stack->frame_descr) {
  97. // region_{address,size} are already 0
  98. } else {
  99. FindInfoForStackVar(addr, stack->frame_descr, stack->offset, name,
  100. name_size, &region_address, &region_size);
  101. }
  102. } else if (auto global = descr.AsGlobal()) {
  103. region_kind = "global";
  104. auto &g = global->globals[0];
  105. internal_strlcpy(name, g.name, name_size);
  106. region_address = g.beg;
  107. region_size = g.size;
  108. } else {
  109. // region_{address,size} are already 0
  110. region_kind = "heap-invalid";
  111. }
  112. CHECK(region_kind);
  113. if (region_address_ptr) *region_address_ptr = region_address;
  114. if (region_size_ptr) *region_size_ptr = region_size;
  115. return region_kind;
  116. }
  117. SANITIZER_INTERFACE_ATTRIBUTE
  118. uptr __asan_get_alloc_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) {
  119. return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ true);
  120. }
  121. SANITIZER_INTERFACE_ATTRIBUTE
  122. uptr __asan_get_free_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) {
  123. return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ false);
  124. }
  125. SANITIZER_INTERFACE_ATTRIBUTE
  126. void __asan_get_shadow_mapping(uptr *shadow_scale, uptr *shadow_offset) {
  127. if (shadow_scale)
  128. *shadow_scale = ASAN_SHADOW_SCALE;
  129. if (shadow_offset)
  130. *shadow_offset = ASAN_SHADOW_OFFSET;
  131. }