Default to dwarf version 4 on hppa64-hpux
[official-gcc.git] / gcc / gimple-ssa-warn-alloca.c
blob4fc7125d378e0d26b4e42f42e656d38628cf5011
1 /* Warn on problematic uses of alloca and variable length arrays.
2 Copyright (C) 2016-2021 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <aldyh@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "tree-pass.h"
28 #include "ssa.h"
29 #include "gimple-pretty-print.h"
30 #include "diagnostic-core.h"
31 #include "fold-const.h"
32 #include "gimple-iterator.h"
33 #include "tree-ssa.h"
34 #include "tree-cfg.h"
35 #include "builtins.h"
36 #include "calls.h"
37 #include "cfgloop.h"
38 #include "intl.h"
39 #include "gimple-range.h"
41 static unsigned HOST_WIDE_INT adjusted_warn_limit (bool);
43 const pass_data pass_data_walloca = {
44 GIMPLE_PASS,
45 "walloca",
46 OPTGROUP_NONE,
47 TV_NONE,
48 PROP_cfg, // properties_required
49 0, // properties_provided
50 0, // properties_destroyed
51 0, // properties_start
52 0, // properties_finish
55 class pass_walloca : public gimple_opt_pass
57 public:
58 pass_walloca (gcc::context *ctxt)
59 : gimple_opt_pass(pass_data_walloca, ctxt), xlimit_certain_p (false)
61 opt_pass *clone () { return new pass_walloca (m_ctxt); }
62 void set_pass_param (unsigned int n, bool param)
64 gcc_assert (n == 0);
65 // Set to true to enable only warnings for alloca calls that
66 // are certainly in excess of the limit. This includes calls
67 // with constant arguments but excludes those in ranges (that
68 // can only be determined by range analysis) as well as
69 // the "may be too large" kind.
70 xlimit_certain_p = param;
72 virtual bool gate (function *);
73 virtual unsigned int execute (function *);
75 private:
76 // Set to TRUE the first time we run this pass on a function.
77 bool xlimit_certain_p;
80 bool
81 pass_walloca::gate (function *fun ATTRIBUTE_UNUSED)
83 // Warning is disabled when its size limit is greater than PTRDIFF_MAX
84 // for the target maximum, which makes the limit negative since when
85 // represented in signed HOST_WIDE_INT.
86 unsigned HOST_WIDE_INT max = tree_to_uhwi (TYPE_MAX_VALUE (ptrdiff_type_node));
87 return (adjusted_warn_limit (false) <= max
88 || adjusted_warn_limit (true) <= max);
91 // Possible problematic uses of alloca.
92 enum alloca_type {
93 // Alloca argument is within known bounds that are appropriate.
94 ALLOCA_OK,
96 // Alloca argument is KNOWN to have a value that is too large.
97 ALLOCA_BOUND_DEFINITELY_LARGE,
99 // Alloca argument may be too large.
100 ALLOCA_BOUND_MAYBE_LARGE,
102 // Alloca appears in a loop.
103 ALLOCA_IN_LOOP,
105 // Alloca argument is 0.
106 ALLOCA_ARG_IS_ZERO,
108 // Alloca call is unbounded. That is, there is no controlling
109 // predicate for its argument.
110 ALLOCA_UNBOUNDED
113 // Type of an alloca call with its corresponding limit, if applicable.
114 class alloca_type_and_limit {
115 public:
116 enum alloca_type type;
117 // For ALLOCA_BOUND_MAYBE_LARGE and ALLOCA_BOUND_DEFINITELY_LARGE
118 // types, this field indicates the assumed limit if known or
119 // integer_zero_node if unknown. For any other alloca types, this
120 // field is undefined.
121 wide_int limit;
122 alloca_type_and_limit ();
123 alloca_type_and_limit (enum alloca_type type,
124 wide_int i) : type(type), limit(i) { }
125 alloca_type_and_limit (enum alloca_type type) : type(type)
127 limit = wi::to_wide (integer_zero_node);
131 /* Return TRUE if the user specified a limit for either VLAs or ALLOCAs. */
133 static bool
134 warn_limit_specified_p (bool is_vla)
136 unsigned HOST_WIDE_INT max = is_vla ? warn_vla_limit : warn_alloca_limit;
137 return max != HOST_WIDE_INT_MAX;
140 /* Return the value of the argument N to -Walloca-larger-than= or
141 -Wvla-larger-than= adjusted for the target data model so that
142 when N == HOST_WIDE_INT_MAX, the adjusted value is set to
143 PTRDIFF_MAX on the target. This is done to prevent warnings
144 for unknown/unbounded allocations in the "permissive mode"
145 while still diagnosing excessive and necessarily invalid
146 allocations. */
148 static unsigned HOST_WIDE_INT
149 adjusted_warn_limit (bool idx)
151 static HOST_WIDE_INT limits[2];
152 if (limits[idx])
153 return limits[idx];
155 limits[idx] = idx ? warn_vla_limit : warn_alloca_limit;
156 if (limits[idx] != HOST_WIDE_INT_MAX)
157 return limits[idx];
159 limits[idx] = tree_to_shwi (TYPE_MAX_VALUE (ptrdiff_type_node));
160 return limits[idx];
163 // Analyze the alloca call in STMT and return the alloca type with its
164 // corresponding limit (if applicable). IS_VLA is set if the alloca
165 // call was created by the gimplifier for a VLA.
167 static class alloca_type_and_limit
168 alloca_call_type (gimple *stmt, bool is_vla)
170 gcc_assert (gimple_alloca_call_p (stmt));
171 tree len = gimple_call_arg (stmt, 0);
173 gcc_assert (!is_vla || warn_vla_limit >= 0);
174 gcc_assert (is_vla || warn_alloca_limit >= 0);
176 // Adjust warn_alloca_max_size for VLAs, by taking the underlying
177 // type into account.
178 unsigned HOST_WIDE_INT max_size = adjusted_warn_limit (is_vla);
180 // Check for the obviously bounded case.
181 if (TREE_CODE (len) == INTEGER_CST)
183 if (tree_to_uhwi (len) > max_size)
184 return alloca_type_and_limit (ALLOCA_BOUND_DEFINITELY_LARGE,
185 wi::to_wide (len));
186 if (integer_zerop (len))
188 const offset_int maxobjsize
189 = wi::to_offset (max_object_size ());
190 alloca_type result = (max_size < maxobjsize
191 ? ALLOCA_ARG_IS_ZERO : ALLOCA_OK);
192 return alloca_type_and_limit (result);
195 return alloca_type_and_limit (ALLOCA_OK);
198 struct alloca_type_and_limit ret = alloca_type_and_limit (ALLOCA_OK);
199 // If we have a declared maximum size, we can take it into account.
200 if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX))
202 tree arg = gimple_call_arg (stmt, 2);
203 if (compare_tree_int (arg, max_size) <= 0)
204 ret = alloca_type_and_limit (ALLOCA_OK);
205 else
207 const offset_int maxobjsize
208 = wi::to_offset (max_object_size ());
209 alloca_type result = (max_size < maxobjsize
210 ? ALLOCA_BOUND_MAYBE_LARGE : ALLOCA_OK);
211 ret = alloca_type_and_limit (result, wi::to_wide (arg));
213 return ret;
216 // If the user specified a limit, use it.
217 int_range_max r;
218 if (warn_limit_specified_p (is_vla)
219 && TREE_CODE (len) == SSA_NAME
220 && get_range_query (cfun)->range_of_expr (r, len, stmt)
221 && !r.varying_p ())
223 // The invalid bits are anything outside of [0, MAX_SIZE].
224 static int_range<2> invalid_range (build_int_cst (size_type_node, 0),
225 build_int_cst (size_type_node,
226 max_size),
227 VR_ANTI_RANGE);
229 r.intersect (invalid_range);
230 if (r.undefined_p ())
231 return alloca_type_and_limit (ALLOCA_OK);
233 return alloca_type_and_limit (ALLOCA_BOUND_MAYBE_LARGE,
234 wi::to_wide (integer_zero_node));
237 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
238 /* When MAX_SIZE is greater than or equal to PTRDIFF_MAX treat
239 allocations that aren't visibly constrained as OK, otherwise
240 report them as (potentially) unbounded. */
241 alloca_type unbounded_result = (max_size < maxobjsize.to_uhwi ()
242 ? ALLOCA_UNBOUNDED : ALLOCA_OK);
243 return alloca_type_and_limit (unbounded_result);
246 // Return TRUE if STMT is in a loop, otherwise return FALSE.
248 static bool
249 in_loop_p (gimple *stmt)
251 basic_block bb = gimple_bb (stmt);
252 return
253 bb->loop_father && bb->loop_father->header != ENTRY_BLOCK_PTR_FOR_FN (cfun);
256 unsigned int
257 pass_walloca::execute (function *fun)
259 gimple_ranger *ranger = enable_ranger (fun);
260 basic_block bb;
261 FOR_EACH_BB_FN (bb, fun)
263 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
264 gsi_next (&si))
266 gimple *stmt = gsi_stmt (si);
267 if (!gimple_alloca_call_p (stmt))
268 continue;
270 location_t loc = gimple_nonartificial_location (stmt);
271 loc = expansion_point_location_if_in_system_header (loc);
273 const bool is_vla
274 = gimple_call_alloca_for_var_p (as_a <gcall *> (stmt));
276 // Strict mode whining for VLAs is handled by the front-end,
277 // so we can safely ignore this case. Also, ignore VLAs if
278 // the user doesn't care about them.
279 if (is_vla)
281 if (warn_vla > 0 || warn_vla_limit < 0)
282 continue;
284 else if (warn_alloca)
286 warning_at (loc, OPT_Walloca, "use of %<alloca%>");
287 continue;
289 else if (warn_alloca_limit < 0)
290 continue;
292 class alloca_type_and_limit t
293 = alloca_call_type (stmt, is_vla);
295 unsigned HOST_WIDE_INT adjusted_alloca_limit
296 = adjusted_warn_limit (false);
297 // Even if we think the alloca call is OK, make sure it's not in a
298 // loop, except for a VLA, since VLAs are guaranteed to be cleaned
299 // up when they go out of scope, including in a loop.
300 if (t.type == ALLOCA_OK && !is_vla && in_loop_p (stmt))
302 /* As in other instances, only diagnose this when the limit
303 is less than the maximum valid object size. */
304 const offset_int maxobjsize
305 = wi::to_offset (max_object_size ());
306 if (adjusted_alloca_limit < maxobjsize.to_uhwi ())
307 t = alloca_type_and_limit (ALLOCA_IN_LOOP);
310 enum opt_code wcode
311 = is_vla ? OPT_Wvla_larger_than_ : OPT_Walloca_larger_than_;
312 char buff[WIDE_INT_MAX_PRECISION / 4 + 4];
313 switch (t.type)
315 case ALLOCA_OK:
316 break;
317 case ALLOCA_BOUND_MAYBE_LARGE:
319 if (xlimit_certain_p)
320 break;
322 auto_diagnostic_group d;
323 if (warning_at (loc, wcode,
324 (is_vla
325 ? G_("argument to variable-length "
326 "array may be too large")
327 : G_("argument to %<alloca%> may be too "
328 "large")))
329 && t.limit != 0)
331 print_decu (t.limit, buff);
332 inform (loc, "limit is %wu bytes, but argument "
333 "may be as large as %s",
334 is_vla ? warn_vla_limit : adjusted_alloca_limit,
335 buff);
338 break;
339 case ALLOCA_BOUND_DEFINITELY_LARGE:
341 auto_diagnostic_group d;
342 if (warning_at (loc, wcode,
343 (is_vla
344 ? G_("argument to variable-length"
345 " array is too large")
346 : G_("argument to %<alloca%> is too large")))
347 && t.limit != 0)
349 print_decu (t.limit, buff);
350 inform (loc, "limit is %wu bytes, but argument is %s",
351 is_vla ? warn_vla_limit : adjusted_alloca_limit,
352 buff);
355 break;
356 case ALLOCA_UNBOUNDED:
357 if (xlimit_certain_p)
358 break;
360 warning_at (loc, wcode,
361 (is_vla
362 ? G_("unbounded use of variable-length array")
363 : G_("unbounded use of %<alloca%>")));
364 break;
365 case ALLOCA_IN_LOOP:
366 gcc_assert (!is_vla);
367 warning_at (loc, wcode,
368 "use of %<alloca%> within a loop");
369 break;
370 case ALLOCA_ARG_IS_ZERO:
371 warning_at (loc, wcode,
372 (is_vla
373 ? G_("argument to variable-length array "
374 "is zero")
375 : G_("argument to %<alloca%> is zero")));
376 break;
377 default:
378 gcc_unreachable ();
382 ranger->export_global_ranges ();
383 disable_ranger (fun);
384 return 0;
387 gimple_opt_pass *
388 make_pass_walloca (gcc::context *ctxt)
390 return new pass_walloca (ctxt);