gdb/fortran: don't access non-existent type fields

When attempting to call a Fortran function for which there is no debug
information we currently trigger undefined behaviour in GDB by
accessing non-existent type fields.

The reason is that in order to prepare the arguments, for a call to a
Fortran function, we need to know the type of each argument.  If the
function being called has no debug information then obviously GDB
doesn't know about the argument types and we should either give the
user an error or pick a suitable default.  What we currently do is
just assume the field exist and access undefined memory, which is
clearly wrong.

The reason GDB needs to know the argument type is to tell if the
argument is artificial or not, artificial arguments will be passed by
value while non-artificial arguments will be passed by reference.

An ideal solution for this problem would be to allow the user to cast
the function to the correct type, we already do this to some degree
with the return value, for example:

  (gdb) print some_func_ ()
  'some_func_' has unknown return type; cast the call to its declared return type
  (gdb) print (integer) some_func_ ()
  $1 = 1

But if we could extend this to allow casting to the full function
type, GDB could figure out from the signature what are real
parameters, and what are artificial parameters.  Maybe something like
this:

  (gdb) print ((integer () (integer, double)) some_other_func_ (1, 2.3)

Alas, right now the Fortran expression parser doesn't seem to support
parsing function signatures, and we certainly don't have support for
figuring out real vs artificial arguments from a signature.

Still, I think we can prevent GDB from accessing undefined memory and
provide a reasonable default behaviour.

In this commit I:

  - Only ask if the argument is artificial if the type of the argument
  is actually known.

  - Unknown arguments are assumed to be artificial and passed by
  value (non-artificial arguments are pass by reference).

  - If an artificial argument is prefixed with '&' by the user then we
  treat the argument as pass-by-reference.

With these three changes we avoid undefined behaviour in GDB, and
allow the user, in most cases, to get a reasonably natural default
behaviour.

gdb/ChangeLog:

	PR fortran/26155
	* f-lang.c (fortran_argument_convert): Delete declaration.
	(fortran_prepare_argument): New function.
	(evaluate_subexp_f): Move logic to new function
	fortran_prepare_argument.

gdb/testsuite/ChangeLog:

	PR fortran/26155
	* gdb.fortran/call-no-debug-func.f90: New file.
	* gdb.fortran/call-no-debug-prog.f90: New file.
	* gdb.fortran/call-no-debug.exp: New file.
This commit is contained in:
Andrew Burgess 2020-11-13 10:39:23 +00:00
parent faeb9f13c1
commit 68337b8be3
6 changed files with 243 additions and 18 deletions

View file

@ -68,8 +68,10 @@ show_fortran_array_slicing_debug (struct ui_file *file, int from_tty,
/* Local functions */
static struct value *fortran_argument_convert (struct value *value,
bool is_artificial);
static value *fortran_prepare_argument (struct expression *exp, int *pos,
int arg_num, bool is_internal_call_p,
struct type *func_type,
enum noside noside);
/* Return the encoding that should be used for the character type
TYPE. */
@ -1278,22 +1280,11 @@ evaluate_subexp_f (struct type *expect_type, struct expression *exp,
int tem = 1;
for (; tem <= nargs; tem++)
{
argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
/* Arguments in Fortran are passed by address. Coerce the
arguments here rather than in value_arg_coerce as
otherwise the call to malloc to place the non-lvalue
parameters in target memory is hit by this Fortran
specific logic. This results in malloc being called
with a pointer to an integer followed by an attempt to
malloc the arguments to malloc in target memory.
Infinite recursion ensues. */
if (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC)
{
bool is_artificial
= TYPE_FIELD_ARTIFICIAL (value_type (arg1), tem - 1);
argvec[tem] = fortran_argument_convert (argvec[tem],
is_artificial);
}
bool is_internal_func = (code == TYPE_CODE_INTERNAL_FUNCTION);
argvec[tem]
= fortran_prepare_argument (exp, pos, (tem - 1),
is_internal_func,
value_type (arg1), noside);
}
argvec[tem] = 0; /* signal end of arglist */
if (noside == EVAL_SKIP)
@ -1780,6 +1771,59 @@ fortran_argument_convert (struct value *value, bool is_artificial)
return value;
}
/* Prepare (and return) an argument value ready for an inferior function
call to a Fortran function. EXP and POS are the expressions describing
the argument to prepare. ARG_NUM is the argument number being
prepared, with 0 being the first argument and so on. FUNC_TYPE is the
type of the function being called.
IS_INTERNAL_CALL_P is true if this is a call to a function of type
TYPE_CODE_INTERNAL_FUNCTION, otherwise this parameter is false.
NOSIDE has its usual meaning for expression parsing (see eval.c).
Arguments in Fortran are normally passed by address, we coerce the
arguments here rather than in value_arg_coerce as otherwise the call to
malloc (to place the non-lvalue parameters in target memory) is hit by
this Fortran specific logic. This results in malloc being called with a
pointer to an integer followed by an attempt to malloc the arguments to
malloc in target memory. Infinite recursion ensues. */
static value *
fortran_prepare_argument (struct expression *exp, int *pos,
int arg_num, bool is_internal_call_p,
struct type *func_type, enum noside noside)
{
if (is_internal_call_p)
return evaluate_subexp_with_coercion (exp, pos, noside);
bool is_artificial = ((arg_num >= func_type->num_fields ())
? true
: TYPE_FIELD_ARTIFICIAL (func_type, arg_num));
/* If this is an artificial argument, then either, this is an argument
beyond the end of the known arguments, or possibly, there are no known
arguments (maybe missing debug info).
For these artificial arguments, if the user has prefixed it with '&'
(for address-of), then lets always allow this to succeed, even if the
argument is not actually in inferior memory. This will allow the user
to pass arguments to a Fortran function even when there's no debug
information.
As we already pass the address of non-artificial arguments, all we
need to do if skip the UNOP_ADDR operator in the expression and mark
the argument as non-artificial. */
if (is_artificial && exp->elts[*pos].opcode == UNOP_ADDR)
{
(*pos)++;
is_artificial = false;
}
struct value *arg_val = evaluate_subexp_with_coercion (exp, pos, noside);
return fortran_argument_convert (arg_val, is_artificial);
}
/* See f-lang.h. */
struct type *