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