aboutsummaryrefslogtreecommitdiff
path: root/gdbserver/win32-low.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/win32-low.cc
parentc360a4732bd7999767616e5f211314510ea677f4 (diff)
downloadbinutils-183be222907a6f419bd71f70ee650989026f0188.zip
binutils-183be222907a6f419bd71f70ee650989026f0188.tar.gz
binutils-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/win32-low.cc')
-rw-r--r--gdbserver/win32-low.cc40
1 files changed, 16 insertions, 24 deletions
diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc
index 1e97f91..cc981d3 100644
--- a/gdbserver/win32-low.cc
+++ b/gdbserver/win32-low.cc
@@ -374,7 +374,7 @@ do_initial_child_stuff (HANDLE proch, DWORD pid, int attached)
if (the_low_target.initial_stuff != NULL)
(*the_low_target.initial_stuff) ();
- cached_status.kind = TARGET_WAITKIND_IGNORE;
+ cached_status.set_ignore ();
/* Flush all currently pending debug events (thread and dll list) up
to the initial breakpoint. */
@@ -385,7 +385,7 @@ do_initial_child_stuff (HANDLE proch, DWORD pid, int attached)
the_target->wait (minus_one_ptid, &status, 0);
/* Note win32_wait doesn't return thread events. */
- if (status.kind != TARGET_WAITKIND_LOADED)
+ if (status.kind () != TARGET_WAITKIND_LOADED)
{
cached_status = status;
break;
@@ -1081,7 +1081,7 @@ get_child_debug_event (DWORD *continue_status,
ptid_t ptid;
last_sig = GDB_SIGNAL_0;
- ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
+ ourstatus->set_spurious ();
*continue_status = DBG_CONTINUE;
/* Check if GDB sent us an interrupt request. */
@@ -1119,8 +1119,7 @@ get_child_debug_event (DWORD *continue_status,
load the application, e.g., if the main executable
tries to pull in a non-existing export from a
DLL. */
- ourstatus->kind = TARGET_WAITKIND_EXITED;
- ourstatus->value.integer = 1;
+ ourstatus->set_exited (1);
return 1;
}
@@ -1192,15 +1191,9 @@ get_child_debug_event (DWORD *continue_status,
int exit_signal
= WIFSIGNALED (exit_status) ? WTERMSIG (exit_status) : -1;
if (exit_signal == -1)
- {
- ourstatus->kind = TARGET_WAITKIND_EXITED;
- ourstatus->value.integer = exit_status;
- }
+ ourstatus->set_exited (exit_status);
else
- {
- ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
- ourstatus->value.sig = gdb_signal_from_host (exit_signal);
- }
+ ourstatus->set_signalled (gdb_signal_from_host (exit_signal));
}
child_continue (DBG_CONTINUE, desired_stop_thread_id);
break;
@@ -1215,8 +1208,7 @@ get_child_debug_event (DWORD *continue_status,
break;
dll_loaded_event ();
- ourstatus->kind = TARGET_WAITKIND_LOADED;
- ourstatus->value.sig = GDB_SIGNAL_TRAP;
+ ourstatus->set_loaded ();
break;
case UNLOAD_DLL_DEBUG_EVENT:
@@ -1227,8 +1219,7 @@ get_child_debug_event (DWORD *continue_status,
if (! child_initialization_done)
break;
handle_unload_dll ();
- ourstatus->kind = TARGET_WAITKIND_LOADED;
- ourstatus->value.sig = GDB_SIGNAL_TRAP;
+ ourstatus->set_loaded ();
break;
case EXCEPTION_DEBUG_EVENT:
@@ -1270,7 +1261,7 @@ get_child_debug_event (DWORD *continue_status,
ptid.lwp (), desired_stop_thread_id));
maybe_adjust_pc ();
pending_stops.push_back ({(DWORD) ptid.lwp (), *ourstatus, current_event});
- ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
+ ourstatus->set_spurious ();
}
else
current_thread = find_thread_ptid (ptid);
@@ -1285,14 +1276,14 @@ ptid_t
win32_process_target::wait (ptid_t ptid, target_waitstatus *ourstatus,
target_wait_flags options)
{
- if (cached_status.kind != TARGET_WAITKIND_IGNORE)
+ if (cached_status.kind () != TARGET_WAITKIND_IGNORE)
{
/* The core always does a wait after creating the inferior, and
do_initial_child_stuff already ran the inferior to the
initial breakpoint (or an exit, if creating the process
fails). Report it now. */
*ourstatus = cached_status;
- cached_status.kind = TARGET_WAITKIND_IGNORE;
+ cached_status.set_ignore ();
return debug_event_ptid (&current_event);
}
@@ -1302,11 +1293,11 @@ win32_process_target::wait (ptid_t ptid, target_waitstatus *ourstatus,
if (!get_child_debug_event (&continue_status, ourstatus))
continue;
- switch (ourstatus->kind)
+ switch (ourstatus->kind ())
{
case TARGET_WAITKIND_EXITED:
OUTMSG2 (("Child exited with retcode = %x\n",
- ourstatus->value.integer));
+ ourstatus->exit_status ()));
win32_clear_inferiors ();
return ptid_t (current_event.dwProcessId);
case TARGET_WAITKIND_STOPPED:
@@ -1314,12 +1305,13 @@ win32_process_target::wait (ptid_t ptid, target_waitstatus *ourstatus,
case TARGET_WAITKIND_LOADED:
{
OUTMSG2 (("Child Stopped with signal = %d \n",
- ourstatus->value.sig));
+ ourstatus->sig ()));
maybe_adjust_pc ();
return debug_event_ptid (&current_event);
}
default:
- OUTMSG (("Ignoring unknown internal event, %d\n", ourstatus->kind));
+ OUTMSG (("Ignoring unknown internal event, %d\n",
+ ourstatus->kind ()));
/* fall-through */
case TARGET_WAITKIND_SPURIOUS:
/* do nothing, just continue */