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