Skip to content

Commit 96b7606

Browse files
committed
[scudo] Fix EXPECT_DEATH tests
Put allocate/deallocate next to memory access inside EXPECT_DEATH block. This way we reduce probability that memory is not mapped by unrelated code. It's still not absolutely guaranty that mmap does not happen so we repeat it few times to be sure. Reviewed By: cryptoad Differential Revision: https://reviews.llvm.org/D102886
1 parent 6c05f2d commit 96b7606

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

compiler-rt/lib/scudo/standalone/tests/map_test.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ TEST(ScudoMapTest, MapNoAccessUnmap) {
3131

3232
TEST(ScudoMapTest, MapUnmap) {
3333
const scudo::uptr Size = 4 * scudo::getPageSizeCached();
34-
void *P = scudo::map(nullptr, Size, MappingName, 0, nullptr);
35-
EXPECT_NE(P, nullptr);
36-
memset(P, 0xaa, Size);
37-
scudo::unmap(P, Size, 0, nullptr);
38-
EXPECT_DEATH(memset(P, 0xbb, Size), "");
34+
EXPECT_DEATH(
35+
{
36+
// Repeat few time to avoid missing crash if it's mmaped by unrelated
37+
// code.
38+
for (int i = 0; i < 10; ++i) {
39+
void *P = scudo::map(nullptr, Size, MappingName, 0, nullptr);
40+
if (!P)
41+
continue;
42+
scudo::unmap(P, Size, 0, nullptr);
43+
memset(P, 0xbb, Size);
44+
}
45+
},
46+
"");
3947
}
4048

4149
TEST(ScudoMapTest, MapWithGuardUnmap) {

compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,20 @@ template <typename Config> static void testSecondaryBasic(void) {
3939
// the test on arm32 until we can debug it further.
4040
#ifndef __arm__
4141
// If the Secondary can't cache that pointer, it will be unmapped.
42-
if (!L->canCache(Size))
43-
EXPECT_DEATH(memset(P, 'A', Size), "");
42+
if (!L->canCache(Size)) {
43+
EXPECT_DEATH(
44+
{
45+
// Repeat few time to avoid missing crash if it's mmaped by unrelated
46+
// code.
47+
for (int i = 0; i < 10; ++i) {
48+
P = L->allocate(scudo::Options{}, Size);
49+
L->deallocate(scudo::Options{}, P);
50+
memset(P, 'A', Size);
51+
}
52+
},
53+
"");
4454
#endif // __arm__
55+
}
4556

4657
const scudo::uptr Align = 1U << 16;
4758
P = L->allocate(scudo::Options{}, Size + Align, Align);

0 commit comments

Comments
 (0)