diff options
author | Lancelot SIX <lancelot.six@amd.com> | 2022-07-08 11:37:18 +0100 |
---|---|---|
committer | Lancelot SIX <lsix@lancelotsix.com> | 2022-07-26 08:27:27 +0100 |
commit | bc20e562ec0436b6117b989c0e3d8f66c9d4d979 (patch) | |
tree | 868b81b4f341e2b9cccc7fbc1d8210bb46f93f48 /gdb/testsuite/gdb.mi | |
parent | 60cd08d40309a2b4ce1daae84074893e6303ad17 (diff) | |
download | gdb-bc20e562ec0436b6117b989c0e3d8f66c9d4d979.zip gdb-bc20e562ec0436b6117b989c0e3d8f66c9d4d979.tar.gz gdb-bc20e562ec0436b6117b989c0e3d8f66c9d4d979.tar.bz2 |
gdb/varobj: Fix use after free in varobj
Varobj object contains references to types, variables (i.e. struct
variable) and expression. All of those can reference data on an
objfile's obstack. It is possible for this objfile to be deleted (and
the obstack to be feed), while the varobj remains valid. Later, if the
user uses the varobj, this will result in a use-after-free error. With
address sanitizer build, this leads to a plain error. For non address
sanitizer build we might see undefined behaviour, which manifest
themself as assertion failures when accessing data backed by feed
memory.
This can be observed if we create a varobj that refers to ta symbol in a
shared library, after either the objfile gets reloaded (using the `file`
command) or after the shared library is unloaded (with a call to dlclose
for example).
This patch fixes those issues by:
- Adding cleanup procedure to the free_objfile observable. When
activated this observer clears expressions referencing the objfile
being freed, and removes references to blocks belonging to this
objfile.
- Adding varobj support in the `preserve_values` (gdb.value.c). This
ensures that before the objfile is unloaded, any type owned by the
objfile referenced by the varobj is replaced by an equivalent type
not owned by the objfile. This process is done here instead of in the
free_objfile observer in order to reuse the type hash table already
used for similar purpose when replacing types of values kept in the
value history.
This patch also makes sure to keep a reference to the expression's
gdbarch and language_defn members when the varobj->root->exp is
initialized. Those structures outlive the objfile, so this is safe.
This is done because those references might be used initialize a python
context even after exp is invalidated. Another approach could have been
to initialize the python context with default gdbarch and language_defn
(i.e. nullptr) if expr is NULL, but since we might still try to display
the value which was obtained by evaluating exp when it was still valid,
keeping track of the context which was used at this time seems
reasonable.
Tested on x86_64-Linux.
Co-Authored-By: Pedro Alves <pedro@palves.net>
Diffstat (limited to 'gdb/testsuite/gdb.mi')
-rw-r--r-- | gdb/testsuite/gdb.mi/mi-var-invalidate-shlib-lib.c | 30 | ||||
-rw-r--r-- | gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.c | 41 | ||||
-rw-r--r-- | gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.exp | 93 |
3 files changed, 164 insertions, 0 deletions
diff --git a/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib-lib.c b/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib-lib.c new file mode 100644 index 0000000..3d4c7aa --- /dev/null +++ b/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib-lib.c @@ -0,0 +1,30 @@ +/* Copyright 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +struct bar +{ + int a; + int b; +}; + +struct bar global_shlib_var = { 2, 2 }; + +void +foo () +{ + struct bar local_var = { 1, 1 }; +} diff --git a/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.c b/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.c new file mode 100644 index 0000000..f9237c3 --- /dev/null +++ b/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.c @@ -0,0 +1,41 @@ +/* Copyright 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include <dlfcn.h> + +static void +no_varobj_in_scope (void) +{ +} + +static void +floating_varobj_in_scope (void) +{ + int local_var = 0; +} + +int +main (int argc, char *argv []) +{ + void *shlib = dlopen (SHLIB_PATH, RTLD_LAZY); + void (*foo)() = dlsym (shlib, "foo"); + foo (); + dlclose (shlib); + + no_varobj_in_scope (); + floating_varobj_in_scope (); +} diff --git a/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.exp b/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.exp new file mode 100644 index 0000000..b405c83 --- /dev/null +++ b/gdb/testsuite/gdb.mi/mi-var-invalidate-shlib.exp @@ -0,0 +1,93 @@ +# Copyright 2007-2022 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# Test that varobj are invalidated after the shlib they point to goes +# away. + + +load_lib mi-support.exp +set MIFLAGS "-i=mi" + +if { [skip_shlib_tests] } { + return 0 +} + +standard_testfile .c -lib.c +set shlib_path [standard_output_file ${testfile}-lib.so] + +if { [gdb_compile_shlib $srcdir/$subdir/$srcfile2 $shlib_path {debug}] != "" } { + untested "failed to compile" + return -1 +} + + +set opts [list shlib_load debug additional_flags=-DSHLIB_PATH="${shlib_path}"] +if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $opts] != "" } { + untested "failed to compile" + return -1 +} + +proc do_test { separate_debuginfo } { + if { $separate_debuginfo } { + gdb_gnu_strip_debug $::shlib_path + } + + if { [mi_clean_restart] } { + unsupported "failed to start GDB" + return + } + + # Start the process once and create varobjs referencing the loaded objfiles. + with_test_prefix "setup" { + mi_load_shlibs $::shlib_path + if { $separate_debuginfo } { + mi_load_shlibs ${::shlib_path}.debug + } + mi_delete_breakpoints + mi_gdb_reinitialize_dir $::srcdir/$::subdir + mi_gdb_load $::binfile + + mi_runto foo -pending + + mi_create_varobj global_shlib_var global_shlib_var "create global gloal_shlib_var" + mi_create_floating_varobj floating_local local_var "create floating local_var" + + # Advance to a point where the shlib's objfile have been deleted. + mi_continue_to "no_varobj_in_scope" + } + + with_test_prefix "after objfile deleted" { + # The global shlib var was invalidated when the objfile got unloaded. + mi_gdb_test "-var-update global_shlib_var" \ + "\\^done,changelist=\\\[\{name=\"global_shlib_var\",in_scope=\"invalid\",has_more=\"0\"\}\]" \ + "global_shlib_var invalidated" + + # The floating var is still valid but not in scope. + mi_gdb_test "-var-update floating_local" \ + "\\^done,changelist=\\\[{name=\"floating_local\",in_scope=\"false\",type_changed=\"false\",has_more=\"0\"}\\\]" \ + "floating_local still valid but not in scope" + + # The varobj can be re-evaluated if the expression is valid in the current + # frame. + mi_continue_to "floating_varobj_in_scope" + mi_gdb_test "-var-update floating_local" \ + "\\^done,changelist=\\\[{name=\"floating_local\",in_scope=\"true\",type_changed=\"true\",new_type=\"int\",new_num_children=\"0\",has_more=\"0\"}\\\]" \ + "floating_local in scope with new type and value" + } +} + +foreach_with_prefix separate_debuginfo {0 1} { + do_test $separate_debuginfo +} |