xref: /freebsd/tools/test/stress2/misc/mprotect.sh (revision d37eb51047221dc3322b34db1038ff3aa533883f)
1#!/bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2021 Mark Johnston <markj@freebsd.org>
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# "panic: pmap_demote_pde: page table page for a wired mapping
30# is missing" seen.
31
32# Fixed by r345382
33
34. ../default.cfg
35
36cd /tmp
37cat > mprotect.c <<EOF
38#include <sys/mman.h>
39
40#include <err.h>
41#include <stdlib.h>
42
43int
44main(void)
45{
46	char *addr, c __unused;
47	size_t i, len;
48
49	len = 2 * 1024 * 1024;
50	addr = mmap(NULL, 2 * 1024 * 1024, PROT_READ,
51	    MAP_ANON | MAP_ALIGNED_SUPER, -1, 0);
52	if (addr == MAP_FAILED)
53		err(1, "mmap");
54	if (mlock(addr, len) != 0) /* hopefully this gets a superpage */
55		err(1, "mlock");
56	if (mprotect(addr, len, PROT_NONE) != 0)
57		err(1, "mprotect");
58	if (mprotect(addr, len, PROT_READ) != 0)
59		err(1, "mprotect");
60	for (i = 0; i < len; i++) /* preemptive superpage mapping */
61		c = *(volatile char *)(addr + i);
62	if (mprotect(addr, 4096, PROT_NONE) != 0) /* trigger demotion */
63		err(1, "mprotect");
64	if (munlock(addr, len) != 0)
65		err(1, "munlock");
66
67	return (0);
68}
69EOF
70mycc -o mprotect -Wall -Wextra -O0 mprotect.c || exit 1
71
72./mprotect; s=$?
73
74rm mprotect.c mprotect
75exit $s
76