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
17 #include <inttypes.h>
18 #include <stdio.h>
19 #include <sys/mman.h>
20 #include <sys/prctl.h>
21 #include <unistd.h>
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"
30 #include "private/bionic_prctl.h"
31
32 // http://b/20017123.
TEST(sys_prctl,bug_20017123)33 TEST(sys_prctl, bug_20017123) {
34 #if defined(__ANDROID__)
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"));
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_end = 0;
45 std::vector<std::string> lines = android::base::Split(file_data, "\n");
46 for (size_t i = 0; i < lines.size(); i++) {
47 if (lines[i].empty()) {
48 continue;
49 }
50 uintptr_t start;
51 uintptr_t end;
52 ASSERT_EQ(2, sscanf(lines[i].c_str(), "%" SCNxPTR "-%" SCNxPTR " ", &start, &end))
53 << "Failed to parse line: " << lines[i];
54 // This will never fail on the first line, so no need to do any special checking.
55 ASSERT_GE(start, last_end)
56 << "Overlapping map detected:\n" << lines[i -1] << '\n' << lines[i] << '\n';
57 last_end = end;
58 }
59
60 ASSERT_EQ(0, munmap(p, page_size * 3));
61 #else
62 GTEST_LOG_(INFO) << "This test does nothing as it tests an Android specific kernel feature.";
63 #endif
64 }
65