1 //===-- map_test.cpp --------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "tests/scudo_unit_test.h" 10 11 #include "common.h" 12 13 #include <string.h> 14 #include <unistd.h> 15 16 static const char *MappingName = "scudo:test"; 17 18 TEST(ScudoMapTest, PageSize) { 19 EXPECT_EQ(scudo::getPageSizeCached(), 20 static_cast<scudo::uptr>(getpagesize())); 21 } 22 23 TEST(ScudoMapDeathTest, MapNoAccessUnmap) { 24 const scudo::uptr Size = 4 * scudo::getPageSizeCached(); 25 scudo::MapPlatformData Data = {}; 26 void *P = scudo::map(nullptr, Size, MappingName, MAP_NOACCESS, &Data); 27 EXPECT_NE(P, nullptr); 28 EXPECT_DEATH(memset(P, 0xaa, Size), ""); 29 scudo::unmap(P, Size, UNMAP_ALL, &Data); 30 } 31 32 TEST(ScudoMapDeathTest, MapUnmap) { 33 const scudo::uptr Size = 4 * scudo::getPageSizeCached(); 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 ""); 47 } 48 49 TEST(ScudoMapDeathTest, MapWithGuardUnmap) { 50 const scudo::uptr PageSize = scudo::getPageSizeCached(); 51 const scudo::uptr Size = 4 * PageSize; 52 scudo::MapPlatformData Data = {}; 53 void *P = scudo::map(nullptr, Size + 2 * PageSize, MappingName, MAP_NOACCESS, 54 &Data); 55 EXPECT_NE(P, nullptr); 56 void *Q = 57 reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(P) + PageSize); 58 EXPECT_EQ(scudo::map(Q, Size, MappingName, 0, &Data), Q); 59 memset(Q, 0xaa, Size); 60 EXPECT_DEATH(memset(Q, 0xaa, Size + 1), ""); 61 scudo::unmap(P, Size + 2 * PageSize, UNMAP_ALL, &Data); 62 } 63 64 TEST(ScudoMapTest, MapGrowUnmap) { 65 const scudo::uptr PageSize = scudo::getPageSizeCached(); 66 const scudo::uptr Size = 4 * PageSize; 67 scudo::MapPlatformData Data = {}; 68 void *P = scudo::map(nullptr, Size, MappingName, MAP_NOACCESS, &Data); 69 EXPECT_NE(P, nullptr); 70 void *Q = 71 reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(P) + PageSize); 72 EXPECT_EQ(scudo::map(Q, PageSize, MappingName, 0, &Data), Q); 73 memset(Q, 0xaa, PageSize); 74 Q = reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(Q) + PageSize); 75 EXPECT_EQ(scudo::map(Q, PageSize, MappingName, 0, &Data), Q); 76 memset(Q, 0xbb, PageSize); 77 scudo::unmap(P, Size, UNMAP_ALL, &Data); 78 } 79