aboutsummaryrefslogtreecommitdiff
path: root/gdb/nat
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 /gdb/nat
parentc360a4732bd7999767616e5f211314510ea677f4 (diff)
downloadfsf-binutils-gdb-183be222907a6f419bd71f70ee650989026f0188.zip
fsf-binutils-gdb-183be222907a6f419bd71f70ee650989026f0188.tar.gz
fsf-binutils-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/nat')
-rw-r--r--gdb/nat/fork-inferior.c19
-rw-r--r--gdb/nat/windows-nat.c52
2 files changed, 33 insertions, 38 deletions
diff --git a/gdb/nat/fork-inferior.c b/gdb/nat/fork-inferior.c
index c88cf4c..fc19f17 100644
--- a/gdb/nat/fork-inferior.c
+++ b/gdb/nat/fork-inferior.c
@@ -479,7 +479,6 @@ startup_inferior (process_stratum_target *proc_target, pid_t pid, int ntraps,
ptid_t event_ptid;
struct target_waitstatus ws;
- memset (&ws, 0, sizeof (ws));
event_ptid = target_wait (resume_ptid, &ws, 0);
if (last_waitstatus != NULL)
@@ -487,11 +486,11 @@ startup_inferior (process_stratum_target *proc_target, pid_t pid, int ntraps,
if (last_ptid != NULL)
*last_ptid = event_ptid;
- if (ws.kind == TARGET_WAITKIND_IGNORE)
+ if (ws.kind () == TARGET_WAITKIND_IGNORE)
/* The inferior didn't really stop, keep waiting. */
continue;
- switch (ws.kind)
+ switch (ws.kind ())
{
case TARGET_WAITKIND_SPURIOUS:
case TARGET_WAITKIND_LOADED:
@@ -507,32 +506,28 @@ startup_inferior (process_stratum_target *proc_target, pid_t pid, int ntraps,
target_terminal::ours ();
target_mourn_inferior (event_ptid);
error (_("During startup program terminated with signal %s, %s."),
- gdb_signal_to_name (ws.value.sig),
- gdb_signal_to_string (ws.value.sig));
+ gdb_signal_to_name (ws.sig ()),
+ gdb_signal_to_string (ws.sig ()));
return resume_ptid;
case TARGET_WAITKIND_EXITED:
target_terminal::ours ();
target_mourn_inferior (event_ptid);
- if (ws.value.integer)
+ if (ws.exit_status ())
error (_("During startup program exited with code %d."),
- ws.value.integer);
+ ws.exit_status ());
else
error (_("During startup program exited normally."));
return resume_ptid;
case TARGET_WAITKIND_EXECD:
/* Handle EXEC signals as if they were SIGTRAP signals. */
- /* Free the exec'ed pathname, but only if this isn't the
- waitstatus we are returning to the caller. */
- if (pending_execs != 1)
- xfree (ws.value.execd_pathname);
resume_signal = GDB_SIGNAL_TRAP;
switch_to_thread (proc_target, event_ptid);
break;
case TARGET_WAITKIND_STOPPED:
- resume_signal = ws.value.sig;
+ resume_signal = ws.sig ();
switch_to_thread (proc_target, event_ptid);
break;
}
diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c
index 0bb87ec..fa0ed56 100644
--- a/gdb/nat/windows-nat.c
+++ b/gdb/nat/windows-nat.c
@@ -197,8 +197,6 @@ handle_exception (struct target_waitstatus *ourstatus, bool debug_exceptions)
memcpy (&siginfo_er, rec, sizeof siginfo_er);
- ourstatus->kind = TARGET_WAITKIND_STOPPED;
-
/* Record the context of the current thread. */
thread_rec (ptid_t (current_event.dwProcessId, current_event.dwThreadId, 0),
DONT_SUSPEND);
@@ -207,53 +205,53 @@ handle_exception (struct target_waitstatus *ourstatus, bool debug_exceptions)
{
case EXCEPTION_ACCESS_VIOLATION:
DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ACCESS_VIOLATION");
- ourstatus->value.sig = GDB_SIGNAL_SEGV;
+ ourstatus->set_stopped (GDB_SIGNAL_SEGV);
if (handle_access_violation (rec))
return HANDLE_EXCEPTION_UNHANDLED;
break;
case STATUS_STACK_OVERFLOW:
DEBUG_EXCEPTION_SIMPLE ("STATUS_STACK_OVERFLOW");
- ourstatus->value.sig = GDB_SIGNAL_SEGV;
+ ourstatus->set_stopped (GDB_SIGNAL_SEGV);
break;
case STATUS_FLOAT_DENORMAL_OPERAND:
DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DENORMAL_OPERAND");
- ourstatus->value.sig = GDB_SIGNAL_FPE;
+ ourstatus->set_stopped (GDB_SIGNAL_FPE);
break;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ARRAY_BOUNDS_EXCEEDED");
- ourstatus->value.sig = GDB_SIGNAL_FPE;
+ ourstatus->set_stopped (GDB_SIGNAL_FPE);
break;
case STATUS_FLOAT_INEXACT_RESULT:
DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INEXACT_RESULT");
- ourstatus->value.sig = GDB_SIGNAL_FPE;
+ ourstatus->set_stopped (GDB_SIGNAL_FPE);
break;
case STATUS_FLOAT_INVALID_OPERATION:
DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INVALID_OPERATION");
- ourstatus->value.sig = GDB_SIGNAL_FPE;
+ ourstatus->set_stopped (GDB_SIGNAL_FPE);
break;
case STATUS_FLOAT_OVERFLOW:
DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_OVERFLOW");
- ourstatus->value.sig = GDB_SIGNAL_FPE;
+ ourstatus->set_stopped (GDB_SIGNAL_FPE);
break;
case STATUS_FLOAT_STACK_CHECK:
DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_STACK_CHECK");
- ourstatus->value.sig = GDB_SIGNAL_FPE;
+ ourstatus->set_stopped (GDB_SIGNAL_FPE);
break;
case STATUS_FLOAT_UNDERFLOW:
DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_UNDERFLOW");
- ourstatus->value.sig = GDB_SIGNAL_FPE;
+ ourstatus->set_stopped (GDB_SIGNAL_FPE);
break;
case STATUS_FLOAT_DIVIDE_BY_ZERO:
DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DIVIDE_BY_ZERO");
- ourstatus->value.sig = GDB_SIGNAL_FPE;
+ ourstatus->set_stopped (GDB_SIGNAL_FPE);
break;
case STATUS_INTEGER_DIVIDE_BY_ZERO:
DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_DIVIDE_BY_ZERO");
- ourstatus->value.sig = GDB_SIGNAL_FPE;
+ ourstatus->set_stopped (GDB_SIGNAL_FPE);
break;
case STATUS_INTEGER_OVERFLOW:
DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_OVERFLOW");
- ourstatus->value.sig = GDB_SIGNAL_FPE;
+ ourstatus->set_stopped (GDB_SIGNAL_FPE);
break;
case EXCEPTION_BREAKPOINT:
#ifdef __x86_64__
@@ -263,7 +261,7 @@ handle_exception (struct target_waitstatus *ourstatus, bool debug_exceptions)
on startup, first a BREAKPOINT for the 64bit ntdll.dll,
then a WX86_BREAKPOINT for the 32bit ntdll.dll.
Here we only care about the WX86_BREAKPOINT's. */
- ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
+ ourstatus->set_spurious ();
ignore_first_breakpoint = false;
}
else if (wow64_process)
@@ -277,45 +275,45 @@ handle_exception (struct target_waitstatus *ourstatus, bool debug_exceptions)
unconditionally. */
DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_BREAKPOINT");
rec->ExceptionCode = DBG_CONTROL_C;
- ourstatus->value.sig = GDB_SIGNAL_INT;
+ ourstatus->set_stopped (GDB_SIGNAL_INT);
break;
}
#endif
/* FALLTHROUGH */
case STATUS_WX86_BREAKPOINT:
DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_BREAKPOINT");
- ourstatus->value.sig = GDB_SIGNAL_TRAP;
+ ourstatus->set_stopped (GDB_SIGNAL_TRAP);
break;
case DBG_CONTROL_C:
DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_C");
- ourstatus->value.sig = GDB_SIGNAL_INT;
+ ourstatus->set_stopped (GDB_SIGNAL_INT);
break;
case DBG_CONTROL_BREAK:
DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_BREAK");
- ourstatus->value.sig = GDB_SIGNAL_INT;
+ ourstatus->set_stopped (GDB_SIGNAL_INT);
break;
case EXCEPTION_SINGLE_STEP:
case STATUS_WX86_SINGLE_STEP:
DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_SINGLE_STEP");
- ourstatus->value.sig = GDB_SIGNAL_TRAP;
+ ourstatus->set_stopped (GDB_SIGNAL_TRAP);
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ILLEGAL_INSTRUCTION");
- ourstatus->value.sig = GDB_SIGNAL_ILL;
+ ourstatus->set_stopped (GDB_SIGNAL_ILL);
break;
case EXCEPTION_PRIV_INSTRUCTION:
DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_PRIV_INSTRUCTION");
- ourstatus->value.sig = GDB_SIGNAL_ILL;
+ ourstatus->set_stopped (GDB_SIGNAL_ILL);
break;
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_NONCONTINUABLE_EXCEPTION");
- ourstatus->value.sig = GDB_SIGNAL_ILL;
+ ourstatus->set_stopped (GDB_SIGNAL_ILL);
break;
case MS_VC_EXCEPTION:
DEBUG_EXCEPTION_SIMPLE ("MS_VC_EXCEPTION");
if (handle_ms_vc_exception (rec))
{
- ourstatus->value.sig = GDB_SIGNAL_TRAP;
+ ourstatus->set_stopped (GDB_SIGNAL_TRAP);
result = HANDLE_EXCEPTION_IGNORED;
break;
}
@@ -329,11 +327,13 @@ handle_exception (struct target_waitstatus *ourstatus, bool debug_exceptions)
(unsigned) current_event.u.Exception.ExceptionRecord.ExceptionCode,
host_address_to_string (
current_event.u.Exception.ExceptionRecord.ExceptionAddress));
- ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
+ ourstatus->set_stopped (GDB_SIGNAL_UNKNOWN);
break;
}
- last_sig = ourstatus->value.sig;
+ if (ourstatus->kind () == TARGET_WAITKIND_STOPPED)
+ last_sig = ourstatus->sig ();
+
return result;
#undef DEBUG_EXCEPTION_SIMPLE