1 // SPDX-License-Identifier: GPL-2.0
3 * Test for remove_on_exec.
5 * Copyright (C) 2021, Google LLC.
10 /* We need the latest siginfo from the kernel repo. */
11 #include <sys/types.h>
12 #include <asm/siginfo.h>
13 #define __have_siginfo_t 1
14 #define __have_sigval_t 1
15 #define __have_sigevent_t 1
16 #define __siginfo_t_defined
17 #define __sigval_t_defined
18 #define __sigevent_t_defined
19 #define _BITS_SIGINFO_CONSTS_H 1
20 #define _BITS_SIGEVENT_CONSTS_H 1
26 #include <linux/perf_event.h>
29 #include <sys/ioctl.h>
30 #include <sys/syscall.h>
33 #include "../kselftest_harness.h"
35 static volatile int signal_count
;
37 static struct perf_event_attr
make_event_attr(void)
39 struct perf_event_attr attr
= {
40 .type
= PERF_TYPE_HARDWARE
,
42 .config
= PERF_COUNT_HW_INSTRUCTIONS
,
43 .sample_period
= 1000,
49 * Children normally retain their inherited event on exec; with
50 * remove_on_exec, we'll remove their event, but the parent and
51 * any other non-exec'd children will keep their events.
59 static void sigtrap_handler(int signum
, siginfo_t
*info
, void *ucontext
)
61 if (info
->si_code
!= TRAP_PERF
) {
62 fprintf(stderr
, "%s: unexpected si_code %d\n", __func__
, info
->si_code
);
69 FIXTURE(remove_on_exec
)
71 struct sigaction oldact
;
75 FIXTURE_SETUP(remove_on_exec
)
77 struct perf_event_attr attr
= make_event_attr();
78 struct sigaction action
= {};
82 /* Initialize sigtrap handler. */
83 action
.sa_flags
= SA_SIGINFO
| SA_NODEFER
;
84 action
.sa_sigaction
= sigtrap_handler
;
85 sigemptyset(&action
.sa_mask
);
86 ASSERT_EQ(sigaction(SIGTRAP
, &action
, &self
->oldact
), 0);
88 /* Initialize perf event. */
89 self
->fd
= syscall(__NR_perf_event_open
, &attr
, 0, -1, -1, PERF_FLAG_FD_CLOEXEC
);
90 ASSERT_NE(self
->fd
, -1);
93 FIXTURE_TEARDOWN(remove_on_exec
)
96 sigaction(SIGTRAP
, &self
->oldact
, NULL
);
99 /* Verify event propagates to fork'd child. */
100 TEST_F(remove_on_exec
, fork_only
)
106 ASSERT_EQ(signal_count
, 0);
107 ASSERT_EQ(ioctl(self
->fd
, PERF_EVENT_IOC_ENABLE
, 0), 0);
108 while (!signal_count
);
112 while (!signal_count
); /* Child enables event. */
113 EXPECT_EQ(waitpid(pid
, &status
, 0), pid
);
114 EXPECT_EQ(WEXITSTATUS(status
), 42);
118 * Verify that event does _not_ propagate to fork+exec'd child; event enabled
121 TEST_F(remove_on_exec
, fork_exec_then_enable
)
123 pid_t pid_exec
, pid_only_fork
;
128 * Non-exec child, to ensure exec does not affect inherited events of
131 pid_only_fork
= fork();
132 if (pid_only_fork
== 0) {
133 /* Block until parent enables event. */
134 while (!signal_count
);
138 ASSERT_NE(pipe(pipefd
), -1);
141 ASSERT_NE(dup2(pipefd
[1], STDOUT_FILENO
), -1);
143 execl("/proc/self/exe", "exec_child", NULL
);
144 _exit((perror("exec failed"), 1));
148 ASSERT_EQ(waitpid(pid_exec
, &tmp
, WNOHANG
), 0); /* Child is running. */
149 /* Wait for exec'd child to start spinning. */
150 EXPECT_EQ(read(pipefd
[0], &tmp
, sizeof(int)), sizeof(int));
153 /* Now we can enable the event, knowing the child is doing work. */
154 EXPECT_EQ(ioctl(self
->fd
, PERF_EVENT_IOC_ENABLE
, 0), 0);
155 /* If the event propagated to the exec'd child, it will exit normally... */
156 usleep(100000); /* ... give time for event to trigger (in case of bug). */
157 EXPECT_EQ(waitpid(pid_exec
, &tmp
, WNOHANG
), 0); /* Should still be running. */
158 EXPECT_EQ(kill(pid_exec
, SIGKILL
), 0);
160 /* Verify removal from child did not affect this task's event. */
162 while (signal_count
== tmp
); /* Should not hang! */
163 /* Nor should it have affected the first child. */
164 EXPECT_EQ(waitpid(pid_only_fork
, &tmp
, 0), pid_only_fork
);
165 EXPECT_EQ(WEXITSTATUS(tmp
), 42);
169 * Verify that event does _not_ propagate to fork+exec'd child; event enabled
172 TEST_F(remove_on_exec
, enable_then_fork_exec
)
177 EXPECT_EQ(ioctl(self
->fd
, PERF_EVENT_IOC_ENABLE
, 0), 0);
181 execl("/proc/self/exe", "exec_child", NULL
);
182 _exit((perror("exec failed"), 1));
186 * The child may exit abnormally at any time if the event propagated and
187 * a SIGTRAP is sent before the handler was set up.
189 usleep(100000); /* ... give time for event to trigger (in case of bug). */
190 EXPECT_EQ(waitpid(pid_exec
, &tmp
, WNOHANG
), 0); /* Should still be running. */
191 EXPECT_EQ(kill(pid_exec
, SIGKILL
), 0);
193 /* Verify removal from child did not affect this task's event. */
195 while (signal_count
== tmp
); /* Should not hang! */
198 TEST_F(remove_on_exec
, exec_stress
)
203 for (i
= 0; i
< sizeof(pids
) / sizeof(pids
[0]); i
++) {
206 execl("/proc/self/exe", "exec_child", NULL
);
207 _exit((perror("exec failed"), 1));
210 /* Some forked with event disabled, rest with enabled. */
212 EXPECT_EQ(ioctl(self
->fd
, PERF_EVENT_IOC_ENABLE
, 0), 0);
215 usleep(100000); /* ... give time for event to trigger (in case of bug). */
217 for (i
= 0; i
< sizeof(pids
) / sizeof(pids
[0]); i
++) {
218 /* All children should still be running. */
219 EXPECT_EQ(waitpid(pids
[i
], &tmp
, WNOHANG
), 0);
220 EXPECT_EQ(kill(pids
[i
], SIGKILL
), 0);
223 /* Verify event is still alive. */
225 while (signal_count
== tmp
);
228 /* For exec'd child. */
229 static void exec_child(void)
231 struct sigaction action
= {};
234 /* Set up sigtrap handler in case we erroneously receive a trap. */
235 action
.sa_flags
= SA_SIGINFO
| SA_NODEFER
;
236 action
.sa_sigaction
= sigtrap_handler
;
237 sigemptyset(&action
.sa_mask
);
238 if (sigaction(SIGTRAP
, &action
, NULL
))
239 _exit((perror("sigaction failed"), 1));
241 /* Signal parent that we're starting to spin. */
242 if (write(STDOUT_FILENO
, &val
, sizeof(int)) == -1)
243 _exit((perror("write failed"), 1));
245 /* Should hang here until killed. */
246 while (!signal_count
);
249 #define main test_main
252 int main(int argc
, char *argv
[])
254 if (!strcmp(argv
[0], "exec_child")) {
259 return test_main(argc
, argv
);