1#!/bin/sh 2 3# 4# SPDX-License-Identifier: BSD-2-Clause 5# 6# Copyright (c) 2019 Dell EMC Isilon 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# Regression test for https://reviews.freebsd.org/D20800 31# "Use a consistent snapshot of the fd's rights in fget_mmap()" 32# https://people.freebsd.org/~pho/stress/log/mmap37.txt 33 34# Reported by syzbot+ae359438769fda1840f8@syzkaller.appspotmail.com 35# Test scenario suggestion by markj@ 36# Fixed by r349547 37 38. ../default.cfg 39 40odir=`pwd` 41cd /tmp 42sed '1,/^EOF/d' < $odir/$0 > mmap37.c 43mycc -o mmap37 -Wall -Wextra -O0 mmap37.c -lpthread || exit 1 44rm -f mmap37.c 45 46set -e 47mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint 48[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart 49mdconfig -a -t swap -s 2g -u $mdstart 50newfs $newfs_flags md$mdstart > /dev/null 51mount /dev/md$mdstart $mntpoint 52set +e 53 54cd $mntpoint 55/tmp/mmap37 56cd $odir 57 58for i in `jot 6`; do 59 mount | grep -q "on $mntpoint " || break 60 umount $mntpoint && break || sleep 10 61 [ $i -eq 6 ] && 62 { echo FATAL; fstat -mf $mntpoint; exit 1; } 63done 64mdconfig -d -u $mdstart 65rm -f /tmp/mmap37 66exit 67 68EOF 69#include <sys/types.h> 70#include <sys/mman.h> 71#include <sys/param.h> 72 73#include <err.h> 74#include <errno.h> 75#include <fcntl.h> 76#include <pthread.h> 77#include <stdio.h> 78#include <stdlib.h> 79#include <string.h> 80#include <unistd.h> 81#include <unistd.h> 82 83#define THREADS 2 84#define SIZ 0x06400000U /* 100 Mb */ 85 86static volatile int go; 87static char path[128]; 88 89void * 90thr(void *arg) 91{ 92 size_t len; 93 void *p; 94 int fd; 95 96 fd = 0; 97 len = SIZ; 98 if (*(int *)arg == 0) { 99 while (go == 1) { 100 if ((fd = open(path, 2)) == -1) 101 err(1,"open()"); 102 p = mmap(NULL, len, PROT_READ | PROT_WRITE, 103 MAP_SHARED, fd, 0); 104 munmap(p, len); 105 close(fd); 106 usleep(10); 107 } 108 } else { 109 while (go == 1) { 110 close(fd); 111 usleep(10); 112 } 113 } 114 115 return (0); 116} 117 118int 119main(void) 120{ 121 pthread_t threads[THREADS]; 122 size_t len; 123 int nr[THREADS]; 124 int i, fd, r; 125 126 sprintf(path, "mmap37.%06d", getpid()); 127 if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) == -1) 128 err(1,"open()"); 129 len = SIZ; 130 if (ftruncate(fd, len) == -1) 131 err(1, "ftruncate"); 132 close(fd); 133 134 go = 1; 135 for (i = 0; i < THREADS; i++) { 136 nr[i] = i; 137 if ((r = pthread_create(&threads[i], NULL, thr, 138 (void *)&nr[i])) != 0) 139 errc(1, r, "pthread_create()"); 140 } 141 142 sleep(30); 143 go = 0; 144 145 for (i = 0; i < THREADS; i++) { 146 if ((r = pthread_join(threads[i], NULL)) != 0) 147 errc(1, r, "pthread_join(%d)", i); 148 } 149 if (unlink(path) == -1) 150 err(1, "unlink(%s)", path); 151 152 return (0); 153} 154