Skip to content

Commit c3c54d3

Browse files
Error out if we try to annotate a zero address (llvm#72)
This patch makes the exegesis annotator error out if we try to map the zero address as we are not able to do this with mmap. To solve this while still being able to annotate the blocks, we probably need some way to adjust the register/memory values.
1 parent eb3f213 commit c3c54d3

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

gematria/datasets/find_accessed_addrs_exegesis.cc

+17-10
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,23 @@ Expected<AccessedAddrs> ExegesisAnnotator::findAccessedAddrs(
143143
if (!std::get<0>(BenchmarkResultOrErr).isA<SnippetSegmentationFault>())
144144
return std::move(std::get<0>(BenchmarkResultOrErr));
145145

146-
handleAllErrors(std::move(std::get<0>(BenchmarkResultOrErr)),
147-
[&](SnippetSegmentationFault &CrashInfo) {
148-
MemoryMapping MemMap;
149-
// Zero out the last twelve bits of the address to align
150-
// the address to a page boundary.
151-
uintptr_t MapAddress = (CrashInfo.getAddress() & ~0xfff);
152-
MemMap.Address = MapAddress;
153-
MemMap.MemoryValueName = "memdef1";
154-
BenchCode.Key.MemoryMappings.push_back(MemMap);
155-
});
146+
Error AnnotationError = handleErrors(
147+
std::move(std::get<0>(BenchmarkResultOrErr)),
148+
[&](SnippetSegmentationFault &CrashInfo) -> Error {
149+
MemoryMapping MemMap;
150+
// Zero out the last twelve bits of the address to align
151+
// the address to a page boundary.
152+
uintptr_t MapAddress = (CrashInfo.getAddress() & ~0xfff);
153+
if (MapAddress == 0)
154+
return make_error<Failure>("Segfault at zero address, cannot map.");
155+
MemMap.Address = MapAddress;
156+
MemMap.MemoryValueName = "memdef1";
157+
BenchCode.Key.MemoryMappings.push_back(MemMap);
158+
159+
return Error::success();
160+
});
161+
162+
if (AnnotationError) return std::move(AnnotationError);
156163
}
157164

158165
MemAnnotations.accessed_blocks.reserve(BenchCode.Key.MemoryMappings.size());

gematria/datasets/find_accessed_addrs_exegesis_test.cc

+8
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,13 @@ TEST_F(FindAccessedAddrsExegesisTest, ExegesisNotPageAligned) {
113113
EXPECT_EQ(Result.accessed_blocks[0], 0x10000);
114114
}
115115

116+
TEST_F(FindAccessedAddrsExegesisTest, ExegesisZeroAddressError) {
117+
auto AddrsOrErr = FindAccessedAddrsExegesis(R"asm(
118+
movq $0x0, %rax
119+
movq (%rax), %rax
120+
)asm");
121+
ASSERT_FALSE(static_cast<bool>(AddrsOrErr));
122+
}
123+
116124
} // namespace
117125
} // namespace gematria

0 commit comments

Comments
 (0)