Path: blob/main/contrib/llvm-project/lldb/source/Plugins/InstrumentationRuntime/ASan/InstrumentationRuntimeASan.cpp
39645 views
//===-- InstrumentationRuntimeASan.cpp ------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "InstrumentationRuntimeASan.h"910#include "lldb/Breakpoint/StoppointCallbackContext.h"11#include "lldb/Core/Module.h"12#include "lldb/Core/PluginInterface.h"13#include "lldb/Core/PluginManager.h"14#include "lldb/Symbol/Symbol.h"15#include "lldb/Target/Process.h"16#include "lldb/Utility/RegularExpression.h"1718#include "Plugins/InstrumentationRuntime/Utility/ReportRetriever.h"1920using namespace lldb;21using namespace lldb_private;2223LLDB_PLUGIN_DEFINE(InstrumentationRuntimeASan)2425lldb::InstrumentationRuntimeSP26InstrumentationRuntimeASan::CreateInstance(const lldb::ProcessSP &process_sp) {27return InstrumentationRuntimeSP(new InstrumentationRuntimeASan(process_sp));28}2930void InstrumentationRuntimeASan::Initialize() {31PluginManager::RegisterPlugin(32GetPluginNameStatic(), "AddressSanitizer instrumentation runtime plugin.",33CreateInstance, GetTypeStatic);34}3536void InstrumentationRuntimeASan::Terminate() {37PluginManager::UnregisterPlugin(CreateInstance);38}3940lldb::InstrumentationRuntimeType InstrumentationRuntimeASan::GetTypeStatic() {41return eInstrumentationRuntimeTypeAddressSanitizer;42}4344InstrumentationRuntimeASan::~InstrumentationRuntimeASan() { Deactivate(); }4546const RegularExpression &47InstrumentationRuntimeASan::GetPatternForRuntimeLibrary() {48// FIXME: This shouldn't include the "dylib" suffix.49static RegularExpression regex(50llvm::StringRef("libclang_rt.asan_(.*)_dynamic\\.dylib"));51return regex;52}5354bool InstrumentationRuntimeASan::CheckIfRuntimeIsValid(55const lldb::ModuleSP module_sp) {56const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(57ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny);5859return symbol != nullptr;60}6162bool InstrumentationRuntimeASan::NotifyBreakpointHit(63void *baton, StoppointCallbackContext *context, user_id_t break_id,64user_id_t break_loc_id) {65assert(baton && "null baton");66if (!baton)67return false;6869InstrumentationRuntimeASan *const instance =70static_cast<InstrumentationRuntimeASan *>(baton);7172ProcessSP process_sp = instance->GetProcessSP();7374return ReportRetriever::NotifyBreakpointHit(process_sp, context, break_id,75break_loc_id);76}7778void InstrumentationRuntimeASan::Activate() {79if (IsActive())80return;8182ProcessSP process_sp = GetProcessSP();83if (!process_sp)84return;8586Breakpoint *breakpoint = ReportRetriever::SetupBreakpoint(87GetRuntimeModuleSP(), process_sp, ConstString("_ZN6__asanL7AsanDieEv"));8889if (!breakpoint)90return;9192const bool sync = false;9394breakpoint->SetCallback(InstrumentationRuntimeASan::NotifyBreakpointHit, this,95sync);96breakpoint->SetBreakpointKind("address-sanitizer-report");97SetBreakpointID(breakpoint->GetID());9899SetActive(true);100}101102void InstrumentationRuntimeASan::Deactivate() {103SetActive(false);104105if (GetBreakpointID() == LLDB_INVALID_BREAK_ID)106return;107108if (ProcessSP process_sp = GetProcessSP()) {109process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());110SetBreakpointID(LLDB_INVALID_BREAK_ID);111}112}113114115