| 1 | //===-- InstrumentationRuntimeUBSan.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 "InstrumentationRuntimeUBSan.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/Expression/UserExpression.h" |
| 18 | #include "lldb/Host/StreamFile.h" |
| 19 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 20 | #include "lldb/Symbol/Symbol.h" |
| 21 | #include "lldb/Symbol/SymbolContext.h" |
| 22 | #include "lldb/Symbol/Variable.h" |
| 23 | #include "lldb/Symbol/VariableList.h" |
| 24 | #include "lldb/Target/InstrumentationRuntimeStopInfo.h" |
| 25 | #include "lldb/Target/SectionLoadList.h" |
| 26 | #include "lldb/Target/StopInfo.h" |
| 27 | #include "lldb/Target/Target.h" |
| 28 | #include "lldb/Target/Thread.h" |
| 29 | #include "lldb/Utility/RegularExpression.h" |
| 30 | #include "lldb/Utility/Stream.h" |
| 31 | #include "lldb/ValueObject/ValueObject.h" |
| 32 | #include <cctype> |
| 33 | |
| 34 | #include <memory> |
| 35 | |
| 36 | using namespace lldb; |
| 37 | using namespace lldb_private; |
| 38 | |
| 39 | LLDB_PLUGIN_DEFINE(InstrumentationRuntimeUBSan) |
| 40 | |
| 41 | InstrumentationRuntimeUBSan::~InstrumentationRuntimeUBSan() { Deactivate(); } |
| 42 | |
| 43 | lldb::InstrumentationRuntimeSP |
| 44 | InstrumentationRuntimeUBSan::CreateInstance(const lldb::ProcessSP &process_sp) { |
| 45 | return InstrumentationRuntimeSP(new InstrumentationRuntimeUBSan(process_sp)); |
| 46 | } |
| 47 | |
| 48 | void InstrumentationRuntimeUBSan::Initialize() { |
| 49 | PluginManager::RegisterPlugin( |
| 50 | name: GetPluginNameStatic(), |
| 51 | description: "UndefinedBehaviorSanitizer instrumentation runtime plugin." , |
| 52 | create_callback: CreateInstance, get_type_callback: GetTypeStatic); |
| 53 | } |
| 54 | |
| 55 | void InstrumentationRuntimeUBSan::Terminate() { |
| 56 | PluginManager::UnregisterPlugin(create_callback: CreateInstance); |
| 57 | } |
| 58 | |
| 59 | lldb::InstrumentationRuntimeType InstrumentationRuntimeUBSan::GetTypeStatic() { |
| 60 | return eInstrumentationRuntimeTypeUndefinedBehaviorSanitizer; |
| 61 | } |
| 62 | |
| 63 | static const char *ub_sanitizer_retrieve_report_data_prefix = R"( |
| 64 | extern "C" { |
| 65 | void |
| 66 | __ubsan_get_current_report_data(const char **OutIssueKind, |
| 67 | const char **OutMessage, const char **OutFilename, unsigned *OutLine, |
| 68 | unsigned *OutCol, char **OutMemoryAddr); |
| 69 | } |
| 70 | )" ; |
| 71 | |
| 72 | static const char *ub_sanitizer_retrieve_report_data_command = R"( |
| 73 | struct { |
| 74 | const char *issue_kind; |
| 75 | const char *message; |
| 76 | const char *filename; |
| 77 | unsigned line; |
| 78 | unsigned col; |
| 79 | char *memory_addr; |
| 80 | } t; |
| 81 | |
| 82 | __ubsan_get_current_report_data(&t.issue_kind, &t.message, &t.filename, &t.line, |
| 83 | &t.col, &t.memory_addr); |
| 84 | t; |
| 85 | )" ; |
| 86 | |
| 87 | static addr_t RetrieveUnsigned(ValueObjectSP return_value_sp, |
| 88 | ProcessSP process_sp, |
| 89 | const std::string &expression_path) { |
| 90 | return return_value_sp->GetValueForExpressionPath(expression: expression_path.c_str()) |
| 91 | ->GetValueAsUnsigned(fail_value: 0); |
| 92 | } |
| 93 | |
| 94 | static std::string RetrieveString(ValueObjectSP return_value_sp, |
| 95 | ProcessSP process_sp, |
| 96 | const std::string &expression_path) { |
| 97 | addr_t ptr = RetrieveUnsigned(return_value_sp, process_sp, expression_path); |
| 98 | std::string str; |
| 99 | Status error; |
| 100 | process_sp->ReadCStringFromMemory(vm_addr: ptr, out_str&: str, error); |
| 101 | return str; |
| 102 | } |
| 103 | |
| 104 | StructuredData::ObjectSP InstrumentationRuntimeUBSan::RetrieveReportData( |
| 105 | ExecutionContextRef exe_ctx_ref) { |
| 106 | ProcessSP process_sp = GetProcessSP(); |
| 107 | if (!process_sp) |
| 108 | return StructuredData::ObjectSP(); |
| 109 | |
| 110 | ThreadSP thread_sp = exe_ctx_ref.GetThreadSP(); |
| 111 | StackFrameSP frame_sp = |
| 112 | thread_sp->GetSelectedFrame(select_most_relevant: DoNoSelectMostRelevantFrame); |
| 113 | ModuleSP runtime_module_sp = GetRuntimeModuleSP(); |
| 114 | Target &target = process_sp->GetTarget(); |
| 115 | |
| 116 | if (!frame_sp) |
| 117 | return StructuredData::ObjectSP(); |
| 118 | |
| 119 | EvaluateExpressionOptions options; |
| 120 | options.SetUnwindOnError(true); |
| 121 | options.SetTryAllThreads(true); |
| 122 | options.SetStopOthers(true); |
| 123 | options.SetIgnoreBreakpoints(true); |
| 124 | options.SetTimeout(process_sp->GetUtilityExpressionTimeout()); |
| 125 | options.SetPrefix(ub_sanitizer_retrieve_report_data_prefix); |
| 126 | options.SetAutoApplyFixIts(false); |
| 127 | options.SetLanguage(eLanguageTypeObjC_plus_plus); |
| 128 | |
| 129 | ValueObjectSP main_value; |
| 130 | ExecutionContext exe_ctx; |
| 131 | frame_sp->CalculateExecutionContext(exe_ctx); |
| 132 | ExpressionResults result = UserExpression::Evaluate( |
| 133 | exe_ctx, options, expr_cstr: ub_sanitizer_retrieve_report_data_command, expr_prefix: "" , |
| 134 | result_valobj_sp&: main_value); |
| 135 | if (result != eExpressionCompleted) { |
| 136 | StreamString ss; |
| 137 | ss << "cannot evaluate UndefinedBehaviorSanitizer expression:\n" ; |
| 138 | if (main_value) |
| 139 | ss << main_value->GetError().AsCString(); |
| 140 | Debugger::ReportWarning(message: ss.GetString().str(), |
| 141 | debugger_id: process_sp->GetTarget().GetDebugger().GetID()); |
| 142 | return StructuredData::ObjectSP(); |
| 143 | } |
| 144 | |
| 145 | // Gather the PCs of the user frames in the backtrace. |
| 146 | StructuredData::Array *trace = new StructuredData::Array(); |
| 147 | auto trace_sp = StructuredData::ObjectSP(trace); |
| 148 | for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) { |
| 149 | const Address FCA = thread_sp->GetStackFrameAtIndex(idx: I) |
| 150 | ->GetFrameCodeAddressForSymbolication(); |
| 151 | if (FCA.GetModule() == runtime_module_sp) // Skip PCs from the runtime. |
| 152 | continue; |
| 153 | |
| 154 | lldb::addr_t PC = FCA.GetLoadAddress(target: &target); |
| 155 | trace->AddIntegerItem(value: PC); |
| 156 | } |
| 157 | |
| 158 | std::string IssueKind = RetrieveString(return_value_sp: main_value, process_sp, expression_path: ".issue_kind" ); |
| 159 | std::string ErrMessage = RetrieveString(return_value_sp: main_value, process_sp, expression_path: ".message" ); |
| 160 | std::string Filename = RetrieveString(return_value_sp: main_value, process_sp, expression_path: ".filename" ); |
| 161 | unsigned Line = RetrieveUnsigned(return_value_sp: main_value, process_sp, expression_path: ".line" ); |
| 162 | unsigned Col = RetrieveUnsigned(return_value_sp: main_value, process_sp, expression_path: ".col" ); |
| 163 | uintptr_t MemoryAddr = |
| 164 | RetrieveUnsigned(return_value_sp: main_value, process_sp, expression_path: ".memory_addr" ); |
| 165 | |
| 166 | auto *d = new StructuredData::Dictionary(); |
| 167 | auto dict_sp = StructuredData::ObjectSP(d); |
| 168 | d->AddStringItem(key: "instrumentation_class" , value: "UndefinedBehaviorSanitizer" ); |
| 169 | d->AddStringItem(key: "description" , value: IssueKind); |
| 170 | d->AddStringItem(key: "summary" , value: ErrMessage); |
| 171 | d->AddStringItem(key: "filename" , value: Filename); |
| 172 | d->AddIntegerItem(key: "line" , value: Line); |
| 173 | d->AddIntegerItem(key: "col" , value: Col); |
| 174 | d->AddIntegerItem(key: "memory_address" , value: MemoryAddr); |
| 175 | d->AddIntegerItem(key: "tid" , value: thread_sp->GetID()); |
| 176 | d->AddItem(key: "trace" , value_sp: trace_sp); |
| 177 | return dict_sp; |
| 178 | } |
| 179 | |
| 180 | static std::string GetStopReasonDescription(StructuredData::ObjectSP report) { |
| 181 | llvm::StringRef stop_reason_description_ref; |
| 182 | report->GetAsDictionary()->GetValueForKeyAsString( |
| 183 | key: "description" , result&: stop_reason_description_ref); |
| 184 | std::string stop_reason_description = |
| 185 | std::string(stop_reason_description_ref); |
| 186 | |
| 187 | if (!stop_reason_description.size()) { |
| 188 | stop_reason_description = "Undefined behavior detected" ; |
| 189 | } else { |
| 190 | stop_reason_description[0] = toupper(c: stop_reason_description[0]); |
| 191 | for (unsigned I = 1; I < stop_reason_description.size(); ++I) |
| 192 | if (stop_reason_description[I] == '-') |
| 193 | stop_reason_description[I] = ' '; |
| 194 | } |
| 195 | return stop_reason_description; |
| 196 | } |
| 197 | |
| 198 | bool InstrumentationRuntimeUBSan::NotifyBreakpointHit( |
| 199 | void *baton, StoppointCallbackContext *context, user_id_t break_id, |
| 200 | user_id_t break_loc_id) { |
| 201 | assert(baton && "null baton" ); |
| 202 | if (!baton) |
| 203 | return false; ///< false => resume execution. |
| 204 | |
| 205 | InstrumentationRuntimeUBSan *const instance = |
| 206 | static_cast<InstrumentationRuntimeUBSan *>(baton); |
| 207 | |
| 208 | ProcessSP process_sp = instance->GetProcessSP(); |
| 209 | ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); |
| 210 | if (!process_sp || !thread_sp || |
| 211 | process_sp != context->exe_ctx_ref.GetProcessSP()) |
| 212 | return false; |
| 213 | |
| 214 | if (process_sp->GetModIDRef().IsLastResumeForUserExpression()) |
| 215 | return false; |
| 216 | |
| 217 | StructuredData::ObjectSP report = |
| 218 | instance->RetrieveReportData(exe_ctx_ref: context->exe_ctx_ref); |
| 219 | |
| 220 | if (report) { |
| 221 | thread_sp->SetStopInfo( |
| 222 | InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData( |
| 223 | thread&: *thread_sp, description: GetStopReasonDescription(report), additional_data: report)); |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | const RegularExpression & |
| 231 | InstrumentationRuntimeUBSan::GetPatternForRuntimeLibrary() { |
| 232 | static RegularExpression regex(llvm::StringRef("libclang_rt\\.(a|t|ub)san_" )); |
| 233 | return regex; |
| 234 | } |
| 235 | |
| 236 | bool InstrumentationRuntimeUBSan::CheckIfRuntimeIsValid( |
| 237 | const lldb::ModuleSP module_sp) { |
| 238 | static ConstString ubsan_test_sym("__ubsan_on_report" ); |
| 239 | const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType( |
| 240 | name: ubsan_test_sym, symbol_type: lldb::eSymbolTypeAny); |
| 241 | return symbol != nullptr; |
| 242 | } |
| 243 | |
| 244 | // FIXME: Factor out all the logic we have in common with the {a,t}san plugins. |
| 245 | void InstrumentationRuntimeUBSan::Activate() { |
| 246 | if (IsActive()) |
| 247 | return; |
| 248 | |
| 249 | ProcessSP process_sp = GetProcessSP(); |
| 250 | if (!process_sp) |
| 251 | return; |
| 252 | |
| 253 | ModuleSP runtime_module_sp = GetRuntimeModuleSP(); |
| 254 | |
| 255 | ConstString symbol_name("__ubsan_on_report" ); |
| 256 | const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType( |
| 257 | name: symbol_name, symbol_type: eSymbolTypeCode); |
| 258 | |
| 259 | if (symbol == nullptr) |
| 260 | return; |
| 261 | |
| 262 | if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) |
| 263 | return; |
| 264 | |
| 265 | Target &target = process_sp->GetTarget(); |
| 266 | addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(target: &target); |
| 267 | |
| 268 | if (symbol_address == LLDB_INVALID_ADDRESS) |
| 269 | return; |
| 270 | |
| 271 | Breakpoint *breakpoint = |
| 272 | process_sp->GetTarget() |
| 273 | .CreateBreakpoint(load_addr: symbol_address, /*internal=*/true, |
| 274 | /*hardware=*/request_hardware: false) |
| 275 | .get(); |
| 276 | const bool sync = false; |
| 277 | breakpoint->SetCallback(callback: InstrumentationRuntimeUBSan::NotifyBreakpointHit, |
| 278 | baton: this, is_synchronous: sync); |
| 279 | breakpoint->SetBreakpointKind("undefined-behavior-sanitizer-report" ); |
| 280 | SetBreakpointID(breakpoint->GetID()); |
| 281 | |
| 282 | SetActive(true); |
| 283 | } |
| 284 | |
| 285 | void InstrumentationRuntimeUBSan::Deactivate() { |
| 286 | SetActive(false); |
| 287 | |
| 288 | auto BID = GetBreakpointID(); |
| 289 | if (BID == LLDB_INVALID_BREAK_ID) |
| 290 | return; |
| 291 | |
| 292 | if (ProcessSP process_sp = GetProcessSP()) { |
| 293 | process_sp->GetTarget().RemoveBreakpointByID(break_id: BID); |
| 294 | SetBreakpointID(LLDB_INVALID_BREAK_ID); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | lldb::ThreadCollectionSP |
| 299 | InstrumentationRuntimeUBSan::GetBacktracesFromExtendedStopInfo( |
| 300 | StructuredData::ObjectSP info) { |
| 301 | ThreadCollectionSP threads; |
| 302 | threads = std::make_shared<ThreadCollection>(); |
| 303 | |
| 304 | ProcessSP process_sp = GetProcessSP(); |
| 305 | |
| 306 | if (info->GetObjectForDotSeparatedPath(path: "instrumentation_class" ) |
| 307 | ->GetStringValue() != "UndefinedBehaviorSanitizer" ) |
| 308 | return threads; |
| 309 | |
| 310 | std::vector<lldb::addr_t> PCs; |
| 311 | auto trace = info->GetObjectForDotSeparatedPath(path: "trace" )->GetAsArray(); |
| 312 | trace->ForEach(foreach_callback: [&PCs](StructuredData::Object *PC) -> bool { |
| 313 | PCs.push_back(x: PC->GetUnsignedIntegerValue()); |
| 314 | return true; |
| 315 | }); |
| 316 | |
| 317 | if (PCs.empty()) |
| 318 | return threads; |
| 319 | |
| 320 | StructuredData::ObjectSP thread_id_obj = |
| 321 | info->GetObjectForDotSeparatedPath(path: "tid" ); |
| 322 | lldb::tid_t tid = |
| 323 | thread_id_obj ? thread_id_obj->GetUnsignedIntegerValue() : 0; |
| 324 | |
| 325 | // We gather symbolication addresses above, so no need for HistoryThread to |
| 326 | // try to infer the call addresses. |
| 327 | bool pcs_are_call_addresses = true; |
| 328 | ThreadSP new_thread_sp = std::make_shared<HistoryThread>( |
| 329 | args&: *process_sp, args&: tid, args&: PCs, args&: pcs_are_call_addresses); |
| 330 | std::string stop_reason_description = GetStopReasonDescription(report: info); |
| 331 | new_thread_sp->SetName(stop_reason_description.c_str()); |
| 332 | |
| 333 | // Save this in the Process' ExtendedThreadList so a strong pointer retains |
| 334 | // the object |
| 335 | process_sp->GetExtendedThreadList().AddThread(thread_sp: new_thread_sp); |
| 336 | threads->AddThread(thread_sp: new_thread_sp); |
| 337 | |
| 338 | return threads; |
| 339 | } |
| 340 | |