Import ptrace(2) tests into appropriate directory tests/lib/libc/sys/
This is the correct directory documented in tests/README for such tests. Discussed with <martin> Sponsored by <The NetBSD Foundation>
This commit is contained in:
parent
7466388691
commit
24e714f435
|
@ -1,4 +1,4 @@
|
|||
# $NetBSD: mi,v 1.202 2017/04/02 01:49:51 kamil Exp $
|
||||
# $NetBSD: mi,v 1.203 2017/04/02 21:44:00 kamil Exp $
|
||||
./etc/mtree/set.debug comp-sys-root
|
||||
./usr/lib comp-sys-usr compatdir
|
||||
./usr/lib/i18n/libBIG5_g.a comp-c-debuglib debuglib,compatfile
|
||||
|
@ -2079,6 +2079,13 @@
|
|||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_poll.debug tests-lib-debug debug,atf,compattestfile
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_posix_fadvise.debug tests-lib-debug debug,atf,rump
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_posix_fallocate.debug tests-lib-debug debug,atf,compattestfile
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_ptrace.debug tests-lib-debug debug,atf,compattestfile
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_ptrace_wait.debug tests-lib-debug debug,atf,compattestfile
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_ptrace_wait3.debug tests-lib-debug debug,atf,compattestfile
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_ptrace_wait4.debug tests-lib-debug debug,atf,compattestfile
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_ptrace_wait6.debug tests-lib-debug debug,atf,compattestfile
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_ptrace_waitid.debug tests-lib-debug debug,atf,compattestfile
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_ptrace_waitpid.debug tests-lib-debug debug,atf,compattestfile
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_recvmmsg.debug tests-lib-debug debug,atf,compattestfile
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_revoke.debug tests-lib-debug debug,atf,compattestfile
|
||||
./usr/libdata/debug/usr/tests/lib/libc/sys/t_select.debug tests-lib-debug debug,atf,compattestfile
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# $NetBSD: mi,v 1.731 2017/04/02 01:49:52 kamil Exp $
|
||||
# $NetBSD: mi,v 1.732 2017/04/02 21:44:00 kamil Exp $
|
||||
#
|
||||
# Note: don't delete entries from here - mark them as "obsolete" instead.
|
||||
#
|
||||
|
@ -2762,6 +2762,13 @@
|
|||
./usr/tests/lib/libc/sys/t_poll tests-lib-tests compattestfile,atf
|
||||
./usr/tests/lib/libc/sys/t_posix_fadvise tests-lib-tests atf,rump
|
||||
./usr/tests/lib/libc/sys/t_posix_fallocate tests-lib-tests compattestfile,atf
|
||||
./usr/tests/lib/libc/sys/t_ptrace tests-lib-tests compattestfile,atf
|
||||
./usr/tests/lib/libc/sys/t_ptrace_wait tests-lib-tests compattestfile,atf
|
||||
./usr/tests/lib/libc/sys/t_ptrace_wait3 tests-lib-tests compattestfile,atf
|
||||
./usr/tests/lib/libc/sys/t_ptrace_wait4 tests-lib-tests compattestfile,atf
|
||||
./usr/tests/lib/libc/sys/t_ptrace_wait6 tests-lib-tests compattestfile,atf
|
||||
./usr/tests/lib/libc/sys/t_ptrace_waitid tests-lib-tests compattestfile,atf
|
||||
./usr/tests/lib/libc/sys/t_ptrace_waitpid tests-lib-tests compattestfile,atf
|
||||
./usr/tests/lib/libc/sys/t_recvmmsg tests-lib-tests compattestfile,atf
|
||||
./usr/tests/lib/libc/sys/t_revoke tests-lib-tests compattestfile,atf
|
||||
./usr/tests/lib/libc/sys/t_select tests-lib-tests compattestfile,atf
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# $NetBSD: Makefile,v 1.48 2017/02/01 10:18:27 martin Exp $
|
||||
# $NetBSD: Makefile,v 1.49 2017/04/02 21:44:00 kamil Exp $
|
||||
|
||||
MKMAN= no
|
||||
|
||||
|
@ -51,6 +51,13 @@ TESTS_C+= t_pipe
|
|||
TESTS_C+= t_pipe2
|
||||
TESTS_C+= t_poll
|
||||
TESTS_C+= t_posix_fallocate
|
||||
TESTS_C+= t_ptrace
|
||||
TESTS_C+= t_ptrace_wait
|
||||
TESTS_C+= t_ptrace_wait3
|
||||
TESTS_C+= t_ptrace_wait4
|
||||
TESTS_C+= t_ptrace_wait6
|
||||
TESTS_C+= t_ptrace_waitid
|
||||
TESTS_C+= t_ptrace_waitpid
|
||||
TESTS_C+= t_recvmmsg
|
||||
TESTS_C+= t_revoke
|
||||
TESTS_C+= t_select
|
||||
|
@ -83,6 +90,12 @@ LDADD.t_posix_fadvise+= -lrumpvfs -lrump -lrumpuser -lrump -lpthread
|
|||
.endif
|
||||
|
||||
CPPFLAGS.t_lwp_create.c += -D_KERNTYPES
|
||||
CPPFLAGS.t_ptrace_wait.c += -D_KERNTYPES
|
||||
CPPFLAGS.t_ptrace_wait3.c += -D_KERNTYPES
|
||||
CPPFLAGS.t_ptrace_wait4.c += -D_KERNTYPES
|
||||
CPPFLAGS.t_ptrace_wait6.c += -D_KERNTYPES
|
||||
CPPFLAGS.t_ptrace_waitid.c += -D_KERNTYPES
|
||||
CPPFLAGS.t_ptrace_waitpid.c += -D_KERNTYPES
|
||||
|
||||
FILES= truncate_test.root_owned
|
||||
FILESBUILD= yes
|
||||
|
|
|
@ -0,0 +1,136 @@
|
|||
/* $NetBSD: msg.h,v 1.1 2017/04/02 21:44:00 kamil Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2016 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Christos Zoulas.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
struct msg_fds {
|
||||
int pfd[2];
|
||||
int cfd[2];
|
||||
};
|
||||
|
||||
#define CLOSEFD(fd) do { \
|
||||
if (fd != -1) { \
|
||||
close(fd); \
|
||||
fd = -1; \
|
||||
} \
|
||||
} while (/*CONSTCOND*/ 0)
|
||||
|
||||
static int
|
||||
msg_open(struct msg_fds *fds)
|
||||
{
|
||||
if (pipe(fds->pfd) == -1)
|
||||
return -1;
|
||||
if (pipe(fds->cfd) == -1) {
|
||||
close(fds->pfd[0]);
|
||||
close(fds->pfd[1]);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
msg_close(struct msg_fds *fds)
|
||||
{
|
||||
CLOSEFD(fds->pfd[0]);
|
||||
CLOSEFD(fds->pfd[1]);
|
||||
CLOSEFD(fds->cfd[0]);
|
||||
CLOSEFD(fds->cfd[1]);
|
||||
}
|
||||
|
||||
static int
|
||||
msg_write_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
|
||||
{
|
||||
ssize_t rv;
|
||||
CLOSEFD(fds->cfd[1]);
|
||||
CLOSEFD(fds->pfd[0]);
|
||||
|
||||
printf("Send %s\n", info);
|
||||
rv = write(fds->pfd[1], msg, len);
|
||||
if (rv != (ssize_t)len)
|
||||
return 1;
|
||||
// printf("Wait %s\n", info);
|
||||
rv = read(fds->cfd[0], msg, len);
|
||||
if (rv != (ssize_t)len)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
msg_write_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
|
||||
{
|
||||
ssize_t rv;
|
||||
CLOSEFD(fds->pfd[1]);
|
||||
CLOSEFD(fds->cfd[0]);
|
||||
|
||||
printf("Send %s\n", info);
|
||||
rv = write(fds->cfd[1], msg, len);
|
||||
if (rv != (ssize_t)len)
|
||||
return 1;
|
||||
// printf("Wait %s\n", info);
|
||||
rv = read(fds->pfd[0], msg, len);
|
||||
if (rv != (ssize_t)len)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
msg_read_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
|
||||
{
|
||||
ssize_t rv;
|
||||
CLOSEFD(fds->pfd[1]);
|
||||
CLOSEFD(fds->cfd[0]);
|
||||
|
||||
printf("Wait %s\n", info);
|
||||
rv = read(fds->pfd[0], msg, len);
|
||||
if (rv != (ssize_t)len)
|
||||
return 1;
|
||||
// printf("Send %s\n", info);
|
||||
rv = write(fds->cfd[1], msg, len);
|
||||
if (rv != (ssize_t)len)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
msg_read_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
|
||||
{
|
||||
ssize_t rv;
|
||||
CLOSEFD(fds->cfd[1]);
|
||||
CLOSEFD(fds->pfd[0]);
|
||||
|
||||
printf("Wait %s\n", info);
|
||||
rv = read(fds->cfd[0], msg, len);
|
||||
if (rv != (ssize_t)len)
|
||||
return 1;
|
||||
// printf("Send %s\n", info);
|
||||
rv = write(fds->pfd[1], msg, len);
|
||||
if (rv != (ssize_t)len)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,208 @@
|
|||
/* $NetBSD: t_ptrace.c,v 1.1 2017/04/02 21:44:00 kamil Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2016 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 <sys/cdefs.h>
|
||||
__RCSID("$NetBSD: t_ptrace.c,v 1.1 2017/04/02 21:44:00 kamil Exp $");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ptrace.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <atf-c.h>
|
||||
|
||||
#include "h_macros.h"
|
||||
|
||||
/*
|
||||
* A child process cannot call atf functions and expect them to magically
|
||||
* work like in the parent.
|
||||
* The printf(3) messaging from a child will not work out of the box as well
|
||||
* without estabilishing a communication protocol with its parent. To not
|
||||
* overcomplicate the tests - do not log from a child and use err(3)/errx(3)
|
||||
* wrapped with FORKEE_ASSERT()/FORKEE_ASSERTX() as that is guaranteed to work.
|
||||
*/
|
||||
#define FORKEE_ASSERTX(x) \
|
||||
do { \
|
||||
int ret = (x); \
|
||||
if (!ret) \
|
||||
errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s", \
|
||||
__FILE__, __LINE__, __func__, #x); \
|
||||
} while (0)
|
||||
|
||||
#define FORKEE_ASSERT(x) \
|
||||
do { \
|
||||
int ret = (x); \
|
||||
if (!ret) \
|
||||
err(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s", \
|
||||
__FILE__, __LINE__, __func__, #x); \
|
||||
} while (0)
|
||||
|
||||
ATF_TC(attach_pid0);
|
||||
ATF_TC_HEAD(attach_pid0, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr",
|
||||
"Assert that a debugger cannot attach to PID 0");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(attach_pid0, tc)
|
||||
{
|
||||
errno = 0;
|
||||
ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 0, NULL, 0) == -1);
|
||||
}
|
||||
|
||||
ATF_TC(attach_pid1);
|
||||
ATF_TC_HEAD(attach_pid1, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr",
|
||||
"Assert that a debugger cannot attach to PID 1 (as non-root)");
|
||||
|
||||
atf_tc_set_md_var(tc, "require.user", "unprivileged");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(attach_pid1, tc)
|
||||
{
|
||||
ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 1, NULL, 0) == -1);
|
||||
}
|
||||
|
||||
ATF_TC(attach_pid1_securelevel);
|
||||
ATF_TC_HEAD(attach_pid1_securelevel, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr",
|
||||
"Assert that a debugger cannot attach to PID 1 with "
|
||||
"securelevel >= 0 (as root)");
|
||||
|
||||
atf_tc_set_md_var(tc, "require.user", "root");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(attach_pid1_securelevel, tc)
|
||||
{
|
||||
int level;
|
||||
size_t len = sizeof(level);
|
||||
|
||||
ATF_REQUIRE(sysctlbyname("kern.securelevel", &level, &len, NULL, 0)
|
||||
!= -1);
|
||||
|
||||
if (level < 0) {
|
||||
atf_tc_skip("Test must be run with securelevel >= 0");
|
||||
}
|
||||
|
||||
ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 1, NULL, 0) == -1);
|
||||
}
|
||||
|
||||
ATF_TC(attach_self);
|
||||
ATF_TC_HEAD(attach_self, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr",
|
||||
"Assert that a debugger cannot attach to self (as it's nonsense)");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(attach_self, tc)
|
||||
{
|
||||
ATF_REQUIRE_ERRNO(EINVAL, ptrace(PT_ATTACH, getpid(), NULL, 0) == -1);
|
||||
}
|
||||
|
||||
ATF_TC(attach_chroot);
|
||||
ATF_TC_HEAD(attach_chroot, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr",
|
||||
"Assert that a debugger cannot trace another process unless the "
|
||||
"process's root directory is at or below the tracing process's "
|
||||
"root");
|
||||
|
||||
atf_tc_set_md_var(tc, "require.user", "root");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(attach_chroot, tc)
|
||||
{
|
||||
char buf[PATH_MAX];
|
||||
pid_t child;
|
||||
int fds_toparent[2], fds_fromparent[2];
|
||||
int rv;
|
||||
uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
|
||||
|
||||
(void)memset(buf, '\0', sizeof(buf));
|
||||
ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
|
||||
(void)strlcat(buf, "/dir", sizeof(buf));
|
||||
|
||||
ATF_REQUIRE(mkdir(buf, 0500) == 0);
|
||||
ATF_REQUIRE(chdir(buf) == 0);
|
||||
|
||||
ATF_REQUIRE(pipe(fds_toparent) == 0);
|
||||
ATF_REQUIRE(pipe(fds_fromparent) == 0);
|
||||
child = atf_utils_fork();
|
||||
if (child == 0) {
|
||||
FORKEE_ASSERT(close(fds_toparent[0]) == 0);
|
||||
FORKEE_ASSERT(close(fds_fromparent[1]) == 0);
|
||||
|
||||
FORKEE_ASSERT(chroot(buf) == 0);
|
||||
|
||||
rv = write(fds_toparent[1], &msg, sizeof(msg));
|
||||
FORKEE_ASSERTX(rv == sizeof(msg));
|
||||
|
||||
ATF_REQUIRE_ERRNO(EPERM,
|
||||
ptrace(PT_ATTACH, getppid(), NULL, 0) == -1);
|
||||
|
||||
rv = read(fds_fromparent[0], &msg, sizeof(msg));
|
||||
FORKEE_ASSERTX(rv == sizeof(msg));
|
||||
|
||||
_exit(0);
|
||||
}
|
||||
ATF_REQUIRE(close(fds_toparent[1]) == 0);
|
||||
ATF_REQUIRE(close(fds_fromparent[0]) == 0);
|
||||
|
||||
printf("Waiting for chrooting of the child PID %d", child);
|
||||
rv = read(fds_toparent[0], &msg, sizeof(msg));
|
||||
ATF_REQUIRE(rv == sizeof(msg));
|
||||
|
||||
printf("Child is ready, it will try to PT_ATTACH to parent\n");
|
||||
rv = write(fds_fromparent[1], &msg, sizeof(msg));
|
||||
ATF_REQUIRE(rv == sizeof(msg));
|
||||
|
||||
printf("fds_fromparent is no longer needed - close it\n");
|
||||
ATF_REQUIRE(close(fds_fromparent[1]) == 0);
|
||||
|
||||
printf("fds_toparent is no longer needed - close it\n");
|
||||
ATF_REQUIRE(close(fds_toparent[0]) == 0);
|
||||
}
|
||||
|
||||
ATF_TP_ADD_TCS(tp)
|
||||
{
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
setvbuf(stderr, NULL, _IONBF, 0);
|
||||
ATF_TP_ADD_TC(tp, attach_pid0);
|
||||
ATF_TP_ADD_TC(tp, attach_pid1);
|
||||
ATF_TP_ADD_TC(tp, attach_pid1_securelevel);
|
||||
ATF_TP_ADD_TC(tp, attach_self);
|
||||
ATF_TP_ADD_TC(tp, attach_chroot);
|
||||
|
||||
return atf_no_error();
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
/* $NetBSD: t_ptrace_amd64_wait.h,v 1.1 2017/04/02 21:44:00 kamil Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2016 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.
|
||||
*/
|
||||
|
||||
#if defined(__x86_64__)
|
||||
ATF_TC(x86_64_regs1);
|
||||
ATF_TC_HEAD(x86_64_regs1, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr",
|
||||
"Call PT_GETREGS and iterate over General Purpose registers");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(x86_64_regs1, tc)
|
||||
{
|
||||
const int exitval = 5;
|
||||
const int sigval = SIGSTOP;
|
||||
pid_t child, wpid;
|
||||
#if defined(TWAIT_HAVE_STATUS)
|
||||
int status;
|
||||
#endif
|
||||
struct reg r;
|
||||
|
||||
printf("Before forking process PID=%d\n", getpid());
|
||||
ATF_REQUIRE((child = fork()) != -1);
|
||||
if (child == 0) {
|
||||
printf("Before calling PT_TRACE_ME from child %d\n", getpid());
|
||||
FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
|
||||
|
||||
printf("Before raising %s from child\n", strsignal(sigval));
|
||||
FORKEE_ASSERT(raise(sigval) == 0);
|
||||
|
||||
printf("Before exiting of the child process\n");
|
||||
_exit(exitval);
|
||||
}
|
||||
printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
|
||||
|
||||
printf("Before calling %s() for the child\n", TWAIT_FNAME);
|
||||
TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
|
||||
|
||||
validate_status_stopped(status, sigval);
|
||||
|
||||
printf("Call GETREGS for the child process\n");
|
||||
ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
|
||||
|
||||
printf("RAX=%#" PRIxREGISTER "\n", r.regs[_REG_RAX]);
|
||||
printf("RBX=%#" PRIxREGISTER "\n", r.regs[_REG_RBX]);
|
||||
printf("RCX=%#" PRIxREGISTER "\n", r.regs[_REG_RCX]);
|
||||
printf("RDX=%#" PRIxREGISTER "\n", r.regs[_REG_RDX]);
|
||||
|
||||
printf("RDI=%#" PRIxREGISTER "\n", r.regs[_REG_RDI]);
|
||||
printf("RSI=%#" PRIxREGISTER "\n", r.regs[_REG_RSI]);
|
||||
|
||||
printf("GS=%#" PRIxREGISTER "\n", r.regs[_REG_GS]);
|
||||
printf("FS=%#" PRIxREGISTER "\n", r.regs[_REG_FS]);
|
||||
printf("ES=%#" PRIxREGISTER "\n", r.regs[_REG_ES]);
|
||||
printf("DS=%#" PRIxREGISTER "\n", r.regs[_REG_DS]);
|
||||
printf("CS=%#" PRIxREGISTER "\n", r.regs[_REG_CS]);
|
||||
printf("SS=%#" PRIxREGISTER "\n", r.regs[_REG_SS]);
|
||||
|
||||
printf("RSP=%#" PRIxREGISTER "\n", r.regs[_REG_RSP]);
|
||||
printf("RIP=%#" PRIxREGISTER "\n", r.regs[_REG_RIP]);
|
||||
|
||||
printf("RFLAGS=%#" PRIxREGISTER "\n", r.regs[_REG_RFLAGS]);
|
||||
|
||||
printf("R8=%#" PRIxREGISTER "\n", r.regs[_REG_R8]);
|
||||
printf("R9=%#" PRIxREGISTER "\n", r.regs[_REG_R9]);
|
||||
printf("R10=%#" PRIxREGISTER "\n", r.regs[_REG_R10]);
|
||||
printf("R11=%#" PRIxREGISTER "\n", r.regs[_REG_R11]);
|
||||
printf("R12=%#" PRIxREGISTER "\n", r.regs[_REG_R12]);
|
||||
printf("R13=%#" PRIxREGISTER "\n", r.regs[_REG_R13]);
|
||||
printf("R14=%#" PRIxREGISTER "\n", r.regs[_REG_R14]);
|
||||
printf("R15=%#" PRIxREGISTER "\n", r.regs[_REG_R15]);
|
||||
|
||||
printf("Before resuming the child process where it left off and "
|
||||
"without signal to be sent\n");
|
||||
ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
|
||||
|
||||
printf("Before calling %s() for the child\n", TWAIT_FNAME);
|
||||
TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
|
||||
|
||||
validate_status_exited(status, exitval);
|
||||
|
||||
printf("Before calling %s() for the child\n", TWAIT_FNAME);
|
||||
TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
|
||||
}
|
||||
#define ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64() \
|
||||
ATF_TP_ADD_TC_HAVE_GPREGS(tp, x86_64_regs1);
|
||||
#else
|
||||
#define ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64()
|
||||
#endif
|
|
@ -0,0 +1,107 @@
|
|||
/* $NetBSD: t_ptrace_i386_wait.h,v 1.1 2017/04/02 21:44:00 kamil Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2016 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.
|
||||
*/
|
||||
|
||||
#if defined(__i386__)
|
||||
ATF_TC(i386_regs1);
|
||||
ATF_TC_HEAD(i386_regs1, tc)
|
||||
{
|
||||
atf_tc_set_md_var(tc, "descr",
|
||||
"Call PT_GETREGS and iterate over General Purpose registers");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(i386_regs1, tc)
|
||||
{
|
||||
const int exitval = 5;
|
||||
const int sigval = SIGSTOP;
|
||||
pid_t child, wpid;
|
||||
#if defined(TWAIT_HAVE_STATUS)
|
||||
int status;
|
||||
#endif
|
||||
struct reg r;
|
||||
|
||||
printf("Before forking process PID=%d\n", getpid());
|
||||
ATF_REQUIRE((child = fork()) != -1);
|
||||
if (child == 0) {
|
||||
printf("Before calling PT_TRACE_ME from child %d\n", getpid());
|
||||
FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
|
||||
|
||||
printf("Before raising %s from child\n", strsignal(sigval));
|
||||
FORKEE_ASSERT(raise(sigval) == 0);
|
||||
|
||||
printf("Before exiting of the child process\n");
|
||||
_exit(exitval);
|
||||
}
|
||||
printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
|
||||
|
||||
printf("Before calling %s() for the child\n", TWAIT_FNAME);
|
||||
TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
|
||||
|
||||
validate_status_stopped(status, sigval);
|
||||
|
||||
printf("Call GETREGS for the child process\n");
|
||||
ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
|
||||
|
||||
printf("EAX=%#" PRIxREGISTER "\n", r.r_eax);
|
||||
printf("EBX=%#" PRIxREGISTER "\n", r.r_ebx);
|
||||
printf("ECX=%#" PRIxREGISTER "\n", r.r_ecx);
|
||||
printf("EDX=%#" PRIxREGISTER "\n", r.r_edx);
|
||||
|
||||
printf("ESP=%#" PRIxREGISTER "\n", r.r_esp);
|
||||
printf("EBP=%#" PRIxREGISTER "\n", r.r_ebp);
|
||||
|
||||
printf("ESI=%#" PRIxREGISTER "\n", r.r_esi);
|
||||
printf("EDI=%#" PRIxREGISTER "\n", r.r_edi);
|
||||
|
||||
printf("EIP=%#" PRIxREGISTER "\n", r.r_eip);
|
||||
|
||||
printf("EFLAGS=%#" PRIxREGISTER "\n", r.r_eflags);
|
||||
|
||||
printf("CS=%#" PRIxREGISTER "\n", r.r_cs);
|
||||
printf("SS=%#" PRIxREGISTER "\n", r.r_ss);
|
||||
printf("DS=%#" PRIxREGISTER "\n", r.r_ds);
|
||||
printf("ES=%#" PRIxREGISTER "\n", r.r_es);
|
||||
printf("FS=%#" PRIxREGISTER "\n", r.r_fs);
|
||||
printf("GS=%#" PRIxREGISTER "\n", r.r_gs);
|
||||
|
||||
printf("Before resuming the child process where it left off and "
|
||||
"without signal to be sent\n");
|
||||
ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
|
||||
|
||||
printf("Before calling %s() for the child\n", TWAIT_FNAME);
|
||||
TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
|
||||
|
||||
validate_status_exited(status, exitval);
|
||||
|
||||
printf("Before calling %s() for the child\n", TWAIT_FNAME);
|
||||
TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
|
||||
}
|
||||
#define ATF_TP_ADD_TCS_PTRACE_WAIT_I386() \
|
||||
ATF_TP_ADD_TC_HAVE_GPREGS(tp, i386_regs1);
|
||||
#else
|
||||
#define ATF_TP_ADD_TCS_PTRACE_WAIT_I386()
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,431 @@
|
|||
/* $NetBSD: t_ptrace_wait.h,v 1.1 2017/04/02 21:44:00 kamil Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2016 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.
|
||||
*/
|
||||
|
||||
/* Detect plain wait(2) use-case */
|
||||
#if !defined(TWAIT_WAITPID) && \
|
||||
!defined(TWAIT_WAITID) && \
|
||||
!defined(TWAIT_WAIT3) && \
|
||||
!defined(TWAIT_WAIT4) && \
|
||||
!defined(TWAIT_WAIT6)
|
||||
#define TWAIT_WAIT
|
||||
#endif
|
||||
|
||||
/*
|
||||
* There are two classes of wait(2)-like functions:
|
||||
* - wait4(2)-like accepting pid_t, optional options parameter, struct rusage*
|
||||
* - wait6(2)-like accepting idtype_t, id_t, struct wrusage, mandatory options
|
||||
*
|
||||
* The TWAIT_FNAME value is to be used for convenience in debug messages.
|
||||
*
|
||||
* The TWAIT_GENERIC() macro is designed to reuse the same unmodified
|
||||
* code with as many wait(2)-like functions as possible.
|
||||
*
|
||||
* In a common use-case wait4(2) and wait6(2)-like function can work the almost
|
||||
* the same way, however there are few important differences:
|
||||
* wait6(2) must specify P_PID for idtype to match wpid from wait4(2).
|
||||
* To behave like wait4(2), wait6(2) the 'options' to wait must include
|
||||
* WEXITED|WTRUNCATED.
|
||||
*
|
||||
* There are two helper macros (they purpose it to mach more than one
|
||||
* wait(2)-like function):
|
||||
* The TWAIT_HAVE_STATUS - specifies whether a function can retrieve
|
||||
* status (as integer value).
|
||||
* The TWAIT_HAVE_PID - specifies whether a function can request
|
||||
* exact process identifier
|
||||
* The TWAIT_HAVE_RUSAGE - specifies whether a function can request
|
||||
* the struct rusage value
|
||||
*
|
||||
*/
|
||||
|
||||
#if defined(TWAIT_WAIT)
|
||||
# define TWAIT_FNAME "wait"
|
||||
# define TWAIT_WAIT4TYPE(a,b,c,d) wait((b))
|
||||
# define TWAIT_GENERIC(a,b,c) wait((b))
|
||||
# define TWAIT_HAVE_STATUS 1
|
||||
#elif defined(TWAIT_WAITPID)
|
||||
# define TWAIT_FNAME "waitpid"
|
||||
# define TWAIT_WAIT4TYPE(a,b,c,d) waitpid((a),(b),(c))
|
||||
# define TWAIT_GENERIC(a,b,c) waitpid((a),(b),(c))
|
||||
# define TWAIT_HAVE_PID 1
|
||||
# define TWAIT_HAVE_STATUS 1
|
||||
#elif defined(TWAIT_WAITID)
|
||||
# define TWAIT_FNAME "waitid"
|
||||
# define TWAIT_GENERIC(a,b,c) \
|
||||
waitid(P_PID,(a),NULL,(c)|WEXITED|WTRAPPED)
|
||||
# define TWAIT_WAIT6TYPE(a,b,c,d,e,f) waitid((a),(b),(f),(d))
|
||||
# define TWAIT_HAVE_PID 1
|
||||
#elif defined(TWAIT_WAIT3)
|
||||
# define TWAIT_FNAME "wait3"
|
||||
# define TWAIT_WAIT4TYPE(a,b,c,d) wait3((b),(c),(d))
|
||||
# define TWAIT_GENERIC(a,b,c) wait3((b),(c),NULL)
|
||||
# define TWAIT_HAVE_STATUS 1
|
||||
# define TWAIT_HAVE_RUSAGE 1
|
||||
#elif defined(TWAIT_WAIT4)
|
||||
# define TWAIT_FNAME "wait4"
|
||||
# define TWAIT_WAIT4TYPE(a,b,c,d) wait4((a),(b),(c),(d))
|
||||
# define TWAIT_GENERIC(a,b,c) wait4((a),(b),(c),NULL)
|
||||
# define TWAIT_HAVE_PID 1
|
||||
# define TWAIT_HAVE_STATUS 1
|
||||
# define TWAIT_HAVE_RUSAGE 1
|
||||
#elif defined(TWAIT_WAIT6)
|
||||
# define TWAIT_FNAME "wait6"
|
||||
# define TWAIT_WAIT6TYPE(a,b,c,d,e,f) wait6((a),(b),(c),(d),(e),(f))
|
||||
# define TWAIT_GENERIC(a,b,c) \
|
||||
wait6(P_PID,(a),(b),(c)|WEXITED|WTRAPPED,NULL,NULL)
|
||||
# define TWAIT_HAVE_PID 1
|
||||
# define TWAIT_HAVE_STATUS 1
|
||||
#endif
|
||||
|
||||
/*
|
||||
* There are 3 groups of tests:
|
||||
* - TWAIT_GENERIC() (wait, wait2, waitpid, wait3, wait4, wait6)
|
||||
* - TWAIT_WAIT4TYPE() (wait2, waitpid, wait3, wait4)
|
||||
* - TWAIT_WAIT6TYPE() (waitid, wait6)
|
||||
*
|
||||
* Tests only in the above categories are allowed. However some tests are not
|
||||
* possible in the context requested functionality to be verified, therefore
|
||||
* there are helper macros:
|
||||
* - TWAIT_HAVE_PID (wait2, waitpid, waitid, wait4, wait6)
|
||||
* - TWAIT_HAVE_STATUS (wait, wait2, waitpid, wait3, wait4, wait6)
|
||||
* - TWAIT_HAVE_RUSAGE (wait3, wait4)
|
||||
* - TWAIT_HAVE_RETPID (wait, wait2, waitpid, wait3, wait4, wait6)
|
||||
*
|
||||
* If there is an intention to test e.g. wait6(2) specific features in the
|
||||
* ptrace(2) context, find the most matching group and with #ifdefs reduce
|
||||
* functionality of less featured than wait6(2) interface (TWAIT_WAIT6TYPE).
|
||||
*
|
||||
* For clarity never use negative preprocessor checks, like:
|
||||
* #if !defined(TWAIT_WAIT4)
|
||||
* always refer to checks for positive values.
|
||||
*/
|
||||
|
||||
#define TEST_REQUIRE_EQ(x, y) \
|
||||
do { \
|
||||
uintmax_t vx = (x); \
|
||||
uintmax_t vy = (y); \
|
||||
int ret = vx == vy; \
|
||||
if (!ret) \
|
||||
ATF_REQUIRE_EQ_MSG(vx, vy, "%s(%ju) == %s(%ju)", \
|
||||
#x, vx, #y, vy); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
/*
|
||||
* A child process cannot call atf functions and expect them to magically
|
||||
* work like in the parent.
|
||||
* The printf(3) messaging from a child will not work out of the box as well
|
||||
* without estabilishing a communication protocol with its parent. To not
|
||||
* overcomplicate the tests - do not log from a child and use err(3)/errx(3)
|
||||
* wrapped with FORKEE_ASSERT()/FORKEE_ASSERTX() as that is guaranteed to work.
|
||||
*/
|
||||
#define FORKEE_ASSERT_EQ(x, y) \
|
||||
do { \
|
||||
uintmax_t vx = (x); \
|
||||
uintmax_t vy = (y); \
|
||||
int ret = vx == vy; \
|
||||
if (!ret) \
|
||||
errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: " \
|
||||
"%s(%ju) == %s(%ju)", __FILE__, __LINE__, __func__, \
|
||||
#x, vx, #y, vy); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define FORKEE_ASSERTX(x) \
|
||||
do { \
|
||||
int ret = (x); \
|
||||
if (!ret) \
|
||||
errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s",\
|
||||
__FILE__, __LINE__, __func__, #x); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define FORKEE_ASSERT(x) \
|
||||
do { \
|
||||
int ret = (x); \
|
||||
if (!ret) \
|
||||
err(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s",\
|
||||
__FILE__, __LINE__, __func__, #x); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
/*
|
||||
* Simplify logic for functions using general purpose registers add HAVE_GPREGS
|
||||
*
|
||||
* For platforms that do not implement all needed calls for simplicity assume
|
||||
* that they are unsupported at all.
|
||||
*/
|
||||
#if defined(PT_GETREGS) \
|
||||
&& defined(PT_SETREGS) \
|
||||
&& defined(PTRACE_REG_PC) \
|
||||
&& defined(PTRACE_REG_SET_PC) \
|
||||
&& defined(PTRACE_REG_SP) \
|
||||
&& defined(PTRACE_REG_INTRV)
|
||||
#define HAVE_GPREGS
|
||||
#endif
|
||||
|
||||
/* Add guards for floating point registers */
|
||||
#if defined(PT_GETFPREGS) \
|
||||
&& defined(PT_SETFPREGS)
|
||||
#define HAVE_FPREGS
|
||||
#endif
|
||||
|
||||
/* Add guards for cpu debug registers */
|
||||
#if defined(PT_GETDBREGS) \
|
||||
&& defined(PT_SETDBREGS)
|
||||
#define HAVE_DBREGS
|
||||
#endif
|
||||
|
||||
/*
|
||||
* If waitid(2) returns because one or more processes have a state change to
|
||||
* report, 0 is returned. If an error is detected, a value of -1 is returned
|
||||
* and errno is set to indicate the error. If WNOHANG is specified and there
|
||||
* are no stopped, continued or exited children, 0 is returned.
|
||||
*/
|
||||
#if defined(TWAIT_WAITID)
|
||||
#define TWAIT_REQUIRE_SUCCESS(a,b) TEST_REQUIRE_EQ((a), 0)
|
||||
#define TWAIT_REQUIRE_FAILURE(a,b) ATF_REQUIRE_ERRNO((a),(b) == -1)
|
||||
#define FORKEE_REQUIRE_SUCCESS(a,b) FORKEE_ASSERT_EQ(a, 0)
|
||||
#define FORKEE_REQUIRE_FAILURE(a,b) \
|
||||
FORKEE_ASSERTX(((a) == errno) && ((b) == -1))
|
||||
#else
|
||||
#define TWAIT_REQUIRE_SUCCESS(a,b) TEST_REQUIRE_EQ((a), (b))
|
||||
#define TWAIT_REQUIRE_FAILURE(a,b) ATF_REQUIRE_ERRNO((a),(b) == -1)
|
||||
#define FORKEE_REQUIRE_SUCCESS(a,b) FORKEE_ASSERT_EQ(a, b)
|
||||
#define FORKEE_REQUIRE_FAILURE(a,b) \
|
||||
FORKEE_ASSERTX(((a) == errno) && ((b) == -1))
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Helper tools to verify whether status reports exited value
|
||||
*/
|
||||
#if TWAIT_HAVE_STATUS
|
||||
static void __used
|
||||
validate_status_exited(int status, int expected)
|
||||
{
|
||||
ATF_REQUIRE_MSG(WIFEXITED(status), "Reported !exited process");
|
||||
ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
|
||||
ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
|
||||
ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
|
||||
|
||||
ATF_REQUIRE_EQ_MSG(WEXITSTATUS(status), expected,
|
||||
"The process has exited with invalid value %d != %d",
|
||||
WEXITSTATUS(status), expected);
|
||||
}
|
||||
|
||||
static void __used
|
||||
forkee_status_exited(int status, int expected)
|
||||
{
|
||||
FORKEE_ASSERTX(WIFEXITED(status));
|
||||
FORKEE_ASSERTX(!WIFCONTINUED(status));
|
||||
FORKEE_ASSERTX(!WIFSIGNALED(status));
|
||||
FORKEE_ASSERTX(!WIFSTOPPED(status));
|
||||
|
||||
FORKEE_ASSERT_EQ(WEXITSTATUS(status), expected);
|
||||
}
|
||||
|
||||
static void __used
|
||||
validate_status_continued(int status)
|
||||
{
|
||||
ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
|
||||
ATF_REQUIRE_MSG(WIFCONTINUED(status), "Reported !continued process");
|
||||
ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
|
||||
ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
|
||||
}
|
||||
|
||||
static void __used
|
||||
forkee_status_continued(int status)
|
||||
{
|
||||
FORKEE_ASSERTX(!WIFEXITED(status));
|
||||
FORKEE_ASSERTX(WIFCONTINUED(status));
|
||||
FORKEE_ASSERTX(!WIFSIGNALED(status));
|
||||
FORKEE_ASSERTX(!WIFSTOPPED(status));
|
||||
}
|
||||
|
||||
static void __used
|
||||
validate_status_signaled(int status, int expected_termsig, int expected_core)
|
||||
{
|
||||
ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
|
||||
ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
|
||||
ATF_REQUIRE_MSG(WIFSIGNALED(status), "Reported !signaled process");
|
||||
ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
|
||||
|
||||
ATF_REQUIRE_EQ_MSG(WTERMSIG(status), expected_termsig,
|
||||
"Unexpected signal received");
|
||||
|
||||
ATF_REQUIRE_EQ_MSG(WCOREDUMP(status), expected_core,
|
||||
"Unexpectedly core file %s generated", expected_core ? "not" : "");
|
||||
}
|
||||
|
||||
static void __used
|
||||
forkee_status_signaled(int status, int expected_termsig, int expected_core)
|
||||
{
|
||||
FORKEE_ASSERTX(!WIFEXITED(status));
|
||||
FORKEE_ASSERTX(!WIFCONTINUED(status));
|
||||
FORKEE_ASSERTX(WIFSIGNALED(status));
|
||||
FORKEE_ASSERTX(!WIFSTOPPED(status));
|
||||
|
||||
FORKEE_ASSERT_EQ(WTERMSIG(status), expected_termsig);
|
||||
FORKEE_ASSERT_EQ(WCOREDUMP(status), expected_core);
|
||||
}
|
||||
|
||||
static void __used
|
||||
validate_status_stopped(int status, int expected)
|
||||
{
|
||||
ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
|
||||
ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
|
||||
ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
|
||||
ATF_REQUIRE_MSG(WIFSTOPPED(status), "Reported !stopped process");
|
||||
|
||||
char st[128], ex[128];
|
||||
strlcpy(st, strsignal(WSTOPSIG(status)), sizeof(st));
|
||||
strlcpy(ex, strsignal(expected), sizeof(ex));
|
||||
|
||||
ATF_REQUIRE_EQ_MSG(WSTOPSIG(status), expected,
|
||||
"Unexpected stop signal received [%s] != [%s]", st, ex);
|
||||
}
|
||||
|
||||
static void __used
|
||||
forkee_status_stopped(int status, int expected)
|
||||
{
|
||||
FORKEE_ASSERTX(!WIFEXITED(status));
|
||||
FORKEE_ASSERTX(!WIFCONTINUED(status));
|
||||
FORKEE_ASSERTX(!WIFSIGNALED(status));
|
||||
FORKEE_ASSERTX(WIFSTOPPED(status));
|
||||
|
||||
FORKEE_ASSERT_EQ(WSTOPSIG(status), expected);
|
||||
}
|
||||
#else
|
||||
#define validate_status_exited(a,b)
|
||||
#define forkee_status_exited(a,b)
|
||||
#define validate_status_continued(a,b)
|
||||
#define forkee_status_continued(a,b)
|
||||
#define validate_status_signaled(a,b,c)
|
||||
#define forkee_status_signaled(a,b,c)
|
||||
#define validate_status_stopped(a,b)
|
||||
#define forkee_status_stopped(a,b)
|
||||
#endif
|
||||
|
||||
/* This function is currently designed to be run in the main/parent process */
|
||||
static void __used
|
||||
await_zombie(pid_t process)
|
||||
{
|
||||
struct kinfo_proc2 p;
|
||||
size_t len = sizeof(p);
|
||||
|
||||
const int name[] = {
|
||||
[0] = CTL_KERN,
|
||||
[1] = KERN_PROC2,
|
||||
[2] = KERN_PROC_PID,
|
||||
[3] = process,
|
||||
[4] = sizeof(p),
|
||||
[5] = 1
|
||||
};
|
||||
|
||||
const size_t namelen = __arraycount(name);
|
||||
|
||||
/* Await the process becoming a zombie */
|
||||
while(1) {
|
||||
ATF_REQUIRE(sysctl(name, namelen, &p, &len, NULL, 0) == 0);
|
||||
|
||||
if (p.p_stat == LSZOMB)
|
||||
break;
|
||||
|
||||
ATF_REQUIRE(usleep(1000) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Happy number sequence -- this function is used to just consume cpu cycles */
|
||||
#define HAPPY_NUMBER 1
|
||||
|
||||
/* If n is not happy then its sequence ends in the cycle:
|
||||
* 4, 16, 37, 58, 89, 145, 42, 20, 4, ... */
|
||||
#define SAD_NUMBER 4
|
||||
|
||||
/* Calculate the sum of the squares of the digits of n */
|
||||
static unsigned __used
|
||||
dsum(unsigned n)
|
||||
{
|
||||
unsigned sum, x;
|
||||
for (sum = 0; n; n /= 10) {
|
||||
x = n % 10;
|
||||
sum += x * x;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/*
|
||||
* XXX: Disabled optimization is required to make tests for hardware assisted
|
||||
* traps in .text functional
|
||||
*
|
||||
* Tested with GCC 5.4 on NetBSD 7.99.47 amd64
|
||||
*/
|
||||
static int __used
|
||||
#ifdef __clang__
|
||||
__attribute__((__optnone__))
|
||||
#else
|
||||
__attribute__((__optimize__("O0")))
|
||||
#endif
|
||||
check_happy(unsigned n)
|
||||
{
|
||||
for (;;) {
|
||||
unsigned total = dsum(n);
|
||||
|
||||
if (total == HAPPY_NUMBER)
|
||||
return 1;
|
||||
if (total == SAD_NUMBER)
|
||||
return 0;
|
||||
|
||||
n = total;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(TWAIT_HAVE_PID)
|
||||
#define ATF_TP_ADD_TC_HAVE_PID(a,b) ATF_TP_ADD_TC(a,b)
|
||||
#else
|
||||
#define ATF_TP_ADD_TC_HAVE_PID(a,b)
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_GPREGS)
|
||||
#define ATF_TP_ADD_TC_HAVE_GPREGS(a,b) ATF_TP_ADD_TC(a,b)
|
||||
#else
|
||||
#define ATF_TP_ADD_TC_HAVE_GPREGS(a,b)
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_FPREGS)
|
||||
#define ATF_TP_ADD_TC_HAVE_FPREGS(a,b) ATF_TP_ADD_TC(a,b)
|
||||
#else
|
||||
#define ATF_TP_ADD_TC_HAVE_FPREGS(a,b)
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_DBREGS)
|
||||
#define ATF_TP_ADD_TC_HAVE_DBREGS(a,b) ATF_TP_ADD_TC(a,b)
|
||||
#else
|
||||
#define ATF_TP_ADD_TC_HAVE_DBREGS(a,b)
|
||||
#endif
|
||||
|
||||
#if defined(PT_STEP)
|
||||
#define ATF_TP_ADD_TC_PT_STEP(a,b) ATF_TP_ADD_TC(a,b)
|
||||
#else
|
||||
#define ATF_TP_ADD_TC_PT_STEP(a,b)
|
||||
#endif
|
|
@ -0,0 +1,30 @@
|
|||
/* $NetBSD: t_ptrace_wait3.c,v 1.1 2017/04/02 21:44:00 kamil Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2016 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.
|
||||
*/
|
||||
|
||||
#define TWAIT_WAIT3
|
||||
#include "t_ptrace_wait.c"
|
|
@ -0,0 +1,30 @@
|
|||
/* $NetBSD: t_ptrace_wait4.c,v 1.1 2017/04/02 21:44:00 kamil Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2016 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.
|
||||
*/
|
||||
|
||||
#define TWAIT_WAIT4
|
||||
#include "t_ptrace_wait.c"
|
|
@ -0,0 +1,30 @@
|
|||
/* $NetBSD: t_ptrace_wait6.c,v 1.1 2017/04/02 21:44:00 kamil Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2016 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.
|
||||
*/
|
||||
|
||||
#define TWAIT_WAIT6
|
||||
#include "t_ptrace_wait.c"
|
|
@ -0,0 +1,30 @@
|
|||
/* $NetBSD: t_ptrace_waitid.c,v 1.1 2017/04/02 21:44:00 kamil Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2016 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.
|
||||
*/
|
||||
|
||||
#define TWAIT_WAITID
|
||||
#include "t_ptrace_wait.c"
|
|
@ -0,0 +1,30 @@
|
|||
/* $NetBSD: t_ptrace_waitpid.c,v 1.1 2017/04/02 21:44:00 kamil Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2016 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.
|
||||
*/
|
||||
|
||||
#define TWAIT_WAITPID
|
||||
#include "t_ptrace_wait.c"
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue