aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiri Olsa <jolsa@kernel.org>2018-08-27 05:12:24 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2018-08-30 13:49:22 -0400
commit9b3579fc6c6ac45502de1fa9a1fdf873805c2157 (patch)
tree814a9fab3d86ed9fe0944b39a73403bafe38d06d
parent1dc27f63303db58ce1b1a6932d1825305f86d574 (diff)
perf tests: Add breakpoint modify tests
Adding to tests that aims on kernel breakpoint modification bugs. First test creates HW breakpoint, tries to change it and checks it was properly changed. It aims on kernel issue that prevents HW breakpoint to be changed via ptrace interface. The first test forks, the child sets itself as ptrace tracee and waits in signal for parent to trace it, then it calls bp_1 and quits. The parent does following steps: - creates a new breakpoint (id 0) for bp_2 function - changes that breakpoint to bp_1 function - waits for the breakpoint to hit and checks it has proper rip of bp_1 function This test aims on an issue in kernel preventing to change disabled breakpoints Second test mimics the first one except for few steps in the parent: - creates a new breakpoint (id 0) for bp_1 function - changes that breakpoint to bogus (-1) address - waits for the breakpoint to hit and checks it has proper rip of bp_1 function This test aims on an issue in kernel disabling enabled breakpoint after unsuccesful change. Committer testing: # uname -a Linux jouet 4.18.0-rc8-00002-g1236568ee3cb #12 SMP Tue Aug 7 14:08:26 -03 2018 x86_64 x86_64 x86_64 GNU/Linux # perf test -v "bp modify" 62: x86 bp modify : --- start --- test child forked, pid 25671 in bp_1 tracee exited prematurely 2 FAILED arch/x86/tests/bp-modify.c:209 modify test 1 failed test child finished with -1 ---- end ---- x86 bp modify: FAILED! # Signed-off-by: Jiri Olsa <jolsa@kernel.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Milind Chabbi <chabbi.milind@gmail.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/20180827091228.2878-2-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r--tools/perf/arch/x86/include/arch-tests.h1
-rw-r--r--tools/perf/arch/x86/tests/Build1
-rw-r--r--tools/perf/arch/x86/tests/arch-tests.c6
-rw-r--r--tools/perf/arch/x86/tests/bp-modify.c213
4 files changed, 221 insertions, 0 deletions
diff --git a/tools/perf/arch/x86/include/arch-tests.h b/tools/perf/arch/x86/include/arch-tests.h
index c1bd979b957b..613709cfbbd0 100644
--- a/tools/perf/arch/x86/include/arch-tests.h
+++ b/tools/perf/arch/x86/include/arch-tests.h
@@ -9,6 +9,7 @@ struct test;
9int test__rdpmc(struct test *test __maybe_unused, int subtest); 9int test__rdpmc(struct test *test __maybe_unused, int subtest);
10int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest); 10int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest);
11int test__insn_x86(struct test *test __maybe_unused, int subtest); 11int test__insn_x86(struct test *test __maybe_unused, int subtest);
12int test__bp_modify(struct test *test, int subtest);
12 13
13#ifdef HAVE_DWARF_UNWIND_SUPPORT 14#ifdef HAVE_DWARF_UNWIND_SUPPORT
14struct thread; 15struct thread;
diff --git a/tools/perf/arch/x86/tests/Build b/tools/perf/arch/x86/tests/Build
index 8e2c5a38c3b9..586849ff83a0 100644
--- a/tools/perf/arch/x86/tests/Build
+++ b/tools/perf/arch/x86/tests/Build
@@ -5,3 +5,4 @@ libperf-y += arch-tests.o
5libperf-y += rdpmc.o 5libperf-y += rdpmc.o
6libperf-y += perf-time-to-tsc.o 6libperf-y += perf-time-to-tsc.o
7libperf-$(CONFIG_AUXTRACE) += insn-x86.o 7libperf-$(CONFIG_AUXTRACE) += insn-x86.o
8libperf-$(CONFIG_X86_64) += bp-modify.o
diff --git a/tools/perf/arch/x86/tests/arch-tests.c b/tools/perf/arch/x86/tests/arch-tests.c
index cc1802ff5410..d47d3f8e3c8e 100644
--- a/tools/perf/arch/x86/tests/arch-tests.c
+++ b/tools/perf/arch/x86/tests/arch-tests.c
@@ -24,6 +24,12 @@ struct test arch_tests[] = {
24 .func = test__insn_x86, 24 .func = test__insn_x86,
25 }, 25 },
26#endif 26#endif
27#if defined(__x86_64__)
28 {
29 .desc = "x86 bp modify",
30 .func = test__bp_modify,
31 },
32#endif
27 { 33 {
28 .func = NULL, 34 .func = NULL,
29 }, 35 },
diff --git a/tools/perf/arch/x86/tests/bp-modify.c b/tools/perf/arch/x86/tests/bp-modify.c
new file mode 100644
index 000000000000..f53e4406709f
--- /dev/null
+++ b/tools/perf/arch/x86/tests/bp-modify.c
@@ -0,0 +1,213 @@
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/compiler.h>
3#include <sys/types.h>
4#include <sys/wait.h>
5#include <sys/user.h>
6#include <syscall.h>
7#include <unistd.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <sys/ptrace.h>
11#include <asm/ptrace.h>
12#include <errno.h>
13#include "debug.h"
14#include "tests/tests.h"
15#include "arch-tests.h"
16
17static noinline int bp_1(void)
18{
19 pr_debug("in %s\n", __func__);
20 return 0;
21}
22
23static noinline int bp_2(void)
24{
25 pr_debug("in %s\n", __func__);
26 return 0;
27}
28
29static int spawn_child(void)
30{
31 int child = fork();
32
33 if (child == 0) {
34 /*
35 * The child sets itself for as tracee and
36 * waits in signal for parent to trace it,
37 * then it calls bp_1 and quits.
38 */
39 int err = ptrace(PTRACE_TRACEME, 0, NULL, NULL);
40
41 if (err) {
42 pr_debug("failed to PTRACE_TRACEME\n");
43 exit(1);
44 }
45
46 raise(SIGCONT);
47 bp_1();
48 exit(0);
49 }
50
51 return child;
52}
53
54/*
55 * This tests creates HW breakpoint, tries to
56 * change it and checks it was properly changed.
57 */
58static int bp_modify1(void)
59{
60 pid_t child;
61 int status;
62 unsigned long rip = 0, dr7 = 1;
63
64 child = spawn_child();
65
66 waitpid(child, &status, 0);
67 if (WIFEXITED(status)) {
68 pr_debug("tracee exited prematurely 1\n");
69 return TEST_FAIL;
70 }
71
72 /*
73 * The parent does following steps:
74 * - creates a new breakpoint (id 0) for bp_2 function
75 * - changes that breakponit to bp_1 function
76 * - waits for the breakpoint to hit and checks
77 * it has proper rip of bp_1 function
78 * - detaches the child
79 */
80 if (ptrace(PTRACE_POKEUSER, child,
81 offsetof(struct user, u_debugreg[0]), bp_2)) {
82 pr_debug("failed to set breakpoint, 1st time: %s\n",
83 strerror(errno));
84 goto out;
85 }
86
87 if (ptrace(PTRACE_POKEUSER, child,
88 offsetof(struct user, u_debugreg[0]), bp_1)) {
89 pr_debug("failed to set breakpoint, 2nd time: %s\n",
90 strerror(errno));
91 goto out;
92 }
93
94 if (ptrace(PTRACE_POKEUSER, child,
95 offsetof(struct user, u_debugreg[7]), dr7)) {
96 pr_debug("failed to set dr7: %s\n", strerror(errno));
97 goto out;
98 }
99
100 if (ptrace(PTRACE_CONT, child, NULL, NULL)) {
101 pr_debug("failed to PTRACE_CONT: %s\n", strerror(errno));
102 goto out;
103 }
104
105 waitpid(child, &status, 0);
106 if (WIFEXITED(status)) {
107 pr_debug("tracee exited prematurely 2\n");
108 return TEST_FAIL;
109 }
110
111 rip = ptrace(PTRACE_PEEKUSER, child,
112 offsetof(struct user_regs_struct, rip), NULL);
113 if (rip == (unsigned long) -1) {
114 pr_debug("failed to PTRACE_PEEKUSER: %s\n",
115 strerror(errno));
116 goto out;
117 }
118
119 pr_debug("rip %lx, bp_1 %p\n", rip, bp_1);
120
121out:
122 if (ptrace(PTRACE_DETACH, child, NULL, NULL)) {
123 pr_debug("failed to PTRACE_DETACH: %s", strerror(errno));
124 return TEST_FAIL;
125 }
126
127 return rip == (unsigned long) bp_1 ? TEST_OK : TEST_FAIL;
128}
129
130/*
131 * This tests creates HW breakpoint, tries to
132 * change it to bogus value and checks the original
133 * breakpoint is hit.
134 */
135static int bp_modify2(void)
136{
137 pid_t child;
138 int status;
139 unsigned long rip = 0, dr7 = 1;
140
141 child = spawn_child();
142
143 waitpid(child, &status, 0);
144 if (WIFEXITED(status)) {
145 pr_debug("tracee exited prematurely 1\n");
146 return TEST_FAIL;
147 }
148
149 /*
150 * The parent does following steps:
151 * - creates a new breakpoint (id 0) for bp_1 function
152 * - tries to change that breakpoint to (-1) address
153 * - waits for the breakpoint to hit and checks
154 * it has proper rip of bp_1 function
155 * - detaches the child
156 */
157 if (ptrace(PTRACE_POKEUSER, child,
158 offsetof(struct user, u_debugreg[0]), bp_1)) {
159 pr_debug("failed to set breakpoint: %s\n",
160 strerror(errno));
161 goto out;
162 }
163
164 if (ptrace(PTRACE_POKEUSER, child,
165 offsetof(struct user, u_debugreg[7]), dr7)) {
166 pr_debug("failed to set dr7: %s\n", strerror(errno));
167 goto out;
168 }
169
170 if (!ptrace(PTRACE_POKEUSER, child,
171 offsetof(struct user, u_debugreg[0]), (unsigned long) (-1))) {
172 pr_debug("failed, breakpoint set to bogus address\n");
173 goto out;
174 }
175
176 if (ptrace(PTRACE_CONT, child, NULL, NULL)) {
177 pr_debug("failed to PTRACE_CONT: %s\n", strerror(errno));
178 goto out;
179 }
180
181 waitpid(child, &status, 0);
182 if (WIFEXITED(status)) {
183 pr_debug("tracee exited prematurely 2\n");
184 return TEST_FAIL;
185 }
186
187 rip = ptrace(PTRACE_PEEKUSER, child,
188 offsetof(struct user_regs_struct, rip), NULL);
189 if (rip == (unsigned long) -1) {
190 pr_debug("failed to PTRACE_PEEKUSER: %s\n",
191 strerror(errno));
192 goto out;
193 }
194
195 pr_debug("rip %lx, bp_1 %p\n", rip, bp_1);
196
197out:
198 if (ptrace(PTRACE_DETACH, child, NULL, NULL)) {
199 pr_debug("failed to PTRACE_DETACH: %s", strerror(errno));
200 return TEST_FAIL;
201 }
202
203 return rip == (unsigned long) bp_1 ? TEST_OK : TEST_FAIL;
204}
205
206int test__bp_modify(struct test *test __maybe_unused,
207 int subtest __maybe_unused)
208{
209 TEST_ASSERT_VAL("modify test 1 failed\n", !bp_modify1());
210 TEST_ASSERT_VAL("modify test 2 failed\n", !bp_modify2());
211
212 return 0;
213}