2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "aslr_test.h"
19 /* run tests if on supported arch */
20 #if defined(__x86__64__) || defined(__i386__) || defined(__aarch64__) || defined(__arm__)
22 /* make sure the default entropy values matches what we expect */
23 TEST_F(AslrMmapTest, match_default) {
25 // running 32-bit userspace on 64-bit kernel, only compat used.
28 EXPECT_EQ(def, get_mmap_rnd_bits(false));
32 /* make sure the default compat entropy values matches what we expect */
33 TEST_F(AslrMmapTest, match_compat_default) {
35 EXPECT_EQ(def_cmpt, get_mmap_rnd_bits(true));
38 /* make sure we can't set entropy below a minimum threshold */
39 TEST_F(AslrMmapTest, match_min) {
41 // running 32-bit userspace on 64-bit kernel, only compat used.
44 EXPECT_FALSE(set_mmap_rnd_bits(min - 1, false));
45 EXPECT_TRUE(set_mmap_rnd_bits(min, false));
46 EXPECT_EQ(min, get_mmap_rnd_bits(false));
50 /* make sure we can't set compat entropy below a minimum threshold */
51 TEST_F(AslrMmapTest, match_compat_min) {
52 if (compat || user32) {
53 EXPECT_FALSE(set_mmap_rnd_bits(min_cmpt - 1, true));
54 EXPECT_TRUE(set_mmap_rnd_bits(min_cmpt, true));
55 EXPECT_EQ(min_cmpt, get_mmap_rnd_bits(true));
59 /* make sure we can't set entropy above a maximum threshold */
60 TEST_F(AslrMmapTest, match_max) {
62 // running 32-bit userspace on 64-bit kernel, only compat used.
65 EXPECT_FALSE(set_mmap_rnd_bits(max + 1, false));
66 EXPECT_TRUE(set_mmap_rnd_bits(max, false));
67 EXPECT_EQ(max, get_mmap_rnd_bits(false));
71 /* make sure we can't set compat entropy above a maximum threshold */
72 TEST_F(AslrMmapTest, match_compat_max) {
73 if (compat || user32) {
74 EXPECT_FALSE(set_mmap_rnd_bits(max_cmpt + 1, true));
75 EXPECT_TRUE(set_mmap_rnd_bits(max_cmpt, true));
76 EXPECT_EQ(max_cmpt, get_mmap_rnd_bits(true));
80 /* make sure observed entropy is what we expect when we set min value */
81 TEST_F(AslrMmapTest, entropy_min) {
83 // running 32-bit userspace on 64-bit kernel, only compat used.
86 EXPECT_TRUE(set_mmap_rnd_bits(min, false));
87 EXPECT_EQ(min, calc_mmap_entropy(path, lib, 16));
91 /* make sure observed compat entropy is what we expect when we set min value */
92 TEST_F(AslrMmapTest, entropy_cmpt_min) {
93 if (compat || user32) {
94 EXPECT_TRUE(set_mmap_rnd_bits(min_cmpt, true));
95 EXPECT_EQ(min_cmpt, calc_mmap_entropy(SCRAPE_PATH_32, SCRAPE_LIB_32, 16));
99 /* make sure observed entropy is what we expect when we set max value */
100 TEST_F(AslrMmapTest, entropy_max) {
102 // running 32-bit userspace on 64-bit kernel, only compat used.
105 EXPECT_TRUE(set_mmap_rnd_bits(max, false));
106 EXPECT_EQ(max, calc_mmap_entropy(path, lib, 16));
110 /* make sure observed compat entropy is what we expect when we set max value */
111 TEST_F(AslrMmapTest, entropy_cmpt_max) {
112 if (compat || user32) {
113 EXPECT_TRUE(set_mmap_rnd_bits(max_cmpt, true));
114 EXPECT_EQ(max_cmpt, calc_mmap_entropy(SCRAPE_PATH_32, SCRAPE_LIB_32, 16));
118 /* make sure observed entropy is what we expect for default value */
119 TEST_F(AslrMmapTest, entropy_def) {
121 // running 32-bit userspace on 64-bit kernel, only compat used.
124 EXPECT_EQ(def, calc_mmap_entropy(path, lib, 16));
128 /* make sure observed entropy is what we expect for default compat value */
129 TEST_F(AslrMmapTest, entropy_cmpt_def) {
130 if (compat || user32) {
131 EXPECT_EQ(def_cmpt, calc_mmap_entropy(SCRAPE_PATH_32, SCRAPE_LIB_32, 16));
135 #endif /* supported arch */