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/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/mman.h> 33 34 #include <errno.h> 35 #include <stdint.h> 36 #include <string.h> 37 #include <unistd.h> 38 39 #include <atf-c.h> 40 41 ATF_TC(brk_basic); 42 ATF_TC_HEAD(brk_basic, tc) 43 { 44 atf_tc_set_md_var(tc, "descr", "Verify basic brk() functionality"); 45 } 46 ATF_TC_BODY(brk_basic, tc) 47 { 48 void *oldbrk, *newbrk; 49 int error; 50 51 /* Reset the break. */ 52 error = brk(0); 53 ATF_REQUIRE_MSG(error == 0, "brk: %s", strerror(errno)); 54 55 oldbrk = sbrk(0); 56 ATF_REQUIRE(oldbrk != (void *)-1); 57 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)); 61 62 /* 63 * Attempt to set the break below minbrk. This should have no effect. 64 */ 65 error = brk((void *)((intptr_t)oldbrk - 1)); 66 ATF_REQUIRE_MSG(error == 0, "brk: %s", strerror(errno)); 67 newbrk = sbrk(0); 68 ATF_REQUIRE_MSG(newbrk != (void *)-1, "sbrk: %s", strerror(errno)); 69 ATF_REQUIRE(newbrk == oldbrk); 70 } 71 72 ATF_TC(sbrk_basic); 73 ATF_TC_HEAD(sbrk_basic, tc) 74 { 75 atf_tc_set_md_var(tc, "descr", "Verify basic sbrk() functionality"); 76 } 77 ATF_TC_BODY(sbrk_basic, tc) 78 { 79 void *newbrk, *oldbrk; 80 int *p; 81 82 oldbrk = sbrk(0); 83 ATF_REQUIRE_MSG(oldbrk != (void *)-1, "sbrk: %s", strerror(errno)); 84 p = sbrk(sizeof(*p)); 85 *p = 0; 86 ATF_REQUIRE(oldbrk == p); 87 88 newbrk = sbrk(-sizeof(*p)); 89 ATF_REQUIRE_MSG(newbrk != (void *)-1, "sbrk: %s", strerror(errno)); 90 ATF_REQUIRE(oldbrk == sbrk(0)); 91 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); 98 } 99 100 ATF_TC(mlockfuture); 101 ATF_TC_HEAD(mlockfuture, tc) 102 { 103 atf_tc_set_md_var(tc, "descr", 104 "Verify that mlockall(MCL_FUTURE) applies to the data segment"); 105 } 106 ATF_TC_BODY(mlockfuture, tc) 107 { 108 void *oldbrk, *n, *newbrk; 109 int error; 110 char v; 111 112 error = mlockall(MCL_FUTURE); 113 ATF_REQUIRE_MSG(error == 0, 114 "mlockall: %s", strerror(errno)); 115 116 /* 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 119 * space. 120 */ 121 oldbrk = sbrk(0); 122 ATF_REQUIRE(oldbrk != (void *)-1); 123 newbrk = sbrk(PAGE_SIZE * 2); 124 ATF_REQUIRE(newbrk != (void *)-1); 125 126 n = (void *)(((uintptr_t)oldbrk + PAGE_SIZE) & ~PAGE_SIZE); 127 v = 0; 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); 133 134 error = brk(oldbrk); 135 ATF_REQUIRE(error == 0); 136 137 error = munlockall(); 138 ATF_REQUIRE_MSG(error == 0, 139 "munlockall: %s", strerror(errno)); 140 } 141 142 ATF_TP_ADD_TCS(tp) 143 { 144 ATF_TP_ADD_TC(tp, brk_basic); 145 ATF_TP_ADD_TC(tp, sbrk_basic); 146 ATF_TP_ADD_TC(tp, mlockfuture); 147 148 return (atf_no_error()); 149 } 150