llvm.org GIT mirror llvm / 41b1ba9
[DWARF] Don't attempt to parse line tables at invalid offsets Whilst working on improvements to the error handling of the debug line parsing code, I noticed that if an invalid offset were to be specified in a call to getOrParseLineTable(), an entry in the LineTableMap would still be created, even if the offset was not within the section range. The immediate parsing attempt afterwards would fail (it would end up getting a version of 0), and thereafter, any subsequent calls to getOrParseLineTable or getLineTable would return the default- constructed, invalid line table. In reality, we shouldn't even attempt to parse this table, and we should always return a nullptr from these two functions for this situation. I have tested this via a unit test, which required some new framework for unit testing debug line. My plan is to add quite a few more unit tests for the new error reporting mechanism that will follow shortly, hence the reason why the supporting code for the tests are written the way they are - I intend to extend the DwarfGenerator class to support generating debug line. At that point, I'll make sure that there are a few positive test cases for this and the parsing code too. Differential Revision: https://reviews.llvm.org/D44200 Reviewers: JDevlieghere, aprantl git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326995 91177308-0d34-0410-b5e6-96231b3b80d8 James Henderson 2 years ago
6 changed file(s) with 143 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
448448 DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData,
449449 uint32_t Offset, const DWARFContext &Ctx,
450450 const DWARFUnit *U) {
451 if (!DebugLineData.isValidOffset(Offset))
452 return nullptr;
453
451454 std::pair Pos =
452455 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
453456 LineTable *LT = &Pos.first->second;
99
1010 set(DebugInfoSources
1111 DwarfGenerator.cpp
12 DwarfUtils.cpp
1213 DWARFDebugInfoTest.cpp
14 DWARFDebugLineTest.cpp
1315 DWARFFormValueTest.cpp
1416 )
1517
None //===- llvm/unittest/DebugInfo/DWARFFormValueTest.cpp ---------------------===//
0 //===- llvm/unittest/DebugInfo/DWARFDebugInfoTest.cpp ---------------------===//
11 //
22 // The LLVM Compiler Infrastructure
33 //
77 //===----------------------------------------------------------------------===//
88
99 #include "DwarfGenerator.h"
10 #include "DwarfUtils.h"
1011 #include "llvm/ADT/ArrayRef.h"
1112 #include "llvm/ADT/Optional.h"
1213 #include "llvm/ADT/SmallString.h"
3536
3637 using namespace llvm;
3738 using namespace dwarf;
39 using namespace utils;
3840
3941 namespace {
40
41 void initLLVMIfNeeded() {
42 static bool gInitialized = false;
43 if (!gInitialized) {
44 gInitialized = true;
45 InitializeAllTargets();
46 InitializeAllTargetMCs();
47 InitializeAllAsmPrinters();
48 InitializeAllAsmParsers();
49 }
50 }
51
52 Triple getHostTripleForAddrSize(uint8_t AddrSize) {
53 Triple PT(Triple::normalize(LLVM_HOST_TRIPLE));
54
55 if (AddrSize == 8 && PT.isArch32Bit())
56 return PT.get64BitArchVariant();
57 if (AddrSize == 4 && PT.isArch64Bit())
58 return PT.get32BitArchVariant();
59 return PT;
60 }
61
62 static bool isConfigurationSupported(Triple &T) {
63 initLLVMIfNeeded();
64 std::string Err;
65 return TargetRegistry::lookupTarget(T.getTriple(), Err);
66 }
6742
6843 template
6944 void TestAllForms() {
0 //===- DWARFDebugLineTest.cpp ---------------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "DwarfGenerator.h"
10 #include "DwarfUtils.h"
11 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
12 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
13 #include "llvm/Object/ObjectFile.h"
14 #include "llvm/Testing/Support/Error.h"
15 #include "gtest/gtest.h"
16
17 using namespace llvm;
18 using namespace dwarf;
19 using namespace object;
20 using namespace utils;
21
22 namespace {
23
24 struct DebugLineGenerator {
25 bool init() {
26 Triple T = getHostTripleForAddrSize(8);
27 if (!isConfigurationSupported(T))
28 return false;
29 auto ExpectedGenerator = dwarfgen::Generator::create(T, 4);
30 if (ExpectedGenerator)
31 Generator.reset(ExpectedGenerator->release());
32 return true;
33 }
34
35 std::unique_ptr createContext() {
36 if (!Generator)
37 return nullptr;
38 StringRef FileBytes = Generator->generate();
39 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
40 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
41 if (Obj)
42 return DWARFContext::create(**Obj);
43 return nullptr;
44 }
45
46 std::unique_ptr Generator;
47 };
48
49 TEST(DWARFDebugLine, GetLineTableAtInvalidOffset) {
50 DebugLineGenerator LineGen;
51 if (!LineGen.init())
52 return;
53
54 DWARFDebugLine Line;
55 std::unique_ptr Context = LineGen.createContext();
56 ASSERT_TRUE(Context != nullptr);
57 const DWARFObject &Obj = Context->getDWARFObj();
58 DWARFDataExtractor LineData(Obj, Obj.getLineSection(), true, 8);
59
60 EXPECT_EQ(Line.getOrParseLineTable(LineData, 0, *Context, nullptr), nullptr);
61 }
62
63 } // end anonymous namespace
0 //===--- unittests/DebugInfo/DWARF/DwarfUtils.cpp ---------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "DwarfUtils.h"
10 #include "llvm/ADT/Triple.h"
11 #include "llvm/Support/TargetRegistry.h"
12 #include "llvm/Support/TargetSelect.h"
13
14 using namespace llvm;
15
16 static void initLLVMIfNeeded() {
17 static bool gInitialized = false;
18 if (!gInitialized) {
19 gInitialized = true;
20 InitializeAllTargets();
21 InitializeAllTargetMCs();
22 InitializeAllAsmPrinters();
23 InitializeAllAsmParsers();
24 }
25 }
26
27 Triple llvm::dwarf::utils::getHostTripleForAddrSize(uint8_t AddrSize) {
28 Triple T(Triple::normalize(LLVM_HOST_TRIPLE));
29
30 if (AddrSize == 8 && T.isArch32Bit())
31 return T.get64BitArchVariant();
32 if (AddrSize == 4 && T.isArch64Bit())
33 return T.get32BitArchVariant();
34 return T;
35 }
36
37 bool llvm::dwarf::utils::isConfigurationSupported(Triple &T) {
38 initLLVMIfNeeded();
39 std::string Err;
40 return TargetRegistry::lookupTarget(T.getTriple(), Err);
41 }
0 //===--- unittests/DebugInfo/DWARF/DwarfUtils.h -----------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_UNITTESTS_DEBUG_INFO_DWARF_DWARFUTILS_H
10 #define LLVM_UNITTESTS_DEBUG_INFO_DWARF_DWARFUTILS_H
11
12 #include
13
14 namespace llvm {
15
16 class Triple;
17
18 namespace dwarf {
19 namespace utils {
20
21 Triple getHostTripleForAddrSize(uint8_t AddrSize);
22 bool isConfigurationSupported(Triple &T);
23
24 } // end namespace utils
25 } // end namespace dwarf
26 } // end namespace llvm
27
28 #endif // LLVM_UNITTESTS_DEBUG_INFO_DWARF_DWARFUTILS_H