From: Andrew Burgess Date: Mon, 21 Jun 2021 22:33:11 +0000 (+0100) Subject: gdb: fix regression in evaluate_funcall for non C++ like cases X-Git-Url: https://git.efficios.com/?a=commitdiff_plain;h=79bd4d34f0583f1c1cea60fa94986e222ade33b8;p=deliverable%2Fbinutils-gdb.git gdb: fix regression in evaluate_funcall for non C++ like cases This regression, as it is exposed by the test added in this commit, first became noticable with this commit: commit d182f2797922a305fbd1ef6a483cc39a56b43e02 Date: Mon Mar 8 07:27:57 2021 -0700 Convert c-exp.y to use operations But, this commit only added converted the C expression parser to make use of code that was added in this commit: commit a00b7254fb614af557de7ae7cc0eb39a0ce0e408 Date: Mon Mar 8 07:27:57 2021 -0700 Implement function call operations And it was this second commit that actually introduced the bugs (there are two). In structop_base_operation::evaluate_funcall we build up an argument list in the vector vals. Later in this function the argument list might be passed to value_struct_elt. Prior to commit a00b7254fb614 the vals vector (or argvec as it used to be called) stored the value for the function callee in the argvec at index 0. This 'callee' value is what ends up being passed to evaluate_subexp_do_call, and represents the function to be called, the value contents are the address of the function, and the value type is the function signature. The remaining items held in the argvec were the values to pass to the function. For a non-static member function the `this' pointer would be at index 1 in the array. After commit a00b7254fb614 this callee value is now held in a separate variable, not the vals array. So, for non-static member functions, the `this' pointer is now at index 0, with any other arguments after that. What this means is that previous, when we called value_struct_elt we would pass the address of argvec[1] as this was the first argument. But now we should be passing the address of vals[0]. Unfortunately, we are still passing vals[1], effectively skipping the first argument. The second issue is that, prior to commit a00b7254fb614, the argvec array was NULL terminated. This is required as value_struct_elt calls search_struct_method, which calls typecmp, and typecmp requires that the array have a NULL at the end. After commit a00b7254fb614 this NULL has been lost, and we are therefore violating the API requirements of typecmp. This commit fixes both of these regressions. I also extended the header comments on search_struct_method and value_struct_elt to make it clearer that the array required a NULL marker at the end. You will notice in the test attached to this commit that I test calling a non-static member function, but not calling a static member function. The reason for this is that calling static member functions is currently broken due to a different bug. That will be fixed in a later patch in this series, at which time I'll add a test for calling a static member function. gdb/ChangeLog: PR gdb/27994 * eval.c (structop_base_operation::evaluate_funcall): Add a nullptr to the end of the args array, which should not be included in the argument array_view. Pass all the arguments through to value_struct_elt. * valops.c (search_struct_method): Update header comment. (value_struct_elt): Likewise. gdb/testsuite/ChangeLog: PR gdb/27994 * gdb.cp/method-call-in-c.cc: New file. * gdb.cp/method-call-in-c.exp: New file. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 53b3c31fd8..04ea77f06e 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,13 @@ +2021-06-25 Andrew Burgess + + PR gdb/27994 + * eval.c (structop_base_operation::evaluate_funcall): Add a + nullptr to the end of the args array, which should not be included + in the argument array_view. Pass all the arguments through to + value_struct_elt. + * valops.c (search_struct_method): Update header comment. + (value_struct_elt): Likewise. + 2021-06-25 Tom Tromey * dwarf2/read.c (create_addrmap_from_aranges): Change padding diff --git a/gdb/eval.c b/gdb/eval.c index 659493c823..ab070a3d9f 100644 --- a/gdb/eval.c +++ b/gdb/eval.c @@ -872,7 +872,9 @@ structop_base_operation::evaluate_funcall (struct type *expect_type, struct expression *exp, enum noside noside, const std::vector &args) { - std::vector vals (args.size () + 1); + /* Allocate space for the function call arguments. Include space for a + `this' pointer at the start, and a trailing nullptr. */ + std::vector vals (args.size () + 2); /* First, evaluate the structure into vals[0]. */ enum exp_opcode op = opcode (); if (op == STRUCTOP_STRUCT) @@ -918,9 +920,16 @@ structop_base_operation::evaluate_funcall } } + /* Evaluate the arguments, and add the trailing nullptr. The '+ 1' here + is to allow for the `this' pointer we placed into vals[0]. */ for (int i = 0; i < args.size (); ++i) vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside); - gdb::array_view arg_view = vals; + vals[args.size () + 1] = nullptr; + + /* The array view includes the `this' pointer, but not the trailing + nullptr. */ + gdb::array_view arg_view + = gdb::make_array_view (&vals[0], args.size () + 1); int static_memfuncp; value *callee; @@ -941,7 +950,7 @@ structop_base_operation::evaluate_funcall { struct value *temp = vals[0]; - callee = value_struct_elt (&temp, &vals[1], tstr, + callee = value_struct_elt (&temp, &vals[0], tstr, &static_memfuncp, op == STRUCTOP_STRUCT ? "structure" : "structure pointer"); diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index fa5476587e..878921d093 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2021-06-25 Andrew Burgess + + PR gdb/27994 + * gdb.cp/method-call-in-c.cc: New file. + * gdb.cp/method-call-in-c.exp: New file. + 2021-06-25 Tom Tromey * lib/gdb.exp (add_gdb_index, ensure_gdb_index): Add "style" diff --git a/gdb/testsuite/gdb.cp/method-call-in-c.cc b/gdb/testsuite/gdb.cp/method-call-in-c.cc new file mode 100644 index 0000000000..09e4285ed5 --- /dev/null +++ b/gdb/testsuite/gdb.cp/method-call-in-c.cc @@ -0,0 +1,44 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2021 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 . */ + +struct baz_type +{ + int a = 0; + int b = 1; + int c = 2; +}; + +struct foo_type +{ + int func (baz_type b, float f) + { + return var++; + } + + int var = 123; +}; + +int +main (void) +{ + baz_type b = {}; + float f = 1.0; + + foo_type foo; + + return foo.func (b, f); /* Break here. */ +} diff --git a/gdb/testsuite/gdb.cp/method-call-in-c.exp b/gdb/testsuite/gdb.cp/method-call-in-c.exp new file mode 100644 index 0000000000..0e6851b347 --- /dev/null +++ b/gdb/testsuite/gdb.cp/method-call-in-c.exp @@ -0,0 +1,43 @@ +# Copyright 2021 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 . + +# Ensure that calling a member function works correctly even when the +# language is forced to 'C' (this should be fine, so long at no +# overload resolution is required), or when overload-resolution is +# off. + +standard_testfile .cc + +if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } { + return -1 +} + +if ![runto_main] then { + return 0 +} + +gdb_breakpoint [gdb_get_line_number "Break here"] +gdb_continue_to_breakpoint "Break here" + +set result 123 +foreach_with_prefix lang { c++ c } { + foreach_with_prefix overload_resolution { on off } { + gdb_test_no_output "set overload-resolution ${overload_resolution}" + gdb_test "set language ${lang}" + + gdb_test "print foo.func (b, f)" " = ${result}" + incr result + } +} diff --git a/gdb/valops.c b/gdb/valops.c index 8694c124b5..2b57930420 100644 --- a/gdb/valops.c +++ b/gdb/valops.c @@ -2181,6 +2181,10 @@ search_struct_field (const char *name, struct value *arg1, ARG1 by OFFSET bytes, and search in it assuming it has (class) type TYPE. + The ARGS array is a list of argument values used to help finding NAME, + though ARGS can be nullptr. If ARGS is not nullptr then the list itself + must have a NULL at the end. + If found, return value, else if name matched and args not return (value) -1, else return NULL. */ @@ -2309,7 +2313,8 @@ search_struct_method (const char *name, struct value **arg1p, ERR is used in the error message if *ARGP's type is wrong. C++: ARGS is a list of argument types to aid in the selection of - an appropriate method. Also, handle derived types. + an appropriate method. Also, handle derived types. The array ARGS must + have a NULL at the end. STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location where the truthvalue of whether the function that was resolved was