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/darwin-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/darwin-nat.c')
-rw-r--r-- | gdb/darwin-nat.c | 58 |
1 files changed, 26 insertions, 32 deletions
diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c index 141eede..e1aeb69 100644 --- a/gdb/darwin-nat.c +++ b/gdb/darwin-nat.c @@ -980,12 +980,12 @@ darwin_nat_target::decode_message (mach_msg_header_t *hdr, printf_unfiltered (_("darwin_wait: ill-formatted message (id=0x%x)\n"), hdr->msgh_id); /* FIXME: send a failure reply? */ - status->kind = TARGET_WAITKIND_IGNORE; + status->set_ignore (); return minus_one_ptid; } if (inf == NULL) { - status->kind = TARGET_WAITKIND_IGNORE; + status->set_ignore (); return minus_one_ptid; } *pinf = inf; @@ -995,7 +995,6 @@ darwin_nat_target::decode_message (mach_msg_header_t *hdr, priv->pending_messages++; - status->kind = TARGET_WAITKIND_STOPPED; thread->msg_state = DARWIN_MESSAGE; inferior_debug (4, _("darwin_wait: thread=0x%x, got %s\n"), @@ -1005,25 +1004,25 @@ darwin_nat_target::decode_message (mach_msg_header_t *hdr, switch (thread->event.ex_type) { case EXC_BAD_ACCESS: - status->value.sig = GDB_EXC_BAD_ACCESS; + status->set_stopped (GDB_EXC_BAD_ACCESS); break; case EXC_BAD_INSTRUCTION: - status->value.sig = GDB_EXC_BAD_INSTRUCTION; + status->set_stopped (GDB_EXC_BAD_INSTRUCTION); break; case EXC_ARITHMETIC: - status->value.sig = GDB_EXC_ARITHMETIC; + status->set_stopped (GDB_EXC_ARITHMETIC); break; case EXC_EMULATION: - status->value.sig = GDB_EXC_EMULATION; + status->set_stopped (GDB_EXC_EMULATION); break; case EXC_SOFTWARE: if (thread->event.ex_data[0] == EXC_SOFT_SIGNAL) { - status->value.sig = - gdb_signal_from_host (thread->event.ex_data[1]); + status->set_stopped + (gdb_signal_from_host (thread->event.ex_data[1])); inferior_debug (5, _(" (signal %d: %s)\n"), thread->event.ex_data[1], - gdb_signal_to_name (status->value.sig)); + gdb_signal_to_name (status->sig ())); /* If the thread is stopped because it has received a signal that gdb has just sent, continue. */ @@ -1032,20 +1031,20 @@ darwin_nat_target::decode_message (mach_msg_header_t *hdr, thread->signaled = 0; darwin_send_reply (inf, thread); thread->msg_state = DARWIN_RUNNING; - status->kind = TARGET_WAITKIND_IGNORE; + status->set_ignore (); } } else - status->value.sig = GDB_EXC_SOFTWARE; + status->set_stopped (GDB_EXC_SOFTWARE); break; case EXC_BREAKPOINT: /* Many internal GDB routines expect breakpoints to be reported as GDB_SIGNAL_TRAP, and will report GDB_EXC_BREAKPOINT as a spurious signal. */ - status->value.sig = GDB_SIGNAL_TRAP; + status->set_stopped (GDB_SIGNAL_TRAP); break; default: - status->value.sig = GDB_SIGNAL_UNKNOWN; + status->set_stopped (GDB_SIGNAL_UNKNOWN); break; } @@ -1071,7 +1070,7 @@ darwin_nat_target::decode_message (mach_msg_header_t *hdr, if (res < 0 || inf == NULL) { - status->kind = TARGET_WAITKIND_IGNORE; + status->set_ignore (); return minus_one_ptid; } @@ -1089,18 +1088,15 @@ darwin_nat_target::decode_message (mach_msg_header_t *hdr, { printf_unfiltered (_("wait4: res=%d: %s\n"), res_pid, safe_strerror (errno)); - status->kind = TARGET_WAITKIND_IGNORE; + status->set_ignore (); return minus_one_ptid; } if (WIFEXITED (wstatus)) - { - status->kind = TARGET_WAITKIND_EXITED; - status->value.integer = WEXITSTATUS (wstatus); - } + status->set_exited (WEXITSTATUS (wstatus)); else { - status->kind = TARGET_WAITKIND_SIGNALLED; - status->value.sig = gdb_signal_from_host (WTERMSIG (wstatus)); + status->set_signalled + (gdb_signal_from_host (WTERMSIG (wstatus))); } inferior_debug (4, _("darwin_wait: pid=%d exit, status=0x%x\n"), @@ -1114,8 +1110,7 @@ darwin_nat_target::decode_message (mach_msg_header_t *hdr, else { inferior_debug (4, _("darwin_wait: pid=%d\n"), inf->pid); - status->kind = TARGET_WAITKIND_EXITED; - status->value.integer = 0; /* Don't know. */ + status->set_exited (0 /* Don't know. */); return ptid_t (inf->pid, 0, 0); } } @@ -1123,7 +1118,7 @@ darwin_nat_target::decode_message (mach_msg_header_t *hdr, /* Unknown message. */ warning (_("darwin: got unknown message, id: 0x%x"), hdr->msgh_id); - status->kind = TARGET_WAITKIND_IGNORE; + status->set_ignore (); return minus_one_ptid; } @@ -1182,8 +1177,7 @@ darwin_nat_target::wait_1 (ptid_t ptid, struct target_waitstatus *status) darwin_inferior *priv = get_darwin_inferior (inf); - status->kind = TARGET_WAITKIND_STOPPED; - status->value.sig = GDB_SIGNAL_TRAP; + status->set_stopped (GDB_SIGNAL_TRAP); thread = priv->threads[0]; thread->msg_state = DARWIN_STOPPED; return ptid_t (inf->pid, 0, thread->gdb_port); @@ -1201,14 +1195,14 @@ darwin_nat_target::wait_1 (ptid_t ptid, struct target_waitstatus *status) if (kret == MACH_RCV_INTERRUPTED) { - status->kind = TARGET_WAITKIND_IGNORE; + status->set_ignore (); return minus_one_ptid; } if (kret != MACH_MSG_SUCCESS) { inferior_debug (5, _("mach_msg: ret=0x%x\n"), kret); - status->kind = TARGET_WAITKIND_SPURIOUS; + status->set_spurious (); return minus_one_ptid; } @@ -1225,7 +1219,7 @@ darwin_nat_target::wait_1 (ptid_t ptid, struct target_waitstatus *status) if (inf == NULL) return res; } - while (status->kind == TARGET_WAITKIND_IGNORE); + while (status->kind () == TARGET_WAITKIND_IGNORE); /* Stop all tasks. */ for (inferior *inf : all_inferiors (this)) @@ -1400,8 +1394,8 @@ darwin_nat_target::stop_inferior (inferior *inf) while (1) { ptid = wait_1 (ptid_t (inf->pid), &wstatus); - if (wstatus.kind == TARGET_WAITKIND_STOPPED - && wstatus.value.sig == GDB_SIGNAL_STOP) + if (wstatus.kind () == TARGET_WAITKIND_STOPPED + && wstatus.sig () == GDB_SIGNAL_STOP) break; } } |