[sanitizer] Implement MprotectReadOnly and MprotectNoAccess

MprotectReadOnly for Win and Fuchsia
MprotectNoAccess for Fuchsia
This commit is contained in:
Vitaly Buka 2021-12-01 14:49:14 -08:00
parent 6146e4cf89
commit e599aa80c0
3 changed files with 32 additions and 0 deletions

View File

@ -274,6 +274,15 @@ void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
UNIMPLEMENTED();
}
bool MprotectNoAccess(uptr addr, uptr size) {
return _zx_vmar_protect(_zx_vmar_root_self(), 0, Addr, Size) == ZX_OK;
}
bool MprotectReadOnly(uptr addr, uptr size) {
return _zx_vmar_protect(_zx_vmar_root_self(), ZX_VM_PERM_READ, Addr, Size) ==
ZX_OK;
}
void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
const char *mem_type) {
CHECK_GE(size, GetPageSize());

View File

@ -337,6 +337,11 @@ bool MprotectNoAccess(uptr addr, uptr size) {
return VirtualProtect((LPVOID)addr, size, PAGE_NOACCESS, &old_protection);
}
bool MprotectReadOnly(uptr addr, uptr size) {
DWORD old_protection;
return VirtualProtect((LPVOID)addr, size, PAGE_READONLY, &old_protection);
}
void ReleaseMemoryPagesToOS(uptr beg, uptr end) {
uptr beg_aligned = RoundDownTo(beg, GetPageSizeCached()),
end_aligned = RoundDownTo(end, GetPageSizeCached());

View File

@ -87,6 +87,24 @@ TEST(SanitizerCommon, MmapAlignedOrDieOnFatalError) {
}
}
TEST(SanitizerCommon, Mprotect) {
uptr PageSize = GetPageSizeCached();
u8 *mem = reinterpret_cast<u8 *>(MmapOrDie(PageSize, "MprotectTest"));
for (u8 *p = mem; p < mem + PageSize; ++p) ++(*p);
MprotectReadOnly(reinterpret_cast<uptr>(mem), PageSize);
for (u8 *p = mem; p < mem + PageSize; ++p) EXPECT_EQ(1u, *p);
EXPECT_DEATH(++mem[0], "");
EXPECT_DEATH(++mem[PageSize / 2], "");
EXPECT_DEATH(++mem[PageSize - 1], "");
MprotectNoAccess(reinterpret_cast<uptr>(mem), PageSize);
volatile u8 t;
EXPECT_DEATH(t = mem[0], "");
EXPECT_DEATH(t = mem[PageSize / 2], "");
EXPECT_DEATH(t = mem[PageSize - 1], "");
}
TEST(SanitizerCommon, InternalMmapVectorRoundUpCapacity) {
InternalMmapVector<uptr> v;
v.reserve(1);