diff options
author | Simon Marchi <simon.marchi@efficios.com> | 2021-10-21 16:12:04 -0400 |
---|---|---|
committer | Simon Marchi <simon.marchi@efficios.com> | 2021-10-21 16:13:56 -0400 |
commit | 183be222907a6f419bd71f70ee650989026f0188 (patch) | |
tree | ddce1e1b3a84b877e6989acb8bbd424dfff0f3b6 /gdb/gnu-nat.c | |
parent | c360a4732bd7999767616e5f211314510ea677f4 (diff) | |
download | gdb-183be222907a6f419bd71f70ee650989026f0188.zip gdb-183be222907a6f419bd71f70ee650989026f0188.tar.gz gdb-183be222907a6f419bd71f70ee650989026f0188.tar.bz2 |
gdb, gdbserver: make target_waitstatus safe
I stumbled on a bug caused by the fact that a code path read
target_waitstatus::value::sig (expecting it to contain a gdb_signal
value) while target_waitstatus::kind was TARGET_WAITKIND_FORKED. This
meant that the active union field was in fact
target_waitstatus::value::related_pid, and contained a ptid. The read
signal value was therefore garbage, and that caused GDB to crash soon
after. Or, since that GDB was built with ubsan, this nice error
message:
/home/simark/src/binutils-gdb/gdb/linux-nat.c:1271:12: runtime error: load of value 2686365, which is not a valid value for type 'gdb_signal'
Despite being a large-ish change, I think it would be nice to make
target_waitstatus safe against that kind of bug. As already done
elsewhere (e.g. dynamic_prop), validate that the type of value read from
the union matches what is supposed to be the active field.
- Make the kind and value of target_waitstatus private.
- Make the kind initialized to TARGET_WAITKIND_IGNORE on
target_waitstatus construction. This is what most users appear to do
explicitly.
- Add setters, one for each kind. Each setter takes as a parameter the
data associated to that kind, if any. This makes it impossible to
forget to attach the associated data.
- Add getters, one for each associated data type. Each getter
validates that the data type fetched by the user matches the wait
status kind.
- Change "integer" to "exit_status", "related_pid" to "child_ptid",
just because that's more precise terminology.
- Fix all users.
That last point is semi-mechanical. There are a lot of obvious changes,
but some less obvious ones. For example, it's not possible to set the
kind at some point and the associated data later, as some users did.
But in any case, the intent of the code should not change in this patch.
This was tested on x86-64 Linux (unix, native-gdbserver and
native-extended-gdbserver boards). It was built-tested on x86-64
FreeBSD, NetBSD, MinGW and macOS. The rest of the changes to native
files was done as a best effort. If I forgot any place to update in
these files, it should be easy to fix (unless the change happens to
reveal an actual bug).
Change-Id: I0ae967df1ff6e28de78abbe3ac9b4b2ff4ad03b7
Diffstat (limited to 'gdb/gnu-nat.c')
-rw-r--r-- | gdb/gnu-nat.c | 38 |
1 files changed, 16 insertions, 22 deletions
diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c index 67ce00e..5483834 100644 --- a/gdb/gnu-nat.c +++ b/gdb/gnu-nat.c @@ -626,13 +626,13 @@ gnu_nat_target::_proc_free (struct proc *proc) static struct inf * make_inf (void) { - struct inf *inf = XNEW (struct inf); + struct inf *inf = new struct inf; inf->task = 0; inf->threads = 0; inf->threads_up_to_date = 0; inf->pid = 0; - inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS; + inf->wait.status.set_spurious (); inf->wait.thread = 0; inf->wait.exc.handler = MACH_PORT_NULL; inf->wait.exc.reply = MACH_PORT_NULL; @@ -661,7 +661,7 @@ void gnu_nat_target::inf_clear_wait (struct inf *inf) { inf_debug (inf, "clearing wait"); - inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS; + inf->wait.status.set_spurious (); inf->wait.thread = 0; inf->wait.suppress = 0; if (inf->wait.exc.handler != MACH_PORT_NULL) @@ -1326,8 +1326,8 @@ gnu_nat_target::inf_signal (struct inf *inf, enum gdb_signal sig) { struct inf_wait *w = &inf->wait; - if (w->status.kind == TARGET_WAITKIND_STOPPED - && w->status.value.sig == sig + if (w->status.kind () == TARGET_WAITKIND_STOPPED + && w->status.sig () == sig && w->thread && !w->thread->aborted) /* We're passing through the last exception we received. This is kind of bogus, because exceptions are per-thread whereas gdb @@ -1549,7 +1549,7 @@ rewait: /* We're waiting for the inferior to finish execing. */ { struct inf_wait *w = &inf->wait; - enum target_waitkind kind = w->status.kind; + enum target_waitkind kind = w->status.kind (); if (kind == TARGET_WAITKIND_SPURIOUS) /* Since gdb is actually counting the number of times the inferior @@ -1560,7 +1560,7 @@ rewait: inf_debug (inf, "pending_execs, ignoring minor event"); } else if (kind == TARGET_WAITKIND_STOPPED - && w->status.value.sig == GDB_SIGNAL_TRAP) + && w->status.sig () == GDB_SIGNAL_TRAP) /* Ah hah! A SIGTRAP from the inferior while starting up probably means we've succesfully completed an exec! */ { @@ -1585,7 +1585,7 @@ rewait: } /* Pass back out our results. */ - memcpy (status, &inf->wait.status, sizeof (*status)); + *status = inf->wait.status; thread = inf->wait.thread; if (thread) @@ -1608,7 +1608,7 @@ rewait: if (thread && ptid != minus_one_ptid - && status->kind != TARGET_WAITKIND_SPURIOUS + && status->kind () != TARGET_WAITKIND_SPURIOUS && inf->pause_sc == 0 && thread->pause_sc == 0) /* If something actually happened to THREAD, make sure we suspend it. */ @@ -1658,12 +1658,10 @@ S_exception_raise_request (mach_port_t port, mach_port_t reply_port, /* Store away the details; this will destroy any previous info. */ inf->wait.thread = thread; - inf->wait.status.kind = TARGET_WAITKIND_STOPPED; - if (exception == EXC_BREAKPOINT) /* GDB likes to get SIGTRAP for breakpoints. */ { - inf->wait.status.value.sig = GDB_SIGNAL_TRAP; + inf->wait.status.set_stopped (GDB_SIGNAL_TRAP); mach_port_deallocate (mach_task_self (), reply_port); } else @@ -1696,8 +1694,8 @@ S_exception_raise_request (mach_port_t port, mach_port_t reply_port, /* Exceptions are encoded in the signal space by putting them after _NSIG; this assumes they're positive (and not extremely large)! */ - inf->wait.status.value.sig = - gdb_signal_from_host (_NSIG + exception); + inf->wait.status.set_stopped + (gdb_signal_from_host (_NSIG + exception)); } } else @@ -1718,8 +1716,7 @@ inf_task_died_status (struct inf *inf) { warning (_("Pid %d died with unknown exit status, using SIGKILL."), inf->pid); - inf->wait.status.kind = TARGET_WAITKIND_SIGNALLED; - inf->wait.status.value.sig = GDB_SIGNAL_KILL; + inf->wait.status.set_signalled (GDB_SIGNAL_KILL); } /* Notify server routines. The only real one is dead name notification. */ @@ -1825,7 +1822,7 @@ S_proc_wait_reply (mach_port_t reply, kern_return_t err, else if (pid == inf->pid) { store_waitstatus (&inf->wait.status, status); - if (inf->wait.status.kind == TARGET_WAITKIND_STOPPED) + if (inf->wait.status.kind () == TARGET_WAITKIND_STOPPED) /* The process has sent us a signal, and stopped itself in a sane state pending our actions. */ { @@ -1915,10 +1912,7 @@ S_msg_sig_post_untraced_reply (mach_port_t reply, kern_return_t err) like the process stopped (using a signal of 0 should mean that the *next* time the user continues, it will pass signal 0, which the crash server should like). */ - { - inf->wait.status.kind = TARGET_WAITKIND_STOPPED; - inf->wait.status.value.sig = GDB_SIGNAL_0; - } + inf->wait.status.set_stopped (GDB_SIGNAL_0); else if (err) warning (_("Signal delivery failed: %s"), safe_strerror (err)); @@ -1994,7 +1988,7 @@ gnu_nat_target::resume (ptid_t ptid, int step, enum gdb_signal sig) proc_abort (inf->wait.thread, 1); warning (_("Aborting %s with unforwarded exception %s."), proc_string (inf->wait.thread), - gdb_signal_to_name (inf->wait.status.value.sig)); + gdb_signal_to_name (inf->wait.status.sig ())); } if (port_msgs_queued (inf->event_port)) |