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 <gtest/gtest.h>
18
19#include <sys/mman.h>
20#include <sys/prctl.h>
21#include <unistd.h>
22#include "private/bionic_prctl.h"
23
24// http://b/20017123.
25TEST(sys_prctl, bug_20017123) {
26#if defined(__ANDROID__)
27  size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
28  void* p = mmap(NULL, page_size * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
29  ASSERT_NE(MAP_FAILED, p);
30  ASSERT_EQ(0, mprotect(p, page_size, PROT_NONE));
31  ASSERT_NE(-1, prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, page_size * 3, "anonymous map space"));
32  volatile char* vp = reinterpret_cast<volatile char*>(p);
33  // Below memory access causes SEGV if the memory map is screwed up.
34  *(vp + page_size) = 0;
35  ASSERT_EQ(0, munmap(p, page_size * 3));
36#else
37  GTEST_LOG_(INFO) << "This test does nothing as it tests an Android specific kernel feature.";
38#endif
39}
40