aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.cp
diff options
context:
space:
mode:
authorAlan Hayward <alan.hayward@arm.com>2018-10-11 14:47:30 +0100
committerAlan Hayward <alan.hayward@arm.com>2018-11-16 13:45:38 +0000
commit38a72da0f1d968432ae6a2a9697ba55932dc075e (patch)
treeb25e8c746263a3201b2a32bf60f2bc094eb8f46f /gdb/testsuite/gdb.cp
parentcf84fa6bcf514157df8343d32885050bafc396f7 (diff)
downloadgdb-38a72da0f1d968432ae6a2a9697ba55932dc075e.zip
gdb-38a72da0f1d968432ae6a2a9697ba55932dc075e.tar.gz
gdb-38a72da0f1d968432ae6a2a9697ba55932dc075e.tar.bz2
Aarch64: Fix segfault when casting dummy calls
The following will segfault on aarch64 if foo is in another object, was compiled as c++ and has no debug symbols: (gdb) p (int)foo() This is because aarch64_push_dummy_call determines the return type of the function and then does not check for null pointer. A null pointer for the return type means the call has no debug information. For the code to get here, then the call must have been cast, otherwise we'd error out sooner. In the case of a no-debug-info call cast, the return type is the type the user had cast the call to, but we do not have that information available here. However, aarch64_push_dummy_call only requires the return type in order to calculate lang_struct_return. This information is available in the return_method enum. The fix is to simply use this instead. Adds testcase to check calls across objects, with all combinations of c, c++, debug and no debug. gdb/ChangeLog: PR gdb/22736: * aarch64-tdep.c (aarch64_push_dummy_call): Remove lang_struct_return code. gdb/testsuite/ChangeLog: PR gdb/22736: * gdb.cp/infcall-nodebug-lib.c: New test. * gdb.cp/infcall-nodebug-main.c: New test. * gdb.cp/infcall-nodebug.exp: New file.
Diffstat (limited to 'gdb/testsuite/gdb.cp')
-rw-r--r--gdb/testsuite/gdb.cp/infcall-nodebug-lib.c22
-rw-r--r--gdb/testsuite/gdb.cp/infcall-nodebug-main.c24
-rw-r--r--gdb/testsuite/gdb.cp/infcall-nodebug.exp119
3 files changed, 165 insertions, 0 deletions
diff --git a/gdb/testsuite/gdb.cp/infcall-nodebug-lib.c b/gdb/testsuite/gdb.cp/infcall-nodebug-lib.c
new file mode 100644
index 0000000..92746f2
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/infcall-nodebug-lib.c
@@ -0,0 +1,22 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2018 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/>. */
+
+int
+foo (void)
+{
+ return 1;
+}
diff --git a/gdb/testsuite/gdb.cp/infcall-nodebug-main.c b/gdb/testsuite/gdb.cp/infcall-nodebug-main.c
new file mode 100644
index 0000000..b623a4a
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/infcall-nodebug-main.c
@@ -0,0 +1,24 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2018 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/>. */
+
+extern int foo (void);
+
+int
+main (void)
+{
+ return 0;
+}
diff --git a/gdb/testsuite/gdb.cp/infcall-nodebug.exp b/gdb/testsuite/gdb.cp/infcall-nodebug.exp
new file mode 100644
index 0000000..bb77b64
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/infcall-nodebug.exp
@@ -0,0 +1,119 @@
+# This testcase is part of GDB, the GNU debugger.
+# Copyright 2018 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 function calls on C++ functions that have no debug information.
+# See gdb/22736. Put the called function in a different object to ensure
+# the rest of the test can be complied with debug information. Whilst we
+# are at it, also test functions with debug information and C functions too.
+
+if [target_info exists gdb,cannot_call_functions] {
+ unsupported "this target can not call functions"
+ continue
+}
+
+# Only test C++ if we are able. Always use C.
+if { [skip_cplus_tests] || [get_compiler_info "c++"] } {
+ set lang {c}
+} else {
+ set lang {c c++}
+}
+
+set main_basename infcall-nodebug-main
+set lib_basename infcall-nodebug-lib
+standard_testfile ${main_basename}.c ${lib_basename}.c
+
+set mainsrc "${srcdir}/${subdir}/${srcfile}"
+set libsrc "${srcdir}/${subdir}/${srcfile2}"
+
+# Build both source files to objects using language LANG. Use SYMBOLS to build
+# with either debug symbols or without - but always build the main file with
+# debug. Then make function calls across the files.
+
+proc build_and_run_test { lang symbols } {
+
+ global main_basename lib_basename mainsrc libsrc binfile testfile
+ global gdb_prompt
+
+ if { $symbols == "debug" } {
+ set debug_flags "debug"
+ } else {
+ set debug_flags ""
+ }
+
+ # Setup directory.
+
+ set dir "$lang-$symbols"
+ remote_exec build "rm -rf [standard_output_file ${dir}]"
+ remote_exec build "mkdir -p [standard_output_file ${dir}]"
+
+ # Compile both files to objects, then link together.
+
+ set main_flags "$lang debug"
+ set lib_flags "$lang $debug_flags"
+ set main_o [standard_output_file ${dir}/${main_basename}.o]
+ set lib_o [standard_output_file ${dir}/${lib_basename}.o]
+ set binfile [standard_output_file ${dir}/${testfile}]
+
+ if { [gdb_compile $mainsrc $main_o object ${main_flags}] != "" } {
+ untested "failed to compile main file to object"
+ return -1
+ }
+
+ if { [gdb_compile $libsrc $lib_o object ${lib_flags}] != "" } {
+ untested "failed to compile secondary file to object"
+ return -1
+ }
+
+ if { [gdb_compile "$main_o $lib_o" ${binfile} executable ""] != "" } {
+ untested "failed to compile"
+ return -1
+ }
+
+ # Startup and run to main.
+
+ clean_restart $binfile
+
+ if ![runto_main] then {
+ fail "can't run to main"
+ return
+ }
+
+ # Function call with cast.
+
+ gdb_test "p (int)foo()" " = 1"
+
+ # Function call without cast. Will error if there are no debug symbols.
+
+ set test "p foo()"
+ gdb_test_multiple $test $test {
+ -re " = 1\r\n$gdb_prompt " {
+ gdb_assert [ string equal $symbols "debug" ]
+ pass $test
+ }
+ -re "has unknown return type; cast the call to its declared return type\r\n$gdb_prompt " {
+ gdb_assert ![ string equal $symbols "debug" ]
+ pass $test
+ }
+ }
+
+}
+
+foreach_with_prefix l $lang {
+ foreach_with_prefix s {debug nodebug} {
+ build_and_run_test $l $s
+ }
+}
+