blob: a9ec9528a188a8324138049b944222f857987cd2 [file] [log] [blame]
Josh Gao682282e2017-04-17 18:31:26 -07001/*
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 "common_runtime_test.h"
20
Josh Gao9a2eea32017-04-19 15:44:20 -070021#include <errno.h>
22#include <string.h>
Josh Gao682282e2017-04-17 18:31:26 -070023#include <sys/mman.h>
24#include <sys/user.h>
25
Andreas Gampe01f77432017-05-24 09:56:30 -070026#include "globals.h"
27
Josh Gao682282e2017-04-17 18:31:26 -070028namespace art {
29
30#if defined(__linux__)
31
32TEST(SafeCopyTest, smoke) {
Andreas Gampe01f77432017-05-24 09:56:30 -070033 DCHECK_EQ(kPageSize, static_cast<decltype(kPageSize)>(PAGE_SIZE));
34
Josh Gao9a2eea32017-04-19 15:44:20 -070035 // Map four pages, mark the second one as PROT_NONE, unmap the last one.
Andreas Gampe01f77432017-05-24 09:56:30 -070036 void* map = mmap(nullptr, kPageSize * 4, PROT_READ | PROT_WRITE,
Josh Gao682282e2017-04-17 18:31:26 -070037 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
38 ASSERT_NE(MAP_FAILED, map);
39 char* page1 = static_cast<char*>(map);
Andreas Gampe01f77432017-05-24 09:56:30 -070040 char* page2 = page1 + kPageSize;
41 char* page3 = page2 + kPageSize;
42 char* page4 = page3 + kPageSize;
43 ASSERT_EQ(0, mprotect(page1 + kPageSize, kPageSize, PROT_NONE));
44 ASSERT_EQ(0, munmap(page4, kPageSize));
Josh Gao682282e2017-04-17 18:31:26 -070045
46 page1[0] = 'a';
Andreas Gampe01f77432017-05-24 09:56:30 -070047 page1[kPageSize - 1] = 'z';
Josh Gao682282e2017-04-17 18:31:26 -070048
Josh Gao9a2eea32017-04-19 15:44:20 -070049 page3[0] = 'b';
Andreas Gampe01f77432017-05-24 09:56:30 -070050 page3[kPageSize - 1] = 'y';
Josh Gao9a2eea32017-04-19 15:44:20 -070051
Andreas Gampe01f77432017-05-24 09:56:30 -070052 char buf[kPageSize];
Josh Gao682282e2017-04-17 18:31:26 -070053
54 // Completely valid read.
55 memset(buf, 0xCC, sizeof(buf));
Andreas Gampe01f77432017-05-24 09:56:30 -070056 EXPECT_EQ(static_cast<ssize_t>(kPageSize), SafeCopy(buf, page1, kPageSize)) << strerror(errno);
57 EXPECT_EQ(0, memcmp(buf, page1, kPageSize));
Josh Gao682282e2017-04-17 18:31:26 -070058
Josh Gao9a2eea32017-04-19 15:44:20 -070059 // Reading into a guard page.
Josh Gao682282e2017-04-17 18:31:26 -070060 memset(buf, 0xCC, sizeof(buf));
Andreas Gampe01f77432017-05-24 09:56:30 -070061 EXPECT_EQ(static_cast<ssize_t>(kPageSize - 1), SafeCopy(buf, page1 + 1, kPageSize));
62 EXPECT_EQ(0, memcmp(buf, page1 + 1, kPageSize - 1));
Josh Gao682282e2017-04-17 18:31:26 -070063
Josh Gao9a2eea32017-04-19 15:44:20 -070064 // Reading from a guard page into a real page.
65 memset(buf, 0xCC, sizeof(buf));
Andreas Gampe01f77432017-05-24 09:56:30 -070066 EXPECT_EQ(0, SafeCopy(buf, page2 + kPageSize - 1, kPageSize));
Josh Gao9a2eea32017-04-19 15:44:20 -070067
68 // Reading off of the end of a mapping.
69 memset(buf, 0xCC, sizeof(buf));
Andreas Gampe01f77432017-05-24 09:56:30 -070070 EXPECT_EQ(static_cast<ssize_t>(kPageSize), SafeCopy(buf, page3, kPageSize * 2));
71 EXPECT_EQ(0, memcmp(buf, page3, kPageSize));
Josh Gao9a2eea32017-04-19 15:44:20 -070072
Josh Gao682282e2017-04-17 18:31:26 -070073 // Completely invalid.
Andreas Gampe01f77432017-05-24 09:56:30 -070074 EXPECT_EQ(0, SafeCopy(buf, page1 + kPageSize, kPageSize));
Josh Gao9a2eea32017-04-19 15:44:20 -070075
76 // Clean up.
Andreas Gampe01f77432017-05-24 09:56:30 -070077 ASSERT_EQ(0, munmap(map, kPageSize * 3));
Josh Gao9a2eea32017-04-19 15:44:20 -070078}
79
80TEST(SafeCopyTest, alignment) {
Andreas Gampe01f77432017-05-24 09:56:30 -070081 DCHECK_EQ(kPageSize, static_cast<decltype(kPageSize)>(PAGE_SIZE));
82
Josh Gao9a2eea32017-04-19 15:44:20 -070083 // Copy the middle of a mapping to the end of another one.
Andreas Gampe01f77432017-05-24 09:56:30 -070084 void* src_map = mmap(nullptr, kPageSize * 3, PROT_READ | PROT_WRITE,
Josh Gao9a2eea32017-04-19 15:44:20 -070085 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
86 ASSERT_NE(MAP_FAILED, src_map);
87
88 // Add a guard page to make sure we don't write past the end of the mapping.
Andreas Gampe01f77432017-05-24 09:56:30 -070089 void* dst_map = mmap(nullptr, kPageSize * 4, PROT_READ | PROT_WRITE,
Josh Gao9a2eea32017-04-19 15:44:20 -070090 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
91 ASSERT_NE(MAP_FAILED, dst_map);
92
93 char* src = static_cast<char*>(src_map);
94 char* dst = static_cast<char*>(dst_map);
Andreas Gampe01f77432017-05-24 09:56:30 -070095 ASSERT_EQ(0, mprotect(dst + 3 * kPageSize, kPageSize, PROT_NONE));
Josh Gao9a2eea32017-04-19 15:44:20 -070096
97 src[512] = 'a';
Andreas Gampe01f77432017-05-24 09:56:30 -070098 src[kPageSize * 3 - 512 - 1] = 'z';
Josh Gao9a2eea32017-04-19 15:44:20 -070099
Andreas Gampe01f77432017-05-24 09:56:30 -0700100 EXPECT_EQ(static_cast<ssize_t>(kPageSize * 3 - 1024),
101 SafeCopy(dst + 1024, src + 512, kPageSize * 3 - 1024));
102 EXPECT_EQ(0, memcmp(dst + 1024, src + 512, kPageSize * 3 - 1024));
Josh Gao9a2eea32017-04-19 15:44:20 -0700103
Andreas Gampe01f77432017-05-24 09:56:30 -0700104 ASSERT_EQ(0, munmap(src_map, kPageSize * 3));
105 ASSERT_EQ(0, munmap(dst_map, kPageSize * 4));
Josh Gao682282e2017-04-17 18:31:26 -0700106}
107
108#endif // defined(__linux__)
109
110} // namespace art