15ffd83dbSDimitry Andric //===-- InstrumentationRuntimeASan.cpp ------------------------------------===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric
95ffd83dbSDimitry Andric #include "InstrumentationRuntimeASan.h"
105ffd83dbSDimitry Andric
115ffd83dbSDimitry Andric #include "lldb/Breakpoint/StoppointCallbackContext.h"
125ffd83dbSDimitry Andric #include "lldb/Core/Module.h"
135ffd83dbSDimitry Andric #include "lldb/Core/PluginInterface.h"
145ffd83dbSDimitry Andric #include "lldb/Core/PluginManager.h"
155ffd83dbSDimitry Andric #include "lldb/Symbol/Symbol.h"
16*5f757f3fSDimitry Andric #include "lldb/Target/Process.h"
175ffd83dbSDimitry Andric #include "lldb/Utility/RegularExpression.h"
185ffd83dbSDimitry Andric
19*5f757f3fSDimitry Andric #include "Plugins/InstrumentationRuntime/Utility/ReportRetriever.h"
205ffd83dbSDimitry Andric
215ffd83dbSDimitry Andric using namespace lldb;
225ffd83dbSDimitry Andric using namespace lldb_private;
235ffd83dbSDimitry Andric
LLDB_PLUGIN_DEFINE(InstrumentationRuntimeASan)245ffd83dbSDimitry Andric LLDB_PLUGIN_DEFINE(InstrumentationRuntimeASan)
255ffd83dbSDimitry Andric
265ffd83dbSDimitry Andric lldb::InstrumentationRuntimeSP
275ffd83dbSDimitry Andric InstrumentationRuntimeASan::CreateInstance(const lldb::ProcessSP &process_sp) {
285ffd83dbSDimitry Andric return InstrumentationRuntimeSP(new InstrumentationRuntimeASan(process_sp));
295ffd83dbSDimitry Andric }
305ffd83dbSDimitry Andric
Initialize()315ffd83dbSDimitry Andric void InstrumentationRuntimeASan::Initialize() {
325ffd83dbSDimitry Andric PluginManager::RegisterPlugin(
335ffd83dbSDimitry Andric GetPluginNameStatic(), "AddressSanitizer instrumentation runtime plugin.",
345ffd83dbSDimitry Andric CreateInstance, GetTypeStatic);
355ffd83dbSDimitry Andric }
365ffd83dbSDimitry Andric
Terminate()375ffd83dbSDimitry Andric void InstrumentationRuntimeASan::Terminate() {
385ffd83dbSDimitry Andric PluginManager::UnregisterPlugin(CreateInstance);
395ffd83dbSDimitry Andric }
405ffd83dbSDimitry Andric
GetTypeStatic()415ffd83dbSDimitry Andric lldb::InstrumentationRuntimeType InstrumentationRuntimeASan::GetTypeStatic() {
425ffd83dbSDimitry Andric return eInstrumentationRuntimeTypeAddressSanitizer;
435ffd83dbSDimitry Andric }
445ffd83dbSDimitry Andric
~InstrumentationRuntimeASan()455ffd83dbSDimitry Andric InstrumentationRuntimeASan::~InstrumentationRuntimeASan() { Deactivate(); }
465ffd83dbSDimitry Andric
475ffd83dbSDimitry Andric const RegularExpression &
GetPatternForRuntimeLibrary()485ffd83dbSDimitry Andric InstrumentationRuntimeASan::GetPatternForRuntimeLibrary() {
495ffd83dbSDimitry Andric // FIXME: This shouldn't include the "dylib" suffix.
505ffd83dbSDimitry Andric static RegularExpression regex(
515ffd83dbSDimitry Andric llvm::StringRef("libclang_rt.asan_(.*)_dynamic\\.dylib"));
525ffd83dbSDimitry Andric return regex;
535ffd83dbSDimitry Andric }
545ffd83dbSDimitry Andric
CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp)555ffd83dbSDimitry Andric bool InstrumentationRuntimeASan::CheckIfRuntimeIsValid(
565ffd83dbSDimitry Andric const lldb::ModuleSP module_sp) {
575ffd83dbSDimitry Andric const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
585ffd83dbSDimitry Andric ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny);
595ffd83dbSDimitry Andric
605ffd83dbSDimitry Andric return symbol != nullptr;
615ffd83dbSDimitry Andric }
625ffd83dbSDimitry Andric
NotifyBreakpointHit(void * baton,StoppointCallbackContext * context,user_id_t break_id,user_id_t break_loc_id)635ffd83dbSDimitry Andric bool InstrumentationRuntimeASan::NotifyBreakpointHit(
645ffd83dbSDimitry Andric void *baton, StoppointCallbackContext *context, user_id_t break_id,
655ffd83dbSDimitry Andric user_id_t break_loc_id) {
665ffd83dbSDimitry Andric assert(baton && "null baton");
675ffd83dbSDimitry Andric if (!baton)
685ffd83dbSDimitry Andric return false;
695ffd83dbSDimitry Andric
705ffd83dbSDimitry Andric InstrumentationRuntimeASan *const instance =
715ffd83dbSDimitry Andric static_cast<InstrumentationRuntimeASan *>(baton);
725ffd83dbSDimitry Andric
735ffd83dbSDimitry Andric ProcessSP process_sp = instance->GetProcessSP();
745ffd83dbSDimitry Andric
75*5f757f3fSDimitry Andric return ReportRetriever::NotifyBreakpointHit(process_sp, context, break_id,
76*5f757f3fSDimitry Andric break_loc_id);
775ffd83dbSDimitry Andric }
785ffd83dbSDimitry Andric
Activate()795ffd83dbSDimitry Andric void InstrumentationRuntimeASan::Activate() {
805ffd83dbSDimitry Andric if (IsActive())
815ffd83dbSDimitry Andric return;
825ffd83dbSDimitry Andric
835ffd83dbSDimitry Andric ProcessSP process_sp = GetProcessSP();
845ffd83dbSDimitry Andric if (!process_sp)
855ffd83dbSDimitry Andric return;
865ffd83dbSDimitry Andric
87*5f757f3fSDimitry Andric Breakpoint *breakpoint = ReportRetriever::SetupBreakpoint(
88*5f757f3fSDimitry Andric GetRuntimeModuleSP(), process_sp, ConstString("_ZN6__asanL7AsanDieEv"));
895ffd83dbSDimitry Andric
90*5f757f3fSDimitry Andric if (!breakpoint)
915ffd83dbSDimitry Andric return;
925ffd83dbSDimitry Andric
93bdd1243dSDimitry Andric const bool sync = false;
94*5f757f3fSDimitry Andric
955ffd83dbSDimitry Andric breakpoint->SetCallback(InstrumentationRuntimeASan::NotifyBreakpointHit, this,
96bdd1243dSDimitry Andric sync);
975ffd83dbSDimitry Andric breakpoint->SetBreakpointKind("address-sanitizer-report");
985ffd83dbSDimitry Andric SetBreakpointID(breakpoint->GetID());
995ffd83dbSDimitry Andric
1005ffd83dbSDimitry Andric SetActive(true);
1015ffd83dbSDimitry Andric }
1025ffd83dbSDimitry Andric
Deactivate()1035ffd83dbSDimitry Andric void InstrumentationRuntimeASan::Deactivate() {
104*5f757f3fSDimitry Andric SetActive(false);
105*5f757f3fSDimitry Andric
106*5f757f3fSDimitry Andric if (GetBreakpointID() == LLDB_INVALID_BREAK_ID)
107*5f757f3fSDimitry Andric return;
108*5f757f3fSDimitry Andric
109*5f757f3fSDimitry Andric if (ProcessSP process_sp = GetProcessSP()) {
1105ffd83dbSDimitry Andric process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
1115ffd83dbSDimitry Andric SetBreakpointID(LLDB_INVALID_BREAK_ID);
1125ffd83dbSDimitry Andric }
1135ffd83dbSDimitry Andric }
114