sys_prctl_test.cpp revision 1dc3ae163e6190508e5b32f256622daa1146bc6b
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.
33TEST(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_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)
57        << "Overlapping map detected:\n" << lines[i -1] << '\n' << lines[i] << '\n';
58    last_start = start;
59    last_end = end;
60  }
61
62  ASSERT_EQ(0, munmap(p, page_size * 3));
63#else
64  GTEST_LOG_(INFO) << "This test does nothing as it tests an Android specific kernel feature.";
65#endif
66}
67