[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
This commit is contained in:
Vitaly Buka 2021-05-20 16:17:25 -07:00
parent 6c05f2dab3
commit 96b760607f
2 changed files with 26 additions and 7 deletions

View File

@ -31,11 +31,19 @@ TEST(ScudoMapTest, MapNoAccessUnmap) {
TEST(ScudoMapTest, MapUnmap) {
const scudo::uptr Size = 4 * scudo::getPageSizeCached();
void *P = scudo::map(nullptr, Size, MappingName, 0, nullptr);
EXPECT_NE(P, nullptr);
memset(P, 0xaa, Size);
scudo::unmap(P, Size, 0, nullptr);
EXPECT_DEATH(memset(P, 0xbb, Size), "");
EXPECT_DEATH(
{
// Repeat few time to avoid missing crash if it's mmaped by unrelated
// code.
for (int i = 0; i < 10; ++i) {
void *P = scudo::map(nullptr, Size, MappingName, 0, nullptr);
if (!P)
continue;
scudo::unmap(P, Size, 0, nullptr);
memset(P, 0xbb, Size);
}
},
"");
}
TEST(ScudoMapTest, MapWithGuardUnmap) {

View File

@ -39,9 +39,20 @@ template <typename Config> static void testSecondaryBasic(void) {
// the test on arm32 until we can debug it further.
#ifndef __arm__
// If the Secondary can't cache that pointer, it will be unmapped.
if (!L->canCache(Size))
EXPECT_DEATH(memset(P, 'A', Size), "");
if (!L->canCache(Size)) {
EXPECT_DEATH(
{
// Repeat few time to avoid missing crash if it's mmaped by unrelated
// code.
for (int i = 0; i < 10; ++i) {
P = L->allocate(scudo::Options{}, Size);
L->deallocate(scudo::Options{}, P);
memset(P, 'A', Size);
}
},
"");
#endif // __arm__
}
const scudo::uptr Align = 1U << 16;
P = L->allocate(scudo::Options{}, Size + Align, Align);