From 4112b4733502f746b766861853c414af0e01d6f9 Mon Sep 17 00:00:00 2001 From: Jan Kratochvil Date: Fri, 4 Oct 2019 19:32:57 +0000 Subject: [lldb] Fix crash on SBCommandReturnObject & assignment I was writing an SB API client and it was crashing on: bool DoExecute(SBDebugger dbg, char **command, SBCommandReturnObject &result) { result = subcommand(dbg, "help"); That is because SBCommandReturnObject &result gets initialized inside LLDB by: bool DoExecute(Args &command, CommandReturnObject &result) override { // std::unique_ptr gets initialized here from `&result`!!! SBCommandReturnObject sb_return(&result); DoExecute(...); sb_return.Release(); Differential revision: https://reviews.llvm.org/D67589 llvm-svn: 373775 --- .../test/api/command-return-object/Makefile | 3 ++ .../TestSBCommandReturnObject.py | 31 +++++++++++++++++++ .../test/api/command-return-object/main.cpp | 35 ++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 lldb/packages/Python/lldbsuite/test/api/command-return-object/Makefile create mode 100644 lldb/packages/Python/lldbsuite/test/api/command-return-object/TestSBCommandReturnObject.py create mode 100644 lldb/packages/Python/lldbsuite/test/api/command-return-object/main.cpp (limited to 'lldb/packages/Python/lldbsuite') diff --git a/lldb/packages/Python/lldbsuite/test/api/command-return-object/Makefile b/lldb/packages/Python/lldbsuite/test/api/command-return-object/Makefile new file mode 100644 index 00000000000..99998b20bcb --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/api/command-return-object/Makefile @@ -0,0 +1,3 @@ +CXX_SOURCES := main.cpp + +include Makefile.rules diff --git a/lldb/packages/Python/lldbsuite/test/api/command-return-object/TestSBCommandReturnObject.py b/lldb/packages/Python/lldbsuite/test/api/command-return-object/TestSBCommandReturnObject.py new file mode 100644 index 00000000000..cf7dbe58daa --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/api/command-return-object/TestSBCommandReturnObject.py @@ -0,0 +1,31 @@ +"""Test the lldb public C++ api for returning SBCommandReturnObject.""" + +from __future__ import print_function + + +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + + +class TestSBCommandReturnObject(TestBase): + + mydir = TestBase.compute_mydir(__file__) + NO_DEBUG_INFO_TESTCASE = True + + @skipIfNoSBHeaders + def test_sb_command_return_object(self): + env = {self.dylibPath: self.getLLDBLibraryEnvVal()} + + self.driver_exe = self.getBuildArtifact("command-return-object") + self.buildDriver('main.cpp', self.driver_exe) + self.addTearDownHook(lambda: os.remove(self.driver_exe)) + self.signBinary(self.driver_exe) + + if self.TraceOn(): + print("Running test %s" % self.driver_exe) + check_call([self.driver_exe, self.driver_exe], env=env) + else: + with open(os.devnull, 'w') as fnull: + check_call([self.driver_exe, self.driver_exe], + env=env, stdout=fnull, stderr=fnull) diff --git a/lldb/packages/Python/lldbsuite/test/api/command-return-object/main.cpp b/lldb/packages/Python/lldbsuite/test/api/command-return-object/main.cpp new file mode 100644 index 00000000000..4aeaf3fd5fd --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/api/command-return-object/main.cpp @@ -0,0 +1,35 @@ +#include "lldb/API/SBCommandInterpreter.h" +#include "lldb/API/SBCommandReturnObject.h" +#include "lldb/API/SBDebugger.h" + +using namespace lldb; + +static SBCommandReturnObject subcommand(SBDebugger &dbg, const char *cmd) { + SBCommandReturnObject Result; + dbg.GetCommandInterpreter().HandleCommand(cmd, Result); + return Result; +} + +class CommandCrasher : public SBCommandPluginInterface { +public: + bool DoExecute(SBDebugger dbg, char **command, + SBCommandReturnObject &result) { + // Test assignment from a different SBCommandReturnObject instance. + result = subcommand(dbg, "help"); + // Test also whether self-assignment is handled correctly. + result = result; + if (!result.Succeeded()) + return false; + return true; + } +}; + +int main() { + SBDebugger::Initialize(); + SBDebugger dbg = SBDebugger::Create(false /*source_init_files*/); + SBCommandInterpreter interp = dbg.GetCommandInterpreter(); + static CommandCrasher crasher; + interp.AddCommand("crasher", &crasher, nullptr /*help*/); + SBCommandReturnObject Result; + dbg.GetCommandInterpreter().HandleCommand("crasher", Result); +} -- cgit v1.2.3