From 423c2e98e4e4c78c72f835fe6e59047419044425 Mon Sep 17 00:00:00 2001 From: Raphael Isemann Date: Fri, 11 Oct 2019 08:42:22 +0000 Subject: [lldb] Fix crash in CxxModuleHandler when std module is empty We currently don't handle the error in the Expected we get when searching for an equal local DeclContext. Usually this can't happen as this would require that we have a STL container and we can find libc++'s std module, but when we load the module in the expression parser the module doesn't even contain the 'std' namespace. The only way I see to test this is by having a fake 'std' module that requires a special define to actually provide its contents, while it will just be empty (that is, it doesn't even contain the 'std' namespace) without that define. LLDB currently doesn't know about that define in the expression parser, so it will load the wrong 'empty' module which should trigger this error. Also removed the 'auto' for that variable as the function name doesn't make it obvious that this is an expected and not just a optional/ptr (which is how this slipped in from the start). llvm-svn: 374525 --- .../import-std-module/empty-module/Makefile | 9 ++++++ .../empty-module/TestEmptyStdModule.py | 35 ++++++++++++++++++++++ .../import-std-module/empty-module/main.cpp | 8 +++++ .../empty-module/root/usr/include/c++/v1/algorithm | 22 ++++++++++++++ .../root/usr/include/c++/v1/module.modulemap | 3 ++ .../empty-module/root/usr/include/libc_header.h | 1 + 6 files changed, 78 insertions(+) create mode 100644 lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/Makefile create mode 100644 lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/TestEmptyStdModule.py create mode 100644 lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/main.cpp create mode 100644 lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/c++/v1/algorithm create mode 100644 lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/c++/v1/module.modulemap create mode 100644 lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/libc_header.h (limited to 'lldb/packages/Python/lldbsuite') diff --git a/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/Makefile b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/Makefile new file mode 100644 index 00000000000..533ff707023 --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/Makefile @@ -0,0 +1,9 @@ +# We don't have any standard include directories, so we can't +# parse the test_common.h header we usually inject as it includes +# system headers. +NO_TEST_COMMON_H := 1 + +CXXFLAGS_EXTRAS = -I $(SRCDIR)/root/usr/include/c++/v1/ -I $(SRCDIR)/root/usr/include/ -nostdinc -nostdinc++ -DENABLE_STD_CONTENT=1 +CXX_SOURCES := main.cpp + +include Makefile.rules diff --git a/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/TestEmptyStdModule.py b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/TestEmptyStdModule.py new file mode 100644 index 00000000000..94af997298f --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/TestEmptyStdModule.py @@ -0,0 +1,35 @@ +""" +Test that LLDB doesn't crash if the std module we load is empty. +""" + +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil +import os + +class ImportStdModule(TestBase): + + mydir = TestBase.compute_mydir(__file__) + + @skipIf(compiler=no_match("clang")) + def test(self): + self.build() + + sysroot = os.path.join(os.getcwd(), "root") + + # Set the sysroot. + self.runCmd("platform select --sysroot '" + sysroot + "' host", CURRENT_EXECUTABLE_SET) + + lldbutil.run_to_source_breakpoint(self, + "// Set break point at this line.", lldb.SBFileSpec("main.cpp")) + + self.runCmd("settings set target.import-std-module true") + self.runCmd("log enable lldb expr") + + # Use the typedef that is only defined in our 'empty' module. If this fails, then LLDB + # somehow figured out the correct define for the header and compiled the right + # standard module that actually contains the std::vector template. + self.expect("expr MissingContent var = 3; var", substrs=['$0 = 3']) + # Try to access our mock std::vector. This should fail but not crash LLDB as the + # std::vector template should be missing from the std module. + self.expect("expr (size_t)v.size()", substrs=["Couldn't lookup symbols"], error=True) diff --git a/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/main.cpp b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/main.cpp new file mode 100644 index 00000000000..b01fe1a7853 --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/main.cpp @@ -0,0 +1,8 @@ +#include + +int main(int argc, char **argv) { + // Makes sure we have the mock libc headers in the debug information. + libc_struct s; + std::vector v; + return 0; // Set break point at this line. +} diff --git a/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/c++/v1/algorithm b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/c++/v1/algorithm new file mode 100644 index 00000000000..a77c3d86739 --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/c++/v1/algorithm @@ -0,0 +1,22 @@ +// This is only defined when building, but LLDB is missing this flag when loading the standard +// library module so the actual contents of the module are missing. +#ifdef ENABLE_STD_CONTENT + +#include "libc_header.h" + +namespace std { + inline namespace __1 { + // Pretend to be a std::vector template we need to instantiate + // in LLDB. + template + struct vector { T i; int size() { return 2; } }; + } +} +#else +// Unused typedef we can use to check that we actually loaded +// an empty module. Will be missing if LLDB somehow can get the +// ENABLE_STD_CONTENT define right and break this test silently +// (as with the define the module isn't empty anymore and this +// test always succeeds). +typedef int MissingContent; +#endif // ENABLE_STD_CONTENT diff --git a/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/c++/v1/module.modulemap b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/c++/v1/module.modulemap new file mode 100644 index 00000000000..0eb48492a65 --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/c++/v1/module.modulemap @@ -0,0 +1,3 @@ +module std { + module "algorithm" { header "algorithm" export * } +} diff --git a/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/libc_header.h b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/libc_header.h new file mode 100644 index 00000000000..47525c9db34 --- /dev/null +++ b/lldb/packages/Python/lldbsuite/test/commands/expression/import-std-module/empty-module/root/usr/include/libc_header.h @@ -0,0 +1 @@ +struct libc_struct {}; -- cgit v1.2.3