2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Mark Johnston <markj@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
42 ATF_TC_HEAD(brk_basic, tc)
44 atf_tc_set_md_var(tc, "descr", "Verify basic brk() functionality");
46 ATF_TC_BODY(brk_basic, tc)
48 void *oldbrk, *newbrk;
51 /* Reset the break. */
53 ATF_REQUIRE_MSG(error == 0, "brk: %s", strerror(errno));
56 ATF_REQUIRE(oldbrk != (void *)-1);
58 /* Try to allocate a page. */
59 error = brk((void *)((intptr_t)oldbrk + PAGE_SIZE * 2));
60 ATF_REQUIRE_MSG(error == 0, "brk: %s", strerror(errno));
63 * Attempt to set the break below minbrk. This should have no effect.
65 error = brk((void *)((intptr_t)oldbrk - 1));
66 ATF_REQUIRE_MSG(error == 0, "brk: %s", strerror(errno));
68 ATF_REQUIRE_MSG(newbrk != (void *)-1, "sbrk: %s", strerror(errno));
69 ATF_REQUIRE(newbrk == oldbrk);
73 ATF_TC_HEAD(sbrk_basic, tc)
75 atf_tc_set_md_var(tc, "descr", "Verify basic sbrk() functionality");
77 ATF_TC_BODY(sbrk_basic, tc)
79 void *newbrk, *oldbrk;
83 ATF_REQUIRE_MSG(oldbrk != (void *)-1, "sbrk: %s", strerror(errno));
86 ATF_REQUIRE(oldbrk == p);
88 newbrk = sbrk(-sizeof(*p));
89 ATF_REQUIRE_MSG(newbrk != (void *)-1, "sbrk: %s", strerror(errno));
90 ATF_REQUIRE(oldbrk == sbrk(0));
92 oldbrk = sbrk(PAGE_SIZE * 2 + 1);
93 ATF_REQUIRE_MSG(oldbrk != (void *)-1, "sbrk: %s", strerror(errno));
94 memset(oldbrk, 0, PAGE_SIZE * 2 + 1);
95 newbrk = sbrk(-(PAGE_SIZE * 2 + 1));
96 ATF_REQUIRE_MSG(newbrk != (void *)-1, "sbrk: %s", strerror(errno));
97 ATF_REQUIRE(sbrk(0) == oldbrk);
101 ATF_TC_HEAD(mlockfuture, tc)
103 atf_tc_set_md_var(tc, "descr",
104 "Verify that mlockall(MCL_FUTURE) applies to the data segment");
106 ATF_TC_BODY(mlockfuture, tc)
108 void *oldbrk, *n, *newbrk;
112 error = mlockall(MCL_FUTURE);
113 ATF_REQUIRE_MSG(error == 0,
114 "mlockall: %s", strerror(errno));
117 * Advance the break so that at least one page is added to the data
118 * segment. This page should be automatically faulted in to the address
122 ATF_REQUIRE(oldbrk != (void *)-1);
123 newbrk = sbrk(PAGE_SIZE * 2);
124 ATF_REQUIRE(newbrk != (void *)-1);
126 n = (void *)(((uintptr_t)oldbrk + PAGE_SIZE) & ~PAGE_SIZE);
128 error = mincore(n, PAGE_SIZE, &v);
129 ATF_REQUIRE_MSG(error == 0,
130 "mincore: %s", strerror(errno));
131 ATF_REQUIRE_MSG((v & MINCORE_INCORE) != 0,
132 "unexpected page flags %#x", v);
135 ATF_REQUIRE(error == 0);
137 error = munlockall();
138 ATF_REQUIRE_MSG(error == 0,
139 "munlockall: %s", strerror(errno));
144 ATF_TP_ADD_TC(tp, brk_basic);
145 ATF_TP_ADD_TC(tp, sbrk_basic);
146 ATF_TP_ADD_TC(tp, mlockfuture);
148 return (atf_no_error());