Skip to contentSkip to conversation

tracehook: add some self tests

PATCH In review Mike Frysinger 4 participants 6 messages Feb 15, 2010 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracehook: add some self tests

At the moment, we stress test the asm/syscall.h functions.

---
 arch/Kconfig           |    2 +
 kernel/Makefile        |    1 +
 kernel/tracehooktest.c |  134 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/Kconfig.debug      |   15 +++++
 4 files changed, 152 insertions(+), 0 deletions(-)
 create mode 100644 kernel/tracehooktest.c
arch/Kconfig +2 -0
index 7f418bb..9c2add3 100644
@@ -105,6 +105,8 @@ config HAVE_KRETPROBES
 #	TIF_NOTIFY_RESUME	calls tracehook_notify_resume()
 #	signal delivery		calls tracehook_signal_handler()
 #
+#	See CONFIG_TRACEHOOK_SELF_TEST for some test code to help.
+#
 config HAVE_ARCH_TRACEHOOK
 	bool
 
kernel/Makefile +1 -0
index d7c13d2..5b5e4e4 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_FREEZER) += power/
 obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o
 obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o
+obj-$(CONFIG_TRACEHOOK_SELF_TEST) += tracehooktest.o
 obj-$(CONFIG_COMPAT) += compat.o
 obj-$(CONFIG_CGROUPS) += cgroup.o
 obj-$(CONFIG_CGROUP_FREEZER) += cgroup_freezer.o
kernel/tracehooktest.c new file +134 -0

134 lines not on this page · raw message

lib/Kconfig.debug +15 -0
index 139fdbc..34b6793 100644
@@ -788,6 +788,21 @@ config BACKTRACE_SELF_TEST
 
 	  Say N if you are unsure.
 
+config TRACEHOOK_SELF_TEST
+	tristate "Self test for the tracehook code"
+	depends on HAVE_ARCH_TRACEHOOK
+	default n
+	help
+	  This option provides a kernel module that can be used to test
+	  the kernel stack tracehook code. This option is not useful
+	  for distributions or general kernels, but only for kernel
+	  developers working on architecture code.
+
+	  Say N if you are unsure.
+
+	  Choose M here to compile this code as a module. The module will
+	  be called tracehooktest.
+
 config DEBUG_BLOCK_EXT_DEVT
         bool "Force extended block device numbers and spread them"
 	depends on DEBUG_KERNEL
Signature · 1 line
1.7.0
#1 lore raw
Quoted 9 lines · Mike Frysinger
kernel/tracehooktest.c new file +8 -0
+static int __init syscall_test(void)
+{
+	struct pt_regs _regs, *regs = &_regs;
+	long _args[8];
+	long *args = _args + 1;
+	long *sys_args[6], *sa;
+	unsigned int i, n, s;
+

kernel/tracehooktest.c line 32

Quoted 10 lines
kernel/tracehooktest.c new file +10 -0
+	/*
+	 * First find each system register in pt_regs.  We have to assume
+	 * syscall_set_arguments() works with very basic arguments.
+	 */
+	pr_info("TEST: asm/syscall.h: arg offsets: { ");
+
+	for (s = 0; s < 6; ++s)
+		args[s] = s;
+	memset(regs, 0xad, sizeof(*regs));
+	syscall_set_arguments(NULL, regs, 0, 6, args);
                              ^^^^

I am not sure ia64 can tolerate task == NULL. Hmm, even x86 checks task_thread_info(task)->status.

Probably CONFIG_TRACEHOOK_SELF_TEST needs more attention ?

Otherwise, I convinced myself I understand what this code does, and it looks good ;)

Oleg.

#2 lore raw
Quoted 21 lines · Oleg Nesterov
Quoted 8 lines · Mike Frysinger
kernel/tracehooktest.c new file +8 -0
+static int __init syscall_test(void)
+{
+     struct pt_regs _regs, *regs = &_regs;
+     long _args[8];
+     long *args = _args + 1;
+     long *sys_args[6], *sa;
+     unsigned int i, n, s;
+

kernel/tracehooktest.c line 32

Quoted 10 lines
kernel/tracehooktest.c new file +10 -0
+     /*
+      * First find each system register in pt_regs.  We have to assume
+      * syscall_set_arguments() works with very basic arguments.
+      */
+     pr_info("TEST: asm/syscall.h: arg offsets: { ");
+
+     for (s = 0; s < 6; ++s)
+             args[s] = s;
+     memset(regs, 0xad, sizeof(*regs));
+     syscall_set_arguments(NULL, regs, 0, 6, args);

                             ^^^^

Quoted 2 lines

I am not sure ia64 can tolerate task == NULL. Hmm, even x86 checks task_thread_info(task)->status.

is there some task available while the kernel is initializing ? i.e. the test is compiled in ?

if current is valid, i'll just [ab]use that ...
-mike

#3 lore raw
Quoted 4 lines

is there some task available while the kernel is initializing ? i.e. the test is compiled in ?

if current is valid, i'll just [ab]use that ...

I don't think you can win with any kind of abuse on ia64. (For other machines that only test the compat state of the task, any task will do--though perhaps only test half the code.) AFAICT, on ia64 only a real empirical test can work at all. That is, you must really be referring to a task and its task_pt_regs() when that task really has entered the kernel for a syscall.

Thanks,
Roland

#4 lore raw
In reply to Mike Frysinger

This is something of a misnomer, since asm/syscall.h is the only thing you are testing.

As Oleg pointed out, not all arch definitions can be used without a proper task_struct argument. Only ia64 actually needs the task as part of the actual register access. But several others (including x86) look at the task to decide whether to use the 32-bit or 64-bit interpretation of the register values.

I'd make the more general point that this sort of "synthetic" test does not seem very useful. At best, it can test the asm/syscall.h code for being internally consistent--but that doesn't test whether it's really correct. IMHO this is not worth having unless it's an "empirical" test. What I mean by that is one that really uses the asm/syscall.h calls as specified, and in the context specified. So, you'd have to fork a user process and use ptrace on it to get it stopped at a syscall entry. Then you can fetch the arguments, modify them, and look at the arguments it actually passes in to the syscall.

Thanks,
Roland

#5 lore raw
Quoted 2 lines · Roland McGrath

This is something of a misnomer, since asm/syscall.h is the only thing you are testing.

it's meant as a starting point, not the end point. so overtime people can easily extend it.

Quoted 3 lines

I'd make the more general point that this sort of "synthetic" test does not seem very useful.  At best, it can test the asm/syscall.h code for being internally consistent--but that doesn't test whether it's really correct.

i wrote it because i needed it when trying to make sure the various i/n values worked correctly. writing a bit of static code based on just "n" is trivial, but avoiding a nest of code with i/n is a lot harder. and as i noted earlier, there is on code in the kernel that ever calls with i being non-zero.

Quoted 6 lines

IMHO this is not worth having unless it's an "empirical" test.  What I mean by that is one that really uses the asm/syscall.h calls as specified, and in the context specified.  So, you'd have to fork a user process and use ptrace on it to get it stopped at a syscall entry.  Then you can fetch the arguments, modify them, and look at the arguments it actually passes in to the syscall.

that would indeed be useful as a next point
-mike