xref: /illumos-gate/usr/src/test/libc-tests/tests/random/inz_split_vpp.c (revision 48edc7cf07b5dccc3ad84bf2dafe4150bd666d60)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2015, Joyent, Inc.
14  */
15 
16 /*
17  * Verify that using MC_INHERIT_ZERO works just fine when applied to a subset of
18  * a region, meaning that we should have created a struct vpage for that region.
19  * Then unmap a hole in the middle, so that way we force subsets of children
20  * that have struct vpage entries.
21  */
22 
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <assert.h>
26 #include <sys/mman.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <wait.h>
31 
32 int
33 main(void)
34 {
35 	void *buf;
36 	pid_t child;
37 	int ret, i;
38 	siginfo_t info;
39 	uint8_t *ubuf;
40 	size_t pgsz = sysconf(_SC_PAGESIZE);
41 	size_t mapsz = 10 * pgsz;
42 	size_t clrsz = 5 * pgsz;
43 	size_t clroff = 2 * pgsz;
44 	size_t spltsz = 1 * pgsz;
45 	size_t spltoff = 3 * pgsz;
46 
47 	buf = mmap(NULL, mapsz, PROT_READ | PROT_WRITE,
48 	    MAP_PRIVATE | MAP_ANON, -1, 0);
49 	assert(buf != MAP_FAILED);
50 	memset(buf, 'a', mapsz);
51 
52 	ret = memcntl(buf + clroff, clrsz, MC_INHERIT_ZERO, 0, 0, 0);
53 	assert(ret == 0);
54 
55 	ret = munmap(buf + spltoff, spltsz);
56 	assert(ret == 0);
57 
58 	child = fork();
59 	if (child == 0) {
60 		ubuf = buf;
61 		for (i = 0; i < clroff; i++)
62 			assert(ubuf[i] == 'a');
63 		for (i = clroff; i < spltoff; i++)
64 			assert(ubuf[i] == 0);
65 		for (i = spltoff + spltsz; i < clroff + clrsz; i++)
66 			assert(ubuf[i] == 0);
67 		for (i = clrsz + clroff; i < mapsz; i++)
68 			assert(ubuf[i] == 'a');
69 		exit(0);
70 	}
71 	assert(child != -1);
72 
73 	do {
74 		ret = waitid(P_PID, child, &info, WEXITED);
75 	} while (ret == -1 && errno == EINTR);
76 	assert(ret == 0);
77 	assert(info.si_pid == child);
78 	assert(info.si_status == 0);
79 
80 	ubuf = buf;
81 	for (i = 0; i < clroff; i++)
82 		assert(ubuf[i] == 'a');
83 	for (i = clroff; i < spltoff; i++)
84 		assert(ubuf[i] == 'a');
85 	for (i = spltoff + spltsz; i < clroff + clrsz; i++)
86 		assert(ubuf[i] == 'a');
87 	for (i = clrsz + clroff; i < mapsz; i++)
88 		assert(ubuf[i] == 'a');
89 
90 
91 	return (0);
92 }
93