aboutsummaryrefslogtreecommitdiff
path: root/gdbserver/netbsd-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/netbsd-low.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/netbsd-low.cc')
-rw-r--r--gdbserver/netbsd-low.cc50
1 files changed, 20 insertions, 30 deletions
diff --git a/gdbserver/netbsd-low.cc b/gdbserver/netbsd-low.cc
index 84e34d0..ada92c1 100644
--- a/gdbserver/netbsd-low.cc
+++ b/gdbserver/netbsd-low.cc
@@ -206,20 +206,11 @@ static void
netbsd_store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
{
if (WIFEXITED (hoststatus))
- {
- ourstatus->kind = TARGET_WAITKIND_EXITED;
- ourstatus->value.integer = WEXITSTATUS (hoststatus);
- }
+ ourstatus->set_exited (WEXITSTATUS (hoststatus));
else if (!WIFSTOPPED (hoststatus))
- {
- ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
- ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
- }
+ ourstatus->set_signalled (gdb_signal_from_host (WTERMSIG (hoststatus)));
else
- {
- ourstatus->kind = TARGET_WAITKIND_STOPPED;
- ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
- }
+ ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (hoststatus)));
}
/* Implement a safe wrapper around waitpid(). */
@@ -258,14 +249,14 @@ netbsd_wait (ptid_t ptid, struct target_waitstatus *ourstatus,
if (pid == 0)
{
gdb_assert (target_options & TARGET_WNOHANG);
- ourstatus->kind = TARGET_WAITKIND_IGNORE;
+ ourstatus->set_ignore ();
return null_ptid;
}
gdb_assert (pid != -1);
/* If the child stopped, keep investigating its status. */
- if (ourstatus->kind != TARGET_WAITKIND_STOPPED)
+ if (ourstatus->kind () != TARGET_WAITKIND_STOPPED)
return wptid;
/* Extract the event and thread that received a signal. */
@@ -307,12 +298,11 @@ netbsd_wait (ptid_t ptid, struct target_waitstatus *ourstatus,
Ignore exited events for an unknown LWP. */
thread_info *thr = find_thread_ptid (wptid);
if (thr == nullptr)
- ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
+ ourstatus->set_spurious ();
else
{
- ourstatus->kind = TARGET_WAITKIND_THREAD_EXITED;
/* NetBSD does not store an LWP exit status. */
- ourstatus->value.integer = 0;
+ ourstatus->set_thread_exited (0);
remove_thread (thr);
}
@@ -329,20 +319,19 @@ netbsd_wait (ptid_t ptid, struct target_waitstatus *ourstatus,
not yet reported their PTRACE_LWP_CREATE event. Ignore
born events for an already-known LWP. */
if (find_thread_ptid (wptid))
- ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
+ ourstatus->set_spurious ();
else
{
add_thread (wptid, NULL);
- ourstatus->kind = TARGET_WAITKIND_THREAD_CREATED;
+ ourstatus->set_thread_created ();
}
return wptid;
}
if (code == TRAP_EXEC)
{
- ourstatus->kind = TARGET_WAITKIND_EXECD;
- ourstatus->value.execd_pathname
- = xstrdup (netbsd_nat::pid_to_exec_file (pid));
+ ourstatus->set_execd
+ (make_unique_xstrdup (netbsd_nat::pid_to_exec_file (pid)));
return wptid;
}
@@ -356,14 +345,15 @@ netbsd_wait (ptid_t ptid, struct target_waitstatus *ourstatus,
if (!netbsd_catch_this_syscall(sysnum))
{
/* If the core isn't interested in this event, ignore it. */
- ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
+ ourstatus->set_spurious ();
return wptid;
}
- ourstatus->kind
- = ((code == TRAP_SCE) ? TARGET_WAITKIND_SYSCALL_ENTRY :
- TARGET_WAITKIND_SYSCALL_RETURN);
- ourstatus->value.syscall_number = sysnum;
+ if (code == TRAP_SCE)
+ ourstatus->set_syscall_entry (sysnum);
+ else
+ ourstatus->set_syscall_return (sysnum);
+
return wptid;
}
@@ -381,7 +371,7 @@ netbsd_wait (ptid_t ptid, struct target_waitstatus *ourstatus,
}
/* Unclassified SIGTRAP event. */
- ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
+ ourstatus->set_spurious ();
return wptid;
}
@@ -401,10 +391,10 @@ netbsd_process_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
This may also happen on attach, when an event is registered on a thread
that was not fully initialized during the attach stage. */
if (wptid.lwp () != 0 && !find_thread_ptid (wptid)
- && ourstatus->kind != TARGET_WAITKIND_THREAD_EXITED)
+ && ourstatus->kind () != TARGET_WAITKIND_THREAD_EXITED)
add_thread (wptid, nullptr);
- switch (ourstatus->kind)
+ switch (ourstatus->kind ())
{
case TARGET_WAITKIND_EXITED:
case TARGET_WAITKIND_STOPPED: