Yabin Cui | 91ce715 | 2015-07-13 16:54:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Christopher Ferris | f1649d9 | 2016-11-03 16:06:40 -0700 | [diff] [blame^] | 17 | #include <inttypes.h> |
| 18 | #include <stdio.h> |
Yabin Cui | 91ce715 | 2015-07-13 16:54:29 -0700 | [diff] [blame] | 19 | #include <sys/mman.h> |
| 20 | #include <sys/prctl.h> |
| 21 | #include <unistd.h> |
Christopher Ferris | f1649d9 | 2016-11-03 16:06:40 -0700 | [diff] [blame^] | 22 | |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "android-base/file.h" |
| 29 | #include "android-base/strings.h" |
Yabin Cui | 91ce715 | 2015-07-13 16:54:29 -0700 | [diff] [blame] | 30 | #include "private/bionic_prctl.h" |
| 31 | |
| 32 | // http://b/20017123. |
| 33 | TEST(sys_prctl, bug_20017123) { |
Elliott Hughes | a9209d7 | 2016-09-16 18:16:47 -0700 | [diff] [blame] | 34 | #if defined(__ANDROID__) // PR_SET_VMA is only available in Android kernels. |
Yabin Cui | 91ce715 | 2015-07-13 16:54:29 -0700 | [diff] [blame] | 35 | size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE)); |
| 36 | void* p = mmap(NULL, page_size * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 37 | ASSERT_NE(MAP_FAILED, p); |
| 38 | ASSERT_EQ(0, mprotect(p, page_size, PROT_NONE)); |
| 39 | ASSERT_NE(-1, prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, page_size * 3, "anonymous map space")); |
Christopher Ferris | f1649d9 | 2016-11-03 16:06:40 -0700 | [diff] [blame^] | 40 | // Now read the maps and verify that there are no overlapped maps. |
| 41 | std::string file_data; |
| 42 | ASSERT_TRUE(android::base::ReadFileToString("/proc/self/maps", &file_data)); |
| 43 | |
| 44 | uintptr_t last_start = 0; |
| 45 | uintptr_t last_end = 0; |
| 46 | std::vector<std::string> lines = android::base::Split(file_data, "\n"); |
| 47 | for (size_t i = 0; i < lines.size(); i++) { |
| 48 | if (lines[i].empty()) { |
| 49 | continue; |
| 50 | } |
| 51 | uintptr_t start; |
| 52 | uintptr_t end; |
| 53 | ASSERT_EQ(2, sscanf(lines[i].c_str(), "%" SCNxPTR "-%" SCNxPTR " ", &start, &end)) |
| 54 | << "Failed to parse line: " << lines[i]; |
| 55 | // This will never fail on the first line, so no need to do any special checking. |
| 56 | ASSERT_GE(start, last_end) << "Overlapping map detected:\n" << lines[i -1] << lines[i]; |
| 57 | last_start = start; |
| 58 | last_end = end; |
| 59 | } |
| 60 | |
Yabin Cui | 91ce715 | 2015-07-13 16:54:29 -0700 | [diff] [blame] | 61 | ASSERT_EQ(0, munmap(p, page_size * 3)); |
| 62 | #else |
| 63 | GTEST_LOG_(INFO) << "This test does nothing as it tests an Android specific kernel feature."; |
| 64 | #endif |
| 65 | } |