1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "safe_copy.h" 18 19 #include <errno.h> 20 #include <string.h> 21 #include <sys/user.h> 22 23 #include "android-base/logging.h" 24 #include "globals.h" 25 #include "gtest/gtest.h" 26 #include "mman.h" 27 28 29 namespace art { 30 31 #if defined(__linux__) 32 33 TEST(SafeCopyTest, smoke) { 34 DCHECK_EQ(kPageSize, static_cast<decltype(kPageSize)>(PAGE_SIZE)); 35 36 // Map four pages, mark the second one as PROT_NONE, unmap the last one. 37 void* map = mmap(nullptr, kPageSize * 4, PROT_READ | PROT_WRITE, 38 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 39 ASSERT_NE(MAP_FAILED, map); 40 char* page1 = static_cast<char*>(map); 41 char* page2 = page1 + kPageSize; 42 char* page3 = page2 + kPageSize; 43 char* page4 = page3 + kPageSize; 44 ASSERT_EQ(0, mprotect(page1 + kPageSize, kPageSize, PROT_NONE)); 45 ASSERT_EQ(0, munmap(page4, kPageSize)); 46 47 page1[0] = 'a'; 48 page1[kPageSize - 1] = 'z'; 49 50 page3[0] = 'b'; 51 page3[kPageSize - 1] = 'y'; 52 53 char buf[kPageSize]; 54 55 // Completely valid read. 56 memset(buf, 0xCC, sizeof(buf)); 57 EXPECT_EQ(static_cast<ssize_t>(kPageSize), SafeCopy(buf, page1, kPageSize)) << strerror(errno); 58 EXPECT_EQ(0, memcmp(buf, page1, kPageSize)); 59 60 // Reading into a guard page. 61 memset(buf, 0xCC, sizeof(buf)); 62 EXPECT_EQ(static_cast<ssize_t>(kPageSize - 1), SafeCopy(buf, page1 + 1, kPageSize)); 63 EXPECT_EQ(0, memcmp(buf, page1 + 1, kPageSize - 1)); 64 65 // Reading from a guard page into a real page. 66 memset(buf, 0xCC, sizeof(buf)); 67 EXPECT_EQ(0, SafeCopy(buf, page2 + kPageSize - 1, kPageSize)); 68 69 // Reading off of the end of a mapping. 70 memset(buf, 0xCC, sizeof(buf)); 71 EXPECT_EQ(static_cast<ssize_t>(kPageSize), SafeCopy(buf, page3, kPageSize * 2)); 72 EXPECT_EQ(0, memcmp(buf, page3, kPageSize)); 73 74 // Completely invalid. 75 EXPECT_EQ(0, SafeCopy(buf, page1 + kPageSize, kPageSize)); 76 77 // Clean up. 78 ASSERT_EQ(0, munmap(map, kPageSize * 3)); 79 } 80 81 TEST(SafeCopyTest, alignment) { 82 DCHECK_EQ(kPageSize, static_cast<decltype(kPageSize)>(PAGE_SIZE)); 83 84 // Copy the middle of a mapping to the end of another one. 85 void* src_map = mmap(nullptr, kPageSize * 3, PROT_READ | PROT_WRITE, 86 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 87 ASSERT_NE(MAP_FAILED, src_map); 88 89 // Add a guard page to make sure we don't write past the end of the mapping. 90 void* dst_map = mmap(nullptr, kPageSize * 4, PROT_READ | PROT_WRITE, 91 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 92 ASSERT_NE(MAP_FAILED, dst_map); 93 94 char* src = static_cast<char*>(src_map); 95 char* dst = static_cast<char*>(dst_map); 96 ASSERT_EQ(0, mprotect(dst + 3 * kPageSize, kPageSize, PROT_NONE)); 97 98 src[512] = 'a'; 99 src[kPageSize * 3 - 512 - 1] = 'z'; 100 101 EXPECT_EQ(static_cast<ssize_t>(kPageSize * 3 - 1024), 102 SafeCopy(dst + 1024, src + 512, kPageSize * 3 - 1024)); 103 EXPECT_EQ(0, memcmp(dst + 1024, src + 512, kPageSize * 3 - 1024)); 104 105 ASSERT_EQ(0, munmap(src_map, kPageSize * 3)); 106 ASSERT_EQ(0, munmap(dst_map, kPageSize * 4)); 107 } 108 109 #endif // defined(__linux__) 110 111 } // namespace art 112