Skip to content

Commit fcc08aa

Browse files
committed
[MachO] Add a test for detecting reserved unit length.
This is a follow-up for D71546 to add a corresponding unit test. Differential Revision: https://reviews.llvm.org/D72695
1 parent 0dc6c24 commit fcc08aa

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

lld/unittests/MachOTests/MachONormalizedFileToAtomsTests.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,41 @@ TEST(ToAtomsTest, basic_obj_x86_64) {
9797
EXPECT_TRUE(atom4->name().equals("_undef"));
9898
EXPECT_EQ(lld::Atom::definitionUndefined, atom4->definition());
9999
}
100+
101+
TEST(ToAtomsTest, reservedUnitLength) {
102+
static const uint8_t debugInfoWithReservedLengthContent[12] = {
103+
0xf0, 0xff, 0xff, 0xff // Reserved length value
104+
};
105+
static const uint8_t debugInfoWithValidBigLengthContent[12] = {
106+
0xef, 0xff, 0xff, 0xff, // The maximum valid length value for DWARF32
107+
0x00, 0x00 // Wrong version
108+
};
109+
static const uint8_t debugAbbrevDummyContent[] = {0x00};
110+
111+
NormalizedFile fReservedLength, fValidBigLength;
112+
fReservedLength.arch = lld::MachOLinkingContext::arch_x86;
113+
fValidBigLength.arch = lld::MachOLinkingContext::arch_x86;
114+
Section section;
115+
section.segmentName = "__DWARF";
116+
section.sectionName = "__debug_info";
117+
section.content = llvm::makeArrayRef(debugInfoWithReservedLengthContent);
118+
fReservedLength.sections.push_back(section);
119+
section.content = llvm::makeArrayRef(debugInfoWithValidBigLengthContent);
120+
fValidBigLength.sections.push_back(section);
121+
section.sectionName = "__debug_abbrev";
122+
section.content = llvm::makeArrayRef(debugAbbrevDummyContent);
123+
fReservedLength.sections.push_back(section);
124+
fValidBigLength.sections.push_back(section);
125+
126+
auto resultReservedLength = normalizedToAtoms(fReservedLength, "foo", false);
127+
auto resultValidBigLength = normalizedToAtoms(fValidBigLength, "foo", false);
128+
129+
// Both cases should return errors, but different.
130+
ASSERT_FALSE(resultReservedLength);
131+
ASSERT_FALSE(resultValidBigLength);
132+
133+
EXPECT_STREQ("Malformed DWARF in foo",
134+
toString(resultReservedLength.takeError()).c_str());
135+
EXPECT_STREQ("Unsupported DWARF version in foo",
136+
toString(resultValidBigLength.takeError()).c_str());
137+
}

0 commit comments

Comments
 (0)