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