xref: /freebsd/tools/test/stress2/misc/msetdomain.sh (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1#!/bin/sh
2
3#
4# Copyright (c) 2018 Dell EMC Isilon
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# msetdomain(2) fuzz test.
30# No problems seen.
31
32. ../default.cfg
33[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
34
35nm /usr/lib/libc.a | grep -q __sys_msetdomain || exit 0
36dir=/tmp
37odir=`pwd`
38cd $dir
39sed '1,/^EOF/d' < $odir/$0 > $dir/msetdomain.c
40mycc -o msetdomain -Wall -Wextra -O0 -g msetdomain.c || exit 1
41rm -f msetdomain.c
42cd $odir
43
44$dir/msetdomain
45s=$?
46[ -f msetdomain.core -a $s -eq 0 ] &&
47    { ls -l msetdomain.core; mv msetdomain.core /tmp; s=1; }
48rm -rf $dir/msetdomain
49exit $s
50
51EOF
52#include <sys/param.h>
53#include <sys/mman.h>
54#include <sys/resource.h>
55#include <sys/cpuset.h>
56#include <sys/domainset.h>
57
58#include <err.h>
59#include <stdlib.h>
60#include <time.h>
61
62/*
63struct msetdomain_args {
64        void            *addr;
65        size_t          size;
66        size_t          domainsetsize;
67        domainset_t     *mask;
68        int             policy;
69        int             flags;
70*/
71
72static long
73random_long(long mi, long ma)
74{
75        return (arc4random()  % (ma - mi + 1) + mi);
76}
77
78void
79flip(void *ap, size_t len)
80{
81	unsigned char *cp;
82	int byte;
83	unsigned char bit, buf, mask, old;
84
85	cp = (unsigned char *)ap;
86	byte = random_long(0, len);
87	bit = random_long(0,7);
88	mask = ~(1 << bit);
89	buf = cp[byte];
90	old = cp[byte];
91	buf = (buf & mask) | (~buf & ~mask);
92	cp[byte] = buf;
93}
94
95int
96main(void)
97{
98	size_t len;
99	time_t start;
100	void *share;
101	domainset_t rootmask;
102	int flags, policy;
103
104	len = PAGE_SIZE;
105	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
106	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
107		err(1, "mmap");
108
109	start = time(NULL);
110	while (time(NULL) - start < 60) {
111		if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
112		    sizeof(rootmask), &rootmask, &policy) != 0)
113			err(EXIT_FAILURE, "getdomain");
114
115		flags = 0;
116		flip(&flags, sizeof(flags));
117		msetdomain(share, len, sizeof(rootmask), &rootmask, policy,
118		    flags);
119	}
120
121	start = time(NULL);
122	while (time(NULL) - start < 60) {
123		if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
124		    sizeof(rootmask), &rootmask, &policy) != 0)
125			err(EXIT_FAILURE, "getdomain");
126
127		flip(&policy, sizeof(policy));
128		msetdomain(share, len, sizeof(rootmask), &rootmask, policy,
129		    flags);
130	}
131
132	start = time(NULL);
133	while (time(NULL) - start < 60) {
134		if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
135		    sizeof(rootmask), &rootmask, &policy) != 0)
136			err(EXIT_FAILURE, "getdomain");
137
138		flip(&rootmask, sizeof(rootmask));
139		msetdomain(share, len, sizeof(rootmask), &rootmask, policy,
140		    flags);
141	}
142
143	return (0);
144}
145