[PATCH 4/4] tests: check syscall fault injection behavior and error handling

Nahim El Atmani nahim+dev at naam.me
Wed Jul 27 18:41:17 UTC 2016


From: Nahim El Atmani <naam at lse.epita.fr>

From: Nahim El Atmani <nahim+dev at naam.me>

* tests/fault.c: New file.
* tests/is_x86.c: New file.
* tests/fault.test: New test.
* tests/fault_parsing.test: Likewise.
* tests/Makefile.am (check_PROGRAMS): Add fault, is_x86.
  (DECODER_TESTS): Add fault.test.
  (MISC_TESTS): Add fault_parsing.test

Signed-off-by: Nahim El Atmani <nahim+dev at naam.me>
Reviewed-By: Gabriel Laskar <gabriel at lse.epita.fr>
---
This check serie is divided into two files. The 'fault.test` check that syscall
fault injection actually works as expected while 'fault_parsing.test` is only
run on x86 platforms to ensure a good option parsing. There is no need to
derive this test for every architecture since the parsing will remain the same.
The program 'is_x86` helps at finding the correct personality if the
architecture is x86 or skiping the test otherwise.

 tests/Makefile.am        |  13 ++++++
 tests/fault.c            | 112 +++++++++++++++++++++++++++++++++++++++++++++++
 tests/fault.test         |  37 ++++++++++++++++
 tests/fault_parsing.test |  65 +++++++++++++++++++++++++++
 tests/is_x86.c           |  42 ++++++++++++++++++
 5 files changed, 269 insertions(+)
 create mode 100644 tests/fault.c
 create mode 100755 tests/fault.test
 create mode 100755 tests/fault_parsing.test
 create mode 100644 tests/is_x86.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9023029..07fd706 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -170,6 +170,7 @@ check_PROGRAMS = \
 	ipc_msgbuf \
 	ipc_sem \
 	ipc_shm \
+	is_x86 \
 	kill \
 	ksysent \
 	lchown \
@@ -364,6 +365,10 @@ check_PROGRAMS = \
 	xettimeofday \
 	# end of check_PROGRAMS
 
+if ENABLE_FAULT_INJECTION
+check_PROGRAMS += fault
+endif
+
 attach_f_p_LDADD = -lrt -lpthread $(LDADD)
 clock_xettime_LDADD = -lrt $(LDADD)
 count_f_LDADD = -lpthread $(LDADD)
@@ -684,6 +689,10 @@ DECODER_TESTS = \
 	xettimeofday.test \
 	# end of DECODER_TESTS
 
+if ENABLE_FAULT_INJECTION
+DECODER_TESTS += fault.test
+endif
+
 MISC_TESTS = \
 	attach-f-p.test \
 	attach-p-cmd.test \
@@ -714,6 +723,10 @@ MISC_TESTS = \
 	vfork-f.test \
 	# end of MISC_TESTS
 
+if ENABLE_FAULT_INJECTION
+MISC_TESTS += fault_parsing.test
+endif
+
 TESTS = $(DECODER_TESTS) $(MISC_TESTS) $(LIBUNWIND_TESTS)
 
 XFAIL_TESTS_ =
diff --git a/tests/fault.c b/tests/fault.c
new file mode 100644
index 0000000..398ca30
--- /dev/null
+++ b/tests/fault.c
@@ -0,0 +1,112 @@
+/*
+ * This file is part of fault injection tests.
+ *
+ * Copyright (c) 2016 Nahim El Atmani <nahim+dev at naam.me>
+ * 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.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 "tests.h"
+#include <sys/syscall.h>
+
+#ifdef __NR_kill
+
+# include <signal.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+# include <unistd.h>
+
+static void
+handler(int sig)
+{
+}
+
+static void
+occ_at(int raw)
+{
+	pid_t pid = getpid();
+	syscall(__NR_kill, pid, SIGALRM);
+	syscall(__NR_kill, pid, SIGALRM);
+	if (!raw)
+		printf("kill(%d, SIGALRM) = 0\n"
+		       "kill(%d, SIGALRM) = -1 EINVAL (Invalid argument)(DISCARDED)\n"
+		       "+++ exited with 0 +++\n", pid, pid);
+	else
+		printf("kill(%d, 0xe) = 0\n"
+		       "kill(%d, 0xe) = -1 (errno 22)(DISCARDED)\n"
+		       "+++ exited with 0 +++\n", pid, pid);
+}
+
+static void
+occ_every(void)
+{
+	pid_t pid = getpid();
+	syscall(__NR_kill, pid, SIGALRM);
+	syscall(__NR_kill, pid, SIGALRM);
+	syscall(__NR_kill, pid, SIGALRM);
+	syscall(__NR_kill, pid, SIGALRM);
+	syscall(__NR_kill, pid, SIGALRM);
+	syscall(__NR_kill, pid, SIGALRM);
+	printf("kill(%d, SIGALRM) = 0\n"
+	       "kill(%d, SIGALRM) = -1 EINVAL (Invalid argument)(DISCARDED)\n"
+	       "kill(%d, SIGALRM) = 0\n"
+	       "kill(%d, SIGALRM) = -1 EINVAL (Invalid argument)(DISCARDED)\n"
+	       "kill(%d, SIGALRM) = 0\n"
+	       "kill(%d, SIGALRM) = -1 EINVAL (Invalid argument)(DISCARDED)\n"
+	       "+++ exited with 0 +++\n", pid, pid, pid, pid, pid, pid);
+}
+
+int
+main(int argc, char *argv[])
+{
+	if (argc != 2)
+		return -EINVAL;
+
+	sigset_t mask;
+	const struct sigaction act = { .sa_handler = handler };
+	if (sigaction(SIGALRM, &act, NULL))
+		perror_msg_and_fail("sigaction");
+	sigemptyset(&mask);
+	sigaddset(&mask, SIGALRM);
+	if (sigprocmask(SIG_UNBLOCK, &mask, NULL))
+		perror_msg_and_fail("sigprocmask");
+
+	if (!strcmp("at", argv[1]))
+		occ_at(0);
+	else if (!strcmp("every", argv[1]))
+		occ_every();
+	else if (!strcmp("raw", argv[1]))
+		occ_at(1);
+	else
+		return -EINVAL;
+
+	return 0;
+}
+
+#else
+
+SKIP_MAIN_UNDEFINED("__NR_kill")
+
+#endif
diff --git a/tests/fault.test b/tests/fault.test
new file mode 100755
index 0000000..ec10500
--- /dev/null
+++ b/tests/fault.test
@@ -0,0 +1,37 @@
+#!/bin/sh
+# This file is part of fault injection tests.
+#
+# Copyright (c) 2016 Nahim El Atmani <nahim+dev at naam.me>
+# 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.
+# 3. The name of the author may not be used to endorse or promote products
+#    derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
+#
+# Ensure that strace -e faultwith=<syscall>:<occurrence>:<error>,... works.
+
+. "${srcdir=.}/init.sh"
+
+run_strace -a0 -e trace=kill -e fault=kill:2:EINVAL -e signal=none ./fault at > "$EXP"
+match_diff "$EXP" "$LOG"
+run_strace -a0 -e trace=kill -e fault=kill:2.:EINVAL -e signal=none ./fault every > "$EXP"
+match_diff "$EXP" "$LOG"
+run_strace -a0 -e trace=kill -e raw=kill -e fault=kill:2.:EINVAL -e signal=none ./fault raw > "$EXP"
diff --git a/tests/fault_parsing.test b/tests/fault_parsing.test
new file mode 100755
index 0000000..b150cb8
--- /dev/null
+++ b/tests/fault_parsing.test
@@ -0,0 +1,65 @@
+#!/bin/sh
+# This file is part of fault injection tests.
+#
+# Copyright (c) 2016 Nahim El Atmani <nahim+dev at naam.me>
+# 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.
+# 3. The name of the author may not be used to endorse or promote products
+#    derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
+#
+# Ensure that strace -e faultwith=<syscall>:<occurrence>:<error>,... works.
+
+. "${srcdir=.}/init.sh"
+
+INV_SC_NAME="invalid_syscall_name"
+INV_ER_NAME="invalid_error_name"
+INV_OCC_NBR1="-1"
+INV_OCC_NBR2="105%"
+ERR_MSG='invalid fault argument'
+$(./is_x86)
+X86=$?
+
+if [ ! $X86 ] # Parser check
+then
+    skip_ "fault parsing not available for non x86 architecture"
+else
+    if [ $X86 == 1 ] # 32 bit
+    then
+	run_strace -a0 -e trace=kill -e fault=37:2:22 -e signal=none ./fault at > "$EXP"
+	match_diff "$EXP" "$LOG"
+    elif [ $X86 == 2 ] # 64 bit
+    then
+	run_strace -a0 -e trace=kill -e fault=62:2:22 -e signal=none ./fault at > "$EXP"
+	match_diff "$EXP" "$LOG"
+    fi
+    rm $EXP $LOG
+    $STRACE -a0 -e trace=kill -e fault="$INV_SC_NAME":2:EINVAL ./fault at 2> "$LOG"
+    grep -v "$ERR_MSG" < "$LOG" && dump_log_and_fail_with 'strace-fault failed'
+    $STRACE -a0 -e trace=kill -e fault=kill:2:"$INV_ER_NAME" ./fault at 2> "$LOG"
+    grep -v "$ERR_MSG" < "$LOG" && dump_log_and_fail_with 'strace-fault failed'
+    $STRACE -a0 -e trace=kill -e fault=kill:"$INV_OCC_NBR1":EAGAIN ./fault at 2> "$LOG"
+    grep -v "$ERR_MSG" < "$LOG" && dump_log_and_fail_with 'strace-fault failed'
+    $STRACE -a0 -e trace=kill -e fault=kill:"$INV_OCC_NBR2":EAGAIN ./fault at 2> "$LOG"
+    grep -v "$ERR_MSG" < "$LOG" && dump_log_and_fail_with 'strace-fault failed'
+fi
+
+exit 0
diff --git a/tests/is_x86.c b/tests/is_x86.c
new file mode 100644
index 0000000..bbc6936
--- /dev/null
+++ b/tests/is_x86.c
@@ -0,0 +1,42 @@
+/*
+ * This file is part of fault injection strace test.
+ *
+ * Copyright (c) 2016 Nahim El Atmani <nahim+dev at naam.me>
+ * 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.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 "tests.h"
+
+int
+main(void)
+{
+#if defined __i386__
+	return 1;
+#elif defined __x86_64__
+	return 2;
+#else
+	return 0;
+#endif
+}
-- 
Nahim El Atmani





More information about the Strace-devel mailing list