1 //===-- MemoryHistoryASan.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 "MemoryHistoryASan.h" 10 11 #include "lldb/Target/MemoryHistory.h" 12 13 #include "Plugins/Process/Utility/HistoryThread.h" 14 #include "lldb/Core/Debugger.h" 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/PluginInterface.h" 17 #include "lldb/Core/PluginManager.h" 18 #include "lldb/Core/ValueObject.h" 19 #include "lldb/Expression/UserExpression.h" 20 #include "lldb/Target/ExecutionContext.h" 21 #include "lldb/Target/Target.h" 22 #include "lldb/Target/Thread.h" 23 #include "lldb/Target/ThreadList.h" 24 #include "lldb/lldb-private.h" 25 26 #include <sstream> 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 LLDB_PLUGIN_DEFINE(MemoryHistoryASan) 32 33 MemoryHistorySP MemoryHistoryASan::CreateInstance(const ProcessSP &process_sp) { 34 if (!process_sp.get()) 35 return nullptr; 36 37 Target &target = process_sp->GetTarget(); 38 39 const ModuleList &target_modules = target.GetImages(); 40 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); 41 const size_t num_modules = target_modules.GetSize(); 42 for (size_t i = 0; i < num_modules; ++i) { 43 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i); 44 45 const Symbol *symbol = module_pointer->FindFirstSymbolWithNameAndType( 46 ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny); 47 48 if (symbol != nullptr) 49 return MemoryHistorySP(new MemoryHistoryASan(process_sp)); 50 } 51 52 return MemoryHistorySP(); 53 } 54 55 void MemoryHistoryASan::Initialize() { 56 PluginManager::RegisterPlugin( 57 GetPluginNameStatic(), "ASan memory history provider.", CreateInstance); 58 } 59 60 void MemoryHistoryASan::Terminate() { 61 PluginManager::UnregisterPlugin(CreateInstance); 62 } 63 64 ConstString MemoryHistoryASan::GetPluginNameStatic() { 65 static ConstString g_name("asan"); 66 return g_name; 67 } 68 69 MemoryHistoryASan::MemoryHistoryASan(const ProcessSP &process_sp) { 70 if (process_sp) 71 m_process_wp = process_sp; 72 } 73 74 const char *memory_history_asan_command_prefix = R"( 75 extern "C" 76 { 77 size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size, int *thread_id); 78 size_t __asan_get_free_stack(void *addr, void **trace, size_t size, int *thread_id); 79 } 80 81 struct data { 82 void *alloc_trace[256]; 83 size_t alloc_count; 84 int alloc_tid; 85 86 void *free_trace[256]; 87 size_t free_count; 88 int free_tid; 89 }; 90 )"; 91 92 const char *memory_history_asan_command_format = 93 R"( 94 data t; 95 96 t.alloc_count = __asan_get_alloc_stack((void *)0x%)" PRIx64 97 R"(, t.alloc_trace, 256, &t.alloc_tid); 98 t.free_count = __asan_get_free_stack((void *)0x%)" PRIx64 99 R"(, t.free_trace, 256, &t.free_tid); 100 101 t; 102 )"; 103 104 static void CreateHistoryThreadFromValueObject(ProcessSP process_sp, 105 ValueObjectSP return_value_sp, 106 const char *type, 107 const char *thread_name, 108 HistoryThreads &result) { 109 std::string count_path = "." + std::string(type) + "_count"; 110 std::string tid_path = "." + std::string(type) + "_tid"; 111 std::string trace_path = "." + std::string(type) + "_trace"; 112 113 ValueObjectSP count_sp = 114 return_value_sp->GetValueForExpressionPath(count_path.c_str()); 115 ValueObjectSP tid_sp = 116 return_value_sp->GetValueForExpressionPath(tid_path.c_str()); 117 118 if (!count_sp || !tid_sp) 119 return; 120 121 int count = count_sp->GetValueAsUnsigned(0); 122 tid_t tid = tid_sp->GetValueAsUnsigned(0) + 1; 123 124 if (count <= 0) 125 return; 126 127 ValueObjectSP trace_sp = 128 return_value_sp->GetValueForExpressionPath(trace_path.c_str()); 129 130 if (!trace_sp) 131 return; 132 133 std::vector<lldb::addr_t> pcs; 134 for (int i = 0; i < count; i++) { 135 addr_t pc = trace_sp->GetChildAtIndex(i, true)->GetValueAsUnsigned(0); 136 if (pc == 0 || pc == 1 || pc == LLDB_INVALID_ADDRESS) 137 continue; 138 pcs.push_back(pc); 139 } 140 141 HistoryThread *history_thread = new HistoryThread(*process_sp, tid, pcs); 142 ThreadSP new_thread_sp(history_thread); 143 std::ostringstream thread_name_with_number; 144 thread_name_with_number << thread_name << " Thread " << tid; 145 history_thread->SetThreadName(thread_name_with_number.str().c_str()); 146 // Save this in the Process' ExtendedThreadList so a strong pointer retains 147 // the object 148 process_sp->GetExtendedThreadList().AddThread(new_thread_sp); 149 result.push_back(new_thread_sp); 150 } 151 152 HistoryThreads MemoryHistoryASan::GetHistoryThreads(lldb::addr_t address) { 153 HistoryThreads result; 154 155 ProcessSP process_sp = m_process_wp.lock(); 156 if (!process_sp) 157 return result; 158 159 ThreadSP thread_sp = 160 process_sp->GetThreadList().GetExpressionExecutionThread(); 161 if (!thread_sp) 162 return result; 163 164 StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); 165 if (!frame_sp) 166 return result; 167 168 ExecutionContext exe_ctx(frame_sp); 169 ValueObjectSP return_value_sp; 170 StreamString expr; 171 Status eval_error; 172 expr.Printf(memory_history_asan_command_format, address, address); 173 174 EvaluateExpressionOptions options; 175 options.SetUnwindOnError(true); 176 options.SetTryAllThreads(true); 177 options.SetStopOthers(true); 178 options.SetIgnoreBreakpoints(true); 179 options.SetTimeout(process_sp->GetUtilityExpressionTimeout()); 180 options.SetPrefix(memory_history_asan_command_prefix); 181 options.SetAutoApplyFixIts(false); 182 options.SetLanguage(eLanguageTypeObjC_plus_plus); 183 184 ExpressionResults expr_result = UserExpression::Evaluate( 185 exe_ctx, options, expr.GetString(), "", return_value_sp, eval_error); 186 if (expr_result != eExpressionCompleted) { 187 process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf( 188 "Warning: Cannot evaluate AddressSanitizer expression:\n%s\n", 189 eval_error.AsCString()); 190 return result; 191 } 192 193 if (!return_value_sp) 194 return result; 195 196 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "free", 197 "Memory deallocated by", result); 198 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "alloc", 199 "Memory allocated by", result); 200 201 return result; 202 } 203