From owner-svn-src-all@freebsd.org Wed May 1 15:28:24 2019 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 740621599AFD; Wed, 1 May 2019 15:28:24 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id DDD7D8239A; Wed, 1 May 2019 15:28:23 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id B454423363; Wed, 1 May 2019 15:28:23 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x41FSN6h071283; Wed, 1 May 2019 15:28:23 GMT (envelope-from markj@FreeBSD.org) Received: (from markj@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x41FSNls071281; Wed, 1 May 2019 15:28:23 GMT (envelope-from markj@FreeBSD.org) Message-Id: <201905011528.x41FSNls071281@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: markj set sender to markj@FreeBSD.org using -f From: Mark Johnston Date: Wed, 1 May 2019 15:28:23 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r346995 - head/tests/sys/vm X-SVN-Group: head X-SVN-Commit-Author: markj X-SVN-Commit-Paths: head/tests/sys/vm X-SVN-Commit-Revision: 346995 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: DDD7D8239A X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.97 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_SHORT(-0.97)[-0.972,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US]; NEURAL_HAM_MEDIUM(-1.00)[-0.999,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0] X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 May 2019 15:28:24 -0000 Author: markj Date: Wed May 1 15:28:23 2019 New Revision: 346995 URL: https://svnweb.freebsd.org/changeset/base/346995 Log: Add a few regression tests for mlock(2). These are intended to exercise some rarely executed code paths. MFC after: 2 weeks Added: head/tests/sys/vm/mlock_test.c (contents, props changed) Modified: head/tests/sys/vm/Makefile Modified: head/tests/sys/vm/Makefile ============================================================================== --- head/tests/sys/vm/Makefile Wed May 1 15:03:12 2019 (r346994) +++ head/tests/sys/vm/Makefile Wed May 1 15:28:23 2019 (r346995) @@ -4,6 +4,7 @@ PACKAGE= tests TESTSDIR= ${TESTSBASE}/sys/vm -ATF_TESTS_C+= mmap_test +ATF_TESTS_C+= mlock_test \ + mmap_test .include Added: head/tests/sys/vm/mlock_test.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tests/sys/vm/mlock_test.c Wed May 1 15:28:23 2019 (r346995) @@ -0,0 +1,174 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2019 Mark Johnston + * + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +static void +test_wired_copy_on_write(void *addr, size_t len) +{ + int status, val; + pid_t pid; + + pid = fork(); + if (pid == -1) + atf_tc_fail("fork() failed: %s", strerror(errno)); + if (pid == 0) { + if (mlock(addr, len) != 0) + _exit(1); + if (ptrace(PT_TRACE_ME, 0, NULL, 0) != 0) + _exit(2); + if (raise(SIGSTOP) != 0) + _exit(3); + if (munlock(addr, len) != 0) + _exit(4); + _exit(0); + } + + ATF_REQUIRE(waitpid(pid, &status, 0) == pid); + ATF_REQUIRE_MSG(!WIFEXITED(status), + "child exited with status %d", WEXITSTATUS(status)); + ATF_REQUIRE(WIFSTOPPED(status)); + ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); + + errno = 0; + val = ptrace(PT_READ_D, pid, addr, 0); + ATF_REQUIRE(errno == 0); + ATF_REQUIRE(ptrace(PT_WRITE_D, pid, addr, val) == 0); + ATF_REQUIRE(ptrace(PT_CONTINUE, pid, (caddr_t)1, 0) == 0); + ATF_REQUIRE(waitpid(pid, &status, 0) == pid); + ATF_REQUIRE(WIFEXITED(status)); + ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0, + "child exited with status %d", WSTOPSIG(status)); +} + +/* + * Use ptrace(2) to trigger a copy-on-write fault of anonymous memory. + */ +ATF_TC_WITHOUT_HEAD(mlock__copy_on_write_anon); +ATF_TC_BODY(mlock__copy_on_write_anon, tc) +{ + char *addr; + int len; + + len = getpagesize(); + addr = mmap(NULL, len, PROT_READ, MAP_ANON, -1, 0); + ATF_REQUIRE(addr != MAP_FAILED); + + test_wired_copy_on_write(addr, len); +} + +/* + * Use ptrace(2) to trigger a copy-on-write fault of a read-only text page. + */ +ATF_TC_WITHOUT_HEAD(mlock__copy_on_write_vnode); +ATF_TC_BODY(mlock__copy_on_write_vnode, tc) +{ + void *addr; + int len; + + len = getpagesize(); + addr = (void *)((uintptr_t)test_wired_copy_on_write & ~(len - 1)); + + test_wired_copy_on_write(addr, len); +} + +/* + * Try truncating and then resizing an mlock()ed mapping. + */ +ATF_TC_WITHOUT_HEAD(mlock__truncate_and_resize); +ATF_TC_BODY(mlock__truncate_and_resize, tc) +{ + char filename[16]; + char *addr; + int fd, i, len; + + snprintf(filename, sizeof(filename), "tmp.XXXXXX"); + fd = mkstemp(filename); + ATF_REQUIRE(fd >= 0); + ATF_REQUIRE(unlink(filename) == 0); + + len = getpagesize(); + ATF_REQUIRE(ftruncate(fd, len) == 0); + + addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + ATF_REQUIRE(addr != MAP_FAILED); + ATF_REQUIRE(mlock(addr, len) == 0); + memset(addr, 1, len); + ATF_REQUIRE(ftruncate(fd, 0) == 0); + ATF_REQUIRE(ftruncate(fd, len) == 0); + for (i = 0; i < len; i++) + ATF_REQUIRE(addr[i] == 0); + ATF_REQUIRE(munlock(addr, len) == 0); +} + +/* + * Make sure that we can munlock() a truncated mapping. + */ +ATF_TC_WITHOUT_HEAD(mlock__truncate_and_unlock); +ATF_TC_BODY(mlock__truncate_and_unlock, tc) +{ + char filename[16]; + void *addr; + int fd, len; + + snprintf(filename, sizeof(filename), "tmp.XXXXXX"); + fd = mkstemp(filename); + ATF_REQUIRE(fd >= 0); + ATF_REQUIRE(unlink(filename) == 0); + + len = getpagesize(); + ATF_REQUIRE(ftruncate(fd, len) == 0); + + addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + ATF_REQUIRE(addr != MAP_FAILED); + ATF_REQUIRE(mlock(addr, len) == 0); + ATF_REQUIRE(ftruncate(fd, 0) == 0); + ATF_REQUIRE(munlock(addr, len) == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, mlock__copy_on_write_anon); + ATF_TP_ADD_TC(tp, mlock__copy_on_write_vnode); + ATF_TP_ADD_TC(tp, mlock__truncate_and_resize); + ATF_TP_ADD_TC(tp, mlock__truncate_and_unlock); + + return (atf_no_error()); +}