t_minherit.c revision 1.1 1 1.1 christos /* $NetBSD: t_minherit.c,v 1.1 2014/07/18 12:34:52 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos #include <sys/cdefs.h>
32 1.1 christos __RCSID("$NetBSD: t_minherit.c,v 1.1 2014/07/18 12:34:52 christos Exp $");
33 1.1 christos
34 1.1 christos #include <sys/param.h>
35 1.1 christos #include <sys/mman.h>
36 1.1 christos #include <sys/sysctl.h>
37 1.1 christos #include <sys/wait.h>
38 1.1 christos
39 1.1 christos #include <errno.h>
40 1.1 christos #include <fcntl.h>
41 1.1 christos #include <stdlib.h>
42 1.1 christos #include <string.h>
43 1.1 christos #include <unistd.h>
44 1.1 christos
45 1.1 christos #include <atf-c.h>
46 1.1 christos
47 1.1 christos static long page;
48 1.1 christos
49 1.1 christos static void *
50 1.1 christos makemap(int v, int f) {
51 1.1 christos void *map = mmap(NULL, page, PROT_READ|PROT_WRITE,
52 1.1 christos MAP_SHARED|MAP_ANON, -1, 0);
53 1.1 christos ATF_REQUIRE(map != MAP_FAILED);
54 1.1 christos memset(map, v, page);
55 1.1 christos if (f != 666)
56 1.1 christos ATF_REQUIRE(minherit(map, page, f) == 0);
57 1.1 christos else
58 1.1 christos ATF_REQUIRE(minherit(map, page, f) == -1);
59 1.1 christos return map;
60 1.1 christos }
61 1.1 christos
62 1.1 christos ATF_TC(minherit_copy);
63 1.1 christos ATF_TC_HEAD(minherit_copy, tc)
64 1.1 christos {
65 1.1 christos atf_tc_set_md_var(tc, "descr",
66 1.1 christos "Test for MAP_INHERIT_COPY from minherit(2)");
67 1.1 christos }
68 1.1 christos
69 1.1 christos ATF_TC_BODY(minherit_copy, tc)
70 1.1 christos {
71 1.1 christos void *map1 = makemap(1, MAP_INHERIT_COPY);
72 1.1 christos void *map2 = makemap(1, MAP_INHERIT_COPY);
73 1.1 christos switch (fork()) {
74 1.1 christos default:
75 1.1 christos ATF_REQUIRE(wait(NULL) != -1);
76 1.1 christos ATF_REQUIRE(memcmp(map1, map2, page) == 0);
77 1.1 christos break;
78 1.1 christos case -1:
79 1.1 christos ATF_REQUIRE(0);
80 1.1 christos break;
81 1.1 christos case 0:
82 1.1 christos ATF_REQUIRE(memcmp(map1, map2, page) == 0);
83 1.1 christos memset(map1, 0, page);
84 1.1 christos exit(0);
85 1.1 christos }
86 1.1 christos }
87 1.1 christos
88 1.1 christos ATF_TC(minherit_share);
89 1.1 christos ATF_TC_HEAD(minherit_share, tc)
90 1.1 christos {
91 1.1 christos atf_tc_set_md_var(tc, "descr",
92 1.1 christos "Test for MAP_INHERIT_SHARE from minherit(2)");
93 1.1 christos }
94 1.1 christos
95 1.1 christos ATF_TC_BODY(minherit_share, tc)
96 1.1 christos {
97 1.1 christos void *map1 = makemap(1, MAP_INHERIT_SHARE);
98 1.1 christos void *map2 = makemap(1, MAP_INHERIT_SHARE);
99 1.1 christos
100 1.1 christos switch (fork()) {
101 1.1 christos default:
102 1.1 christos ATF_REQUIRE(wait(NULL) != -1);
103 1.1 christos memset(map2, 0, page);
104 1.1 christos ATF_REQUIRE(memcmp(map1, map2, page) == 0);
105 1.1 christos break;
106 1.1 christos case -1:
107 1.1 christos ATF_REQUIRE(0);
108 1.1 christos break;
109 1.1 christos case 0:
110 1.1 christos ATF_REQUIRE(memcmp(map1, map2, page) == 0);
111 1.1 christos memset(map1, 0, page);
112 1.1 christos exit(0);
113 1.1 christos }
114 1.1 christos }
115 1.1 christos
116 1.1 christos static void
117 1.1 christos segv(int n) {
118 1.1 christos _exit(n);
119 1.1 christos }
120 1.1 christos
121 1.1 christos ATF_TC(minherit_none);
122 1.1 christos ATF_TC_HEAD(minherit_none, tc)
123 1.1 christos {
124 1.1 christos atf_tc_set_md_var(tc, "descr",
125 1.1 christos "Test for MAP_INHERIT_NONE from minherit(2)");
126 1.1 christos }
127 1.1 christos
128 1.1 christos ATF_TC_BODY(minherit_none, tc)
129 1.1 christos {
130 1.1 christos void *map1 = makemap(0, MAP_INHERIT_NONE);
131 1.1 christos int status;
132 1.1 christos
133 1.1 christos switch (fork()) {
134 1.1 christos default:
135 1.1 christos ATF_REQUIRE(wait(&status) != -1);
136 1.1 christos ATF_REQUIRE(WEXITSTATUS(status) == SIGSEGV);
137 1.1 christos break;
138 1.1 christos case -1:
139 1.1 christos ATF_REQUIRE(0);
140 1.1 christos break;
141 1.1 christos case 0:
142 1.1 christos ATF_REQUIRE(signal(SIGSEGV, segv) != SIG_ERR);
143 1.1 christos memset(map1, 0, page);
144 1.1 christos exit(0);
145 1.1 christos }
146 1.1 christos }
147 1.1 christos
148 1.1 christos ATF_TC(minherit_zero);
149 1.1 christos ATF_TC_HEAD(minherit_zero, tc)
150 1.1 christos {
151 1.1 christos atf_tc_set_md_var(tc, "descr",
152 1.1 christos "Test for MAP_INHERIT_ZERO from minherit(2)");
153 1.1 christos }
154 1.1 christos
155 1.1 christos ATF_TC_BODY(minherit_zero, tc)
156 1.1 christos {
157 1.1 christos void *map1 = makemap(1, MAP_INHERIT_ZERO);
158 1.1 christos void *map2 = makemap(0, MAP_INHERIT_SHARE);
159 1.1 christos
160 1.1 christos switch (fork()) {
161 1.1 christos default:
162 1.1 christos ATF_REQUIRE(wait(NULL) != -1);
163 1.1 christos memset(map2, 1, page);
164 1.1 christos ATF_REQUIRE(memcmp(map1, map2, page) == 0);
165 1.1 christos break;
166 1.1 christos case -1:
167 1.1 christos ATF_REQUIRE(0);
168 1.1 christos break;
169 1.1 christos case 0:
170 1.1 christos ATF_REQUIRE(memcmp(map1, map2, page) == 0);
171 1.1 christos memset(map1, 2, page);
172 1.1 christos exit(0);
173 1.1 christos }
174 1.1 christos }
175 1.1 christos
176 1.1 christos ATF_TC(minherit_bad);
177 1.1 christos ATF_TC_HEAD(minherit_bad, tc)
178 1.1 christos {
179 1.1 christos atf_tc_set_md_var(tc, "descr",
180 1.1 christos "Test for bad minherit(2)");
181 1.1 christos }
182 1.1 christos
183 1.1 christos ATF_TC_BODY(minherit_bad, tc)
184 1.1 christos {
185 1.1 christos (void)makemap(0, 666);
186 1.1 christos }
187 1.1 christos
188 1.1 christos ATF_TP_ADD_TCS(tp)
189 1.1 christos {
190 1.1 christos page = sysconf(_SC_PAGESIZE);
191 1.1 christos ATF_REQUIRE(page >= 0);
192 1.1 christos
193 1.1 christos ATF_TP_ADD_TC(tp, minherit_copy);
194 1.1 christos ATF_TP_ADD_TC(tp, minherit_share);
195 1.1 christos ATF_TP_ADD_TC(tp, minherit_none);
196 1.1 christos ATF_TP_ADD_TC(tp, minherit_zero);
197 1.1 christos ATF_TP_ADD_TC(tp, minherit_bad);
198 1.1 christos
199 1.1 christos return atf_no_error();
200 1.1 christos }
201