@@ -761,46 +761,7 @@ bool RecordSession::handle_ptrace_event(RecordTask** t_ptr,
761761 }
762762
763763 case PTRACE_EVENT_EXEC : {
764- if (t->thread_group ()->task_set ().size () > 1 ) {
765- // All tasks but the task that did the execve should have exited by
766- // now and notified us of their exits. However, it's possible that
767- // while running the thread-group leader, our PTRACE_CONT raced with its
768- // PTRACE_EVENT_EXIT and it exited, and the next event we got is this
769- // PTRACE_EVENT_EXEC after the exec'ing task changed its tid to the
770- // leader's tid. Or maybe there are kernel bugs; on
771- // 4.2.0-42-generic running exec_from_other_thread, we reproducibly
772- // enter PTRACE_EVENT_EXEC for the thread-group leader without seeing
773- // its PTRACE_EVENT_EXIT.
774-
775- // So, record this task's exit and destroy it.
776- // XXX We can't do record_robust_futex_changes here because the address
777- // space has already gone. That would only matter if some of them were
778- // in memory accessible to another process even after exec, i.e. a
779- // shared-memory mapping or two different thread-groups sharing the same
780- // address space.
781- pid_t tid = t->rec_tid ;
782- WaitStatus status = t->status ();
783- t->record_exit_trace_event (WaitStatus (0 ));
784- t->record_exit_event ();
785- // Don't call RecordTask::destroy() because we don't want to
786- // PTRACE_DETACH.
787- delete t;
788- // Steal the exec'ing task and make it the thread-group leader, and
789- // carry on!
790- t = revive_task_for_exec (tid);
791- scheduler ().set_current (t);
792- *t_ptr = t;
793- // Tell t that it is actually stopped, because the stop we got is really
794- // for this task, not the old dead task.
795- if (!t->did_waitpid (status)) {
796- // This is totally untested and almost certainly broken, but if the
797- // task was SIGKILLed out of the EXEC stop then we should probably
798- // just pretend the exec never happened.
799- step_state->continue_type = CONTINUE_SYSCALL ;
800- break ;
801- }
802- }
803- t->post_exec ();
764+ // record_step() has called switch_to_exec_image() for this stop.
804765 t->session ().scheduler ().did_exit_execve (t);
805766
806767 // This is purely for testing purposes. See RRCALL_KILL_AFTER_EXEC_STOP.
@@ -1476,8 +1437,7 @@ bool RecordSession::kill_exec_task_for_testing(pid_t rec_tid, int point) {
14761437 // only serializes them later on), so try each of them.
14771438 for (Task* tt : tg->task_set ()) {
14781439 RecordTask* rt = static_cast <RecordTask*>(tt);
1479- if (rt->ev ().is_syscall_event () && rt->ev ().Syscall ().is_exec () &&
1480- rt->take_armed_kill (point)) {
1440+ if (rt->is_in_execve () && rt->take_armed_kill (point)) {
14811441 syscall (SYS_tgkill, rec_tid, rec_tid, SIGKILL );
14821442 if (point == RRCALL_KILL_BEFORE_EXEC_GETEVENTMSG_AT_EXIT_STOP ) {
14831443 WaitOptions options (rec_tid);
@@ -1532,6 +1492,55 @@ RecordTask* RecordSession::revive_task_for_exec(pid_t rec_tid) {
15321492 return t;
15331493}
15341494
1495+ void RecordSession::dispose_of_leader_killed_by_exec (RecordTask* leader) {
1496+ if (!leader->handled_ptrace_exit_event ()) {
1497+ // We haven't handled the leader's PTRACE_EVENT_EXIT. Either it never
1498+ // stopped there, because it was already exiting (e.g. in SYS_exit or
1499+ // exit_group) when the exec killed it, or the exec's SIGKILL took it out
1500+ // of that stop before we handled it. Record its exit now.
1501+ // did_die_in_exec() marks the exit as handled, which ~Task() requires,
1502+ // and the task as reaped, so we don't touch it through its tid, which
1503+ // now refers to the new image; e.g. record_event() leaves the leader's
1504+ // rseq area alone.
1505+ // The ticks hpc.stop() reads are lost, as in handle_ptrace_exit_event();
1506+ // replay doesn't check them for EV_EXIT.
1507+ // XXX We can't do record_robust_futex_changes here because the address
1508+ // space has already gone. That would only matter if some of them were
1509+ // in memory accessible to another process even after exec, i.e. a
1510+ // shared-memory mapping or two different thread-groups sharing the same
1511+ // address space.
1512+ leader->did_die_in_exec ();
1513+ leader->hpc .stop (leader);
1514+ leader->record_exit_trace_event (WaitStatus (0 ));
1515+ leader->record_exit_event ();
1516+ }
1517+ // Don't call RecordTask::destroy() because we don't want to PTRACE_DETACH.
1518+ delete leader;
1519+ }
1520+
1521+ RecordTask* RecordSession::switch_to_exec_image (RecordTask* t) {
1522+ if (!t->is_in_execve ()) {
1523+ // t isn't in an execve, so another thread of t's thread group did the
1524+ // exec. The exec killed t, the thread-group leader, and the thread that
1525+ // exec'd has taken over t's tid; we collected its exec stop for t, e.g.
1526+ // because we hadn't handled t's exit yet.
1527+ pid_t tid = t->tid ;
1528+ WaitStatus status = t->status ();
1529+ dispose_of_leader_killed_by_exec (t);
1530+ t = revive_task_for_exec (tid);
1531+ scheduler ().set_current (t);
1532+ // The stop we got is really for this task.
1533+ if (!t->did_waitpid (status)) {
1534+ return nullptr ;
1535+ }
1536+ }
1537+ if (!t->did_post_exec_at_exec_stop ) {
1538+ t->post_exec ();
1539+ t->did_post_exec_at_exec_stop = true ;
1540+ }
1541+ return t;
1542+ }
1543+
15351544/* *
15361545 * Take a NativeArch::siginfo_t& here instead of siginfo_t because different
15371546 * versions of system headers have inconsistent field naming.
@@ -2778,6 +2787,21 @@ RecordSession::RecordResult RecordSession::record_step() {
27782787 }
27792788 prev_task->pop_event (EV_SCHED );
27802789 }
2790+ // This comes before switch_to_exec_image(): at the initial exec stop,
2791+ // post_exec() sets done_initial_exec(), which ends these kills.
2792+ maybe_kill_task_before_initial_exec (t);
2793+ if (rescheduled.by_waitpid && t->ptrace_event () == PTRACE_EVENT_EXEC ) {
2794+ // t's tid now refers to the new image. Switch to it before anything
2795+ // (e.g. will_schedule() writing to the rseq area) uses the old image's
2796+ // state through that tid.
2797+ t = switch_to_exec_image (t);
2798+ if (!t) {
2799+ // The task that did the exec was SIGKILLed out of the exec stop.
2800+ // Pretend this stop never happened and wait for its exit event.
2801+ last_task_switchable = ALLOW_SWITCH ;
2802+ return result;
2803+ }
2804+ }
27812805 if (t->tuid () != prev_task_tuid) {
27822806 t->will_schedule ();
27832807 }
@@ -2792,7 +2816,6 @@ RecordSession::RecordResult RecordSession::record_step() {
27922816 t->log_pending_events ();
27932817 }
27942818
2795- maybe_kill_task_before_initial_exec (t);
27962819 if (handle_ptrace_exit_event (t)) {
27972820 // t may have been deleted.
27982821 last_task_switchable = ALLOW_SWITCH ;
0 commit comments