Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 12/13] Fix calling prototyped functions via function pointers
Date: Thu, 13 Jul 2017 02:29:00 -0000	[thread overview]
Message-ID: <1499912370-1842-13-git-send-email-palves@redhat.com> (raw)
In-Reply-To: <1499912370-1842-1-git-send-email-palves@redhat.com>

Calling a prototyped function via a function pointer with the right
prototype doesn't work correctly, if the called function requires
argument coercion...  Like, e.g., with:

  float mult (float f1, float f2) { return f1 * f2; }

  (gdb) p mult (2, 3.5)
  $1 = 7
  (gdb) p ((float (*) (float, float)) mult) (2, 3.5)
  $2 = 0

both calls should have returned the same, of course.  The problem is
that GDB misses marking the type of the function pointer target as
prototyped...

Without the fix, the new test fails like this:

 (gdb) p ((int (*) (float, float)) t_float_values2)(3.14159,float_val2)
 $30 = 0
 (gdb) FAIL: gdb.base/callfuncs.exp: p ((int (*) (float, float)) t_float_values2)(3.14159,float_val2)

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* gdbtypes.c (lookup_function_type_with_arguments): Mark function
	types with more than one parameter as prototyped.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* gdb.base/callfuncs.exp (do_function_calls): New parameter
	"prototypes".  Test calling float functions via prototyped and
	unprototyped function pointers.
	(perform_all_tests): New parameter "prototypes".  Pass it down.
	(top level): Pass down "prototypes" parameter to
	perform_all_tests.
---
 gdb/gdbtypes.c                       |  2 ++
 gdb/testsuite/gdb.base/callfuncs.exp | 26 +++++++++++++++++++++-----
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 674a724..fc86225 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -547,6 +547,8 @@ lookup_function_type_with_arguments (struct type *type,
 	  gdb_assert (nparams == 0);
 	  TYPE_PROTOTYPED (fn) = 1;
 	}
+      else
+	TYPE_PROTOTYPED (fn) = 1;
     }
 
   TYPE_NFIELDS (fn) = nparams;
diff --git a/gdb/testsuite/gdb.base/callfuncs.exp b/gdb/testsuite/gdb.base/callfuncs.exp
index 3651963..a537ebd 100644
--- a/gdb/testsuite/gdb.base/callfuncs.exp
+++ b/gdb/testsuite/gdb.base/callfuncs.exp
@@ -39,7 +39,7 @@ set skip_float_test [gdb_skip_float_test]
 # specifies that the numeric value of a relational or logical expression
 # (computed in the inferior) is 1 for true and 0 for false.
 
-proc do_function_calls {} {
+proc do_function_calls {prototypes} {
     global gdb_prompt skip_float_test
 
     # We need to up this because this can be really slow on some boards.
@@ -95,11 +95,25 @@ proc do_function_calls {} {
 	setup_xfail "mn10300-*-*"
 	if { [test_compiler_info "armcc-*"] } { setup_xfail "*-*-*" }
 	gdb_test "p t_float_values(float_val1,-2.3765)" " = 1"
+	# Same, via unprototyped function pointer (t_float_values is
+	# always unprototyped).
+	gdb_test "p ((int (*) ()) t_float_values)(float_val1,-2.3765)" " = 1"
 
 	# Test passing of arguments which might not be widened.
 	gdb_test "p t_float_values2(0.0,0.0)" " = 0"
+	# Same, via function pointer.
+	if {$prototypes} {
+	    gdb_test "p ((int (*) (float, float)) t_float_values2)(0.0,0.0)" " = 0"
+	} else {
+	    gdb_test "p ((int (*) ()) t_float_values2)(0.0,0.0)" " = 0"
+	}
 
 	gdb_test "p t_float_values2(3.14159,float_val2)" " = 1"
+	if {$prototypes} {
+	    gdb_test "p ((int (*) (float, float)) t_float_values2)(3.14159,float_val2)" " = 1"
+	} else {
+	    gdb_test "p ((int (*) ()) t_float_values2)(3.14159,float_val2)" " = 1"
+	}
 
 	gdb_test "p t_float_many_args (float_val1, float_val2, float_val3, float_val4, float_val5, float_val6, float_val7, float_val8, float_val9, float_val10, float_val11, float_val12, float_val13, float_val14, float_val15)" " = 1" "call function with many float arguments."
 
@@ -315,7 +329,7 @@ proc rerun_and_prepare {} {
 	"next to t_structs_c"
 }
 
-proc perform_all_tests {} {
+proc perform_all_tests {prototypes} {
     gdb_test_no_output "set print sevenbit-strings"
     gdb_test_no_output "set print address off"
     gdb_test_no_output "set width 0"
@@ -326,7 +340,7 @@ proc perform_all_tests {} {
     set old_reg_content [fetch_all_registers "retrieve original register contents"]
 
     # Perform function calls.
-    do_function_calls
+    do_function_calls $prototypes
 
     # Check if all registers still have the same value.
     set new_reg_content [fetch_all_registers \
@@ -500,9 +514,11 @@ proc perform_all_tests {} {
 # Perform all tests with and without function prototypes.
 
 if { ![prepare_for_testing "failed to prepare" $testfile $srcfile "$compile_flags additional_flags=-DPROTOTYPES"] } {
-    perform_all_tests
+    perform_all_tests 1
 }
 
 if { ![prepare_for_testing "failed to prepare" $testfile $srcfile "$compile_flags additional_flags=-DNO_PROTOTYPES"] } {
-    with_test_prefix "noproto" perform_all_tests
+    with_test_prefix "noproto" {
+	perform_all_tests 0
+    }
 }
-- 
2.5.5


      parent reply	other threads:[~2017-07-13  2:29 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-13  2:19 [PATCH 00/13] No-debug-info debugging improvements Pedro Alves
2017-07-13  2:19 ` [PATCH 04/13] evaluate_subexp_standard: Eliminate one goto Pedro Alves
2017-07-13  2:19 ` [PATCH 03/13] Make ptype/whatis print function name of functions with no debug info too Pedro Alves
2017-07-13  2:19 ` [PATCH 01/13] Stop assuming no-debug-info functions return int Pedro Alves
2017-07-13  2:19 ` [PATCH 07/13] Stop assuming no-debug-info variables have type int Pedro Alves
2017-07-13  2:19 ` [PATCH 08/13] Eliminate UNOP_MEMVAL_TLS Pedro Alves
2017-07-13  2:19 ` [PATCH 02/13] Introduce OP_VAR_MSYM_VALUE Pedro Alves
2017-07-13  2:27 ` [PATCH 09/13] Handle "p S::method()::static_var" in the C++ parser Pedro Alves
2017-07-13  2:27 ` [PATCH 06/13] evaluate_subexp_standard: Factor out OP_VAR_VALUE handling Pedro Alves
2017-07-13  2:28 ` [PATCH 11/13] Make "p S::method() const::static_var" work too Pedro Alves
2017-07-13  2:28 ` [PATCH 12/13] Document "no debug info debugging" improvements Pedro Alves
2017-07-13 11:09   ` [PATCH 13/13] " Pedro Alves
2017-07-13 13:51     ` Pedro Alves
2017-07-13 13:54       ` Pedro Alves
2017-07-13 15:33         ` Pedro Alves
2017-07-13 15:47   ` [PATCH 12/13] " Eli Zaretskii
2017-07-13 16:14     ` Pedro Alves
2017-07-13  2:28 ` [PATCH 05/13] evaluate_subexp_standard: Remove useless assignments Pedro Alves
2017-07-13  2:29 ` [PATCH 10/13] Handle "p 'S::method()::static_var'" (quoted) in symbol lookup Pedro Alves
2017-07-13  2:29 ` Pedro Alves [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1499912370-1842-13-git-send-email-palves@redhat.com \
    --to=palves@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox