1 //===-- InstrumentationRuntimeTSan.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 #include "InstrumentationRuntimeTSan.h" 10 11 #include "Plugins/Process/Utility/HistoryThread.h" 12 #include "lldb/Breakpoint/StoppointCallbackContext.h" 13 #include "lldb/Core/Debugger.h" 14 #include "lldb/Core/Module.h" 15 #include "lldb/Core/PluginInterface.h" 16 #include "lldb/Core/PluginManager.h" 17 #include "lldb/Core/StreamFile.h" 18 #include "lldb/Core/ValueObject.h" 19 #include "lldb/Expression/UserExpression.h" 20 #include "lldb/Interpreter/CommandReturnObject.h" 21 #include "lldb/Symbol/Symbol.h" 22 #include "lldb/Symbol/SymbolContext.h" 23 #include "lldb/Symbol/Variable.h" 24 #include "lldb/Symbol/VariableList.h" 25 #include "lldb/Target/InstrumentationRuntimeStopInfo.h" 26 #include "lldb/Target/SectionLoadList.h" 27 #include "lldb/Target/StopInfo.h" 28 #include "lldb/Target/Target.h" 29 #include "lldb/Target/Thread.h" 30 #include "lldb/Utility/RegularExpression.h" 31 #include "lldb/Utility/Stream.h" 32 33 #include <memory> 34 35 using namespace lldb; 36 using namespace lldb_private; 37 38 lldb::InstrumentationRuntimeSP 39 InstrumentationRuntimeTSan::CreateInstance(const lldb::ProcessSP &process_sp) { 40 return InstrumentationRuntimeSP(new InstrumentationRuntimeTSan(process_sp)); 41 } 42 43 void InstrumentationRuntimeTSan::Initialize() { 44 PluginManager::RegisterPlugin( 45 GetPluginNameStatic(), "ThreadSanitizer instrumentation runtime plugin.", 46 CreateInstance, GetTypeStatic); 47 } 48 49 void InstrumentationRuntimeTSan::Terminate() { 50 PluginManager::UnregisterPlugin(CreateInstance); 51 } 52 53 lldb_private::ConstString InstrumentationRuntimeTSan::GetPluginNameStatic() { 54 return ConstString("ThreadSanitizer"); 55 } 56 57 lldb::InstrumentationRuntimeType InstrumentationRuntimeTSan::GetTypeStatic() { 58 return eInstrumentationRuntimeTypeThreadSanitizer; 59 } 60 61 InstrumentationRuntimeTSan::~InstrumentationRuntimeTSan() { Deactivate(); } 62 63 const char *thread_sanitizer_retrieve_report_data_prefix = R"( 64 extern "C" 65 { 66 void *__tsan_get_current_report(); 67 int __tsan_get_report_data(void *report, const char **description, int *count, 68 int *stack_count, int *mop_count, int *loc_count, 69 int *mutex_count, int *thread_count, 70 int *unique_tid_count, void **sleep_trace, 71 unsigned long trace_size); 72 int __tsan_get_report_stack(void *report, unsigned long idx, void **trace, 73 unsigned long trace_size); 74 int __tsan_get_report_mop(void *report, unsigned long idx, int *tid, void **addr, 75 int *size, int *write, int *atomic, void **trace, 76 unsigned long trace_size); 77 int __tsan_get_report_loc(void *report, unsigned long idx, const char **type, 78 void **addr, unsigned long *start, unsigned long *size, int *tid, 79 int *fd, int *suppressable, void **trace, 80 unsigned long trace_size); 81 int __tsan_get_report_mutex(void *report, unsigned long idx, unsigned long *mutex_id, void **addr, 82 int *destroyed, void **trace, unsigned long trace_size); 83 int __tsan_get_report_thread(void *report, unsigned long idx, int *tid, unsigned long *os_id, 84 int *running, const char **name, int *parent_tid, 85 void **trace, unsigned long trace_size); 86 int __tsan_get_report_unique_tid(void *report, unsigned long idx, int *tid); 87 88 // TODO: dlsym won't work on Windows. 89 void *dlsym(void* handle, const char* symbol); 90 int (*ptr__tsan_get_report_loc_object_type)(void *report, unsigned long idx, const char **object_type); 91 } 92 93 const int REPORT_TRACE_SIZE = 128; 94 const int REPORT_ARRAY_SIZE = 4; 95 96 struct data { 97 void *report; 98 const char *description; 99 int report_count; 100 101 void *sleep_trace[REPORT_TRACE_SIZE]; 102 103 int stack_count; 104 struct { 105 int idx; 106 void *trace[REPORT_TRACE_SIZE]; 107 } stacks[REPORT_ARRAY_SIZE]; 108 109 int mop_count; 110 struct { 111 int idx; 112 int tid; 113 int size; 114 int write; 115 int atomic; 116 void *addr; 117 void *trace[REPORT_TRACE_SIZE]; 118 } mops[REPORT_ARRAY_SIZE]; 119 120 int loc_count; 121 struct { 122 int idx; 123 const char *type; 124 void *addr; 125 unsigned long start; 126 unsigned long size; 127 int tid; 128 int fd; 129 int suppressable; 130 void *trace[REPORT_TRACE_SIZE]; 131 const char *object_type; 132 } locs[REPORT_ARRAY_SIZE]; 133 134 int mutex_count; 135 struct { 136 int idx; 137 unsigned long mutex_id; 138 void *addr; 139 int destroyed; 140 void *trace[REPORT_TRACE_SIZE]; 141 } mutexes[REPORT_ARRAY_SIZE]; 142 143 int thread_count; 144 struct { 145 int idx; 146 int tid; 147 unsigned long os_id; 148 int running; 149 const char *name; 150 int parent_tid; 151 void *trace[REPORT_TRACE_SIZE]; 152 } threads[REPORT_ARRAY_SIZE]; 153 154 int unique_tid_count; 155 struct { 156 int idx; 157 int tid; 158 } unique_tids[REPORT_ARRAY_SIZE]; 159 }; 160 )"; 161 162 const char *thread_sanitizer_retrieve_report_data_command = R"( 163 data t = {0}; 164 165 ptr__tsan_get_report_loc_object_type = (typeof(ptr__tsan_get_report_loc_object_type))(void *)dlsym((void*)-2 /*RTLD_DEFAULT*/, "__tsan_get_report_loc_object_type"); 166 167 t.report = __tsan_get_current_report(); 168 __tsan_get_report_data(t.report, &t.description, &t.report_count, &t.stack_count, &t.mop_count, &t.loc_count, &t.mutex_count, &t.thread_count, &t.unique_tid_count, t.sleep_trace, REPORT_TRACE_SIZE); 169 170 if (t.stack_count > REPORT_ARRAY_SIZE) t.stack_count = REPORT_ARRAY_SIZE; 171 for (int i = 0; i < t.stack_count; i++) { 172 t.stacks[i].idx = i; 173 __tsan_get_report_stack(t.report, i, t.stacks[i].trace, REPORT_TRACE_SIZE); 174 } 175 176 if (t.mop_count > REPORT_ARRAY_SIZE) t.mop_count = REPORT_ARRAY_SIZE; 177 for (int i = 0; i < t.mop_count; i++) { 178 t.mops[i].idx = i; 179 __tsan_get_report_mop(t.report, i, &t.mops[i].tid, &t.mops[i].addr, &t.mops[i].size, &t.mops[i].write, &t.mops[i].atomic, t.mops[i].trace, REPORT_TRACE_SIZE); 180 } 181 182 if (t.loc_count > REPORT_ARRAY_SIZE) t.loc_count = REPORT_ARRAY_SIZE; 183 for (int i = 0; i < t.loc_count; i++) { 184 t.locs[i].idx = i; 185 __tsan_get_report_loc(t.report, i, &t.locs[i].type, &t.locs[i].addr, &t.locs[i].start, &t.locs[i].size, &t.locs[i].tid, &t.locs[i].fd, &t.locs[i].suppressable, t.locs[i].trace, REPORT_TRACE_SIZE); 186 if (ptr__tsan_get_report_loc_object_type) 187 ptr__tsan_get_report_loc_object_type(t.report, i, &t.locs[i].object_type); 188 } 189 190 if (t.mutex_count > REPORT_ARRAY_SIZE) t.mutex_count = REPORT_ARRAY_SIZE; 191 for (int i = 0; i < t.mutex_count; i++) { 192 t.mutexes[i].idx = i; 193 __tsan_get_report_mutex(t.report, i, &t.mutexes[i].mutex_id, &t.mutexes[i].addr, &t.mutexes[i].destroyed, t.mutexes[i].trace, REPORT_TRACE_SIZE); 194 } 195 196 if (t.thread_count > REPORT_ARRAY_SIZE) t.thread_count = REPORT_ARRAY_SIZE; 197 for (int i = 0; i < t.thread_count; i++) { 198 t.threads[i].idx = i; 199 __tsan_get_report_thread(t.report, i, &t.threads[i].tid, &t.threads[i].os_id, &t.threads[i].running, &t.threads[i].name, &t.threads[i].parent_tid, t.threads[i].trace, REPORT_TRACE_SIZE); 200 } 201 202 if (t.unique_tid_count > REPORT_ARRAY_SIZE) t.unique_tid_count = REPORT_ARRAY_SIZE; 203 for (int i = 0; i < t.unique_tid_count; i++) { 204 t.unique_tids[i].idx = i; 205 __tsan_get_report_unique_tid(t.report, i, &t.unique_tids[i].tid); 206 } 207 208 t; 209 )"; 210 211 static StructuredData::Array * 212 CreateStackTrace(ValueObjectSP o, 213 const std::string &trace_item_name = ".trace") { 214 StructuredData::Array *trace = new StructuredData::Array(); 215 ValueObjectSP trace_value_object = 216 o->GetValueForExpressionPath(trace_item_name.c_str()); 217 size_t count = trace_value_object->GetNumChildren(); 218 for (size_t j = 0; j < count; j++) { 219 addr_t trace_addr = 220 trace_value_object->GetChildAtIndex(j, true)->GetValueAsUnsigned(0); 221 if (trace_addr == 0) 222 break; 223 trace->AddItem( 224 StructuredData::ObjectSP(new StructuredData::Integer(trace_addr))); 225 } 226 return trace; 227 } 228 229 static StructuredData::Array *ConvertToStructuredArray( 230 ValueObjectSP return_value_sp, const std::string &items_name, 231 const std::string &count_name, 232 std::function<void(ValueObjectSP o, StructuredData::Dictionary *dict)> const 233 &callback) { 234 StructuredData::Array *array = new StructuredData::Array(); 235 unsigned int count = 236 return_value_sp->GetValueForExpressionPath(count_name.c_str()) 237 ->GetValueAsUnsigned(0); 238 ValueObjectSP objects = 239 return_value_sp->GetValueForExpressionPath(items_name.c_str()); 240 for (unsigned int i = 0; i < count; i++) { 241 ValueObjectSP o = objects->GetChildAtIndex(i, true); 242 StructuredData::Dictionary *dict = new StructuredData::Dictionary(); 243 244 callback(o, dict); 245 246 array->AddItem(StructuredData::ObjectSP(dict)); 247 } 248 return array; 249 } 250 251 static std::string RetrieveString(ValueObjectSP return_value_sp, 252 ProcessSP process_sp, 253 const std::string &expression_path) { 254 addr_t ptr = 255 return_value_sp->GetValueForExpressionPath(expression_path.c_str()) 256 ->GetValueAsUnsigned(0); 257 std::string str; 258 Status error; 259 process_sp->ReadCStringFromMemory(ptr, str, error); 260 return str; 261 } 262 263 static void 264 GetRenumberedThreadIds(ProcessSP process_sp, ValueObjectSP data, 265 std::map<uint64_t, user_id_t> &thread_id_map) { 266 ConvertToStructuredArray( 267 data, ".threads", ".thread_count", 268 [process_sp, &thread_id_map](ValueObjectSP o, 269 StructuredData::Dictionary *dict) { 270 uint64_t thread_id = 271 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0); 272 uint64_t thread_os_id = 273 o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0); 274 user_id_t lldb_user_id = 0; 275 276 bool can_update = true; 277 ThreadSP lldb_thread = process_sp->GetThreadList().FindThreadByID( 278 thread_os_id, can_update); 279 if (lldb_thread) { 280 lldb_user_id = lldb_thread->GetIndexID(); 281 } else { 282 // This isn't a live thread anymore. Ask process to assign a new 283 // Index ID (or return an old one if we've already seen this 284 // thread_os_id). It will also make sure that no new threads are 285 // assigned this Index ID. 286 lldb_user_id = process_sp->AssignIndexIDToThread(thread_os_id); 287 } 288 289 thread_id_map[thread_id] = lldb_user_id; 290 }); 291 } 292 293 static user_id_t Renumber(uint64_t id, 294 std::map<uint64_t, user_id_t> &thread_id_map) { 295 auto IT = thread_id_map.find(id); 296 if (IT == thread_id_map.end()) 297 return 0; 298 299 return IT->second; 300 } 301 302 StructuredData::ObjectSP InstrumentationRuntimeTSan::RetrieveReportData( 303 ExecutionContextRef exe_ctx_ref) { 304 ProcessSP process_sp = GetProcessSP(); 305 if (!process_sp) 306 return StructuredData::ObjectSP(); 307 308 ThreadSP thread_sp = exe_ctx_ref.GetThreadSP(); 309 StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); 310 311 if (!frame_sp) 312 return StructuredData::ObjectSP(); 313 314 EvaluateExpressionOptions options; 315 options.SetUnwindOnError(true); 316 options.SetTryAllThreads(true); 317 options.SetStopOthers(true); 318 options.SetIgnoreBreakpoints(true); 319 options.SetTimeout(process_sp->GetUtilityExpressionTimeout()); 320 options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix); 321 options.SetAutoApplyFixIts(false); 322 options.SetLanguage(eLanguageTypeObjC_plus_plus); 323 324 ValueObjectSP main_value; 325 ExecutionContext exe_ctx; 326 Status eval_error; 327 frame_sp->CalculateExecutionContext(exe_ctx); 328 ExpressionResults result = UserExpression::Evaluate( 329 exe_ctx, options, thread_sanitizer_retrieve_report_data_command, "", 330 main_value, eval_error); 331 if (result != eExpressionCompleted) { 332 process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf( 333 "Warning: Cannot evaluate ThreadSanitizer expression:\n%s\n", 334 eval_error.AsCString()); 335 return StructuredData::ObjectSP(); 336 } 337 338 std::map<uint64_t, user_id_t> thread_id_map; 339 GetRenumberedThreadIds(process_sp, main_value, thread_id_map); 340 341 StructuredData::Dictionary *dict = new StructuredData::Dictionary(); 342 dict->AddStringItem("instrumentation_class", "ThreadSanitizer"); 343 dict->AddStringItem("issue_type", 344 RetrieveString(main_value, process_sp, ".description")); 345 dict->AddIntegerItem("report_count", 346 main_value->GetValueForExpressionPath(".report_count") 347 ->GetValueAsUnsigned(0)); 348 dict->AddItem("sleep_trace", StructuredData::ObjectSP(CreateStackTrace( 349 main_value, ".sleep_trace"))); 350 351 StructuredData::Array *stacks = ConvertToStructuredArray( 352 main_value, ".stacks", ".stack_count", 353 [thread_sp](ValueObjectSP o, StructuredData::Dictionary *dict) { 354 dict->AddIntegerItem( 355 "index", 356 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); 357 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); 358 // "stacks" happen on the current thread 359 dict->AddIntegerItem("thread_id", thread_sp->GetIndexID()); 360 }); 361 dict->AddItem("stacks", StructuredData::ObjectSP(stacks)); 362 363 StructuredData::Array *mops = ConvertToStructuredArray( 364 main_value, ".mops", ".mop_count", 365 [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) { 366 dict->AddIntegerItem( 367 "index", 368 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); 369 dict->AddIntegerItem( 370 "thread_id", 371 Renumber( 372 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), 373 thread_id_map)); 374 dict->AddIntegerItem( 375 "size", 376 o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); 377 dict->AddBooleanItem( 378 "is_write", 379 o->GetValueForExpressionPath(".write")->GetValueAsUnsigned(0)); 380 dict->AddBooleanItem( 381 "is_atomic", 382 o->GetValueForExpressionPath(".atomic")->GetValueAsUnsigned(0)); 383 dict->AddIntegerItem( 384 "address", 385 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); 386 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); 387 }); 388 dict->AddItem("mops", StructuredData::ObjectSP(mops)); 389 390 StructuredData::Array *locs = ConvertToStructuredArray( 391 main_value, ".locs", ".loc_count", 392 [process_sp, &thread_id_map](ValueObjectSP o, 393 StructuredData::Dictionary *dict) { 394 dict->AddIntegerItem( 395 "index", 396 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); 397 dict->AddStringItem("type", RetrieveString(o, process_sp, ".type")); 398 dict->AddIntegerItem( 399 "address", 400 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); 401 dict->AddIntegerItem( 402 "start", 403 o->GetValueForExpressionPath(".start")->GetValueAsUnsigned(0)); 404 dict->AddIntegerItem( 405 "size", 406 o->GetValueForExpressionPath(".size")->GetValueAsUnsigned(0)); 407 dict->AddIntegerItem( 408 "thread_id", 409 Renumber( 410 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), 411 thread_id_map)); 412 dict->AddIntegerItem( 413 "file_descriptor", 414 o->GetValueForExpressionPath(".fd")->GetValueAsUnsigned(0)); 415 dict->AddIntegerItem("suppressable", 416 o->GetValueForExpressionPath(".suppressable") 417 ->GetValueAsUnsigned(0)); 418 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); 419 dict->AddStringItem("object_type", 420 RetrieveString(o, process_sp, ".object_type")); 421 }); 422 dict->AddItem("locs", StructuredData::ObjectSP(locs)); 423 424 StructuredData::Array *mutexes = ConvertToStructuredArray( 425 main_value, ".mutexes", ".mutex_count", 426 [](ValueObjectSP o, StructuredData::Dictionary *dict) { 427 dict->AddIntegerItem( 428 "index", 429 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); 430 dict->AddIntegerItem( 431 "mutex_id", 432 o->GetValueForExpressionPath(".mutex_id")->GetValueAsUnsigned(0)); 433 dict->AddIntegerItem( 434 "address", 435 o->GetValueForExpressionPath(".addr")->GetValueAsUnsigned(0)); 436 dict->AddIntegerItem( 437 "destroyed", 438 o->GetValueForExpressionPath(".destroyed")->GetValueAsUnsigned(0)); 439 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); 440 }); 441 dict->AddItem("mutexes", StructuredData::ObjectSP(mutexes)); 442 443 StructuredData::Array *threads = ConvertToStructuredArray( 444 main_value, ".threads", ".thread_count", 445 [process_sp, &thread_id_map](ValueObjectSP o, 446 StructuredData::Dictionary *dict) { 447 dict->AddIntegerItem( 448 "index", 449 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); 450 dict->AddIntegerItem( 451 "thread_id", 452 Renumber( 453 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), 454 thread_id_map)); 455 dict->AddIntegerItem( 456 "thread_os_id", 457 o->GetValueForExpressionPath(".os_id")->GetValueAsUnsigned(0)); 458 dict->AddIntegerItem( 459 "running", 460 o->GetValueForExpressionPath(".running")->GetValueAsUnsigned(0)); 461 dict->AddStringItem("name", RetrieveString(o, process_sp, ".name")); 462 dict->AddIntegerItem( 463 "parent_thread_id", 464 Renumber(o->GetValueForExpressionPath(".parent_tid") 465 ->GetValueAsUnsigned(0), 466 thread_id_map)); 467 dict->AddItem("trace", StructuredData::ObjectSP(CreateStackTrace(o))); 468 }); 469 dict->AddItem("threads", StructuredData::ObjectSP(threads)); 470 471 StructuredData::Array *unique_tids = ConvertToStructuredArray( 472 main_value, ".unique_tids", ".unique_tid_count", 473 [&thread_id_map](ValueObjectSP o, StructuredData::Dictionary *dict) { 474 dict->AddIntegerItem( 475 "index", 476 o->GetValueForExpressionPath(".idx")->GetValueAsUnsigned(0)); 477 dict->AddIntegerItem( 478 "tid", 479 Renumber( 480 o->GetValueForExpressionPath(".tid")->GetValueAsUnsigned(0), 481 thread_id_map)); 482 }); 483 dict->AddItem("unique_tids", StructuredData::ObjectSP(unique_tids)); 484 485 return StructuredData::ObjectSP(dict); 486 } 487 488 std::string 489 InstrumentationRuntimeTSan::FormatDescription(StructuredData::ObjectSP report) { 490 std::string description = std::string(report->GetAsDictionary() 491 ->GetValueForKey("issue_type") 492 ->GetAsString() 493 ->GetValue()); 494 495 if (description == "data-race") { 496 return "Data race"; 497 } else if (description == "data-race-vptr") { 498 return "Data race on C++ virtual pointer"; 499 } else if (description == "heap-use-after-free") { 500 return "Use of deallocated memory"; 501 } else if (description == "heap-use-after-free-vptr") { 502 return "Use of deallocated C++ virtual pointer"; 503 } else if (description == "thread-leak") { 504 return "Thread leak"; 505 } else if (description == "locked-mutex-destroy") { 506 return "Destruction of a locked mutex"; 507 } else if (description == "mutex-double-lock") { 508 return "Double lock of a mutex"; 509 } else if (description == "mutex-invalid-access") { 510 return "Use of an uninitialized or destroyed mutex"; 511 } else if (description == "mutex-bad-unlock") { 512 return "Unlock of an unlocked mutex (or by a wrong thread)"; 513 } else if (description == "mutex-bad-read-lock") { 514 return "Read lock of a write locked mutex"; 515 } else if (description == "mutex-bad-read-unlock") { 516 return "Read unlock of a write locked mutex"; 517 } else if (description == "signal-unsafe-call") { 518 return "Signal-unsafe call inside a signal handler"; 519 } else if (description == "errno-in-signal-handler") { 520 return "Overwrite of errno in a signal handler"; 521 } else if (description == "lock-order-inversion") { 522 return "Lock order inversion (potential deadlock)"; 523 } else if (description == "external-race") { 524 return "Race on a library object"; 525 } else if (description == "swift-access-race") { 526 return "Swift access race"; 527 } 528 529 // for unknown report codes just show the code 530 return description; 531 } 532 533 static std::string Sprintf(const char *format, ...) { 534 StreamString s; 535 va_list args; 536 va_start(args, format); 537 s.PrintfVarArg(format, args); 538 va_end(args); 539 return std::string(s.GetString()); 540 } 541 542 static std::string GetSymbolNameFromAddress(ProcessSP process_sp, addr_t addr) { 543 lldb_private::Address so_addr; 544 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, 545 so_addr)) 546 return ""; 547 548 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol(); 549 if (!symbol) 550 return ""; 551 552 std::string sym_name = symbol->GetName().GetCString(); 553 return sym_name; 554 } 555 556 static void GetSymbolDeclarationFromAddress(ProcessSP process_sp, addr_t addr, 557 Declaration &decl) { 558 lldb_private::Address so_addr; 559 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, 560 so_addr)) 561 return; 562 563 lldb_private::Symbol *symbol = so_addr.CalculateSymbolContextSymbol(); 564 if (!symbol) 565 return; 566 567 ConstString sym_name = symbol->GetMangled().GetName(Mangled::ePreferMangled); 568 569 ModuleSP module = symbol->CalculateSymbolContextModule(); 570 if (!module) 571 return; 572 573 VariableList var_list; 574 module->FindGlobalVariables(sym_name, nullptr, 1U, var_list); 575 if (var_list.GetSize() < 1) 576 return; 577 578 VariableSP var = var_list.GetVariableAtIndex(0); 579 decl = var->GetDeclaration(); 580 } 581 582 addr_t InstrumentationRuntimeTSan::GetFirstNonInternalFramePc( 583 StructuredData::ObjectSP trace, bool skip_one_frame) { 584 ProcessSP process_sp = GetProcessSP(); 585 ModuleSP runtime_module_sp = GetRuntimeModuleSP(); 586 587 StructuredData::Array *trace_array = trace->GetAsArray(); 588 for (size_t i = 0; i < trace_array->GetSize(); i++) { 589 if (skip_one_frame && i == 0) 590 continue; 591 592 addr_t addr; 593 if (!trace_array->GetItemAtIndexAsInteger(i, addr)) 594 continue; 595 596 lldb_private::Address so_addr; 597 if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress( 598 addr, so_addr)) 599 continue; 600 601 if (so_addr.GetModule() == runtime_module_sp) 602 continue; 603 604 return addr; 605 } 606 607 return 0; 608 } 609 610 std::string 611 InstrumentationRuntimeTSan::GenerateSummary(StructuredData::ObjectSP report) { 612 ProcessSP process_sp = GetProcessSP(); 613 614 std::string summary = std::string(report->GetAsDictionary() 615 ->GetValueForKey("description") 616 ->GetAsString() 617 ->GetValue()); 618 bool skip_one_frame = 619 report->GetObjectForDotSeparatedPath("issue_type")->GetStringValue() == 620 "external-race"; 621 622 addr_t pc = 0; 623 if (report->GetAsDictionary() 624 ->GetValueForKey("mops") 625 ->GetAsArray() 626 ->GetSize() > 0) 627 pc = GetFirstNonInternalFramePc(report->GetAsDictionary() 628 ->GetValueForKey("mops") 629 ->GetAsArray() 630 ->GetItemAtIndex(0) 631 ->GetAsDictionary() 632 ->GetValueForKey("trace"), 633 skip_one_frame); 634 635 if (report->GetAsDictionary() 636 ->GetValueForKey("stacks") 637 ->GetAsArray() 638 ->GetSize() > 0) 639 pc = GetFirstNonInternalFramePc(report->GetAsDictionary() 640 ->GetValueForKey("stacks") 641 ->GetAsArray() 642 ->GetItemAtIndex(0) 643 ->GetAsDictionary() 644 ->GetValueForKey("trace"), 645 skip_one_frame); 646 647 if (pc != 0) { 648 summary = summary + " in " + GetSymbolNameFromAddress(process_sp, pc); 649 } 650 651 if (report->GetAsDictionary() 652 ->GetValueForKey("locs") 653 ->GetAsArray() 654 ->GetSize() > 0) { 655 StructuredData::ObjectSP loc = report->GetAsDictionary() 656 ->GetValueForKey("locs") 657 ->GetAsArray() 658 ->GetItemAtIndex(0); 659 std::string object_type = std::string(loc->GetAsDictionary() 660 ->GetValueForKey("object_type") 661 ->GetAsString() 662 ->GetValue()); 663 if (!object_type.empty()) { 664 summary = "Race on " + object_type + " object"; 665 } 666 addr_t addr = loc->GetAsDictionary() 667 ->GetValueForKey("address") 668 ->GetAsInteger() 669 ->GetValue(); 670 if (addr == 0) 671 addr = loc->GetAsDictionary() 672 ->GetValueForKey("start") 673 ->GetAsInteger() 674 ->GetValue(); 675 676 if (addr != 0) { 677 std::string global_name = GetSymbolNameFromAddress(process_sp, addr); 678 if (!global_name.empty()) { 679 summary = summary + " at " + global_name; 680 } else { 681 summary = summary + " at " + Sprintf("0x%llx", addr); 682 } 683 } else { 684 int fd = loc->GetAsDictionary() 685 ->GetValueForKey("file_descriptor") 686 ->GetAsInteger() 687 ->GetValue(); 688 if (fd != 0) { 689 summary = summary + " on file descriptor " + Sprintf("%d", fd); 690 } 691 } 692 } 693 694 return summary; 695 } 696 697 addr_t InstrumentationRuntimeTSan::GetMainRacyAddress( 698 StructuredData::ObjectSP report) { 699 addr_t result = (addr_t)-1; 700 701 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach( 702 [&result](StructuredData::Object *o) -> bool { 703 addr_t addr = 704 o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); 705 if (addr < result) 706 result = addr; 707 return true; 708 }); 709 710 return (result == (addr_t)-1) ? 0 : result; 711 } 712 713 std::string InstrumentationRuntimeTSan::GetLocationDescription( 714 StructuredData::ObjectSP report, addr_t &global_addr, 715 std::string &global_name, std::string &filename, uint32_t &line) { 716 std::string result = ""; 717 718 ProcessSP process_sp = GetProcessSP(); 719 720 if (report->GetAsDictionary() 721 ->GetValueForKey("locs") 722 ->GetAsArray() 723 ->GetSize() > 0) { 724 StructuredData::ObjectSP loc = report->GetAsDictionary() 725 ->GetValueForKey("locs") 726 ->GetAsArray() 727 ->GetItemAtIndex(0); 728 std::string type = std::string( 729 loc->GetAsDictionary()->GetValueForKey("type")->GetStringValue()); 730 if (type == "global") { 731 global_addr = loc->GetAsDictionary() 732 ->GetValueForKey("address") 733 ->GetAsInteger() 734 ->GetValue(); 735 global_name = GetSymbolNameFromAddress(process_sp, global_addr); 736 if (!global_name.empty()) { 737 result = Sprintf("'%s' is a global variable (0x%llx)", 738 global_name.c_str(), global_addr); 739 } else { 740 result = Sprintf("0x%llx is a global variable", global_addr); 741 } 742 743 Declaration decl; 744 GetSymbolDeclarationFromAddress(process_sp, global_addr, decl); 745 if (decl.GetFile()) { 746 filename = decl.GetFile().GetPath(); 747 line = decl.GetLine(); 748 } 749 } else if (type == "heap") { 750 addr_t addr = loc->GetAsDictionary() 751 ->GetValueForKey("start") 752 ->GetAsInteger() 753 ->GetValue(); 754 long size = loc->GetAsDictionary() 755 ->GetValueForKey("size") 756 ->GetAsInteger() 757 ->GetValue(); 758 std::string object_type = std::string(loc->GetAsDictionary() 759 ->GetValueForKey("object_type") 760 ->GetAsString() 761 ->GetValue()); 762 if (!object_type.empty()) { 763 result = Sprintf("Location is a %ld-byte %s object at 0x%llx", size, 764 object_type.c_str(), addr); 765 } else { 766 result = 767 Sprintf("Location is a %ld-byte heap object at 0x%llx", size, addr); 768 } 769 } else if (type == "stack") { 770 int tid = loc->GetAsDictionary() 771 ->GetValueForKey("thread_id") 772 ->GetAsInteger() 773 ->GetValue(); 774 result = Sprintf("Location is stack of thread %d", tid); 775 } else if (type == "tls") { 776 int tid = loc->GetAsDictionary() 777 ->GetValueForKey("thread_id") 778 ->GetAsInteger() 779 ->GetValue(); 780 result = Sprintf("Location is TLS of thread %d", tid); 781 } else if (type == "fd") { 782 int fd = loc->GetAsDictionary() 783 ->GetValueForKey("file_descriptor") 784 ->GetAsInteger() 785 ->GetValue(); 786 result = Sprintf("Location is file descriptor %d", fd); 787 } 788 } 789 790 return result; 791 } 792 793 bool InstrumentationRuntimeTSan::NotifyBreakpointHit( 794 void *baton, StoppointCallbackContext *context, user_id_t break_id, 795 user_id_t break_loc_id) { 796 assert(baton && "null baton"); 797 if (!baton) 798 return false; 799 800 InstrumentationRuntimeTSan *const instance = 801 static_cast<InstrumentationRuntimeTSan *>(baton); 802 803 ProcessSP process_sp = instance->GetProcessSP(); 804 805 if (process_sp->GetModIDRef().IsLastResumeForUserExpression()) 806 return false; 807 808 StructuredData::ObjectSP report = 809 instance->RetrieveReportData(context->exe_ctx_ref); 810 std::string stop_reason_description; 811 if (report) { 812 std::string issue_description = instance->FormatDescription(report); 813 report->GetAsDictionary()->AddStringItem("description", issue_description); 814 stop_reason_description = issue_description + " detected"; 815 report->GetAsDictionary()->AddStringItem("stop_description", 816 stop_reason_description); 817 std::string summary = instance->GenerateSummary(report); 818 report->GetAsDictionary()->AddStringItem("summary", summary); 819 addr_t main_address = instance->GetMainRacyAddress(report); 820 report->GetAsDictionary()->AddIntegerItem("memory_address", main_address); 821 822 addr_t global_addr = 0; 823 std::string global_name = ""; 824 std::string location_filename = ""; 825 uint32_t location_line = 0; 826 std::string location_description = instance->GetLocationDescription( 827 report, global_addr, global_name, location_filename, location_line); 828 report->GetAsDictionary()->AddStringItem("location_description", 829 location_description); 830 if (global_addr != 0) { 831 report->GetAsDictionary()->AddIntegerItem("global_address", global_addr); 832 } 833 if (!global_name.empty()) { 834 report->GetAsDictionary()->AddStringItem("global_name", global_name); 835 } 836 if (location_filename != "") { 837 report->GetAsDictionary()->AddStringItem("location_filename", 838 location_filename); 839 report->GetAsDictionary()->AddIntegerItem("location_line", location_line); 840 } 841 842 bool all_addresses_are_same = true; 843 report->GetObjectForDotSeparatedPath("mops")->GetAsArray()->ForEach( 844 [&all_addresses_are_same, 845 main_address](StructuredData::Object *o) -> bool { 846 addr_t addr = 847 o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); 848 if (main_address != addr) 849 all_addresses_are_same = false; 850 return true; 851 }); 852 report->GetAsDictionary()->AddBooleanItem("all_addresses_are_same", 853 all_addresses_are_same); 854 } 855 856 // Make sure this is the right process 857 if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) { 858 ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); 859 if (thread_sp) 860 thread_sp->SetStopInfo( 861 InstrumentationRuntimeStopInfo:: 862 CreateStopReasonWithInstrumentationData( 863 *thread_sp, stop_reason_description, report)); 864 865 StreamFile &s = process_sp->GetTarget().GetDebugger().GetOutputStream(); 866 s.Printf("ThreadSanitizer report breakpoint hit. Use 'thread " 867 "info -s' to get extended information about the " 868 "report.\n"); 869 870 return true; // Return true to stop the target 871 } else 872 return false; // Let target run 873 } 874 875 const RegularExpression & 876 InstrumentationRuntimeTSan::GetPatternForRuntimeLibrary() { 877 static RegularExpression regex(llvm::StringRef("libclang_rt.tsan_")); 878 return regex; 879 } 880 881 bool InstrumentationRuntimeTSan::CheckIfRuntimeIsValid( 882 const lldb::ModuleSP module_sp) { 883 static ConstString g_tsan_get_current_report("__tsan_get_current_report"); 884 const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType( 885 g_tsan_get_current_report, lldb::eSymbolTypeAny); 886 return symbol != nullptr; 887 } 888 889 void InstrumentationRuntimeTSan::Activate() { 890 if (IsActive()) 891 return; 892 893 ProcessSP process_sp = GetProcessSP(); 894 if (!process_sp) 895 return; 896 897 ConstString symbol_name("__tsan_on_report"); 898 const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType( 899 symbol_name, eSymbolTypeCode); 900 901 if (symbol == nullptr) 902 return; 903 904 if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) 905 return; 906 907 Target &target = process_sp->GetTarget(); 908 addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target); 909 910 if (symbol_address == LLDB_INVALID_ADDRESS) 911 return; 912 913 bool internal = true; 914 bool hardware = false; 915 Breakpoint *breakpoint = 916 process_sp->GetTarget() 917 .CreateBreakpoint(symbol_address, internal, hardware) 918 .get(); 919 breakpoint->SetCallback(InstrumentationRuntimeTSan::NotifyBreakpointHit, this, 920 true); 921 breakpoint->SetBreakpointKind("thread-sanitizer-report"); 922 SetBreakpointID(breakpoint->GetID()); 923 924 SetActive(true); 925 } 926 927 void InstrumentationRuntimeTSan::Deactivate() { 928 if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 929 ProcessSP process_sp = GetProcessSP(); 930 if (process_sp) { 931 process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID()); 932 SetBreakpointID(LLDB_INVALID_BREAK_ID); 933 } 934 } 935 SetActive(false); 936 } 937 static std::string GenerateThreadName(const std::string &path, 938 StructuredData::Object *o, 939 StructuredData::ObjectSP main_info) { 940 std::string result = "additional information"; 941 942 if (path == "mops") { 943 int size = o->GetObjectForDotSeparatedPath("size")->GetIntegerValue(); 944 int thread_id = 945 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); 946 bool is_write = 947 o->GetObjectForDotSeparatedPath("is_write")->GetBooleanValue(); 948 bool is_atomic = 949 o->GetObjectForDotSeparatedPath("is_atomic")->GetBooleanValue(); 950 addr_t addr = o->GetObjectForDotSeparatedPath("address")->GetIntegerValue(); 951 952 std::string addr_string = Sprintf(" at 0x%llx", addr); 953 954 if (main_info->GetObjectForDotSeparatedPath("all_addresses_are_same") 955 ->GetBooleanValue()) { 956 addr_string = ""; 957 } 958 959 if (main_info->GetObjectForDotSeparatedPath("issue_type") 960 ->GetStringValue() == "external-race") { 961 result = Sprintf("%s access by thread %d", 962 is_write ? "mutating" : "read-only", thread_id); 963 } else if (main_info->GetObjectForDotSeparatedPath("issue_type") 964 ->GetStringValue() == "swift-access-race") { 965 result = Sprintf("modifying access by thread %d", thread_id); 966 } else { 967 result = Sprintf("%s%s of size %d%s by thread %d", 968 is_atomic ? "atomic " : "", is_write ? "write" : "read", 969 size, addr_string.c_str(), thread_id); 970 } 971 } 972 973 if (path == "threads") { 974 int thread_id = 975 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); 976 result = Sprintf("Thread %d created", thread_id); 977 } 978 979 if (path == "locs") { 980 std::string type = std::string( 981 o->GetAsDictionary()->GetValueForKey("type")->GetStringValue()); 982 int thread_id = 983 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); 984 int fd = 985 o->GetObjectForDotSeparatedPath("file_descriptor")->GetIntegerValue(); 986 if (type == "heap") { 987 result = Sprintf("Heap block allocated by thread %d", thread_id); 988 } else if (type == "fd") { 989 result = 990 Sprintf("File descriptor %d created by thread %t", fd, thread_id); 991 } 992 } 993 994 if (path == "mutexes") { 995 int mutex_id = 996 o->GetObjectForDotSeparatedPath("mutex_id")->GetIntegerValue(); 997 998 result = Sprintf("Mutex M%d created", mutex_id); 999 } 1000 1001 if (path == "stacks") { 1002 int thread_id = 1003 o->GetObjectForDotSeparatedPath("thread_id")->GetIntegerValue(); 1004 result = Sprintf("Thread %d", thread_id); 1005 } 1006 1007 result[0] = toupper(result[0]); 1008 1009 return result; 1010 } 1011 1012 static void AddThreadsForPath(const std::string &path, 1013 ThreadCollectionSP threads, ProcessSP process_sp, 1014 StructuredData::ObjectSP info) { 1015 info->GetObjectForDotSeparatedPath(path)->GetAsArray()->ForEach( 1016 [process_sp, threads, path, info](StructuredData::Object *o) -> bool { 1017 std::vector<lldb::addr_t> pcs; 1018 o->GetObjectForDotSeparatedPath("trace")->GetAsArray()->ForEach( 1019 [&pcs](StructuredData::Object *pc) -> bool { 1020 pcs.push_back(pc->GetAsInteger()->GetValue()); 1021 return true; 1022 }); 1023 1024 if (pcs.size() == 0) 1025 return true; 1026 1027 StructuredData::ObjectSP thread_id_obj = 1028 o->GetObjectForDotSeparatedPath("thread_os_id"); 1029 tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0; 1030 1031 HistoryThread *history_thread = 1032 new HistoryThread(*process_sp, tid, pcs); 1033 ThreadSP new_thread_sp(history_thread); 1034 new_thread_sp->SetName(GenerateThreadName(path, o, info).c_str()); 1035 1036 // Save this in the Process' ExtendedThreadList so a strong pointer 1037 // retains the object 1038 process_sp->GetExtendedThreadList().AddThread(new_thread_sp); 1039 threads->AddThread(new_thread_sp); 1040 1041 return true; 1042 }); 1043 } 1044 1045 lldb::ThreadCollectionSP 1046 InstrumentationRuntimeTSan::GetBacktracesFromExtendedStopInfo( 1047 StructuredData::ObjectSP info) { 1048 ThreadCollectionSP threads; 1049 threads = std::make_shared<ThreadCollection>(); 1050 1051 if (info->GetObjectForDotSeparatedPath("instrumentation_class") 1052 ->GetStringValue() != "ThreadSanitizer") 1053 return threads; 1054 1055 ProcessSP process_sp = GetProcessSP(); 1056 1057 AddThreadsForPath("stacks", threads, process_sp, info); 1058 AddThreadsForPath("mops", threads, process_sp, info); 1059 AddThreadsForPath("locs", threads, process_sp, info); 1060 AddThreadsForPath("mutexes", threads, process_sp, info); 1061 AddThreadsForPath("threads", threads, process_sp, info); 1062 1063 return threads; 1064 } 1065