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