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   for (ModuleSP module_sp : target.GetImages().Modules()) {
40     const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
41         ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny);
42 
43     if (symbol != nullptr)
44       return MemoryHistorySP(new MemoryHistoryASan(process_sp));
45   }
46 
47   return MemoryHistorySP();
48 }
49 
50 void MemoryHistoryASan::Initialize() {
51   PluginManager::RegisterPlugin(
52       GetPluginNameStatic(), "ASan memory history provider.", CreateInstance);
53 }
54 
55 void MemoryHistoryASan::Terminate() {
56   PluginManager::UnregisterPlugin(CreateInstance);
57 }
58 
59 ConstString MemoryHistoryASan::GetPluginNameStatic() {
60   static ConstString g_name("asan");
61   return g_name;
62 }
63 
64 MemoryHistoryASan::MemoryHistoryASan(const ProcessSP &process_sp) {
65   if (process_sp)
66     m_process_wp = process_sp;
67 }
68 
69 const char *memory_history_asan_command_prefix = R"(
70     extern "C"
71     {
72         size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size, int *thread_id);
73         size_t __asan_get_free_stack(void *addr, void **trace, size_t size, int *thread_id);
74     }
75 
76     struct data {
77         void *alloc_trace[256];
78         size_t alloc_count;
79         int alloc_tid;
80 
81         void *free_trace[256];
82         size_t free_count;
83         int free_tid;
84     };
85 )";
86 
87 const char *memory_history_asan_command_format =
88     R"(
89     data t;
90 
91     t.alloc_count = __asan_get_alloc_stack((void *)0x%)" PRIx64
92     R"(, t.alloc_trace, 256, &t.alloc_tid);
93     t.free_count = __asan_get_free_stack((void *)0x%)" PRIx64
94     R"(, t.free_trace, 256, &t.free_tid);
95 
96     t;
97 )";
98 
99 static void CreateHistoryThreadFromValueObject(ProcessSP process_sp,
100                                                ValueObjectSP return_value_sp,
101                                                const char *type,
102                                                const char *thread_name,
103                                                HistoryThreads &result) {
104   std::string count_path = "." + std::string(type) + "_count";
105   std::string tid_path = "." + std::string(type) + "_tid";
106   std::string trace_path = "." + std::string(type) + "_trace";
107 
108   ValueObjectSP count_sp =
109       return_value_sp->GetValueForExpressionPath(count_path.c_str());
110   ValueObjectSP tid_sp =
111       return_value_sp->GetValueForExpressionPath(tid_path.c_str());
112 
113   if (!count_sp || !tid_sp)
114     return;
115 
116   int count = count_sp->GetValueAsUnsigned(0);
117   tid_t tid = tid_sp->GetValueAsUnsigned(0) + 1;
118 
119   if (count <= 0)
120     return;
121 
122   ValueObjectSP trace_sp =
123       return_value_sp->GetValueForExpressionPath(trace_path.c_str());
124 
125   if (!trace_sp)
126     return;
127 
128   std::vector<lldb::addr_t> pcs;
129   for (int i = 0; i < count; i++) {
130     addr_t pc = trace_sp->GetChildAtIndex(i, true)->GetValueAsUnsigned(0);
131     if (pc == 0 || pc == 1 || pc == LLDB_INVALID_ADDRESS)
132       continue;
133     pcs.push_back(pc);
134   }
135 
136   // The ASAN runtime already massages the return addresses into call
137   // addresses, we don't want LLDB's unwinder to try to locate the previous
138   // instruction again as this might lead to us reporting a different line.
139   bool pcs_are_call_addresses = true;
140   HistoryThread *history_thread =
141       new HistoryThread(*process_sp, tid, pcs, pcs_are_call_addresses);
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