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/procfs.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/procfs.c')
-rw-r--r-- | gdb/procfs.c | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/gdb/procfs.c b/gdb/procfs.c index a8ee933..2c96919 100644 --- a/gdb/procfs.c +++ b/gdb/procfs.c @@ -1311,8 +1311,8 @@ proc_set_current_signal (procinfo *pi, int signo) get_last_target_status (&wait_target, &wait_ptid, &wait_status); if (wait_target == &the_procfs_target && wait_ptid == inferior_ptid - && wait_status.kind == TARGET_WAITKIND_STOPPED - && wait_status.value.sig == gdb_signal_from_host (signo) + && wait_status.kind () == TARGET_WAITKIND_STOPPED + && wait_status.sig () == gdb_signal_from_host (signo) && proc_get_status (pi) && pi->prstatus.pr_lwp.pr_info.si_signo == signo ) @@ -2256,7 +2256,7 @@ wait_again: printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (retval).c_str ()); delete_thread (find_thread_ptid (this, retval)); - status->kind = TARGET_WAITKIND_SPURIOUS; + status->set_spurious (); return retval; } else @@ -2306,8 +2306,7 @@ wait_again: if (!in_thread_list (this, temp_ptid)) add_thread (this, temp_ptid); - status->kind = TARGET_WAITKIND_STOPPED; - status->value.sig = GDB_SIGNAL_0; + status->set_stopped (GDB_SIGNAL_0); return retval; } #endif |