114 lines · cpp
1//===-- InstrumentationRuntimeASan.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "InstrumentationRuntimeASan.h"10 11#include "lldb/Breakpoint/StoppointCallbackContext.h"12#include "lldb/Core/Module.h"13#include "lldb/Core/PluginInterface.h"14#include "lldb/Core/PluginManager.h"15#include "lldb/Symbol/Symbol.h"16#include "lldb/Target/Process.h"17#include "lldb/Utility/RegularExpression.h"18 19#include "Plugins/InstrumentationRuntime/Utility/ReportRetriever.h"20 21using namespace lldb;22using namespace lldb_private;23 24LLDB_PLUGIN_DEFINE(InstrumentationRuntimeASan)25 26lldb::InstrumentationRuntimeSP27InstrumentationRuntimeASan::CreateInstance(const lldb::ProcessSP &process_sp) {28 return InstrumentationRuntimeSP(new InstrumentationRuntimeASan(process_sp));29}30 31void InstrumentationRuntimeASan::Initialize() {32 PluginManager::RegisterPlugin(33 GetPluginNameStatic(), "AddressSanitizer instrumentation runtime plugin.",34 CreateInstance, GetTypeStatic);35}36 37void InstrumentationRuntimeASan::Terminate() {38 PluginManager::UnregisterPlugin(CreateInstance);39}40 41lldb::InstrumentationRuntimeType InstrumentationRuntimeASan::GetTypeStatic() {42 return eInstrumentationRuntimeTypeAddressSanitizer;43}44 45InstrumentationRuntimeASan::~InstrumentationRuntimeASan() { Deactivate(); }46 47const RegularExpression &48InstrumentationRuntimeASan::GetPatternForRuntimeLibrary() {49 // FIXME: This shouldn't include the "dylib" suffix.50 static RegularExpression regex(51 llvm::StringRef("libclang_rt.asan_(.*)_dynamic\\.dylib"));52 return regex;53}54 55bool InstrumentationRuntimeASan::CheckIfRuntimeIsValid(56 const lldb::ModuleSP module_sp) {57 const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(58 ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny);59 60 return symbol != nullptr;61}62 63bool InstrumentationRuntimeASan::NotifyBreakpointHit(64 void *baton, StoppointCallbackContext *context, user_id_t break_id,65 user_id_t break_loc_id) {66 assert(baton && "null baton");67 if (!baton)68 return false;69 70 InstrumentationRuntimeASan *const instance =71 static_cast<InstrumentationRuntimeASan *>(baton);72 73 ProcessSP process_sp = instance->GetProcessSP();74 75 return ReportRetriever::NotifyBreakpointHit(process_sp, context, break_id,76 break_loc_id);77}78 79void InstrumentationRuntimeASan::Activate() {80 if (IsActive())81 return;82 83 ProcessSP process_sp = GetProcessSP();84 if (!process_sp)85 return;86 87 Breakpoint *breakpoint = ReportRetriever::SetupBreakpoint(88 GetRuntimeModuleSP(), process_sp, ConstString("_ZN6__asanL7AsanDieEv"));89 90 if (!breakpoint)91 return;92 93 const bool sync = false;94 95 breakpoint->SetCallback(InstrumentationRuntimeASan::NotifyBreakpointHit, this,96 sync);97 breakpoint->SetBreakpointKind("address-sanitizer-report");98 SetBreakpointID(breakpoint->GetID());99 100 SetActive(true);101}102 103void InstrumentationRuntimeASan::Deactivate() {104 SetActive(false);105 106 if (GetBreakpointID() == LLDB_INVALID_BREAK_ID)107 return;108 109 if (ProcessSP process_sp = GetProcessSP()) {110 process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());111 SetBreakpointID(LLDB_INVALID_BREAK_ID);112 }113}114