Author: kuba.brecka Date: Fri Jun 16 15:59:08 2017 New Revision: 305589 URL: http://llvm.org/viewvc/llvm-project?rev=305589&view=rev Log: Upstreaming the UndefinedBehaviorSanitizerRuntime and MainThreadCheckerRuntime plugins.
Added: lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/ lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/ lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/Makefile lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/TestUbsanBasic.py lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/main.c lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/ lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/CMakeLists.txt lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/MainThreadCheckerRuntime.cpp lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/MainThreadCheckerRuntime.h lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/ lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/CMakeLists.txt lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.cpp lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.h Modified: lldb/trunk/include/lldb/lldb-enumerations.h lldb/trunk/lldb.xcodeproj/project.pbxproj lldb/trunk/packages/Python/lldbsuite/test/decorators.py lldb/trunk/source/API/SBThread.cpp lldb/trunk/source/API/SystemInitializerFull.cpp lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp lldb/trunk/source/Plugins/InstrumentationRuntime/CMakeLists.txt lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp Modified: lldb/trunk/include/lldb/lldb-enumerations.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/lldb-enumerations.h?rev=305589&r1=305588&r2=305589&view=diff ============================================================================== --- lldb/trunk/include/lldb/lldb-enumerations.h (original) +++ lldb/trunk/include/lldb/lldb-enumerations.h Fri Jun 16 15:59:08 2017 @@ -454,6 +454,8 @@ enum LanguageType { enum InstrumentationRuntimeType { eInstrumentationRuntimeTypeAddressSanitizer = 0x0000, eInstrumentationRuntimeTypeThreadSanitizer = 0x0001, + eInstrumentationRuntimeTypeUndefinedBehaviorSanitizer = 0x0002, + eInstrumentationRuntimeTypeMainThreadChecker = 0x0003, eNumInstrumentationRuntimeTypes }; Modified: lldb/trunk/lldb.xcodeproj/project.pbxproj URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lldb.xcodeproj/project.pbxproj?rev=305589&r1=305588&r2=305589&view=diff ============================================================================== --- lldb/trunk/lldb.xcodeproj/project.pbxproj (original) +++ lldb/trunk/lldb.xcodeproj/project.pbxproj Fri Jun 16 15:59:08 2017 @@ -744,6 +744,7 @@ 4CF3D80C15AF4DC800845BF3 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EDB919B414F6F10D008FF64B /* Security.framework */; }; 4CF52AF51428291E0051E832 /* SBFileSpecList.h in Headers */ = {isa = PBXBuildFile; fileRef = 4CF52AF41428291E0051E832 /* SBFileSpecList.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4CF52AF8142829390051E832 /* SBFileSpecList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CF52AF7142829390051E832 /* SBFileSpecList.cpp */; }; + 54067BF11DF2041B00749AA5 /* UndefinedBehaviorSanitizerRuntime.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 54067BEC1DF2034B00749AA5 /* UndefinedBehaviorSanitizerRuntime.cpp */; }; 6D0F61431C80AAAE00A4ECEE /* JavaASTContext.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6D0F61411C80AAAA00A4ECEE /* JavaASTContext.cpp */; }; 6D0F61481C80AAD600A4ECEE /* DWARFASTParserJava.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6D0F61441C80AACF00A4ECEE /* DWARFASTParserJava.cpp */; }; 6D0F614E1C80AB0700A4ECEE /* JavaLanguageRuntime.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6D0F614A1C80AB0400A4ECEE /* JavaLanguageRuntime.cpp */; }; @@ -766,6 +767,7 @@ 8C26C4261C3EA5F90031DF7C /* ThreadSanitizerRuntime.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C26C4241C3EA4340031DF7C /* ThreadSanitizerRuntime.cpp */; }; 8C2D6A53197A1EAF006989C9 /* MemoryHistory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C2D6A52197A1EAF006989C9 /* MemoryHistory.cpp */; }; 8C2D6A5E197A250F006989C9 /* MemoryHistoryASan.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C2D6A5A197A1FDC006989C9 /* MemoryHistoryASan.cpp */; }; + 8C3BD9961EF45DA50016C343 /* MainThreadCheckerRuntime.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C3BD9951EF45D9B0016C343 /* MainThreadCheckerRuntime.cpp */; }; 8CCB017E19BA28A80009FD44 /* ThreadCollection.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8CCB017A19BA283D0009FD44 /* ThreadCollection.cpp */; }; 8CCB018219BA4E270009FD44 /* SBThreadCollection.h in Headers */ = {isa = PBXBuildFile; fileRef = 8CCB018119BA4E210009FD44 /* SBThreadCollection.h */; settings = {ATTRIBUTES = (Public, ); }; }; 8CCB018319BA51BF0009FD44 /* SBThreadCollection.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8CCB017F19BA4DD00009FD44 /* SBThreadCollection.cpp */; }; @@ -2576,6 +2578,8 @@ 4CEDAED311754F5E00E875A6 /* ThreadPlanStepUntil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ThreadPlanStepUntil.h; path = include/lldb/Target/ThreadPlanStepUntil.h; sourceTree = "<group>"; }; 4CF52AF41428291E0051E832 /* SBFileSpecList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBFileSpecList.h; path = include/lldb/API/SBFileSpecList.h; sourceTree = "<group>"; }; 4CF52AF7142829390051E832 /* SBFileSpecList.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SBFileSpecList.cpp; path = source/API/SBFileSpecList.cpp; sourceTree = "<group>"; }; + 54067BEC1DF2034B00749AA5 /* UndefinedBehaviorSanitizerRuntime.cpp */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.cpp.cpp; name = UndefinedBehaviorSanitizerRuntime.cpp; path = UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.cpp; sourceTree = "<group>"; }; + 54067BED1DF2034B00749AA5 /* UndefinedBehaviorSanitizerRuntime.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UndefinedBehaviorSanitizerRuntime.h; path = UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.h; sourceTree = "<group>"; }; 69A01E1C1236C5D400C660B5 /* Host.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Host.cpp; sourceTree = "<group>"; }; 69A01E1F1236C5D400C660B5 /* Symbols.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Symbols.cpp; sourceTree = "<group>"; }; 6D0F613C1C80AA8900A4ECEE /* DebugMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DebugMacros.h; path = include/lldb/Symbol/DebugMacros.h; sourceTree = "<group>"; }; @@ -2624,6 +2628,8 @@ 8C2D6A54197A1EBE006989C9 /* MemoryHistory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = MemoryHistory.h; path = include/lldb/Target/MemoryHistory.h; sourceTree = "<group>"; }; 8C2D6A5A197A1FDC006989C9 /* MemoryHistoryASan.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MemoryHistoryASan.cpp; sourceTree = "<group>"; }; 8C2D6A5B197A1FDC006989C9 /* MemoryHistoryASan.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MemoryHistoryASan.h; sourceTree = "<group>"; }; + 8C3BD9931EF45D9B0016C343 /* MainThreadCheckerRuntime.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MainThreadCheckerRuntime.h; sourceTree = "<group>"; }; + 8C3BD9951EF45D9B0016C343 /* MainThreadCheckerRuntime.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = MainThreadCheckerRuntime.cpp; sourceTree = "<group>"; }; 8CCB017A19BA283D0009FD44 /* ThreadCollection.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ThreadCollection.cpp; path = source/Target/ThreadCollection.cpp; sourceTree = "<group>"; }; 8CCB017C19BA289B0009FD44 /* ThreadCollection.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ThreadCollection.h; path = include/lldb/Target/ThreadCollection.h; sourceTree = "<group>"; }; 8CCB017F19BA4DD00009FD44 /* SBThreadCollection.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = SBThreadCollection.cpp; path = source/API/SBThreadCollection.cpp; sourceTree = "<group>"; }; @@ -5912,6 +5918,15 @@ path = "gdb-remote"; sourceTree = "<group>"; }; + 54067BEA1DF2033700749AA5 /* UndefinedBehaviorSanitizer */ = { + isa = PBXGroup; + children = ( + 54067BEC1DF2034B00749AA5 /* UndefinedBehaviorSanitizerRuntime.cpp */, + 54067BED1DF2034B00749AA5 /* UndefinedBehaviorSanitizerRuntime.h */, + ); + name = UndefinedBehaviorSanitizer; + sourceTree = "<group>"; + }; 69A01E1A1236C5D400C660B5 /* common */ = { isa = PBXGroup; children = ( @@ -6011,11 +6026,22 @@ path = asan; sourceTree = "<group>"; }; + 8C3BD9911EF45D9B0016C343 /* MainThreadChecker */ = { + isa = PBXGroup; + children = ( + 8C3BD9931EF45D9B0016C343 /* MainThreadCheckerRuntime.h */, + 8C3BD9951EF45D9B0016C343 /* MainThreadCheckerRuntime.cpp */, + ); + path = MainThreadChecker; + sourceTree = "<group>"; + }; 8CF02ADD19DCBEC200B14BE0 /* InstrumentationRuntime */ = { isa = PBXGroup; children = ( + 54067BEA1DF2033700749AA5 /* UndefinedBehaviorSanitizer */, 8C26C4221C3EA4050031DF7C /* ThreadSanitizer */, 8CF02ADE19DCBEE600B14BE0 /* AddressSanitizer */, + 8C3BD9911EF45D9B0016C343 /* MainThreadChecker */, ); path = InstrumentationRuntime; sourceTree = "<group>"; @@ -7133,6 +7159,7 @@ 2689001213353DDE00698AC0 /* CommandObjectApropos.cpp in Sources */, 4C88BC2A1BA3722B00AA0964 /* Expression.cpp in Sources */, AE44FB4C1BB4BB540033EB62 /* GoFormatterFunctions.cpp in Sources */, + 8C3BD9961EF45DA50016C343 /* MainThreadCheckerRuntime.cpp in Sources */, 23042D121976CA1D00621B2C /* PlatformKalimba.cpp in Sources */, 2689001313353DDE00698AC0 /* CommandObjectArgs.cpp in Sources */, 2689001413353DDE00698AC0 /* CommandObjectBreakpoint.cpp in Sources */, @@ -7623,6 +7650,7 @@ 26954EBE1401EE8B00294D09 /* DynamicRegisterInfo.cpp in Sources */, 6D9AB3DD1BB2B74E003F2289 /* TypeMap.cpp in Sources */, 255EFF761AFABA950069F277 /* LockFilePosix.cpp in Sources */, + 54067BF11DF2041B00749AA5 /* UndefinedBehaviorSanitizerRuntime.cpp in Sources */, 3FBA69EC1B6067430008F44A /* PythonDataObjects.cpp in Sources */, 26274FA714030F79006BA130 /* DynamicLoaderDarwinKernel.cpp in Sources */, 94FA3DE01405D50400833217 /* ValueObjectConstResultChild.cpp in Sources */, Modified: lldb/trunk/packages/Python/lldbsuite/test/decorators.py URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/decorators.py?rev=305589&r1=305588&r2=305589&view=diff ============================================================================== --- lldb/trunk/packages/Python/lldbsuite/test/decorators.py (original) +++ lldb/trunk/packages/Python/lldbsuite/test/decorators.py Fri Jun 16 15:59:08 2017 @@ -681,6 +681,53 @@ def skipUnlessThreadSanitizer(func): return None return skipTestIfFn(is_compiler_clang_with_thread_sanitizer)(func) +def skipUnlessUndefinedBehaviorSanitizer(func): + """Decorate the item to skip test unless -fsanitize=undefined is supported.""" + + def is_compiler_clang_with_ubsan(self): + # Write out a temp file which exhibits UB. + inputf = tempfile.NamedTemporaryFile(suffix='.c') + inputf.write('int main() { int x = 0; return x / x; }\n') + inputf.flush() + + # We need to write out the object into a named temp file for inspection. + outputf = tempfile.NamedTemporaryFile() + + # Try to compile with ubsan turned on. + cmd = '%s -fsanitize=undefined %s -o %s' % (self.getCompiler(), inputf.name, outputf.name) + if os.popen(cmd).close() is not None: + return "Compiler cannot compile with -fsanitize=undefined" + + # Check that we actually see ubsan instrumentation in the binary. + cmd = 'nm %s' % outputf.name + with os.popen(cmd) as nm_output: + if '___ubsan_handle_divrem_overflow' not in nm_output.read(): + return "Division by zero instrumentation is missing" + + # Find the ubsan dylib. + # FIXME: This check should go away once compiler-rt gains support for __ubsan_on_report. + cmd = '%s -fsanitize=undefined -x c - -o - -### 2>&1' % self.getCompiler() + with os.popen(cmd) as cc_output: + driver_jobs = cc_output.read() + m = re.search(r'"([^"]+libclang_rt.ubsan_osx_dynamic.dylib)"', driver_jobs) + if not m: + return "Could not find the ubsan dylib used by the driver" + ubsan_dylib = m.group(1) + + # Check that the ubsan dylib has special monitor hooks. + cmd = 'nm -gU %s' % ubsan_dylib + with os.popen(cmd) as nm_output: + syms = nm_output.read() + if '___ubsan_on_report' not in syms: + return "Missing ___ubsan_on_report" + if '___ubsan_get_current_report_data' not in syms: + return "Missing ___ubsan_get_current_report_data" + + # OK, this dylib + compiler works for us. + return None + + return skipTestIfFn(is_compiler_clang_with_ubsan)(func) + def skipUnlessAddressSanitizer(func): """Decorate the item to skip test unless Clang -fsanitize=thread is supported.""" Added: lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/Makefile URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/Makefile?rev=305589&view=auto ============================================================================== --- lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/Makefile (added) +++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/Makefile Fri Jun 16 15:59:08 2017 @@ -0,0 +1,6 @@ +LEVEL = ../../../make + +C_SOURCES := main.c +CFLAGS_EXTRAS := -fsanitize=undefined -g + +include $(LEVEL)/Makefile.rules Added: lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/TestUbsanBasic.py URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/TestUbsanBasic.py?rev=305589&view=auto ============================================================================== --- lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/TestUbsanBasic.py (added) +++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/TestUbsanBasic.py Fri Jun 16 15:59:08 2017 @@ -0,0 +1,90 @@ +""" +Tests basic UndefinedBehaviorSanitizer support (detecting an alignment error). +""" + +import os +import time +import lldb +from lldbsuite.test.lldbtest import * +from lldbsuite.test.decorators import * +import lldbsuite.test.lldbutil as lldbutil +import json + + +class UbsanBasicTestCase(TestBase): + + mydir = TestBase.compute_mydir(__file__) + + @skipUnlessUndefinedBehaviorSanitizer + def test(self): + self.build() + self.ubsan_tests() + + def setUp(self): + # Call super's setUp(). + TestBase.setUp(self) + self.line_align = line_number('main.c', '// align line') + + def ubsan_tests(self): + # Load the test + exe = os.path.join(os.getcwd(), "a.out") + self.expect( + "file " + exe, + patterns=["Current executable set to .*a.out"]) + + self.runCmd("run") + + process = self.dbg.GetSelectedTarget().process + thread = process.GetSelectedThread() + frame = thread.GetSelectedFrame() + + # the stop reason of the thread should be breakpoint. + self.expect("thread list", "A ubsan issue should be detected", + substrs=['stopped', 'stop reason =']) + + stop_reason = thread.GetStopReason() + self.assertEqual(stop_reason, lldb.eStopReasonInstrumentation) + + # test that the UBSan dylib is present + self.expect( + "image lookup -n __ubsan_on_report", + "__ubsan_on_report should be present", + substrs=['1 match found']) + + # We should be stopped in __ubsan_on_report + self.assertTrue("__ubsan_on_report" in frame.GetFunctionName()) + + # The stopped thread backtrace should contain either 'align line' + found = False + for i in range(thread.GetNumFrames()): + frame = thread.GetFrameAtIndex(i) + if frame.GetLineEntry().GetFileSpec().GetFilename() == "main.c": + if frame.GetLineEntry().GetLine() == self.line_align: + found = True + self.assertTrue(found) + + backtraces = thread.GetStopReasonExtendedBacktraces( + lldb.eInstrumentationRuntimeTypeUndefinedBehaviorSanitizer) + self.assertTrue(backtraces.GetSize() == 1) + + self.expect( + "thread info -s", + "The extended stop info should contain the UBSan provided fields", + substrs=[ + "instrumentation_class", + "memory_address", + "description", + "filename", + "line", + "col"]) + + output_lines = self.res.GetOutput().split('\n') + json_line = '\n'.join(output_lines[2:]) + data = json.loads(json_line) + + self.assertEqual(data["instrumentation_class"], "UndefinedBehaviorSanitizer") + self.assertEqual(data["description"], "misaligned-pointer-use") + self.assertEqual(data["filename"], "main.c") + self.assertEqual(data["line"], self.line_align) + + self.runCmd("continue") Added: lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/main.c URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/main.c?rev=305589&view=auto ============================================================================== --- lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/main.c (added) +++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/ubsan/basic/main.c Fri Jun 16 15:59:08 2017 @@ -0,0 +1,4 @@ +int main() { + int data[4]; + return *(int *)(((char *)&data[0]) + 2); // align line +} Modified: lldb/trunk/source/API/SBThread.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBThread.cpp?rev=305589&r1=305588&r2=305589&view=diff ============================================================================== --- lldb/trunk/source/API/SBThread.cpp (original) +++ lldb/trunk/source/API/SBThread.cpp Fri Jun 16 15:59:08 2017 @@ -293,10 +293,6 @@ SBThread::GetStopReasonExtendedBacktrace ThreadCollectionSP threads; threads.reset(new ThreadCollection()); - // We currently only support ThreadSanitizer. - if (type != eInstrumentationRuntimeTypeThreadSanitizer) - return threads; - std::unique_lock<std::recursive_mutex> lock; ExecutionContext exe_ctx(m_opaque_sp.get(), lock); Modified: lldb/trunk/source/API/SystemInitializerFull.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SystemInitializerFull.cpp?rev=305589&r1=305588&r2=305589&view=diff ============================================================================== --- lldb/trunk/source/API/SystemInitializerFull.cpp (original) +++ lldb/trunk/source/API/SystemInitializerFull.cpp Fri Jun 16 15:59:08 2017 @@ -51,6 +51,8 @@ #include "Plugins/Instruction/ARM64/EmulateInstructionARM64.h" #include "Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h" #include "Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.h" +#include "Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.h" +#include "Plugins/InstrumentationRuntime/MainThreadChecker/MainThreadCheckerRuntime.h" #include "Plugins/JITLoader/GDB/JITLoaderGDB.h" #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" #include "Plugins/Language/Go/GoLanguage.h" @@ -310,6 +312,8 @@ void SystemInitializerFull::Initialize() MemoryHistoryASan::Initialize(); AddressSanitizerRuntime::Initialize(); ThreadSanitizerRuntime::Initialize(); + UndefinedBehaviorSanitizerRuntime::Initialize(); + MainThreadCheckerRuntime::Initialize(); SymbolVendorELF::Initialize(); SymbolFileDWARF::Initialize(); @@ -434,6 +438,8 @@ void SystemInitializerFull::Terminate() MemoryHistoryASan::Terminate(); AddressSanitizerRuntime::Terminate(); ThreadSanitizerRuntime::Terminate(); + UndefinedBehaviorSanitizerRuntime::Terminate(); + MainThreadCheckerRuntime::Terminate(); SymbolVendorELF::Terminate(); SymbolFileDWARF::Terminate(); SymbolFilePDB::Terminate(); Modified: lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp?rev=305589&r1=305588&r2=305589&view=diff ============================================================================== --- lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp (original) +++ lldb/trunk/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp Fri Jun 16 15:59:08 2017 @@ -308,13 +308,6 @@ void AddressSanitizerRuntime::Activate() breakpoint->SetBreakpointKind("address-sanitizer-report"); SetBreakpointID(breakpoint->GetID()); - StreamFileSP stream_sp(process_sp->GetTarget().GetDebugger().GetOutputFile()); - if (stream_sp) { - stream_sp->Printf("AddressSanitizer debugger support is active. Memory " - "error breakpoint has been installed and you can now use " - "the 'memory history' command.\n"); - } - SetActive(true); } Modified: lldb/trunk/source/Plugins/InstrumentationRuntime/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/CMakeLists.txt?rev=305589&r1=305588&r2=305589&view=diff ============================================================================== --- lldb/trunk/source/Plugins/InstrumentationRuntime/CMakeLists.txt (original) +++ lldb/trunk/source/Plugins/InstrumentationRuntime/CMakeLists.txt Fri Jun 16 15:59:08 2017 @@ -1,2 +1,4 @@ add_subdirectory(AddressSanitizer) add_subdirectory(ThreadSanitizer) +add_subdirectory(UndefinedBehaviorSanitizer) +add_subdirectory(MainThreadChecker) Added: lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/CMakeLists.txt?rev=305589&view=auto ============================================================================== --- lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/CMakeLists.txt (added) +++ lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/CMakeLists.txt Fri Jun 16 15:59:08 2017 @@ -0,0 +1,3 @@ +add_lldb_library(lldbPluginInstrumentationRuntimeMainThreadChecker + MainThreadCheckerRuntime.cpp + ) Added: lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/MainThreadCheckerRuntime.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/MainThreadCheckerRuntime.cpp?rev=305589&view=auto ============================================================================== --- lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/MainThreadCheckerRuntime.cpp (added) +++ lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/MainThreadCheckerRuntime.cpp Fri Jun 16 15:59:08 2017 @@ -0,0 +1,273 @@ +//===-- MainThreadCheckerRuntime.cpp ----------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "MainThreadCheckerRuntime.h" + +#include "lldb/Breakpoint/StoppointCallbackContext.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Symbol/Symbol.h" +#include "lldb/Symbol/SymbolContext.h" +#include "lldb/Symbol/Variable.h" +#include "lldb/Symbol/VariableList.h" +#include "lldb/Target/InstrumentationRuntimeStopInfo.h" +#include "lldb/Target/RegisterContext.h" +#include "lldb/Target/SectionLoadList.h" +#include "lldb/Target/StopInfo.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Thread.h" +#include "lldb/Utility/RegularExpression.h" +#include "Plugins/Process/Utility/HistoryThread.h" + +using namespace lldb; +using namespace lldb_private; + +MainThreadCheckerRuntime::~MainThreadCheckerRuntime() { + Deactivate(); +} + +lldb::InstrumentationRuntimeSP +MainThreadCheckerRuntime::CreateInstance(const lldb::ProcessSP &process_sp) { + return InstrumentationRuntimeSP(new MainThreadCheckerRuntime(process_sp)); +} + +void MainThreadCheckerRuntime::Initialize() { + PluginManager::RegisterPlugin( + GetPluginNameStatic(), "MainThreadChecker instrumentation runtime plugin.", + CreateInstance, GetTypeStatic); +} + +void MainThreadCheckerRuntime::Terminate() { + PluginManager::UnregisterPlugin(CreateInstance); +} + +lldb_private::ConstString MainThreadCheckerRuntime::GetPluginNameStatic() { + return ConstString("MainThreadChecker"); +} + +lldb::InstrumentationRuntimeType MainThreadCheckerRuntime::GetTypeStatic() { + return eInstrumentationRuntimeTypeMainThreadChecker; +} + +const RegularExpression & +MainThreadCheckerRuntime::GetPatternForRuntimeLibrary() { + static RegularExpression regex(llvm::StringRef("libMainThreadChecker.dylib")); + return regex; +} + +bool MainThreadCheckerRuntime::CheckIfRuntimeIsValid( + const lldb::ModuleSP module_sp) { + static ConstString test_sym("__main_thread_checker_on_report"); + const Symbol *symbol = + module_sp->FindFirstSymbolWithNameAndType(test_sym, lldb::eSymbolTypeAny); + return symbol != nullptr; +} + +StructuredData::ObjectSP +MainThreadCheckerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) { + ProcessSP process_sp = GetProcessSP(); + if (!process_sp) + return StructuredData::ObjectSP(); + + ThreadSP thread_sp = exe_ctx_ref.GetThreadSP(); + StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); + ModuleSP runtime_module_sp = GetRuntimeModuleSP(); + Target &target = process_sp->GetTarget(); + + if (!frame_sp) + return StructuredData::ObjectSP(); + + RegisterContextSP regctx_sp = frame_sp->GetRegisterContext(); + if (!regctx_sp) + return StructuredData::ObjectSP(); + + const RegisterInfo *reginfo = regctx_sp->GetRegisterInfoByName("arg1"); + if (!reginfo) + return StructuredData::ObjectSP(); + + uint64_t apiname_ptr = regctx_sp->ReadRegisterAsUnsigned(reginfo, 0); + if (!apiname_ptr) + return StructuredData::ObjectSP(); + + std::string apiName = ""; + Status read_error; + target.ReadCStringFromMemory(apiname_ptr, apiName, read_error); + if (read_error.Fail()) + return StructuredData::ObjectSP(); + + std::string className = ""; + std::string selector = ""; + if (apiName.substr(0, 2) == "-[") { + size_t spacePos = apiName.find(" "); + if (spacePos != std::string::npos) { + className = apiName.substr(2, spacePos - 2); + selector = apiName.substr(spacePos + 1, apiName.length() - spacePos - 2); + } + } + + // Gather the PCs of the user frames in the backtrace. + StructuredData::Array *trace = new StructuredData::Array(); + auto trace_sp = StructuredData::ObjectSP(trace); + StackFrameSP responsible_frame; + for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) { + StackFrameSP frame = thread_sp->GetStackFrameAtIndex(I); + Address addr = frame->GetFrameCodeAddress(); + if (addr.GetModule() == runtime_module_sp) // Skip PCs from the runtime. + continue; + + // The first non-runtime frame is responsible for the bug. + if (!responsible_frame) + responsible_frame = frame; + + // First frame in stacktrace should point to a real PC, not return address. + if (I != 0 && trace->GetSize() == 0) { + addr.Slide(-1); + } + + lldb::addr_t PC = addr.GetLoadAddress(&target); + trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(PC))); + } + + auto *d = new StructuredData::Dictionary(); + auto dict_sp = StructuredData::ObjectSP(d); + d->AddStringItem("instrumentation_class", "MainThreadChecker"); + d->AddStringItem("api_name", apiName); + d->AddStringItem("class_name", className); + d->AddStringItem("selector", selector); + d->AddStringItem("description", + apiName + " must be called from main thread only"); + d->AddIntegerItem("tid", thread_sp->GetIndexID()); + d->AddItem("trace", trace_sp); + return dict_sp; +} + +bool MainThreadCheckerRuntime::NotifyBreakpointHit( + void *baton, StoppointCallbackContext *context, user_id_t break_id, + user_id_t break_loc_id) { + assert(baton && "null baton"); + if (!baton) + return false; //< false => resume execution. + + MainThreadCheckerRuntime *const instance = + static_cast<MainThreadCheckerRuntime *>(baton); + + ProcessSP process_sp = instance->GetProcessSP(); + ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); + if (!process_sp || !thread_sp || + process_sp != context->exe_ctx_ref.GetProcessSP()) + return false; + + StructuredData::ObjectSP report = + instance->RetrieveReportData(context->exe_ctx_ref); + + if (report) { + std::string description = report->GetAsDictionary() + ->GetValueForKey("description") + ->GetAsString() + ->GetValue(); + thread_sp->SetStopInfo( + InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData( + *thread_sp, description, report)); + return true; + } + + return false; +} + +void MainThreadCheckerRuntime::Activate() { + if (IsActive()) + return; + + ProcessSP process_sp = GetProcessSP(); + if (!process_sp) + return; + + ModuleSP runtime_module_sp = GetRuntimeModuleSP(); + + ConstString symbol_name("__main_thread_checker_on_report"); + const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType( + symbol_name, eSymbolTypeCode); + + if (symbol == nullptr) + return; + + if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) + return; + + Target &target = process_sp->GetTarget(); + addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target); + + if (symbol_address == LLDB_INVALID_ADDRESS) + return; + + Breakpoint *breakpoint = + process_sp->GetTarget() + .CreateBreakpoint(symbol_address, /*internal=*/true, + /*hardware=*/false) + .get(); + breakpoint->SetCallback(MainThreadCheckerRuntime::NotifyBreakpointHit, this, + true); + breakpoint->SetBreakpointKind("main-thread-checker-report"); + SetBreakpointID(breakpoint->GetID()); + + SetActive(true); +} + +void MainThreadCheckerRuntime::Deactivate() { + SetActive(false); + + auto BID = GetBreakpointID(); + if (BID == LLDB_INVALID_BREAK_ID) + return; + + if (ProcessSP process_sp = GetProcessSP()) { + process_sp->GetTarget().RemoveBreakpointByID(BID); + SetBreakpointID(LLDB_INVALID_BREAK_ID); + } +} + +lldb::ThreadCollectionSP +MainThreadCheckerRuntime::GetBacktracesFromExtendedStopInfo( + StructuredData::ObjectSP info) { + ThreadCollectionSP threads; + threads.reset(new ThreadCollection()); + + ProcessSP process_sp = GetProcessSP(); + + if (info->GetObjectForDotSeparatedPath("instrumentation_class") + ->GetStringValue() != "MainThreadChecker") + return threads; + + std::vector<lldb::addr_t> PCs; + auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray(); + trace->ForEach([&PCs](StructuredData::Object *PC) -> bool { + PCs.push_back(PC->GetAsInteger()->GetValue()); + return true; + }); + + if (PCs.empty()) + return threads; + + StructuredData::ObjectSP thread_id_obj = + info->GetObjectForDotSeparatedPath("tid"); + tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0; + + uint32_t stop_id = 0; + bool stop_id_is_valid = false; + HistoryThread *history_thread = + new HistoryThread(*process_sp, tid, PCs, stop_id, stop_id_is_valid); + ThreadSP new_thread_sp(history_thread); + + // Save this in the Process' ExtendedThreadList so a strong pointer + // retains the object + process_sp->GetExtendedThreadList().AddThread(new_thread_sp); + threads->AddThread(new_thread_sp); + + return threads; +} Added: lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/MainThreadCheckerRuntime.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/MainThreadCheckerRuntime.h?rev=305589&view=auto ============================================================================== --- lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/MainThreadCheckerRuntime.h (added) +++ lldb/trunk/source/Plugins/InstrumentationRuntime/MainThreadChecker/MainThreadCheckerRuntime.h Fri Jun 16 15:59:08 2017 @@ -0,0 +1,68 @@ +//===-- MainThreadCheckerRuntime.h ------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_MainThreadCheckerRuntime_h_ +#define liblldb_MainThreadCheckerRuntime_h_ + +#include "lldb/Core/StructuredData.h" +#include "lldb/Target/ABI.h" +#include "lldb/Target/InstrumentationRuntime.h" +#include "lldb/lldb-private.h" + +namespace lldb_private { + + class MainThreadCheckerRuntime : public lldb_private::InstrumentationRuntime { + public: + ~MainThreadCheckerRuntime() override; + + static lldb::InstrumentationRuntimeSP + CreateInstance(const lldb::ProcessSP &process_sp); + + static void Initialize(); + + static void Terminate(); + + static lldb_private::ConstString GetPluginNameStatic(); + + static lldb::InstrumentationRuntimeType GetTypeStatic(); + + lldb_private::ConstString GetPluginName() override { + return GetPluginNameStatic(); + } + + virtual lldb::InstrumentationRuntimeType GetType() { return GetTypeStatic(); } + + uint32_t GetPluginVersion() override { return 1; } + + lldb::ThreadCollectionSP + GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) override; + + private: + MainThreadCheckerRuntime(const lldb::ProcessSP &process_sp) + : lldb_private::InstrumentationRuntime(process_sp) {} + + const RegularExpression &GetPatternForRuntimeLibrary() override; + + bool CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override; + + void Activate() override; + + void Deactivate(); + + static bool NotifyBreakpointHit(void *baton, + StoppointCallbackContext *context, + lldb::user_id_t break_id, + lldb::user_id_t break_loc_id); + + StructuredData::ObjectSP RetrieveReportData(ExecutionContextRef exe_ctx_ref); + }; + +} // namespace lldb_private + +#endif // liblldb_MainThreadCheckerRuntime_h_ Modified: lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp?rev=305589&r1=305588&r2=305589&view=diff ============================================================================== --- lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp (original) +++ lldb/trunk/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp Fri Jun 16 15:59:08 2017 @@ -921,11 +921,6 @@ void ThreadSanitizerRuntime::Activate() breakpoint->SetBreakpointKind("thread-sanitizer-report"); SetBreakpointID(breakpoint->GetID()); - StreamFileSP stream_sp(process_sp->GetTarget().GetDebugger().GetOutputFile()); - if (stream_sp) { - stream_sp->Printf("ThreadSanitizer debugger support is active.\n"); - } - SetActive(true); } Added: lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/CMakeLists.txt?rev=305589&view=auto ============================================================================== --- lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/CMakeLists.txt (added) +++ lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/CMakeLists.txt Fri Jun 16 15:59:08 2017 @@ -0,0 +1,3 @@ +add_lldb_library(lldbPluginInstrumentationRuntimeUndefinedBehaviorSanitizer + UndefinedBehaviorSanitizerRuntime.cpp + ) Added: lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.cpp?rev=305589&view=auto ============================================================================== --- lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.cpp (added) +++ lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.cpp Fri Jun 16 15:59:08 2017 @@ -0,0 +1,340 @@ +//===-- UndefinedBehaviorSanitizerRuntime.cpp -------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "UndefinedBehaviorSanitizerRuntime.h" + +#include "Plugins/Process/Utility/HistoryThread.h" +#include "lldb/Breakpoint/StoppointCallbackContext.h" +#include "lldb/Core/Debugger.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/PluginInterface.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Core/ValueObject.h" +#include "lldb/Expression/UserExpression.h" +#include "lldb/Interpreter/CommandReturnObject.h" +#include "lldb/Symbol/Symbol.h" +#include "lldb/Symbol/SymbolContext.h" +#include "lldb/Symbol/Variable.h" +#include "lldb/Symbol/VariableList.h" +#include "lldb/Target/InstrumentationRuntimeStopInfo.h" +#include "lldb/Target/SectionLoadList.h" +#include "lldb/Target/StopInfo.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Thread.h" +#include "lldb/Utility/RegularExpression.h" +#include "lldb/Utility/Stream.h" +#include <ctype.h> + +using namespace lldb; +using namespace lldb_private; + +UndefinedBehaviorSanitizerRuntime::~UndefinedBehaviorSanitizerRuntime() { + Deactivate(); +} + +lldb::InstrumentationRuntimeSP +UndefinedBehaviorSanitizerRuntime::CreateInstance( + const lldb::ProcessSP &process_sp) { + return InstrumentationRuntimeSP( + new UndefinedBehaviorSanitizerRuntime(process_sp)); +} + +void UndefinedBehaviorSanitizerRuntime::Initialize() { + PluginManager::RegisterPlugin( + GetPluginNameStatic(), + "UndefinedBehaviorSanitizer instrumentation runtime plugin.", + CreateInstance, GetTypeStatic); +} + +void UndefinedBehaviorSanitizerRuntime::Terminate() { + PluginManager::UnregisterPlugin(CreateInstance); +} + +lldb_private::ConstString +UndefinedBehaviorSanitizerRuntime::GetPluginNameStatic() { + return ConstString("UndefinedBehaviorSanitizer"); +} + +lldb::InstrumentationRuntimeType +UndefinedBehaviorSanitizerRuntime::GetTypeStatic() { + return eInstrumentationRuntimeTypeUndefinedBehaviorSanitizer; +} + +static const char *ub_sanitizer_retrieve_report_data_prefix = R"( +extern "C" { +void +__ubsan_get_current_report_data(const char **OutIssueKind, + const char **OutMessage, const char **OutFilename, unsigned *OutLine, + unsigned *OutCol, char **OutMemoryAddr); +} + +struct data { + const char *issue_kind; + const char *message; + const char *filename; + unsigned line; + unsigned col; + char *memory_addr; +}; +)"; + +static const char *ub_sanitizer_retrieve_report_data_command = R"( +data t; +__ubsan_get_current_report_data(&t.issue_kind, &t.message, &t.filename, &t.line, + &t.col, &t.memory_addr); +t; +)"; + +static addr_t RetrieveUnsigned(ValueObjectSP return_value_sp, + ProcessSP process_sp, + const std::string &expression_path) { + return return_value_sp->GetValueForExpressionPath(expression_path.c_str()) + ->GetValueAsUnsigned(0); +} + +static std::string RetrieveString(ValueObjectSP return_value_sp, + ProcessSP process_sp, + const std::string &expression_path) { + addr_t ptr = RetrieveUnsigned(return_value_sp, process_sp, expression_path); + std::string str; + Status error; + process_sp->ReadCStringFromMemory(ptr, str, error); + return str; +} + +StructuredData::ObjectSP UndefinedBehaviorSanitizerRuntime::RetrieveReportData( + ExecutionContextRef exe_ctx_ref) { + ProcessSP process_sp = GetProcessSP(); + if (!process_sp) + return StructuredData::ObjectSP(); + + ThreadSP thread_sp = exe_ctx_ref.GetThreadSP(); + StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); + ModuleSP runtime_module_sp = GetRuntimeModuleSP(); + Target &target = process_sp->GetTarget(); + + if (!frame_sp) + return StructuredData::ObjectSP(); + + StreamFileSP Stream(target.GetDebugger().GetOutputFile()); + + EvaluateExpressionOptions options; + options.SetUnwindOnError(true); + options.SetTryAllThreads(true); + options.SetStopOthers(true); + options.SetIgnoreBreakpoints(true); + options.SetTimeout(std::chrono::seconds(2)); + options.SetPrefix(ub_sanitizer_retrieve_report_data_prefix); + options.SetAutoApplyFixIts(false); + options.SetLanguage(eLanguageTypeObjC_plus_plus); + + ValueObjectSP main_value; + ExecutionContext exe_ctx; + Status eval_error; + frame_sp->CalculateExecutionContext(exe_ctx); + ExpressionResults result = UserExpression::Evaluate( + exe_ctx, options, ub_sanitizer_retrieve_report_data_command, "", + main_value, eval_error); + if (result != eExpressionCompleted) { + target.GetDebugger().GetAsyncOutputStream()->Printf( + "Warning: Cannot evaluate UndefinedBehaviorSanitizer expression:\n%s\n", + eval_error.AsCString()); + return StructuredData::ObjectSP(); + } + + // Gather the PCs of the user frames in the backtrace. + StructuredData::Array *trace = new StructuredData::Array(); + auto trace_sp = StructuredData::ObjectSP(trace); + for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) { + const Address FCA = + thread_sp->GetStackFrameAtIndex(I)->GetFrameCodeAddress(); + if (FCA.GetModule() == runtime_module_sp) // Skip PCs from the runtime. + continue; + + lldb::addr_t PC = FCA.GetLoadAddress(&target); + trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(PC))); + } + + std::string IssueKind = RetrieveString(main_value, process_sp, ".issue_kind"); + std::string ErrMessage = RetrieveString(main_value, process_sp, ".message"); + std::string Filename = RetrieveString(main_value, process_sp, ".filename"); + unsigned Line = RetrieveUnsigned(main_value, process_sp, ".line"); + unsigned Col = RetrieveUnsigned(main_value, process_sp, ".col"); + uintptr_t MemoryAddr = + RetrieveUnsigned(main_value, process_sp, ".memory_addr"); + + auto *d = new StructuredData::Dictionary(); + auto dict_sp = StructuredData::ObjectSP(d); + d->AddStringItem("instrumentation_class", "UndefinedBehaviorSanitizer"); + d->AddStringItem("description", IssueKind); + d->AddStringItem("summary", ErrMessage); + d->AddStringItem("filename", Filename); + d->AddIntegerItem("line", Line); + d->AddIntegerItem("col", Col); + d->AddIntegerItem("memory_address", MemoryAddr); + d->AddIntegerItem("tid", thread_sp->GetID()); + d->AddItem("trace", trace_sp); + return dict_sp; +} + +static std::string GetStopReasonDescription(StructuredData::ObjectSP report) { + llvm::StringRef stop_reason_description_ref; + report->GetAsDictionary()->GetValueForKeyAsString("description", + stop_reason_description_ref); + std::string stop_reason_description = stop_reason_description_ref; + + if (!stop_reason_description.size()) { + stop_reason_description = "Undefined behavior detected"; + } else { + stop_reason_description[0] = toupper(stop_reason_description[0]); + for (unsigned I = 1; I < stop_reason_description.size(); ++I) + if (stop_reason_description[I] == '-') + stop_reason_description[I] = ' '; + } + return stop_reason_description; +} + +bool UndefinedBehaviorSanitizerRuntime::NotifyBreakpointHit( + void *baton, StoppointCallbackContext *context, user_id_t break_id, + user_id_t break_loc_id) { + assert(baton && "null baton"); + if (!baton) + return false; //< false => resume execution. + + UndefinedBehaviorSanitizerRuntime *const instance = + static_cast<UndefinedBehaviorSanitizerRuntime *>(baton); + + ProcessSP process_sp = instance->GetProcessSP(); + ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP(); + if (!process_sp || !thread_sp || + process_sp != context->exe_ctx_ref.GetProcessSP()) + return false; + + StructuredData::ObjectSP report = + instance->RetrieveReportData(context->exe_ctx_ref); + + if (report) { + thread_sp->SetStopInfo( + InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData( + *thread_sp, GetStopReasonDescription(report), report)); + return true; + } + + return false; +} + +const RegularExpression & +UndefinedBehaviorSanitizerRuntime::GetPatternForRuntimeLibrary() { + static RegularExpression regex(llvm::StringRef("libclang_rt\\.(a|t|ub)san_")); + return regex; +} + +bool UndefinedBehaviorSanitizerRuntime::CheckIfRuntimeIsValid( + const lldb::ModuleSP module_sp) { + static ConstString ubsan_test_sym("__ubsan_on_report"); + const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType( + ubsan_test_sym, lldb::eSymbolTypeAny); + return symbol != nullptr; +} + +// FIXME: Factor out all the logic we have in common with the {a,t}san plugins. +void UndefinedBehaviorSanitizerRuntime::Activate() { + if (IsActive()) + return; + + ProcessSP process_sp = GetProcessSP(); + if (!process_sp) + return; + + ModuleSP runtime_module_sp = GetRuntimeModuleSP(); + + ConstString symbol_name("__ubsan_on_report"); + const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType( + symbol_name, eSymbolTypeCode); + + if (symbol == nullptr) + return; + + if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid()) + return; + + Target &target = process_sp->GetTarget(); + addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target); + + if (symbol_address == LLDB_INVALID_ADDRESS) + return; + + Breakpoint *breakpoint = + process_sp->GetTarget() + .CreateBreakpoint(symbol_address, /*internal=*/true, + /*hardware=*/false) + .get(); + breakpoint->SetCallback( + UndefinedBehaviorSanitizerRuntime::NotifyBreakpointHit, this, true); + breakpoint->SetBreakpointKind("undefined-behavior-sanitizer-report"); + SetBreakpointID(breakpoint->GetID()); + + SetActive(true); +} + +void UndefinedBehaviorSanitizerRuntime::Deactivate() { + SetActive(false); + + auto BID = GetBreakpointID(); + if (BID == LLDB_INVALID_BREAK_ID) + return; + + if (ProcessSP process_sp = GetProcessSP()) { + process_sp->GetTarget().RemoveBreakpointByID(BID); + SetBreakpointID(LLDB_INVALID_BREAK_ID); + } +} + +lldb::ThreadCollectionSP +UndefinedBehaviorSanitizerRuntime::GetBacktracesFromExtendedStopInfo( + StructuredData::ObjectSP info) { + ThreadCollectionSP threads; + threads.reset(new ThreadCollection()); + + ProcessSP process_sp = GetProcessSP(); + + if (info->GetObjectForDotSeparatedPath("instrumentation_class") + ->GetStringValue() != "UndefinedBehaviorSanitizer") + return threads; + + std::vector<lldb::addr_t> PCs; + auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray(); + trace->ForEach([&PCs](StructuredData::Object *PC) -> bool { + PCs.push_back(PC->GetAsInteger()->GetValue()); + return true; + }); + + if (PCs.empty()) + return threads; + + StructuredData::ObjectSP thread_id_obj = + info->GetObjectForDotSeparatedPath("tid"); + tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0; + + uint32_t stop_id = 0; + bool stop_id_is_valid = false; + HistoryThread *history_thread = + new HistoryThread(*process_sp, tid, PCs, stop_id, stop_id_is_valid); + ThreadSP new_thread_sp(history_thread); + std::string stop_reason_description = GetStopReasonDescription(info); + new_thread_sp->SetName(stop_reason_description.c_str()); + + // Save this in the Process' ExtendedThreadList so a strong pointer + // retains the object + process_sp->GetExtendedThreadList().AddThread(new_thread_sp); + threads->AddThread(new_thread_sp); + + return threads; +} Added: lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.h?rev=305589&view=auto ============================================================================== --- lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.h (added) +++ lldb/trunk/source/Plugins/InstrumentationRuntime/UndefinedBehaviorSanitizer/UndefinedBehaviorSanitizerRuntime.h Fri Jun 16 15:59:08 2017 @@ -0,0 +1,69 @@ +//===-- UndefinedBehaviorSanitizerRuntime.h ---------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_UndefinedBehaviorSanitizerRuntime_h_ +#define liblldb_UndefinedBehaviorSanitizerRuntime_h_ + +#include "lldb/Core/StructuredData.h" +#include "lldb/Target/ABI.h" +#include "lldb/Target/InstrumentationRuntime.h" +#include "lldb/lldb-private.h" + +namespace lldb_private { + +class UndefinedBehaviorSanitizerRuntime + : public lldb_private::InstrumentationRuntime { +public: + ~UndefinedBehaviorSanitizerRuntime() override; + + static lldb::InstrumentationRuntimeSP + CreateInstance(const lldb::ProcessSP &process_sp); + + static void Initialize(); + + static void Terminate(); + + static lldb_private::ConstString GetPluginNameStatic(); + + static lldb::InstrumentationRuntimeType GetTypeStatic(); + + lldb_private::ConstString GetPluginName() override { + return GetPluginNameStatic(); + } + + virtual lldb::InstrumentationRuntimeType GetType() { return GetTypeStatic(); } + + uint32_t GetPluginVersion() override { return 1; } + + lldb::ThreadCollectionSP + GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) override; + +private: + UndefinedBehaviorSanitizerRuntime(const lldb::ProcessSP &process_sp) + : lldb_private::InstrumentationRuntime(process_sp) {} + + const RegularExpression &GetPatternForRuntimeLibrary() override; + + bool CheckIfRuntimeIsValid(const lldb::ModuleSP module_sp) override; + + void Activate() override; + + void Deactivate(); + + static bool NotifyBreakpointHit(void *baton, + StoppointCallbackContext *context, + lldb::user_id_t break_id, + lldb::user_id_t break_loc_id); + + StructuredData::ObjectSP RetrieveReportData(ExecutionContextRef exe_ctx_ref); +}; + +} // namespace lldb_private + +#endif // liblldb_UndefinedBehaviorSanitizerRuntime_h_ _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits