aboutsummaryrefslogtreecommitdiff
path: root/gcc/calls.c
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/calls.c')
-rw-r--r--gcc/calls.c274
1 files changed, 139 insertions, 135 deletions
diff --git a/gcc/calls.c b/gcc/calls.c
index b7bbec5..7b45b9a 100644
--- a/gcc/calls.c
+++ b/gcc/calls.c
@@ -1197,92 +1197,144 @@ alloc_max_size (void)
{
alloc_object_size_limit = TYPE_MAX_VALUE (ssizetype);
- unsigned HOST_WIDE_INT unit = 1;
-
- char *end;
- errno = 0;
- unsigned HOST_WIDE_INT limit
- = warn_alloc_size_limit ? strtoull (warn_alloc_size_limit, &end, 10) : 0;
-
- if (limit && !errno)
+ if (warn_alloc_size_limit)
{
- if (end && *end)
+ char *end = NULL;
+ errno = 0;
+ unsigned HOST_WIDE_INT unit = 1;
+ unsigned HOST_WIDE_INT limit
+ = strtoull (warn_alloc_size_limit, &end, 10);
+
+ if (!errno)
{
- /* Numeric option arguments are at most INT_MAX. Make it
- possible to specify a larger value by accepting common
- suffixes. */
- if (!strcmp (end, "kB"))
- unit = 1000;
- else if (!strcasecmp (end, "KiB") || strcmp (end, "KB"))
- unit = 1024;
- else if (!strcmp (end, "MB"))
- unit = 1000LU * 1000;
- else if (!strcasecmp (end, "MiB"))
- unit = 1024LU * 1024;
- else if (!strcasecmp (end, "GB"))
- unit = 1000LU * 1000 * 1000;
- else if (!strcasecmp (end, "GiB"))
- unit = 1024LU * 1024 * 1024;
- else if (!strcasecmp (end, "TB"))
- unit = 1000LU * 1000 * 1000 * 1000;
- else if (!strcasecmp (end, "TiB"))
- unit = 1024LU * 1024 * 1024 * 1024;
- else if (!strcasecmp (end, "PB"))
- unit = 1000LU * 1000 * 1000 * 1000 * 1000;
- else if (!strcasecmp (end, "PiB"))
- unit = 1024LU * 1024 * 1024 * 1024 * 1024;
- else if (!strcasecmp (end, "EB"))
- unit = 1000LU * 1000 * 1000 * 1000 * 1000 * 1000;
- else if (!strcasecmp (end, "EiB"))
- unit = 1024LU * 1024 * 1024 * 1024 * 1024 * 1024;
- else
- unit = 0;
- }
+ if (end && *end)
+ {
+ /* Numeric option arguments are at most INT_MAX. Make it
+ possible to specify a larger value by accepting common
+ suffixes. */
+ if (!strcmp (end, "kB"))
+ unit = 1000;
+ else if (!strcasecmp (end, "KiB") || strcmp (end, "KB"))
+ unit = 1024;
+ else if (!strcmp (end, "MB"))
+ unit = 1000LU * 1000;
+ else if (!strcasecmp (end, "MiB"))
+ unit = 1024LU * 1024;
+ else if (!strcasecmp (end, "GB"))
+ unit = 1000LU * 1000 * 1000;
+ else if (!strcasecmp (end, "GiB"))
+ unit = 1024LU * 1024 * 1024;
+ else if (!strcasecmp (end, "TB"))
+ unit = 1000LU * 1000 * 1000 * 1000;
+ else if (!strcasecmp (end, "TiB"))
+ unit = 1024LU * 1024 * 1024 * 1024;
+ else if (!strcasecmp (end, "PB"))
+ unit = 1000LU * 1000 * 1000 * 1000 * 1000;
+ else if (!strcasecmp (end, "PiB"))
+ unit = 1024LU * 1024 * 1024 * 1024 * 1024;
+ else if (!strcasecmp (end, "EB"))
+ unit = 1000LU * 1000 * 1000 * 1000 * 1000 * 1000;
+ else if (!strcasecmp (end, "EiB"))
+ unit = 1024LU * 1024 * 1024 * 1024 * 1024 * 1024;
+ else
+ unit = 0;
+ }
- if (unit)
- alloc_object_size_limit = build_int_cst (ssizetype, limit * unit);
+ if (unit)
+ alloc_object_size_limit
+ = build_int_cst (ssizetype, limit * unit);
+ }
}
}
return alloc_object_size_limit;
}
-/* Return true if the type of OP is signed, looking through any casts
- to an unsigned type. */
+/* Return true when EXP's range can be determined and set RANGE[] to it
+ after adjusting it if necessary to make EXP a valid size argument to
+ an allocation function declared with attribute alloc_size (whose
+ argument may be signed), or to a string manipulation function like
+ memset. */
-static bool
-operand_signed_p (tree op)
+bool
+get_size_range (tree exp, tree range[2])
{
- if (TREE_CODE (op) == SSA_NAME)
+ if (tree_fits_uhwi_p (exp))
{
- gimple *def = SSA_NAME_DEF_STMT (op);
- if (is_gimple_assign (def))
- {
- /* In an assignment involving a cast, ignore the type
- of the cast and consider the type of its operand. */
- tree_code code = gimple_assign_rhs_code (def);
- if (code == NOP_EXPR)
- op = gimple_assign_rhs1 (def);
- }
- else if (gimple_code (def) == GIMPLE_PHI)
+ /* EXP is a constant. */
+ range[0] = range[1] = exp;
+ return true;
+ }
+
+ wide_int min, max;
+ enum value_range_type range_type
+ = (TREE_CODE (exp) == SSA_NAME
+ ? get_range_info (exp, &min, &max) : VR_VARYING);
+
+ if (range_type == VR_VARYING)
+ {
+ /* No range information available. */
+ range[0] = NULL_TREE;
+ range[1] = NULL_TREE;
+ return false;
+ }
+
+ tree exptype = TREE_TYPE (exp);
+ unsigned expprec = TYPE_PRECISION (exptype);
+ wide_int wzero = wi::zero (expprec);
+ wide_int wmaxval = wide_int (TYPE_MAX_VALUE (exptype));
+
+ bool signed_p = !TYPE_UNSIGNED (exptype);
+
+ if (range_type == VR_ANTI_RANGE)
+ {
+ if (signed_p)
{
- /* In a phi, a constant argument may be unsigned even
- if in the source it's signed and negative. Ignore
- those and consider the result of a phi signed if
- all its non-constant operands are. */
- unsigned nargs = gimple_phi_num_args (def);
- for (unsigned i = 0; i != nargs; ++i)
+ if (wi::les_p (max, wzero))
{
- tree op = gimple_phi_arg_def (def, i);
- if (TREE_CODE (op) != INTEGER_CST
- && !operand_signed_p (op))
- return false;
+ /* EXP is not in a strictly negative range. That means
+ it must be in some (not necessarily strictly) positive
+ range which includes zero. Since in signed to unsigned
+ conversions negative values end up converted to large
+ positive values, and otherwise they are not valid sizes,
+ the resulting range is in both cases [0, TYPE_MAX]. */
+ min = wzero;
+ max = wmaxval;
}
-
- return true;
+ else if (wi::les_p (min - 1, wzero))
+ {
+ /* EXP is not in a negative-positive range. That means EXP
+ is either negative, or greater than max. Since negative
+ sizes are invalid make the range [MAX + 1, TYPE_MAX]. */
+ min = max + 1;
+ max = wmaxval;
+ }
+ else
+ {
+ max = min - 1;
+ min = wzero;
+ }
+ }
+ else if (wi::eq_p (wzero, min - 1))
+ {
+ /* EXP is unsigned and not in the range [1, MAX]. That means
+ it's either zero or greater than MAX. Even though 0 would
+ normally be detected by -Walloc-zero set the range to
+ [MAX, TYPE_MAX] so that when MAX is greater than the limit
+ the whole range is diagnosed. */
+ min = max + 1;
+ max = wmaxval;
+ }
+ else
+ {
+ max = min - 1;
+ min = wzero;
}
}
- return !TYPE_UNSIGNED (TREE_TYPE (op));
+ range[0] = wide_int_to_tree (exptype, min);
+ range[1] = wide_int_to_tree (exptype, max);
+
+ return true;
}
/* Diagnose a call EXP to function FN decorated with attribute alloc_size
@@ -1316,8 +1368,8 @@ maybe_warn_alloc_args_overflow (tree fn, tree exp, tree args[2], int idx[2])
if (tree_int_cst_lt (args[i], integer_zero_node))
{
warned = warning_at (loc, OPT_Walloc_size_larger_than_,
- "argument %i value %qE is negative",
- idx[i] + 1, args[i]);
+ "%Kargument %i value %qE is negative",
+ exp, idx[i] + 1, args[i]);
}
else if (integer_zerop (args[i]))
{
@@ -1334,8 +1386,8 @@ maybe_warn_alloc_args_overflow (tree fn, tree exp, tree args[2], int idx[2])
&& !lookup_attribute ("returns_nonnull",
TYPE_ATTRIBUTES (TREE_TYPE (fn)))))
warned = warning_at (loc, OPT_Walloc_zero,
- "argument %i value is zero",
- idx[i] + 1);
+ "%Kargument %i value is zero",
+ exp, idx[i] + 1);
}
else if (tree_int_cst_lt (maxobjsize, args[i]))
{
@@ -1351,79 +1403,31 @@ maybe_warn_alloc_args_overflow (tree fn, tree exp, tree args[2], int idx[2])
continue;
warned = warning_at (loc, OPT_Walloc_size_larger_than_,
- "argument %i value %qE exceeds "
+ "%Kargument %i value %qE exceeds "
"maximum object size %E",
- idx[i] + 1, args[i], maxobjsize);
+ exp, idx[i] + 1, args[i], maxobjsize);
}
}
- else if (TREE_CODE (args[i]) == SSA_NAME)
+ else if (TREE_CODE (args[i]) == SSA_NAME
+ && get_size_range (args[i], argrange[i]))
{
- tree type = TREE_TYPE (args[i]);
-
- wide_int min, max;
- value_range_type range_type = get_range_info (args[i], &min, &max);
- if (range_type == VR_RANGE)
- {
- argrange[i][0] = wide_int_to_tree (type, min);
- argrange[i][1] = wide_int_to_tree (type, max);
- }
- else if (range_type == VR_ANTI_RANGE)
- {
- /* For an anti-range, if the type of the formal argument
- is unsigned and the bounds of the range are of opposite
- signs when interpreted as signed, check to see if the
- type of the actual argument is signed. If so, the lower
- bound must be taken to be zero (rather than a large
- positive value corresonding to the actual lower bound
- interpreted as unsigned) and there is nothing else that
- can be inferred from it. */
- --min;
- ++max;
- wide_int zero = wi::uhwi (0, TYPE_PRECISION (type));
- if (TYPE_UNSIGNED (type)
- && wi::lts_p (zero, min) && wi::lts_p (max, zero)
- && operand_signed_p (args[i]))
- continue;
-
- argrange[i][0] = wide_int_to_tree (type, max);
- argrange[i][1] = wide_int_to_tree (type, min);
-
- /* Verify that the anti-range doesn't make all arguments
- invalid (treat the anti-range ~[0, 0] as invalid). */
- if (tree_int_cst_lt (maxobjsize, argrange[i][0])
- && tree_int_cst_le (argrange[i][1], integer_zero_node))
- {
- warned
- = warning_at (loc, OPT_Walloc_size_larger_than_,
- (TYPE_UNSIGNED (type)
- ? G_("argument %i range [%E, %E] exceeds "
- "maximum object size %E")
- : G_("argument %i range [%E, %E] is both "
- "negative and exceeds maximum object "
- "size %E")),
- idx[i] + 1, argrange[i][0],
- argrange[i][1], maxobjsize);
- }
- continue;
- }
- else
- continue;
-
/* Verify that the argument's range is not negative (including
upper bound of zero). */
if (tree_int_cst_lt (argrange[i][0], integer_zero_node)
&& tree_int_cst_le (argrange[i][1], integer_zero_node))
{
warned = warning_at (loc, OPT_Walloc_size_larger_than_,
- "argument %i range [%E, %E] is negative",
- idx[i] + 1, argrange[i][0], argrange[i][1]);
+ "%Kargument %i range [%E, %E] is negative",
+ exp, idx[i] + 1,
+ argrange[i][0], argrange[i][1]);
}
else if (tree_int_cst_lt (maxobjsize, argrange[i][0]))
{
warned = warning_at (loc, OPT_Walloc_size_larger_than_,
- "argument %i range [%E, %E] exceeds "
+ "%Kargument %i range [%E, %E] exceeds "
"maximum object size %E",
- idx[i] + 1, argrange[i][0], argrange[i][1],
+ exp, idx[i] + 1,
+ argrange[i][0], argrange[i][1],
maxobjsize);
}
}
@@ -1450,15 +1454,15 @@ maybe_warn_alloc_args_overflow (tree fn, tree exp, tree args[2], int idx[2])
if (vflow)
warned = warning_at (loc, OPT_Walloc_size_larger_than_,
- "product %<%E * %E%> of arguments %i and %i "
+ "%Kproduct %<%E * %E%> of arguments %i and %i "
"exceeds %<SIZE_MAX%>",
- argrange[0][0], argrange[1][0],
+ exp, argrange[0][0], argrange[1][0],
idx[0] + 1, idx[1] + 1);
else if (wi::ltu_p (wi::to_wide (maxobjsize, szprec), prod))
warned = warning_at (loc, OPT_Walloc_size_larger_than_,
- "product %<%E * %E%> of arguments %i and %i "
+ "%Kproduct %<%E * %E%> of arguments %i and %i "
"exceeds maximum object size %E",
- argrange[0][0], argrange[1][0],
+ exp, argrange[0][0], argrange[1][0],
idx[0] + 1, idx[1] + 1,
maxobjsize);