2 * Copyright (c) 2009 Simon L. Nielsen <simon@FreeBSD.org>,
3 * Bjoern A. Zeeb <bz@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
31 #include <sys/sysctl.h>
32 #include <sys/types.h>
40 int ok[2]; /* Depending on security.bsd.map_at_zero {0, !=0}. */
42 { (void *)0, { 0, 1 } }, /* Test sysctl. */
43 { (void *)1, { 0, 0 } },
44 { (void *)(PAGE_SIZE - 1), { 0, 0 } },
45 { (void *)PAGE_SIZE, { 1, 1 } },
46 { (void *)-1, { 0, 0 } },
47 { (void *)(-PAGE_SIZE), { 0, 0 } },
48 { (void *)(-1 - PAGE_SIZE), { 0, 0 } },
49 { (void *)(-1 - PAGE_SIZE - 1), { 0, 0 } },
50 { (void *)(0x1000 * PAGE_SIZE), { 1, 1 } },
53 #define MAP_AT_ZERO "security.bsd.map_at_zero"
60 int i, error, mib[3], map_at_zero;
64 /* Get the current sysctl value of security.bsd.map_at_zero. */
65 len = sizeof(mib) / sizeof(*mib);
66 if (sysctlnametomib(MAP_AT_ZERO, mib, &len) == -1) {
67 printf("1..0 # SKIP: sysctlnametomib(\"%s\") failed: %s\n",
68 MAP_AT_ZERO, strerror(errno));
72 len = sizeof(map_at_zero);
73 if (sysctl(mib, 3, &map_at_zero, &len, NULL, 0) == -1) {
74 printf("1..0 # SKIP: sysctl for %s failed: %s\n", MAP_AT_ZERO,
79 /* Normalize to 0 or 1 for array access. */
80 map_at_zero = !!map_at_zero;
82 printf("1..%zu\n", nitems(tests));
83 for (i = 0; i < (int)nitems(tests); i++) {
84 p = mmap((void *)tests[i].addr, PAGE_SIZE,
85 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED,
87 if (p == MAP_FAILED) {
88 if (tests[i].ok[map_at_zero] != 0)
90 printf("%sok %d # mmap(%p, ...) failed\n",
91 tests[i].ok[map_at_zero] == 0 ? "" : "not ",
95 if (tests[i].ok[map_at_zero] != 1)
97 printf("%sok %d # mmap(%p, ...) succeeded: p=%p\n",
98 tests[i].ok[map_at_zero] == 1 ? "" : "not ",