1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2018 Mark Johnston <markj@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/mman.h>
30
31 #include <errno.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #include <atf-c.h>
37
38 ATF_TC(brk_basic);
ATF_TC_HEAD(brk_basic,tc)39 ATF_TC_HEAD(brk_basic, tc)
40 {
41 atf_tc_set_md_var(tc, "descr", "Verify basic brk() functionality");
42 }
ATF_TC_BODY(brk_basic,tc)43 ATF_TC_BODY(brk_basic, tc)
44 {
45 void *oldbrk, *newbrk;
46 int error;
47
48 /* Reset the break. */
49 error = brk(0);
50 ATF_REQUIRE_MSG(error == 0, "brk: %s", strerror(errno));
51
52 oldbrk = sbrk(0);
53 ATF_REQUIRE(oldbrk != (void *)-1);
54
55 /* Try to allocate a page. */
56 error = brk((void *)((intptr_t)oldbrk + PAGE_SIZE * 2));
57 ATF_REQUIRE_MSG(error == 0, "brk: %s", strerror(errno));
58
59 /*
60 * Attempt to set the break below minbrk. This should have no effect.
61 */
62 error = brk((void *)((intptr_t)oldbrk - 1));
63 ATF_REQUIRE_MSG(error == 0, "brk: %s", strerror(errno));
64 newbrk = sbrk(0);
65 ATF_REQUIRE_MSG(newbrk != (void *)-1, "sbrk: %s", strerror(errno));
66 ATF_REQUIRE(newbrk == oldbrk);
67 }
68
69 ATF_TC(sbrk_basic);
ATF_TC_HEAD(sbrk_basic,tc)70 ATF_TC_HEAD(sbrk_basic, tc)
71 {
72 atf_tc_set_md_var(tc, "descr", "Verify basic sbrk() functionality");
73 }
ATF_TC_BODY(sbrk_basic,tc)74 ATF_TC_BODY(sbrk_basic, tc)
75 {
76 void *newbrk, *oldbrk;
77 int *p;
78
79 oldbrk = sbrk(0);
80 ATF_REQUIRE_MSG(oldbrk != (void *)-1, "sbrk: %s", strerror(errno));
81 p = sbrk(sizeof(*p));
82 *p = 0;
83 ATF_REQUIRE(oldbrk == p);
84
85 newbrk = sbrk(-sizeof(*p));
86 ATF_REQUIRE_MSG(newbrk != (void *)-1, "sbrk: %s", strerror(errno));
87 ATF_REQUIRE(oldbrk == sbrk(0));
88
89 oldbrk = sbrk(PAGE_SIZE * 2 + 1);
90 ATF_REQUIRE_MSG(oldbrk != (void *)-1, "sbrk: %s", strerror(errno));
91 memset(oldbrk, 0, PAGE_SIZE * 2 + 1);
92 newbrk = sbrk(-(PAGE_SIZE * 2 + 1));
93 ATF_REQUIRE_MSG(newbrk != (void *)-1, "sbrk: %s", strerror(errno));
94 ATF_REQUIRE(sbrk(0) == oldbrk);
95 }
96
97 ATF_TC(mlockfuture);
ATF_TC_HEAD(mlockfuture,tc)98 ATF_TC_HEAD(mlockfuture, tc)
99 {
100 atf_tc_set_md_var(tc, "descr",
101 "Verify that mlockall(MCL_FUTURE) applies to the data segment");
102 }
ATF_TC_BODY(mlockfuture,tc)103 ATF_TC_BODY(mlockfuture, tc)
104 {
105 void *oldbrk, *n, *newbrk;
106 int error;
107 char v;
108
109 error = mlockall(MCL_FUTURE);
110 ATF_REQUIRE_MSG(error == 0,
111 "mlockall: %s", strerror(errno));
112
113 /*
114 * Advance the break so that at least one page is added to the data
115 * segment. This page should be automatically faulted in to the address
116 * space.
117 */
118 oldbrk = sbrk(0);
119 ATF_REQUIRE(oldbrk != (void *)-1);
120 newbrk = sbrk(PAGE_SIZE * 2);
121 ATF_REQUIRE(newbrk != (void *)-1);
122
123 n = (void *)(((uintptr_t)oldbrk + PAGE_SIZE) & ~PAGE_SIZE);
124 v = 0;
125 error = mincore(n, PAGE_SIZE, &v);
126 ATF_REQUIRE_MSG(error == 0,
127 "mincore: %s", strerror(errno));
128 ATF_REQUIRE_MSG((v & MINCORE_INCORE) != 0,
129 "unexpected page flags %#x", v);
130
131 error = brk(oldbrk);
132 ATF_REQUIRE(error == 0);
133
134 error = munlockall();
135 ATF_REQUIRE_MSG(error == 0,
136 "munlockall: %s", strerror(errno));
137 }
138
ATF_TP_ADD_TCS(tp)139 ATF_TP_ADD_TCS(tp)
140 {
141 ATF_TP_ADD_TC(tp, brk_basic);
142 ATF_TP_ADD_TC(tp, sbrk_basic);
143 ATF_TP_ADD_TC(tp, mlockfuture);
144
145 return (atf_no_error());
146 }
147