180814287SRaphael Isemann //===-- InstrumentationRuntimeASan.cpp ------------------------------------===//
20feedebfSJonas Devlieghere //
30feedebfSJonas Devlieghere // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40feedebfSJonas Devlieghere // See https://llvm.org/LICENSE.txt for license information.
50feedebfSJonas Devlieghere // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60feedebfSJonas Devlieghere //
70feedebfSJonas Devlieghere //===----------------------------------------------------------------------===//
80feedebfSJonas Devlieghere
90feedebfSJonas Devlieghere #include "InstrumentationRuntimeASan.h"
100feedebfSJonas Devlieghere
110feedebfSJonas Devlieghere #include "lldb/Breakpoint/StoppointCallbackContext.h"
120feedebfSJonas Devlieghere #include "lldb/Core/Module.h"
130feedebfSJonas Devlieghere #include "lldb/Core/PluginInterface.h"
140feedebfSJonas Devlieghere #include "lldb/Core/PluginManager.h"
150feedebfSJonas Devlieghere #include "lldb/Symbol/Symbol.h"
16*77edd9b7SUsama Hameed #include "lldb/Target/Process.h"
170feedebfSJonas Devlieghere #include "lldb/Utility/RegularExpression.h"
180feedebfSJonas Devlieghere
19*77edd9b7SUsama Hameed #include "Plugins/InstrumentationRuntime/Utility/ReportRetriever.h"
200feedebfSJonas Devlieghere
210feedebfSJonas Devlieghere using namespace lldb;
220feedebfSJonas Devlieghere using namespace lldb_private;
230feedebfSJonas Devlieghere
LLDB_PLUGIN_DEFINE(InstrumentationRuntimeASan)24bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(InstrumentationRuntimeASan)
25fbb4d1e4SJonas Devlieghere
260feedebfSJonas Devlieghere lldb::InstrumentationRuntimeSP
270feedebfSJonas Devlieghere InstrumentationRuntimeASan::CreateInstance(const lldb::ProcessSP &process_sp) {
280feedebfSJonas Devlieghere return InstrumentationRuntimeSP(new InstrumentationRuntimeASan(process_sp));
290feedebfSJonas Devlieghere }
300feedebfSJonas Devlieghere
Initialize()310feedebfSJonas Devlieghere void InstrumentationRuntimeASan::Initialize() {
320feedebfSJonas Devlieghere PluginManager::RegisterPlugin(
330feedebfSJonas Devlieghere GetPluginNameStatic(), "AddressSanitizer instrumentation runtime plugin.",
340feedebfSJonas Devlieghere CreateInstance, GetTypeStatic);
350feedebfSJonas Devlieghere }
360feedebfSJonas Devlieghere
Terminate()370feedebfSJonas Devlieghere void InstrumentationRuntimeASan::Terminate() {
380feedebfSJonas Devlieghere PluginManager::UnregisterPlugin(CreateInstance);
390feedebfSJonas Devlieghere }
400feedebfSJonas Devlieghere
GetTypeStatic()410feedebfSJonas Devlieghere lldb::InstrumentationRuntimeType InstrumentationRuntimeASan::GetTypeStatic() {
420feedebfSJonas Devlieghere return eInstrumentationRuntimeTypeAddressSanitizer;
430feedebfSJonas Devlieghere }
440feedebfSJonas Devlieghere
~InstrumentationRuntimeASan()450feedebfSJonas Devlieghere InstrumentationRuntimeASan::~InstrumentationRuntimeASan() { Deactivate(); }
460feedebfSJonas Devlieghere
470feedebfSJonas Devlieghere const RegularExpression &
GetPatternForRuntimeLibrary()480feedebfSJonas Devlieghere InstrumentationRuntimeASan::GetPatternForRuntimeLibrary() {
490feedebfSJonas Devlieghere // FIXME: This shouldn't include the "dylib" suffix.
500feedebfSJonas Devlieghere static RegularExpression regex(
510feedebfSJonas Devlieghere llvm::StringRef("libclang_rt.asan_(.*)_dynamic\\.dylib"));
520feedebfSJonas Devlieghere return regex;
530feedebfSJonas Devlieghere }
540feedebfSJonas Devlieghere
CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp)550feedebfSJonas Devlieghere bool InstrumentationRuntimeASan::CheckIfRuntimeIsValid(
560feedebfSJonas Devlieghere const lldb::ModuleSP module_sp) {
570feedebfSJonas Devlieghere const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
580feedebfSJonas Devlieghere ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny);
590feedebfSJonas Devlieghere
600feedebfSJonas Devlieghere return symbol != nullptr;
610feedebfSJonas Devlieghere }
620feedebfSJonas Devlieghere
NotifyBreakpointHit(void * baton,StoppointCallbackContext * context,user_id_t break_id,user_id_t break_loc_id)630feedebfSJonas Devlieghere bool InstrumentationRuntimeASan::NotifyBreakpointHit(
640feedebfSJonas Devlieghere void *baton, StoppointCallbackContext *context, user_id_t break_id,
650feedebfSJonas Devlieghere user_id_t break_loc_id) {
660feedebfSJonas Devlieghere assert(baton && "null baton");
670feedebfSJonas Devlieghere if (!baton)
680feedebfSJonas Devlieghere return false;
690feedebfSJonas Devlieghere
700feedebfSJonas Devlieghere InstrumentationRuntimeASan *const instance =
710feedebfSJonas Devlieghere static_cast<InstrumentationRuntimeASan *>(baton);
720feedebfSJonas Devlieghere
730feedebfSJonas Devlieghere ProcessSP process_sp = instance->GetProcessSP();
740feedebfSJonas Devlieghere
75*77edd9b7SUsama Hameed return ReportRetriever::NotifyBreakpointHit(process_sp, context, break_id,
76*77edd9b7SUsama Hameed break_loc_id);
770feedebfSJonas Devlieghere }
780feedebfSJonas Devlieghere
Activate()790feedebfSJonas Devlieghere void InstrumentationRuntimeASan::Activate() {
800feedebfSJonas Devlieghere if (IsActive())
810feedebfSJonas Devlieghere return;
820feedebfSJonas Devlieghere
830feedebfSJonas Devlieghere ProcessSP process_sp = GetProcessSP();
840feedebfSJonas Devlieghere if (!process_sp)
850feedebfSJonas Devlieghere return;
860feedebfSJonas Devlieghere
87*77edd9b7SUsama Hameed Breakpoint *breakpoint = ReportRetriever::SetupBreakpoint(
88*77edd9b7SUsama Hameed GetRuntimeModuleSP(), process_sp, ConstString("_ZN6__asanL7AsanDieEv"));
890feedebfSJonas Devlieghere
90*77edd9b7SUsama Hameed if (!breakpoint)
910feedebfSJonas Devlieghere return;
920feedebfSJonas Devlieghere
93852a4bdbSJim Ingham const bool sync = false;
94*77edd9b7SUsama Hameed
950feedebfSJonas Devlieghere breakpoint->SetCallback(InstrumentationRuntimeASan::NotifyBreakpointHit, this,
96852a4bdbSJim Ingham sync);
970feedebfSJonas Devlieghere breakpoint->SetBreakpointKind("address-sanitizer-report");
980feedebfSJonas Devlieghere SetBreakpointID(breakpoint->GetID());
990feedebfSJonas Devlieghere
1000feedebfSJonas Devlieghere SetActive(true);
1010feedebfSJonas Devlieghere }
1020feedebfSJonas Devlieghere
Deactivate()1030feedebfSJonas Devlieghere void InstrumentationRuntimeASan::Deactivate() {
104*77edd9b7SUsama Hameed SetActive(false);
105*77edd9b7SUsama Hameed
106*77edd9b7SUsama Hameed if (GetBreakpointID() == LLDB_INVALID_BREAK_ID)
107*77edd9b7SUsama Hameed return;
108*77edd9b7SUsama Hameed
109*77edd9b7SUsama Hameed if (ProcessSP process_sp = GetProcessSP()) {
1100feedebfSJonas Devlieghere process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
1110feedebfSJonas Devlieghere SetBreakpointID(LLDB_INVALID_BREAK_ID);
1120feedebfSJonas Devlieghere }
1130feedebfSJonas Devlieghere }
114