Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add new test t_ptrace with traceme1



details:   https://anonhg.NetBSD.org/src/rev/058f2eaf5075
branches:  trunk
changeset: 348687:058f2eaf5075
user:      kamil <kamil%NetBSD.org@localhost>
date:      Wed Nov 02 12:51:22 2016 +0000

description:
Add new test t_ptrace with traceme1

This test is a placeholder for further checks of the native ptrace(2)
function calls.

XXX: Is it safe to call ATF functions from a child? FreeBSD seems to
     construct dedicated asserts for them.

XXX: printf(3) calls from a child are not intercepted by atf-run(1)

Sponsored by <The NetBSD Foundation>.

diffstat:

 distrib/sets/lists/debug/mi |    3 +-
 distrib/sets/lists/tests/mi |    3 +-
 tests/kernel/Makefile       |    3 +-
 tests/kernel/t_ptrace.c     |  152 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 158 insertions(+), 3 deletions(-)

diffs (207 lines):

diff -r b2c8567b72b4 -r 058f2eaf5075 distrib/sets/lists/debug/mi
--- a/distrib/sets/lists/debug/mi       Wed Nov 02 11:03:33 2016 +0000
+++ b/distrib/sets/lists/debug/mi       Wed Nov 02 12:51:22 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.169 2016/11/01 19:21:17 christos Exp $
+# $NetBSD: mi,v 1.170 2016/11/02 12:51:22 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
@@ -1690,6 +1690,7 @@
 ./usr/libdata/debug/usr/tests/kernel/t_poll3w.debug                    tests-obsolete          obsolete,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_pollts.debug                    tests-obsolete          obsolete,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_posix_fadvise.debug             tests-obsolete          obsolete,compattestfile
+./usr/libdata/debug/usr/tests/kernel/t_ptrace.debug                    tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_pty.debug                       tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_rnd.debug                       tests-kernel-tests      debug,atf,rump
 ./usr/libdata/debug/usr/tests/kernel/t_sigaction.debug                 tests-obsolete          obsolete,compattestfile
diff -r b2c8567b72b4 -r 058f2eaf5075 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Wed Nov 02 11:03:33 2016 +0000
+++ b/distrib/sets/lists/tests/mi       Wed Nov 02 12:51:22 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.690 2016/10/31 10:38:25 ozaki-r Exp $
+# $NetBSD: mi,v 1.691 2016/11/02 12:51:22 kamil Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -2125,6 +2125,7 @@
 ./usr/tests/kernel/t_posix_fadvise             tests-obsolete          obsolete
 ./usr/tests/kernel/t_posix_fallocate           tests-obsolete          obsolete
 ./usr/tests/kernel/t_ps_strings                        tests-kernel-tests      compattestfile,atf
+./usr/tests/kernel/t_ptrace                    tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/t_pty                       tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/t_rnd                       tests-kernel-tests      atf,rump
 ./usr/tests/kernel/t_sigaction                 tests-obsolete          obsolete
diff -r b2c8567b72b4 -r 058f2eaf5075 tests/kernel/Makefile
--- a/tests/kernel/Makefile     Wed Nov 02 11:03:33 2016 +0000
+++ b/tests/kernel/Makefile     Wed Nov 02 12:51:22 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.39 2016/02/15 14:59:38 christos Exp $
+# $NetBSD: Makefile,v 1.40 2016/11/02 12:51:22 kamil Exp $
 
 NOMAN=         # defined
 
@@ -9,6 +9,7 @@
 TESTS_SUBDIRS= kqueue
 TESTS_C=       t_lock
 TESTS_C+=      t_lockf
+TESTS_C+=      t_ptrace
 TESTS_C+=      t_pty
 TESTS_C+=      t_mqueue
 TESTS_C+=      t_sysv
diff -r b2c8567b72b4 -r 058f2eaf5075 tests/kernel/t_ptrace.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/kernel/t_ptrace.c   Wed Nov 02 12:51:22 2016 +0000
@@ -0,0 +1,152 @@
+/*     $NetBSD: t_ptrace.c,v 1.1 2016/11/02 12:51:22 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 2016/11/02 12:51:22 kamil Exp $");
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "../h_macros.h"
+
+ATF_TC(traceme1);
+ATF_TC_HEAD(traceme1, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Verify SIGSTOP followed by _exit(2) in a child");
+}
+
+ATF_TC_BODY(traceme1, tc)
+{
+       int status;
+       const int exitval = 5;
+       const int sigval = SIGSTOP;
+       pid_t child, wpid;
+
+       printf("1: Before forking process PID=%d\n", getpid());
+       ATF_REQUIRE((child = fork()) != -1);
+       if (child == 0) {
+               /* printf(3) messages from a child aren't intercepted by ATF */
+               /* "2: Child process PID=%d\n", getpid() */
+
+               /* "2: Before calling ptrace(PT_TRACE_ME, ...)\n" */
+               if (ptrace(PT_TRACE_ME, 0, NULL, 0) == -1) {
+                       /* XXX: Is it safe to use ATF functions in a child? */
+                       err(EXIT_FAILURE, "2: ptrace(2) call failed with "
+                           "status %s", sys_errlist[errno]);
+               }
+
+               /* "2: Before raising SIGSTOP\n" */
+               raise(sigval);
+
+               /* "2: Before calling _exit(%d)\n", exitval */
+               _exit(exitval);
+       } else {
+               printf("1: Parent process PID=%d, child's PID=%d\n", getpid(),
+                   child);
+
+               printf("1: Before calling waitpid() for the child\n");
+               wpid = waitpid(child, &status, 0);
+
+               printf("1: Validating child's PID (expected %d, got %d)\n",
+                   child, wpid);
+               ATF_REQUIRE(child == wpid);
+
+               printf("1: Ensuring that the child has not been exited\n");
+               ATF_REQUIRE(!WIFEXITED(status));
+
+               printf("1: Ensuring that the child has not been continued\n");
+               ATF_REQUIRE(!WIFCONTINUED(status));
+
+               printf("1: Ensuring that the child has not been terminated "
+                   "with a signal\n");
+               ATF_REQUIRE(!WIFSIGNALED(status));
+
+               printf("1: Ensuring that the child has been stopped\n");
+               ATF_REQUIRE(WIFSTOPPED(status));
+
+               printf("1: Verifying that he child has been stopped with the"
+                   " %s signal (received %s)\n", sys_signame[sigval],
+                   sys_signame[WSTOPSIG(status)]);
+               ATF_REQUIRE(WSTOPSIG(status) == sigval);
+
+               printf("1: 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("1: Before calling waitpid() for the child\n");
+               wpid = waitpid(child, &status, 0);
+
+               printf("1: Validating that child's PID is still there\n");
+               ATF_REQUIRE(wpid == child);
+
+               printf("1: Ensuring that the child has been exited\n");
+               ATF_REQUIRE(WIFEXITED(status));
+
+               printf("1: Ensuring that the child has not been continued\n");
+               ATF_REQUIRE(!WIFCONTINUED(status));
+
+               printf("1: Ensuring that the child has not been terminated "
+                   "with a signal\n");
+               ATF_REQUIRE(!WIFSIGNALED(status));
+
+               printf("1: Ensuring that the child has not been stopped\n");
+               ATF_REQUIRE(!WIFSTOPPED(status));
+
+               printf("1: Verifying that he child has exited with the "
+                   "%d status (received %d)\n", exitval, WEXITSTATUS(status));
+               ATF_REQUIRE(WEXITSTATUS(status) == exitval);
+
+               printf("1: Before calling waitpid() for the exited child\n");
+               wpid = waitpid(child, &status, 0);
+
+               printf("1: Validating that child's PID no longer exists\n");
+               ATF_REQUIRE(wpid == -1);
+
+               printf("1: Validating that errno is set to %s (got %s)\n",
+                   sys_errlist[ECHILD], sys_errlist[errno]);
+               ATF_REQUIRE(errno == ECHILD);
+       }
+
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+       ATF_TP_ADD_TC(tp, traceme1);
+
+       return atf_no_error();
+}



Home | Main Index | Thread Index | Old Index