Skip to content

Commit 16fa499

Browse files
Kenoclaude
authored andcommitted
Don't resume a task killed while we prepare its exit()
A SIGKILL while rr prepared a task's exit(), blocking its signals and unmapping its buffers, made rr resume the task out of its PTRACE_EVENT_EXIT stop, after which recording failed. Don't restart the exit once the task has died, and don't resume a task that died in rec_prepare_syscall(). The new kill point RRCALL_KILL_IN_EXIT_PREPARATION lets tests send that SIGKILL. Test: exit_killed_during_preparation Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1005e4a commit 16fa499

5 files changed

Lines changed: 85 additions & 0 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,7 @@ set(BASIC_TESTS
10931093
exec_killed_before_exit_event
10941094
exec_stopsig
10951095
execveat
1096+
exit_killed_during_preparation
10961097
exit_with_syscallbuf_signal
10971098
fadvise
10981099
fatal_init_signal

‎src/RecordSession.cc‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,14 @@ void RecordSession::syscall_state_changed(RecordTask* t,
12261226
debug_exec_state("after cont", t);
12271227
t->ev().Syscall().state = PROCESSING_SYSCALL;
12281228

1229+
if (t->is_exiting()) {
1230+
// The task was killed (e.g. by SIGKILL) while rec_prepare_syscall()
1231+
// was running it. Don't resume it out of its PTRACE_EVENT_EXIT stop;
1232+
// the next record_step() handles the exit.
1233+
step_state->continue_type = DONT_CONTINUE;
1234+
return;
1235+
}
1236+
12291237
if (t->emulated_stop_pending) {
12301238
t->held_at_emulated_ptrace_event = true;
12311239
step_state->continue_type = DONT_CONTINUE;

‎src/preload/rrcalls.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,16 @@ enum rrcall_kill_point {
255255
the task is dying. */
256256
RRCALL_KILL_BEFORE_SYSCALL_PATCH,
257257

258+
/* exit() and exit_group() */
259+
260+
/* A task in exit(), or in exit_group() if it's the only task in its
261+
thread group, while rr prepares the exit, at the step given by `arg`:
262+
1, just before rr makes the task block all signals; 2, just before rr
263+
unmaps the task's syscall buffer and scratch memory and closes rr's
264+
file descriptors in it; 3, just before rr restarts the exit. rr waits
265+
until the task has reached its PTRACE_EVENT_EXIT stop (or has exited),
266+
without collecting that stop. */
267+
RRCALL_KILL_IN_EXIT_PREPARATION,
268+
258269
RRCALL_KILL_NUM_POINTS
259270
};

‎src/record_syscall.cc‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
#include "SysCpuMonitor.h"
9696
#include "TraceStream.h"
9797
#include "VirtualPerfCounterMonitor.h"
98+
#include "WaitManager.h"
9899
#include "cpp_supplement.h"
99100
#include "ftrace.h"
100101
#include "kernel_abi.h"
@@ -3461,6 +3462,27 @@ static bool send_signal_during_init_buffers() {
34613462
return send;
34623463
}
34633464

3465+
/**
3466+
* This is purely for testing purposes. See RRCALL_KILL_IN_EXIT_PREPARATION.
3467+
* If a kill armed for `t` at `step` of prepare_exit() fires now, SIGKILL `t`
3468+
* and wait until it has reached its PTRACE_EVENT_EXIT stop (or has exited),
3469+
* leaving that stop for prepare_exit() to find.
3470+
*/
3471+
static void maybe_kill_task_in_exit_preparation(RecordTask* t, int step) {
3472+
if (t->is_exiting() ||
3473+
!t->take_armed_kill(RRCALL_KILL_IN_EXIT_PREPARATION, step)) {
3474+
return;
3475+
}
3476+
t->tgkill(SIGKILL);
3477+
WaitOptions options(t->tid);
3478+
options.consume = false;
3479+
WaitResult result;
3480+
do {
3481+
result = WaitManager::wait_stop_or_exit(options);
3482+
} while (result.code == WAIT_NO_STATUS);
3483+
ASSERT(t, result.code == WAIT_OK) << "Task disappeared";
3484+
}
3485+
34643486
/**
34653487
* At thread exit time, undo the work that init_buffers() did.
34663488
*
@@ -3502,15 +3524,22 @@ static void prepare_exit(RecordTask* t) {
35023524
r.set_arg3(0);
35033525
r.set_arg4(sizeof(sig_set_t));
35043526
t->set_regs(r);
3527+
maybe_kill_task_in_exit_preparation(t, 1);
35053528
// This exits the SYS_rt_sigprocmask. Now the tracee is ready to do our
35063529
// bidding.
35073530
t->exit_syscall(arch);
35083531
check_signals_while_exiting(t);
35093532

35103533
// Do the actual buffer and fd cleanup.
3534+
maybe_kill_task_in_exit_preparation(t, 2);
35113535
t->destroy_buffers();
35123536

35133537
check_signals_while_exiting(t);
3538+
if (t->is_exiting()) {
3539+
// The task was killed (e.g. by SIGKILL) while we were preparing its exit.
3540+
// Don't resume it out of its PTRACE_EVENT_EXIT stop.
3541+
return;
3542+
}
35143543

35153544
// Restore these regs to what they would have been just before
35163545
// the tracee trapped at SYS_exit/SYS_exit_group. When we've finished
@@ -3522,6 +3551,7 @@ static void prepare_exit(RecordTask* t) {
35223551
<< "Tracee should have entered through int $0x80.";
35233552
// Restart the SYS_exit call.
35243553
t->set_regs(exit_regs);
3554+
maybe_kill_task_in_exit_preparation(t, 3);
35253555
t->enter_syscall(arch);
35263556
check_signals_while_exiting(t);
35273557

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* -*- Mode: C; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
2+
3+
#include "util_internal.h"
4+
5+
/* For each step of rr's preparation of a task's exit (see
6+
RRCALL_KILL_IN_EXIT_PREPARATION), we have rr SIGKILL a child at that step
7+
of its exit(), and another at that step of its exit_group(). */
8+
9+
int main(void) {
10+
/* This arms nothing; it tells us whether we're running under rr. */
11+
int under_rr = rr_arm_kill(RRCALL_KILL_NONE, 0, 0);
12+
int step;
13+
int use_exit_group;
14+
15+
for (step = 1; step <= 3; ++step) {
16+
for (use_exit_group = 0; use_exit_group <= 1; ++use_exit_group) {
17+
int status;
18+
pid_t child = fork();
19+
if (!child) {
20+
rr_arm_kill(RRCALL_KILL_IN_EXIT_PREPARATION, step, 0);
21+
syscall(use_exit_group ? SYS_exit_group : SYS_exit, 0);
22+
test_assert(0 && "exit returned");
23+
}
24+
test_assert(child == waitpid(child, &status, 0));
25+
if (under_rr) {
26+
test_assert(WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL);
27+
} else {
28+
test_assert(WIFEXITED(status) && WEXITSTATUS(status) == 0);
29+
}
30+
}
31+
}
32+
33+
atomic_puts("EXIT-SUCCESS");
34+
return 0;
35+
}

0 commit comments

Comments
 (0)