Skip to content

Commit f202555

Browse files
nv-cmeerwDarkArc
authored andcommitted
GNU/Clang compatibility: argument checking for arithmetic overflow builtins [GH #325]
1 parent f51894d commit f202555

17 files changed

Lines changed: 1185 additions & 216 deletions

File tree

‎src/expr.c‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5931,6 +5931,7 @@ typedef a_routine_ptr a_builtin_call_adjustment_callback(
59315931
static a_builtin_call_adjustment_callback
59325932
adjust_sync_atomic_builtin,
59335933
adjust_builtin_zero_non_value_bits,
5934+
adjust_overflow_builtin,
59345935
adjust_elementwise_or_reduce_builtin,
59355936
adjust_type_generic_builtin,
59365937
adjust_return_type_to_type_of_first_argument,
@@ -6220,6 +6221,14 @@ be called to check and adjust the argument and routine types as needed.
62206221
/* Callback will validate arguments. */
62216222
bcap->callback = adjust_builtin_zero_non_value_bits;
62226223
break;
6224+
case bfk_add_overflow:
6225+
case bfk_add_overflow_p:
6226+
case bfk_sub_overflow:
6227+
case bfk_sub_overflow_p:
6228+
case bfk_mul_overflow:
6229+
case bfk_mul_overflow_p:
6230+
bcap->callback = adjust_overflow_builtin;
6231+
break;
62236232
case bfk_source_location:
62246233
{ /* The source location builtin has been used, require and process the
62256234
GNU std::source_location::__impl type here. Note that this result
@@ -6894,6 +6903,85 @@ be a pointer to a complete type and may not be const-qualified.
68946903
} /* adjust_builtin_zero_non_value_bits */
68956904

68966905

6906+
static a_routine_ptr adjust_overflow_builtin(
6907+
an_operand *target,
6908+
an_arg_list_elem_ptr args,
6909+
a_source_position *closing_paren_position,
6910+
ARG_UNUSED a_builtin_call_adjustment *bcap,
6911+
an_expr_node_ptr *arg_list)
6912+
/*
6913+
Check the arguments for the generic arithmetic overflow builtin described by
6914+
target. The operands must be integers or enums (excluding scoped enums in
6915+
Clang mode). The result must point to a non-const integer, or be an integer
6916+
for the _p form. GCC excludes bool and enum results; Clang also allows bool
6917+
and unscoped enums. Use closing_paren_position for missing-argument
6918+
diagnostics. bcap is unused. Return the routine to call and set *arg_list to
6919+
the checked argument expressions, or return NULL on error.
6920+
*/
6921+
{
6922+
a_routine_ptr rout = routine_from_function_operand(target);
6923+
a_boolean err = FALSE;
6924+
a_boolean result_is_pointer;
6925+
int arg_index = 0;
6926+
6927+
*arg_list = NULL;
6928+
result_is_pointer =
6929+
rout->variant.builtin_function_kind != bfk_add_overflow_p &&
6930+
rout->variant.builtin_function_kind != bfk_sub_overflow_p &&
6931+
rout->variant.builtin_function_kind != bfk_mul_overflow_p;
6932+
if (args == NULL || args->next == NULL || args->next->next == NULL) {
6933+
expr_pos_error(ec_too_few_arguments, closing_paren_position);
6934+
err = TRUE;
6935+
} else if (args->next->next->next != NULL) {
6936+
expr_pos_error(ec_too_many_arguments,
6937+
init_component_pos(args->next->next->next));
6938+
err = TRUE;
6939+
} else {
6940+
an_arg_list_elem_ptr arg;
6941+
for (arg = args; arg != NULL; arg = arg->next, ++arg_index) {
6942+
an_operand_ptr operand;
6943+
a_type_ptr arg_type;
6944+
6945+
operand = operand_of_arg_list_elem(arg);
6946+
do_operand_transformations(operand, TOPT_NO_OPTIONS);
6947+
arg_type = skip_typerefs(operand->type);
6948+
if (is_error_type(arg_type)) {
6949+
/* An error argument. */
6950+
} else if (arg_index < 2) {
6951+
if (!is_integral_or_enum_type(arg_type) ||
6952+
(clang_mode && is_scoped_enum_type(arg_type))) {
6953+
expr_pos_ty_error(ec_invalid_type_for_builtin,
6954+
init_component_pos(arg), arg_type);
6955+
err = TRUE;
6956+
} /* if */
6957+
} else {
6958+
a_type_ptr result_type = arg_type;
6959+
if (result_is_pointer && is_pointer_type(arg_type)) {
6960+
result_type = type_pointed_to(arg_type);
6961+
} /* if */
6962+
if ((result_is_pointer && !is_pointer_type(arg_type)) ||
6963+
(clang_mode ? !is_integral_or_unscoped_enum_type(result_type) :
6964+
(!is_integral_type(result_type) ||
6965+
is_enum_type(result_type) ||
6966+
is_bool_type(result_type))) ||
6967+
(result_is_pointer && is_const_qualified_type(result_type))) {
6968+
expr_pos_ty_error(ec_invalid_type_for_builtin,
6969+
init_component_pos(arg), arg_type);
6970+
err = TRUE;
6971+
} /* if */
6972+
} /* if */
6973+
} /* for */
6974+
} /* if */
6975+
if (err) {
6976+
rout = NULL;
6977+
} else {
6978+
*arg_list = make_expr_list_from_argument_list(
6979+
args, /*dependent_expression=*/FALSE);
6980+
} /* if */
6981+
return rout;
6982+
} /* adjust_overflow_builtin */
6983+
6984+
68976985
static a_boolean convert_to_prvalue_and_check_for_identical_types(
68986986
a_type_ptr tp,
68996987
an_arg_list_elem_ptr arg)

‎src/pending_changes/gh-325.txt‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[GH #325]
2+
GNU/Clang compatibility: argument checking for arithmetic overflow builtins
3+
4+
Previously, calls to __builtin_add_overflow, __builtin_sub_overflow, and
5+
__builtin_mul_overflow (and their GNU _p forms) could be accepted with invalid
6+
argument types or an incorrect number of arguments. These calls are now
7+
properly diagnosed. For example, with --gnu_version=150200:
8+
9+
struct S {};
10+
void f(S s, int *p) {
11+
__builtin_add_overflow(s, 1, p); // Previously accepted, now an error.
12+
}

‎tests/expectations/edg_x86_64/imported.log‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ clang/c/C/C11/n1464.sft.c:fp:-DTEST_NUMBER=1 --c:FAIL:
88
clang/c/C/C11/n1518.sft.c:fp:-DTEST_NUMBER=1 --c11:FAIL:
99
clang/c/C/C11/n1518.sft.c:fp:-DTEST_NUMBER=2 --c23:FAIL:
1010
clang/c/C/C23/n2322.sft.c:fp:-DTEST_NUMBER=1 --c23:FAIL:
11-
clang/c/C/C23/n2683.sft.c:fn:-DTEST_NUMBER=1 --c23:FAIL:
1211
clang/c/C/C23/n2819.sft.c:fp:-DTEST_NUMBER=1 --c23:ABORT:
1312
clang/c/C/C23/n2826.sft.c:fn:-DTEST_NUMBER=1 --c23:FAIL:
1413
clang/c/C/C23/n2886.sft.c:fn:-DTEST_NUMBER=1 --c11:FAIL:
@@ -475,7 +474,6 @@ clang/c/Sema/builtins-hexagon-v60.sft.c:fn:-DTEST_NUMBER=1 -DTEST_V60 -DTEST_V62
475474
clang/c/Sema/builtins-hexagon-v62.sft.c:fn:-DTEST_NUMBER=1 -DTEST_V60 -DTEST_V62 -DTEST_V65 --c:FAIL:
476475
clang/c/Sema/builtins-hvx-v65.sft.c:fp:-DTEST_NUMBER=1 --c:FAIL:
477476
clang/c/Sema/builtins-mips-features.sft.c:fn:-DTEST_NUMBER=1 --c:FAIL:
478-
clang/c/Sema/builtins-overflow.sft.c:fn:-DTEST_NUMBER=1 --c:FAIL:
479477
clang/c/Sema/builtins-ppc.sft.c:fn:-DTEST_NUMBER=1 -DTEST_MAXMIN -DTEST_MAXMIN --c:FAIL:
480478
clang/c/Sema/builtins-x86.sft.c:fn:-DTEST_NUMBER=1 --c:FAIL:
481479
clang/c/Sema/builtins-x86_64.sft.c:fn:-DTEST_NUMBER=1 --c:FAIL:
@@ -3826,7 +3824,6 @@ gnu/c/stack-check-7.sft.c:s:-DTEST_NUMBER=1:SKIPPED:
38263824
gnu/c/stack-check-8.sft.c:s:-DTEST_NUMBER=1:SKIPPED:
38273825
gnu/c/stack-check-9.sft.c:s:-DTEST_NUMBER=1:SKIPPED:
38283826
gnu/c/stack-layout-dynamic-1.sft.c:fp:-DTEST_NUMBER=1:FAIL:
3829-
gnu/c/stdckdint-2.sft.c:fn:-DTEST_NUMBER=1 --c23:FAIL:
38303827
gnu/c/stmt-expr-3.sft.c:fn:-DTEST_NUMBER=1 --c99 --strict_gnu:FAIL:
38313828
gnu/c/stmt-expr-label-1.sft.c:fn:-DTEST_NUMBER=1:CATASTROPHE:
38323829
gnu/c/strict-flex-array-1.sft.c:fn:-DTEST_NUMBER=1:FAIL:

‎tests/tests/imported/clang/c/C/C23/.n2683.rto/default.1.1.txt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,16 @@ fe_only -DTEST_NUMBER=1 --clang -e 200 --no_wrap_diagnostics --clang_version 999
33
char plain_char[] = {U'牛'};
44
^
55

6+
"C/C23/n2683.c", line 47: error: type "char (*)[1]" is invalid as an argument for this builtin
7+
bool flag_nostr = __builtin_sub_overflow((a), (b), (&plain_char));
8+
^
9+
10+
"C/C23/n2683.c", line 49: error: type "int" is invalid as an argument for this builtin
11+
bool flag_noptr = __builtin_add_overflow((a), (b), (result));
12+
^
13+
14+
"C/C23/n2683.c", line 50: error: type "const int *" is invalid as an argument for this builtin
15+
bool flag_noconst = __builtin_sub_overflow((a), (b), (&const_result));
16+
^
17+
18+
3 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/c/C/C23/.n2683.rto/edg_x86_32.1.1.txt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ fe_only -DTEST_NUMBER=1 --clang -e 200 --no_wrap_diagnostics --clang_version 999
77
char plain_char[] = {U'牛'};
88
^
99

10+
"C/C23/n2683.c", line 47: error: type "char (*)[1]" is invalid as an argument for this builtin
11+
bool flag_nostr = __builtin_sub_overflow((a), (b), (&plain_char));
12+
^
13+
14+
"C/C23/n2683.c", line 49: error: type "int" is invalid as an argument for this builtin
15+
bool flag_noptr = __builtin_add_overflow((a), (b), (result));
16+
^
17+
18+
"C/C23/n2683.c", line 50: error: type "const int *" is invalid as an argument for this builtin
19+
bool flag_noconst = __builtin_sub_overflow((a), (b), (&const_result));
20+
^
21+
22+
3 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/c/C/C23/.n2683.rto/edg_x86_32_cp.1.1.txt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ fe_only -DTEST_NUMBER=1 --clang -e 200 --no_wrap_diagnostics --clang_version 999
77
char plain_char[] = {U'牛'};
88
^
99

10+
"C/C23/n2683.c", line 47: error: type "char (*)[1]" is invalid as an argument for this builtin
11+
bool flag_nostr = __builtin_sub_overflow((a), (b), (&plain_char));
12+
^
13+
14+
"C/C23/n2683.c", line 49: error: type "int" is invalid as an argument for this builtin
15+
bool flag_noptr = __builtin_add_overflow((a), (b), (result));
16+
^
17+
18+
"C/C23/n2683.c", line 50: error: type "const int *" is invalid as an argument for this builtin
19+
bool flag_noconst = __builtin_sub_overflow((a), (b), (&const_result));
20+
^
21+
22+
3 errors detected in the compilation of "Test_name.c".
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
fe_only -DTEST_NUMBER=1 --clang -e 200 --no_wrap_diagnostics --clang_version 999999 --c Test_name.c
2+
"Test_name.c", line 16: error: too few arguments in function call
3+
__builtin_add_overflow(); // expected-error {{too few arguments to function call, expected 3, have 0}}
4+
^
5+
6+
"Test_name.c", line 17: error: too many arguments in function call
7+
__builtin_add_overflow(1, 1, 1, 1); // expected-error {{too many arguments to function call, expected 3, have 4}}
8+
^
9+
10+
"Test_name.c", line 19: error: type "const char *" is invalid as an argument for this builtin
11+
__builtin_add_overflow(c, 1, &r); // expected-error {{operand argument to overflow builtin must be an integer type ('const char *' invalid)}}
12+
^
13+
214
"Test_name.c", line 19: warning: variable "c" is used before its value is set
315
__builtin_add_overflow(c, 1, &r); // expected-error {{operand argument to overflow builtin must be an integer type ('const char *' invalid)}}
416
^
517

18+
"Test_name.c", line 20: error: type "const char *" is invalid as an argument for this builtin
19+
__builtin_add_overflow(1, c, &r); // expected-error {{operand argument to overflow builtin must be an integer type ('const char *' invalid)}}
20+
^
21+
22+
"Test_name.c", line 21: error: type "int" is invalid as an argument for this builtin
23+
__builtin_add_overflow(1, 1, 3); // expected-error {{result argument to overflow builtin must be a pointer to a non-const integer type ('int' invalid)}}
24+
^
25+
26+
"Test_name.c", line 22: error: type "float *" is invalid as an argument for this builtin
27+
__builtin_add_overflow(1, 1, &f); // expected-error {{result argument to overflow builtin must be a pointer to a non-const integer type ('float *' invalid)}}
28+
^
29+
30+
"Test_name.c", line 23: error: type "const unsigned int *" is invalid as an argument for this builtin
31+
__builtin_add_overflow(1, 1, &q); // expected-error {{result argument to overflow builtin must be a pointer to a non-const integer type ('const unsigned int *' invalid)}}
32+
^
33+
34+
7 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/gnu/c/.pr70859.rto/default.1.1.txt‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ fe_only -DTEST_NUMBER=1 --gnu_version=999999 -e 200 --no_wrap --no_wrap_diagnost
3131
d = __builtin_assume_aligned (p, n, p); /* { dg-error "39:non-integer argument 3 in call" } */
3232
^
3333

34+
"Test_name.c", line 35: error: type "double" is invalid as an argument for this builtin
35+
b = __builtin_add_overflow (n, *d, &r); /* { dg-error "34:argument 2 in call to function" } */
36+
^
37+
38+
"Test_name.c", line 36: error: type "double *" is invalid as an argument for this builtin
39+
b = __builtin_add_overflow (n, 5, d); /* { dg-error "37:argument 3 in call" } */
40+
^
41+
42+
"Test_name.c", line 37: error: type "double" is invalid as an argument for this builtin
43+
b = __builtin_sub_overflow (n, *d, &r); /* { dg-error "34:argument 2 in call to function" } */
44+
^
45+
46+
"Test_name.c", line 38: error: type "double *" is invalid as an argument for this builtin
47+
b = __builtin_sub_overflow (n, 5, d); /* { dg-error "37:argument 3 in call" } */
48+
^
49+
50+
"Test_name.c", line 39: error: type "double" is invalid as an argument for this builtin
51+
b = __builtin_mul_overflow (n, *d, &r); /* { dg-error "34:argument 2 in call to function" } */
52+
^
53+
54+
"Test_name.c", line 40: error: type "double *" is invalid as an argument for this builtin
55+
b = __builtin_mul_overflow (n, 5, d); /* { dg-error "37:argument 3 in call" } */
56+
^
57+
3458
"Test_name.c", line 46: error: expected an expression
3559
if (__builtin_constant_p ()) /* { dg-error "7:too few" } */
3660
^
@@ -67,8 +91,16 @@ fe_only -DTEST_NUMBER=1 --gnu_version=999999 -e 200 --no_wrap --no_wrap_diagnost
6791
if (__builtin_assume_aligned (p, r, p, p)) /* { dg-error "7:too many" } */
6892
^
6993

94+
"Test_name.c", line 66: error: too few arguments in function call
95+
if (__builtin_add_overflow ()) /* { dg-error "7:too few" } */
96+
^
97+
98+
"Test_name.c", line 68: error: too many arguments in function call
99+
if (__builtin_add_overflow (1, 2, 3, &r)) /* { dg-error "7:too many" } */
100+
^
101+
70102
"Test_name.c", line 9: warning: variable "b" was set but never used
71103
__extension__ static _Bool b;
72104
^
73105

74-
16 errors detected in the compilation of "Test_name.c".
106+
24 errors detected in the compilation of "Test_name.c".

0 commit comments

Comments
 (0)