xref: /freebsd/tools/test/stress2/misc/mmap16.sh (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1#!/bin/sh
2
3#
4# Copyright (c) 2014 EMC Corp.
5# All rights reserved.
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# Test scenario by kib@
30
31[ `uname -m` = "i386" ] || exit 0
32
33. ../default.cfg
34
35grep -q MAP_GUARD /usr/include/sys/mman.h 2>/dev/null || exit 0
36here=`pwd`
37cd /tmp
38sed '1,/^EOF/d' < $here/$0 > mmap16.c
39mycc -o mmap16 -Wall -Wextra -O2 -g mmap16.c -lpthread || exit 1
40rm -f mmap16.c /tmp/mmap16.core
41
42echo "Expect:
43    mmap16: mprotect: Permission denied"
44/tmp/mmap16 > /dev/null
45s=$?
46
47rm -f /tmp/mmap16 /tmp/mmap16.core
48exit $s
49EOF
50/* $Id: map_hole.c,v 1.6 2014/06/16 05:52:03 kostik Exp kostik $ */
51
52#include <sys/types.h>
53#include <sys/mman.h>
54#include <sys/resource.h>
55
56#include <err.h>
57#include <errno.h>
58#include <signal.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <ucontext.h>
63#include <unistd.h>
64
65static void
66sighandler(int signo, siginfo_t *info, void *uap1)
67{
68	static char scratch;
69	ucontext_t *uap;
70
71	uap = uap1;
72	printf("SIG%s(%d) at %p (%%eax %p)\n",
73	    signo < sys_nsig ? sys_signame[signo] : "SOME", signo,
74	    info->si_addr, (void *)(uintptr_t)uap->uc_mcontext.mc_eax);
75	uap->uc_mcontext.mc_eax = (uintptr_t)&scratch;
76}
77
78static void
79access_addr(char *addr)
80{
81	char r;
82
83	r = '1';
84	printf("accessing %p\n", addr);
85	__asm __volatile("movb	%0,(%%eax)" : : "i"(r), "a"(addr) : "memory");
86	printf("done\n");
87}
88
89static int pagesz;
90
91static void
92test_access(char *addr)
93{
94	struct rusage ru;
95	long majflt, minflt;
96
97	if (getrusage(RUSAGE_THREAD, &ru) == -1)
98		err(1, "getrusage");
99	majflt = ru.ru_majflt;
100	minflt = ru.ru_minflt;
101	access_addr(addr);
102	if (mprotect(addr, pagesz, PROT_READ | PROT_WRITE) == -1)
103		warn("mprotect");
104	access_addr(addr);
105	if (getrusage(RUSAGE_THREAD, &ru) == -1)
106		err(1, "getrusage");
107	majflt = ru.ru_majflt - majflt;
108	minflt = ru.ru_minflt - minflt;
109	printf("majflt %ld minflt %ld\n", majflt, minflt);
110}
111
112int
113main(void)
114{
115	struct sigaction sa;
116	char *addr;
117	char cmd[128];
118
119	bzero(&sa, sizeof(sa));
120	sa.sa_sigaction = sighandler;
121	sa.sa_flags = SA_SIGINFO;
122	if (sigaction(SIGSEGV, &sa, NULL) == -1)
123		err(1, "sigaction");
124	pagesz = getpagesize();
125
126	printf("MAP_GUARD\n");
127	addr = mmap(NULL, pagesz, PROT_NONE, MAP_GUARD, -1, 0);
128	if (addr == (char *)MAP_FAILED)
129		err(1, "FAIL: mmap(MAP_GUARD)");
130	test_access(addr);
131
132	printf("PROT_NONE wire\n");
133	addr = mmap(NULL, pagesz, PROT_NONE, MAP_ANON, -1, 0);
134	if (addr == (char *)MAP_FAILED)
135		err(1, "mmap(PROT_NONE)");
136	if (mlock(addr, pagesz) == -1)
137		if (errno != ENOMEM)
138			err(1, "mlock");
139	test_access(addr);
140
141	snprintf(cmd, sizeof(cmd), "procstat -v %d", getpid());
142	system(cmd);
143
144	return (0);
145}
146