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