tsan_symbolize.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //===-- tsan_symbolize.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 ThreadSanitizer (TSan), a race detector.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "tsan_symbolize.h"
  13. #include "sanitizer_common/sanitizer_common.h"
  14. #include "sanitizer_common/sanitizer_placement_new.h"
  15. #include "sanitizer_common/sanitizer_symbolizer.h"
  16. #include "tsan_flags.h"
  17. #include "tsan_report.h"
  18. #include "tsan_rtl.h"
  19. namespace __tsan {
  20. void EnterSymbolizer() {
  21. ThreadState *thr = cur_thread();
  22. CHECK(!thr->in_symbolizer);
  23. thr->in_symbolizer = true;
  24. thr->ignore_interceptors++;
  25. }
  26. void ExitSymbolizer() {
  27. ThreadState *thr = cur_thread();
  28. CHECK(thr->in_symbolizer);
  29. thr->in_symbolizer = false;
  30. thr->ignore_interceptors--;
  31. }
  32. // Legacy API.
  33. // May be overriden by JIT/JAVA/etc,
  34. // whatever produces PCs marked with kExternalPCBit.
  35. SANITIZER_WEAK_DEFAULT_IMPL
  36. bool __tsan_symbolize_external(uptr pc, char *func_buf, uptr func_siz,
  37. char *file_buf, uptr file_siz, int *line,
  38. int *col) {
  39. return false;
  40. }
  41. // New API: call __tsan_symbolize_external_ex only when it exists.
  42. // Once old clients are gone, provide dummy implementation.
  43. SANITIZER_WEAK_DEFAULT_IMPL
  44. void __tsan_symbolize_external_ex(uptr pc,
  45. void (*add_frame)(void *, const char *,
  46. const char *, int, int),
  47. void *ctx) {}
  48. struct SymbolizedStackBuilder {
  49. SymbolizedStack *head;
  50. SymbolizedStack *tail;
  51. uptr addr;
  52. };
  53. static void AddFrame(void *ctx, const char *function_name, const char *file,
  54. int line, int column) {
  55. SymbolizedStackBuilder *ssb = (struct SymbolizedStackBuilder *)ctx;
  56. if (ssb->tail) {
  57. ssb->tail->next = SymbolizedStack::New(ssb->addr);
  58. ssb->tail = ssb->tail->next;
  59. } else {
  60. ssb->head = ssb->tail = SymbolizedStack::New(ssb->addr);
  61. }
  62. AddressInfo *info = &ssb->tail->info;
  63. if (function_name) {
  64. info->function = internal_strdup(function_name);
  65. }
  66. if (file) {
  67. info->file = internal_strdup(file);
  68. }
  69. info->line = line;
  70. info->column = column;
  71. }
  72. SymbolizedStack *SymbolizeCode(uptr addr) {
  73. // Check if PC comes from non-native land.
  74. if (addr & kExternalPCBit) {
  75. SymbolizedStackBuilder ssb = {nullptr, nullptr, addr};
  76. __tsan_symbolize_external_ex(addr, AddFrame, &ssb);
  77. if (ssb.head)
  78. return ssb.head;
  79. // Legacy code: remove along with the declaration above
  80. // once all clients using this API are gone.
  81. // Declare static to not consume too much stack space.
  82. // We symbolize reports in a single thread, so this is fine.
  83. static char func_buf[1024];
  84. static char file_buf[1024];
  85. int line, col;
  86. SymbolizedStack *frame = SymbolizedStack::New(addr);
  87. if (__tsan_symbolize_external(addr, func_buf, sizeof(func_buf), file_buf,
  88. sizeof(file_buf), &line, &col)) {
  89. frame->info.function = internal_strdup(func_buf);
  90. frame->info.file = internal_strdup(file_buf);
  91. frame->info.line = line;
  92. frame->info.column = col;
  93. }
  94. return frame;
  95. }
  96. return Symbolizer::GetOrInit()->SymbolizePC(addr);
  97. }
  98. ReportLocation *SymbolizeData(uptr addr) {
  99. DataInfo info;
  100. if (!Symbolizer::GetOrInit()->SymbolizeData(addr, &info))
  101. return 0;
  102. auto *ent = New<ReportLocation>();
  103. ent->type = ReportLocationGlobal;
  104. internal_memcpy(&ent->global, &info, sizeof(info));
  105. return ent;
  106. }
  107. void SymbolizeFlush() {
  108. Symbolizer::GetOrInit()->Flush();
  109. }
  110. } // namespace __tsan