xref: /titanic_50/usr/src/test/libc-tests/tests/random/inz_vpp.c (revision 5c5f137104b2d56181283389fa902220f2023809)
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  */
20 
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <assert.h>
24 #include <sys/mman.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <wait.h>
29 
30 int
31 main(void)
32 {
33 	void *buf;
34 	pid_t child;
35 	int ret, i;
36 	siginfo_t info;
37 	uint8_t *ubuf;
38 	size_t pgsz = sysconf(_SC_PAGESIZE);
39 	size_t mapsz = 10 * pgsz;
40 	size_t clrsz = 5 * pgsz;
41 	size_t clroff = 2 * pgsz;
42 
43 	buf = mmap(NULL, mapsz, PROT_READ | PROT_WRITE,
44 	    MAP_PRIVATE | MAP_ANON, -1, 0);
45 	assert(buf != MAP_FAILED);
46 	memset(buf, 'a', mapsz);
47 
48 	ret = memcntl(buf + clroff, clrsz, MC_INHERIT_ZERO, 0, 0, 0);
49 	assert(ret == 0);
50 
51 	child = fork();
52 	if (child == 0) {
53 		ubuf = buf;
54 		for (i = 0; i < clroff; i++)
55 			assert(ubuf[i] == 'a');
56 		for (i = clroff; i < clrsz + clroff; i++)
57 			assert(ubuf[i] == 0);
58 		for (i = clrsz + clroff; i < mapsz; i++)
59 			assert(ubuf[i] == 'a');
60 		exit(0);
61 	}
62 	assert(child != -1);
63 
64 	do {
65 		ret = waitid(P_PID, child, &info, WEXITED);
66 	} while (ret == -1 && errno == EINTR);
67 	assert(ret == 0);
68 	assert(info.si_pid == child);
69 	assert(info.si_status == 0);
70 
71 	for (i = 0, ubuf = buf; i < mapsz; i++)
72 		assert(ubuf[i] == 'a');
73 
74 	return (0);
75 }
76