aboutsummaryrefslogtreecommitdiff
path: root/gdbserver/server.cc
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@efficios.com>2021-10-21 16:12:04 -0400
committerSimon Marchi <simon.marchi@efficios.com>2021-10-21 16:13:56 -0400
commit183be222907a6f419bd71f70ee650989026f0188 (patch)
treeddce1e1b3a84b877e6989acb8bbd424dfff0f3b6 /gdbserver/server.cc
parentc360a4732bd7999767616e5f211314510ea677f4 (diff)
downloadgdb-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 'gdbserver/server.cc')
-rw-r--r--gdbserver/server.cc90
1 files changed, 42 insertions, 48 deletions
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index 193c3d9..2cb378c 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -239,9 +239,9 @@ in_queued_stop_replies_ptid (struct notif_event *event, ptid_t filter_ptid)
return true;
/* Don't resume fork children that GDB does not know about yet. */
- if ((vstop_event->status.kind == TARGET_WAITKIND_FORKED
- || vstop_event->status.kind == TARGET_WAITKIND_VFORKED)
- && vstop_event->status.value.related_pid.matches (filter_ptid))
+ if ((vstop_event->status.kind () == TARGET_WAITKIND_FORKED
+ || vstop_event->status.kind () == TARGET_WAITKIND_VFORKED)
+ && vstop_event->status.child_ptid ().matches (filter_ptid))
return true;
return false;
@@ -327,9 +327,9 @@ attach_inferior (int pid)
/* GDB knows to ignore the first SIGSTOP after attaching to a running
process using the "attach" command, but this is different; it's
just using "target remote". Pretend it's just starting up. */
- if (cs.last_status.kind == TARGET_WAITKIND_STOPPED
- && cs.last_status.value.sig == GDB_SIGNAL_STOP)
- cs.last_status.value.sig = GDB_SIGNAL_TRAP;
+ if (cs.last_status.kind () == TARGET_WAITKIND_STOPPED
+ && cs.last_status.sig () == GDB_SIGNAL_STOP)
+ cs.last_status.set_stopped (GDB_SIGNAL_TRAP);
current_thread->last_resume_kind = resume_stop;
current_thread->last_status = cs.last_status;
@@ -1262,8 +1262,7 @@ handle_detach (char *own_buf)
/* There is still at least one inferior remaining or
we are in extended mode, so don't terminate gdbserver,
and instead treat this like a normal program exit. */
- cs.last_status.kind = TARGET_WAITKIND_EXITED;
- cs.last_status.value.integer = 0;
+ cs.last_status.set_exited (0);
cs.last_ptid = ptid_t (pid);
current_thread = NULL;
@@ -2944,7 +2943,7 @@ resume (struct thread_resume *actions, size_t num_actions)
{
cs.last_ptid = mywait (minus_one_ptid, &cs.last_status, 0, 1);
- if (cs.last_status.kind == TARGET_WAITKIND_NO_RESUMED
+ if (cs.last_status.kind () == TARGET_WAITKIND_NO_RESUMED
&& !report_no_resumed)
{
/* The client does not support this stop reply. At least
@@ -2954,9 +2953,9 @@ resume (struct thread_resume *actions, size_t num_actions)
return;
}
- if (cs.last_status.kind != TARGET_WAITKIND_EXITED
- && cs.last_status.kind != TARGET_WAITKIND_SIGNALLED
- && cs.last_status.kind != TARGET_WAITKIND_NO_RESUMED)
+ if (cs.last_status.kind () != TARGET_WAITKIND_EXITED
+ && cs.last_status.kind () != TARGET_WAITKIND_SIGNALLED
+ && cs.last_status.kind () != TARGET_WAITKIND_NO_RESUMED)
current_thread->last_status = cs.last_status;
/* From the client's perspective, all-stop mode always stops all
@@ -2967,8 +2966,8 @@ resume (struct thread_resume *actions, size_t num_actions)
prepare_resume_reply (cs.own_buf, cs.last_ptid, &cs.last_status);
disable_async_io ();
- if (cs.last_status.kind == TARGET_WAITKIND_EXITED
- || cs.last_status.kind == TARGET_WAITKIND_SIGNALLED)
+ if (cs.last_status.kind () == TARGET_WAITKIND_EXITED
+ || cs.last_status.kind () == TARGET_WAITKIND_SIGNALLED)
target_mourn_inferior (cs.last_ptid);
}
}
@@ -3113,7 +3112,7 @@ handle_v_run (char *own_buf)
target_create_inferior (program_path.get (), program_args);
- if (cs.last_status.kind == TARGET_WAITKIND_STOPPED)
+ if (cs.last_status.kind () == TARGET_WAITKIND_STOPPED)
{
prepare_resume_reply (own_buf, cs.last_ptid, &cs.last_status);
@@ -3143,8 +3142,7 @@ handle_v_kill (char *own_buf)
if (proc != nullptr && kill_inferior (proc) == 0)
{
- cs.last_status.kind = TARGET_WAITKIND_SIGNALLED;
- cs.last_status.value.sig = GDB_SIGNAL_KILL;
+ cs.last_status.set_signalled (GDB_SIGNAL_KILL);
cs.last_ptid = ptid_t (pid);
discard_queued_stop_replies (cs.last_ptid);
write_ok (own_buf);
@@ -3316,7 +3314,7 @@ queue_stop_reply_callback (thread_info *thread)
status_string.c_str ());
}
- gdb_assert (thread->last_status.kind != TARGET_WAITKIND_IGNORE);
+ gdb_assert (thread->last_status.kind () != TARGET_WAITKIND_IGNORE);
/* Pass the last stop reply back to GDB, but don't notify
yet. */
@@ -3334,12 +3332,11 @@ gdb_wants_thread_stopped (thread_info *thread)
{
thread->last_resume_kind = resume_stop;
- if (thread->last_status.kind == TARGET_WAITKIND_IGNORE)
+ if (thread->last_status.kind () == TARGET_WAITKIND_IGNORE)
{
/* Most threads are stopped implicitly (all-stop); tag that with
signal 0. */
- thread->last_status.kind = TARGET_WAITKIND_STOPPED;
- thread->last_status.value.sig = GDB_SIGNAL_0;
+ thread->last_status.set_stopped (GDB_SIGNAL_0);
}
}
@@ -3357,15 +3354,15 @@ gdb_wants_all_threads_stopped (void)
static void
set_pending_status_callback (thread_info *thread)
{
- if (thread->last_status.kind != TARGET_WAITKIND_STOPPED
- || (thread->last_status.value.sig != GDB_SIGNAL_0
+ if (thread->last_status.kind () != TARGET_WAITKIND_STOPPED
+ || (thread->last_status.sig () != GDB_SIGNAL_0
/* A breakpoint, watchpoint or finished step from a previous
GDB run isn't considered interesting for a new GDB run.
If we left those pending, the new GDB could consider them
random SIGTRAPs. This leaves out real async traps. We'd
have to peek into the (target-specific) siginfo to
distinguish those. */
- && thread->last_status.value.sig != GDB_SIGNAL_TRAP))
+ && thread->last_status.sig () != GDB_SIGNAL_TRAP))
thread->status_pending_p = 1;
}
@@ -3412,9 +3409,9 @@ handle_status (char *own_buf)
/* Prefer the last thread that reported an event to GDB (even if
that was a GDB_SIGNAL_TRAP). */
- if (cs.last_status.kind != TARGET_WAITKIND_IGNORE
- && cs.last_status.kind != TARGET_WAITKIND_EXITED
- && cs.last_status.kind != TARGET_WAITKIND_SIGNALLED)
+ if (cs.last_status.kind () != TARGET_WAITKIND_IGNORE
+ && cs.last_status.kind () != TARGET_WAITKIND_EXITED
+ && cs.last_status.kind () != TARGET_WAITKIND_SIGNALLED)
thread = find_thread_ptid (cs.last_ptid);
/* If the last event thread is not found for some reason, look
@@ -3443,7 +3440,7 @@ handle_status (char *own_buf)
cs.general_thread = thread->id;
set_desired_thread ();
- gdb_assert (tp->last_status.kind != TARGET_WAITKIND_IGNORE);
+ gdb_assert (tp->last_status.kind () != TARGET_WAITKIND_IGNORE);
prepare_resume_reply (own_buf, tp->id, &tp->last_status);
}
else
@@ -3989,8 +3986,7 @@ captured_main (int argc, char *argv[])
}
else
{
- cs.last_status.kind = TARGET_WAITKIND_EXITED;
- cs.last_status.value.integer = 0;
+ cs.last_status.set_exited (0);
cs.last_ptid = minus_one_ptid;
}
@@ -4002,8 +3998,8 @@ captured_main (int argc, char *argv[])
if (current_thread != nullptr)
current_process ()->dlls_changed = false;
- if (cs.last_status.kind == TARGET_WAITKIND_EXITED
- || cs.last_status.kind == TARGET_WAITKIND_SIGNALLED)
+ if (cs.last_status.kind () == TARGET_WAITKIND_EXITED
+ || cs.last_status.kind () == TARGET_WAITKIND_SIGNALLED)
was_running = 0;
else
was_running = 1;
@@ -4456,8 +4452,7 @@ process_serial_event (void)
running. The traditional protocol will exit instead. */
if (extended_protocol)
{
- cs.last_status.kind = TARGET_WAITKIND_EXITED;
- cs.last_status.value.sig = GDB_SIGNAL_KILL;
+ cs.last_status.set_exited (GDB_SIGNAL_KILL);
return 0;
}
else
@@ -4497,7 +4492,7 @@ process_serial_event (void)
{
target_create_inferior (program_path.get (), program_args);
- if (cs.last_status.kind == TARGET_WAITKIND_STOPPED)
+ if (cs.last_status.kind () == TARGET_WAITKIND_STOPPED)
{
/* Stopped at the first instruction of the target
process. */
@@ -4511,8 +4506,7 @@ process_serial_event (void)
}
else
{
- cs.last_status.kind = TARGET_WAITKIND_EXITED;
- cs.last_status.value.sig = GDB_SIGNAL_KILL;
+ cs.last_status.set_exited (GDB_SIGNAL_KILL);
}
return 0;
}
@@ -4595,24 +4589,24 @@ handle_target_event (int err, gdb_client_data client_data)
cs.last_ptid = mywait (minus_one_ptid, &cs.last_status,
TARGET_WNOHANG, 1);
- if (cs.last_status.kind == TARGET_WAITKIND_NO_RESUMED)
+ if (cs.last_status.kind () == TARGET_WAITKIND_NO_RESUMED)
{
if (gdb_connected () && report_no_resumed)
push_stop_notification (null_ptid, &cs.last_status);
}
- else if (cs.last_status.kind != TARGET_WAITKIND_IGNORE)
+ else if (cs.last_status.kind () != TARGET_WAITKIND_IGNORE)
{
int pid = cs.last_ptid.pid ();
struct process_info *process = find_process_pid (pid);
int forward_event = !gdb_connected () || process->gdb_detached;
- if (cs.last_status.kind == TARGET_WAITKIND_EXITED
- || cs.last_status.kind == TARGET_WAITKIND_SIGNALLED)
+ if (cs.last_status.kind () == TARGET_WAITKIND_EXITED
+ || cs.last_status.kind () == TARGET_WAITKIND_SIGNALLED)
{
mark_breakpoints_out (process);
target_mourn_inferior (cs.last_ptid);
}
- else if (cs.last_status.kind == TARGET_WAITKIND_THREAD_EXITED)
+ else if (cs.last_status.kind () == TARGET_WAITKIND_THREAD_EXITED)
;
else
{
@@ -4631,9 +4625,9 @@ handle_target_event (int err, gdb_client_data client_data)
exit (0);
}
- if (cs.last_status.kind == TARGET_WAITKIND_EXITED
- || cs.last_status.kind == TARGET_WAITKIND_SIGNALLED
- || cs.last_status.kind == TARGET_WAITKIND_THREAD_EXITED)
+ if (cs.last_status.kind () == TARGET_WAITKIND_EXITED
+ || cs.last_status.kind () == TARGET_WAITKIND_SIGNALLED
+ || cs.last_status.kind () == TARGET_WAITKIND_THREAD_EXITED)
;
else
{
@@ -4645,11 +4639,11 @@ handle_target_event (int err, gdb_client_data client_data)
if (debug_threads)
debug_printf ("GDB not connected; forwarding event %d for"
" [%s]\n",
- (int) cs.last_status.kind,
+ (int) cs.last_status.kind (),
target_pid_to_str (cs.last_ptid));
- if (cs.last_status.kind == TARGET_WAITKIND_STOPPED)
- signal = cs.last_status.value.sig;
+ if (cs.last_status.kind () == TARGET_WAITKIND_STOPPED)
+ signal = cs.last_status.sig ();
else
signal = GDB_SIGNAL_0;
target_continue (cs.last_ptid, signal);