From owner-svn-src-all@freebsd.org Wed Aug 7 20:36:37 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B84DDB712D; Wed, 7 Aug 2019 20:36:37 +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 463jw54QVDz4JVj; Wed, 7 Aug 2019 20:36:37 +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 793D81A3E1; Wed, 7 Aug 2019 20:36:37 +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 x77Kab85027535; Wed, 7 Aug 2019 20:36:37 GMT (envelope-from markj@FreeBSD.org) Received: (from markj@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x77Kabid027533; Wed, 7 Aug 2019 20:36:37 GMT (envelope-from markj@FreeBSD.org) Message-Id: <201908072036.x77Kabid027533@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: markj set sender to markj@FreeBSD.org using -f From: Mark Johnston Date: Wed, 7 Aug 2019 20:36:37 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r350699 - stable/12/tests/sys/vm X-SVN-Group: stable-12 X-SVN-Commit-Author: markj X-SVN-Commit-Paths: stable/12/tests/sys/vm X-SVN-Commit-Revision: 350699 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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, 07 Aug 2019 20:36:37 -0000 Author: markj Date: Wed Aug 7 20:36:36 2019 New Revision: 350699 URL: https://svnweb.freebsd.org/changeset/base/350699 Log: MFC r346995: Add a few regression tests for mlock(2). Added: stable/12/tests/sys/vm/mlock_test.c - copied unchanged from r346995, head/tests/sys/vm/mlock_test.c Modified: stable/12/tests/sys/vm/Makefile Directory Properties: stable/12/ (props changed) Modified: stable/12/tests/sys/vm/Makefile ============================================================================== --- stable/12/tests/sys/vm/Makefile Wed Aug 7 20:28:27 2019 (r350698) +++ stable/12/tests/sys/vm/Makefile Wed Aug 7 20:36:36 2019 (r350699) @@ -4,6 +4,7 @@ PACKAGE= tests TESTSDIR= ${TESTSBASE}/sys/vm -ATF_TESTS_C+= mmap_test +ATF_TESTS_C+= mlock_test \ + mmap_test .include Copied: stable/12/tests/sys/vm/mlock_test.c (from r346995, head/tests/sys/vm/mlock_test.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/12/tests/sys/vm/mlock_test.c Wed Aug 7 20:36:36 2019 (r350699, copy of r346995, head/tests/sys/vm/mlock_test.c) @@ -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()); +}