1#!/bin/sh 2 3# 4# SPDX-License-Identifier: BSD-2-Clause 5# 6# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org> 7# 8# Redistribution and use in source and binary forms, with or without 9# modification, are permitted provided that the following conditions 10# are met: 11# 1. Redistributions of source code must retain the above copyright 12# notice, this list of conditions and the following disclaimer. 13# 2. Redistributions in binary form must reproduce the above copyright 14# notice, this list of conditions and the following disclaimer in the 15# documentation and/or other materials provided with the distribution. 16# 17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27# SUCH DAMAGE. 28# 29 30# Test scenario suggestion by alc@ 31 32. ../default.cfg 33 34cd /tmp 35cat > mprotect2.c <<EOF 36/* 37 For example, write a program that mmap(MAP_ANON)'s a gigabyte of 38 PROT_WRITE virtual address space, iterates over that space removing 39 PROT_WRITE on every other page, and then measure the time to perform a 40 single mprotect() restoring PROT_WRITE to the entire gigabyte of virtual 41 address space. 42 */ 43 44#include <sys/param.h> 45#include <sys/mman.h> 46 47#include <err.h> 48#include <errno.h> 49#include <fcntl.h> 50#include <stdio.h> 51#include <stdlib.h> 52#include <unistd.h> 53 54#define SIZ 0x40000000LL /* 1GB */ 55 56static void 57test(void) 58{ 59 size_t i, len; 60 char *cp; 61 62 len = SIZ; 63 if ((cp = mmap(NULL, len, PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0)) 64 == MAP_FAILED) 65 err(1, "mmap"); 66 67 for (i = 0; i < SIZ; i += PAGE_SIZE * 2) { 68 if (mprotect(cp + i, PAGE_SIZE, 0) != 0) 69 err(1, "mprotect(). %d", __LINE__); 70 } 71 if (mprotect(cp, SIZ, PROT_WRITE) != 0) 72 err(1, "mprotect(). %d", __LINE__); 73 74 if (munmap(cp, SIZ) == -1) 75 err(1, "munmap()"); 76} 77 78int 79main(void) 80{ 81 int i; 82 83 /* Slow run with debug.vmmap_check=1 */ 84 alarm(120); 85 86 for (i = 0; i < 64; i++) 87 test(); 88 89 return (0); 90} 91EOF 92mycc -o mprotect2 -Wall -Wextra -O2 mprotect2.c || exit 1 93 94./mprotect2; s=$? 95 96rm mprotect2.c mprotect2 97exit $s 98