https://gcc.gnu.org/g:acdcf0ea95e5edaaf14391fbb532a8eeb31245b4
commit r17-6052-gacdcf0ea95e5edaaf14391fbb532a8eeb31245b4 Author: Kyrylo Tkachov <[email protected]> Date: Wed Nov 4 00:44:42 2026 -0800 Check the arguments of internal function calls verify_gimple_call does not check the arguments of internal function calls. The GIMPLE front end also builds these calls without checks. Thus invalid input, for example a signed argument to .CLZ or a call with a wrong number of arguments, causes an ICE in a later pass instead of an error. Add check_internal_fn_args. For a direct internal function it checks that the number of arguments is in the range that direct_internal_fn_info records. For IFN_CLZ and IFN_CTZ it also checks that the first argument has an unsigned integer type or an integer vector type with unsigned elements, and that the optional second argument is an integer constant. For invalid arguments it reports an error at the given location and returns false. Checks for other functions can go into its switch. Call check_internal_fn_args from verify_gimple_call and from the GIMPLE front end before it builds the call. To prevent follow-on errors, the front end does not check a call that has an invalid argument, and does not report a missing '=' after an invalid statement. For example, the GIMPLE front end now gives for r = .CLZ (x); with a signed int x: error: '.CLZ' call with an argument that is not unsigned integer Bootstrapped and tested on aarch64-none-linux-gnu and x86_64-linux. gcc/ChangeLog: * internal-fn.cc (check_internal_fn_args): New function. * internal-fn.h (check_internal_fn_args): Declare. * tree-cfg.cc: Include internal-fn.h. (verify_gimple_call): Call check_internal_fn_args for internal function calls. * doc/ifn.texi (IFN_CLZ, IFN_CTZ): Document the type of the first argument. gcc/c/ChangeLog: * gimple-parser.cc (c_parser_gimple_statement): Do not report a missing '=' after an invalid expression. (c_parser_gimple_call_internal): Call check_internal_fn_args. Do not build a call that has an invalid argument. gcc/testsuite/ChangeLog: * gcc.dg/gimplefe-62.c: New test. * gcc.dg/gimplefe-error-17.c: New test. Signed-off-by: Kyrylo Tkachov <[email protected]> Diff: --- gcc/c/gimple-parser.cc | 9 +++- gcc/doc/ifn.texi | 12 +++-- gcc/internal-fn.cc | 52 +++++++++++++++++++++ gcc/internal-fn.h | 2 + gcc/testsuite/gcc.dg/gimplefe-62.c | 27 +++++++++++ gcc/testsuite/gcc.dg/gimplefe-error-17.c | 77 ++++++++++++++++++++++++++++++++ gcc/tree-cfg.cc | 11 +++++ 7 files changed, 185 insertions(+), 5 deletions(-) diff --git a/gcc/c/gimple-parser.cc b/gcc/c/gimple-parser.cc index 0e0c8ba673b0..1d4d1ad11ef7 100644 --- a/gcc/c/gimple-parser.cc +++ b/gcc/c/gimple-parser.cc @@ -791,6 +791,11 @@ c_parser_gimple_statement (gimple_parser &parser, gimple_seq *seq) loc = EXPR_LOCATION (lhs.value); rhs.set_error (); + /* The parser already reported the error in an invalid statement. */ + if (c_parser_next_token_is (parser, CPP_SEMICOLON) + && lhs.value == error_mark_node) + return; + /* GIMPLE call statement without LHS. */ if (c_parser_next_token_is (parser, CPP_SEMICOLON) && TREE_CODE (lhs.value) == CALL_EXPR) @@ -1439,7 +1444,9 @@ c_parser_gimple_call_internal (gimple_parser &parser) c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); if (ifn == IFN_LAST) error_at (loc, "unknown internal function %qE", id); - else + else if (!exprlist.contains (error_mark_node) + && check_internal_fn_args (loc, ifn, exprlist.length (), + exprlist.address ())) { expr.value = build_call_expr_internal_loc_array (loc, ifn, void_type_node, exprlist.length (), diff --git a/gcc/doc/ifn.texi b/gcc/doc/ifn.texi index 7d58c2233739..8b1b5842d69e 100644 --- a/gcc/doc/ifn.texi +++ b/gcc/doc/ifn.texi @@ -1088,8 +1088,10 @@ instruction pattern for integer and integer vector modes. @cindex @code{IFN_CLZ} internal function @item @samp{IFN_CLZ} The @code{IFN_CLZ} internal function directly maps to the @code{clz} -instruction pattern for integer and integer vector modes. The optional -second argument is an @code{int} constant that gives the result for zero. +instruction pattern for integer and integer vector modes. The first +argument has an unsigned integer type or an integer vector type with +unsigned elements. The optional second argument is an @code{int} +constant that gives the result for zero. Note, this is always a scalar value even for vector modes where it represents the scalar value of a lane. For a scalar call it can be any value. If the instruction gives another value for zero, the expansion compares the first @@ -1100,8 +1102,10 @@ the second argument the result for zero is undefined. @cindex @code{IFN_CTZ} internal function @item @samp{IFN_CTZ} The @code{IFN_CTZ} internal function directly maps to the @code{ctz} -instruction pattern for integer and integer vector modes. The optional -second argument is an @code{int} constant that gives the result for zero. +instruction pattern for integer and integer vector modes. The first +argument has an unsigned integer type or an integer vector type with +unsigned elements. The optional second argument is an @code{int} +constant that gives the result for zero. Note, this is always a scalar value even for vector modes where it represents the scalar value of a lane. For a scalar call it can be any value. If the instruction gives another value for zero, the expansion compares the first diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index fb859404c302..a2b5a9ea1042 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -4836,6 +4836,58 @@ widening_evenodd_fn_p (code_helper code) } } +/* Check the NARGS arguments ARGS of a call to internal function FN. + Return true if they are valid. Otherwise report an error at LOC + and return false. */ + +bool +check_internal_fn_args (location_t loc, internal_fn fn, unsigned int nargs, + const tree *args) +{ + const char *name = internal_fn_name (fn); + if (direct_internal_fn_p (fn)) + { + const direct_internal_fn_info &info = direct_internal_fn (fn); + if (nargs < info.nargs_min) + { + error_at (loc, "%<.%s%> call with too few arguments", name); + return false; + } + if (nargs > info.nargs_max) + { + error_at (loc, "%<.%s%> call with too many arguments", name); + return false; + } + } + + switch (fn) + { + case IFN_CLZ: + case IFN_CTZ: + { + tree type = TREE_TYPE (args[0]); + if (!(INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) + || !TYPE_UNSIGNED (type)) + { + error_at (loc, "%<.%s%> call with an argument that is not " + "unsigned integer", name); + return false; + } + if (nargs == 2 && TREE_CODE (args[1]) != INTEGER_CST) + { + error_at (loc, "%<.%s%> call with a value at zero that is not " + "constant", name); + return false; + } + break; + } + + default: + break; + } + return true; +} + /* Return true if IFN_SET_EDOM is supported. */ bool diff --git a/gcc/internal-fn.h b/gcc/internal-fn.h index e8f4829a346a..ca7cd3cf9a98 100644 --- a/gcc/internal-fn.h +++ b/gcc/internal-fn.h @@ -236,6 +236,8 @@ extern bool widening_fn_p (code_helper); extern bool widening_evenodd_fn_p (code_helper); extern bool set_edom_supported_p (void); +extern bool check_internal_fn_args (location_t, internal_fn, unsigned int, + const tree *); extern internal_fn get_conditional_internal_fn (tree_code); extern internal_fn get_conditional_internal_fn (internal_fn); diff --git a/gcc/testsuite/gcc.dg/gimplefe-62.c b/gcc/testsuite/gcc.dg/gimplefe-62.c new file mode 100644 index 000000000000..2f11b52ec12b --- /dev/null +++ b/gcc/testsuite/gcc.dg/gimplefe-62.c @@ -0,0 +1,27 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fgimple -fdump-tree-ssa -Wno-psabi" } */ + +/* The calls are dead, so no target needs to expand them. */ + +typedef unsigned int v4usi __attribute__ ((vector_size (16))); + +void __GIMPLE +f (unsigned int x, v4usi y) +{ + int r1; + int r2; + v4usi r3; + int r4; + int r5; + + r1 = .CLZ (x, 32); + r2 = .CTZ (x); + r3 = .CTZ (y); + r4 = .POPCOUNT (x); + r5 = .POPCOUNT (x, 0); + return; +} + +/* { dg-final { scan-tree-dump-times {\.CLZ \(} 1 "ssa" } } */ +/* { dg-final { scan-tree-dump-times {\.CTZ \(} 2 "ssa" } } */ +/* { dg-final { scan-tree-dump-times {\.POPCOUNT \(} 2 "ssa" } } */ diff --git a/gcc/testsuite/gcc.dg/gimplefe-error-17.c b/gcc/testsuite/gcc.dg/gimplefe-error-17.c new file mode 100644 index 000000000000..3db790fbc8a1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/gimplefe-error-17.c @@ -0,0 +1,77 @@ +/* { dg-do compile } */ +/* { dg-options "-fgimple -Wno-psabi" } */ + +typedef int v4si __attribute__ ((vector_size (16))); + +int __GIMPLE +f1 (int x) +{ + int r; + r = .CLZ (x); /* { dg-error "'.CLZ' call with an argument that is not unsigned integer" } */ + return r; +} + +v4si __GIMPLE +f2 (v4si x) +{ + v4si r; + r = .CTZ (x); /* { dg-error "'.CTZ' call with an argument that is not unsigned integer" } */ + return r; +} + +int __GIMPLE +f3 (unsigned int x) +{ + int r; + r = .CLZ (x, 32, 0); /* { dg-error "'.CLZ' call with too many arguments" } */ + return r; +} + +int __GIMPLE +f4 (unsigned int x, int y) +{ + int r; + r = .CTZ (x, y); /* { dg-error "'.CTZ' call with a value at zero that is not constant" } */ + return r; +} + +int __GIMPLE +f5 () +{ + int r; + r = .CLZ (); /* { dg-error "'.CLZ' call with too few arguments" } */ + return r; +} + +double __GIMPLE +f6 (double x) +{ + double r; + r = .FMA (x, x); /* { dg-error "'.FMA' call with too few arguments" } */ + return r; +} + +double __GIMPLE +f7 (double x) +{ + double r; + r = .SQRT (x, x); /* { dg-error "'.SQRT' call with too many arguments" } */ + return r; +} + +void __GIMPLE +f8 (int x) +{ + .CTZ (x); /* { dg-error "'.CTZ' call with an argument that is not unsigned integer" } */ + return; +} + +int __GIMPLE +f9 (unsigned int x) +{ + int r; + r = .CLZ (y); /* { dg-error "'y' undeclared" } */ + /* { dg-message "each undeclared identifier" "" { target *-*-* } .-1 } */ + .CTZ (y); + return r; +} diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc index 93a830f0eb84..a851b6c21f05 100644 --- a/gcc/tree-cfg.cc +++ b/gcc/tree-cfg.cc @@ -59,6 +59,7 @@ along with GCC; see the file COPYING3. If not see #include "omp-expand.h" #include "tree-cfgcleanup.h" #include "gimplify.h" +#include "internal-fn.h" #include "attribs.h" #include "selftest.h" #include "opts.h" @@ -3507,6 +3508,16 @@ verify_gimple_call (gcall *stmt) "outside of the pass that introduced them"); return true; } + + if (gimple_call_internal_p (stmt)) + { + unsigned nargs = gimple_call_num_args (stmt); + tree *args = nargs ? gimple_call_arg_ptr (stmt, 0) : NULL; + if (!check_internal_fn_args (gimple_location (stmt), + gimple_call_internal_fn (stmt), nargs, args)) + return true; + } + /* ??? The C frontend passes unpromoted arguments in case it didn't see a function declaration before the call. So for now leave the call arguments mostly unverified. Once we gimplify
