From: Alexey Samsonov Date: Fri, 5 Sep 2014 19:29:45 +0000 (+0000) Subject: [DWARF parser] Fix nasty memory corruption in .dwo files handling. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=54543afeba0c7434b74f4f61bb39f12b3779414c;p=oota-llvm.git [DWARF parser] Fix nasty memory corruption in .dwo files handling. Forge a test case where llvm-symbolizer has to use external .dwo file to produce the inlining information. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217270 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/DebugInfo/DWARFUnit.cpp b/lib/DebugInfo/DWARFUnit.cpp index fe75ebe2dc5..2bb7933e88b 100644 --- a/lib/DebugInfo/DWARFUnit.cpp +++ b/lib/DebugInfo/DWARFUnit.cpp @@ -235,11 +235,14 @@ size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) { return DieArray.size(); } -DWARFUnit::DWOHolder::DWOHolder(std::unique_ptr DWOFile) - : DWOFile(std::move(DWOFile)), - DWOContext( - cast(DIContext::getDWARFContext(*this->DWOFile))), - DWOU(nullptr) { +DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath) + : DWOFile(), DWOContext(), DWOU(nullptr) { + auto Obj = object::ObjectFile::createObjectFile(DWOPath); + if (!Obj) + return; + DWOFile = std::move(Obj.get()); + DWOContext.reset( + cast(DIContext::getDWARFContext(*DWOFile.getBinary()))); if (DWOContext->getNumDWOCompileUnits() > 0) DWOU = DWOContext->getDWOCompileUnitAtIndex(0); } @@ -261,12 +264,7 @@ bool DWARFUnit::parseDWO() { sys::path::append(AbsolutePath, CompilationDir); } sys::path::append(AbsolutePath, DWOFileName); - ErrorOr> DWOFile = - object::ObjectFile::createObjectFile(AbsolutePath); - if (!DWOFile) - return false; - // Reset DWOHolder. - DWO = llvm::make_unique(std::move(DWOFile->getBinary())); + DWO = llvm::make_unique(AbsolutePath); DWARFUnit *DWOCU = DWO->getUnit(); // Verify that compile unit in .dwo file is valid. if (!DWOCU || DWOCU->getDWOId() != getDWOId()) { diff --git a/lib/DebugInfo/DWARFUnit.h b/lib/DebugInfo/DWARFUnit.h index 763caddb9d7..ba26d55a1dd 100644 --- a/lib/DebugInfo/DWARFUnit.h +++ b/lib/DebugInfo/DWARFUnit.h @@ -51,11 +51,11 @@ class DWARFUnit { std::vector DieArray; class DWOHolder { - std::unique_ptr DWOFile; + object::OwningBinary DWOFile; std::unique_ptr DWOContext; DWARFUnit *DWOU; public: - DWOHolder(std::unique_ptr DWOFile); + DWOHolder(StringRef DWOPath); DWARFUnit *getUnit() const { return DWOU; } }; std::unique_ptr DWO; diff --git a/test/DebugInfo/Inputs/split-dwarf-test b/test/DebugInfo/Inputs/split-dwarf-test new file mode 100755 index 00000000000..a4411129a9b Binary files /dev/null and b/test/DebugInfo/Inputs/split-dwarf-test differ diff --git a/test/DebugInfo/Inputs/split-dwarf-test.cc b/test/DebugInfo/Inputs/split-dwarf-test.cc new file mode 100644 index 00000000000..5ed56f94b36 --- /dev/null +++ b/test/DebugInfo/Inputs/split-dwarf-test.cc @@ -0,0 +1,17 @@ +int foo(int a) { + return a + 1; +} + +int main(int argc, char *argv[]) { + return foo(argc); +} + +// Build instructions: +// 1) clang++ -### -O2 -gsplit-dwarf.cc split-dwarf-test.cc -o split-dwarf-test +// 2) Replace the value "-fdebug-compilation-dir" flag to "Output" +// (this is the temp directory used by lit). +// 3) Manually run clang-cc1, objcopy and ld invocations. +// 4) Copy the binary and .dwo file to the Inputs directory. Make sure the +// .dwo file will be available for symbolizer (use test RUN-lines to copy +// the .dwo file to a directory +// /. diff --git a/test/DebugInfo/Inputs/split-dwarf-test.dwo b/test/DebugInfo/Inputs/split-dwarf-test.dwo new file mode 100644 index 00000000000..74183a4b622 Binary files /dev/null and b/test/DebugInfo/Inputs/split-dwarf-test.dwo differ diff --git a/test/DebugInfo/llvm-symbolizer.test b/test/DebugInfo/llvm-symbolizer.test index 20d3dda21ab..fdd64d69f5b 100644 --- a/test/DebugInfo/llvm-symbolizer.test +++ b/test/DebugInfo/llvm-symbolizer.test @@ -19,10 +19,15 @@ RUN: echo "%p/Inputs/macho-universal:x86_64 0x100000f05" >> %t.input RUN: echo "%p/Inputs/llvm-symbolizer-dwo-test 0x400514" >> %t.input RUN: echo "%p/Inputs/fission-ranges.elf-x86_64 0x720" >> %t.input RUN: echo "%p/Inputs/arange-overlap.elf-x86_64 0x714" >> %t.input +RUN: cp %p/Inputs/split-dwarf-test.dwo %T +RUN: echo "%p/Inputs/split-dwarf-test 0x4004d0" >> %t.input +RUN: echo "%p/Inputs/split-dwarf-test 0x4004c0" >> %t.input RUN: llvm-symbolizer --functions=linkage --inlining --demangle=false \ RUN: --default-arch=i386 < %t.input | FileCheck %s +REQUIRES: shell + CHECK: main CHECK-NEXT: /tmp/dbginfo{{[/\\]}}dwarfdump-test.cc:16 @@ -98,6 +103,14 @@ CHECK-NEXT: {{.*}}fission-ranges.cc:6 CHECK: _ZN1S3bazEv CHECK-NEXT: {{.*}}arange-overlap.cc:6 +CHECK: _Z3fooi +CHECK-NEXT: {{.*}}split-dwarf-test.cc +CHECK-NEXT: main +CHECK-NEXT: {{.*}}split-dwarf-test.cc + +CHECK: _Z3fooi +CHECK-NEXT: {{.*}}split-dwarf-test.cc + RUN: echo "unexisting-file 0x1234" > %t.input2 RUN: llvm-symbolizer < %t.input2