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