Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@@ -0,0 +1,327 @@
//===-- ASanRuntime.cpp -----------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "ASanRuntime.h"
#include "lldb/Breakpoint/StoppointCallbackContext.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/PluginInterface.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/ValueObject.h"
#include "lldb/Expression/UserExpression.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Target/InstrumentationRuntimeStopInfo.h"
#include "lldb/Target/StopInfo.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/Stream.h"
#include "llvm/ADT/StringSwitch.h"
using namespace lldb;
using namespace lldb_private;
lldb::InstrumentationRuntimeSP
AddressSanitizerRuntime::CreateInstance(const lldb::ProcessSP &process_sp) {
return InstrumentationRuntimeSP(new AddressSanitizerRuntime(process_sp));
}
void AddressSanitizerRuntime::Initialize() {
PluginManager::RegisterPlugin(
GetPluginNameStatic(), "AddressSanitizer instrumentation runtime plugin.",
CreateInstance, GetTypeStatic);
}
void AddressSanitizerRuntime::Terminate() {
PluginManager::UnregisterPlugin(CreateInstance);
}
lldb_private::ConstString AddressSanitizerRuntime::GetPluginNameStatic() {
return ConstString("AddressSanitizer");
}
lldb::InstrumentationRuntimeType AddressSanitizerRuntime::GetTypeStatic() {
return eInstrumentationRuntimeTypeAddressSanitizer;
}
AddressSanitizerRuntime::~AddressSanitizerRuntime() { Deactivate(); }
const RegularExpression &
AddressSanitizerRuntime::GetPatternForRuntimeLibrary() {
// FIXME: This shouldn't include the "dylib" suffix.
static RegularExpression regex(
llvm::StringRef("libclang_rt.asan_(.*)_dynamic\\.dylib"));
return regex;
}
bool AddressSanitizerRuntime::CheckIfRuntimeIsValid(
const lldb::ModuleSP module_sp) {
const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny);
return symbol != nullptr;
}
static constexpr std::chrono::seconds g_retrieve_report_data_function_timeout(2);
const char *address_sanitizer_retrieve_report_data_prefix = R"(
extern "C"
{
int __asan_report_present();
void *__asan_get_report_pc();
void *__asan_get_report_bp();
void *__asan_get_report_sp();
void *__asan_get_report_address();
const char *__asan_get_report_description();
int __asan_get_report_access_type();
size_t __asan_get_report_access_size();
}
)";
const char *address_sanitizer_retrieve_report_data_command = R"(
struct {
int present;
int access_type;
void *pc;
void *bp;
void *sp;
void *address;
size_t access_size;
const char *description;
} t;
t.present = __asan_report_present();
t.access_type = __asan_get_report_access_type();
t.pc = __asan_get_report_pc();
t.bp = __asan_get_report_bp();
t.sp = __asan_get_report_sp();
t.address = __asan_get_report_address();
t.access_size = __asan_get_report_access_size();
t.description = __asan_get_report_description();
t
)";
StructuredData::ObjectSP AddressSanitizerRuntime::RetrieveReportData() {
ProcessSP process_sp = GetProcessSP();
if (!process_sp)
return StructuredData::ObjectSP();
ThreadSP thread_sp =
process_sp->GetThreadList().GetExpressionExecutionThread();
StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
if (!frame_sp)
return StructuredData::ObjectSP();
EvaluateExpressionOptions options;
options.SetUnwindOnError(true);
options.SetTryAllThreads(true);
options.SetStopOthers(true);
options.SetIgnoreBreakpoints(true);
options.SetTimeout(g_retrieve_report_data_function_timeout);
options.SetPrefix(address_sanitizer_retrieve_report_data_prefix);
options.SetAutoApplyFixIts(false);
options.SetLanguage(eLanguageTypeObjC_plus_plus);
ValueObjectSP return_value_sp;
ExecutionContext exe_ctx;
Status eval_error;
frame_sp->CalculateExecutionContext(exe_ctx);
ExpressionResults result = UserExpression::Evaluate(
exe_ctx, options, address_sanitizer_retrieve_report_data_command, "",
return_value_sp, eval_error);
if (result != eExpressionCompleted) {
process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
"Warning: Cannot evaluate AddressSanitizer expression:\n%s\n",
eval_error.AsCString());
return StructuredData::ObjectSP();
}
int present = return_value_sp->GetValueForExpressionPath(".present")
->GetValueAsUnsigned(0);
if (present != 1)
return StructuredData::ObjectSP();
addr_t pc =
return_value_sp->GetValueForExpressionPath(".pc")->GetValueAsUnsigned(0);
/* commented out because rdar://problem/18533301
addr_t bp =
return_value_sp->GetValueForExpressionPath(".bp")->GetValueAsUnsigned(0);
addr_t sp =
return_value_sp->GetValueForExpressionPath(".sp")->GetValueAsUnsigned(0);
*/
addr_t address = return_value_sp->GetValueForExpressionPath(".address")
->GetValueAsUnsigned(0);
addr_t access_type =
return_value_sp->GetValueForExpressionPath(".access_type")
->GetValueAsUnsigned(0);
addr_t access_size =
return_value_sp->GetValueForExpressionPath(".access_size")
->GetValueAsUnsigned(0);
addr_t description_ptr =
return_value_sp->GetValueForExpressionPath(".description")
->GetValueAsUnsigned(0);
std::string description;
Status error;
process_sp->ReadCStringFromMemory(description_ptr, description, error);
StructuredData::Dictionary *dict = new StructuredData::Dictionary();
dict->AddStringItem("instrumentation_class", "AddressSanitizer");
dict->AddStringItem("stop_type", "fatal_error");
dict->AddIntegerItem("pc", pc);
/* commented out because rdar://problem/18533301
dict->AddIntegerItem("bp", bp);
dict->AddIntegerItem("sp", sp);
*/
dict->AddIntegerItem("address", address);
dict->AddIntegerItem("access_type", access_type);
dict->AddIntegerItem("access_size", access_size);
dict->AddStringItem("description", description);
return StructuredData::ObjectSP(dict);
}
std::string
AddressSanitizerRuntime::FormatDescription(StructuredData::ObjectSP report) {
std::string description = report->GetAsDictionary()
->GetValueForKey("description")
->GetAsString()
->GetValue();
return llvm::StringSwitch<std::string>(description)
.Case("heap-use-after-free", "Use of deallocated memory")
.Case("heap-buffer-overflow", "Heap buffer overflow")
.Case("stack-buffer-underflow", "Stack buffer underflow")
.Case("initialization-order-fiasco", "Initialization order problem")
.Case("stack-buffer-overflow", "Stack buffer overflow")
.Case("stack-use-after-return", "Use of stack memory after return")
.Case("use-after-poison", "Use of poisoned memory")
.Case("container-overflow", "Container overflow")
.Case("stack-use-after-scope", "Use of out-of-scope stack memory")
.Case("global-buffer-overflow", "Global buffer overflow")
.Case("unknown-crash", "Invalid memory access")
.Case("stack-overflow", "Stack space exhausted")
.Case("null-deref", "Dereference of null pointer")
.Case("wild-jump", "Jump to non-executable address")
.Case("wild-addr-write", "Write through wild pointer")
.Case("wild-addr-read", "Read from wild pointer")
.Case("wild-addr", "Access through wild pointer")
.Case("signal", "Deadly signal")
.Case("double-free", "Deallocation of freed memory")
.Case("new-delete-type-mismatch",
"Deallocation size different from allocation size")
.Case("bad-free", "Deallocation of non-allocated memory")
.Case("alloc-dealloc-mismatch",
"Mismatch between allocation and deallocation APIs")
.Case("bad-malloc_usable_size", "Invalid argument to malloc_usable_size")
.Case("bad-__sanitizer_get_allocated_size",
"Invalid argument to __sanitizer_get_allocated_size")
.Case("param-overlap",
"Call to function disallowing overlapping memory ranges")
.Case("negative-size-param", "Negative size used when accessing memory")
.Case("bad-__sanitizer_annotate_contiguous_container",
"Invalid argument to __sanitizer_annotate_contiguous_container")
.Case("odr-violation", "Symbol defined in multiple translation units")
.Case(
"invalid-pointer-pair",
"Comparison or arithmetic on pointers from different memory regions")
// for unknown report codes just show the code
.Default("AddressSanitizer detected: " + description);
}
bool AddressSanitizerRuntime::NotifyBreakpointHit(
void *baton, StoppointCallbackContext *context, user_id_t break_id,
user_id_t break_loc_id) {
assert(baton && "null baton");
if (!baton)
return false;
AddressSanitizerRuntime *const instance =
static_cast<AddressSanitizerRuntime *>(baton);
ProcessSP process_sp = instance->GetProcessSP();
if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
return false;
StructuredData::ObjectSP report = instance->RetrieveReportData();
std::string description;
if (report) {
description = instance->FormatDescription(report);
}
// Make sure this is the right process
if (process_sp && process_sp == context->exe_ctx_ref.GetProcessSP()) {
ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
if (thread_sp)
thread_sp->SetStopInfo(InstrumentationRuntimeStopInfo::
CreateStopReasonWithInstrumentationData(
*thread_sp, description, report));
StreamFileSP stream_sp(
process_sp->GetTarget().GetDebugger().GetOutputFile());
if (stream_sp) {
stream_sp->Printf("AddressSanitizer report breakpoint hit. Use 'thread "
"info -s' to get extended information about the "
"report.\n");
}
return true; // Return true to stop the target
} else
return false; // Let target run
}
void AddressSanitizerRuntime::Activate() {
if (IsActive())
return;
ProcessSP process_sp = GetProcessSP();
if (!process_sp)
return;
ConstString symbol_name("__asan::AsanDie()");
const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType(
symbol_name, eSymbolTypeCode);
if (symbol == NULL)
return;
if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
return;
Target &target = process_sp->GetTarget();
addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
if (symbol_address == LLDB_INVALID_ADDRESS)
return;
bool internal = true;
bool hardware = false;
Breakpoint *breakpoint =
process_sp->GetTarget()
.CreateBreakpoint(symbol_address, internal, hardware)
.get();
breakpoint->SetCallback(AddressSanitizerRuntime::NotifyBreakpointHit, this,
true);
breakpoint->SetBreakpointKind("address-sanitizer-report");
SetBreakpointID(breakpoint->GetID());
SetActive(true);
}
void AddressSanitizerRuntime::Deactivate() {
if (GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
ProcessSP process_sp = GetProcessSP();
if (process_sp) {
process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
SetBreakpointID(LLDB_INVALID_BREAK_ID);
}
}
SetActive(false);
}

View File

@@ -0,0 +1,71 @@
//===-- AddressSanitizerRuntime.h -------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_AddressSanitizerRuntime_h_
#define liblldb_AddressSanitizerRuntime_h_
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Target/InstrumentationRuntime.h"
#include "lldb/Target/Process.h"
#include "lldb/Utility/StructuredData.h"
#include "lldb/lldb-private.h"
namespace lldb_private {
class AddressSanitizerRuntime : public lldb_private::InstrumentationRuntime {
public:
~AddressSanitizerRuntime() override;
static lldb::InstrumentationRuntimeSP
CreateInstance(const lldb::ProcessSP &process_sp);
static void Initialize();
static void Terminate();
static lldb_private::ConstString GetPluginNameStatic();
static lldb::InstrumentationRuntimeType GetTypeStatic();
lldb_private::ConstString GetPluginName() override {
return GetPluginNameStatic();
}
virtual lldb::InstrumentationRuntimeType GetType() { return GetTypeStatic(); }
uint32_t GetPluginVersion() override { return 1; }
private:
AddressSanitizerRuntime(const lldb::ProcessSP &process_sp)
: lldb_private::InstrumentationRuntime(process_sp) {}
const RegularExpression &GetPatternForRuntimeLibrary() override;
bool CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override;
void Activate() override;
void Deactivate();
static bool NotifyBreakpointHit(void *baton,
StoppointCallbackContext *context,
lldb::user_id_t break_id,
lldb::user_id_t break_loc_id);
StructuredData::ObjectSP RetrieveReportData();
std::string FormatDescription(StructuredData::ObjectSP report);
};
} // namespace lldb_private
#endif // liblldb_AddressSanitizerRuntime_h_

View File

@@ -0,0 +1,13 @@
add_lldb_library(lldbPluginInstrumentationRuntimeASan PLUGIN
ASanRuntime.cpp
LINK_LIBS
lldbBreakpoint
lldbCore
lldbExpression
lldbInterpreter
lldbSymbol
lldbTarget
LINK_COMPONENTS
Support
)

View File

@@ -0,0 +1,4 @@
add_subdirectory(ASan)
add_subdirectory(MainThreadChecker)
add_subdirectory(TSan)
add_subdirectory(UBSan)

View File

@@ -0,0 +1,13 @@
add_lldb_library(lldbPluginInstrumentationRuntimeMainThreadChecker PLUGIN
MainThreadCheckerRuntime.cpp
LINK_LIBS
lldbBreakpoint
lldbCore
lldbExpression
lldbInterpreter
lldbSymbol
lldbTarget
LINK_COMPONENTS
Support
)

View File

@@ -0,0 +1,276 @@
//===-- MainThreadCheckerRuntime.cpp ----------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "MainThreadCheckerRuntime.h"
#include "lldb/Breakpoint/StoppointCallbackContext.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/InstrumentationRuntimeStopInfo.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/StopInfo.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/RegularExpression.h"
#include "Plugins/Process/Utility/HistoryThread.h"
using namespace lldb;
using namespace lldb_private;
MainThreadCheckerRuntime::~MainThreadCheckerRuntime() {
Deactivate();
}
lldb::InstrumentationRuntimeSP
MainThreadCheckerRuntime::CreateInstance(const lldb::ProcessSP &process_sp) {
return InstrumentationRuntimeSP(new MainThreadCheckerRuntime(process_sp));
}
void MainThreadCheckerRuntime::Initialize() {
PluginManager::RegisterPlugin(
GetPluginNameStatic(), "MainThreadChecker instrumentation runtime plugin.",
CreateInstance, GetTypeStatic);
}
void MainThreadCheckerRuntime::Terminate() {
PluginManager::UnregisterPlugin(CreateInstance);
}
lldb_private::ConstString MainThreadCheckerRuntime::GetPluginNameStatic() {
return ConstString("MainThreadChecker");
}
lldb::InstrumentationRuntimeType MainThreadCheckerRuntime::GetTypeStatic() {
return eInstrumentationRuntimeTypeMainThreadChecker;
}
const RegularExpression &
MainThreadCheckerRuntime::GetPatternForRuntimeLibrary() {
static RegularExpression regex(llvm::StringRef("libMainThreadChecker.dylib"));
return regex;
}
bool MainThreadCheckerRuntime::CheckIfRuntimeIsValid(
const lldb::ModuleSP module_sp) {
static ConstString test_sym("__main_thread_checker_on_report");
const Symbol *symbol =
module_sp->FindFirstSymbolWithNameAndType(test_sym, lldb::eSymbolTypeAny);
return symbol != nullptr;
}
StructuredData::ObjectSP
MainThreadCheckerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) {
ProcessSP process_sp = GetProcessSP();
if (!process_sp)
return StructuredData::ObjectSP();
ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
ModuleSP runtime_module_sp = GetRuntimeModuleSP();
Target &target = process_sp->GetTarget();
if (!frame_sp)
return StructuredData::ObjectSP();
RegisterContextSP regctx_sp = frame_sp->GetRegisterContext();
if (!regctx_sp)
return StructuredData::ObjectSP();
const RegisterInfo *reginfo = regctx_sp->GetRegisterInfoByName("arg1");
if (!reginfo)
return StructuredData::ObjectSP();
uint64_t apiname_ptr = regctx_sp->ReadRegisterAsUnsigned(reginfo, 0);
if (!apiname_ptr)
return StructuredData::ObjectSP();
std::string apiName = "";
Status read_error;
target.ReadCStringFromMemory(apiname_ptr, apiName, read_error);
if (read_error.Fail())
return StructuredData::ObjectSP();
std::string className = "";
std::string selector = "";
if (apiName.substr(0, 2) == "-[") {
size_t spacePos = apiName.find(" ");
if (spacePos != std::string::npos) {
className = apiName.substr(2, spacePos - 2);
selector = apiName.substr(spacePos + 1, apiName.length() - spacePos - 2);
}
}
// Gather the PCs of the user frames in the backtrace.
StructuredData::Array *trace = new StructuredData::Array();
auto trace_sp = StructuredData::ObjectSP(trace);
StackFrameSP responsible_frame;
for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) {
StackFrameSP frame = thread_sp->GetStackFrameAtIndex(I);
Address addr = frame->GetFrameCodeAddress();
if (addr.GetModule() == runtime_module_sp) // Skip PCs from the runtime.
continue;
// The first non-runtime frame is responsible for the bug.
if (!responsible_frame)
responsible_frame = frame;
// First frame in stacktrace should point to a real PC, not return address.
if (I != 0 && trace->GetSize() == 0) {
addr.Slide(-1);
}
lldb::addr_t PC = addr.GetLoadAddress(&target);
trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(PC)));
}
auto *d = new StructuredData::Dictionary();
auto dict_sp = StructuredData::ObjectSP(d);
d->AddStringItem("instrumentation_class", "MainThreadChecker");
d->AddStringItem("api_name", apiName);
d->AddStringItem("class_name", className);
d->AddStringItem("selector", selector);
d->AddStringItem("description",
apiName + " must be used from main thread only");
d->AddIntegerItem("tid", thread_sp->GetIndexID());
d->AddItem("trace", trace_sp);
return dict_sp;
}
bool MainThreadCheckerRuntime::NotifyBreakpointHit(
void *baton, StoppointCallbackContext *context, user_id_t break_id,
user_id_t break_loc_id) {
assert(baton && "null baton");
if (!baton)
return false; //< false => resume execution.
MainThreadCheckerRuntime *const instance =
static_cast<MainThreadCheckerRuntime *>(baton);
ProcessSP process_sp = instance->GetProcessSP();
ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
if (!process_sp || !thread_sp ||
process_sp != context->exe_ctx_ref.GetProcessSP())
return false;
if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
return false;
StructuredData::ObjectSP report =
instance->RetrieveReportData(context->exe_ctx_ref);
if (report) {
std::string description = report->GetAsDictionary()
->GetValueForKey("description")
->GetAsString()
->GetValue();
thread_sp->SetStopInfo(
InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(
*thread_sp, description, report));
return true;
}
return false;
}
void MainThreadCheckerRuntime::Activate() {
if (IsActive())
return;
ProcessSP process_sp = GetProcessSP();
if (!process_sp)
return;
ModuleSP runtime_module_sp = GetRuntimeModuleSP();
ConstString symbol_name("__main_thread_checker_on_report");
const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType(
symbol_name, eSymbolTypeCode);
if (symbol == nullptr)
return;
if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
return;
Target &target = process_sp->GetTarget();
addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
if (symbol_address == LLDB_INVALID_ADDRESS)
return;
Breakpoint *breakpoint =
process_sp->GetTarget()
.CreateBreakpoint(symbol_address, /*internal=*/true,
/*hardware=*/false)
.get();
breakpoint->SetCallback(MainThreadCheckerRuntime::NotifyBreakpointHit, this,
true);
breakpoint->SetBreakpointKind("main-thread-checker-report");
SetBreakpointID(breakpoint->GetID());
SetActive(true);
}
void MainThreadCheckerRuntime::Deactivate() {
SetActive(false);
auto BID = GetBreakpointID();
if (BID == LLDB_INVALID_BREAK_ID)
return;
if (ProcessSP process_sp = GetProcessSP()) {
process_sp->GetTarget().RemoveBreakpointByID(BID);
SetBreakpointID(LLDB_INVALID_BREAK_ID);
}
}
lldb::ThreadCollectionSP
MainThreadCheckerRuntime::GetBacktracesFromExtendedStopInfo(
StructuredData::ObjectSP info) {
ThreadCollectionSP threads;
threads.reset(new ThreadCollection());
ProcessSP process_sp = GetProcessSP();
if (info->GetObjectForDotSeparatedPath("instrumentation_class")
->GetStringValue() != "MainThreadChecker")
return threads;
std::vector<lldb::addr_t> PCs;
auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray();
trace->ForEach([&PCs](StructuredData::Object *PC) -> bool {
PCs.push_back(PC->GetAsInteger()->GetValue());
return true;
});
if (PCs.empty())
return threads;
StructuredData::ObjectSP thread_id_obj =
info->GetObjectForDotSeparatedPath("tid");
tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
uint32_t stop_id = 0;
bool stop_id_is_valid = false;
HistoryThread *history_thread =
new HistoryThread(*process_sp, tid, PCs, stop_id, stop_id_is_valid);
ThreadSP new_thread_sp(history_thread);
// Save this in the Process' ExtendedThreadList so a strong pointer
// retains the object
process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
threads->AddThread(new_thread_sp);
return threads;
}

View File

@@ -0,0 +1,68 @@
//===-- MainThreadCheckerRuntime.h ------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_MainThreadCheckerRuntime_h_
#define liblldb_MainThreadCheckerRuntime_h_
#include "lldb/Target/ABI.h"
#include "lldb/Target/InstrumentationRuntime.h"
#include "lldb/Utility/StructuredData.h"
#include "lldb/lldb-private.h"
namespace lldb_private {
class MainThreadCheckerRuntime : public lldb_private::InstrumentationRuntime {
public:
~MainThreadCheckerRuntime() override;
static lldb::InstrumentationRuntimeSP
CreateInstance(const lldb::ProcessSP &process_sp);
static void Initialize();
static void Terminate();
static lldb_private::ConstString GetPluginNameStatic();
static lldb::InstrumentationRuntimeType GetTypeStatic();
lldb_private::ConstString GetPluginName() override {
return GetPluginNameStatic();
}
virtual lldb::InstrumentationRuntimeType GetType() { return GetTypeStatic(); }
uint32_t GetPluginVersion() override { return 1; }
lldb::ThreadCollectionSP
GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) override;
private:
MainThreadCheckerRuntime(const lldb::ProcessSP &process_sp)
: lldb_private::InstrumentationRuntime(process_sp) {}
const RegularExpression &GetPatternForRuntimeLibrary() override;
bool CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override;
void Activate() override;
void Deactivate();
static bool NotifyBreakpointHit(void *baton,
StoppointCallbackContext *context,
lldb::user_id_t break_id,
lldb::user_id_t break_loc_id);
StructuredData::ObjectSP RetrieveReportData(ExecutionContextRef exe_ctx_ref);
};
} // namespace lldb_private
#endif // liblldb_MainThreadCheckerRuntime_h_

View File

@@ -0,0 +1,12 @@
add_lldb_library(lldbPluginInstrumentationRuntimeTSan PLUGIN
TSanRuntime.cpp
LINK_LIBS
lldbBreakpoint
lldbCore
lldbExpression
lldbInterpreter
lldbSymbol
lldbTarget
lldbPluginProcessUtility
)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,86 @@
//===-- ThreadSanitizerRuntime.h --------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_ThreadSanitizerRuntime_h_
#define liblldb_ThreadSanitizerRuntime_h_
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Target/ABI.h"
#include "lldb/Target/InstrumentationRuntime.h"
#include "lldb/Utility/StructuredData.h"
#include "lldb/lldb-private.h"
namespace lldb_private {
class ThreadSanitizerRuntime : public lldb_private::InstrumentationRuntime {
public:
~ThreadSanitizerRuntime() override;
static lldb::InstrumentationRuntimeSP
CreateInstance(const lldb::ProcessSP &process_sp);
static void Initialize();
static void Terminate();
static lldb_private::ConstString GetPluginNameStatic();
static lldb::InstrumentationRuntimeType GetTypeStatic();
lldb_private::ConstString GetPluginName() override {
return GetPluginNameStatic();
}
virtual lldb::InstrumentationRuntimeType GetType() { return GetTypeStatic(); }
uint32_t GetPluginVersion() override { return 1; }
lldb::ThreadCollectionSP
GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) override;
private:
ThreadSanitizerRuntime(const lldb::ProcessSP &process_sp)
: lldb_private::InstrumentationRuntime(process_sp) {}
const RegularExpression &GetPatternForRuntimeLibrary() override;
bool CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override;
void Activate() override;
void Deactivate();
static bool NotifyBreakpointHit(void *baton,
StoppointCallbackContext *context,
lldb::user_id_t break_id,
lldb::user_id_t break_loc_id);
StructuredData::ObjectSP RetrieveReportData(ExecutionContextRef exe_ctx_ref);
std::string FormatDescription(StructuredData::ObjectSP report);
std::string GenerateSummary(StructuredData::ObjectSP report);
lldb::addr_t GetMainRacyAddress(StructuredData::ObjectSP report);
std::string GetLocationDescription(StructuredData::ObjectSP report,
lldb::addr_t &global_addr,
std::string &global_name,
std::string &filename, uint32_t &line);
lldb::addr_t GetFirstNonInternalFramePc(StructuredData::ObjectSP trace,
bool skip_one_frame = false);
};
} // namespace lldb_private
#endif // liblldb_ThreadSanitizerRuntime_h_

View File

@@ -0,0 +1,13 @@
add_lldb_library(lldbPluginInstrumentationRuntimeUBSan PLUGIN
UBSanRuntime.cpp
LINK_LIBS
lldbBreakpoint
lldbCore
lldbExpression
lldbInterpreter
lldbSymbol
lldbTarget
LINK_COMPONENTS
Support
)

View File

@@ -0,0 +1,343 @@
//===-- UBSanRuntime.cpp ----------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "UBSanRuntime.h"
#include "Plugins/Process/Utility/HistoryThread.h"
#include "lldb/Breakpoint/StoppointCallbackContext.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/PluginInterface.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/ValueObject.h"
#include "lldb/Expression/UserExpression.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/InstrumentationRuntimeStopInfo.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/StopInfo.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/Stream.h"
#include <ctype.h>
using namespace lldb;
using namespace lldb_private;
UndefinedBehaviorSanitizerRuntime::~UndefinedBehaviorSanitizerRuntime() {
Deactivate();
}
lldb::InstrumentationRuntimeSP
UndefinedBehaviorSanitizerRuntime::CreateInstance(
const lldb::ProcessSP &process_sp) {
return InstrumentationRuntimeSP(
new UndefinedBehaviorSanitizerRuntime(process_sp));
}
void UndefinedBehaviorSanitizerRuntime::Initialize() {
PluginManager::RegisterPlugin(
GetPluginNameStatic(),
"UndefinedBehaviorSanitizer instrumentation runtime plugin.",
CreateInstance, GetTypeStatic);
}
void UndefinedBehaviorSanitizerRuntime::Terminate() {
PluginManager::UnregisterPlugin(CreateInstance);
}
lldb_private::ConstString
UndefinedBehaviorSanitizerRuntime::GetPluginNameStatic() {
return ConstString("UndefinedBehaviorSanitizer");
}
lldb::InstrumentationRuntimeType
UndefinedBehaviorSanitizerRuntime::GetTypeStatic() {
return eInstrumentationRuntimeTypeUndefinedBehaviorSanitizer;
}
static const char *ub_sanitizer_retrieve_report_data_prefix = R"(
extern "C" {
void
__ubsan_get_current_report_data(const char **OutIssueKind,
const char **OutMessage, const char **OutFilename, unsigned *OutLine,
unsigned *OutCol, char **OutMemoryAddr);
}
struct data {
const char *issue_kind;
const char *message;
const char *filename;
unsigned line;
unsigned col;
char *memory_addr;
};
)";
static const char *ub_sanitizer_retrieve_report_data_command = R"(
data t;
__ubsan_get_current_report_data(&t.issue_kind, &t.message, &t.filename, &t.line,
&t.col, &t.memory_addr);
t;
)";
static addr_t RetrieveUnsigned(ValueObjectSP return_value_sp,
ProcessSP process_sp,
const std::string &expression_path) {
return return_value_sp->GetValueForExpressionPath(expression_path.c_str())
->GetValueAsUnsigned(0);
}
static std::string RetrieveString(ValueObjectSP return_value_sp,
ProcessSP process_sp,
const std::string &expression_path) {
addr_t ptr = RetrieveUnsigned(return_value_sp, process_sp, expression_path);
std::string str;
Status error;
process_sp->ReadCStringFromMemory(ptr, str, error);
return str;
}
StructuredData::ObjectSP UndefinedBehaviorSanitizerRuntime::RetrieveReportData(
ExecutionContextRef exe_ctx_ref) {
ProcessSP process_sp = GetProcessSP();
if (!process_sp)
return StructuredData::ObjectSP();
ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
ModuleSP runtime_module_sp = GetRuntimeModuleSP();
Target &target = process_sp->GetTarget();
if (!frame_sp)
return StructuredData::ObjectSP();
StreamFileSP Stream(target.GetDebugger().GetOutputFile());
EvaluateExpressionOptions options;
options.SetUnwindOnError(true);
options.SetTryAllThreads(true);
options.SetStopOthers(true);
options.SetIgnoreBreakpoints(true);
options.SetTimeout(std::chrono::seconds(2));
options.SetPrefix(ub_sanitizer_retrieve_report_data_prefix);
options.SetAutoApplyFixIts(false);
options.SetLanguage(eLanguageTypeObjC_plus_plus);
ValueObjectSP main_value;
ExecutionContext exe_ctx;
Status eval_error;
frame_sp->CalculateExecutionContext(exe_ctx);
ExpressionResults result = UserExpression::Evaluate(
exe_ctx, options, ub_sanitizer_retrieve_report_data_command, "",
main_value, eval_error);
if (result != eExpressionCompleted) {
target.GetDebugger().GetAsyncOutputStream()->Printf(
"Warning: Cannot evaluate UndefinedBehaviorSanitizer expression:\n%s\n",
eval_error.AsCString());
return StructuredData::ObjectSP();
}
// Gather the PCs of the user frames in the backtrace.
StructuredData::Array *trace = new StructuredData::Array();
auto trace_sp = StructuredData::ObjectSP(trace);
for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) {
const Address FCA =
thread_sp->GetStackFrameAtIndex(I)->GetFrameCodeAddress();
if (FCA.GetModule() == runtime_module_sp) // Skip PCs from the runtime.
continue;
lldb::addr_t PC = FCA.GetLoadAddress(&target);
trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(PC)));
}
std::string IssueKind = RetrieveString(main_value, process_sp, ".issue_kind");
std::string ErrMessage = RetrieveString(main_value, process_sp, ".message");
std::string Filename = RetrieveString(main_value, process_sp, ".filename");
unsigned Line = RetrieveUnsigned(main_value, process_sp, ".line");
unsigned Col = RetrieveUnsigned(main_value, process_sp, ".col");
uintptr_t MemoryAddr =
RetrieveUnsigned(main_value, process_sp, ".memory_addr");
auto *d = new StructuredData::Dictionary();
auto dict_sp = StructuredData::ObjectSP(d);
d->AddStringItem("instrumentation_class", "UndefinedBehaviorSanitizer");
d->AddStringItem("description", IssueKind);
d->AddStringItem("summary", ErrMessage);
d->AddStringItem("filename", Filename);
d->AddIntegerItem("line", Line);
d->AddIntegerItem("col", Col);
d->AddIntegerItem("memory_address", MemoryAddr);
d->AddIntegerItem("tid", thread_sp->GetID());
d->AddItem("trace", trace_sp);
return dict_sp;
}
static std::string GetStopReasonDescription(StructuredData::ObjectSP report) {
llvm::StringRef stop_reason_description_ref;
report->GetAsDictionary()->GetValueForKeyAsString("description",
stop_reason_description_ref);
std::string stop_reason_description = stop_reason_description_ref;
if (!stop_reason_description.size()) {
stop_reason_description = "Undefined behavior detected";
} else {
stop_reason_description[0] = toupper(stop_reason_description[0]);
for (unsigned I = 1; I < stop_reason_description.size(); ++I)
if (stop_reason_description[I] == '-')
stop_reason_description[I] = ' ';
}
return stop_reason_description;
}
bool UndefinedBehaviorSanitizerRuntime::NotifyBreakpointHit(
void *baton, StoppointCallbackContext *context, user_id_t break_id,
user_id_t break_loc_id) {
assert(baton && "null baton");
if (!baton)
return false; //< false => resume execution.
UndefinedBehaviorSanitizerRuntime *const instance =
static_cast<UndefinedBehaviorSanitizerRuntime *>(baton);
ProcessSP process_sp = instance->GetProcessSP();
ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
if (!process_sp || !thread_sp ||
process_sp != context->exe_ctx_ref.GetProcessSP())
return false;
if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
return false;
StructuredData::ObjectSP report =
instance->RetrieveReportData(context->exe_ctx_ref);
if (report) {
thread_sp->SetStopInfo(
InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(
*thread_sp, GetStopReasonDescription(report), report));
return true;
}
return false;
}
const RegularExpression &
UndefinedBehaviorSanitizerRuntime::GetPatternForRuntimeLibrary() {
static RegularExpression regex(llvm::StringRef("libclang_rt\\.(a|t|ub)san_"));
return regex;
}
bool UndefinedBehaviorSanitizerRuntime::CheckIfRuntimeIsValid(
const lldb::ModuleSP module_sp) {
static ConstString ubsan_test_sym("__ubsan_on_report");
const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
ubsan_test_sym, lldb::eSymbolTypeAny);
return symbol != nullptr;
}
// FIXME: Factor out all the logic we have in common with the {a,t}san plugins.
void UndefinedBehaviorSanitizerRuntime::Activate() {
if (IsActive())
return;
ProcessSP process_sp = GetProcessSP();
if (!process_sp)
return;
ModuleSP runtime_module_sp = GetRuntimeModuleSP();
ConstString symbol_name("__ubsan_on_report");
const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType(
symbol_name, eSymbolTypeCode);
if (symbol == nullptr)
return;
if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
return;
Target &target = process_sp->GetTarget();
addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
if (symbol_address == LLDB_INVALID_ADDRESS)
return;
Breakpoint *breakpoint =
process_sp->GetTarget()
.CreateBreakpoint(symbol_address, /*internal=*/true,
/*hardware=*/false)
.get();
breakpoint->SetCallback(
UndefinedBehaviorSanitizerRuntime::NotifyBreakpointHit, this, true);
breakpoint->SetBreakpointKind("undefined-behavior-sanitizer-report");
SetBreakpointID(breakpoint->GetID());
SetActive(true);
}
void UndefinedBehaviorSanitizerRuntime::Deactivate() {
SetActive(false);
auto BID = GetBreakpointID();
if (BID == LLDB_INVALID_BREAK_ID)
return;
if (ProcessSP process_sp = GetProcessSP()) {
process_sp->GetTarget().RemoveBreakpointByID(BID);
SetBreakpointID(LLDB_INVALID_BREAK_ID);
}
}
lldb::ThreadCollectionSP
UndefinedBehaviorSanitizerRuntime::GetBacktracesFromExtendedStopInfo(
StructuredData::ObjectSP info) {
ThreadCollectionSP threads;
threads.reset(new ThreadCollection());
ProcessSP process_sp = GetProcessSP();
if (info->GetObjectForDotSeparatedPath("instrumentation_class")
->GetStringValue() != "UndefinedBehaviorSanitizer")
return threads;
std::vector<lldb::addr_t> PCs;
auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray();
trace->ForEach([&PCs](StructuredData::Object *PC) -> bool {
PCs.push_back(PC->GetAsInteger()->GetValue());
return true;
});
if (PCs.empty())
return threads;
StructuredData::ObjectSP thread_id_obj =
info->GetObjectForDotSeparatedPath("tid");
tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
uint32_t stop_id = 0;
bool stop_id_is_valid = false;
HistoryThread *history_thread =
new HistoryThread(*process_sp, tid, PCs, stop_id, stop_id_is_valid);
ThreadSP new_thread_sp(history_thread);
std::string stop_reason_description = GetStopReasonDescription(info);
new_thread_sp->SetName(stop_reason_description.c_str());
// Save this in the Process' ExtendedThreadList so a strong pointer
// retains the object
process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
threads->AddThread(new_thread_sp);
return threads;
}

View File

@@ -0,0 +1,69 @@
//===-- UndefinedBehaviorSanitizerRuntime.h ---------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_UndefinedBehaviorSanitizerRuntime_h_
#define liblldb_UndefinedBehaviorSanitizerRuntime_h_
#include "lldb/Target/ABI.h"
#include "lldb/Target/InstrumentationRuntime.h"
#include "lldb/Utility/StructuredData.h"
#include "lldb/lldb-private.h"
namespace lldb_private {
class UndefinedBehaviorSanitizerRuntime
: public lldb_private::InstrumentationRuntime {
public:
~UndefinedBehaviorSanitizerRuntime() override;
static lldb::InstrumentationRuntimeSP
CreateInstance(const lldb::ProcessSP &process_sp);
static void Initialize();
static void Terminate();
static lldb_private::ConstString GetPluginNameStatic();
static lldb::InstrumentationRuntimeType GetTypeStatic();
lldb_private::ConstString GetPluginName() override {
return GetPluginNameStatic();
}
virtual lldb::InstrumentationRuntimeType GetType() { return GetTypeStatic(); }
uint32_t GetPluginVersion() override { return 1; }
lldb::ThreadCollectionSP
GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) override;
private:
UndefinedBehaviorSanitizerRuntime(const lldb::ProcessSP &process_sp)
: lldb_private::InstrumentationRuntime(process_sp) {}
const RegularExpression &GetPatternForRuntimeLibrary() override;
bool CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override;
void Activate() override;
void Deactivate();
static bool NotifyBreakpointHit(void *baton,
StoppointCallbackContext *context,
lldb::user_id_t break_id,
lldb::user_id_t break_loc_id);
StructuredData::ObjectSP RetrieveReportData(ExecutionContextRef exe_ctx_ref);
};
} // namespace lldb_private
#endif // liblldb_UndefinedBehaviorSanitizerRuntime_h_