| 1 | //===----------------------------------------------------------------------===// |
| 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 | /// \file |
| 10 | /// Host-side device race report generation. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "csan_offload.h" |
| 15 | |
| 16 | #include "sanitizer_common/sanitizer_common.h" |
| 17 | #include "sanitizer_common/sanitizer_flags.h" |
| 18 | #include "sanitizer_common/sanitizer_libc.h" |
| 19 | #include "sanitizer_common/sanitizer_mutex.h" |
| 20 | #include "sanitizer_common/sanitizer_offload.h" |
| 21 | #include "sanitizer_common/sanitizer_report_decorator.h" |
| 22 | #include "sanitizer_common/sanitizer_stacktrace_printer.h" |
| 23 | #include "sanitizer_common/sanitizer_symbolizer.h" |
| 24 | #include "shared/rpc.h" |
| 25 | |
| 26 | using namespace __sanitizer; |
| 27 | |
| 28 | namespace __csan { |
| 29 | namespace { |
| 30 | |
| 31 | class Decorator : public SanitizerCommonDecorator { |
| 32 | public: |
| 33 | const char *Access() { return Blue(); } |
| 34 | const char *Location() { return Green(); } |
| 35 | }; |
| 36 | |
| 37 | const char *KindName(u32 Kind) { |
| 38 | switch (Kind) { |
| 39 | case CSAN_RACE_UNKNOWN_ORIGIN: |
| 40 | return "data race of unknown origin" ; |
| 41 | case CSAN_RACE_INTRA_WAVE: |
| 42 | return "intra-wave data race" ; |
| 43 | default: |
| 44 | return "data race" ; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | const char *MopDesc(bool First, u32 Type) { |
| 49 | const bool Write = Type & CSAN_ACCESS_WRITE; |
| 50 | return First ? (Write ? "Write" : "Read" ) |
| 51 | : (Write ? "Previous write" : "Previous read" ); |
| 52 | } |
| 53 | |
| 54 | // A pair of PCs that have already been reported as racing. The device samples |
| 55 | // the same static access from thousands of threads, so without this every |
| 56 | // launch buries the user in duplicates of one bug. |
| 57 | struct RacyPcs { |
| 58 | u64 pc[2]; |
| 59 | |
| 60 | bool operator==(const RacyPcs &other) const { |
| 61 | if (pc[0] == other.pc[0] && pc[1] == other.pc[1]) |
| 62 | return true; |
| 63 | return pc[0] == other.pc[1] && pc[1] == other.pc[0]; |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | Mutex RacyMutex; |
| 68 | InternalMmapVectorNoCtor<RacyPcs> RacyPcsSeen; |
| 69 | |
| 70 | bool FindRacyPcs(const RacyPcs &Racy) { |
| 71 | for (uptr I = 0; I < RacyPcsSeen.size(); ++I) { |
| 72 | if (Racy == RacyPcsSeen[I]) { |
| 73 | VReport(2, "%s: suppressing report as doubled\n" , SanitizerToolName); |
| 74 | return true; |
| 75 | } |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | bool HandleRacyPcs(const __csan_gpu_race &R) { |
| 81 | RacyPcs Racy = {.pc: {R.pc, R.peer_pc}}; |
| 82 | { |
| 83 | ReadLock L(&RacyMutex); |
| 84 | if (FindRacyPcs(Racy)) |
| 85 | return true; |
| 86 | } |
| 87 | Lock L(&RacyMutex); |
| 88 | if (FindRacyPcs(Racy)) |
| 89 | return true; |
| 90 | RacyPcsSeen.push_back(element: Racy); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | void PrintFrames(SymbolizedStack *Frames, u64 PC) { |
| 95 | if (!Frames) { |
| 96 | Printf(format: " #0 (%p)\n" , (void *)(uptr)PC); |
| 97 | return; |
| 98 | } |
| 99 | const SymbolizedStack *F = SkipInternalFrames(frames: Frames); |
| 100 | if (!F) |
| 101 | F = Frames; |
| 102 | int N = 0; |
| 103 | for (; F; F = F->next, ++N) { |
| 104 | InternalScopedString Res; |
| 105 | StackTracePrinter::GetOrInit()->RenderFrame( |
| 106 | buffer: &Res, format: common_flags()->stack_trace_format, frame_no: N, address: F->info.address, info: &F->info, |
| 107 | vs_style: common_flags()->symbolize_vs_style, strip_path_prefix: common_flags()->strip_path_prefix); |
| 108 | Printf(format: "%s\n" , Res.data()); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } // namespace |
| 113 | |
| 114 | void PrintOffloadReport(const __csan_gpu_race &R) { |
| 115 | if (HandleRacyPcs(R)) |
| 116 | return; |
| 117 | |
| 118 | Decorator D; |
| 119 | Printf(format: "==================\n" ); |
| 120 | Printf(format: "%s" , D.Warning()); |
| 121 | Printf(format: "WARNING: ConcurrencySanitizer: %s\n" , KindName(Kind: R.kind)); |
| 122 | Printf(format: "%s" , D.Default()); |
| 123 | |
| 124 | Printf(format: "%s" , D.Access()); |
| 125 | Printf(format: " %s of size %u at 0x%zx in block (%u,%u,%u) thread (%u,%u,%u) " |
| 126 | "lane %u:\n" , |
| 127 | MopDesc(First: true, Type: R.access_type), R.size, (uptr)R.addr, R.block[0], |
| 128 | R.block[1], R.block[2], R.thread[0], R.thread[1], R.thread[2], R.lane); |
| 129 | Printf(format: "%s" , D.Default()); |
| 130 | SymbolizedStack *This = Offload::Get().Symbolize(PC: (uptr)R.pc); |
| 131 | PrintFrames(Frames: This, PC: R.pc); |
| 132 | |
| 133 | Printf(format: "%s" , D.Access()); |
| 134 | if (R.peer_pc) { |
| 135 | Printf(format: " %s of size %u at 0x%zx:\n" , MopDesc(First: false, Type: R.peer_access_type), |
| 136 | R.peer_size, (uptr)R.addr); |
| 137 | Printf(format: "%s" , D.Default()); |
| 138 | SymbolizedStack *Peer = Offload::Get().Symbolize(PC: (uptr)R.peer_pc); |
| 139 | PrintFrames(Frames: Peer, PC: R.peer_pc); |
| 140 | if (Peer) |
| 141 | Peer->ClearAll(); |
| 142 | } else if (R.kind == CSAN_RACE_INTRA_WAVE) { |
| 143 | Printf(format: " Previous access by lane %u in the same wave\n" , R.peer_lane); |
| 144 | Printf(format: "%s" , D.Default()); |
| 145 | } else { |
| 146 | Printf(format: " Previous access of unknown origin\n" ); |
| 147 | Printf(format: "%s" , D.Default()); |
| 148 | } |
| 149 | |
| 150 | DataInfo Loc; |
| 151 | if (Offload::Get().SymbolizeData(Addr: (uptr)R.addr, Info: &Loc)) { |
| 152 | Printf(format: "%s" , D.Location()); |
| 153 | if (Loc.size) |
| 154 | Printf(format: " Location is global '%s' of size %zu at 0x%zx\n" , Loc.name, |
| 155 | Loc.size, (uptr)R.addr); |
| 156 | else |
| 157 | Printf(format: " Location is global '%s' at 0x%zx\n" , Loc.name, (uptr)R.addr); |
| 158 | Printf(format: "%s" , D.Default()); |
| 159 | Loc.Clear(); |
| 160 | } |
| 161 | |
| 162 | if (This) { |
| 163 | const SymbolizedStack *User = SkipInternalFrames(frames: This); |
| 164 | ReportErrorSummary(error_type: KindName(Kind: R.kind), info: (User ? User : This)->info); |
| 165 | This->ClearAll(); |
| 166 | } |
| 167 | |
| 168 | Printf(format: "==================\n" ); |
| 169 | } |
| 170 | |
| 171 | u32 HandleOffloadReport(void *PortPtr, u32) { |
| 172 | auto &Port = *reinterpret_cast<rpc::Server::Port *>(PortPtr); |
| 173 | if (Port.get_opcode() != SANITIZER_OFFLOAD_CSAN) |
| 174 | return rpc::RPC_UNHANDLED_OPCODE; |
| 175 | |
| 176 | Port.recv(use: [&](rpc::Buffer *Buffer, u32) { |
| 177 | __csan_gpu_race R; |
| 178 | internal_memcpy(dest: &R, src: Buffer->data, n: sizeof(R)); |
| 179 | PrintOffloadReport(R); |
| 180 | }); |
| 181 | return rpc::RPC_SUCCESS; |
| 182 | } |
| 183 | |
| 184 | } // namespace __csan |
| 185 | |