Index: stable/10/contrib/netbsd-tests/lib/libc/gen/t_humanize_number.c =================================================================== --- stable/10/contrib/netbsd-tests/lib/libc/gen/t_humanize_number.c (revision 313520) +++ stable/10/contrib/netbsd-tests/lib/libc/gen/t_humanize_number.c (revision 313521) @@ -1,318 +1,321 @@ /* $NetBSD: t_humanize_number.c,v 1.8 2012/03/18 07:14:08 jruoho Exp $ */ /*- * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #ifdef __FreeBSD__ #include #else #include #endif const struct hnopts { size_t ho_len; int64_t ho_num; const char *ho_suffix; int ho_scale; int ho_flags; int ho_retval; /* expected return value */ const char *ho_retstr; /* expected string in buffer */ } hnopts[] = { /* * Rev. 1.6 produces "10.0". */ { 5, 10737418236ULL * 1024, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 3, "10T" }, { 5, 10450000, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 3, "10M" }, { 5, 10500000, "", /* just for reference */ HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 3, "10M" }, /* * Trailing space. Rev. 1.7 produces "1 ". */ { 5, 1, "", 0, HN_NOSPACE, 1, "1" }, { 5, 1, "", 0, 0, 2, "1 " }, /* just for reference */ { 5, 1, "", 0, HN_B, 3, "1 B" }, /* and more ... */ { 5, 1, "", 0, HN_DECIMAL, 2, "1 " }, { 5, 1, "", 0, HN_NOSPACE | HN_B, 2, "1B" }, { 5, 1, "", 0, HN_B | HN_DECIMAL, 3, "1 B" }, { 5, 1, "", 0, HN_NOSPACE | HN_B | HN_DECIMAL, 2, "1B" }, /* * Space and HN_B. Rev. 1.7 produces "1B". */ { 5, 1, "", HN_AUTOSCALE, HN_B, 3, "1 B" }, { 5, 1000, "", /* just for reference */ HN_AUTOSCALE, HN_B, 3, "1 K" }, /* * Truncated output. Rev. 1.7 produces "1.0 K". */ #ifndef __FreeBSD__ { 6, 1000, "A", HN_AUTOSCALE, HN_DECIMAL, -1, "" }, /* * Failure case reported by Greg Troxel . * Rev. 1.11 incorrectly returns 5 with filling the buffer * with "1000". */ { 5, 1048258238, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 4, "1.0G" }, /* Similar case it prints 1000 where it shouldn't */ { 5, 1023488, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 4, "1.0M" }, #endif { 5, 1023999, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL, 4, "1.0M" }, }; struct hnflags { int hf_flags; const char *hf_name; }; const struct hnflags scale_flags[] = { { HN_GETSCALE, "HN_GETSCALE" }, { HN_AUTOSCALE, "HN_AUTOSCALE" }, }; const struct hnflags normal_flags[] = { { HN_DECIMAL, "HN_DECIMAL" }, { HN_NOSPACE, "HN_NOSPACE" }, { HN_B, "HN_B" }, { HN_DIVISOR_1000, "HN_DIVISOR_1000" }, }; const char *formatflags(char *, size_t, const struct hnflags *, size_t, int); void newline(void); void w_printf(const char *, ...) __printflike(1, 2); int main(int, char *[]); const char * formatflags(char *buf, size_t buflen, const struct hnflags *hfs, size_t hfslen, int flags) { const struct hnflags *hf; char *p = buf; ssize_t len = buflen; unsigned int i, found; int n; if (flags == 0) { snprintf(buf, buflen, "0"); return (buf); } for (i = found = 0; i < hfslen && flags & ~found; i++) { hf = &hfs[i]; if (flags & hf->hf_flags) { found |= hf->hf_flags; n = snprintf(p, len, "|%s", hf->hf_name); if (n >= len) { p = buf; len = buflen; /* Print `flags' as number */ goto bad; } p += n; len -= n; } } flags &= ~found; if (flags) bad: snprintf(p, len, "|0x%x", flags); return (*buf == '|' ? buf + 1 : buf); } static int col, bol = 1; void newline(void) { fprintf(stderr, "\n"); col = 0; bol = 1; } void w_printf(const char *fmt, ...) { char buf[80]; va_list ap; int n; va_start(ap, fmt); if (col >= 0) { n = vsnprintf(buf, sizeof(buf), fmt, ap); if (n >= (int)sizeof(buf)) { col = -1; goto overflow; } else if (n == 0) goto out; if (!bol) { if (col + n > 75) fprintf(stderr, "\n "), col = 4; else fprintf(stderr, " "), col++; } fprintf(stderr, "%s", buf); col += n; bol = 0; } else { overflow: vfprintf(stderr, fmt, ap); } out: va_end(ap); } ATF_TC(humanize_number_basic); ATF_TC_HEAD(humanize_number_basic, tc) { atf_tc_set_md_var(tc, "descr", "Test humanize_number(3)"); } ATF_TC_BODY(humanize_number_basic, tc) { char fbuf[128]; const struct hnopts *ho; char *buf = NULL; size_t buflen = 0; unsigned int i; int rv = 0; for (i = 0; i < __arraycount(hnopts); i++) { ho = &hnopts[i]; if (buflen < ho->ho_len) { buflen = ho->ho_len; buf = realloc(buf, buflen); if (buf == NULL) atf_tc_fail("realloc(..., %zu) failed", buflen); } rv = humanize_number(buf, ho->ho_len, ho->ho_num, ho->ho_suffix, ho->ho_scale, ho->ho_flags); if (rv == ho->ho_retval && (rv == -1 || strcmp(buf, ho->ho_retstr) == 0)) continue; w_printf("humanize_number(\"%s\", %zu, %" PRId64 ",", ho->ho_retstr, ho->ho_len, ho->ho_num); w_printf("\"%s\",", ho->ho_suffix); w_printf("%s,", formatflags(fbuf, sizeof(fbuf), scale_flags, sizeof(scale_flags) / sizeof(scale_flags[0]), ho->ho_scale)); w_printf("%s)", formatflags(fbuf, sizeof(fbuf), normal_flags, sizeof(normal_flags) / sizeof(normal_flags[0]), ho->ho_flags)); w_printf("= %d,", ho->ho_retval); w_printf("but got"); w_printf("%d/[%s]", rv, rv == -1 ? "" : buf); newline(); atf_tc_fail_nonfatal("Failed for table entry %d", i); } +#ifdef __FreeBSD__ + free(buf); +#endif } ATF_TC(humanize_number_big); ATF_TC_HEAD(humanize_number_big, tc) { atf_tc_set_md_var(tc, "descr", "Test humanize " "big numbers (PR lib/44097)"); } ATF_TC_BODY(humanize_number_big, tc) { char buf[1024]; int rv; /* * Seems to work. */ (void)memset(buf, 0, sizeof(buf)); rv = humanize_number(buf, 10, 10000, "", HN_AUTOSCALE, HN_NOSPACE); ATF_REQUIRE(rv != -1); ATF_CHECK_STREQ(buf, "10000"); /* * A bogus value with large number. */ (void)memset(buf, 0, sizeof(buf)); rv = humanize_number(buf, 10, INT64_MAX, "", HN_AUTOSCALE, HN_NOSPACE); ATF_REQUIRE(rv != -1); ATF_REQUIRE(strcmp(buf, "0") != 0); /* * Large buffer with HN_AUTOSCALE. Entirely bogus. */ (void)memset(buf, 0, sizeof(buf)); rv = humanize_number(buf, sizeof(buf), 10000, "", HN_AUTOSCALE, HN_NOSPACE); ATF_REQUIRE(rv != -1); ATF_REQUIRE(strcmp(buf, "0%d%s%d%s%s%s") != 0); /* * Tight buffer. * * The man page says that len must be at least 4. * 3 works, but anything less that will not. This * is because baselen starts with 2 for positive * numbers. */ (void)memset(buf, 0, sizeof(buf)); rv = humanize_number(buf, 3, 1, "", HN_AUTOSCALE, HN_NOSPACE); ATF_REQUIRE(rv != -1); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, humanize_number_basic); ATF_TP_ADD_TC(tp, humanize_number_big); return atf_no_error(); } Index: stable/10/contrib/netbsd-tests/lib/libc/sys/t_mmap.c =================================================================== --- stable/10/contrib/netbsd-tests/lib/libc/sys/t_mmap.c (revision 313520) +++ stable/10/contrib/netbsd-tests/lib/libc/sys/t_mmap.c (revision 313521) @@ -1,591 +1,605 @@ /* $NetBSD: t_mmap.c,v 1.9 2015/02/28 13:57:08 martin Exp $ */ /*- * Copyright (c) 2011 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Jukka Ruohonen. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /*- * Copyright (c)2004 YAMAMOTO Takashi, * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __RCSID("$NetBSD: t_mmap.c,v 1.9 2015/02/28 13:57:08 martin Exp $"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __NetBSD__ #include #endif #ifdef __FreeBSD__ #include #include #include #endif static long page = 0; static char path[] = "mmap"; static void map_check(void *, int); static void map_sighandler(int); static void testloan(void *, void *, char, int); #define BUFSIZE (32 * 1024) /* enough size to trigger sosend_loan */ static void map_check(void *map, int flag) { if (flag != 0) { ATF_REQUIRE(map == MAP_FAILED); return; } ATF_REQUIRE(map != MAP_FAILED); ATF_REQUIRE(munmap(map, page) == 0); } void testloan(void *vp, void *vp2, char pat, int docheck) { char buf[BUFSIZE]; char backup[BUFSIZE]; ssize_t nwritten; ssize_t nread; int fds[2]; int val; val = BUFSIZE; if (docheck != 0) (void)memcpy(backup, vp, BUFSIZE); if (socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, fds) != 0) atf_tc_fail("socketpair() failed"); val = BUFSIZE; if (setsockopt(fds[1], SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) != 0) atf_tc_fail("setsockopt() failed, SO_RCVBUF"); val = BUFSIZE; if (setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) != 0) atf_tc_fail("setsockopt() failed, SO_SNDBUF"); if (fcntl(fds[0], F_SETFL, O_NONBLOCK) != 0) atf_tc_fail("fcntl() failed"); nwritten = write(fds[0], (char *)vp + page, BUFSIZE - page); if (nwritten == -1) atf_tc_fail("write() failed"); /* Break loan. */ (void)memset(vp2, pat, BUFSIZE); nread = read(fds[1], buf + page, BUFSIZE - page); if (nread == -1) atf_tc_fail("read() failed"); if (nread != nwritten) atf_tc_fail("too short read"); if (docheck != 0 && memcmp(backup, buf + page, nread) != 0) atf_tc_fail("data mismatch"); ATF_REQUIRE(close(fds[0]) == 0); ATF_REQUIRE(close(fds[1]) == 0); } static void map_sighandler(int signo) { _exit(signo); } #ifdef __NetBSD__ ATF_TC(mmap_block); ATF_TC_HEAD(mmap_block, tc) { atf_tc_set_md_var(tc, "descr", "Test mmap(2) with a block device"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(mmap_block, tc) { static const int mib[] = { CTL_HW, HW_DISKNAMES }; static const unsigned int miblen = __arraycount(mib); char *map, *dk, *drives, dev[PATH_MAX]; size_t len; int fd = -1; atf_tc_skip("The test case causes a panic (PR kern/38889, kern/46592)"); ATF_REQUIRE(sysctl(mib, miblen, NULL, &len, NULL, 0) == 0); drives = malloc(len); ATF_REQUIRE(drives != NULL); ATF_REQUIRE(sysctl(mib, miblen, drives, &len, NULL, 0) == 0); for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) { sprintf(dev, _PATH_DEV "%s%c", dk, 'a'+RAW_PART); fprintf(stderr, "trying: %s\n", dev); if ((fd = open(dev, O_RDONLY)) >= 0) { (void)fprintf(stderr, "using %s\n", dev); break; } } free(drives); if (fd < 0) atf_tc_skip("failed to find suitable block device"); map = mmap(NULL, 4096, PROT_READ, MAP_FILE, fd, 0); ATF_REQUIRE(map != MAP_FAILED); (void)fprintf(stderr, "first byte %x\n", *map); ATF_REQUIRE(close(fd) == 0); (void)fprintf(stderr, "first byte %x\n", *map); ATF_REQUIRE(munmap(map, 4096) == 0); } #endif ATF_TC(mmap_err); ATF_TC_HEAD(mmap_err, tc) { atf_tc_set_md_var(tc, "descr", "Test error conditions of mmap(2)"); } ATF_TC_BODY(mmap_err, tc) { size_t addr = SIZE_MAX; void *map; errno = 0; map = mmap(NULL, 3, PROT_READ, MAP_FILE|MAP_PRIVATE, -1, 0); ATF_REQUIRE(map == MAP_FAILED); ATF_REQUIRE(errno == EBADF); errno = 0; map = mmap(&addr, page, PROT_READ, MAP_FIXED|MAP_PRIVATE, -1, 0); ATF_REQUIRE(map == MAP_FAILED); ATF_REQUIRE(errno == EINVAL); errno = 0; map = mmap(NULL, page, PROT_READ, MAP_ANON|MAP_PRIVATE, INT_MAX, 0); ATF_REQUIRE(map == MAP_FAILED); ATF_REQUIRE(errno == EINVAL); } ATF_TC_WITH_CLEANUP(mmap_loan); ATF_TC_HEAD(mmap_loan, tc) { atf_tc_set_md_var(tc, "descr", "Test uvm page loanout with mmap(2)"); } ATF_TC_BODY(mmap_loan, tc) { char buf[BUFSIZE]; char *vp, *vp2; int fd; fd = open(path, O_RDWR | O_CREAT, 0600); ATF_REQUIRE(fd >= 0); (void)memset(buf, 'x', sizeof(buf)); (void)write(fd, buf, sizeof(buf)); vp = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, fd, 0); ATF_REQUIRE(vp != MAP_FAILED); vp2 = vp; testloan(vp, vp2, 'A', 0); testloan(vp, vp2, 'B', 1); ATF_REQUIRE(munmap(vp, BUFSIZE) == 0); vp = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0); vp2 = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0); ATF_REQUIRE(vp != MAP_FAILED); ATF_REQUIRE(vp2 != MAP_FAILED); testloan(vp, vp2, 'E', 1); ATF_REQUIRE(munmap(vp, BUFSIZE) == 0); ATF_REQUIRE(munmap(vp2, BUFSIZE) == 0); } ATF_TC_CLEANUP(mmap_loan, tc) { (void)unlink(path); } ATF_TC_WITH_CLEANUP(mmap_prot_1); ATF_TC_HEAD(mmap_prot_1, tc) { atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #1"); } ATF_TC_BODY(mmap_prot_1, tc) { void *map; int fd; /* * Open a file write-only and try to * map it read-only. This should fail. */ fd = open(path, O_WRONLY | O_CREAT, 0700); if (fd < 0) return; ATF_REQUIRE(write(fd, "XXX", 3) == 3); map = mmap(NULL, 3, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0); map_check(map, 1); map = mmap(NULL, 3, PROT_WRITE, MAP_FILE|MAP_PRIVATE, fd, 0); map_check(map, 0); ATF_REQUIRE(close(fd) == 0); } ATF_TC_CLEANUP(mmap_prot_1, tc) { (void)unlink(path); } ATF_TC(mmap_prot_2); ATF_TC_HEAD(mmap_prot_2, tc) { atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #2"); } ATF_TC_BODY(mmap_prot_2, tc) { char buf[2]; void *map; pid_t pid; int sta; /* * Make a PROT_NONE mapping and try to access it. * If we catch a SIGSEGV, all works as expected. */ map = mmap(NULL, page, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0); ATF_REQUIRE(map != MAP_FAILED); pid = fork(); ATF_REQUIRE(pid >= 0); if (pid == 0) { ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR); ATF_REQUIRE(strlcpy(buf, map, sizeof(buf)) != 0); } (void)wait(&sta); ATF_REQUIRE(WIFEXITED(sta) != 0); ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV); ATF_REQUIRE(munmap(map, page) == 0); } ATF_TC_WITH_CLEANUP(mmap_prot_3); ATF_TC_HEAD(mmap_prot_3, tc) { atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #3"); } ATF_TC_BODY(mmap_prot_3, tc) { char buf[2]; int fd, sta; void *map; pid_t pid; /* * Open a file, change the permissions * to read-only, and try to map it as * PROT_NONE. This should succeed, but * the access should generate SIGSEGV. */ fd = open(path, O_RDWR | O_CREAT, 0700); - if (fd < 0) +#ifdef __FreeBSD__ + atf_tc_skip("opening %s failed; skipping testcase: %s", + path, strerror(errno)); +#else return; +#endif ATF_REQUIRE(write(fd, "XXX", 3) == 3); ATF_REQUIRE(close(fd) == 0); ATF_REQUIRE(chmod(path, 0444) == 0); fd = open(path, O_RDONLY); ATF_REQUIRE(fd != -1); map = mmap(NULL, 3, PROT_NONE, MAP_FILE | MAP_SHARED, fd, 0); ATF_REQUIRE(map != MAP_FAILED); pid = fork(); ATF_REQUIRE(pid >= 0); if (pid == 0) { ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR); ATF_REQUIRE(strlcpy(buf, map, sizeof(buf)) != 0); } (void)wait(&sta); ATF_REQUIRE(WIFEXITED(sta) != 0); ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV); ATF_REQUIRE(munmap(map, 3) == 0); +#ifdef __FreeBSD__ + (void)close(fd); +#endif } ATF_TC_CLEANUP(mmap_prot_3, tc) { (void)unlink(path); } ATF_TC_WITH_CLEANUP(mmap_truncate); ATF_TC_HEAD(mmap_truncate, tc) { atf_tc_set_md_var(tc, "descr", "Test mmap(2) and ftruncate(2)"); } ATF_TC_BODY(mmap_truncate, tc) { char *map; long i; int fd; fd = open(path, O_RDWR | O_CREAT, 0700); if (fd < 0) return; /* * See that ftruncate(2) works * while the file is mapped. */ ATF_REQUIRE(ftruncate(fd, page) == 0); map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_FILE|MAP_PRIVATE, fd, 0); ATF_REQUIRE(map != MAP_FAILED); for (i = 0; i < page; i++) map[i] = 'x'; ATF_REQUIRE(ftruncate(fd, 0) == 0); ATF_REQUIRE(ftruncate(fd, page / 8) == 0); ATF_REQUIRE(ftruncate(fd, page / 4) == 0); ATF_REQUIRE(ftruncate(fd, page / 2) == 0); ATF_REQUIRE(ftruncate(fd, page / 12) == 0); ATF_REQUIRE(ftruncate(fd, page / 64) == 0); +#ifdef __FreeBSD__ + (void)munmap(map, page); +#endif ATF_REQUIRE(close(fd) == 0); } ATF_TC_CLEANUP(mmap_truncate, tc) { (void)unlink(path); } ATF_TC_WITH_CLEANUP(mmap_truncate_signal); ATF_TC_HEAD(mmap_truncate_signal, tc) { atf_tc_set_md_var(tc, "descr", "Test mmap(2) ftruncate(2) causing signal"); } ATF_TC_BODY(mmap_truncate_signal, tc) { char *map; long i; int fd, sta; pid_t pid; #ifdef __FreeBSD__ atf_tc_expect_fail("testcase fails with SIGSEGV on FreeBSD; bug # 211924"); #endif fd = open(path, O_RDWR | O_CREAT, 0700); if (fd < 0) return; ATF_REQUIRE(write(fd, "foo\n", 5) == 5); map = mmap(NULL, page, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0); ATF_REQUIRE(map != MAP_FAILED); sta = 0; for (i = 0; i < 5; i++) sta += map[i]; ATF_REQUIRE(sta == 334); ATF_REQUIRE(ftruncate(fd, 0) == 0); pid = fork(); ATF_REQUIRE(pid >= 0); if (pid == 0) { ATF_REQUIRE(signal(SIGBUS, map_sighandler) != SIG_ERR); ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR); sta = 0; for (i = 0; i < page; i++) sta += map[i]; /* child never will get this far, but the compiler will not know, so better use the values calculated to prevent the access to be optimized out */ ATF_REQUIRE(i == 0); ATF_REQUIRE(sta == 0); +#ifdef __FreeBSD__ + (void)munmap(map, page); + (void)close(fd); +#endif return; } (void)wait(&sta); ATF_REQUIRE(WIFEXITED(sta) != 0); if (WEXITSTATUS(sta) == SIGSEGV) atf_tc_fail("child process got SIGSEGV instead of SIGBUS"); ATF_REQUIRE(WEXITSTATUS(sta) == SIGBUS); ATF_REQUIRE(munmap(map, page) == 0); ATF_REQUIRE(close(fd) == 0); } ATF_TC_CLEANUP(mmap_truncate_signal, tc) { (void)unlink(path); } ATF_TC(mmap_va0); ATF_TC_HEAD(mmap_va0, tc) { atf_tc_set_md_var(tc, "descr", "Test mmap(2) and vm.user_va0_disable"); } ATF_TC_BODY(mmap_va0, tc) { int flags = MAP_ANON | MAP_FIXED | MAP_PRIVATE; size_t len = sizeof(int); void *map; int val; /* * Make an anonymous fixed mapping at zero address. If the address * is restricted as noted in security(7), the syscall should fail. */ #ifdef __FreeBSD__ if (sysctlbyname("security.bsd.map_at_zero", &val, &len, NULL, 0) != 0) atf_tc_fail("failed to read security.bsd.map_at_zero"); val = !val; /* 1 == enable map at zero */ #endif #ifdef __NetBSD__ if (sysctlbyname("vm.user_va0_disable", &val, &len, NULL, 0) != 0) atf_tc_fail("failed to read vm.user_va0_disable"); #endif map = mmap(NULL, page, PROT_EXEC, flags, -1, 0); map_check(map, val); map = mmap(NULL, page, PROT_READ, flags, -1, 0); map_check(map, val); map = mmap(NULL, page, PROT_WRITE, flags, -1, 0); map_check(map, val); map = mmap(NULL, page, PROT_READ|PROT_WRITE, flags, -1, 0); map_check(map, val); map = mmap(NULL, page, PROT_EXEC|PROT_READ|PROT_WRITE, flags, -1, 0); map_check(map, val); } ATF_TP_ADD_TCS(tp) { page = sysconf(_SC_PAGESIZE); ATF_REQUIRE(page >= 0); #ifdef __NetBSD__ ATF_TP_ADD_TC(tp, mmap_block); #endif ATF_TP_ADD_TC(tp, mmap_err); ATF_TP_ADD_TC(tp, mmap_loan); ATF_TP_ADD_TC(tp, mmap_prot_1); ATF_TP_ADD_TC(tp, mmap_prot_2); ATF_TP_ADD_TC(tp, mmap_prot_3); ATF_TP_ADD_TC(tp, mmap_truncate); ATF_TP_ADD_TC(tp, mmap_truncate_signal); ATF_TP_ADD_TC(tp, mmap_va0); return atf_no_error(); } Index: stable/10 =================================================================== --- stable/10 (revision 313520) +++ stable/10 (revision 313521) Property changes on: stable/10 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r311229,311244