File: | build/gcc/gimple-ssa-warn-access.cc |
Warning: | line 1682, column 18 Null pointer passed to 1st parameter expecting 'nonnull' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* Pass to detect and issue warnings for invalid accesses, including | |||
2 | invalid or mismatched allocation/deallocation calls. | |||
3 | ||||
4 | Copyright (C) 2020-2023 Free Software Foundation, Inc. | |||
5 | Contributed by Martin Sebor <msebor@redhat.com>. | |||
6 | ||||
7 | This file is part of GCC. | |||
8 | ||||
9 | GCC is free software; you can redistribute it and/or modify it under | |||
10 | the terms of the GNU General Public License as published by the Free | |||
11 | Software Foundation; either version 3, or (at your option) any later | |||
12 | version. | |||
13 | ||||
14 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |||
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |||
16 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |||
17 | for more details. | |||
18 | ||||
19 | You should have received a copy of the GNU General Public License | |||
20 | along with GCC; see the file COPYING3. If not see | |||
21 | <http://www.gnu.org/licenses/>. */ | |||
22 | ||||
23 | #define INCLUDE_STRING | |||
24 | #include "config.h" | |||
25 | #include "system.h" | |||
26 | #include "coretypes.h" | |||
27 | #include "backend.h" | |||
28 | #include "tree.h" | |||
29 | #include "gimple.h" | |||
30 | #include "tree-pass.h" | |||
31 | #include "builtins.h" | |||
32 | #include "diagnostic.h" | |||
33 | #include "ssa.h" | |||
34 | #include "gimple-pretty-print.h" | |||
35 | #include "gimple-ssa-warn-access.h" | |||
36 | #include "gimple-ssa-warn-restrict.h" | |||
37 | #include "diagnostic-core.h" | |||
38 | #include "fold-const.h" | |||
39 | #include "gimple-iterator.h" | |||
40 | #include "gimple-fold.h" | |||
41 | #include "langhooks.h" | |||
42 | #include "memmodel.h" | |||
43 | #include "target.h" | |||
44 | #include "tree-dfa.h" | |||
45 | #include "tree-ssa.h" | |||
46 | #include "tree-cfg.h" | |||
47 | #include "tree-object-size.h" | |||
48 | #include "tree-ssa-strlen.h" | |||
49 | #include "calls.h" | |||
50 | #include "cfganal.h" | |||
51 | #include "intl.h" | |||
52 | #include "gimple-range.h" | |||
53 | #include "stringpool.h" | |||
54 | #include "attribs.h" | |||
55 | #include "demangle.h" | |||
56 | #include "attr-fnspec.h" | |||
57 | #include "pointer-query.h" | |||
58 | ||||
59 | /* Return true if tree node X has an associated location. */ | |||
60 | ||||
61 | static inline location_t | |||
62 | has_location (const_tree x) | |||
63 | { | |||
64 | if (DECL_P (x)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (x)->base.code))] == tcc_declaration)) | |||
65 | return DECL_SOURCE_LOCATION (x)((contains_struct_check ((x), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 65, __FUNCTION__))->decl_minimal.locus) != UNKNOWN_LOCATION((location_t) 0); | |||
66 | ||||
67 | if (EXPR_P (x)((tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) (x)->base.code))]) >= tcc_reference && (tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) (x)->base.code))]) <= tcc_expression)) | |||
68 | return EXPR_HAS_LOCATION (x)(((IS_ADHOC_LOC (((((x)) && ((tree_code_type_tmpl < 0>::tree_code_type[(int) (((enum tree_code) ((x))->base .code))]) >= tcc_reference && (tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) ((x))-> base.code))]) <= tcc_expression)) ? (x)->exp.locus : (( location_t) 0)))) ? get_location_from_adhoc_loc (line_table, ( (((x)) && ((tree_code_type_tmpl <0>::tree_code_type [(int) (((enum tree_code) ((x))->base.code))]) >= tcc_reference && (tree_code_type_tmpl <0>::tree_code_type[(int ) (((enum tree_code) ((x))->base.code))]) <= tcc_expression )) ? (x)->exp.locus : ((location_t) 0))) : (((((x)) && ((tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) ((x))->base.code))]) >= tcc_reference && (tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) ((x))->base.code))]) <= tcc_expression)) ? ( x)->exp.locus : ((location_t) 0)))) != ((location_t) 0)); | |||
69 | ||||
70 | return false; | |||
71 | } | |||
72 | ||||
73 | /* Return the associated location of STMT. */ | |||
74 | ||||
75 | static inline location_t | |||
76 | get_location (const gimple *stmt) | |||
77 | { | |||
78 | return gimple_location (stmt); | |||
79 | } | |||
80 | ||||
81 | /* Return the associated location of tree node X. */ | |||
82 | ||||
83 | static inline location_t | |||
84 | get_location (tree x) | |||
85 | { | |||
86 | if (DECL_P (x)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (x)->base.code))] == tcc_declaration)) | |||
87 | return DECL_SOURCE_LOCATION (x)((contains_struct_check ((x), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 87, __FUNCTION__))->decl_minimal.locus); | |||
88 | ||||
89 | if (EXPR_P (x)((tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) (x)->base.code))]) >= tcc_reference && (tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) (x)->base.code))]) <= tcc_expression)) | |||
90 | return EXPR_LOCATION (x)((((x)) && ((tree_code_type_tmpl <0>::tree_code_type [(int) (((enum tree_code) ((x))->base.code))]) >= tcc_reference && (tree_code_type_tmpl <0>::tree_code_type[(int ) (((enum tree_code) ((x))->base.code))]) <= tcc_expression )) ? (x)->exp.locus : ((location_t) 0)); | |||
91 | ||||
92 | return UNKNOWN_LOCATION((location_t) 0); | |||
93 | } | |||
94 | ||||
95 | /* Overload of the nascent tree function for GIMPLE STMT. */ | |||
96 | ||||
97 | static inline tree | |||
98 | get_callee_fndecl (const gimple *stmt) | |||
99 | { | |||
100 | return gimple_call_fndecl (stmt); | |||
101 | } | |||
102 | ||||
103 | static inline unsigned | |||
104 | call_nargs (const gimple *stmt) | |||
105 | { | |||
106 | return gimple_call_num_args (stmt); | |||
107 | } | |||
108 | ||||
109 | static inline unsigned | |||
110 | call_nargs (const_tree expr) | |||
111 | { | |||
112 | return call_expr_nargs (expr)(((int)((unsigned long) (*tree_int_cst_elt_check (((tree_class_check ((expr), (tcc_vl_exp), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 112, __FUNCTION__))->exp.operands[0]), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 112, __FUNCTION__)))) - 3); | |||
113 | } | |||
114 | ||||
115 | ||||
116 | static inline tree | |||
117 | call_arg (const gimple *stmt, unsigned argno) | |||
118 | { | |||
119 | return gimple_call_arg (stmt, argno); | |||
120 | } | |||
121 | ||||
122 | static inline tree | |||
123 | call_arg (tree expr, unsigned argno) | |||
124 | { | |||
125 | return CALL_EXPR_ARG (expr, argno)(*((const_cast<tree*> (tree_operand_check (((tree_check ((expr), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 125, __FUNCTION__, (CALL_EXPR)))), ((argno) + 3), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 125, __FUNCTION__))))); | |||
126 | } | |||
127 | ||||
128 | /* For a call EXPR at LOC to a function FNAME that expects a string | |||
129 | in the argument ARG, issue a diagnostic due to it being a called | |||
130 | with an argument that is a character array with no terminating | |||
131 | NUL. SIZE is the EXACT size of the array, and BNDRNG the number | |||
132 | of characters in which the NUL is expected. Either EXPR or FNAME | |||
133 | may be null but noth both. SIZE may be null when BNDRNG is null. */ | |||
134 | ||||
135 | template <class GimpleOrTree> | |||
136 | static void | |||
137 | warn_string_no_nul (location_t loc, GimpleOrTree expr, const char *fname, | |||
138 | tree arg, tree decl, tree size, bool exact, | |||
139 | const wide_int bndrng[2] /* = NULL */) | |||
140 | { | |||
141 | const opt_code opt = OPT_Wstringop_overread; | |||
142 | if ((expr && warning_suppressed_p (expr, opt)) | |||
143 | || warning_suppressed_p (arg, opt)) | |||
144 | return; | |||
145 | ||||
146 | loc = expansion_point_location_if_in_system_header (loc); | |||
147 | bool warned; | |||
148 | ||||
149 | /* Format the bound range as a string to keep the number of messages | |||
150 | from exploding. */ | |||
151 | char bndstr[80]; | |||
152 | *bndstr = 0; | |||
153 | if (bndrng) | |||
154 | { | |||
155 | if (bndrng[0] == bndrng[1]) | |||
156 | sprintf (bndstr, "%llu", (unsigned long long) bndrng[0].to_uhwi ()); | |||
157 | else | |||
158 | sprintf (bndstr, "[%llu, %llu]", | |||
159 | (unsigned long long) bndrng[0].to_uhwi (), | |||
160 | (unsigned long long) bndrng[1].to_uhwi ()); | |||
161 | } | |||
162 | ||||
163 | auto_diagnostic_group d; | |||
164 | ||||
165 | const tree maxobjsize = max_object_size (); | |||
166 | const wide_int maxsiz = wi::to_wide (maxobjsize); | |||
167 | if (expr) | |||
168 | { | |||
169 | tree func = get_callee_fndecl (expr); | |||
170 | if (bndrng) | |||
171 | { | |||
172 | if (wi::ltu_p (maxsiz, bndrng[0])) | |||
173 | warned = warning_at (loc, opt, | |||
174 | "%qD specified bound %s exceeds " | |||
175 | "maximum object size %E", | |||
176 | func, bndstr, maxobjsize); | |||
177 | else | |||
178 | { | |||
179 | bool maybe = wi::to_wide (size) == bndrng[0]; | |||
180 | warned = warning_at (loc, opt, | |||
181 | exact | |||
182 | ? G_("%qD specified bound %s exceeds ""%qD specified bound %s exceeds " "the size %E of unterminated array" | |||
183 | "the size %E of unterminated array")"%qD specified bound %s exceeds " "the size %E of unterminated array" | |||
184 | : (maybe | |||
185 | ? G_("%qD specified bound %s may ""%qD specified bound %s may " "exceed the size of at most %E " "of unterminated array" | |||
186 | "exceed the size of at most %E ""%qD specified bound %s may " "exceed the size of at most %E " "of unterminated array" | |||
187 | "of unterminated array")"%qD specified bound %s may " "exceed the size of at most %E " "of unterminated array" | |||
188 | : G_("%qD specified bound %s exceeds ""%qD specified bound %s exceeds " "the size of at most %E " "of unterminated array" | |||
189 | "the size of at most %E ""%qD specified bound %s exceeds " "the size of at most %E " "of unterminated array" | |||
190 | "of unterminated array")"%qD specified bound %s exceeds " "the size of at most %E " "of unterminated array"), | |||
191 | func, bndstr, size); | |||
192 | } | |||
193 | } | |||
194 | else | |||
195 | warned = warning_at (loc, opt, | |||
196 | "%qD argument missing terminating nul", | |||
197 | func); | |||
198 | } | |||
199 | else | |||
200 | { | |||
201 | if (bndrng) | |||
202 | { | |||
203 | if (wi::ltu_p (maxsiz, bndrng[0])) | |||
204 | warned = warning_at (loc, opt, | |||
205 | "%qs specified bound %s exceeds " | |||
206 | "maximum object size %E", | |||
207 | fname, bndstr, maxobjsize); | |||
208 | else | |||
209 | { | |||
210 | bool maybe = wi::to_wide (size) == bndrng[0]; | |||
211 | warned = warning_at (loc, opt, | |||
212 | exact | |||
213 | ? G_("%qs specified bound %s exceeds ""%qs specified bound %s exceeds " "the size %E of unterminated array" | |||
214 | "the size %E of unterminated array")"%qs specified bound %s exceeds " "the size %E of unterminated array" | |||
215 | : (maybe | |||
216 | ? G_("%qs specified bound %s may ""%qs specified bound %s may " "exceed the size of at most %E " "of unterminated array" | |||
217 | "exceed the size of at most %E ""%qs specified bound %s may " "exceed the size of at most %E " "of unterminated array" | |||
218 | "of unterminated array")"%qs specified bound %s may " "exceed the size of at most %E " "of unterminated array" | |||
219 | : G_("%qs specified bound %s exceeds ""%qs specified bound %s exceeds " "the size of at most %E " "of unterminated array" | |||
220 | "the size of at most %E ""%qs specified bound %s exceeds " "the size of at most %E " "of unterminated array" | |||
221 | "of unterminated array")"%qs specified bound %s exceeds " "the size of at most %E " "of unterminated array"), | |||
222 | fname, bndstr, size); | |||
223 | } | |||
224 | } | |||
225 | else | |||
226 | warned = warning_at (loc, opt, | |||
227 | "%qs argument missing terminating nul", | |||
228 | fname); | |||
229 | } | |||
230 | ||||
231 | if (warned) | |||
232 | { | |||
233 | inform (get_location (decl), | |||
234 | "referenced argument declared here"); | |||
235 | suppress_warning (arg, opt); | |||
236 | if (expr) | |||
237 | suppress_warning (expr, opt); | |||
238 | } | |||
239 | } | |||
240 | ||||
241 | void | |||
242 | warn_string_no_nul (location_t loc, gimple *stmt, const char *fname, | |||
243 | tree arg, tree decl, tree size /* = NULL_TREE */, | |||
244 | bool exact /* = false */, | |||
245 | const wide_int bndrng[2] /* = NULL */) | |||
246 | { | |||
247 | return warn_string_no_nul<gimple *> (loc, stmt, fname, | |||
248 | arg, decl, size, exact, bndrng); | |||
249 | } | |||
250 | ||||
251 | void | |||
252 | warn_string_no_nul (location_t loc, tree expr, const char *fname, | |||
253 | tree arg, tree decl, tree size /* = NULL_TREE */, | |||
254 | bool exact /* = false */, | |||
255 | const wide_int bndrng[2] /* = NULL */) | |||
256 | { | |||
257 | return warn_string_no_nul<tree> (loc, expr, fname, | |||
258 | arg, decl, size, exact, bndrng); | |||
259 | } | |||
260 | ||||
261 | /* If EXP refers to an unterminated constant character array return | |||
262 | the declaration of the object of which the array is a member or | |||
263 | element and if SIZE is not null, set *SIZE to the size of | |||
264 | the unterminated array and set *EXACT if the size is exact or | |||
265 | clear it otherwise. Otherwise return null. */ | |||
266 | ||||
267 | tree | |||
268 | unterminated_array (tree exp, tree *size /* = NULL */, bool *exact /* = NULL */) | |||
269 | { | |||
270 | /* C_STRLEN will return NULL and set DECL in the info | |||
271 | structure if EXP references a unterminated array. */ | |||
272 | c_strlen_data lendata = { }; | |||
273 | tree len = c_strlen (exp, 1, &lendata); | |||
274 | if (len || !lendata.minlen || !lendata.decl) | |||
275 | return NULL_TREE(tree) nullptr; | |||
276 | ||||
277 | if (!size) | |||
278 | return lendata.decl; | |||
279 | ||||
280 | len = lendata.minlen; | |||
281 | if (lendata.off) | |||
282 | { | |||
283 | /* Constant offsets are already accounted for in LENDATA.MINLEN, | |||
284 | but not in a SSA_NAME + CST expression. */ | |||
285 | if (TREE_CODE (lendata.off)((enum tree_code) (lendata.off)->base.code) == INTEGER_CST) | |||
286 | *exact = true; | |||
287 | else if (TREE_CODE (lendata.off)((enum tree_code) (lendata.off)->base.code) == PLUS_EXPR | |||
288 | && TREE_CODE (TREE_OPERAND (lendata.off, 1))((enum tree_code) ((*((const_cast<tree*> (tree_operand_check ((lendata.off), (1), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 288, __FUNCTION__))))))->base.code) == INTEGER_CST) | |||
289 | { | |||
290 | /* Subtract the offset from the size of the array. */ | |||
291 | *exact = false; | |||
292 | tree temp = TREE_OPERAND (lendata.off, 1)(*((const_cast<tree*> (tree_operand_check ((lendata.off ), (1), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 292, __FUNCTION__))))); | |||
293 | temp = fold_convert (ssizetype, temp)fold_convert_loc (((location_t) 0), sizetype_tab[(int) stk_ssizetype ], temp); | |||
294 | len = fold_build2 (MINUS_EXPR, ssizetype, len, temp)fold_build2_loc (((location_t) 0), MINUS_EXPR, sizetype_tab[( int) stk_ssizetype], len, temp ); | |||
295 | } | |||
296 | else | |||
297 | *exact = false; | |||
298 | } | |||
299 | else | |||
300 | *exact = true; | |||
301 | ||||
302 | *size = len; | |||
303 | return lendata.decl; | |||
304 | } | |||
305 | ||||
306 | /* For a call EXPR (which may be null) that expects a string argument | |||
307 | SRC as an argument, returns false if SRC is a character array with | |||
308 | no terminating NUL. When nonnull, BOUND is the number of characters | |||
309 | in which to expect the terminating NUL. When EXPR is nonnull also | |||
310 | issues a warning. */ | |||
311 | ||||
312 | template <class GimpleOrTree> | |||
313 | static bool | |||
314 | check_nul_terminated_array (GimpleOrTree expr, tree src, tree bound) | |||
315 | { | |||
316 | /* The constant size of the array SRC points to. The actual size | |||
317 | may be less of EXACT is true, but not more. */ | |||
318 | tree size; | |||
319 | /* True if SRC involves a non-constant offset into the array. */ | |||
320 | bool exact; | |||
321 | /* The unterminated constant array SRC points to. */ | |||
322 | tree nonstr = unterminated_array (src, &size, &exact); | |||
323 | if (!nonstr) | |||
324 | return true; | |||
325 | ||||
326 | /* NONSTR refers to the non-nul terminated constant array and SIZE | |||
327 | is the constant size of the array in bytes. EXACT is true when | |||
328 | SIZE is exact. */ | |||
329 | ||||
330 | wide_int bndrng[2]; | |||
331 | if (bound) | |||
332 | { | |||
333 | Value_Range r (TREE_TYPE (bound)((contains_struct_check ((bound), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 333, __FUNCTION__))->typed.type)); | |||
334 | ||||
335 | get_global_range_query ()->range_of_expr (r, bound); | |||
336 | ||||
337 | if (r.undefined_p () || r.varying_p ()) | |||
338 | return true; | |||
339 | ||||
340 | bndrng[0] = r.lower_bound (); | |||
341 | bndrng[1] = r.upper_bound (); | |||
342 | ||||
343 | if (exact) | |||
344 | { | |||
345 | if (wi::leu_p (bndrng[0], wi::to_wide (size))) | |||
346 | return true; | |||
347 | } | |||
348 | else if (wi::lt_p (bndrng[0], wi::to_wide (size), UNSIGNED)) | |||
349 | return true; | |||
350 | } | |||
351 | ||||
352 | if (expr) | |||
353 | warn_string_no_nul (get_location (expr), expr, NULLnullptr, src, nonstr, | |||
354 | size, exact, bound ? bndrng : NULLnullptr); | |||
355 | ||||
356 | return false; | |||
357 | } | |||
358 | ||||
359 | bool | |||
360 | check_nul_terminated_array (gimple *stmt, tree src, tree bound /* = NULL_TREE */) | |||
361 | { | |||
362 | return check_nul_terminated_array<gimple *>(stmt, src, bound); | |||
363 | } | |||
364 | ||||
365 | bool | |||
366 | check_nul_terminated_array (tree expr, tree src, tree bound /* = NULL_TREE */) | |||
367 | { | |||
368 | return check_nul_terminated_array<tree>(expr, src, bound); | |||
369 | } | |||
370 | ||||
371 | /* Warn about passing a non-string array/pointer to a built-in function | |||
372 | that expects a nul-terminated string argument. Returns true if | |||
373 | a warning has been issued.*/ | |||
374 | ||||
375 | template <class GimpleOrTree> | |||
376 | static bool | |||
377 | maybe_warn_nonstring_arg (tree fndecl, GimpleOrTree exp) | |||
378 | { | |||
379 | if (!fndecl || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) | |||
380 | return false; | |||
381 | ||||
382 | if (!warn_stringop_overreadglobal_options.x_warn_stringop_overread | |||
383 | || warning_suppressed_p (exp, OPT_Wstringop_overread)) | |||
384 | return false; | |||
385 | ||||
386 | /* Avoid clearly invalid calls (more checking done below). */ | |||
387 | unsigned nargs = call_nargs (exp); | |||
388 | if (!nargs) | |||
389 | return false; | |||
390 | ||||
391 | /* The bound argument to a bounded string function like strncpy. */ | |||
392 | tree bound = NULL_TREE(tree) nullptr; | |||
393 | ||||
394 | /* The longest known or possible string argument to one of the comparison | |||
395 | functions. If the length is less than the bound it is used instead. | |||
396 | Since the length is only used for warning and not for code generation | |||
397 | disable strict mode in the calls to get_range_strlen below. */ | |||
398 | tree maxlen = NULL_TREE(tree) nullptr; | |||
399 | ||||
400 | /* It's safe to call "bounded" string functions with a non-string | |||
401 | argument since the functions provide an explicit bound for this | |||
402 | purpose. The exception is strncat where the bound may refer to | |||
403 | either the destination or the source. */ | |||
404 | int fncode = DECL_FUNCTION_CODE (fndecl); | |||
405 | switch (fncode) | |||
406 | { | |||
407 | case BUILT_IN_STRCMP: | |||
408 | case BUILT_IN_STRNCMP: | |||
409 | case BUILT_IN_STRNCASECMP: | |||
410 | { | |||
411 | /* For these, if one argument refers to one or more of a set | |||
412 | of string constants or arrays of known size, determine | |||
413 | the range of their known or possible lengths and use it | |||
414 | conservatively as the bound for the unbounded function, | |||
415 | and to adjust the range of the bound of the bounded ones. */ | |||
416 | for (unsigned argno = 0; | |||
417 | argno < MIN (nargs, 2)((nargs) < (2) ? (nargs) : (2)) | |||
418 | && !(maxlen && TREE_CODE (maxlen)((enum tree_code) (maxlen)->base.code) == INTEGER_CST); argno++) | |||
419 | { | |||
420 | tree arg = call_arg (exp, argno); | |||
421 | if (!get_attr_nonstring_decl (arg)) | |||
422 | { | |||
423 | c_strlen_data lendata = { }; | |||
424 | /* Set MAXBOUND to an arbitrary non-null non-integer | |||
425 | node as a request to have it set to the length of | |||
426 | the longest string in a PHI. */ | |||
427 | lendata.maxbound = arg; | |||
428 | get_range_strlen (arg, &lendata, /* eltsize = */ 1); | |||
429 | maxlen = lendata.maxbound; | |||
430 | } | |||
431 | } | |||
432 | } | |||
433 | /* Fall through. */ | |||
434 | ||||
435 | case BUILT_IN_STRNCAT: | |||
436 | case BUILT_IN_STPNCPY: | |||
437 | case BUILT_IN_STRNCPY: | |||
438 | if (nargs > 2) | |||
439 | bound = call_arg (exp, 2); | |||
440 | break; | |||
441 | ||||
442 | case BUILT_IN_STRNDUP: | |||
443 | if (nargs < 2) | |||
444 | return false; | |||
445 | bound = call_arg (exp, 1); | |||
446 | break; | |||
447 | ||||
448 | case BUILT_IN_STRNLEN: | |||
449 | { | |||
450 | tree arg = call_arg (exp, 0); | |||
451 | if (!get_attr_nonstring_decl (arg)) | |||
452 | { | |||
453 | c_strlen_data lendata = { }; | |||
454 | /* Set MAXBOUND to an arbitrary non-null non-integer | |||
455 | node as a request to have it set to the length of | |||
456 | the longest string in a PHI. */ | |||
457 | lendata.maxbound = arg; | |||
458 | get_range_strlen (arg, &lendata, /* eltsize = */ 1); | |||
459 | maxlen = lendata.maxbound; | |||
460 | } | |||
461 | if (nargs > 1) | |||
462 | bound = call_arg (exp, 1); | |||
463 | break; | |||
464 | } | |||
465 | ||||
466 | default: | |||
467 | break; | |||
468 | } | |||
469 | ||||
470 | /* Determine the range of the bound argument (if specified). */ | |||
471 | tree bndrng[2] = { NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr }; | |||
472 | if (bound) | |||
473 | { | |||
474 | STRIP_NOPS (bound)(bound) = tree_strip_nop_conversions ((const_cast<union tree_node *> (((bound))))); | |||
475 | get_size_range (bound, bndrng); | |||
476 | } | |||
477 | ||||
478 | location_t loc = get_location (exp); | |||
479 | ||||
480 | if (bndrng[0]) | |||
481 | { | |||
482 | /* Diagnose excessive bound prior to the adjustment below and | |||
483 | regardless of attribute nonstring. */ | |||
484 | tree maxobjsize = max_object_size (); | |||
485 | if (tree_int_cst_lt (maxobjsize, bndrng[0])) | |||
486 | { | |||
487 | bool warned = false; | |||
488 | if (tree_int_cst_equal (bndrng[0], bndrng[1])) | |||
489 | warned = warning_at (loc, OPT_Wstringop_overread, | |||
490 | "%qD specified bound %E " | |||
491 | "exceeds maximum object size %E", | |||
492 | fndecl, bndrng[0], maxobjsize); | |||
493 | else | |||
494 | warned = warning_at (loc, OPT_Wstringop_overread, | |||
495 | "%qD specified bound [%E, %E] " | |||
496 | "exceeds maximum object size %E", | |||
497 | fndecl, bndrng[0], bndrng[1], | |||
498 | maxobjsize); | |||
499 | if (warned) | |||
500 | suppress_warning (exp, OPT_Wstringop_overread); | |||
501 | ||||
502 | return warned; | |||
503 | } | |||
504 | } | |||
505 | ||||
506 | if (maxlen && !integer_all_onesp (maxlen)) | |||
507 | { | |||
508 | /* Add one for the nul. */ | |||
509 | maxlen = const_binop (PLUS_EXPR, TREE_TYPE (maxlen)((contains_struct_check ((maxlen), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 509, __FUNCTION__))->typed.type), maxlen, | |||
510 | size_one_nodeglobal_trees[TI_SIZE_ONE]); | |||
511 | ||||
512 | if (!bndrng[0]) | |||
513 | { | |||
514 | /* Conservatively use the upper bound of the lengths for | |||
515 | both the lower and the upper bound of the operation. */ | |||
516 | bndrng[0] = maxlen; | |||
517 | bndrng[1] = maxlen; | |||
518 | bound = void_type_nodeglobal_trees[TI_VOID_TYPE]; | |||
519 | } | |||
520 | else if (maxlen) | |||
521 | { | |||
522 | /* Replace the bound on the operation with the upper bound | |||
523 | of the length of the string if the latter is smaller. */ | |||
524 | if (tree_int_cst_lt (maxlen, bndrng[0])) | |||
525 | bndrng[0] = maxlen; | |||
526 | else if (tree_int_cst_lt (maxlen, bndrng[1])) | |||
527 | bndrng[1] = maxlen; | |||
528 | } | |||
529 | } | |||
530 | ||||
531 | bool any_arg_warned = false; | |||
532 | /* Iterate over the built-in function's formal arguments and check | |||
533 | each const char* against the actual argument. If the actual | |||
534 | argument is declared attribute non-string issue a warning unless | |||
535 | the argument's maximum length is bounded. */ | |||
536 | function_args_iterator it; | |||
537 | function_args_iter_init (&it, TREE_TYPE (fndecl)((contains_struct_check ((fndecl), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 537, __FUNCTION__))->typed.type)); | |||
538 | ||||
539 | for (unsigned argno = 0; ; ++argno, function_args_iter_next (&it)) | |||
540 | { | |||
541 | /* Avoid iterating past the declared argument in a call | |||
542 | to function declared without a prototype. */ | |||
543 | if (argno >= nargs) | |||
544 | break; | |||
545 | ||||
546 | tree argtype = function_args_iter_cond (&it); | |||
547 | if (!argtype) | |||
548 | break; | |||
549 | ||||
550 | if (TREE_CODE (argtype)((enum tree_code) (argtype)->base.code) != POINTER_TYPE) | |||
551 | continue; | |||
552 | ||||
553 | argtype = TREE_TYPE (argtype)((contains_struct_check ((argtype), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 553, __FUNCTION__))->typed.type); | |||
554 | ||||
555 | if (TREE_CODE (argtype)((enum tree_code) (argtype)->base.code) != INTEGER_TYPE | |||
556 | || !TYPE_READONLY (argtype)((tree_class_check ((argtype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 556, __FUNCTION__))->base.readonly_flag)) | |||
557 | continue; | |||
558 | ||||
559 | argtype = TYPE_MAIN_VARIANT (argtype)((tree_class_check ((argtype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 559, __FUNCTION__))->type_common.main_variant); | |||
560 | if (argtype != char_type_nodeinteger_types[itk_char]) | |||
561 | continue; | |||
562 | ||||
563 | tree callarg = call_arg (exp, argno); | |||
564 | if (TREE_CODE (callarg)((enum tree_code) (callarg)->base.code) == ADDR_EXPR) | |||
565 | callarg = TREE_OPERAND (callarg, 0)(*((const_cast<tree*> (tree_operand_check ((callarg), ( 0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 565, __FUNCTION__))))); | |||
566 | ||||
567 | /* See if the destination is declared with attribute "nonstring". */ | |||
568 | tree decl = get_attr_nonstring_decl (callarg); | |||
569 | if (!decl) | |||
570 | continue; | |||
571 | ||||
572 | /* The maximum number of array elements accessed. */ | |||
573 | offset_int wibnd = 0; | |||
574 | ||||
575 | if (argno && fncode == BUILT_IN_STRNCAT) | |||
576 | { | |||
577 | /* See if the bound in strncat is derived from the length | |||
578 | of the strlen of the destination (as it's expected to be). | |||
579 | If so, reset BOUND and FNCODE to trigger a warning. */ | |||
580 | tree dstarg = call_arg (exp, 0); | |||
581 | if (is_strlen_related_p (dstarg, bound)) | |||
582 | { | |||
583 | /* The bound applies to the destination, not to the source, | |||
584 | so reset these to trigger a warning without mentioning | |||
585 | the bound. */ | |||
586 | bound = NULLnullptr; | |||
587 | fncode = 0; | |||
588 | } | |||
589 | else if (bndrng[1]) | |||
590 | /* Use the upper bound of the range for strncat. */ | |||
591 | wibnd = wi::to_offset (bndrng[1]); | |||
592 | } | |||
593 | else if (bndrng[0]) | |||
594 | /* Use the lower bound of the range for functions other than | |||
595 | strncat. */ | |||
596 | wibnd = wi::to_offset (bndrng[0]); | |||
597 | ||||
598 | /* Determine the size of the argument array if it is one. */ | |||
599 | offset_int asize = wibnd; | |||
600 | bool known_size = false; | |||
601 | tree type = TREE_TYPE (decl)((contains_struct_check ((decl), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 601, __FUNCTION__))->typed.type); | |||
602 | ||||
603 | /* Determine the array size. For arrays of unknown bound and | |||
604 | pointers reset BOUND to trigger the appropriate warning. */ | |||
605 | if (TREE_CODE (type)((enum tree_code) (type)->base.code) == ARRAY_TYPE) | |||
606 | { | |||
607 | if (tree arrbnd = TYPE_DOMAIN (type)((tree_check ((type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 607, __FUNCTION__, (ARRAY_TYPE)))->type_non_common.values )) | |||
608 | { | |||
609 | if ((arrbnd = TYPE_MAX_VALUE (arrbnd)((tree_check5 ((arrbnd), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 609, __FUNCTION__, (INTEGER_TYPE), (ENUMERAL_TYPE), (BOOLEAN_TYPE ), (REAL_TYPE), (FIXED_POINT_TYPE)))->type_non_common.maxval ))) | |||
610 | { | |||
611 | asize = wi::to_offset (arrbnd) + 1; | |||
612 | known_size = true; | |||
613 | } | |||
614 | } | |||
615 | else if (bound == void_type_nodeglobal_trees[TI_VOID_TYPE]) | |||
616 | bound = NULL_TREE(tree) nullptr; | |||
617 | } | |||
618 | else if (bound == void_type_nodeglobal_trees[TI_VOID_TYPE]) | |||
619 | bound = NULL_TREE(tree) nullptr; | |||
620 | ||||
621 | /* In a call to strncat with a bound in a range whose lower but | |||
622 | not upper bound is less than the array size, reset ASIZE to | |||
623 | be the same as the bound and the other variable to trigger | |||
624 | the appropriate warning below. */ | |||
625 | if (fncode == BUILT_IN_STRNCAT | |||
626 | && bndrng[0] != bndrng[1] | |||
627 | && wi::ltu_p (wi::to_offset (bndrng[0]), asize) | |||
628 | && (!known_size | |||
629 | || wi::ltu_p (asize, wibnd))) | |||
630 | { | |||
631 | asize = wibnd; | |||
632 | bound = NULL_TREE(tree) nullptr; | |||
633 | fncode = 0; | |||
634 | } | |||
635 | ||||
636 | bool warned = false; | |||
637 | ||||
638 | auto_diagnostic_group d; | |||
639 | if (wi::ltu_p (asize, wibnd)) | |||
640 | { | |||
641 | if (bndrng[0] == bndrng[1]) | |||
642 | warned = warning_at (loc, OPT_Wstringop_overread, | |||
643 | "%qD argument %i declared attribute " | |||
644 | "%<nonstring%> is smaller than the specified " | |||
645 | "bound %wu", | |||
646 | fndecl, argno + 1, wibnd.to_uhwi ()); | |||
647 | else if (wi::ltu_p (asize, wi::to_offset (bndrng[0]))) | |||
648 | warned = warning_at (loc, OPT_Wstringop_overread, | |||
649 | "%qD argument %i declared attribute " | |||
650 | "%<nonstring%> is smaller than " | |||
651 | "the specified bound [%E, %E]", | |||
652 | fndecl, argno + 1, bndrng[0], bndrng[1]); | |||
653 | else | |||
654 | warned = warning_at (loc, OPT_Wstringop_overread, | |||
655 | "%qD argument %i declared attribute " | |||
656 | "%<nonstring%> may be smaller than " | |||
657 | "the specified bound [%E, %E]", | |||
658 | fndecl, argno + 1, bndrng[0], bndrng[1]); | |||
659 | } | |||
660 | else if (fncode == BUILT_IN_STRNCAT) | |||
661 | ; /* Avoid warning for calls to strncat() when the bound | |||
662 | is equal to the size of the non-string argument. */ | |||
663 | else if (!bound) | |||
664 | warned = warning_at (loc, OPT_Wstringop_overread, | |||
665 | "%qD argument %i declared attribute %<nonstring%>", | |||
666 | fndecl, argno + 1); | |||
667 | ||||
668 | if (warned) | |||
669 | { | |||
670 | inform (DECL_SOURCE_LOCATION (decl)((contains_struct_check ((decl), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 670, __FUNCTION__))->decl_minimal.locus), | |||
671 | "argument %qD declared here", decl); | |||
672 | any_arg_warned = true; | |||
673 | } | |||
674 | } | |||
675 | ||||
676 | if (any_arg_warned) | |||
677 | suppress_warning (exp, OPT_Wstringop_overread); | |||
678 | ||||
679 | return any_arg_warned; | |||
680 | } | |||
681 | ||||
682 | bool | |||
683 | maybe_warn_nonstring_arg (tree fndecl, gimple *stmt) | |||
684 | { | |||
685 | return maybe_warn_nonstring_arg<gimple *>(fndecl, stmt); | |||
686 | } | |||
687 | ||||
688 | ||||
689 | bool | |||
690 | maybe_warn_nonstring_arg (tree fndecl, tree expr) | |||
691 | { | |||
692 | return maybe_warn_nonstring_arg<tree>(fndecl, expr); | |||
693 | } | |||
694 | ||||
695 | /* Issue a warning OPT for a bounded call EXP with a bound in RANGE | |||
696 | accessing an object with SIZE. */ | |||
697 | ||||
698 | template <class GimpleOrTree> | |||
699 | static bool | |||
700 | maybe_warn_for_bound (opt_code opt, location_t loc, GimpleOrTree exp, tree func, | |||
701 | tree bndrng[2], tree size, const access_data *pad) | |||
702 | { | |||
703 | if (!bndrng[0] || warning_suppressed_p (exp, opt)) | |||
704 | return false; | |||
705 | ||||
706 | tree maxobjsize = max_object_size (); | |||
707 | ||||
708 | bool warned = false; | |||
709 | ||||
710 | if (opt == OPT_Wstringop_overread) | |||
711 | { | |||
712 | bool maybe = pad && pad->src.phi (); | |||
713 | if (maybe) | |||
714 | { | |||
715 | /* Issue a "maybe" warning only if the PHI refers to objects | |||
716 | at least one of which has more space remaining than the bound. | |||
717 | Otherwise, if the bound is greater, use the definitive form. */ | |||
718 | offset_int remmax = pad->src.size_remaining (); | |||
719 | if (remmax < wi::to_offset (bndrng[0])) | |||
720 | maybe = false; | |||
721 | } | |||
722 | ||||
723 | auto_diagnostic_group d; | |||
724 | if (tree_int_cst_lt (maxobjsize, bndrng[0])) | |||
725 | { | |||
726 | if (bndrng[0] == bndrng[1]) | |||
727 | warned = (func | |||
728 | ? warning_at (loc, opt, | |||
729 | (maybe | |||
730 | ? G_("%qD specified bound %E may ""%qD specified bound %E may " "exceed maximum object size %E" | |||
731 | "exceed maximum object size %E")"%qD specified bound %E may " "exceed maximum object size %E" | |||
732 | : G_("%qD specified bound %E ""%qD specified bound %E " "exceeds maximum object size %E" | |||
733 | "exceeds maximum object size %E")"%qD specified bound %E " "exceeds maximum object size %E"), | |||
734 | func, bndrng[0], maxobjsize) | |||
735 | : warning_at (loc, opt, | |||
736 | (maybe | |||
737 | ? G_("specified bound %E may ""specified bound %E may " "exceed maximum object size %E" | |||
738 | "exceed maximum object size %E")"specified bound %E may " "exceed maximum object size %E" | |||
739 | : G_("specified bound %E ""specified bound %E " "exceeds maximum object size %E" | |||
740 | "exceeds maximum object size %E")"specified bound %E " "exceeds maximum object size %E"), | |||
741 | bndrng[0], maxobjsize)); | |||
742 | else | |||
743 | warned = (func | |||
744 | ? warning_at (loc, opt, | |||
745 | (maybe | |||
746 | ? G_("%qD specified bound [%E, %E] may ""%qD specified bound [%E, %E] may " "exceed maximum object size %E" | |||
747 | "exceed maximum object size %E")"%qD specified bound [%E, %E] may " "exceed maximum object size %E" | |||
748 | : G_("%qD specified bound [%E, %E] ""%qD specified bound [%E, %E] " "exceeds maximum object size %E" | |||
749 | "exceeds maximum object size %E")"%qD specified bound [%E, %E] " "exceeds maximum object size %E"), | |||
750 | func, | |||
751 | bndrng[0], bndrng[1], maxobjsize) | |||
752 | : warning_at (loc, opt, | |||
753 | (maybe | |||
754 | ? G_("specified bound [%E, %E] may ""specified bound [%E, %E] may " "exceed maximum object size %E" | |||
755 | "exceed maximum object size %E")"specified bound [%E, %E] may " "exceed maximum object size %E" | |||
756 | : G_("specified bound [%E, %E] ""specified bound [%E, %E] " "exceeds maximum object size %E" | |||
757 | "exceeds maximum object size %E")"specified bound [%E, %E] " "exceeds maximum object size %E"), | |||
758 | bndrng[0], bndrng[1], maxobjsize)); | |||
759 | } | |||
760 | else if (!size || tree_int_cst_le (bndrng[0], size)) | |||
761 | return false; | |||
762 | else if (tree_int_cst_equal (bndrng[0], bndrng[1])) | |||
763 | warned = (func | |||
764 | ? warning_at (loc, opt, | |||
765 | (maybe | |||
766 | ? G_("%qD specified bound %E may exceed ""%qD specified bound %E may exceed " "source size %E" | |||
767 | "source size %E")"%qD specified bound %E may exceed " "source size %E" | |||
768 | : G_("%qD specified bound %E exceeds ""%qD specified bound %E exceeds " "source size %E" | |||
769 | "source size %E")"%qD specified bound %E exceeds " "source size %E"), | |||
770 | func, bndrng[0], size) | |||
771 | : warning_at (loc, opt, | |||
772 | (maybe | |||
773 | ? G_("specified bound %E may exceed ""specified bound %E may exceed " "source size %E" | |||
774 | "source size %E")"specified bound %E may exceed " "source size %E" | |||
775 | : G_("specified bound %E exceeds ""specified bound %E exceeds " "source size %E" | |||
776 | "source size %E")"specified bound %E exceeds " "source size %E"), | |||
777 | bndrng[0], size)); | |||
778 | else | |||
779 | warned = (func | |||
780 | ? warning_at (loc, opt, | |||
781 | (maybe | |||
782 | ? G_("%qD specified bound [%E, %E] may ""%qD specified bound [%E, %E] may " "exceed source size %E" | |||
783 | "exceed source size %E")"%qD specified bound [%E, %E] may " "exceed source size %E" | |||
784 | : G_("%qD specified bound [%E, %E] exceeds ""%qD specified bound [%E, %E] exceeds " "source size %E" | |||
785 | "source size %E")"%qD specified bound [%E, %E] exceeds " "source size %E"), | |||
786 | func, bndrng[0], bndrng[1], size) | |||
787 | : warning_at (loc, opt, | |||
788 | (maybe | |||
789 | ? G_("specified bound [%E, %E] may exceed ""specified bound [%E, %E] may exceed " "source size %E" | |||
790 | "source size %E")"specified bound [%E, %E] may exceed " "source size %E" | |||
791 | : G_("specified bound [%E, %E] exceeds ""specified bound [%E, %E] exceeds " "source size %E" | |||
792 | "source size %E")"specified bound [%E, %E] exceeds " "source size %E"), | |||
793 | bndrng[0], bndrng[1], size)); | |||
794 | if (warned) | |||
795 | { | |||
796 | if (pad && pad->src.ref | |||
797 | && has_location (pad->src.ref)) | |||
798 | inform (get_location (pad->src.ref), | |||
799 | "source object allocated here"); | |||
800 | suppress_warning (exp, opt); | |||
801 | } | |||
802 | ||||
803 | return warned; | |||
804 | } | |||
805 | ||||
806 | bool maybe = pad && pad->dst.phi (); | |||
807 | if (maybe) | |||
808 | { | |||
809 | /* Issue a "maybe" warning only if the PHI refers to objects | |||
810 | at least one of which has more space remaining than the bound. | |||
811 | Otherwise, if the bound is greater, use the definitive form. */ | |||
812 | offset_int remmax = pad->dst.size_remaining (); | |||
813 | if (remmax < wi::to_offset (bndrng[0])) | |||
814 | maybe = false; | |||
815 | } | |||
816 | if (tree_int_cst_lt (maxobjsize, bndrng[0])) | |||
817 | { | |||
818 | if (bndrng[0] == bndrng[1]) | |||
819 | warned = (func | |||
820 | ? warning_at (loc, opt, | |||
821 | (maybe | |||
822 | ? G_("%qD specified size %E may ""%qD specified size %E may " "exceed maximum object size %E" | |||
823 | "exceed maximum object size %E")"%qD specified size %E may " "exceed maximum object size %E" | |||
824 | : G_("%qD specified size %E ""%qD specified size %E " "exceeds maximum object size %E" | |||
825 | "exceeds maximum object size %E")"%qD specified size %E " "exceeds maximum object size %E"), | |||
826 | func, bndrng[0], maxobjsize) | |||
827 | : warning_at (loc, opt, | |||
828 | (maybe | |||
829 | ? G_("specified size %E may exceed ""specified size %E may exceed " "maximum object size %E" | |||
830 | "maximum object size %E")"specified size %E may exceed " "maximum object size %E" | |||
831 | : G_("specified size %E exceeds ""specified size %E exceeds " "maximum object size %E" | |||
832 | "maximum object size %E")"specified size %E exceeds " "maximum object size %E"), | |||
833 | bndrng[0], maxobjsize)); | |||
834 | else | |||
835 | warned = (func | |||
836 | ? warning_at (loc, opt, | |||
837 | (maybe | |||
838 | ? G_("%qD specified size between %E and %E ""%qD specified size between %E and %E " "may exceed maximum object size %E" | |||
839 | "may exceed maximum object size %E")"%qD specified size between %E and %E " "may exceed maximum object size %E" | |||
840 | : G_("%qD specified size between %E and %E ""%qD specified size between %E and %E " "exceeds maximum object size %E" | |||
841 | "exceeds maximum object size %E")"%qD specified size between %E and %E " "exceeds maximum object size %E"), | |||
842 | func, bndrng[0], bndrng[1], maxobjsize) | |||
843 | : warning_at (loc, opt, | |||
844 | (maybe | |||
845 | ? G_("specified size between %E and %E ""specified size between %E and %E " "may exceed maximum object size %E" | |||
846 | "may exceed maximum object size %E")"specified size between %E and %E " "may exceed maximum object size %E" | |||
847 | : G_("specified size between %E and %E ""specified size between %E and %E " "exceeds maximum object size %E" | |||
848 | "exceeds maximum object size %E")"specified size between %E and %E " "exceeds maximum object size %E"), | |||
849 | bndrng[0], bndrng[1], maxobjsize)); | |||
850 | } | |||
851 | else if (!size || tree_int_cst_le (bndrng[0], size)) | |||
852 | return false; | |||
853 | else if (tree_int_cst_equal (bndrng[0], bndrng[1])) | |||
854 | warned = (func | |||
855 | ? warning_at (loc, opt, | |||
856 | (maybe | |||
857 | ? G_("%qD specified bound %E may exceed ""%qD specified bound %E may exceed " "destination size %E" | |||
858 | "destination size %E")"%qD specified bound %E may exceed " "destination size %E" | |||
859 | : G_("%qD specified bound %E exceeds ""%qD specified bound %E exceeds " "destination size %E" | |||
860 | "destination size %E")"%qD specified bound %E exceeds " "destination size %E"), | |||
861 | func, bndrng[0], size) | |||
862 | : warning_at (loc, opt, | |||
863 | (maybe | |||
864 | ? G_("specified bound %E may exceed ""specified bound %E may exceed " "destination size %E" | |||
865 | "destination size %E")"specified bound %E may exceed " "destination size %E" | |||
866 | : G_("specified bound %E exceeds ""specified bound %E exceeds " "destination size %E" | |||
867 | "destination size %E")"specified bound %E exceeds " "destination size %E"), | |||
868 | bndrng[0], size)); | |||
869 | else | |||
870 | warned = (func | |||
871 | ? warning_at (loc, opt, | |||
872 | (maybe | |||
873 | ? G_("%qD specified bound [%E, %E] may exceed ""%qD specified bound [%E, %E] may exceed " "destination size %E" | |||
874 | "destination size %E")"%qD specified bound [%E, %E] may exceed " "destination size %E" | |||
875 | : G_("%qD specified bound [%E, %E] exceeds ""%qD specified bound [%E, %E] exceeds " "destination size %E" | |||
876 | "destination size %E")"%qD specified bound [%E, %E] exceeds " "destination size %E"), | |||
877 | func, bndrng[0], bndrng[1], size) | |||
878 | : warning_at (loc, opt, | |||
879 | (maybe | |||
880 | ? G_("specified bound [%E, %E] exceeds ""specified bound [%E, %E] exceeds " "destination size %E" | |||
881 | "destination size %E")"specified bound [%E, %E] exceeds " "destination size %E" | |||
882 | : G_("specified bound [%E, %E] exceeds ""specified bound [%E, %E] exceeds " "destination size %E" | |||
883 | "destination size %E")"specified bound [%E, %E] exceeds " "destination size %E"), | |||
884 | bndrng[0], bndrng[1], size)); | |||
885 | ||||
886 | if (warned) | |||
887 | { | |||
888 | if (pad && pad->dst.ref | |||
889 | && has_location (pad->dst.ref)) | |||
890 | inform (get_location (pad->dst.ref), | |||
891 | "destination object allocated here"); | |||
892 | suppress_warning (exp, opt); | |||
893 | } | |||
894 | ||||
895 | return warned; | |||
896 | } | |||
897 | ||||
898 | bool | |||
899 | maybe_warn_for_bound (opt_code opt, location_t loc, gimple *stmt, tree func, | |||
900 | tree bndrng[2], tree size, | |||
901 | const access_data *pad /* = NULL */) | |||
902 | { | |||
903 | return maybe_warn_for_bound<gimple *> (opt, loc, stmt, func, bndrng, size, | |||
904 | pad); | |||
905 | } | |||
906 | ||||
907 | bool | |||
908 | maybe_warn_for_bound (opt_code opt, location_t loc, tree expr, tree func, | |||
909 | tree bndrng[2], tree size, | |||
910 | const access_data *pad /* = NULL */) | |||
911 | { | |||
912 | return maybe_warn_for_bound<tree> (opt, loc, expr, func, bndrng, size, pad); | |||
913 | } | |||
914 | ||||
915 | /* For an expression EXP issue an access warning controlled by option OPT | |||
916 | with access to a region SIZE bytes in size in the RANGE of sizes. | |||
917 | WRITE is true for a write access, READ for a read access, neither for | |||
918 | call that may or may not perform an access but for which the range | |||
919 | is expected to valid. | |||
920 | Returns true when a warning has been issued. */ | |||
921 | ||||
922 | template <class GimpleOrTree> | |||
923 | static bool | |||
924 | warn_for_access (location_t loc, tree func, GimpleOrTree exp, int opt, | |||
925 | tree range[2], tree size, bool write, bool read, bool maybe) | |||
926 | { | |||
927 | bool warned = false; | |||
928 | ||||
929 | if (write && read) | |||
930 | { | |||
931 | if (tree_int_cst_equal (range[0], range[1])) | |||
932 | warned = (func | |||
933 | ? warning_n (loc, opt, tree_to_uhwi (range[0]), | |||
934 | (maybe | |||
935 | ? G_("%qD may access %E byte in a region ""%qD may access %E byte in a region " "of size %E" | |||
936 | "of size %E")"%qD may access %E byte in a region " "of size %E" | |||
937 | : G_("%qD accessing %E byte in a region ""%qD accessing %E byte in a region " "of size %E" | |||
938 | "of size %E")"%qD accessing %E byte in a region " "of size %E"), | |||
939 | (maybe | |||
940 | ? G_ ("%qD may access %E bytes in a region ""%qD may access %E bytes in a region " "of size %E" | |||
941 | "of size %E")"%qD may access %E bytes in a region " "of size %E" | |||
942 | : G_ ("%qD accessing %E bytes in a region ""%qD accessing %E bytes in a region " "of size %E" | |||
943 | "of size %E")"%qD accessing %E bytes in a region " "of size %E"), | |||
944 | func, range[0], size) | |||
945 | : warning_n (loc, opt, tree_to_uhwi (range[0]), | |||
946 | (maybe | |||
947 | ? G_("may access %E byte in a region ""may access %E byte in a region " "of size %E" | |||
948 | "of size %E")"may access %E byte in a region " "of size %E" | |||
949 | : G_("accessing %E byte in a region ""accessing %E byte in a region " "of size %E" | |||
950 | "of size %E")"accessing %E byte in a region " "of size %E"), | |||
951 | (maybe | |||
952 | ? G_("may access %E bytes in a region ""may access %E bytes in a region " "of size %E" | |||
953 | "of size %E")"may access %E bytes in a region " "of size %E" | |||
954 | : G_("accessing %E bytes in a region ""accessing %E bytes in a region " "of size %E" | |||
955 | "of size %E")"accessing %E bytes in a region " "of size %E"), | |||
956 | range[0], size)); | |||
957 | else if (tree_int_cst_sign_bit (range[1])) | |||
958 | { | |||
959 | /* Avoid printing the upper bound if it's invalid. */ | |||
960 | warned = (func | |||
961 | ? warning_at (loc, opt, | |||
962 | (maybe | |||
963 | ? G_("%qD may access %E or more bytes ""%qD may access %E or more bytes " "in a region of size %E" | |||
964 | "in a region of size %E")"%qD may access %E or more bytes " "in a region of size %E" | |||
965 | : G_("%qD accessing %E or more bytes ""%qD accessing %E or more bytes " "in a region of size %E" | |||
966 | "in a region of size %E")"%qD accessing %E or more bytes " "in a region of size %E"), | |||
967 | func, range[0], size) | |||
968 | : warning_at (loc, opt, | |||
969 | (maybe | |||
970 | ? G_("may access %E or more bytes ""may access %E or more bytes " "in a region of size %E" | |||
971 | "in a region of size %E")"may access %E or more bytes " "in a region of size %E" | |||
972 | : G_("accessing %E or more bytes ""accessing %E or more bytes " "in a region of size %E" | |||
973 | "in a region of size %E")"accessing %E or more bytes " "in a region of size %E"), | |||
974 | range[0], size)); | |||
975 | } | |||
976 | else | |||
977 | warned = (func | |||
978 | ? warning_at (loc, opt, | |||
979 | (maybe | |||
980 | ? G_("%qD may access between %E and %E ""%qD may access between %E and %E " "bytes in a region of size %E" | |||
981 | "bytes in a region of size %E")"%qD may access between %E and %E " "bytes in a region of size %E" | |||
982 | : G_("%qD accessing between %E and %E ""%qD accessing between %E and %E " "bytes in a region of size %E" | |||
983 | "bytes in a region of size %E")"%qD accessing between %E and %E " "bytes in a region of size %E"), | |||
984 | func, range[0], range[1], size) | |||
985 | : warning_at (loc, opt, | |||
986 | (maybe | |||
987 | ? G_("may access between %E and %E bytes ""may access between %E and %E bytes " "in a region of size %E" | |||
988 | "in a region of size %E")"may access between %E and %E bytes " "in a region of size %E" | |||
989 | : G_("accessing between %E and %E bytes ""accessing between %E and %E bytes " "in a region of size %E" | |||
990 | "in a region of size %E")"accessing between %E and %E bytes " "in a region of size %E"), | |||
991 | range[0], range[1], size)); | |||
992 | return warned; | |||
993 | } | |||
994 | ||||
995 | if (write) | |||
996 | { | |||
997 | if (tree_int_cst_equal (range[0], range[1])) | |||
998 | warned = (func | |||
999 | ? warning_n (loc, opt, tree_to_uhwi (range[0]), | |||
1000 | (maybe | |||
1001 | ? G_("%qD may write %E byte into a region ""%qD may write %E byte into a region " "of size %E" | |||
1002 | "of size %E")"%qD may write %E byte into a region " "of size %E" | |||
1003 | : G_("%qD writing %E byte into a region ""%qD writing %E byte into a region " "of size %E overflows the destination" | |||
1004 | "of size %E overflows the destination")"%qD writing %E byte into a region " "of size %E overflows the destination"), | |||
1005 | (maybe | |||
1006 | ? G_("%qD may write %E bytes into a region ""%qD may write %E bytes into a region " "of size %E" | |||
1007 | "of size %E")"%qD may write %E bytes into a region " "of size %E" | |||
1008 | : G_("%qD writing %E bytes into a region ""%qD writing %E bytes into a region " "of size %E overflows the destination" | |||
1009 | "of size %E overflows the destination")"%qD writing %E bytes into a region " "of size %E overflows the destination"), | |||
1010 | func, range[0], size) | |||
1011 | : warning_n (loc, opt, tree_to_uhwi (range[0]), | |||
1012 | (maybe | |||
1013 | ? G_("may write %E byte into a region ""may write %E byte into a region " "of size %E" | |||
1014 | "of size %E")"may write %E byte into a region " "of size %E" | |||
1015 | : G_("writing %E byte into a region ""writing %E byte into a region " "of size %E overflows the destination" | |||
1016 | "of size %E overflows the destination")"writing %E byte into a region " "of size %E overflows the destination"), | |||
1017 | (maybe | |||
1018 | ? G_("may write %E bytes into a region ""may write %E bytes into a region " "of size %E" | |||
1019 | "of size %E")"may write %E bytes into a region " "of size %E" | |||
1020 | : G_("writing %E bytes into a region ""writing %E bytes into a region " "of size %E overflows the destination" | |||
1021 | "of size %E overflows the destination")"writing %E bytes into a region " "of size %E overflows the destination"), | |||
1022 | range[0], size)); | |||
1023 | else if (tree_int_cst_sign_bit (range[1])) | |||
1024 | { | |||
1025 | /* Avoid printing the upper bound if it's invalid. */ | |||
1026 | warned = (func | |||
1027 | ? warning_at (loc, opt, | |||
1028 | (maybe | |||
1029 | ? G_("%qD may write %E or more bytes ""%qD may write %E or more bytes " "into a region of size %E" | |||
1030 | "into a region of size %E")"%qD may write %E or more bytes " "into a region of size %E" | |||
1031 | : G_("%qD writing %E or more bytes ""%qD writing %E or more bytes " "into a region of size %E overflows " "the destination" | |||
1032 | "into a region of size %E overflows ""%qD writing %E or more bytes " "into a region of size %E overflows " "the destination" | |||
1033 | "the destination")"%qD writing %E or more bytes " "into a region of size %E overflows " "the destination"), | |||
1034 | func, range[0], size) | |||
1035 | : warning_at (loc, opt, | |||
1036 | (maybe | |||
1037 | ? G_("may write %E or more bytes into ""may write %E or more bytes into " "a region of size %E" | |||
1038 | "a region of size %E")"may write %E or more bytes into " "a region of size %E" | |||
1039 | : G_("writing %E or more bytes into ""writing %E or more bytes into " "a region of size %E overflows " "the destination" | |||
1040 | "a region of size %E overflows ""writing %E or more bytes into " "a region of size %E overflows " "the destination" | |||
1041 | "the destination")"writing %E or more bytes into " "a region of size %E overflows " "the destination"), | |||
1042 | range[0], size)); | |||
1043 | } | |||
1044 | else | |||
1045 | warned = (func | |||
1046 | ? warning_at (loc, opt, | |||
1047 | (maybe | |||
1048 | ? G_("%qD may write between %E and %E bytes ""%qD may write between %E and %E bytes " "into a region of size %E" | |||
1049 | "into a region of size %E")"%qD may write between %E and %E bytes " "into a region of size %E" | |||
1050 | : G_("%qD writing between %E and %E bytes ""%qD writing between %E and %E bytes " "into a region of size %E overflows " "the destination" | |||
1051 | "into a region of size %E overflows ""%qD writing between %E and %E bytes " "into a region of size %E overflows " "the destination" | |||
1052 | "the destination")"%qD writing between %E and %E bytes " "into a region of size %E overflows " "the destination"), | |||
1053 | func, range[0], range[1], size) | |||
1054 | : warning_at (loc, opt, | |||
1055 | (maybe | |||
1056 | ? G_("may write between %E and %E bytes ""may write between %E and %E bytes " "into a region of size %E" | |||
1057 | "into a region of size %E")"may write between %E and %E bytes " "into a region of size %E" | |||
1058 | : G_("writing between %E and %E bytes ""writing between %E and %E bytes " "into a region of size %E overflows " "the destination" | |||
1059 | "into a region of size %E overflows ""writing between %E and %E bytes " "into a region of size %E overflows " "the destination" | |||
1060 | "the destination")"writing between %E and %E bytes " "into a region of size %E overflows " "the destination"), | |||
1061 | range[0], range[1], size)); | |||
1062 | return warned; | |||
1063 | } | |||
1064 | ||||
1065 | if (read) | |||
1066 | { | |||
1067 | if (tree_int_cst_equal (range[0], range[1])) | |||
1068 | warned = (func | |||
1069 | ? warning_n (loc, OPT_Wstringop_overread, | |||
1070 | tree_to_uhwi (range[0]), | |||
1071 | (maybe | |||
1072 | ? G_("%qD may read %E byte from a region ""%qD may read %E byte from a region " "of size %E" | |||
1073 | "of size %E")"%qD may read %E byte from a region " "of size %E" | |||
1074 | : G_("%qD reading %E byte from a region ""%qD reading %E byte from a region " "of size %E" | |||
1075 | "of size %E")"%qD reading %E byte from a region " "of size %E"), | |||
1076 | (maybe | |||
1077 | ? G_("%qD may read %E bytes from a region ""%qD may read %E bytes from a region " "of size %E" | |||
1078 | "of size %E")"%qD may read %E bytes from a region " "of size %E" | |||
1079 | : G_("%qD reading %E bytes from a region ""%qD reading %E bytes from a region " "of size %E" | |||
1080 | "of size %E")"%qD reading %E bytes from a region " "of size %E"), | |||
1081 | func, range[0], size) | |||
1082 | : warning_n (loc, OPT_Wstringop_overread, | |||
1083 | tree_to_uhwi (range[0]), | |||
1084 | (maybe | |||
1085 | ? G_("may read %E byte from a region ""may read %E byte from a region " "of size %E" | |||
1086 | "of size %E")"may read %E byte from a region " "of size %E" | |||
1087 | : G_("reading %E byte from a region ""reading %E byte from a region " "of size %E" | |||
1088 | "of size %E")"reading %E byte from a region " "of size %E"), | |||
1089 | (maybe | |||
1090 | ? G_("may read %E bytes from a region ""may read %E bytes from a region " "of size %E" | |||
1091 | "of size %E")"may read %E bytes from a region " "of size %E" | |||
1092 | : G_("reading %E bytes from a region ""reading %E bytes from a region " "of size %E" | |||
1093 | "of size %E")"reading %E bytes from a region " "of size %E"), | |||
1094 | range[0], size)); | |||
1095 | else if (tree_int_cst_sign_bit (range[1])) | |||
1096 | { | |||
1097 | /* Avoid printing the upper bound if it's invalid. */ | |||
1098 | warned = (func | |||
1099 | ? warning_at (loc, OPT_Wstringop_overread, | |||
1100 | (maybe | |||
1101 | ? G_("%qD may read %E or more bytes ""%qD may read %E or more bytes " "from a region of size %E" | |||
1102 | "from a region of size %E")"%qD may read %E or more bytes " "from a region of size %E" | |||
1103 | : G_("%qD reading %E or more bytes ""%qD reading %E or more bytes " "from a region of size %E" | |||
1104 | "from a region of size %E")"%qD reading %E or more bytes " "from a region of size %E"), | |||
1105 | func, range[0], size) | |||
1106 | : warning_at (loc, OPT_Wstringop_overread, | |||
1107 | (maybe | |||
1108 | ? G_("may read %E or more bytes ""may read %E or more bytes " "from a region of size %E" | |||
1109 | "from a region of size %E")"may read %E or more bytes " "from a region of size %E" | |||
1110 | : G_("reading %E or more bytes ""reading %E or more bytes " "from a region of size %E" | |||
1111 | "from a region of size %E")"reading %E or more bytes " "from a region of size %E"), | |||
1112 | range[0], size)); | |||
1113 | } | |||
1114 | else | |||
1115 | warned = (func | |||
1116 | ? warning_at (loc, OPT_Wstringop_overread, | |||
1117 | (maybe | |||
1118 | ? G_("%qD may read between %E and %E bytes ""%qD may read between %E and %E bytes " "from a region of size %E" | |||
1119 | "from a region of size %E")"%qD may read between %E and %E bytes " "from a region of size %E" | |||
1120 | : G_("%qD reading between %E and %E bytes ""%qD reading between %E and %E bytes " "from a region of size %E" | |||
1121 | "from a region of size %E")"%qD reading between %E and %E bytes " "from a region of size %E"), | |||
1122 | func, range[0], range[1], size) | |||
1123 | : warning_at (loc, opt, | |||
1124 | (maybe | |||
1125 | ? G_("may read between %E and %E bytes ""may read between %E and %E bytes " "from a region of size %E" | |||
1126 | "from a region of size %E")"may read between %E and %E bytes " "from a region of size %E" | |||
1127 | : G_("reading between %E and %E bytes ""reading between %E and %E bytes " "from a region of size %E" | |||
1128 | "from a region of size %E")"reading between %E and %E bytes " "from a region of size %E"), | |||
1129 | range[0], range[1], size)); | |||
1130 | ||||
1131 | if (warned) | |||
1132 | suppress_warning (exp, OPT_Wstringop_overread); | |||
1133 | ||||
1134 | return warned; | |||
1135 | } | |||
1136 | ||||
1137 | if (tree_int_cst_equal (range[0], range[1]) | |||
1138 | || tree_int_cst_sign_bit (range[1])) | |||
1139 | warned = (func | |||
1140 | ? warning_n (loc, OPT_Wstringop_overread, | |||
1141 | tree_to_uhwi (range[0]), | |||
1142 | "%qD expecting %E byte in a region of size %E", | |||
1143 | "%qD expecting %E bytes in a region of size %E", | |||
1144 | func, range[0], size) | |||
1145 | : warning_n (loc, OPT_Wstringop_overread, | |||
1146 | tree_to_uhwi (range[0]), | |||
1147 | "expecting %E byte in a region of size %E", | |||
1148 | "expecting %E bytes in a region of size %E", | |||
1149 | range[0], size)); | |||
1150 | else if (tree_int_cst_sign_bit (range[1])) | |||
1151 | { | |||
1152 | /* Avoid printing the upper bound if it's invalid. */ | |||
1153 | warned = (func | |||
1154 | ? warning_at (loc, OPT_Wstringop_overread, | |||
1155 | "%qD expecting %E or more bytes in a region " | |||
1156 | "of size %E", | |||
1157 | func, range[0], size) | |||
1158 | : warning_at (loc, OPT_Wstringop_overread, | |||
1159 | "expecting %E or more bytes in a region " | |||
1160 | "of size %E", | |||
1161 | range[0], size)); | |||
1162 | } | |||
1163 | else | |||
1164 | warned = (func | |||
1165 | ? warning_at (loc, OPT_Wstringop_overread, | |||
1166 | "%qD expecting between %E and %E bytes in " | |||
1167 | "a region of size %E", | |||
1168 | func, range[0], range[1], size) | |||
1169 | : warning_at (loc, OPT_Wstringop_overread, | |||
1170 | "expecting between %E and %E bytes in " | |||
1171 | "a region of size %E", | |||
1172 | range[0], range[1], size)); | |||
1173 | ||||
1174 | if (warned) | |||
1175 | suppress_warning (exp, OPT_Wstringop_overread); | |||
1176 | ||||
1177 | return warned; | |||
1178 | } | |||
1179 | ||||
1180 | static bool | |||
1181 | warn_for_access (location_t loc, tree func, gimple *stmt, int opt, | |||
1182 | tree range[2], tree size, bool write, bool read, bool maybe) | |||
1183 | { | |||
1184 | return warn_for_access<gimple *>(loc, func, stmt, opt, range, size, | |||
1185 | write, read, maybe); | |||
1186 | } | |||
1187 | ||||
1188 | static bool | |||
1189 | warn_for_access (location_t loc, tree func, tree expr, int opt, | |||
1190 | tree range[2], tree size, bool write, bool read, bool maybe) | |||
1191 | { | |||
1192 | return warn_for_access<tree>(loc, func, expr, opt, range, size, | |||
1193 | write, read, maybe); | |||
1194 | } | |||
1195 | ||||
1196 | /* Helper to set RANGE to the range of BOUND if it's nonnull, bounded | |||
1197 | by BNDRNG if nonnull and valid. */ | |||
1198 | ||||
1199 | static void | |||
1200 | get_size_range (range_query *query, tree bound, gimple *stmt, tree range[2], | |||
1201 | const offset_int bndrng[2]) | |||
1202 | { | |||
1203 | if (bound) | |||
1204 | get_size_range (query, bound, stmt, range); | |||
1205 | ||||
1206 | if (!bndrng || (bndrng[0] == 0 && bndrng[1] == HOST_WIDE_INT_M1U-1UL)) | |||
1207 | return; | |||
1208 | ||||
1209 | if (range[0] && TREE_CODE (range[0])((enum tree_code) (range[0])->base.code) == INTEGER_CST) | |||
1210 | { | |||
1211 | offset_int r[] = | |||
1212 | { wi::to_offset (range[0]), wi::to_offset (range[1]) }; | |||
1213 | if (r[0] < bndrng[0]) | |||
1214 | range[0] = wide_int_to_tree (sizetypesizetype_tab[(int) stk_sizetype], bndrng[0]); | |||
1215 | if (bndrng[1] < r[1]) | |||
1216 | range[1] = wide_int_to_tree (sizetypesizetype_tab[(int) stk_sizetype], bndrng[1]); | |||
1217 | } | |||
1218 | else | |||
1219 | { | |||
1220 | range[0] = wide_int_to_tree (sizetypesizetype_tab[(int) stk_sizetype], bndrng[0]); | |||
1221 | range[1] = wide_int_to_tree (sizetypesizetype_tab[(int) stk_sizetype], bndrng[1]); | |||
1222 | } | |||
1223 | } | |||
1224 | ||||
1225 | /* Try to verify that the sizes and lengths of the arguments to a string | |||
1226 | manipulation function given by EXP are within valid bounds and that | |||
1227 | the operation does not lead to buffer overflow or read past the end. | |||
1228 | Arguments other than EXP may be null. When non-null, the arguments | |||
1229 | have the following meaning: | |||
1230 | DST is the destination of a copy call or NULL otherwise. | |||
1231 | SRC is the source of a copy call or NULL otherwise. | |||
1232 | DSTWRITE is the number of bytes written into the destination obtained | |||
1233 | from the user-supplied size argument to the function (such as in | |||
1234 | memcpy(DST, SRCs, DSTWRITE) or strncpy(DST, DRC, DSTWRITE). | |||
1235 | MAXREAD is the user-supplied bound on the length of the source sequence | |||
1236 | (such as in strncat(d, s, N). It specifies the upper limit on the number | |||
1237 | of bytes to write. If NULL, it's taken to be the same as DSTWRITE. | |||
1238 | SRCSTR is the source string (such as in strcpy(DST, SRC)) when the | |||
1239 | expression EXP is a string function call (as opposed to a memory call | |||
1240 | like memcpy). As an exception, SRCSTR can also be an integer denoting | |||
1241 | the precomputed size of the source string or object (for functions like | |||
1242 | memcpy). | |||
1243 | DSTSIZE is the size of the destination object. | |||
1244 | ||||
1245 | When DSTWRITE is null LEN is checked to verify that it doesn't exceed | |||
1246 | SIZE_MAX. | |||
1247 | ||||
1248 | WRITE is true for write accesses, READ is true for reads. Both are | |||
1249 | false for simple size checks in calls to functions that neither read | |||
1250 | from nor write to the region. | |||
1251 | ||||
1252 | When nonnull, PAD points to a more detailed description of the access. | |||
1253 | ||||
1254 | If the call is successfully verified as safe return true, otherwise | |||
1255 | return false. */ | |||
1256 | ||||
1257 | template <class GimpleOrTree> | |||
1258 | static bool | |||
1259 | check_access (GimpleOrTree exp, tree dstwrite, | |||
1260 | tree maxread, tree srcstr, tree dstsize, | |||
1261 | access_mode mode, const access_data *pad, | |||
1262 | range_query *rvals) | |||
1263 | { | |||
1264 | /* The size of the largest object is half the address space, or | |||
1265 | PTRDIFF_MAX. (This is way too permissive.) */ | |||
1266 | tree maxobjsize = max_object_size (); | |||
1267 | ||||
1268 | /* Either an approximate/minimum the length of the source string for | |||
1269 | string functions or the size of the source object for raw memory | |||
1270 | functions. */ | |||
1271 | tree slen = NULL_TREE(tree) nullptr; | |||
1272 | ||||
1273 | /* The range of the access in bytes; first set to the write access | |||
1274 | for functions that write and then read for those that also (or | |||
1275 | just) read. */ | |||
1276 | tree range[2] = { NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr }; | |||
1277 | ||||
1278 | /* Set to true when the exact number of bytes written by a string | |||
1279 | function like strcpy is not known and the only thing that is | |||
1280 | known is that it must be at least one (for the terminating nul). */ | |||
1281 | bool at_least_one = false; | |||
1282 | if (srcstr) | |||
1283 | { | |||
1284 | /* SRCSTR is normally a pointer to string but as a special case | |||
1285 | it can be an integer denoting the length of a string. */ | |||
1286 | if (POINTER_TYPE_P (TREE_TYPE (srcstr))(((enum tree_code) (((contains_struct_check ((srcstr), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1286, __FUNCTION__))->typed.type))->base.code) == POINTER_TYPE || ((enum tree_code) (((contains_struct_check ((srcstr), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1286, __FUNCTION__))->typed.type))->base.code) == REFERENCE_TYPE )) | |||
1287 | { | |||
1288 | if (!check_nul_terminated_array (exp, srcstr, maxread)) | |||
1289 | /* Return if the array is not nul-terminated and a warning | |||
1290 | has been issued. */ | |||
1291 | return false; | |||
1292 | ||||
1293 | /* Try to determine the range of lengths the source string | |||
1294 | refers to. If it can be determined and is less than | |||
1295 | the upper bound given by MAXREAD add one to it for | |||
1296 | the terminating nul. Otherwise, set it to one for | |||
1297 | the same reason, or to MAXREAD as appropriate. */ | |||
1298 | c_strlen_data lendata = { }; | |||
1299 | get_range_strlen (srcstr, &lendata, /* eltsize = */ 1); | |||
1300 | range[0] = lendata.minlen; | |||
1301 | range[1] = lendata.maxbound ? lendata.maxbound : lendata.maxlen; | |||
1302 | if (range[0] | |||
1303 | && TREE_CODE (range[0])((enum tree_code) (range[0])->base.code) == INTEGER_CST | |||
1304 | && TREE_CODE (range[1])((enum tree_code) (range[1])->base.code) == INTEGER_CST | |||
1305 | && (!maxread || TREE_CODE (maxread)((enum tree_code) (maxread)->base.code) == INTEGER_CST)) | |||
1306 | { | |||
1307 | if (maxread && tree_int_cst_le (maxread, range[0])) | |||
1308 | range[0] = range[1] = maxread; | |||
1309 | else | |||
1310 | range[0] = fold_build2 (PLUS_EXPR, size_type_node,fold_build2_loc (((location_t) 0), PLUS_EXPR, global_trees[TI_SIZE_TYPE ], range[0], global_trees[TI_SIZE_ONE] ) | |||
1311 | range[0], size_one_node)fold_build2_loc (((location_t) 0), PLUS_EXPR, global_trees[TI_SIZE_TYPE ], range[0], global_trees[TI_SIZE_ONE] ); | |||
1312 | ||||
1313 | if (maxread && tree_int_cst_le (maxread, range[1])) | |||
1314 | range[1] = maxread; | |||
1315 | else if (!integer_all_onesp (range[1])) | |||
1316 | range[1] = fold_build2 (PLUS_EXPR, size_type_node,fold_build2_loc (((location_t) 0), PLUS_EXPR, global_trees[TI_SIZE_TYPE ], range[1], global_trees[TI_SIZE_ONE] ) | |||
1317 | range[1], size_one_node)fold_build2_loc (((location_t) 0), PLUS_EXPR, global_trees[TI_SIZE_TYPE ], range[1], global_trees[TI_SIZE_ONE] ); | |||
1318 | ||||
1319 | slen = range[0]; | |||
1320 | } | |||
1321 | else | |||
1322 | { | |||
1323 | at_least_one = true; | |||
1324 | slen = size_one_nodeglobal_trees[TI_SIZE_ONE]; | |||
1325 | } | |||
1326 | } | |||
1327 | else | |||
1328 | slen = srcstr; | |||
1329 | } | |||
1330 | ||||
1331 | if (!dstwrite && !maxread) | |||
1332 | { | |||
1333 | /* When the only available piece of data is the object size | |||
1334 | there is nothing to do. */ | |||
1335 | if (!slen) | |||
1336 | return true; | |||
1337 | ||||
1338 | /* Otherwise, when the length of the source sequence is known | |||
1339 | (as with strlen), set DSTWRITE to it. */ | |||
1340 | if (!range[0]) | |||
1341 | dstwrite = slen; | |||
1342 | } | |||
1343 | ||||
1344 | if (!dstsize) | |||
1345 | dstsize = maxobjsize; | |||
1346 | ||||
1347 | /* Set RANGE to that of DSTWRITE if non-null, bounded by PAD->DST_BNDRNG | |||
1348 | if valid. */ | |||
1349 | gimple *stmt = pad ? pad->stmt : nullptr; | |||
1350 | get_size_range (rvals, dstwrite, stmt, range, pad ? pad->dst_bndrng : NULLnullptr); | |||
1351 | ||||
1352 | tree func = get_callee_fndecl (exp); | |||
1353 | /* Read vs write access by built-ins can be determined from the const | |||
1354 | qualifiers on the pointer argument. In the absence of attribute | |||
1355 | access, non-const qualified pointer arguments to user-defined | |||
1356 | functions are assumed to both read and write the objects. */ | |||
1357 | const bool builtin = func ? fndecl_built_in_p (func) : false; | |||
1358 | ||||
1359 | /* First check the number of bytes to be written against the maximum | |||
1360 | object size. */ | |||
1361 | if (range[0] | |||
1362 | && TREE_CODE (range[0])((enum tree_code) (range[0])->base.code) == INTEGER_CST | |||
1363 | && tree_int_cst_lt (maxobjsize, range[0])) | |||
1364 | { | |||
1365 | location_t loc = get_location (exp); | |||
1366 | maybe_warn_for_bound (OPT_Wstringop_overflow_, loc, exp, func, range, | |||
1367 | NULL_TREE(tree) nullptr, pad); | |||
1368 | return false; | |||
1369 | } | |||
1370 | ||||
1371 | /* The number of bytes to write is "exact" if DSTWRITE is non-null, | |||
1372 | constant, and in range of unsigned HOST_WIDE_INT. */ | |||
1373 | bool exactwrite = dstwrite && tree_fits_uhwi_p (dstwrite); | |||
1374 | ||||
1375 | /* Next check the number of bytes to be written against the destination | |||
1376 | object size. */ | |||
1377 | if (range[0] || !exactwrite || integer_all_onesp (dstwrite)) | |||
1378 | { | |||
1379 | if (range[0] | |||
1380 | && TREE_CODE (range[0])((enum tree_code) (range[0])->base.code) == INTEGER_CST | |||
1381 | && ((tree_fits_uhwi_p (dstsize) | |||
1382 | && tree_int_cst_lt (dstsize, range[0])) | |||
1383 | || (dstwrite | |||
1384 | && tree_fits_uhwi_p (dstwrite) | |||
1385 | && tree_int_cst_lt (dstwrite, range[0])))) | |||
1386 | { | |||
1387 | const opt_code opt = OPT_Wstringop_overflow_; | |||
1388 | if (warning_suppressed_p (exp, opt) | |||
1389 | || (pad && pad->dst.ref | |||
1390 | && warning_suppressed_p (pad->dst.ref, opt))) | |||
1391 | return false; | |||
1392 | ||||
1393 | auto_diagnostic_group d; | |||
1394 | location_t loc = get_location (exp); | |||
1395 | bool warned = false; | |||
1396 | if (dstwrite == slen && at_least_one) | |||
1397 | { | |||
1398 | /* This is a call to strcpy with a destination of 0 size | |||
1399 | and a source of unknown length. The call will write | |||
1400 | at least one byte past the end of the destination. */ | |||
1401 | warned = (func | |||
1402 | ? warning_at (loc, opt, | |||
1403 | "%qD writing %E or more bytes into " | |||
1404 | "a region of size %E overflows " | |||
1405 | "the destination", | |||
1406 | func, range[0], dstsize) | |||
1407 | : warning_at (loc, opt, | |||
1408 | "writing %E or more bytes into " | |||
1409 | "a region of size %E overflows " | |||
1410 | "the destination", | |||
1411 | range[0], dstsize)); | |||
1412 | } | |||
1413 | else | |||
1414 | { | |||
1415 | const bool read | |||
1416 | = mode == access_read_only || mode == access_read_write; | |||
1417 | const bool write | |||
1418 | = mode == access_write_only || mode == access_read_write; | |||
1419 | const bool maybe = pad && pad->dst.parmarray; | |||
1420 | warned = warn_for_access (loc, func, exp, | |||
1421 | OPT_Wstringop_overflow_, | |||
1422 | range, dstsize, | |||
1423 | write, read && !builtin, maybe); | |||
1424 | } | |||
1425 | ||||
1426 | if (warned) | |||
1427 | { | |||
1428 | suppress_warning (exp, OPT_Wstringop_overflow_); | |||
1429 | if (pad) | |||
1430 | pad->dst.inform_access (pad->mode); | |||
1431 | } | |||
1432 | ||||
1433 | /* Return error when an overflow has been detected. */ | |||
1434 | return false; | |||
1435 | } | |||
1436 | } | |||
1437 | ||||
1438 | /* Check the maximum length of the source sequence against the size | |||
1439 | of the destination object if known, or against the maximum size | |||
1440 | of an object. */ | |||
1441 | if (maxread) | |||
1442 | { | |||
1443 | /* Set RANGE to that of MAXREAD, bounded by PAD->SRC_BNDRNG if | |||
1444 | PAD is nonnull and BNDRNG is valid. */ | |||
1445 | get_size_range (rvals, maxread, stmt, range, pad ? pad->src_bndrng : NULLnullptr); | |||
1446 | ||||
1447 | location_t loc = get_location (exp); | |||
1448 | tree size = dstsize; | |||
1449 | if (pad && pad->mode == access_read_only) | |||
1450 | size = wide_int_to_tree (sizetypesizetype_tab[(int) stk_sizetype], pad->src.size_remaining ()); | |||
1451 | ||||
1452 | if (range[0] && maxread && tree_fits_uhwi_p (size)) | |||
1453 | { | |||
1454 | if (tree_int_cst_lt (maxobjsize, range[0])) | |||
1455 | { | |||
1456 | maybe_warn_for_bound (OPT_Wstringop_overread, loc, exp, func, | |||
1457 | range, size, pad); | |||
1458 | return false; | |||
1459 | } | |||
1460 | ||||
1461 | if (size != maxobjsize && tree_int_cst_lt (size, range[0])) | |||
1462 | { | |||
1463 | opt_code opt = (dstwrite || mode != access_read_only | |||
1464 | ? OPT_Wstringop_overflow_ | |||
1465 | : OPT_Wstringop_overread); | |||
1466 | maybe_warn_for_bound (opt, loc, exp, func, range, size, pad); | |||
1467 | return false; | |||
1468 | } | |||
1469 | } | |||
1470 | ||||
1471 | maybe_warn_nonstring_arg (func, exp); | |||
1472 | } | |||
1473 | ||||
1474 | /* Check for reading past the end of SRC. */ | |||
1475 | bool overread = (slen | |||
1476 | && slen == srcstr | |||
1477 | && dstwrite | |||
1478 | && range[0] | |||
1479 | && TREE_CODE (slen)((enum tree_code) (slen)->base.code) == INTEGER_CST | |||
1480 | && tree_int_cst_lt (slen, range[0])); | |||
1481 | /* If none is determined try to get a better answer based on the details | |||
1482 | in PAD. */ | |||
1483 | if (!overread | |||
1484 | && pad | |||
1485 | && pad->src.sizrng[1] >= 0 | |||
1486 | && pad->src.offrng[0] >= 0 | |||
1487 | && (pad->src.offrng[1] < 0 | |||
1488 | || pad->src.offrng[0] <= pad->src.offrng[1])) | |||
1489 | { | |||
1490 | /* Set RANGE to that of MAXREAD, bounded by PAD->SRC_BNDRNG if | |||
1491 | PAD is nonnull and BNDRNG is valid. */ | |||
1492 | get_size_range (rvals, maxread, stmt, range, pad ? pad->src_bndrng : NULLnullptr); | |||
1493 | /* Set OVERREAD for reads starting just past the end of an object. */ | |||
1494 | overread = pad->src.sizrng[1] - pad->src.offrng[0] < pad->src_bndrng[0]; | |||
1495 | range[0] = wide_int_to_tree (sizetypesizetype_tab[(int) stk_sizetype], pad->src_bndrng[0]); | |||
1496 | slen = size_zero_nodeglobal_trees[TI_SIZE_ZERO]; | |||
1497 | } | |||
1498 | ||||
1499 | if (overread) | |||
1500 | { | |||
1501 | const opt_code opt = OPT_Wstringop_overread; | |||
1502 | if (warning_suppressed_p (exp, opt) | |||
1503 | || (srcstr && warning_suppressed_p (srcstr, opt)) | |||
1504 | || (pad && pad->src.ref | |||
1505 | && warning_suppressed_p (pad->src.ref, opt))) | |||
1506 | return false; | |||
1507 | ||||
1508 | location_t loc = get_location (exp); | |||
1509 | const bool read | |||
1510 | = mode == access_read_only || mode == access_read_write; | |||
1511 | const bool maybe = pad && pad->dst.parmarray; | |||
1512 | auto_diagnostic_group d; | |||
1513 | if (warn_for_access (loc, func, exp, opt, range, slen, false, read, | |||
1514 | maybe)) | |||
1515 | { | |||
1516 | suppress_warning (exp, opt); | |||
1517 | if (pad) | |||
1518 | pad->src.inform_access (access_read_only); | |||
1519 | } | |||
1520 | return false; | |||
1521 | } | |||
1522 | ||||
1523 | return true; | |||
1524 | } | |||
1525 | ||||
1526 | static bool | |||
1527 | check_access (gimple *stmt, tree dstwrite, | |||
1528 | tree maxread, tree srcstr, tree dstsize, | |||
1529 | access_mode mode, const access_data *pad, | |||
1530 | range_query *rvals) | |||
1531 | { | |||
1532 | return check_access<gimple *> (stmt, dstwrite, maxread, srcstr, dstsize, | |||
1533 | mode, pad, rvals); | |||
1534 | } | |||
1535 | ||||
1536 | bool | |||
1537 | check_access (tree expr, tree dstwrite, | |||
1538 | tree maxread, tree srcstr, tree dstsize, | |||
1539 | access_mode mode, const access_data *pad /* = NULL */) | |||
1540 | { | |||
1541 | return check_access<tree> (expr, dstwrite, maxread, srcstr, dstsize, | |||
1542 | mode, pad, nullptr); | |||
1543 | } | |||
1544 | ||||
1545 | /* Return true if STMT is a call to an allocation function. Unless | |||
1546 | ALL_ALLOC is set, consider only functions that return dynamically | |||
1547 | allocated objects. Otherwise return true even for all forms of | |||
1548 | alloca (including VLA). */ | |||
1549 | ||||
1550 | static bool | |||
1551 | fndecl_alloc_p (tree fndecl, bool all_alloc) | |||
1552 | { | |||
1553 | if (!fndecl) | |||
1554 | return false; | |||
1555 | ||||
1556 | /* A call to operator new isn't recognized as one to a built-in. */ | |||
1557 | if (DECL_IS_OPERATOR_NEW_P (fndecl)(((tree_check ((fndecl), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1557, __FUNCTION__, (FUNCTION_DECL)))->function_decl.decl_type ) == OPERATOR_NEW)) | |||
1558 | return true; | |||
1559 | ||||
1560 | if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) | |||
1561 | { | |||
1562 | switch (DECL_FUNCTION_CODE (fndecl)) | |||
1563 | { | |||
1564 | case BUILT_IN_ALLOCA: | |||
1565 | case BUILT_IN_ALLOCA_WITH_ALIGN: | |||
1566 | return all_alloc; | |||
1567 | case BUILT_IN_ALIGNED_ALLOC: | |||
1568 | case BUILT_IN_CALLOC: | |||
1569 | case BUILT_IN_GOMP_ALLOC: | |||
1570 | case BUILT_IN_MALLOC: | |||
1571 | case BUILT_IN_REALLOC: | |||
1572 | case BUILT_IN_STRDUP: | |||
1573 | case BUILT_IN_STRNDUP: | |||
1574 | return true; | |||
1575 | default: | |||
1576 | break; | |||
1577 | } | |||
1578 | } | |||
1579 | ||||
1580 | /* A function is considered an allocation function if it's declared | |||
1581 | with attribute malloc with an argument naming its associated | |||
1582 | deallocation function. */ | |||
1583 | tree attrs = DECL_ATTRIBUTES (fndecl)((contains_struct_check ((fndecl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1583, __FUNCTION__))->decl_common.attributes); | |||
1584 | if (!attrs) | |||
1585 | return false; | |||
1586 | ||||
1587 | for (tree allocs = attrs; | |||
1588 | (allocs = lookup_attribute ("malloc", allocs)); | |||
1589 | allocs = TREE_CHAIN (allocs)((contains_struct_check ((allocs), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1589, __FUNCTION__))->common.chain)) | |||
1590 | { | |||
1591 | tree args = TREE_VALUE (allocs)((tree_check ((allocs), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1591, __FUNCTION__, (TREE_LIST)))->list.value); | |||
1592 | if (!args) | |||
1593 | continue; | |||
1594 | ||||
1595 | if (TREE_VALUE (args)((tree_check ((args), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1595, __FUNCTION__, (TREE_LIST)))->list.value)) | |||
1596 | return true; | |||
1597 | } | |||
1598 | ||||
1599 | return false; | |||
1600 | } | |||
1601 | ||||
1602 | /* Return true if STMT is a call to an allocation function. A wrapper | |||
1603 | around fndecl_alloc_p. */ | |||
1604 | ||||
1605 | static bool | |||
1606 | gimple_call_alloc_p (gimple *stmt, bool all_alloc = false) | |||
1607 | { | |||
1608 | return fndecl_alloc_p (gimple_call_fndecl (stmt), all_alloc); | |||
1609 | } | |||
1610 | ||||
1611 | /* Return true if DELC doesn't refer to an operator delete that's | |||
1612 | suitable to call with a pointer returned from the operator new | |||
1613 | described by NEWC. */ | |||
1614 | ||||
1615 | static bool | |||
1616 | new_delete_mismatch_p (const demangle_component &newc, | |||
1617 | const demangle_component &delc) | |||
1618 | { | |||
1619 | if (newc.type != delc.type) | |||
1620 | return true; | |||
1621 | ||||
1622 | switch (newc.type) | |||
1623 | { | |||
1624 | case DEMANGLE_COMPONENT_NAME: | |||
1625 | { | |||
1626 | int len = newc.u.s_name.len; | |||
1627 | const char *news = newc.u.s_name.s; | |||
1628 | const char *dels = delc.u.s_name.s; | |||
1629 | if (len != delc.u.s_name.len || memcmp (news, dels, len)) | |||
1630 | return true; | |||
1631 | ||||
1632 | if (news[len] == 'n') | |||
1633 | { | |||
1634 | if (news[len + 1] == 'a') | |||
1635 | return dels[len] != 'd' || dels[len + 1] != 'a'; | |||
1636 | if (news[len + 1] == 'w') | |||
1637 | return dels[len] != 'd' || dels[len + 1] != 'l'; | |||
1638 | } | |||
1639 | return false; | |||
1640 | } | |||
1641 | ||||
1642 | case DEMANGLE_COMPONENT_OPERATOR: | |||
1643 | /* Operator mismatches are handled above. */ | |||
1644 | return false; | |||
1645 | ||||
1646 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: | |||
1647 | if (newc.u.s_extended_operator.args != delc.u.s_extended_operator.args) | |||
1648 | return true; | |||
1649 | return new_delete_mismatch_p (*newc.u.s_extended_operator.name, | |||
1650 | *delc.u.s_extended_operator.name); | |||
1651 | ||||
1652 | case DEMANGLE_COMPONENT_FIXED_TYPE: | |||
1653 | if (newc.u.s_fixed.accum != delc.u.s_fixed.accum | |||
1654 | || newc.u.s_fixed.sat != delc.u.s_fixed.sat) | |||
1655 | return true; | |||
1656 | return new_delete_mismatch_p (*newc.u.s_fixed.length, | |||
1657 | *delc.u.s_fixed.length); | |||
1658 | ||||
1659 | case DEMANGLE_COMPONENT_CTOR: | |||
1660 | if (newc.u.s_ctor.kind != delc.u.s_ctor.kind) | |||
1661 | return true; | |||
1662 | return new_delete_mismatch_p (*newc.u.s_ctor.name, | |||
1663 | *delc.u.s_ctor.name); | |||
1664 | ||||
1665 | case DEMANGLE_COMPONENT_DTOR: | |||
1666 | if (newc.u.s_dtor.kind != delc.u.s_dtor.kind) | |||
1667 | return true; | |||
1668 | return new_delete_mismatch_p (*newc.u.s_dtor.name, | |||
1669 | *delc.u.s_dtor.name); | |||
1670 | ||||
1671 | case DEMANGLE_COMPONENT_BUILTIN_TYPE: | |||
1672 | { | |||
1673 | /* The demangler API provides no better way to compare built-in | |||
1674 | types except to by comparing their demangled names. */ | |||
1675 | size_t nsz, dsz; | |||
1676 | demangle_component *pnc = const_cast<demangle_component *>(&newc); | |||
1677 | demangle_component *pdc = const_cast<demangle_component *>(&delc); | |||
1678 | char *nts = cplus_demangle_print (0, pnc, 16, &nsz); | |||
1679 | char *dts = cplus_demangle_print (0, pdc, 16, &dsz); | |||
1680 | if (!nts != !dts) | |||
1681 | return true; | |||
1682 | bool mismatch = strcmp (nts, dts); | |||
| ||||
1683 | free (nts); | |||
1684 | free (dts); | |||
1685 | return mismatch; | |||
1686 | } | |||
1687 | ||||
1688 | case DEMANGLE_COMPONENT_SUB_STD: | |||
1689 | if (newc.u.s_string.len != delc.u.s_string.len) | |||
1690 | return true; | |||
1691 | return memcmp (newc.u.s_string.string, delc.u.s_string.string, | |||
1692 | newc.u.s_string.len); | |||
1693 | ||||
1694 | case DEMANGLE_COMPONENT_FUNCTION_PARAM: | |||
1695 | case DEMANGLE_COMPONENT_TEMPLATE_PARAM: | |||
1696 | return newc.u.s_number.number != delc.u.s_number.number; | |||
1697 | ||||
1698 | case DEMANGLE_COMPONENT_CHARACTER: | |||
1699 | return newc.u.s_character.character != delc.u.s_character.character; | |||
1700 | ||||
1701 | case DEMANGLE_COMPONENT_DEFAULT_ARG: | |||
1702 | case DEMANGLE_COMPONENT_LAMBDA: | |||
1703 | if (newc.u.s_unary_num.num != delc.u.s_unary_num.num) | |||
1704 | return true; | |||
1705 | return new_delete_mismatch_p (*newc.u.s_unary_num.sub, | |||
1706 | *delc.u.s_unary_num.sub); | |||
1707 | default: | |||
1708 | break; | |||
1709 | } | |||
1710 | ||||
1711 | if (!newc.u.s_binary.left != !delc.u.s_binary.left) | |||
1712 | return true; | |||
1713 | ||||
1714 | if (!newc.u.s_binary.left) | |||
1715 | return false; | |||
1716 | ||||
1717 | if (new_delete_mismatch_p (*newc.u.s_binary.left, *delc.u.s_binary.left) | |||
1718 | || !newc.u.s_binary.right != !delc.u.s_binary.right) | |||
1719 | return true; | |||
1720 | ||||
1721 | if (newc.u.s_binary.right) | |||
1722 | return new_delete_mismatch_p (*newc.u.s_binary.right, | |||
1723 | *delc.u.s_binary.right); | |||
1724 | return false; | |||
1725 | } | |||
1726 | ||||
1727 | /* Return true if DELETE_DECL is an operator delete that's not suitable | |||
1728 | to call with a pointer returned from NEW_DECL. */ | |||
1729 | ||||
1730 | static bool | |||
1731 | new_delete_mismatch_p (tree new_decl, tree delete_decl) | |||
1732 | { | |||
1733 | tree new_name = DECL_ASSEMBLER_NAME (new_decl)decl_assembler_name (new_decl); | |||
1734 | tree delete_name = DECL_ASSEMBLER_NAME (delete_decl)decl_assembler_name (delete_decl); | |||
1735 | ||||
1736 | /* valid_new_delete_pair_p() returns a conservative result (currently | |||
1737 | it only handles global operators). A true result is reliable but | |||
1738 | a false result doesn't necessarily mean the operators don't match | |||
1739 | unless CERTAIN is set. */ | |||
1740 | bool certain; | |||
1741 | if (valid_new_delete_pair_p (new_name, delete_name, &certain)) | |||
1742 | return false; | |||
1743 | /* CERTAIN is set when the negative result is certain. */ | |||
1744 | if (certain) | |||
1745 | return true; | |||
1746 | ||||
1747 | /* For anything not handled by valid_new_delete_pair_p() such as member | |||
1748 | operators compare the individual demangled components of the mangled | |||
1749 | name. */ | |||
1750 | const char *new_str = IDENTIFIER_POINTER (new_name)((const char *) (tree_check ((new_name), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1750, __FUNCTION__, (IDENTIFIER_NODE)))->identifier.id.str ); | |||
1751 | const char *del_str = IDENTIFIER_POINTER (delete_name)((const char *) (tree_check ((delete_name), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1751, __FUNCTION__, (IDENTIFIER_NODE)))->identifier.id.str ); | |||
1752 | ||||
1753 | void *np = NULLnullptr, *dp = NULLnullptr; | |||
1754 | demangle_component *ndc = cplus_demangle_v3_components (new_str, 0, &np); | |||
1755 | demangle_component *ddc = cplus_demangle_v3_components (del_str, 0, &dp); | |||
1756 | bool mismatch = new_delete_mismatch_p (*ndc, *ddc); | |||
1757 | free (np); | |||
1758 | free (dp); | |||
1759 | return mismatch; | |||
1760 | } | |||
1761 | ||||
1762 | /* ALLOC_DECL and DEALLOC_DECL are pair of allocation and deallocation | |||
1763 | functions. Return true if the latter is suitable to deallocate objects | |||
1764 | allocated by calls to the former. */ | |||
1765 | ||||
1766 | static bool | |||
1767 | matching_alloc_calls_p (tree alloc_decl, tree dealloc_decl) | |||
1768 | { | |||
1769 | /* Set to alloc_kind_t::builtin if ALLOC_DECL is associated with | |||
1770 | a built-in deallocator. */ | |||
1771 | enum class alloc_kind_t { none, builtin, user } | |||
1772 | alloc_dealloc_kind = alloc_kind_t::none; | |||
1773 | ||||
1774 | if (DECL_IS_OPERATOR_NEW_P (alloc_decl)(((tree_check ((alloc_decl), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1774, __FUNCTION__, (FUNCTION_DECL)))->function_decl.decl_type ) == OPERATOR_NEW)) | |||
| ||||
1775 | { | |||
1776 | if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl)(((tree_check ((dealloc_decl), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1776, __FUNCTION__, (FUNCTION_DECL)))->function_decl.decl_type ) == OPERATOR_DELETE)) | |||
1777 | /* Return true iff both functions are of the same array or | |||
1778 | singleton form and false otherwise. */ | |||
1779 | return !new_delete_mismatch_p (alloc_decl, dealloc_decl); | |||
1780 | ||||
1781 | /* Return false for deallocation functions that are known not | |||
1782 | to match. */ | |||
1783 | if (fndecl_built_in_p (dealloc_decl, BUILT_IN_FREE) | |||
1784 | || fndecl_built_in_p (dealloc_decl, BUILT_IN_REALLOC)) | |||
1785 | return false; | |||
1786 | /* Otherwise proceed below to check the deallocation function's | |||
1787 | "*dealloc" attributes to look for one that mentions this operator | |||
1788 | new. */ | |||
1789 | } | |||
1790 | else if (fndecl_built_in_p (alloc_decl, BUILT_IN_NORMAL)) | |||
1791 | { | |||
1792 | switch (DECL_FUNCTION_CODE (alloc_decl)) | |||
1793 | { | |||
1794 | case BUILT_IN_ALLOCA: | |||
1795 | case BUILT_IN_ALLOCA_WITH_ALIGN: | |||
1796 | return false; | |||
1797 | ||||
1798 | case BUILT_IN_ALIGNED_ALLOC: | |||
1799 | case BUILT_IN_CALLOC: | |||
1800 | case BUILT_IN_GOMP_ALLOC: | |||
1801 | case BUILT_IN_MALLOC: | |||
1802 | case BUILT_IN_REALLOC: | |||
1803 | case BUILT_IN_STRDUP: | |||
1804 | case BUILT_IN_STRNDUP: | |||
1805 | if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl)(((tree_check ((dealloc_decl), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1805, __FUNCTION__, (FUNCTION_DECL)))->function_decl.decl_type ) == OPERATOR_DELETE)) | |||
1806 | return false; | |||
1807 | ||||
1808 | if (fndecl_built_in_p (dealloc_decl, BUILT_IN_FREE) | |||
1809 | || fndecl_built_in_p (dealloc_decl, BUILT_IN_REALLOC)) | |||
1810 | return true; | |||
1811 | ||||
1812 | alloc_dealloc_kind = alloc_kind_t::builtin; | |||
1813 | break; | |||
1814 | ||||
1815 | default: | |||
1816 | break; | |||
1817 | } | |||
1818 | } | |||
1819 | ||||
1820 | /* Set if DEALLOC_DECL both allocates and deallocates. */ | |||
1821 | alloc_kind_t realloc_kind = alloc_kind_t::none; | |||
1822 | ||||
1823 | if (fndecl_built_in_p (dealloc_decl, BUILT_IN_NORMAL)) | |||
1824 | { | |||
1825 | built_in_function dealloc_code = DECL_FUNCTION_CODE (dealloc_decl); | |||
1826 | if (dealloc_code == BUILT_IN_REALLOC) | |||
1827 | realloc_kind = alloc_kind_t::builtin; | |||
1828 | ||||
1829 | for (tree amats = DECL_ATTRIBUTES (alloc_decl)((contains_struct_check ((alloc_decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1829, __FUNCTION__))->decl_common.attributes); | |||
1830 | (amats = lookup_attribute ("malloc", amats)); | |||
1831 | amats = TREE_CHAIN (amats)((contains_struct_check ((amats), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1831, __FUNCTION__))->common.chain)) | |||
1832 | { | |||
1833 | tree args = TREE_VALUE (amats)((tree_check ((amats), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1833, __FUNCTION__, (TREE_LIST)))->list.value); | |||
1834 | if (!args) | |||
1835 | continue; | |||
1836 | ||||
1837 | tree fndecl = TREE_VALUE (args)((tree_check ((args), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1837, __FUNCTION__, (TREE_LIST)))->list.value); | |||
1838 | if (!fndecl || !DECL_P (fndecl)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (fndecl)->base.code))] == tcc_declaration)) | |||
1839 | continue; | |||
1840 | ||||
1841 | if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL) | |||
1842 | && dealloc_code == DECL_FUNCTION_CODE (fndecl)) | |||
1843 | return true; | |||
1844 | } | |||
1845 | } | |||
1846 | ||||
1847 | const bool alloc_builtin = fndecl_built_in_p (alloc_decl, BUILT_IN_NORMAL); | |||
1848 | alloc_kind_t realloc_dealloc_kind = alloc_kind_t::none; | |||
1849 | ||||
1850 | /* If DEALLOC_DECL has an internal "*dealloc" attribute scan the list | |||
1851 | of its associated allocation functions for ALLOC_DECL. | |||
1852 | If the corresponding ALLOC_DECL is found they're a matching pair, | |||
1853 | otherwise they're not. | |||
1854 | With DDATS set to the Deallocator's *Dealloc ATtributes... */ | |||
1855 | for (tree ddats = DECL_ATTRIBUTES (dealloc_decl)((contains_struct_check ((dealloc_decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1855, __FUNCTION__))->decl_common.attributes); | |||
1856 | (ddats = lookup_attribute ("*dealloc", ddats)); | |||
1857 | ddats = TREE_CHAIN (ddats)((contains_struct_check ((ddats), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1857, __FUNCTION__))->common.chain)) | |||
1858 | { | |||
1859 | tree args = TREE_VALUE (ddats)((tree_check ((ddats), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1859, __FUNCTION__, (TREE_LIST)))->list.value); | |||
1860 | if (!args) | |||
1861 | continue; | |||
1862 | ||||
1863 | tree alloc = TREE_VALUE (args)((tree_check ((args), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1863, __FUNCTION__, (TREE_LIST)))->list.value); | |||
1864 | if (!alloc) | |||
1865 | continue; | |||
1866 | ||||
1867 | if (alloc == DECL_NAME (dealloc_decl)((contains_struct_check ((dealloc_decl), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1867, __FUNCTION__))->decl_minimal.name)) | |||
1868 | realloc_kind = alloc_kind_t::user; | |||
1869 | ||||
1870 | if (DECL_P (alloc)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (alloc)->base.code))] == tcc_declaration)) | |||
1871 | { | |||
1872 | gcc_checking_assert (fndecl_built_in_p (alloc, BUILT_IN_NORMAL))((void)(!(fndecl_built_in_p (alloc, BUILT_IN_NORMAL)) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1872, __FUNCTION__), 0 : 0)); | |||
1873 | ||||
1874 | switch (DECL_FUNCTION_CODE (alloc)) | |||
1875 | { | |||
1876 | case BUILT_IN_ALIGNED_ALLOC: | |||
1877 | case BUILT_IN_CALLOC: | |||
1878 | case BUILT_IN_GOMP_ALLOC: | |||
1879 | case BUILT_IN_MALLOC: | |||
1880 | case BUILT_IN_REALLOC: | |||
1881 | case BUILT_IN_STRDUP: | |||
1882 | case BUILT_IN_STRNDUP: | |||
1883 | realloc_dealloc_kind = alloc_kind_t::builtin; | |||
1884 | break; | |||
1885 | default: | |||
1886 | break; | |||
1887 | } | |||
1888 | ||||
1889 | if (!alloc_builtin) | |||
1890 | continue; | |||
1891 | ||||
1892 | if (DECL_FUNCTION_CODE (alloc) != DECL_FUNCTION_CODE (alloc_decl)) | |||
1893 | continue; | |||
1894 | ||||
1895 | return true; | |||
1896 | } | |||
1897 | ||||
1898 | if (alloc == DECL_NAME (alloc_decl)((contains_struct_check ((alloc_decl), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1898, __FUNCTION__))->decl_minimal.name)) | |||
1899 | return true; | |||
1900 | } | |||
1901 | ||||
1902 | if (realloc_kind == alloc_kind_t::none) | |||
1903 | return false; | |||
1904 | ||||
1905 | hash_set<tree> common_deallocs; | |||
1906 | /* Special handling for deallocators. Iterate over both the allocator's | |||
1907 | and the reallocator's associated deallocator functions looking for | |||
1908 | the first one in common. If one is found, the de/reallocator is | |||
1909 | a match for the allocator even though the latter isn't directly | |||
1910 | associated with the former. This simplifies declarations in system | |||
1911 | headers. | |||
1912 | With AMATS set to the Allocator's Malloc ATtributes, | |||
1913 | and RMATS set to Reallocator's Malloc ATtributes... */ | |||
1914 | for (tree amats = DECL_ATTRIBUTES (alloc_decl)((contains_struct_check ((alloc_decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1914, __FUNCTION__))->decl_common.attributes), | |||
1915 | rmats = DECL_ATTRIBUTES (dealloc_decl)((contains_struct_check ((dealloc_decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1915, __FUNCTION__))->decl_common.attributes); | |||
1916 | (amats = lookup_attribute ("malloc", amats)) | |||
1917 | || (rmats = lookup_attribute ("malloc", rmats)); | |||
1918 | amats = amats ? TREE_CHAIN (amats)((contains_struct_check ((amats), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1918, __FUNCTION__))->common.chain) : NULL_TREE(tree) nullptr, | |||
1919 | rmats = rmats ? TREE_CHAIN (rmats)((contains_struct_check ((rmats), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1919, __FUNCTION__))->common.chain) : NULL_TREE(tree) nullptr) | |||
1920 | { | |||
1921 | if (tree args = amats ? TREE_VALUE (amats)((tree_check ((amats), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1921, __FUNCTION__, (TREE_LIST)))->list.value) : NULL_TREE(tree) nullptr) | |||
1922 | if (tree adealloc = TREE_VALUE (args)((tree_check ((args), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1922, __FUNCTION__, (TREE_LIST)))->list.value)) | |||
1923 | { | |||
1924 | if (DECL_P (adealloc)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (adealloc)->base.code))] == tcc_declaration) | |||
1925 | && fndecl_built_in_p (adealloc, BUILT_IN_NORMAL)) | |||
1926 | { | |||
1927 | built_in_function fncode = DECL_FUNCTION_CODE (adealloc); | |||
1928 | if (fncode == BUILT_IN_FREE || fncode == BUILT_IN_REALLOC) | |||
1929 | { | |||
1930 | if (realloc_kind == alloc_kind_t::builtin) | |||
1931 | return true; | |||
1932 | alloc_dealloc_kind = alloc_kind_t::builtin; | |||
1933 | } | |||
1934 | continue; | |||
1935 | } | |||
1936 | ||||
1937 | common_deallocs.add (adealloc); | |||
1938 | } | |||
1939 | ||||
1940 | if (tree args = rmats ? TREE_VALUE (rmats)((tree_check ((rmats), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1940, __FUNCTION__, (TREE_LIST)))->list.value) : NULL_TREE(tree) nullptr) | |||
1941 | if (tree ddealloc = TREE_VALUE (args)((tree_check ((args), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1941, __FUNCTION__, (TREE_LIST)))->list.value)) | |||
1942 | { | |||
1943 | if (DECL_P (ddealloc)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (ddealloc)->base.code))] == tcc_declaration) | |||
1944 | && fndecl_built_in_p (ddealloc, BUILT_IN_NORMAL)) | |||
1945 | { | |||
1946 | built_in_function fncode = DECL_FUNCTION_CODE (ddealloc); | |||
1947 | if (fncode == BUILT_IN_FREE || fncode == BUILT_IN_REALLOC) | |||
1948 | { | |||
1949 | if (alloc_dealloc_kind == alloc_kind_t::builtin) | |||
1950 | return true; | |||
1951 | realloc_dealloc_kind = alloc_kind_t::builtin; | |||
1952 | } | |||
1953 | continue; | |||
1954 | } | |||
1955 | ||||
1956 | if (common_deallocs.add (ddealloc)) | |||
1957 | return true; | |||
1958 | } | |||
1959 | } | |||
1960 | ||||
1961 | /* Succeed only if ALLOC_DECL and the reallocator DEALLOC_DECL share | |||
1962 | a built-in deallocator. */ | |||
1963 | return (alloc_dealloc_kind == alloc_kind_t::builtin | |||
1964 | && realloc_dealloc_kind == alloc_kind_t::builtin); | |||
1965 | } | |||
1966 | ||||
1967 | /* Return true if DEALLOC_DECL is a function suitable to deallocate | |||
1968 | objects allocated by the ALLOC call. */ | |||
1969 | ||||
1970 | static bool | |||
1971 | matching_alloc_calls_p (gimple *alloc, tree dealloc_decl) | |||
1972 | { | |||
1973 | tree alloc_decl = gimple_call_fndecl (alloc); | |||
1974 | if (!alloc_decl) | |||
1975 | return true; | |||
1976 | ||||
1977 | return matching_alloc_calls_p (alloc_decl, dealloc_decl); | |||
1978 | } | |||
1979 | ||||
1980 | /* Diagnose a call EXP to deallocate a pointer referenced by AREF if it | |||
1981 | includes a nonzero offset. Such a pointer cannot refer to the beginning | |||
1982 | of an allocated object. A negative offset may refer to it only if | |||
1983 | the target pointer is unknown. */ | |||
1984 | ||||
1985 | static bool | |||
1986 | warn_dealloc_offset (location_t loc, gimple *call, const access_ref &aref) | |||
1987 | { | |||
1988 | if (aref.deref || aref.offrng[0] <= 0 || aref.offrng[1] <= 0) | |||
1989 | return false; | |||
1990 | ||||
1991 | tree dealloc_decl = gimple_call_fndecl (call); | |||
1992 | if (!dealloc_decl) | |||
1993 | return false; | |||
1994 | ||||
1995 | if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl)(((tree_check ((dealloc_decl), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1995, __FUNCTION__, (FUNCTION_DECL)))->function_decl.decl_type ) == OPERATOR_DELETE) | |||
1996 | && !DECL_IS_REPLACEABLE_OPERATOR (dealloc_decl)((tree_check ((dealloc_decl), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 1996, __FUNCTION__, (FUNCTION_DECL)))->function_decl.replaceable_operator )) | |||
1997 | { | |||
1998 | /* A call to a user-defined operator delete with a pointer plus offset | |||
1999 | may be valid if it's returned from an unknown function (i.e., one | |||
2000 | that's not operator new). */ | |||
2001 | if (TREE_CODE (aref.ref)((enum tree_code) (aref.ref)->base.code) == SSA_NAME) | |||
2002 | { | |||
2003 | gimple *def_stmt = SSA_NAME_DEF_STMT (aref.ref)(tree_check ((aref.ref), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2003, __FUNCTION__, (SSA_NAME)))->ssa_name.def_stmt; | |||
2004 | if (is_gimple_call (def_stmt)) | |||
2005 | { | |||
2006 | tree alloc_decl = gimple_call_fndecl (def_stmt); | |||
2007 | if (!alloc_decl || !DECL_IS_OPERATOR_NEW_P (alloc_decl)(((tree_check ((alloc_decl), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2007, __FUNCTION__, (FUNCTION_DECL)))->function_decl.decl_type ) == OPERATOR_NEW)) | |||
2008 | return false; | |||
2009 | } | |||
2010 | } | |||
2011 | } | |||
2012 | ||||
2013 | char offstr[80]; | |||
2014 | offstr[0] = '\0'; | |||
2015 | if (wi::fits_shwi_p (aref.offrng[0])) | |||
2016 | { | |||
2017 | if (aref.offrng[0] == aref.offrng[1] | |||
2018 | || !wi::fits_shwi_p (aref.offrng[1])) | |||
2019 | sprintf (offstr, " %lli", | |||
2020 | (long long)aref.offrng[0].to_shwi ()); | |||
2021 | else | |||
2022 | sprintf (offstr, " [%lli, %lli]", | |||
2023 | (long long)aref.offrng[0].to_shwi (), | |||
2024 | (long long)aref.offrng[1].to_shwi ()); | |||
2025 | } | |||
2026 | ||||
2027 | auto_diagnostic_group d; | |||
2028 | if (!warning_at (loc, OPT_Wfree_nonheap_object, | |||
2029 | "%qD called on pointer %qE with nonzero offset%s", | |||
2030 | dealloc_decl, aref.ref, offstr)) | |||
2031 | return false; | |||
2032 | ||||
2033 | if (DECL_P (aref.ref)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (aref.ref)->base.code))] == tcc_declaration)) | |||
2034 | inform (get_location (aref.ref), "declared here"); | |||
2035 | else if (TREE_CODE (aref.ref)((enum tree_code) (aref.ref)->base.code) == SSA_NAME) | |||
2036 | { | |||
2037 | gimple *def_stmt = SSA_NAME_DEF_STMT (aref.ref)(tree_check ((aref.ref), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2037, __FUNCTION__, (SSA_NAME)))->ssa_name.def_stmt; | |||
2038 | if (is_gimple_call (def_stmt)) | |||
2039 | { | |||
2040 | location_t def_loc = get_location (def_stmt); | |||
2041 | tree alloc_decl = gimple_call_fndecl (def_stmt); | |||
2042 | if (alloc_decl) | |||
2043 | inform (def_loc, | |||
2044 | "returned from %qD", alloc_decl); | |||
2045 | else if (tree alloc_fntype = gimple_call_fntype (def_stmt)) | |||
2046 | inform (def_loc, | |||
2047 | "returned from %qT", alloc_fntype); | |||
2048 | else | |||
2049 | inform (def_loc, "obtained here"); | |||
2050 | } | |||
2051 | } | |||
2052 | ||||
2053 | return true; | |||
2054 | } | |||
2055 | ||||
2056 | namespace { | |||
2057 | ||||
2058 | const pass_data pass_data_waccess = { | |||
2059 | GIMPLE_PASS, | |||
2060 | "waccess", | |||
2061 | OPTGROUP_NONE, | |||
2062 | TV_WARN_ACCESS, /* timer variable */ | |||
2063 | PROP_cfg(1 << 3), /* properties_required */ | |||
2064 | 0, /* properties_provided */ | |||
2065 | 0, /* properties_destroyed */ | |||
2066 | 0, /* properties_start */ | |||
2067 | 0, /* properties_finish */ | |||
2068 | }; | |||
2069 | ||||
2070 | /* Pass to detect invalid accesses. */ | |||
2071 | class pass_waccess : public gimple_opt_pass | |||
2072 | { | |||
2073 | public: | |||
2074 | pass_waccess (gcc::context *); | |||
2075 | ||||
2076 | ~pass_waccess (); | |||
2077 | ||||
2078 | opt_pass *clone () final override; | |||
2079 | ||||
2080 | bool gate (function *) final override; | |||
2081 | ||||
2082 | void set_pass_param (unsigned, bool) final override; | |||
2083 | ||||
2084 | unsigned int execute (function *) final override; | |||
2085 | ||||
2086 | private: | |||
2087 | /* Not copyable or assignable. */ | |||
2088 | pass_waccess (pass_waccess &) = delete; | |||
2089 | void operator= (pass_waccess &) = delete; | |||
2090 | ||||
2091 | /* Check a call to an atomic built-in function. */ | |||
2092 | bool check_atomic_builtin (gcall *); | |||
2093 | ||||
2094 | /* Check a call to a built-in function. */ | |||
2095 | bool check_builtin (gcall *); | |||
2096 | ||||
2097 | /* Check a call to an ordinary function for invalid accesses. */ | |||
2098 | bool check_call_access (gcall *); | |||
2099 | ||||
2100 | /* Check a non-call statement. */ | |||
2101 | void check_stmt (gimple *); | |||
2102 | ||||
2103 | /* Check statements in a basic block. */ | |||
2104 | void check_block (basic_block); | |||
2105 | ||||
2106 | /* Check a call to a function. */ | |||
2107 | void check_call (gcall *); | |||
2108 | ||||
2109 | /* Check a call to the named built-in function. */ | |||
2110 | void check_alloca (gcall *); | |||
2111 | void check_alloc_size_call (gcall *); | |||
2112 | void check_strcat (gcall *); | |||
2113 | void check_strncat (gcall *); | |||
2114 | void check_stxcpy (gcall *); | |||
2115 | void check_stxncpy (gcall *); | |||
2116 | void check_strncmp (gcall *); | |||
2117 | void check_memop_access (gimple *, tree, tree, tree); | |||
2118 | void check_read_access (gimple *, tree, tree = NULL_TREE(tree) nullptr, int = 1); | |||
2119 | ||||
2120 | void maybe_check_dealloc_call (gcall *); | |||
2121 | void maybe_check_access_sizes (rdwr_map *, tree, tree, gimple *); | |||
2122 | bool maybe_warn_memmodel (gimple *, tree, tree, const unsigned char *); | |||
2123 | void check_atomic_memmodel (gimple *, tree, tree, const unsigned char *); | |||
2124 | ||||
2125 | /* Check for uses of indeterminate pointers. */ | |||
2126 | void check_pointer_uses (gimple *, tree, tree = NULL_TREE(tree) nullptr, bool = false); | |||
2127 | ||||
2128 | /* Return the argument that a call returns. */ | |||
2129 | tree gimple_call_return_arg (gcall *); | |||
2130 | ||||
2131 | /* Check a call for uses of a dangling pointer arguments. */ | |||
2132 | void check_call_dangling (gcall *); | |||
2133 | ||||
2134 | /* Check uses of a dangling pointer or those derived from it. */ | |||
2135 | void check_dangling_uses (tree, tree, bool = false, bool = false); | |||
2136 | void check_dangling_uses (); | |||
2137 | void check_dangling_stores (); | |||
2138 | void check_dangling_stores (basic_block, hash_set<tree> &, auto_bitmap &); | |||
2139 | ||||
2140 | void warn_invalid_pointer (tree, gimple *, gimple *, tree, bool, bool = false); | |||
2141 | ||||
2142 | /* Return true if use follows an invalidating statement. */ | |||
2143 | bool use_after_inval_p (gimple *, gimple *, bool = false); | |||
2144 | ||||
2145 | /* A pointer_query object to store information about pointers and | |||
2146 | their targets in. */ | |||
2147 | pointer_query m_ptr_qry; | |||
2148 | /* Mapping from DECLs and their clobber statements in the function. */ | |||
2149 | hash_map<tree, gimple *> m_clobbers; | |||
2150 | /* A bit is set for each basic block whose statements have been assigned | |||
2151 | valid UIDs. */ | |||
2152 | bitmap m_bb_uids_set; | |||
2153 | /* The current function. */ | |||
2154 | function *m_func; | |||
2155 | /* True to run checks for uses of dangling pointers. */ | |||
2156 | bool m_check_dangling_p; | |||
2157 | /* True to run checks early on in the optimization pipeline. */ | |||
2158 | bool m_early_checks_p; | |||
2159 | }; | |||
2160 | ||||
2161 | /* Construct the pass. */ | |||
2162 | ||||
2163 | pass_waccess::pass_waccess (gcc::context *ctxt) | |||
2164 | : gimple_opt_pass (pass_data_waccess, ctxt), | |||
2165 | m_ptr_qry (NULLnullptr), | |||
2166 | m_clobbers (), | |||
2167 | m_bb_uids_set (), | |||
2168 | m_func (), | |||
2169 | m_check_dangling_p (), | |||
2170 | m_early_checks_p () | |||
2171 | { | |||
2172 | } | |||
2173 | ||||
2174 | /* Return a copy of the pass with RUN_NUMBER one greater than THIS. */ | |||
2175 | ||||
2176 | opt_pass* | |||
2177 | pass_waccess::clone () | |||
2178 | { | |||
2179 | return new pass_waccess (m_ctxt); | |||
2180 | } | |||
2181 | ||||
2182 | /* Release pointer_query cache. */ | |||
2183 | ||||
2184 | pass_waccess::~pass_waccess () | |||
2185 | { | |||
2186 | m_ptr_qry.flush_cache (); | |||
2187 | } | |||
2188 | ||||
2189 | void | |||
2190 | pass_waccess::set_pass_param (unsigned int n, bool early) | |||
2191 | { | |||
2192 | gcc_assert (n == 0)((void)(!(n == 0) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2192, __FUNCTION__), 0 : 0)); | |||
2193 | ||||
2194 | m_early_checks_p = early; | |||
2195 | } | |||
2196 | ||||
2197 | /* Return true when any checks performed by the pass are enabled. */ | |||
2198 | ||||
2199 | bool | |||
2200 | pass_waccess::gate (function *) | |||
2201 | { | |||
2202 | return (warn_free_nonheap_objectglobal_options.x_warn_free_nonheap_object | |||
2203 | || warn_mismatched_allocglobal_options.x_warn_mismatched_alloc | |||
2204 | || warn_mismatched_new_deleteglobal_options.x_warn_mismatched_new_delete); | |||
2205 | } | |||
2206 | ||||
2207 | /* Initialize ALLOC_OBJECT_SIZE_LIMIT based on the -Walloc-size-larger-than= | |||
2208 | setting if the option is specified, or to the maximum object size if it | |||
2209 | is not. Return the initialized value. */ | |||
2210 | ||||
2211 | static tree | |||
2212 | alloc_max_size (void) | |||
2213 | { | |||
2214 | HOST_WIDE_INTlong limit = warn_alloc_size_limitglobal_options.x_warn_alloc_size_limit; | |||
2215 | if (limit == HOST_WIDE_INT_MAX(~((long) (1UL << (64 - 1))))) | |||
2216 | limit = tree_to_shwi (TYPE_MAX_VALUE (ptrdiff_type_node)((tree_check5 ((global_trees[TI_PTRDIFF_TYPE]), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2216, __FUNCTION__, (INTEGER_TYPE), (ENUMERAL_TYPE), (BOOLEAN_TYPE ), (REAL_TYPE), (FIXED_POINT_TYPE)))->type_non_common.maxval )); | |||
2217 | ||||
2218 | return build_int_cst (size_type_nodeglobal_trees[TI_SIZE_TYPE], limit); | |||
2219 | } | |||
2220 | ||||
2221 | /* Diagnose a call EXP to function FN decorated with attribute alloc_size | |||
2222 | whose argument numbers given by IDX with values given by ARGS exceed | |||
2223 | the maximum object size or cause an unsigned overflow (wrapping) when | |||
2224 | multiplied. FN is null when EXP is a call via a function pointer. | |||
2225 | When ARGS[0] is null the function does nothing. ARGS[1] may be null | |||
2226 | for functions like malloc, and non-null for those like calloc that | |||
2227 | are decorated with a two-argument attribute alloc_size. */ | |||
2228 | ||||
2229 | void | |||
2230 | maybe_warn_alloc_args_overflow (gimple *stmt, const tree args[2], | |||
2231 | const int idx[2]) | |||
2232 | { | |||
2233 | /* The range each of the (up to) two arguments is known to be in. */ | |||
2234 | tree argrange[2][2] = { { NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr }, { NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr } }; | |||
2235 | ||||
2236 | /* Maximum object size set by -Walloc-size-larger-than= or SIZE_MAX / 2. */ | |||
2237 | tree maxobjsize = alloc_max_size (); | |||
2238 | ||||
2239 | location_t loc = get_location (stmt); | |||
2240 | ||||
2241 | tree fn = gimple_call_fndecl (stmt); | |||
2242 | tree fntype = fn ? TREE_TYPE (fn)((contains_struct_check ((fn), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2242, __FUNCTION__))->typed.type) : gimple_call_fntype (stmt); | |||
2243 | bool warned = false; | |||
2244 | ||||
2245 | /* Validate each argument individually. */ | |||
2246 | for (unsigned i = 0; i != 2 && args[i]; ++i) | |||
2247 | { | |||
2248 | if (TREE_CODE (args[i])((enum tree_code) (args[i])->base.code) == INTEGER_CST) | |||
2249 | { | |||
2250 | argrange[i][0] = args[i]; | |||
2251 | argrange[i][1] = args[i]; | |||
2252 | ||||
2253 | if (tree_int_cst_lt (args[i], integer_zero_nodeglobal_trees[TI_INTEGER_ZERO])) | |||
2254 | { | |||
2255 | warned = warning_at (loc, OPT_Walloc_size_larger_than_, | |||
2256 | "argument %i value %qE is negative", | |||
2257 | idx[i] + 1, args[i]); | |||
2258 | } | |||
2259 | else if (integer_zerop (args[i])) | |||
2260 | { | |||
2261 | /* Avoid issuing -Walloc-zero for allocation functions other | |||
2262 | than __builtin_alloca that are declared with attribute | |||
2263 | returns_nonnull because there's no portability risk. This | |||
2264 | avoids warning for such calls to libiberty's xmalloc and | |||
2265 | friends. | |||
2266 | Also avoid issuing the warning for calls to function named | |||
2267 | "alloca". */ | |||
2268 | if (fn && fndecl_built_in_p (fn, BUILT_IN_ALLOCA) | |||
2269 | ? IDENTIFIER_LENGTH (DECL_NAME (fn))((tree_check ((((contains_struct_check ((fn), (TS_DECL_MINIMAL ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2269, __FUNCTION__))->decl_minimal.name)), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2269, __FUNCTION__, (IDENTIFIER_NODE)))->identifier.id.len ) != 6 | |||
2270 | : !lookup_attribute ("returns_nonnull", | |||
2271 | TYPE_ATTRIBUTES (fntype)((tree_class_check ((fntype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2271, __FUNCTION__))->type_common.attributes))) | |||
2272 | warned = warning_at (loc, OPT_Walloc_zero, | |||
2273 | "argument %i value is zero", | |||
2274 | idx[i] + 1); | |||
2275 | } | |||
2276 | else if (tree_int_cst_lt (maxobjsize, args[i])) | |||
2277 | { | |||
2278 | /* G++ emits calls to ::operator new[](SIZE_MAX) in C++98 | |||
2279 | mode and with -fno-exceptions as a way to indicate array | |||
2280 | size overflow. There's no good way to detect C++98 here | |||
2281 | so avoid diagnosing these calls for all C++ modes. */ | |||
2282 | if (i == 0 | |||
2283 | && fn | |||
2284 | && !args[1] | |||
2285 | && lang_GNU_CXX () | |||
2286 | && DECL_IS_OPERATOR_NEW_P (fn)(((tree_check ((fn), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2286, __FUNCTION__, (FUNCTION_DECL)))->function_decl.decl_type ) == OPERATOR_NEW) | |||
2287 | && integer_all_onesp (args[i])) | |||
2288 | continue; | |||
2289 | ||||
2290 | warned = warning_at (loc, OPT_Walloc_size_larger_than_, | |||
2291 | "argument %i value %qE exceeds " | |||
2292 | "maximum object size %E", | |||
2293 | idx[i] + 1, args[i], maxobjsize); | |||
2294 | } | |||
2295 | } | |||
2296 | else if (TREE_CODE (args[i])((enum tree_code) (args[i])->base.code) == SSA_NAME | |||
2297 | && get_size_range (args[i], argrange[i])) | |||
2298 | { | |||
2299 | /* Verify that the argument's range is not negative (including | |||
2300 | upper bound of zero). */ | |||
2301 | if (tree_int_cst_lt (argrange[i][0], integer_zero_nodeglobal_trees[TI_INTEGER_ZERO]) | |||
2302 | && tree_int_cst_le (argrange[i][1], integer_zero_nodeglobal_trees[TI_INTEGER_ZERO])) | |||
2303 | { | |||
2304 | warned = warning_at (loc, OPT_Walloc_size_larger_than_, | |||
2305 | "argument %i range [%E, %E] is negative", | |||
2306 | idx[i] + 1, | |||
2307 | argrange[i][0], argrange[i][1]); | |||
2308 | } | |||
2309 | else if (tree_int_cst_lt (maxobjsize, argrange[i][0])) | |||
2310 | { | |||
2311 | warned = warning_at (loc, OPT_Walloc_size_larger_than_, | |||
2312 | "argument %i range [%E, %E] exceeds " | |||
2313 | "maximum object size %E", | |||
2314 | idx[i] + 1, | |||
2315 | argrange[i][0], argrange[i][1], | |||
2316 | maxobjsize); | |||
2317 | } | |||
2318 | } | |||
2319 | } | |||
2320 | ||||
2321 | if (!argrange[0][0]) | |||
2322 | return; | |||
2323 | ||||
2324 | /* For a two-argument alloc_size, validate the product of the two | |||
2325 | arguments if both of their values or ranges are known. */ | |||
2326 | if (!warned && tree_fits_uhwi_p (argrange[0][0]) | |||
2327 | && argrange[1][0] && tree_fits_uhwi_p (argrange[1][0]) | |||
2328 | && !integer_onep (argrange[0][0]) | |||
2329 | && !integer_onep (argrange[1][0])) | |||
2330 | { | |||
2331 | /* Check for overflow in the product of a function decorated with | |||
2332 | attribute alloc_size (X, Y). */ | |||
2333 | unsigned szprec = TYPE_PRECISION (size_type_node)((tree_class_check ((global_trees[TI_SIZE_TYPE]), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2333, __FUNCTION__))->type_common.precision); | |||
2334 | wide_int x = wi::to_wide (argrange[0][0], szprec); | |||
2335 | wide_int y = wi::to_wide (argrange[1][0], szprec); | |||
2336 | ||||
2337 | wi::overflow_type vflow; | |||
2338 | wide_int prod = wi::umul (x, y, &vflow); | |||
2339 | ||||
2340 | if (vflow) | |||
2341 | warned = warning_at (loc, OPT_Walloc_size_larger_than_, | |||
2342 | "product %<%E * %E%> of arguments %i and %i " | |||
2343 | "exceeds %<SIZE_MAX%>", | |||
2344 | argrange[0][0], argrange[1][0], | |||
2345 | idx[0] + 1, idx[1] + 1); | |||
2346 | else if (wi::ltu_p (wi::to_wide (maxobjsize, szprec), prod)) | |||
2347 | warned = warning_at (loc, OPT_Walloc_size_larger_than_, | |||
2348 | "product %<%E * %E%> of arguments %i and %i " | |||
2349 | "exceeds maximum object size %E", | |||
2350 | argrange[0][0], argrange[1][0], | |||
2351 | idx[0] + 1, idx[1] + 1, | |||
2352 | maxobjsize); | |||
2353 | ||||
2354 | if (warned) | |||
2355 | { | |||
2356 | /* Print the full range of each of the two arguments to make | |||
2357 | it clear when it is, in fact, in a range and not constant. */ | |||
2358 | if (argrange[0][0] != argrange [0][1]) | |||
2359 | inform (loc, "argument %i in the range [%E, %E]", | |||
2360 | idx[0] + 1, argrange[0][0], argrange[0][1]); | |||
2361 | if (argrange[1][0] != argrange [1][1]) | |||
2362 | inform (loc, "argument %i in the range [%E, %E]", | |||
2363 | idx[1] + 1, argrange[1][0], argrange[1][1]); | |||
2364 | } | |||
2365 | } | |||
2366 | ||||
2367 | if (warned && fn) | |||
2368 | { | |||
2369 | location_t fnloc = DECL_SOURCE_LOCATION (fn)((contains_struct_check ((fn), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2369, __FUNCTION__))->decl_minimal.locus); | |||
2370 | ||||
2371 | if (DECL_IS_UNDECLARED_BUILTIN (fn)(((contains_struct_check ((fn), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2371, __FUNCTION__))->decl_minimal.locus) <= ((location_t ) 1))) | |||
2372 | inform (loc, | |||
2373 | "in a call to built-in allocation function %qD", fn); | |||
2374 | else | |||
2375 | inform (fnloc, | |||
2376 | "in a call to allocation function %qD declared here", fn); | |||
2377 | } | |||
2378 | } | |||
2379 | ||||
2380 | /* Check a call to an alloca function for an excessive size. */ | |||
2381 | ||||
2382 | void | |||
2383 | pass_waccess::check_alloca (gcall *stmt) | |||
2384 | { | |||
2385 | if (m_early_checks_p) | |||
2386 | return; | |||
2387 | ||||
2388 | if ((warn_vla_limitglobal_options.x_warn_vla_limit >= HOST_WIDE_INT_MAX(~((long) (1UL << (64 - 1)))) | |||
2389 | && warn_alloc_size_limitglobal_options.x_warn_alloc_size_limit < warn_vla_limitglobal_options.x_warn_vla_limit) | |||
2390 | || (warn_alloca_limitglobal_options.x_warn_alloca_limit >= HOST_WIDE_INT_MAX(~((long) (1UL << (64 - 1)))) | |||
2391 | && warn_alloc_size_limitglobal_options.x_warn_alloc_size_limit < warn_alloca_limitglobal_options.x_warn_alloca_limit)) | |||
2392 | { | |||
2393 | /* -Walloca-larger-than and -Wvla-larger-than settings of less | |||
2394 | than HWI_MAX override the more general -Walloc-size-larger-than | |||
2395 | so unless either of the former options is smaller than the last | |||
2396 | one (which would imply that the call was already checked), check | |||
2397 | the alloca arguments for overflow. */ | |||
2398 | const tree alloc_args[] = { call_arg (stmt, 0), NULL_TREE(tree) nullptr }; | |||
2399 | const int idx[] = { 0, -1 }; | |||
2400 | maybe_warn_alloc_args_overflow (stmt, alloc_args, idx); | |||
2401 | } | |||
2402 | } | |||
2403 | ||||
2404 | /* Check a call to an allocation function for an excessive size. */ | |||
2405 | ||||
2406 | void | |||
2407 | pass_waccess::check_alloc_size_call (gcall *stmt) | |||
2408 | { | |||
2409 | if (m_early_checks_p) | |||
2410 | return; | |||
2411 | ||||
2412 | if (gimple_call_num_args (stmt) < 1) | |||
2413 | /* Avoid invalid calls to functions without a prototype. */ | |||
2414 | return; | |||
2415 | ||||
2416 | tree fndecl = gimple_call_fndecl (stmt); | |||
2417 | if (fndecl && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)) | |||
2418 | { | |||
2419 | /* Alloca is handled separately. */ | |||
2420 | switch (DECL_FUNCTION_CODE (fndecl)) | |||
2421 | { | |||
2422 | case BUILT_IN_ALLOCA: | |||
2423 | case BUILT_IN_ALLOCA_WITH_ALIGN: | |||
2424 | case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX: | |||
2425 | return; | |||
2426 | default: | |||
2427 | break; | |||
2428 | } | |||
2429 | } | |||
2430 | ||||
2431 | tree fntype = gimple_call_fntype (stmt); | |||
2432 | tree fntypeattrs = TYPE_ATTRIBUTES (fntype)((tree_class_check ((fntype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2432, __FUNCTION__))->type_common.attributes); | |||
2433 | ||||
2434 | tree alloc_size = lookup_attribute ("alloc_size", fntypeattrs); | |||
2435 | if (!alloc_size) | |||
2436 | return; | |||
2437 | ||||
2438 | /* Extract attribute alloc_size from the type of the called expression | |||
2439 | (which could be a function or a function pointer) and if set, store | |||
2440 | the indices of the corresponding arguments in ALLOC_IDX, and then | |||
2441 | the actual argument(s) at those indices in ALLOC_ARGS. */ | |||
2442 | int idx[2] = { -1, -1 }; | |||
2443 | tree alloc_args[] = { NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr }; | |||
2444 | unsigned nargs = gimple_call_num_args (stmt); | |||
2445 | ||||
2446 | tree args = TREE_VALUE (alloc_size)((tree_check ((alloc_size), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2446, __FUNCTION__, (TREE_LIST)))->list.value); | |||
2447 | idx[0] = TREE_INT_CST_LOW (TREE_VALUE (args))((unsigned long) (*tree_int_cst_elt_check ((((tree_check ((args ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2447, __FUNCTION__, (TREE_LIST)))->list.value)), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2447, __FUNCTION__))) - 1; | |||
2448 | /* Avoid invalid calls to functions without a prototype. */ | |||
2449 | if ((unsigned) idx[0] >= nargs) | |||
2450 | return; | |||
2451 | alloc_args[0] = call_arg (stmt, idx[0]); | |||
2452 | if (TREE_CHAIN (args)((contains_struct_check ((args), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2452, __FUNCTION__))->common.chain)) | |||
2453 | { | |||
2454 | idx[1] = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args)))((unsigned long) (*tree_int_cst_elt_check ((((tree_check (((( contains_struct_check ((args), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2454, __FUNCTION__))->common.chain)), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2454, __FUNCTION__, (TREE_LIST)))->list.value)), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2454, __FUNCTION__))) - 1; | |||
2455 | if ((unsigned) idx[1] >= nargs) | |||
2456 | return; | |||
2457 | alloc_args[1] = call_arg (stmt, idx[1]); | |||
2458 | } | |||
2459 | ||||
2460 | maybe_warn_alloc_args_overflow (stmt, alloc_args, idx); | |||
2461 | } | |||
2462 | ||||
2463 | /* Check a call STMT to strcat() for overflow and warn if it does. */ | |||
2464 | ||||
2465 | void | |||
2466 | pass_waccess::check_strcat (gcall *stmt) | |||
2467 | { | |||
2468 | if (m_early_checks_p) | |||
2469 | return; | |||
2470 | ||||
2471 | if (!warn_stringop_overflowglobal_options.x_warn_stringop_overflow && !warn_stringop_overreadglobal_options.x_warn_stringop_overread) | |||
2472 | return; | |||
2473 | ||||
2474 | tree dest = call_arg (stmt, 0); | |||
2475 | tree src = call_arg (stmt, 1); | |||
2476 | ||||
2477 | /* There is no way here to determine the length of the string in | |||
2478 | the destination to which the SRC string is being appended so | |||
2479 | just diagnose cases when the source string is longer than | |||
2480 | the destination object. */ | |||
2481 | access_data data (m_ptr_qry.rvals, stmt, access_read_write, NULL_TREE(tree) nullptr, | |||
2482 | true, NULL_TREE(tree) nullptr, true); | |||
2483 | const int ost = warn_stringop_overflowglobal_options.x_warn_stringop_overflow ? warn_stringop_overflowglobal_options.x_warn_stringop_overflow - 1 : 1; | |||
2484 | compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry); | |||
2485 | tree destsize = compute_objsize (dest, stmt, ost, &data.dst, &m_ptr_qry); | |||
2486 | ||||
2487 | check_access (stmt, /*dstwrite=*/NULL_TREE(tree) nullptr, /*maxread=*/NULL_TREE(tree) nullptr, | |||
2488 | src, destsize, data.mode, &data, m_ptr_qry.rvals); | |||
2489 | } | |||
2490 | ||||
2491 | /* Check a call STMT to strcat() for overflow and warn if it does. */ | |||
2492 | ||||
2493 | void | |||
2494 | pass_waccess::check_strncat (gcall *stmt) | |||
2495 | { | |||
2496 | if (m_early_checks_p) | |||
2497 | return; | |||
2498 | ||||
2499 | if (!warn_stringop_overflowglobal_options.x_warn_stringop_overflow && !warn_stringop_overreadglobal_options.x_warn_stringop_overread) | |||
2500 | return; | |||
2501 | ||||
2502 | tree dest = call_arg (stmt, 0); | |||
2503 | tree src = call_arg (stmt, 1); | |||
2504 | /* The upper bound on the number of bytes to write. */ | |||
2505 | tree maxread = call_arg (stmt, 2); | |||
2506 | ||||
2507 | /* Detect unterminated source (only). */ | |||
2508 | if (!check_nul_terminated_array (stmt, src, maxread)) | |||
2509 | return; | |||
2510 | ||||
2511 | /* The length of the source sequence. */ | |||
2512 | tree slen = c_strlen (src, 1); | |||
2513 | ||||
2514 | /* Try to determine the range of lengths that the source expression | |||
2515 | refers to. Since the lengths are only used for warning and not | |||
2516 | for code generation disable strict mode below. */ | |||
2517 | tree maxlen = slen; | |||
2518 | if (!maxlen) | |||
2519 | { | |||
2520 | c_strlen_data lendata = { }; | |||
2521 | get_range_strlen (src, &lendata, /* eltsize = */ 1); | |||
2522 | maxlen = lendata.maxbound; | |||
2523 | } | |||
2524 | ||||
2525 | access_data data (m_ptr_qry.rvals, stmt, access_read_write); | |||
2526 | /* Try to verify that the destination is big enough for the shortest | |||
2527 | string. First try to determine the size of the destination object | |||
2528 | into which the source is being copied. */ | |||
2529 | const int ost = warn_stringop_overflowglobal_options.x_warn_stringop_overflow - 1; | |||
2530 | tree destsize = compute_objsize (dest, stmt, ost, &data.dst, &m_ptr_qry); | |||
2531 | ||||
2532 | /* Add one for the terminating nul. */ | |||
2533 | tree srclen = (maxlen | |||
2534 | ? fold_build2 (PLUS_EXPR, size_type_node, maxlen,fold_build2_loc (((location_t) 0), PLUS_EXPR, global_trees[TI_SIZE_TYPE ], maxlen, global_trees[TI_SIZE_ONE] ) | |||
2535 | size_one_node)fold_build2_loc (((location_t) 0), PLUS_EXPR, global_trees[TI_SIZE_TYPE ], maxlen, global_trees[TI_SIZE_ONE] ) | |||
2536 | : NULL_TREE(tree) nullptr); | |||
2537 | ||||
2538 | /* The strncat function copies at most MAXREAD bytes and always appends | |||
2539 | the terminating nul so the specified upper bound should never be equal | |||
2540 | to (or greater than) the size of the destination. */ | |||
2541 | if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (destsize) | |||
2542 | && tree_int_cst_equal (destsize, maxread)) | |||
2543 | { | |||
2544 | location_t loc = get_location (stmt); | |||
2545 | warning_at (loc, OPT_Wstringop_overflow_, | |||
2546 | "%qD specified bound %E equals destination size", | |||
2547 | get_callee_fndecl (stmt), maxread); | |||
2548 | ||||
2549 | return; | |||
2550 | } | |||
2551 | ||||
2552 | if (!srclen | |||
2553 | || (maxread && tree_fits_uhwi_p (maxread) | |||
2554 | && tree_fits_uhwi_p (srclen) | |||
2555 | && tree_int_cst_lt (maxread, srclen))) | |||
2556 | srclen = maxread; | |||
2557 | ||||
2558 | check_access (stmt, /*dstwrite=*/NULL_TREE(tree) nullptr, maxread, srclen, | |||
2559 | destsize, data.mode, &data, m_ptr_qry.rvals); | |||
2560 | } | |||
2561 | ||||
2562 | /* Check a call STMT to stpcpy() or strcpy() for overflow and warn | |||
2563 | if it does. */ | |||
2564 | ||||
2565 | void | |||
2566 | pass_waccess::check_stxcpy (gcall *stmt) | |||
2567 | { | |||
2568 | if (m_early_checks_p) | |||
2569 | return; | |||
2570 | ||||
2571 | tree dst = call_arg (stmt, 0); | |||
2572 | tree src = call_arg (stmt, 1); | |||
2573 | ||||
2574 | tree size; | |||
2575 | bool exact; | |||
2576 | if (tree nonstr = unterminated_array (src, &size, &exact)) | |||
2577 | { | |||
2578 | /* NONSTR refers to the non-nul terminated constant array. */ | |||
2579 | warn_string_no_nul (get_location (stmt), stmt, NULLnullptr, src, nonstr, | |||
2580 | size, exact); | |||
2581 | return; | |||
2582 | } | |||
2583 | ||||
2584 | if (warn_stringop_overflowglobal_options.x_warn_stringop_overflow) | |||
2585 | { | |||
2586 | access_data data (m_ptr_qry.rvals, stmt, access_read_write, NULL_TREE(tree) nullptr, | |||
2587 | true, NULL_TREE(tree) nullptr, true); | |||
2588 | const int ost = warn_stringop_overflowglobal_options.x_warn_stringop_overflow ? warn_stringop_overflowglobal_options.x_warn_stringop_overflow - 1 : 1; | |||
2589 | compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry); | |||
2590 | tree dstsize = compute_objsize (dst, stmt, ost, &data.dst, &m_ptr_qry); | |||
2591 | check_access (stmt, /*dstwrite=*/ NULL_TREE(tree) nullptr, | |||
2592 | /*maxread=*/ NULL_TREE(tree) nullptr, /*srcstr=*/ src, | |||
2593 | dstsize, data.mode, &data, m_ptr_qry.rvals); | |||
2594 | } | |||
2595 | ||||
2596 | /* Check to see if the argument was declared attribute nonstring | |||
2597 | and if so, issue a warning since at this point it's not known | |||
2598 | to be nul-terminated. */ | |||
2599 | tree fndecl = get_callee_fndecl (stmt); | |||
2600 | maybe_warn_nonstring_arg (fndecl, stmt); | |||
2601 | } | |||
2602 | ||||
2603 | /* Check a call STMT to stpncpy() or strncpy() for overflow and warn | |||
2604 | if it does. */ | |||
2605 | ||||
2606 | void | |||
2607 | pass_waccess::check_stxncpy (gcall *stmt) | |||
2608 | { | |||
2609 | if (m_early_checks_p || !warn_stringop_overflowglobal_options.x_warn_stringop_overflow) | |||
2610 | return; | |||
2611 | ||||
2612 | tree dst = call_arg (stmt, 0); | |||
2613 | tree src = call_arg (stmt, 1); | |||
2614 | /* The number of bytes to write (not the maximum). */ | |||
2615 | tree len = call_arg (stmt, 2); | |||
2616 | ||||
2617 | access_data data (m_ptr_qry.rvals, stmt, access_read_write, len, true, len, | |||
2618 | true); | |||
2619 | const int ost = warn_stringop_overflowglobal_options.x_warn_stringop_overflow ? warn_stringop_overflowglobal_options.x_warn_stringop_overflow - 1 : 1; | |||
2620 | compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry); | |||
2621 | tree dstsize = compute_objsize (dst, stmt, ost, &data.dst, &m_ptr_qry); | |||
2622 | ||||
2623 | check_access (stmt, /*dstwrite=*/len, /*maxread=*/len, src, dstsize, | |||
2624 | data.mode, &data, m_ptr_qry.rvals); | |||
2625 | } | |||
2626 | ||||
2627 | /* Check a call STMT to stpncpy() or strncpy() for overflow and warn | |||
2628 | if it does. */ | |||
2629 | ||||
2630 | void | |||
2631 | pass_waccess::check_strncmp (gcall *stmt) | |||
2632 | { | |||
2633 | if (m_early_checks_p || !warn_stringop_overreadglobal_options.x_warn_stringop_overread) | |||
2634 | return; | |||
2635 | ||||
2636 | tree arg1 = call_arg (stmt, 0); | |||
2637 | tree arg2 = call_arg (stmt, 1); | |||
2638 | tree bound = call_arg (stmt, 2); | |||
2639 | ||||
2640 | /* First check each argument separately, considering the bound. */ | |||
2641 | if (!check_nul_terminated_array (stmt, arg1, bound) | |||
2642 | || !check_nul_terminated_array (stmt, arg2, bound)) | |||
2643 | return; | |||
2644 | ||||
2645 | /* A strncmp read from each argument is constrained not just by | |||
2646 | the bound but also by the length of the shorter string. Specifying | |||
2647 | a bound that's larger than the size of either array makes no sense | |||
2648 | and is likely a bug. When the length of neither of the two strings | |||
2649 | is known but the sizes of both of the arrays they are stored in is, | |||
2650 | issue a warning if the bound is larger than the size of | |||
2651 | the larger of the two arrays. */ | |||
2652 | ||||
2653 | c_strlen_data lendata1{ }, lendata2{ }; | |||
2654 | tree len1 = c_strlen (arg1, 1, &lendata1); | |||
2655 | tree len2 = c_strlen (arg2, 1, &lendata2); | |||
2656 | ||||
2657 | if (len1 && TREE_CODE (len1)((enum tree_code) (len1)->base.code) != INTEGER_CST) | |||
2658 | len1 = NULL_TREE(tree) nullptr; | |||
2659 | if (len2 && TREE_CODE (len2)((enum tree_code) (len2)->base.code) != INTEGER_CST) | |||
2660 | len2 = NULL_TREE(tree) nullptr; | |||
2661 | ||||
2662 | if (len1 && len2) | |||
2663 | /* If the length of both arguments was computed they must both be | |||
2664 | nul-terminated and no further checking is necessary regardless | |||
2665 | of the bound. */ | |||
2666 | return; | |||
2667 | ||||
2668 | /* Check to see if the argument was declared with attribute nonstring | |||
2669 | and if so, issue a warning since at this point it's not known to be | |||
2670 | nul-terminated. */ | |||
2671 | if (maybe_warn_nonstring_arg (get_callee_fndecl (stmt), stmt)) | |||
2672 | return; | |||
2673 | ||||
2674 | access_data adata1 (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE(tree) nullptr, false, | |||
2675 | bound, true); | |||
2676 | access_data adata2 (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE(tree) nullptr, false, | |||
2677 | bound, true); | |||
2678 | ||||
2679 | /* Determine the range of the bound first and bail if it fails; it's | |||
2680 | cheaper than computing the size of the objects. */ | |||
2681 | tree bndrng[2] = { NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr }; | |||
2682 | get_size_range (m_ptr_qry.rvals, bound, stmt, bndrng, adata1.src_bndrng); | |||
2683 | if (!bndrng[0] || integer_zerop (bndrng[0])) | |||
2684 | return; | |||
2685 | ||||
2686 | if (len1 && tree_int_cst_lt (len1, bndrng[0])) | |||
2687 | bndrng[0] = len1; | |||
2688 | if (len2 && tree_int_cst_lt (len2, bndrng[0])) | |||
2689 | bndrng[0] = len2; | |||
2690 | ||||
2691 | /* compute_objsize almost never fails (and ultimately should never | |||
2692 | fail). Don't bother to handle the rare case when it does. */ | |||
2693 | if (!compute_objsize (arg1, stmt, 1, &adata1.src, &m_ptr_qry) | |||
2694 | || !compute_objsize (arg2, stmt, 1, &adata2.src, &m_ptr_qry)) | |||
2695 | return; | |||
2696 | ||||
2697 | /* Compute the size of the remaining space in each array after | |||
2698 | subtracting any offset into it. */ | |||
2699 | offset_int rem1 = adata1.src.size_remaining (); | |||
2700 | offset_int rem2 = adata2.src.size_remaining (); | |||
2701 | ||||
2702 | /* Cap REM1 and REM2 at the other if the other's argument is known | |||
2703 | to be an unterminated array, either because there's no space | |||
2704 | left in it after adding its offset or because it's constant and | |||
2705 | has no nul. */ | |||
2706 | if (rem1 == 0 || (rem1 < rem2 && lendata1.decl)) | |||
2707 | rem2 = rem1; | |||
2708 | else if (rem2 == 0 || (rem2 < rem1 && lendata2.decl)) | |||
2709 | rem1 = rem2; | |||
2710 | ||||
2711 | /* Point PAD at the array to reference in the note if a warning | |||
2712 | is issued. */ | |||
2713 | access_data *pad = len1 ? &adata2 : &adata1; | |||
2714 | offset_int maxrem = wi::max (rem1, rem2, UNSIGNED); | |||
2715 | if (lendata1.decl || lendata2.decl | |||
2716 | || maxrem < wi::to_offset (bndrng[0])) | |||
2717 | { | |||
2718 | /* Warn when either argument isn't nul-terminated or the maximum | |||
2719 | remaining space in the two arrays is less than the bound. */ | |||
2720 | tree func = get_callee_fndecl (stmt); | |||
2721 | location_t loc = gimple_location (stmt); | |||
2722 | maybe_warn_for_bound (OPT_Wstringop_overread, loc, stmt, func, | |||
2723 | bndrng, wide_int_to_tree (sizetypesizetype_tab[(int) stk_sizetype], maxrem), | |||
2724 | pad); | |||
2725 | } | |||
2726 | } | |||
2727 | ||||
2728 | /* Determine and check the sizes of the source and the destination | |||
2729 | of calls to __builtin_{bzero,memcpy,mempcpy,memset} calls. STMT is | |||
2730 | the call statement, DEST is the destination argument, SRC is the source | |||
2731 | argument or null, and SIZE is the number of bytes being accessed. Use | |||
2732 | Object Size type-0 regardless of the OPT_Wstringop_overflow_ setting. | |||
2733 | Return true on success (no overflow or invalid sizes), false otherwise. */ | |||
2734 | ||||
2735 | void | |||
2736 | pass_waccess::check_memop_access (gimple *stmt, tree dest, tree src, tree size) | |||
2737 | { | |||
2738 | if (m_early_checks_p) | |||
2739 | return; | |||
2740 | ||||
2741 | /* For functions like memset and memcpy that operate on raw memory | |||
2742 | try to determine the size of the largest source and destination | |||
2743 | object using type-0 Object Size regardless of the object size | |||
2744 | type specified by the option. */ | |||
2745 | access_data data (m_ptr_qry.rvals, stmt, access_read_write); | |||
2746 | tree srcsize | |||
2747 | = src ? compute_objsize (src, stmt, 0, &data.src, &m_ptr_qry) : NULL_TREE(tree) nullptr; | |||
2748 | tree dstsize = compute_objsize (dest, stmt, 0, &data.dst, &m_ptr_qry); | |||
2749 | ||||
2750 | check_access (stmt, size, /*maxread=*/NULL_TREE(tree) nullptr, srcsize, dstsize, | |||
2751 | data.mode, &data, m_ptr_qry.rvals); | |||
2752 | } | |||
2753 | ||||
2754 | /* A convenience wrapper for check_access to check access by a read-only | |||
2755 | function like puts or strcmp. */ | |||
2756 | ||||
2757 | void | |||
2758 | pass_waccess::check_read_access (gimple *stmt, tree src, | |||
2759 | tree bound /* = NULL_TREE */, | |||
2760 | int ost /* = 1 */) | |||
2761 | { | |||
2762 | if (m_early_checks_p || !warn_stringop_overreadglobal_options.x_warn_stringop_overread) | |||
2763 | return; | |||
2764 | ||||
2765 | if (bound && !useless_type_conversion_p (size_type_nodeglobal_trees[TI_SIZE_TYPE], TREE_TYPE (bound)((contains_struct_check ((bound), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2765, __FUNCTION__))->typed.type))) | |||
2766 | bound = fold_convert (size_type_node, bound)fold_convert_loc (((location_t) 0), global_trees[TI_SIZE_TYPE ], bound); | |||
2767 | ||||
2768 | tree fndecl = get_callee_fndecl (stmt); | |||
2769 | maybe_warn_nonstring_arg (fndecl, stmt); | |||
2770 | ||||
2771 | access_data data (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE(tree) nullptr, | |||
2772 | false, bound, true); | |||
2773 | compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry); | |||
2774 | check_access (stmt, /*dstwrite=*/ NULL_TREE(tree) nullptr, /*maxread=*/ bound, | |||
2775 | /*srcstr=*/ src, /*dstsize=*/ NULL_TREE(tree) nullptr, data.mode, | |||
2776 | &data, m_ptr_qry.rvals); | |||
2777 | } | |||
2778 | ||||
2779 | /* Return true if memory model ORD is constant in the context of STMT and | |||
2780 | set *CSTVAL to the constant value. Otherwise return false. Warn for | |||
2781 | invalid ORD. */ | |||
2782 | ||||
2783 | bool | |||
2784 | memmodel_to_uhwi (tree ord, gimple *stmt, unsigned HOST_WIDE_INTlong *cstval) | |||
2785 | { | |||
2786 | unsigned HOST_WIDE_INTlong val; | |||
2787 | ||||
2788 | if (TREE_CODE (ord)((enum tree_code) (ord)->base.code) == INTEGER_CST) | |||
2789 | { | |||
2790 | if (!tree_fits_uhwi_p (ord)) | |||
2791 | return false; | |||
2792 | val = tree_to_uhwi (ord); | |||
2793 | } | |||
2794 | else | |||
2795 | { | |||
2796 | /* Use the range query to determine constant values in the absence | |||
2797 | of constant propagation (such as at -O0). */ | |||
2798 | Value_Range rng (TREE_TYPE (ord)((contains_struct_check ((ord), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 2798, __FUNCTION__))->typed.type)); | |||
2799 | if (!get_range_query (cfun(cfun + 0))->range_of_expr (rng, ord, stmt) | |||
2800 | || !rng.singleton_p (&ord)) | |||
2801 | return false; | |||
2802 | ||||
2803 | wide_int lob = rng.lower_bound (); | |||
2804 | if (!wi::fits_uhwi_p (lob)) | |||
2805 | return false; | |||
2806 | ||||
2807 | val = lob.to_shwi (); | |||
2808 | } | |||
2809 | ||||
2810 | if (targetm.memmodel_check) | |||
2811 | /* This might warn for an invalid VAL but return a conservatively | |||
2812 | valid result. */ | |||
2813 | val = targetm.memmodel_check (val); | |||
2814 | else if (val & ~MEMMODEL_MASK((1<<16)-1)) | |||
2815 | { | |||
2816 | tree fndecl = gimple_call_fndecl (stmt); | |||
2817 | location_t loc = gimple_location (stmt); | |||
2818 | loc = expansion_point_location_if_in_system_header (loc); | |||
2819 | ||||
2820 | warning_at (loc, OPT_Winvalid_memory_model, | |||
2821 | "unknown architecture specifier in memory model " | |||
2822 | "%wi for %qD", val, fndecl); | |||
2823 | return false; | |||
2824 | } | |||
2825 | ||||
2826 | *cstval = val; | |||
2827 | ||||
2828 | return true; | |||
2829 | } | |||
2830 | ||||
2831 | /* Valid memory model for each set of atomic built-in functions. */ | |||
2832 | ||||
2833 | struct memmodel_pair | |||
2834 | { | |||
2835 | memmodel modval; | |||
2836 | const char* modname; | |||
2837 | ||||
2838 | #define MEMMODEL_PAIR(val, str){ MEMMODEL_val, "memory_order_" str } \ | |||
2839 | { MEMMODEL_ ## val, "memory_order_" str } | |||
2840 | }; | |||
2841 | ||||
2842 | /* Valid memory models in the order of increasing strength. */ | |||
2843 | ||||
2844 | static const memmodel_pair memory_models[] = | |||
2845 | { MEMMODEL_PAIR (RELAXED, "relaxed"){ MEMMODEL_RELAXED, "memory_order_" "relaxed" }, | |||
2846 | MEMMODEL_PAIR (SEQ_CST, "seq_cst"){ MEMMODEL_SEQ_CST, "memory_order_" "seq_cst" }, | |||
2847 | MEMMODEL_PAIR (ACQUIRE, "acquire"){ MEMMODEL_ACQUIRE, "memory_order_" "acquire" }, | |||
2848 | MEMMODEL_PAIR (CONSUME, "consume"){ MEMMODEL_CONSUME, "memory_order_" "consume" }, | |||
2849 | MEMMODEL_PAIR (RELEASE, "release"){ MEMMODEL_RELEASE, "memory_order_" "release" }, | |||
2850 | MEMMODEL_PAIR (ACQ_REL, "acq_rel"){ MEMMODEL_ACQ_REL, "memory_order_" "acq_rel" } | |||
2851 | }; | |||
2852 | ||||
2853 | /* Return the name of the memory model VAL. */ | |||
2854 | ||||
2855 | static const char* | |||
2856 | memmodel_name (unsigned HOST_WIDE_INTlong val) | |||
2857 | { | |||
2858 | val = memmodel_base (val); | |||
2859 | ||||
2860 | for (unsigned i = 0; i != ARRAY_SIZE (memory_models)(sizeof (memory_models) / sizeof ((memory_models)[0])); ++i) | |||
2861 | { | |||
2862 | if (val == memory_models[i].modval) | |||
2863 | return memory_models[i].modname; | |||
2864 | } | |||
2865 | return NULLnullptr; | |||
2866 | } | |||
2867 | ||||
2868 | /* Indices of valid MEMORY_MODELS above for corresponding atomic operations. */ | |||
2869 | static const unsigned char load_models[] = { 0, 1, 2, 3, UCHAR_MAX(127*2 +1) }; | |||
2870 | static const unsigned char store_models[] = { 0, 1, 4, UCHAR_MAX(127*2 +1) }; | |||
2871 | static const unsigned char xchg_models[] = { 0, 1, 3, 4, 5, UCHAR_MAX(127*2 +1) }; | |||
2872 | static const unsigned char flag_clr_models[] = { 0, 1, 4, UCHAR_MAX(127*2 +1) }; | |||
2873 | static const unsigned char all_models[] = { 0, 1, 2, 3, 4, 5, UCHAR_MAX(127*2 +1) }; | |||
2874 | ||||
2875 | /* Check the success memory model argument ORD_SUCS to the call STMT to | |||
2876 | an atomic function and warn if it's invalid. If nonnull, also check | |||
2877 | the failure memory model ORD_FAIL and warn if it's invalid. Return | |||
2878 | true if a warning has been issued. */ | |||
2879 | ||||
2880 | bool | |||
2881 | pass_waccess::maybe_warn_memmodel (gimple *stmt, tree ord_sucs, | |||
2882 | tree ord_fail, const unsigned char *valid) | |||
2883 | { | |||
2884 | unsigned HOST_WIDE_INTlong sucs, fail = 0; | |||
2885 | if (!memmodel_to_uhwi (ord_sucs, stmt, &sucs) | |||
2886 | || (ord_fail && !memmodel_to_uhwi (ord_fail, stmt, &fail))) | |||
2887 | return false; | |||
2888 | ||||
2889 | bool is_valid = false; | |||
2890 | if (valid) | |||
2891 | for (unsigned i = 0; valid[i] != UCHAR_MAX(127*2 +1); ++i) | |||
2892 | { | |||
2893 | memmodel model = memory_models[valid[i]].modval; | |||
2894 | if (memmodel_base (sucs) == model) | |||
2895 | { | |||
2896 | is_valid = true; | |||
2897 | break; | |||
2898 | } | |||
2899 | } | |||
2900 | else | |||
2901 | is_valid = true; | |||
2902 | ||||
2903 | tree fndecl = gimple_call_fndecl (stmt); | |||
2904 | location_t loc = gimple_location (stmt); | |||
2905 | loc = expansion_point_location_if_in_system_header (loc); | |||
2906 | ||||
2907 | if (!is_valid) | |||
2908 | { | |||
2909 | bool warned = false; | |||
2910 | auto_diagnostic_group d; | |||
2911 | if (const char *modname = memmodel_name (sucs)) | |||
2912 | warned = warning_at (loc, OPT_Winvalid_memory_model, | |||
2913 | "invalid memory model %qs for %qD", | |||
2914 | modname, fndecl); | |||
2915 | else | |||
2916 | warned = warning_at (loc, OPT_Winvalid_memory_model, | |||
2917 | "invalid memory model %wi for %qD", | |||
2918 | sucs, fndecl); | |||
2919 | ||||
2920 | if (!warned) | |||
2921 | return false; | |||
2922 | ||||
2923 | /* Print a note with the valid memory models. */ | |||
2924 | pretty_printer pp; | |||
2925 | pp_show_color (&pp)(&pp)->show_color = pp_show_color (global_dc->printer)(global_dc->printer)->show_color; | |||
2926 | for (unsigned i = 0; valid[i] != UCHAR_MAX(127*2 +1); ++i) | |||
2927 | { | |||
2928 | const char *modname = memory_models[valid[i]].modname; | |||
2929 | pp_printf (&pp, "%s%qs", i ? ", " : "", modname); | |||
2930 | } | |||
2931 | ||||
2932 | inform (loc, "valid models are %s", pp_formatted_text (&pp)); | |||
2933 | return true; | |||
2934 | } | |||
2935 | ||||
2936 | if (!ord_fail) | |||
2937 | return false; | |||
2938 | ||||
2939 | if (fail == MEMMODEL_RELEASE || fail == MEMMODEL_ACQ_REL) | |||
2940 | if (const char *failname = memmodel_name (fail)) | |||
2941 | { | |||
2942 | /* If both memory model arguments are valid but their combination | |||
2943 | is not, use their names in the warning. */ | |||
2944 | auto_diagnostic_group d; | |||
2945 | if (!warning_at (loc, OPT_Winvalid_memory_model, | |||
2946 | "invalid failure memory model %qs for %qD", | |||
2947 | failname, fndecl)) | |||
2948 | return false; | |||
2949 | ||||
2950 | inform (loc, | |||
2951 | "valid failure models are %qs, %qs, %qs, %qs", | |||
2952 | "memory_order_relaxed", "memory_order_seq_cst", | |||
2953 | "memory_order_acquire", "memory_order_consume"); | |||
2954 | return true; | |||
2955 | } | |||
2956 | ||||
2957 | if (memmodel_base (fail) <= memmodel_base (sucs)) | |||
2958 | return false; | |||
2959 | ||||
2960 | if (const char *sucsname = memmodel_name (sucs)) | |||
2961 | if (const char *failname = memmodel_name (fail)) | |||
2962 | { | |||
2963 | /* If both memory model arguments are valid but their combination | |||
2964 | is not, use their names in the warning. */ | |||
2965 | auto_diagnostic_group d; | |||
2966 | if (!warning_at (loc, OPT_Winvalid_memory_model, | |||
2967 | "failure memory model %qs cannot be stronger " | |||
2968 | "than success memory model %qs for %qD", | |||
2969 | failname, sucsname, fndecl)) | |||
2970 | return false; | |||
2971 | ||||
2972 | /* Print a note with the valid failure memory models which are | |||
2973 | those with a value less than or equal to the success mode. */ | |||
2974 | char buf[120]; | |||
2975 | *buf = '\0'; | |||
2976 | for (unsigned i = 0; | |||
2977 | memory_models[i].modval <= memmodel_base (sucs); ++i) | |||
2978 | { | |||
2979 | if (*buf) | |||
2980 | strcat (buf, ", "); | |||
2981 | ||||
2982 | const char *modname = memory_models[valid[i]].modname; | |||
2983 | sprintf (buf + strlen (buf), "'%s'", modname); | |||
2984 | } | |||
2985 | ||||
2986 | inform (loc, "valid models are %s", buf); | |||
2987 | return true; | |||
2988 | } | |||
2989 | ||||
2990 | /* If either memory model argument value is invalid use the numerical | |||
2991 | value of both in the message. */ | |||
2992 | return warning_at (loc, OPT_Winvalid_memory_model, | |||
2993 | "failure memory model %wi cannot be stronger " | |||
2994 | "than success memory model %wi for %qD", | |||
2995 | fail, sucs, fndecl); | |||
2996 | } | |||
2997 | ||||
2998 | /* Wrapper for the above. */ | |||
2999 | ||||
3000 | void | |||
3001 | pass_waccess::check_atomic_memmodel (gimple *stmt, tree ord_sucs, | |||
3002 | tree ord_fail, const unsigned char *valid) | |||
3003 | { | |||
3004 | if (warning_suppressed_p (stmt, OPT_Winvalid_memory_model)) | |||
3005 | return; | |||
3006 | ||||
3007 | if (!maybe_warn_memmodel (stmt, ord_sucs, ord_fail, valid)) | |||
3008 | return; | |||
3009 | ||||
3010 | suppress_warning (stmt, OPT_Winvalid_memory_model); | |||
3011 | } | |||
3012 | ||||
3013 | /* Check a call STMT to an atomic or sync built-in. */ | |||
3014 | ||||
3015 | bool | |||
3016 | pass_waccess::check_atomic_builtin (gcall *stmt) | |||
3017 | { | |||
3018 | tree callee = gimple_call_fndecl (stmt); | |||
3019 | if (!callee) | |||
3020 | return false; | |||
3021 | ||||
3022 | /* The size in bytes of the access by the function, and the number | |||
3023 | of the second argument to check (if any). */ | |||
3024 | unsigned bytes = 0, arg2 = UINT_MAX(2147483647 *2U +1U); | |||
3025 | unsigned sucs_arg = UINT_MAX(2147483647 *2U +1U), fail_arg = UINT_MAX(2147483647 *2U +1U); | |||
3026 | /* Points to the array of indices of valid memory models. */ | |||
3027 | const unsigned char *pvalid_models = NULLnullptr; | |||
3028 | ||||
3029 | switch (DECL_FUNCTION_CODE (callee)) | |||
3030 | { | |||
3031 | #define BUILTIN_ACCESS_SIZE_FNSPEC(N)BUILT_IN_SYNC_FETCH_AND_ADD_N: case BUILT_IN_SYNC_FETCH_AND_SUB_N : case BUILT_IN_SYNC_FETCH_AND_OR_N: case BUILT_IN_SYNC_FETCH_AND_AND_N : case BUILT_IN_SYNC_FETCH_AND_XOR_N: case BUILT_IN_SYNC_FETCH_AND_NAND_N : case BUILT_IN_SYNC_ADD_AND_FETCH_N: case BUILT_IN_SYNC_SUB_AND_FETCH_N : case BUILT_IN_SYNC_OR_AND_FETCH_N: case BUILT_IN_SYNC_AND_AND_FETCH_N : case BUILT_IN_SYNC_XOR_AND_FETCH_N: case BUILT_IN_SYNC_NAND_AND_FETCH_N : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N: case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N: case BUILT_IN_SYNC_LOCK_RELEASE_N : bytes = N; break; case BUILT_IN_ATOMIC_LOAD_N: pvalid_models = load_models; sucs_arg = 1; case BUILT_IN_ATOMIC_STORE_N: if (!pvalid_models) pvalid_models = store_models; case BUILT_IN_ATOMIC_ADD_FETCH_N : case BUILT_IN_ATOMIC_SUB_FETCH_N: case BUILT_IN_ATOMIC_AND_FETCH_N : case BUILT_IN_ATOMIC_NAND_FETCH_N: case BUILT_IN_ATOMIC_XOR_FETCH_N : case BUILT_IN_ATOMIC_OR_FETCH_N: case BUILT_IN_ATOMIC_FETCH_ADD_N : case BUILT_IN_ATOMIC_FETCH_SUB_N: case BUILT_IN_ATOMIC_FETCH_AND_N : case BUILT_IN_ATOMIC_FETCH_NAND_N: case BUILT_IN_ATOMIC_FETCH_OR_N : case BUILT_IN_ATOMIC_FETCH_XOR_N: bytes = N; if (sucs_arg == (2147483647 *2U +1U)) sucs_arg = 2; if (!pvalid_models) pvalid_models = all_models; break; case BUILT_IN_ATOMIC_EXCHANGE_N: bytes = N; sucs_arg = 3; pvalid_models = xchg_models; break; case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N : bytes = N; sucs_arg = 4; fail_arg = 5; pvalid_models = all_models ; arg2 = 1 \ | |||
3032 | BUILT_IN_SYNC_FETCH_AND_ADD_ ## N: \ | |||
3033 | case BUILT_IN_SYNC_FETCH_AND_SUB_ ## N: \ | |||
3034 | case BUILT_IN_SYNC_FETCH_AND_OR_ ## N: \ | |||
3035 | case BUILT_IN_SYNC_FETCH_AND_AND_ ## N: \ | |||
3036 | case BUILT_IN_SYNC_FETCH_AND_XOR_ ## N: \ | |||
3037 | case BUILT_IN_SYNC_FETCH_AND_NAND_ ## N: \ | |||
3038 | case BUILT_IN_SYNC_ADD_AND_FETCH_ ## N: \ | |||
3039 | case BUILT_IN_SYNC_SUB_AND_FETCH_ ## N: \ | |||
3040 | case BUILT_IN_SYNC_OR_AND_FETCH_ ## N: \ | |||
3041 | case BUILT_IN_SYNC_AND_AND_FETCH_ ## N: \ | |||
3042 | case BUILT_IN_SYNC_XOR_AND_FETCH_ ## N: \ | |||
3043 | case BUILT_IN_SYNC_NAND_AND_FETCH_ ## N: \ | |||
3044 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_ ## N: \ | |||
3045 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_ ## N: \ | |||
3046 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_ ## N: \ | |||
3047 | case BUILT_IN_SYNC_LOCK_RELEASE_ ## N: \ | |||
3048 | bytes = N; \ | |||
3049 | break; \ | |||
3050 | case BUILT_IN_ATOMIC_LOAD_ ## N: \ | |||
3051 | pvalid_models = load_models; \ | |||
3052 | sucs_arg = 1; \ | |||
3053 | /* FALLTHROUGH */ \ | |||
3054 | case BUILT_IN_ATOMIC_STORE_ ## N: \ | |||
3055 | if (!pvalid_models) \ | |||
3056 | pvalid_models = store_models; \ | |||
3057 | /* FALLTHROUGH */ \ | |||
3058 | case BUILT_IN_ATOMIC_ADD_FETCH_ ## N: \ | |||
3059 | case BUILT_IN_ATOMIC_SUB_FETCH_ ## N: \ | |||
3060 | case BUILT_IN_ATOMIC_AND_FETCH_ ## N: \ | |||
3061 | case BUILT_IN_ATOMIC_NAND_FETCH_ ## N: \ | |||
3062 | case BUILT_IN_ATOMIC_XOR_FETCH_ ## N: \ | |||
3063 | case BUILT_IN_ATOMIC_OR_FETCH_ ## N: \ | |||
3064 | case BUILT_IN_ATOMIC_FETCH_ADD_ ## N: \ | |||
3065 | case BUILT_IN_ATOMIC_FETCH_SUB_ ## N: \ | |||
3066 | case BUILT_IN_ATOMIC_FETCH_AND_ ## N: \ | |||
3067 | case BUILT_IN_ATOMIC_FETCH_NAND_ ## N: \ | |||
3068 | case BUILT_IN_ATOMIC_FETCH_OR_ ## N: \ | |||
3069 | case BUILT_IN_ATOMIC_FETCH_XOR_ ## N: \ | |||
3070 | bytes = N; \ | |||
3071 | if (sucs_arg == UINT_MAX(2147483647 *2U +1U)) \ | |||
3072 | sucs_arg = 2; \ | |||
3073 | if (!pvalid_models) \ | |||
3074 | pvalid_models = all_models; \ | |||
3075 | break; \ | |||
3076 | case BUILT_IN_ATOMIC_EXCHANGE_ ## N: \ | |||
3077 | bytes = N; \ | |||
3078 | sucs_arg = 3; \ | |||
3079 | pvalid_models = xchg_models; \ | |||
3080 | break; \ | |||
3081 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_ ## N: \ | |||
3082 | bytes = N; \ | |||
3083 | sucs_arg = 4; \ | |||
3084 | fail_arg = 5; \ | |||
3085 | pvalid_models = all_models; \ | |||
3086 | arg2 = 1 | |||
3087 | ||||
3088 | case BUILTIN_ACCESS_SIZE_FNSPEC (1)BUILT_IN_SYNC_FETCH_AND_ADD_1: case BUILT_IN_SYNC_FETCH_AND_SUB_1 : case BUILT_IN_SYNC_FETCH_AND_OR_1: case BUILT_IN_SYNC_FETCH_AND_AND_1 : case BUILT_IN_SYNC_FETCH_AND_XOR_1: case BUILT_IN_SYNC_FETCH_AND_NAND_1 : case BUILT_IN_SYNC_ADD_AND_FETCH_1: case BUILT_IN_SYNC_SUB_AND_FETCH_1 : case BUILT_IN_SYNC_OR_AND_FETCH_1: case BUILT_IN_SYNC_AND_AND_FETCH_1 : case BUILT_IN_SYNC_XOR_AND_FETCH_1: case BUILT_IN_SYNC_NAND_AND_FETCH_1 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1: case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1: case BUILT_IN_SYNC_LOCK_RELEASE_1 : bytes = 1; break; case BUILT_IN_ATOMIC_LOAD_1: pvalid_models = load_models; sucs_arg = 1; case BUILT_IN_ATOMIC_STORE_1: if (!pvalid_models) pvalid_models = store_models; case BUILT_IN_ATOMIC_ADD_FETCH_1 : case BUILT_IN_ATOMIC_SUB_FETCH_1: case BUILT_IN_ATOMIC_AND_FETCH_1 : case BUILT_IN_ATOMIC_NAND_FETCH_1: case BUILT_IN_ATOMIC_XOR_FETCH_1 : case BUILT_IN_ATOMIC_OR_FETCH_1: case BUILT_IN_ATOMIC_FETCH_ADD_1 : case BUILT_IN_ATOMIC_FETCH_SUB_1: case BUILT_IN_ATOMIC_FETCH_AND_1 : case BUILT_IN_ATOMIC_FETCH_NAND_1: case BUILT_IN_ATOMIC_FETCH_OR_1 : case BUILT_IN_ATOMIC_FETCH_XOR_1: bytes = 1; if (sucs_arg == (2147483647 *2U +1U)) sucs_arg = 2; if (!pvalid_models) pvalid_models = all_models; break; case BUILT_IN_ATOMIC_EXCHANGE_1: bytes = 1; sucs_arg = 3; pvalid_models = xchg_models; break; case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1 : bytes = 1; sucs_arg = 4; fail_arg = 5; pvalid_models = all_models ; arg2 = 1; | |||
3089 | break; | |||
3090 | case BUILTIN_ACCESS_SIZE_FNSPEC (2)BUILT_IN_SYNC_FETCH_AND_ADD_2: case BUILT_IN_SYNC_FETCH_AND_SUB_2 : case BUILT_IN_SYNC_FETCH_AND_OR_2: case BUILT_IN_SYNC_FETCH_AND_AND_2 : case BUILT_IN_SYNC_FETCH_AND_XOR_2: case BUILT_IN_SYNC_FETCH_AND_NAND_2 : case BUILT_IN_SYNC_ADD_AND_FETCH_2: case BUILT_IN_SYNC_SUB_AND_FETCH_2 : case BUILT_IN_SYNC_OR_AND_FETCH_2: case BUILT_IN_SYNC_AND_AND_FETCH_2 : case BUILT_IN_SYNC_XOR_AND_FETCH_2: case BUILT_IN_SYNC_NAND_AND_FETCH_2 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2: case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2: case BUILT_IN_SYNC_LOCK_RELEASE_2 : bytes = 2; break; case BUILT_IN_ATOMIC_LOAD_2: pvalid_models = load_models; sucs_arg = 1; case BUILT_IN_ATOMIC_STORE_2: if (!pvalid_models) pvalid_models = store_models; case BUILT_IN_ATOMIC_ADD_FETCH_2 : case BUILT_IN_ATOMIC_SUB_FETCH_2: case BUILT_IN_ATOMIC_AND_FETCH_2 : case BUILT_IN_ATOMIC_NAND_FETCH_2: case BUILT_IN_ATOMIC_XOR_FETCH_2 : case BUILT_IN_ATOMIC_OR_FETCH_2: case BUILT_IN_ATOMIC_FETCH_ADD_2 : case BUILT_IN_ATOMIC_FETCH_SUB_2: case BUILT_IN_ATOMIC_FETCH_AND_2 : case BUILT_IN_ATOMIC_FETCH_NAND_2: case BUILT_IN_ATOMIC_FETCH_OR_2 : case BUILT_IN_ATOMIC_FETCH_XOR_2: bytes = 2; if (sucs_arg == (2147483647 *2U +1U)) sucs_arg = 2; if (!pvalid_models) pvalid_models = all_models; break; case BUILT_IN_ATOMIC_EXCHANGE_2: bytes = 2; sucs_arg = 3; pvalid_models = xchg_models; break; case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2 : bytes = 2; sucs_arg = 4; fail_arg = 5; pvalid_models = all_models ; arg2 = 1; | |||
3091 | break; | |||
3092 | case BUILTIN_ACCESS_SIZE_FNSPEC (4)BUILT_IN_SYNC_FETCH_AND_ADD_4: case BUILT_IN_SYNC_FETCH_AND_SUB_4 : case BUILT_IN_SYNC_FETCH_AND_OR_4: case BUILT_IN_SYNC_FETCH_AND_AND_4 : case BUILT_IN_SYNC_FETCH_AND_XOR_4: case BUILT_IN_SYNC_FETCH_AND_NAND_4 : case BUILT_IN_SYNC_ADD_AND_FETCH_4: case BUILT_IN_SYNC_SUB_AND_FETCH_4 : case BUILT_IN_SYNC_OR_AND_FETCH_4: case BUILT_IN_SYNC_AND_AND_FETCH_4 : case BUILT_IN_SYNC_XOR_AND_FETCH_4: case BUILT_IN_SYNC_NAND_AND_FETCH_4 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4: case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4: case BUILT_IN_SYNC_LOCK_RELEASE_4 : bytes = 4; break; case BUILT_IN_ATOMIC_LOAD_4: pvalid_models = load_models; sucs_arg = 1; case BUILT_IN_ATOMIC_STORE_4: if (!pvalid_models) pvalid_models = store_models; case BUILT_IN_ATOMIC_ADD_FETCH_4 : case BUILT_IN_ATOMIC_SUB_FETCH_4: case BUILT_IN_ATOMIC_AND_FETCH_4 : case BUILT_IN_ATOMIC_NAND_FETCH_4: case BUILT_IN_ATOMIC_XOR_FETCH_4 : case BUILT_IN_ATOMIC_OR_FETCH_4: case BUILT_IN_ATOMIC_FETCH_ADD_4 : case BUILT_IN_ATOMIC_FETCH_SUB_4: case BUILT_IN_ATOMIC_FETCH_AND_4 : case BUILT_IN_ATOMIC_FETCH_NAND_4: case BUILT_IN_ATOMIC_FETCH_OR_4 : case BUILT_IN_ATOMIC_FETCH_XOR_4: bytes = 4; if (sucs_arg == (2147483647 *2U +1U)) sucs_arg = 2; if (!pvalid_models) pvalid_models = all_models; break; case BUILT_IN_ATOMIC_EXCHANGE_4: bytes = 4; sucs_arg = 3; pvalid_models = xchg_models; break; case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4 : bytes = 4; sucs_arg = 4; fail_arg = 5; pvalid_models = all_models ; arg2 = 1; | |||
3093 | break; | |||
3094 | case BUILTIN_ACCESS_SIZE_FNSPEC (8)BUILT_IN_SYNC_FETCH_AND_ADD_8: case BUILT_IN_SYNC_FETCH_AND_SUB_8 : case BUILT_IN_SYNC_FETCH_AND_OR_8: case BUILT_IN_SYNC_FETCH_AND_AND_8 : case BUILT_IN_SYNC_FETCH_AND_XOR_8: case BUILT_IN_SYNC_FETCH_AND_NAND_8 : case BUILT_IN_SYNC_ADD_AND_FETCH_8: case BUILT_IN_SYNC_SUB_AND_FETCH_8 : case BUILT_IN_SYNC_OR_AND_FETCH_8: case BUILT_IN_SYNC_AND_AND_FETCH_8 : case BUILT_IN_SYNC_XOR_AND_FETCH_8: case BUILT_IN_SYNC_NAND_AND_FETCH_8 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8: case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8: case BUILT_IN_SYNC_LOCK_RELEASE_8 : bytes = 8; break; case BUILT_IN_ATOMIC_LOAD_8: pvalid_models = load_models; sucs_arg = 1; case BUILT_IN_ATOMIC_STORE_8: if (!pvalid_models) pvalid_models = store_models; case BUILT_IN_ATOMIC_ADD_FETCH_8 : case BUILT_IN_ATOMIC_SUB_FETCH_8: case BUILT_IN_ATOMIC_AND_FETCH_8 : case BUILT_IN_ATOMIC_NAND_FETCH_8: case BUILT_IN_ATOMIC_XOR_FETCH_8 : case BUILT_IN_ATOMIC_OR_FETCH_8: case BUILT_IN_ATOMIC_FETCH_ADD_8 : case BUILT_IN_ATOMIC_FETCH_SUB_8: case BUILT_IN_ATOMIC_FETCH_AND_8 : case BUILT_IN_ATOMIC_FETCH_NAND_8: case BUILT_IN_ATOMIC_FETCH_OR_8 : case BUILT_IN_ATOMIC_FETCH_XOR_8: bytes = 8; if (sucs_arg == (2147483647 *2U +1U)) sucs_arg = 2; if (!pvalid_models) pvalid_models = all_models; break; case BUILT_IN_ATOMIC_EXCHANGE_8: bytes = 8; sucs_arg = 3; pvalid_models = xchg_models; break; case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8 : bytes = 8; sucs_arg = 4; fail_arg = 5; pvalid_models = all_models ; arg2 = 1; | |||
3095 | break; | |||
3096 | case BUILTIN_ACCESS_SIZE_FNSPEC (16)BUILT_IN_SYNC_FETCH_AND_ADD_16: case BUILT_IN_SYNC_FETCH_AND_SUB_16 : case BUILT_IN_SYNC_FETCH_AND_OR_16: case BUILT_IN_SYNC_FETCH_AND_AND_16 : case BUILT_IN_SYNC_FETCH_AND_XOR_16: case BUILT_IN_SYNC_FETCH_AND_NAND_16 : case BUILT_IN_SYNC_ADD_AND_FETCH_16: case BUILT_IN_SYNC_SUB_AND_FETCH_16 : case BUILT_IN_SYNC_OR_AND_FETCH_16: case BUILT_IN_SYNC_AND_AND_FETCH_16 : case BUILT_IN_SYNC_XOR_AND_FETCH_16: case BUILT_IN_SYNC_NAND_AND_FETCH_16 : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16: case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16 : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16: case BUILT_IN_SYNC_LOCK_RELEASE_16 : bytes = 16; break; case BUILT_IN_ATOMIC_LOAD_16: pvalid_models = load_models; sucs_arg = 1; case BUILT_IN_ATOMIC_STORE_16: if (!pvalid_models) pvalid_models = store_models; case BUILT_IN_ATOMIC_ADD_FETCH_16 : case BUILT_IN_ATOMIC_SUB_FETCH_16: case BUILT_IN_ATOMIC_AND_FETCH_16 : case BUILT_IN_ATOMIC_NAND_FETCH_16: case BUILT_IN_ATOMIC_XOR_FETCH_16 : case BUILT_IN_ATOMIC_OR_FETCH_16: case BUILT_IN_ATOMIC_FETCH_ADD_16 : case BUILT_IN_ATOMIC_FETCH_SUB_16: case BUILT_IN_ATOMIC_FETCH_AND_16 : case BUILT_IN_ATOMIC_FETCH_NAND_16: case BUILT_IN_ATOMIC_FETCH_OR_16 : case BUILT_IN_ATOMIC_FETCH_XOR_16: bytes = 16; if (sucs_arg == (2147483647 *2U +1U)) sucs_arg = 2; if (!pvalid_models) pvalid_models = all_models; break; case BUILT_IN_ATOMIC_EXCHANGE_16: bytes = 16; sucs_arg = 3; pvalid_models = xchg_models; break; case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16: bytes = 16; sucs_arg = 4 ; fail_arg = 5; pvalid_models = all_models; arg2 = 1; | |||
3097 | break; | |||
3098 | ||||
3099 | case BUILT_IN_ATOMIC_CLEAR: | |||
3100 | sucs_arg = 1; | |||
3101 | pvalid_models = flag_clr_models; | |||
3102 | break; | |||
3103 | ||||
3104 | default: | |||
3105 | return false; | |||
3106 | } | |||
3107 | ||||
3108 | unsigned nargs = gimple_call_num_args (stmt); | |||
3109 | if (sucs_arg < nargs) | |||
3110 | { | |||
3111 | tree ord_sucs = gimple_call_arg (stmt, sucs_arg); | |||
3112 | tree ord_fail = NULL_TREE(tree) nullptr; | |||
3113 | if (fail_arg < nargs) | |||
3114 | ord_fail = gimple_call_arg (stmt, fail_arg); | |||
3115 | check_atomic_memmodel (stmt, ord_sucs, ord_fail, pvalid_models); | |||
3116 | } | |||
3117 | ||||
3118 | if (!bytes) | |||
3119 | return true; | |||
3120 | ||||
3121 | tree size = build_int_cstu (sizetypesizetype_tab[(int) stk_sizetype], bytes); | |||
3122 | tree dst = gimple_call_arg (stmt, 0); | |||
3123 | check_memop_access (stmt, dst, NULL_TREE(tree) nullptr, size); | |||
3124 | ||||
3125 | if (arg2 != UINT_MAX(2147483647 *2U +1U)) | |||
3126 | { | |||
3127 | tree dst = gimple_call_arg (stmt, arg2); | |||
3128 | check_memop_access (stmt, dst, NULL_TREE(tree) nullptr, size); | |||
3129 | } | |||
3130 | ||||
3131 | return true; | |||
3132 | } | |||
3133 | ||||
3134 | /* Check call STMT to a built-in function for invalid accesses. Return | |||
3135 | true if a call has been handled. */ | |||
3136 | ||||
3137 | bool | |||
3138 | pass_waccess::check_builtin (gcall *stmt) | |||
3139 | { | |||
3140 | tree callee = gimple_call_fndecl (stmt); | |||
3141 | if (!callee) | |||
3142 | return false; | |||
3143 | ||||
3144 | switch (DECL_FUNCTION_CODE (callee)) | |||
3145 | { | |||
3146 | case BUILT_IN_ALLOCA: | |||
3147 | case BUILT_IN_ALLOCA_WITH_ALIGN: | |||
3148 | case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX: | |||
3149 | check_alloca (stmt); | |||
3150 | return true; | |||
3151 | ||||
3152 | case BUILT_IN_EXECL: | |||
3153 | case BUILT_IN_EXECLE: | |||
3154 | case BUILT_IN_EXECLP: | |||
3155 | case BUILT_IN_EXECV: | |||
3156 | case BUILT_IN_EXECVE: | |||
3157 | case BUILT_IN_EXECVP: | |||
3158 | check_read_access (stmt, call_arg (stmt, 0)); | |||
3159 | return true; | |||
3160 | ||||
3161 | case BUILT_IN_FREE: | |||
3162 | case BUILT_IN_REALLOC: | |||
3163 | if (!m_early_checks_p) | |||
3164 | { | |||
3165 | tree arg = call_arg (stmt, 0); | |||
3166 | if (TREE_CODE (arg)((enum tree_code) (arg)->base.code) == SSA_NAME) | |||
3167 | check_pointer_uses (stmt, arg); | |||
3168 | } | |||
3169 | return true; | |||
3170 | ||||
3171 | case BUILT_IN_GETTEXT: | |||
3172 | case BUILT_IN_PUTS: | |||
3173 | case BUILT_IN_PUTS_UNLOCKED: | |||
3174 | case BUILT_IN_STRDUP: | |||
3175 | check_read_access (stmt, call_arg (stmt, 0)); | |||
3176 | return true; | |||
3177 | ||||
3178 | case BUILT_IN_INDEX: | |||
3179 | case BUILT_IN_RINDEX: | |||
3180 | case BUILT_IN_STRCHR: | |||
3181 | case BUILT_IN_STRRCHR: | |||
3182 | case BUILT_IN_STRLEN: | |||
3183 | check_read_access (stmt, call_arg (stmt, 0)); | |||
3184 | return true; | |||
3185 | ||||
3186 | case BUILT_IN_FPUTS: | |||
3187 | case BUILT_IN_FPUTS_UNLOCKED: | |||
3188 | check_read_access (stmt, call_arg (stmt, 0)); | |||
3189 | return true; | |||
3190 | ||||
3191 | case BUILT_IN_STRNDUP: | |||
3192 | case BUILT_IN_STRNLEN: | |||
3193 | { | |||
3194 | tree str = call_arg (stmt, 0); | |||
3195 | tree len = call_arg (stmt, 1); | |||
3196 | check_read_access (stmt, str, len); | |||
3197 | return true; | |||
3198 | } | |||
3199 | ||||
3200 | case BUILT_IN_STRCAT: | |||
3201 | check_strcat (stmt); | |||
3202 | return true; | |||
3203 | ||||
3204 | case BUILT_IN_STRNCAT: | |||
3205 | check_strncat (stmt); | |||
3206 | return true; | |||
3207 | ||||
3208 | case BUILT_IN_STPCPY: | |||
3209 | case BUILT_IN_STRCPY: | |||
3210 | check_stxcpy (stmt); | |||
3211 | return true; | |||
3212 | ||||
3213 | case BUILT_IN_STPNCPY: | |||
3214 | case BUILT_IN_STRNCPY: | |||
3215 | check_stxncpy (stmt); | |||
3216 | return true; | |||
3217 | ||||
3218 | case BUILT_IN_STRCASECMP: | |||
3219 | case BUILT_IN_STRCMP: | |||
3220 | case BUILT_IN_STRPBRK: | |||
3221 | case BUILT_IN_STRSPN: | |||
3222 | case BUILT_IN_STRCSPN: | |||
3223 | case BUILT_IN_STRSTR: | |||
3224 | check_read_access (stmt, call_arg (stmt, 0)); | |||
3225 | check_read_access (stmt, call_arg (stmt, 1)); | |||
3226 | return true; | |||
3227 | ||||
3228 | case BUILT_IN_STRNCASECMP: | |||
3229 | case BUILT_IN_STRNCMP: | |||
3230 | check_strncmp (stmt); | |||
3231 | return true; | |||
3232 | ||||
3233 | case BUILT_IN_MEMCMP: | |||
3234 | { | |||
3235 | tree a1 = call_arg (stmt, 0); | |||
3236 | tree a2 = call_arg (stmt, 1); | |||
3237 | tree len = call_arg (stmt, 2); | |||
3238 | check_read_access (stmt, a1, len, 0); | |||
3239 | check_read_access (stmt, a2, len, 0); | |||
3240 | return true; | |||
3241 | } | |||
3242 | ||||
3243 | case BUILT_IN_MEMCPY: | |||
3244 | case BUILT_IN_MEMPCPY: | |||
3245 | case BUILT_IN_MEMMOVE: | |||
3246 | { | |||
3247 | tree dst = call_arg (stmt, 0); | |||
3248 | tree src = call_arg (stmt, 1); | |||
3249 | tree len = call_arg (stmt, 2); | |||
3250 | check_memop_access (stmt, dst, src, len); | |||
3251 | return true; | |||
3252 | } | |||
3253 | ||||
3254 | case BUILT_IN_MEMCHR: | |||
3255 | { | |||
3256 | tree src = call_arg (stmt, 0); | |||
3257 | tree len = call_arg (stmt, 2); | |||
3258 | check_read_access (stmt, src, len, 0); | |||
3259 | return true; | |||
3260 | } | |||
3261 | ||||
3262 | case BUILT_IN_MEMSET: | |||
3263 | { | |||
3264 | tree dst = call_arg (stmt, 0); | |||
3265 | tree len = call_arg (stmt, 2); | |||
3266 | check_memop_access (stmt, dst, NULL_TREE(tree) nullptr, len); | |||
3267 | return true; | |||
3268 | } | |||
3269 | ||||
3270 | default: | |||
3271 | if (check_atomic_builtin (stmt)) | |||
3272 | return true; | |||
3273 | break; | |||
3274 | } | |||
3275 | ||||
3276 | return false; | |||
3277 | } | |||
3278 | ||||
3279 | /* Returns the type of the argument ARGNO to function with type FNTYPE | |||
3280 | or null when the type cannot be determined or no such argument exists. */ | |||
3281 | ||||
3282 | static tree | |||
3283 | fntype_argno_type (tree fntype, unsigned argno) | |||
3284 | { | |||
3285 | if (!prototype_p (fntype)) | |||
3286 | return NULL_TREE(tree) nullptr; | |||
3287 | ||||
3288 | tree argtype; | |||
3289 | function_args_iterator it; | |||
3290 | FOREACH_FUNCTION_ARGS (fntype, argtype, it)for (function_args_iter_init (&(it), (fntype)); (argtype = function_args_iter_cond (&(it))) != (tree) nullptr; function_args_iter_next (&(it))) | |||
3291 | if (argno-- == 0) | |||
3292 | return argtype; | |||
3293 | ||||
3294 | return NULL_TREE(tree) nullptr; | |||
3295 | } | |||
3296 | ||||
3297 | /* Helper to append the "human readable" attribute access specification | |||
3298 | described by ACCESS to the array ATTRSTR with size STRSIZE. Used in | |||
3299 | diagnostics. */ | |||
3300 | ||||
3301 | static inline void | |||
3302 | append_attrname (const std::pair<int, attr_access> &access, | |||
3303 | char *attrstr, size_t strsize) | |||
3304 | { | |||
3305 | if (access.second.internal_p) | |||
3306 | return; | |||
3307 | ||||
3308 | tree str = access.second.to_external_string (); | |||
3309 | gcc_assert (strsize >= (size_t) TREE_STRING_LENGTH (str))((void)(!(strsize >= (size_t) ((tree_check ((str), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3309, __FUNCTION__, (STRING_CST)))->string.length)) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3309, __FUNCTION__), 0 : 0)); | |||
3310 | strcpy (attrstr, TREE_STRING_POINTER (str)((const char *)((tree_check ((str), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3310, __FUNCTION__, (STRING_CST)))->string.str))); | |||
3311 | } | |||
3312 | ||||
3313 | /* Iterate over attribute access read-only, read-write, and write-only | |||
3314 | arguments and diagnose past-the-end accesses and related problems | |||
3315 | in the function call EXP. */ | |||
3316 | ||||
3317 | void | |||
3318 | pass_waccess::maybe_check_access_sizes (rdwr_map *rwm, tree fndecl, tree fntype, | |||
3319 | gimple *stmt) | |||
3320 | { | |||
3321 | if (warning_suppressed_p (stmt, OPT_Wnonnull) | |||
3322 | || warning_suppressed_p (stmt, OPT_Wstringop_overflow_)) | |||
3323 | return; | |||
3324 | ||||
3325 | auto_diagnostic_group adg; | |||
3326 | ||||
3327 | /* Set if a warning has been issued for any argument (used to decide | |||
3328 | whether to emit an informational note at the end). */ | |||
3329 | opt_code opt_warned = no_warning; | |||
3330 | ||||
3331 | /* A string describing the attributes that the warnings issued by this | |||
3332 | function apply to. Used to print one informational note per function | |||
3333 | call, rather than one per warning. That reduces clutter. */ | |||
3334 | char attrstr[80]; | |||
3335 | attrstr[0] = 0; | |||
3336 | ||||
3337 | for (rdwr_map::iterator it = rwm->begin (); it != rwm->end (); ++it) | |||
3338 | { | |||
3339 | std::pair<int, attr_access> access = *it; | |||
3340 | ||||
3341 | /* Get the function call arguments corresponding to the attribute's | |||
3342 | positional arguments. When both arguments have been specified | |||
3343 | there will be two entries in *RWM, one for each. They are | |||
3344 | cross-referenced by their respective argument numbers in | |||
3345 | ACCESS.PTRARG and ACCESS.SIZARG. */ | |||
3346 | const int ptridx = access.second.ptrarg; | |||
3347 | const int sizidx = access.second.sizarg; | |||
3348 | ||||
3349 | gcc_assert (ptridx != -1)((void)(!(ptridx != -1) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3349, __FUNCTION__), 0 : 0)); | |||
3350 | gcc_assert (access.first == ptridx || access.first == sizidx)((void)(!(access.first == ptridx || access.first == sizidx) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3350, __FUNCTION__), 0 : 0)); | |||
3351 | ||||
3352 | /* The pointer is set to null for the entry corresponding to | |||
3353 | the size argument. Skip it. It's handled when the entry | |||
3354 | corresponding to the pointer argument comes up. */ | |||
3355 | if (!access.second.ptr) | |||
3356 | continue; | |||
3357 | ||||
3358 | tree ptrtype = fntype_argno_type (fntype, ptridx); | |||
3359 | if (!ptrtype) | |||
3360 | /* A function with a prototype was redeclared without one and | |||
3361 | the prototype has been lost. See pr102759. Avoid dealing | |||
3362 | with this pathological case. */ | |||
3363 | return; | |||
3364 | ||||
3365 | tree argtype = TREE_TYPE (ptrtype)((contains_struct_check ((ptrtype), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3365, __FUNCTION__))->typed.type); | |||
3366 | ||||
3367 | /* The size of the access by the call in elements. */ | |||
3368 | tree access_nelts; | |||
3369 | if (sizidx == -1) | |||
3370 | { | |||
3371 | /* If only the pointer attribute operand was specified and | |||
3372 | not size, set SIZE to the greater of MINSIZE or size of | |||
3373 | one element of the pointed to type to detect smaller | |||
3374 | objects (null pointers are diagnosed in this case only | |||
3375 | if the pointer is also declared with attribute nonnull. */ | |||
3376 | if (access.second.minsize | |||
3377 | && access.second.minsize != HOST_WIDE_INT_M1U-1UL) | |||
3378 | access_nelts = build_int_cstu (sizetypesizetype_tab[(int) stk_sizetype], access.second.minsize); | |||
3379 | else if (VOID_TYPE_P (argtype)(((enum tree_code) (argtype)->base.code) == VOID_TYPE) && access.second.mode == access_none) | |||
3380 | /* Treat access mode none on a void* argument as expecting | |||
3381 | as little as zero bytes. */ | |||
3382 | access_nelts = size_zero_nodeglobal_trees[TI_SIZE_ZERO]; | |||
3383 | else | |||
3384 | access_nelts = size_one_nodeglobal_trees[TI_SIZE_ONE]; | |||
3385 | } | |||
3386 | else | |||
3387 | access_nelts = rwm->get (sizidx)->size; | |||
3388 | ||||
3389 | /* Format the value or range to avoid an explosion of messages. */ | |||
3390 | char sizstr[80]; | |||
3391 | tree sizrng[2] = { size_zero_nodeglobal_trees[TI_SIZE_ZERO], build_all_ones_cst (sizetypesizetype_tab[(int) stk_sizetype]) }; | |||
3392 | if (get_size_range (m_ptr_qry.rvals, access_nelts, stmt, sizrng, 1)) | |||
3393 | { | |||
3394 | char *s0 = print_generic_expr_to_str (sizrng[0]); | |||
3395 | if (tree_int_cst_equal (sizrng[0], sizrng[1])) | |||
3396 | { | |||
3397 | gcc_checking_assert (strlen (s0) < sizeof sizstr)((void)(!(strlen (s0) < sizeof sizstr) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3397, __FUNCTION__), 0 : 0)); | |||
3398 | strcpy (sizstr, s0); | |||
3399 | } | |||
3400 | else | |||
3401 | { | |||
3402 | char *s1 = print_generic_expr_to_str (sizrng[1]); | |||
3403 | gcc_checking_assert (strlen (s0) + strlen (s1)((void)(!(strlen (s0) + strlen (s1) < sizeof sizstr - 4) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3404, __FUNCTION__), 0 : 0)) | |||
3404 | < sizeof sizstr - 4)((void)(!(strlen (s0) + strlen (s1) < sizeof sizstr - 4) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3404, __FUNCTION__), 0 : 0)); | |||
3405 | sprintf (sizstr, "[%.37s, %.37s]", s0, s1); | |||
3406 | free (s1); | |||
3407 | } | |||
3408 | free (s0); | |||
3409 | } | |||
3410 | else | |||
3411 | *sizstr = '\0'; | |||
3412 | ||||
3413 | /* Set if a warning has been issued for the current argument. */ | |||
3414 | opt_code arg_warned = no_warning; | |||
3415 | location_t loc = get_location (stmt); | |||
3416 | tree ptr = access.second.ptr; | |||
3417 | if (*sizstr | |||
3418 | && tree_int_cst_sgn (sizrng[0]) < 0 | |||
3419 | && tree_int_cst_sgn (sizrng[1]) < 0) | |||
3420 | { | |||
3421 | /* Warn about negative sizes. */ | |||
3422 | if (access.second.internal_p) | |||
3423 | { | |||
3424 | const std::string argtypestr | |||
3425 | = access.second.array_as_string (ptrtype); | |||
3426 | ||||
3427 | if (warning_at (loc, OPT_Wstringop_overflow_, | |||
3428 | "bound argument %i value %s is " | |||
3429 | "negative for a variable length array " | |||
3430 | "argument %i of type %s", | |||
3431 | sizidx + 1, sizstr, | |||
3432 | ptridx + 1, argtypestr.c_str ())) | |||
3433 | arg_warned = OPT_Wstringop_overflow_; | |||
3434 | } | |||
3435 | else if (warning_at (loc, OPT_Wstringop_overflow_, | |||
3436 | "argument %i value %s is negative", | |||
3437 | sizidx + 1, sizstr)) | |||
3438 | arg_warned = OPT_Wstringop_overflow_; | |||
3439 | ||||
3440 | if (arg_warned != no_warning) | |||
3441 | { | |||
3442 | append_attrname (access, attrstr, sizeof attrstr); | |||
3443 | /* Remember a warning has been issued and avoid warning | |||
3444 | again below for the same attribute. */ | |||
3445 | opt_warned = arg_warned; | |||
3446 | continue; | |||
3447 | } | |||
3448 | } | |||
3449 | ||||
3450 | /* The size of the access by the call in bytes. */ | |||
3451 | tree access_size = NULL_TREE(tree) nullptr; | |||
3452 | if (tree_int_cst_sgn (sizrng[0]) >= 0) | |||
3453 | { | |||
3454 | if (COMPLETE_TYPE_P (argtype)(((tree_class_check ((argtype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3454, __FUNCTION__))->type_common.size) != (tree) nullptr )) | |||
3455 | { | |||
3456 | /* Multiply ACCESS_SIZE by the size of the type the pointer | |||
3457 | argument points to. If it's incomplete the size is used | |||
3458 | as is. */ | |||
3459 | if (tree argsize = TYPE_SIZE_UNIT (argtype)((tree_class_check ((argtype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3459, __FUNCTION__))->type_common.size_unit)) | |||
3460 | if (TREE_CODE (argsize)((enum tree_code) (argsize)->base.code) == INTEGER_CST) | |||
3461 | { | |||
3462 | const int prec = TYPE_PRECISION (sizetype)((tree_class_check ((sizetype_tab[(int) stk_sizetype]), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3462, __FUNCTION__))->type_common.precision); | |||
3463 | wide_int minsize = wi::to_wide (sizrng[0], prec); | |||
3464 | minsize *= wi::to_wide (argsize, prec); | |||
3465 | access_size = wide_int_to_tree (sizetypesizetype_tab[(int) stk_sizetype], minsize); | |||
3466 | } | |||
3467 | } | |||
3468 | else | |||
3469 | access_size = access_nelts; | |||
3470 | } | |||
3471 | ||||
3472 | if (integer_zerop (ptr)) | |||
3473 | { | |||
3474 | if (sizidx >= 0 && tree_int_cst_sgn (sizrng[0]) > 0) | |||
3475 | { | |||
3476 | /* Warn about null pointers with positive sizes. This is | |||
3477 | different from also declaring the pointer argument with | |||
3478 | attribute nonnull when the function accepts null pointers | |||
3479 | only when the corresponding size is zero. */ | |||
3480 | if (access.second.internal_p) | |||
3481 | { | |||
3482 | const std::string argtypestr | |||
3483 | = access.second.array_as_string (ptrtype); | |||
3484 | ||||
3485 | if (warning_at (loc, OPT_Wnonnull, | |||
3486 | "argument %i of variable length " | |||
3487 | "array %s is null but " | |||
3488 | "the corresponding bound argument " | |||
3489 | "%i value is %s", | |||
3490 | ptridx + 1, argtypestr.c_str (), | |||
3491 | sizidx + 1, sizstr)) | |||
3492 | arg_warned = OPT_Wnonnull; | |||
3493 | } | |||
3494 | else if (warning_at (loc, OPT_Wnonnull, | |||
3495 | "argument %i is null but " | |||
3496 | "the corresponding size argument " | |||
3497 | "%i value is %s", | |||
3498 | ptridx + 1, sizidx + 1, sizstr)) | |||
3499 | arg_warned = OPT_Wnonnull; | |||
3500 | } | |||
3501 | else if (access_size && access.second.static_p) | |||
3502 | { | |||
3503 | /* Warn about null pointers for [static N] array arguments | |||
3504 | but do not warn for ordinary (i.e., nonstatic) arrays. */ | |||
3505 | if (warning_at (loc, OPT_Wnonnull, | |||
3506 | "argument %i to %<%T[static %E]%> " | |||
3507 | "is null where non-null expected", | |||
3508 | ptridx + 1, argtype, access_nelts)) | |||
3509 | arg_warned = OPT_Wnonnull; | |||
3510 | } | |||
3511 | ||||
3512 | if (arg_warned != no_warning) | |||
3513 | { | |||
3514 | append_attrname (access, attrstr, sizeof attrstr); | |||
3515 | /* Remember a warning has been issued and avoid warning | |||
3516 | again below for the same attribute. */ | |||
3517 | opt_warned = OPT_Wnonnull; | |||
3518 | continue; | |||
3519 | } | |||
3520 | } | |||
3521 | ||||
3522 | access_data data (m_ptr_qry.rvals, stmt, access.second.mode, | |||
3523 | NULL_TREE(tree) nullptr, false, NULL_TREE(tree) nullptr, false); | |||
3524 | access_ref* const pobj = (access.second.mode == access_write_only | |||
3525 | ? &data.dst : &data.src); | |||
3526 | tree objsize = compute_objsize (ptr, stmt, 1, pobj, &m_ptr_qry); | |||
3527 | ||||
3528 | /* The size of the destination or source object. */ | |||
3529 | tree dstsize = NULL_TREE(tree) nullptr, srcsize = NULL_TREE(tree) nullptr; | |||
3530 | if (access.second.mode == access_read_only | |||
3531 | || access.second.mode == access_none) | |||
3532 | { | |||
3533 | /* For a read-only argument there is no destination. For | |||
3534 | no access, set the source as well and differentiate via | |||
3535 | the access flag below. */ | |||
3536 | srcsize = objsize; | |||
3537 | if (access.second.mode == access_read_only | |||
3538 | || access.second.mode == access_none) | |||
3539 | { | |||
3540 | /* For a read-only attribute there is no destination so | |||
3541 | clear OBJSIZE. This emits "reading N bytes" kind of | |||
3542 | diagnostics instead of the "writing N bytes" kind, | |||
3543 | unless MODE is none. */ | |||
3544 | objsize = NULL_TREE(tree) nullptr; | |||
3545 | } | |||
3546 | } | |||
3547 | else | |||
3548 | dstsize = objsize; | |||
3549 | ||||
3550 | /* Clear the no-warning bit in case it was set by check_access | |||
3551 | in a prior iteration so that accesses via different arguments | |||
3552 | are diagnosed. */ | |||
3553 | suppress_warning (stmt, OPT_Wstringop_overflow_, false); | |||
3554 | access_mode mode = data.mode; | |||
3555 | if (mode == access_deferred) | |||
3556 | mode = TYPE_READONLY (argtype)((tree_class_check ((argtype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3556, __FUNCTION__))->base.readonly_flag) ? access_read_only : access_read_write; | |||
3557 | check_access (stmt, access_size, /*maxread=*/ NULL_TREE(tree) nullptr, srcsize, | |||
3558 | dstsize, mode, &data, m_ptr_qry.rvals); | |||
3559 | ||||
3560 | if (warning_suppressed_p (stmt, OPT_Wstringop_overflow_)) | |||
3561 | opt_warned = OPT_Wstringop_overflow_; | |||
3562 | if (opt_warned != no_warning) | |||
3563 | { | |||
3564 | if (access.second.internal_p) | |||
3565 | { | |||
3566 | unsigned HOST_WIDE_INTlong nelts = | |||
3567 | access_nelts ? access.second.minsize : HOST_WIDE_INT_M1U-1UL; | |||
3568 | tree arrtype = build_printable_array_type (argtype, nelts); | |||
3569 | inform (loc, "referencing argument %u of type %qT", | |||
3570 | ptridx + 1, arrtype); | |||
3571 | } | |||
3572 | else | |||
3573 | /* If check_access issued a warning above, append the relevant | |||
3574 | attribute to the string. */ | |||
3575 | append_attrname (access, attrstr, sizeof attrstr); | |||
3576 | } | |||
3577 | } | |||
3578 | ||||
3579 | if (*attrstr) | |||
3580 | { | |||
3581 | if (fndecl) | |||
3582 | inform (get_location (fndecl), | |||
3583 | "in a call to function %qD declared with attribute %qs", | |||
3584 | fndecl, attrstr); | |||
3585 | else | |||
3586 | inform (get_location (stmt), | |||
3587 | "in a call with type %qT and attribute %qs", | |||
3588 | fntype, attrstr); | |||
3589 | } | |||
3590 | else if (opt_warned != no_warning) | |||
3591 | { | |||
3592 | if (fndecl) | |||
3593 | inform (get_location (fndecl), | |||
3594 | "in a call to function %qD", fndecl); | |||
3595 | else | |||
3596 | inform (get_location (stmt), | |||
3597 | "in a call with type %qT", fntype); | |||
3598 | } | |||
3599 | ||||
3600 | /* Set the bit in case it was cleared and not set above. */ | |||
3601 | if (opt_warned != no_warning) | |||
3602 | suppress_warning (stmt, opt_warned); | |||
3603 | } | |||
3604 | ||||
3605 | /* Check call STMT to an ordinary (non-built-in) function for invalid | |||
3606 | accesses. Return true if a call has been handled. */ | |||
3607 | ||||
3608 | bool | |||
3609 | pass_waccess::check_call_access (gcall *stmt) | |||
3610 | { | |||
3611 | tree fntype = gimple_call_fntype (stmt); | |||
3612 | if (!fntype) | |||
3613 | return false; | |||
3614 | ||||
3615 | tree fntypeattrs = TYPE_ATTRIBUTES (fntype)((tree_class_check ((fntype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3615, __FUNCTION__))->type_common.attributes); | |||
3616 | if (!fntypeattrs) | |||
3617 | return false; | |||
3618 | ||||
3619 | /* Map of attribute access specifications for function arguments. */ | |||
3620 | rdwr_map rdwr_idx; | |||
3621 | init_attr_rdwr_indices (&rdwr_idx, fntypeattrs); | |||
3622 | ||||
3623 | unsigned nargs = call_nargs (stmt); | |||
3624 | for (unsigned i = 0; i != nargs; ++i) | |||
3625 | { | |||
3626 | tree arg = call_arg (stmt, i); | |||
3627 | ||||
3628 | /* Save the actual argument that corresponds to the access attribute | |||
3629 | operand for later processing. */ | |||
3630 | if (attr_access *access = rdwr_idx.get (i)) | |||
3631 | { | |||
3632 | if (POINTER_TYPE_P (TREE_TYPE (arg))(((enum tree_code) (((contains_struct_check ((arg), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3632, __FUNCTION__))->typed.type))->base.code) == POINTER_TYPE || ((enum tree_code) (((contains_struct_check ((arg), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3632, __FUNCTION__))->typed.type))->base.code) == REFERENCE_TYPE )) | |||
3633 | { | |||
3634 | access->ptr = arg; | |||
3635 | /* A nonnull ACCESS->SIZE contains VLA bounds. */ | |||
3636 | } | |||
3637 | else | |||
3638 | { | |||
3639 | access->size = arg; | |||
3640 | gcc_assert (access->ptr == NULL_TREE)((void)(!(access->ptr == (tree) nullptr) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3640, __FUNCTION__), 0 : 0)); | |||
3641 | } | |||
3642 | } | |||
3643 | } | |||
3644 | ||||
3645 | /* Check attribute access arguments. */ | |||
3646 | tree fndecl = gimple_call_fndecl (stmt); | |||
3647 | maybe_check_access_sizes (&rdwr_idx, fndecl, fntype, stmt); | |||
3648 | ||||
3649 | check_alloc_size_call (stmt); | |||
3650 | return true; | |||
3651 | } | |||
3652 | ||||
3653 | /* Check arguments in a call STMT for attribute nonstring. */ | |||
3654 | ||||
3655 | static void | |||
3656 | check_nonstring_args (gcall *stmt) | |||
3657 | { | |||
3658 | tree fndecl = gimple_call_fndecl (stmt); | |||
3659 | ||||
3660 | /* Detect passing non-string arguments to functions expecting | |||
3661 | nul-terminated strings. */ | |||
3662 | maybe_warn_nonstring_arg (fndecl, stmt); | |||
3663 | } | |||
3664 | ||||
3665 | /* Issue a warning if a deallocation function such as free, realloc, | |||
3666 | or C++ operator delete is called with an argument not returned by | |||
3667 | a matching allocation function such as malloc or the corresponding | |||
3668 | form of C++ operator new. */ | |||
3669 | ||||
3670 | void | |||
3671 | pass_waccess::maybe_check_dealloc_call (gcall *call) | |||
3672 | { | |||
3673 | tree fndecl = gimple_call_fndecl (call); | |||
3674 | if (!fndecl) | |||
3675 | return; | |||
3676 | ||||
3677 | unsigned argno = fndecl_dealloc_argno (fndecl); | |||
3678 | if ((unsigned) call_nargs (call) <= argno) | |||
3679 | return; | |||
3680 | ||||
3681 | tree ptr = gimple_call_arg (call, argno); | |||
3682 | if (integer_zerop (ptr)) | |||
3683 | return; | |||
3684 | ||||
3685 | access_ref aref; | |||
3686 | if (!compute_objsize (ptr, call, 0, &aref, &m_ptr_qry)) | |||
3687 | return; | |||
3688 | ||||
3689 | tree ref = aref.ref; | |||
3690 | if (integer_zerop (ref)) | |||
3691 | return; | |||
3692 | ||||
3693 | tree dealloc_decl = fndecl; | |||
3694 | location_t loc = gimple_location (call); | |||
3695 | ||||
3696 | if (DECL_P (ref)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (ref)->base.code))] == tcc_declaration) || EXPR_P (ref)((tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) (ref)->base.code))]) >= tcc_reference && (tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) (ref)->base.code))]) <= tcc_expression)) | |||
3697 | { | |||
3698 | /* Diagnose freeing a declared object. */ | |||
3699 | if (aref.ref_declared ()) | |||
3700 | { | |||
3701 | auto_diagnostic_group d; | |||
3702 | if (warning_at (loc, OPT_Wfree_nonheap_object, | |||
3703 | "%qD called on unallocated object %qD", | |||
3704 | dealloc_decl, ref)) | |||
3705 | { | |||
3706 | inform (get_location (ref), "declared here"); | |||
3707 | return; | |||
3708 | } | |||
3709 | } | |||
3710 | ||||
3711 | /* Diagnose freeing a pointer that includes a positive offset. | |||
3712 | Such a pointer cannot refer to the beginning of an allocated | |||
3713 | object. A negative offset may refer to it. */ | |||
3714 | if (aref.sizrng[0] != aref.sizrng[1] | |||
3715 | && warn_dealloc_offset (loc, call, aref)) | |||
3716 | return; | |||
3717 | } | |||
3718 | else if (CONSTANT_CLASS_P (ref)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (ref)->base.code))] == tcc_constant)) | |||
3719 | { | |||
3720 | auto_diagnostic_group d; | |||
3721 | if (warning_at (loc, OPT_Wfree_nonheap_object, | |||
3722 | "%qD called on a pointer to an unallocated " | |||
3723 | "object %qE", dealloc_decl, ref)) | |||
3724 | { | |||
3725 | if (TREE_CODE (ptr)((enum tree_code) (ptr)->base.code) == SSA_NAME) | |||
3726 | { | |||
3727 | gimple *def_stmt = SSA_NAME_DEF_STMT (ptr)(tree_check ((ptr), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3727, __FUNCTION__, (SSA_NAME)))->ssa_name.def_stmt; | |||
3728 | if (is_gimple_assign (def_stmt)) | |||
3729 | { | |||
3730 | location_t loc = gimple_location (def_stmt); | |||
3731 | inform (loc, "assigned here"); | |||
3732 | } | |||
3733 | } | |||
3734 | return; | |||
3735 | } | |||
3736 | } | |||
3737 | else if (TREE_CODE (ref)((enum tree_code) (ref)->base.code) == SSA_NAME) | |||
3738 | { | |||
3739 | /* Also warn if the pointer argument refers to the result | |||
3740 | of an allocation call like alloca or VLA. */ | |||
3741 | gimple *def_stmt = SSA_NAME_DEF_STMT (ref)(tree_check ((ref), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3741, __FUNCTION__, (SSA_NAME)))->ssa_name.def_stmt; | |||
3742 | if (!def_stmt) | |||
3743 | return; | |||
3744 | ||||
3745 | if (is_gimple_call (def_stmt)) | |||
3746 | { | |||
3747 | bool warned = false; | |||
3748 | if (gimple_call_alloc_p (def_stmt)) | |||
3749 | { | |||
3750 | if (matching_alloc_calls_p (def_stmt, dealloc_decl)) | |||
3751 | { | |||
3752 | if (warn_dealloc_offset (loc, call, aref)) | |||
3753 | return; | |||
3754 | } | |||
3755 | else | |||
3756 | { | |||
3757 | tree alloc_decl = gimple_call_fndecl (def_stmt); | |||
3758 | const opt_code opt = | |||
3759 | (DECL_IS_OPERATOR_NEW_P (alloc_decl)(((tree_check ((alloc_decl), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3759, __FUNCTION__, (FUNCTION_DECL)))->function_decl.decl_type ) == OPERATOR_NEW) | |||
3760 | || DECL_IS_OPERATOR_DELETE_P (dealloc_decl)(((tree_check ((dealloc_decl), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3760, __FUNCTION__, (FUNCTION_DECL)))->function_decl.decl_type ) == OPERATOR_DELETE) | |||
3761 | ? OPT_Wmismatched_new_delete | |||
3762 | : OPT_Wmismatched_dealloc); | |||
3763 | warned = warning_at (loc, opt, | |||
3764 | "%qD called on pointer returned " | |||
3765 | "from a mismatched allocation " | |||
3766 | "function", dealloc_decl); | |||
3767 | } | |||
3768 | } | |||
3769 | else if (gimple_call_builtin_p (def_stmt, BUILT_IN_ALLOCA) | |||
3770 | || gimple_call_builtin_p (def_stmt, | |||
3771 | BUILT_IN_ALLOCA_WITH_ALIGN)) | |||
3772 | warned = warning_at (loc, OPT_Wfree_nonheap_object, | |||
3773 | "%qD called on pointer to " | |||
3774 | "an unallocated object", | |||
3775 | dealloc_decl); | |||
3776 | else if (warn_dealloc_offset (loc, call, aref)) | |||
3777 | return; | |||
3778 | ||||
3779 | if (warned) | |||
3780 | { | |||
3781 | tree fndecl = gimple_call_fndecl (def_stmt); | |||
3782 | inform (gimple_location (def_stmt), | |||
3783 | "returned from %qD", fndecl); | |||
3784 | return; | |||
3785 | } | |||
3786 | } | |||
3787 | else if (gimple_nop_p (def_stmt)) | |||
3788 | { | |||
3789 | ref = SSA_NAME_VAR (ref)((tree_check ((ref), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3789, __FUNCTION__, (SSA_NAME)))->ssa_name.var == (tree) nullptr || ((enum tree_code) ((ref)->ssa_name.var)->base .code) == IDENTIFIER_NODE ? (tree) nullptr : (ref)->ssa_name .var); | |||
3790 | /* Diagnose freeing a pointer that includes a positive offset. */ | |||
3791 | if (TREE_CODE (ref)((enum tree_code) (ref)->base.code) == PARM_DECL | |||
3792 | && !aref.deref | |||
3793 | && aref.sizrng[0] != aref.sizrng[1] | |||
3794 | && aref.offrng[0] > 0 && aref.offrng[1] > 0 | |||
3795 | && warn_dealloc_offset (loc, call, aref)) | |||
3796 | return; | |||
3797 | } | |||
3798 | } | |||
3799 | } | |||
3800 | ||||
3801 | /* Return true if either USE_STMT's basic block (that of a pointer's use) | |||
3802 | is dominated by INVAL_STMT's (that of a pointer's invalidating statement, | |||
3803 | which is either a clobber or a deallocation call), or if they're in | |||
3804 | the same block, USE_STMT follows INVAL_STMT. */ | |||
3805 | ||||
3806 | bool | |||
3807 | pass_waccess::use_after_inval_p (gimple *inval_stmt, gimple *use_stmt, | |||
3808 | bool last_block /* = false */) | |||
3809 | { | |||
3810 | tree clobvar = | |||
3811 | gimple_clobber_p (inval_stmt) ? gimple_assign_lhs (inval_stmt) : NULL_TREE(tree) nullptr; | |||
3812 | ||||
3813 | basic_block inval_bb = gimple_bb (inval_stmt); | |||
3814 | basic_block use_bb = gimple_bb (use_stmt); | |||
3815 | ||||
3816 | if (!inval_bb || !use_bb) | |||
3817 | return false; | |||
3818 | ||||
3819 | if (inval_bb != use_bb) | |||
3820 | { | |||
3821 | if (dominated_by_p (CDI_DOMINATORS, use_bb, inval_bb)) | |||
3822 | return true; | |||
3823 | ||||
3824 | if (!clobvar || !last_block) | |||
3825 | return false; | |||
3826 | ||||
3827 | /* Proceed only when looking for uses of dangling pointers. */ | |||
3828 | auto gsi = gsi_for_stmt (use_stmt); | |||
3829 | ||||
3830 | /* A use statement in the last basic block in a function or one that | |||
3831 | falls through to it is after any other prior clobber of the used | |||
3832 | variable unless it's followed by a clobber of the same variable. */ | |||
3833 | basic_block bb = use_bb; | |||
3834 | while (bb != inval_bb | |||
3835 | && single_succ_p (bb) | |||
3836 | && !(single_succ_edge (bb)->flags | |||
3837 | & (EDGE_EH | EDGE_ABNORMAL | EDGE_DFS_BACK))) | |||
3838 | { | |||
3839 | for (; !gsi_end_p (gsi); gsi_next_nondebug (&gsi)) | |||
3840 | { | |||
3841 | gimple *stmt = gsi_stmt (gsi); | |||
3842 | if (gimple_clobber_p (stmt)) | |||
3843 | { | |||
3844 | if (clobvar == gimple_assign_lhs (stmt)) | |||
3845 | /* The use is followed by a clobber. */ | |||
3846 | return false; | |||
3847 | } | |||
3848 | } | |||
3849 | ||||
3850 | bb = single_succ (bb); | |||
3851 | gsi = gsi_start_bb (bb); | |||
3852 | } | |||
3853 | ||||
3854 | /* The use is one of a dangling pointer if a clobber of the variable | |||
3855 | [the pointer points to] has not been found before the function exit | |||
3856 | point. */ | |||
3857 | return bb == EXIT_BLOCK_PTR_FOR_FN (cfun)(((cfun + 0))->cfg->x_exit_block_ptr); | |||
3858 | } | |||
3859 | ||||
3860 | if (bitmap_set_bit (m_bb_uids_set, inval_bb->index)) | |||
3861 | /* The first time this basic block is visited assign increasing ids | |||
3862 | to consecutive statements in it. Use the ids to determine which | |||
3863 | precedes which. This avoids the linear traversal on subsequent | |||
3864 | visits to the same block. */ | |||
3865 | renumber_gimple_stmt_uids_in_block (m_func, inval_bb); | |||
3866 | ||||
3867 | return gimple_uid (inval_stmt) < gimple_uid (use_stmt); | |||
3868 | } | |||
3869 | ||||
3870 | /* Issue a warning for the USE_STMT of pointer or reference REF rendered | |||
3871 | invalid by INVAL_STMT. REF may be null when it's been optimized away. | |||
3872 | When nonnull, INVAL_STMT is the deallocation function that rendered | |||
3873 | the pointer or reference dangling. Otherwise, VAR is the auto variable | |||
3874 | (including an unnamed temporary such as a compound literal) whose | |||
3875 | lifetime's rended it dangling. MAYBE is true to issue the "maybe" | |||
3876 | kind of warning. EQUALITY is true when the pointer is used in | |||
3877 | an equality expression. */ | |||
3878 | ||||
3879 | void | |||
3880 | pass_waccess::warn_invalid_pointer (tree ref, gimple *use_stmt, | |||
3881 | gimple *inval_stmt, tree var, | |||
3882 | bool maybe, bool equality /* = false */) | |||
3883 | { | |||
3884 | /* Avoid printing the unhelpful "<unknown>" in the diagnostics. */ | |||
3885 | if (ref && TREE_CODE (ref)((enum tree_code) (ref)->base.code) == SSA_NAME) | |||
3886 | { | |||
3887 | tree var = SSA_NAME_VAR (ref)((tree_check ((ref), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3887, __FUNCTION__, (SSA_NAME)))->ssa_name.var == (tree) nullptr || ((enum tree_code) ((ref)->ssa_name.var)->base .code) == IDENTIFIER_NODE ? (tree) nullptr : (ref)->ssa_name .var); | |||
3888 | if (!var) | |||
3889 | ref = NULL_TREE(tree) nullptr; | |||
3890 | /* Don't warn for cases like when a cdtor returns 'this' on ARM. */ | |||
3891 | else if (warning_suppressed_p (var, OPT_Wuse_after_free)) | |||
3892 | return; | |||
3893 | else if (DECL_ARTIFICIAL (var)((contains_struct_check ((var), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3893, __FUNCTION__))->decl_common.artificial_flag)) | |||
3894 | ref = NULL_TREE(tree) nullptr; | |||
3895 | } | |||
3896 | ||||
3897 | location_t use_loc = gimple_location (use_stmt); | |||
3898 | if (use_loc == UNKNOWN_LOCATION((location_t) 0)) | |||
3899 | { | |||
3900 | use_loc = m_func->function_end_locus; | |||
3901 | if (!ref) | |||
3902 | /* Avoid issuing a warning with no context other than | |||
3903 | the function. That would make it difficult to debug | |||
3904 | in any but very simple cases. */ | |||
3905 | return; | |||
3906 | } | |||
3907 | ||||
3908 | if (is_gimple_call (inval_stmt)) | |||
3909 | { | |||
3910 | if (!m_early_checks_p | |||
3911 | || (equality && warn_use_after_freeglobal_options.x_warn_use_after_free < 3) | |||
3912 | || (maybe && warn_use_after_freeglobal_options.x_warn_use_after_free < 2) | |||
3913 | || warning_suppressed_p (use_stmt, OPT_Wuse_after_free)) | |||
3914 | return; | |||
3915 | ||||
3916 | const tree inval_decl = gimple_call_fndecl (inval_stmt); | |||
3917 | ||||
3918 | auto_diagnostic_group d; | |||
3919 | if ((ref && warning_at (use_loc, OPT_Wuse_after_free, | |||
3920 | (maybe | |||
3921 | ? G_("pointer %qE may be used after %qD")"pointer %qE may be used after %qD" | |||
3922 | : G_("pointer %qE used after %qD")"pointer %qE used after %qD"), | |||
3923 | ref, inval_decl)) | |||
3924 | || (!ref && warning_at (use_loc, OPT_Wuse_after_free, | |||
3925 | (maybe | |||
3926 | ? G_("pointer may be used after %qD")"pointer may be used after %qD" | |||
3927 | : G_("pointer used after %qD")"pointer used after %qD"), | |||
3928 | inval_decl))) | |||
3929 | { | |||
3930 | location_t loc = gimple_location (inval_stmt); | |||
3931 | inform (loc, "call to %qD here", inval_decl); | |||
3932 | suppress_warning (use_stmt, OPT_Wuse_after_free); | |||
3933 | } | |||
3934 | return; | |||
3935 | } | |||
3936 | ||||
3937 | if (equality | |||
3938 | || (maybe && warn_dangling_pointerglobal_options.x_warn_dangling_pointer < 2) | |||
3939 | || warning_suppressed_p (use_stmt, OPT_Wdangling_pointer_)) | |||
3940 | return; | |||
3941 | ||||
3942 | if (DECL_NAME (var)((contains_struct_check ((var), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3942, __FUNCTION__))->decl_minimal.name)) | |||
3943 | { | |||
3944 | auto_diagnostic_group d; | |||
3945 | if ((ref | |||
3946 | && warning_at (use_loc, OPT_Wdangling_pointer_, | |||
3947 | (maybe | |||
3948 | ? G_("dangling pointer %qE to %qD may be used")"dangling pointer %qE to %qD may be used" | |||
3949 | : G_("using dangling pointer %qE to %qD")"using dangling pointer %qE to %qD"), | |||
3950 | ref, var)) | |||
3951 | || (!ref | |||
3952 | && warning_at (use_loc, OPT_Wdangling_pointer_, | |||
3953 | (maybe | |||
3954 | ? G_("dangling pointer to %qD may be used")"dangling pointer to %qD may be used" | |||
3955 | : G_("using a dangling pointer to %qD")"using a dangling pointer to %qD"), | |||
3956 | var))) | |||
3957 | inform (DECL_SOURCE_LOCATION (var)((contains_struct_check ((var), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3957, __FUNCTION__))->decl_minimal.locus), | |||
3958 | "%qD declared here", var); | |||
3959 | suppress_warning (use_stmt, OPT_Wdangling_pointer_); | |||
3960 | return; | |||
3961 | } | |||
3962 | ||||
3963 | if ((ref | |||
3964 | && warning_at (use_loc, OPT_Wdangling_pointer_, | |||
3965 | (maybe | |||
3966 | ? G_("dangling pointer %qE to an unnamed temporary ""dangling pointer %qE to an unnamed temporary " "may be used" | |||
3967 | "may be used")"dangling pointer %qE to an unnamed temporary " "may be used" | |||
3968 | : G_("using dangling pointer %qE to an unnamed ""using dangling pointer %qE to an unnamed " "temporary" | |||
3969 | "temporary")"using dangling pointer %qE to an unnamed " "temporary"), | |||
3970 | ref)) | |||
3971 | || (!ref | |||
3972 | && warning_at (use_loc, OPT_Wdangling_pointer_, | |||
3973 | (maybe | |||
3974 | ? G_("dangling pointer to an unnamed temporary ""dangling pointer to an unnamed temporary " "may be used" | |||
3975 | "may be used")"dangling pointer to an unnamed temporary " "may be used" | |||
3976 | : G_("using a dangling pointer to an unnamed ""using a dangling pointer to an unnamed " "temporary" | |||
3977 | "temporary")"using a dangling pointer to an unnamed " "temporary")))) | |||
3978 | { | |||
3979 | inform (DECL_SOURCE_LOCATION (var)((contains_struct_check ((var), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 3979, __FUNCTION__))->decl_minimal.locus), | |||
3980 | "unnamed temporary defined here"); | |||
3981 | suppress_warning (use_stmt, OPT_Wdangling_pointer_); | |||
3982 | } | |||
3983 | } | |||
3984 | ||||
3985 | /* If STMT is a call to either the standard realloc or to a user-defined | |||
3986 | reallocation function returns its LHS and set *PTR to the reallocated | |||
3987 | pointer. Otherwise return null. */ | |||
3988 | ||||
3989 | static tree | |||
3990 | get_realloc_lhs (gimple *stmt, tree *ptr) | |||
3991 | { | |||
3992 | if (gimple_call_builtin_p (stmt, BUILT_IN_REALLOC)) | |||
3993 | { | |||
3994 | *ptr = gimple_call_arg (stmt, 0); | |||
3995 | return gimple_call_lhs (stmt); | |||
3996 | } | |||
3997 | ||||
3998 | gcall *call = dyn_cast<gcall *>(stmt); | |||
3999 | if (!call) | |||
4000 | return NULL_TREE(tree) nullptr; | |||
4001 | ||||
4002 | tree fnattr = NULL_TREE(tree) nullptr; | |||
4003 | tree fndecl = gimple_call_fndecl (call); | |||
4004 | if (fndecl) | |||
4005 | fnattr = DECL_ATTRIBUTES (fndecl)((contains_struct_check ((fndecl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4005, __FUNCTION__))->decl_common.attributes); | |||
4006 | else | |||
4007 | { | |||
4008 | tree fntype = gimple_call_fntype (stmt); | |||
4009 | if (!fntype) | |||
4010 | return NULL_TREE(tree) nullptr; | |||
4011 | fnattr = TYPE_ATTRIBUTES (fntype)((tree_class_check ((fntype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4011, __FUNCTION__))->type_common.attributes); | |||
4012 | } | |||
4013 | ||||
4014 | if (!fnattr) | |||
4015 | return NULL_TREE(tree) nullptr; | |||
4016 | ||||
4017 | for (tree ats = fnattr; (ats = lookup_attribute ("*dealloc", ats)); | |||
4018 | ats = TREE_CHAIN (ats)((contains_struct_check ((ats), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4018, __FUNCTION__))->common.chain)) | |||
4019 | { | |||
4020 | tree args = TREE_VALUE (ats)((tree_check ((ats), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4020, __FUNCTION__, (TREE_LIST)))->list.value); | |||
4021 | if (!args) | |||
4022 | continue; | |||
4023 | ||||
4024 | tree alloc = TREE_VALUE (args)((tree_check ((args), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4024, __FUNCTION__, (TREE_LIST)))->list.value); | |||
4025 | if (!alloc) | |||
4026 | continue; | |||
4027 | ||||
4028 | if (alloc == DECL_NAME (fndecl)((contains_struct_check ((fndecl), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4028, __FUNCTION__))->decl_minimal.name)) | |||
4029 | { | |||
4030 | unsigned argno = 0; | |||
4031 | if (tree index = TREE_CHAIN (args)((contains_struct_check ((args), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4031, __FUNCTION__))->common.chain)) | |||
4032 | argno = TREE_INT_CST_LOW (TREE_VALUE (index))((unsigned long) (*tree_int_cst_elt_check ((((tree_check ((index ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4032, __FUNCTION__, (TREE_LIST)))->list.value)), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4032, __FUNCTION__))) - 1; | |||
4033 | *ptr = gimple_call_arg (stmt, argno); | |||
4034 | return gimple_call_lhs (stmt); | |||
4035 | } | |||
4036 | } | |||
4037 | ||||
4038 | return NULL_TREE(tree) nullptr; | |||
4039 | } | |||
4040 | ||||
4041 | /* Warn if STMT is a call to a deallocation function that's not a match | |||
4042 | for the REALLOC_STMT call. Return true if warned. */ | |||
4043 | ||||
4044 | static bool | |||
4045 | maybe_warn_mismatched_realloc (tree ptr, gimple *realloc_stmt, gimple *stmt) | |||
4046 | { | |||
4047 | if (!is_gimple_call (stmt)) | |||
4048 | return false; | |||
4049 | ||||
4050 | tree fndecl = gimple_call_fndecl (stmt); | |||
4051 | if (!fndecl) | |||
4052 | return false; | |||
4053 | ||||
4054 | unsigned argno = fndecl_dealloc_argno (fndecl); | |||
4055 | if (call_nargs (stmt) <= argno) | |||
4056 | return false; | |||
4057 | ||||
4058 | if (matching_alloc_calls_p (realloc_stmt, fndecl)) | |||
4059 | return false; | |||
4060 | ||||
4061 | /* Avoid printing the unhelpful "<unknown>" in the diagnostics. */ | |||
4062 | if (ptr && TREE_CODE (ptr)((enum tree_code) (ptr)->base.code) == SSA_NAME | |||
4063 | && (!SSA_NAME_VAR (ptr)((tree_check ((ptr), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4063, __FUNCTION__, (SSA_NAME)))->ssa_name.var == (tree) nullptr || ((enum tree_code) ((ptr)->ssa_name.var)->base .code) == IDENTIFIER_NODE ? (tree) nullptr : (ptr)->ssa_name .var) || DECL_ARTIFICIAL (SSA_NAME_VAR (ptr))((contains_struct_check ((((tree_check ((ptr), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4063, __FUNCTION__, (SSA_NAME)))->ssa_name.var == (tree) nullptr || ((enum tree_code) ((ptr)->ssa_name.var)->base .code) == IDENTIFIER_NODE ? (tree) nullptr : (ptr)->ssa_name .var)), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4063, __FUNCTION__))->decl_common.artificial_flag))) | |||
4064 | ptr = NULL_TREE(tree) nullptr; | |||
4065 | ||||
4066 | location_t loc = gimple_location (stmt); | |||
4067 | tree realloc_decl = gimple_call_fndecl (realloc_stmt); | |||
4068 | tree dealloc_decl = gimple_call_fndecl (stmt); | |||
4069 | if (ptr && !warning_at (loc, OPT_Wmismatched_dealloc, | |||
4070 | "%qD called on pointer %qE passed to mismatched " | |||
4071 | "allocation function %qD", | |||
4072 | dealloc_decl, ptr, realloc_decl)) | |||
4073 | return false; | |||
4074 | if (!ptr && !warning_at (loc, OPT_Wmismatched_dealloc, | |||
4075 | "%qD called on a pointer passed to mismatched " | |||
4076 | "reallocation function %qD", | |||
4077 | dealloc_decl, realloc_decl)) | |||
4078 | return false; | |||
4079 | ||||
4080 | inform (gimple_location (realloc_stmt), | |||
4081 | "call to %qD", realloc_decl); | |||
4082 | return true; | |||
4083 | } | |||
4084 | ||||
4085 | /* Return true if P and Q point to the same object, and false if they | |||
4086 | either don't or their relationship cannot be determined. */ | |||
4087 | ||||
4088 | static bool | |||
4089 | pointers_related_p (gimple *stmt, tree p, tree q, pointer_query &qry, | |||
4090 | auto_bitmap &visited) | |||
4091 | { | |||
4092 | if (!ptr_derefs_may_alias_p (p, q)) | |||
4093 | return false; | |||
4094 | ||||
4095 | /* TODO: Work harder to rule out relatedness. */ | |||
4096 | access_ref pref, qref; | |||
4097 | if (!qry.get_ref (p, stmt, &pref, 0) | |||
4098 | || !qry.get_ref (q, stmt, &qref, 0)) | |||
4099 | /* GET_REF() only rarely fails. When it does, it's likely because | |||
4100 | it involves a self-referential PHI. Return a conservative result. */ | |||
4101 | return false; | |||
4102 | ||||
4103 | if (pref.ref == qref.ref) | |||
4104 | return true; | |||
4105 | ||||
4106 | /* If either pointer is a PHI, iterate over all its operands and | |||
4107 | return true if they're all related to the other pointer. */ | |||
4108 | tree ptr = q; | |||
4109 | unsigned version; | |||
4110 | gphi *phi = pref.phi (); | |||
4111 | if (phi) | |||
4112 | version = SSA_NAME_VERSION (pref.ref)(tree_check ((pref.ref), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4112, __FUNCTION__, (SSA_NAME)))->base.u.version; | |||
4113 | else | |||
4114 | { | |||
4115 | phi = qref.phi (); | |||
4116 | if (!phi) | |||
4117 | return false; | |||
4118 | ||||
4119 | ptr = p; | |||
4120 | version = SSA_NAME_VERSION (qref.ref)(tree_check ((qref.ref), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4120, __FUNCTION__, (SSA_NAME)))->base.u.version; | |||
4121 | } | |||
4122 | ||||
4123 | if (!bitmap_set_bit (visited, version)) | |||
4124 | return true; | |||
4125 | ||||
4126 | unsigned nargs = gimple_phi_num_args (phi); | |||
4127 | for (unsigned i = 0; i != nargs; ++i) | |||
4128 | { | |||
4129 | tree arg = gimple_phi_arg_def (phi, i); | |||
4130 | if (!pointers_related_p (stmt, arg, ptr, qry, visited)) | |||
4131 | return false; | |||
4132 | } | |||
4133 | ||||
4134 | return true; | |||
4135 | } | |||
4136 | ||||
4137 | /* Convenience wrapper for the above. */ | |||
4138 | ||||
4139 | static bool | |||
4140 | pointers_related_p (gimple *stmt, tree p, tree q, pointer_query &qry) | |||
4141 | { | |||
4142 | auto_bitmap visited; | |||
4143 | return pointers_related_p (stmt, p, q, qry, visited); | |||
4144 | } | |||
4145 | ||||
4146 | /* For a STMT either a call to a deallocation function or a clobber, warn | |||
4147 | for uses of the pointer PTR it was called with (including its copies | |||
4148 | or others derived from it by pointer arithmetic). If STMT is a clobber, | |||
4149 | VAR is the decl of the clobbered variable. When MAYBE is true use | |||
4150 | a "maybe" form of diagnostic. */ | |||
4151 | ||||
4152 | void | |||
4153 | pass_waccess::check_pointer_uses (gimple *stmt, tree ptr, | |||
4154 | tree var /* = NULL_TREE */, | |||
4155 | bool maybe /* = false */) | |||
4156 | { | |||
4157 | gcc_assert (TREE_CODE (ptr) == SSA_NAME)((void)(!(((enum tree_code) (ptr)->base.code) == SSA_NAME) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4157, __FUNCTION__), 0 : 0)); | |||
4158 | ||||
4159 | const bool check_dangling = !is_gimple_call (stmt); | |||
4160 | basic_block stmt_bb = gimple_bb (stmt); | |||
4161 | ||||
4162 | /* If STMT is a reallocation function set to the reallocated pointer | |||
4163 | and the LHS of the call, respectively. */ | |||
4164 | tree realloc_ptr = NULL_TREE(tree) nullptr; | |||
4165 | tree realloc_lhs = get_realloc_lhs (stmt, &realloc_ptr); | |||
4166 | ||||
4167 | auto_bitmap visited; | |||
4168 | ||||
4169 | auto_vec<tree> pointers; | |||
4170 | pointers.safe_push (ptr); | |||
4171 | ||||
4172 | /* Starting with PTR, iterate over POINTERS added by the loop, and | |||
4173 | either warn for their uses in basic blocks dominated by the STMT | |||
4174 | or in statements that follow it in the same basic block, or add | |||
4175 | them to POINTERS if they point into the same object as PTR (i.e., | |||
4176 | are obtained by pointer arithmetic on PTR). */ | |||
4177 | for (unsigned i = 0; i != pointers.length (); ++i) | |||
4178 | { | |||
4179 | tree ptr = pointers[i]; | |||
4180 | if (!bitmap_set_bit (visited, SSA_NAME_VERSION (ptr)(tree_check ((ptr), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4180, __FUNCTION__, (SSA_NAME)))->base.u.version)) | |||
4181 | /* Avoid revisiting the same pointer. */ | |||
4182 | continue; | |||
4183 | ||||
4184 | use_operand_p use_p; | |||
4185 | imm_use_iterator iter; | |||
4186 | FOR_EACH_IMM_USE_FAST (use_p, iter, ptr)for ((use_p) = first_readonly_imm_use (&(iter), (ptr)); ! end_readonly_imm_use_p (&(iter)); (void) ((use_p) = next_readonly_imm_use (&(iter)))) | |||
4187 | { | |||
4188 | gimple *use_stmt = USE_STMT (use_p)(use_p)->loc.stmt; | |||
4189 | if (use_stmt == stmt || is_gimple_debug (use_stmt)) | |||
4190 | continue; | |||
4191 | ||||
4192 | /* A clobber isn't a use. */ | |||
4193 | if (gimple_clobber_p (use_stmt)) | |||
4194 | continue; | |||
4195 | ||||
4196 | if (realloc_lhs) | |||
4197 | { | |||
4198 | /* Check to see if USE_STMT is a mismatched deallocation | |||
4199 | call for the pointer passed to realloc. That's a bug | |||
4200 | regardless of the pointer's value and so warn. */ | |||
4201 | if (maybe_warn_mismatched_realloc (*use_p->use, stmt, use_stmt)) | |||
4202 | continue; | |||
4203 | ||||
4204 | /* Pointers passed to realloc that are used in basic blocks | |||
4205 | where the realloc call is known to have failed are valid. | |||
4206 | Ignore pointers that nothing is known about. Those could | |||
4207 | have escaped along with their nullness. */ | |||
4208 | value_range vr; | |||
4209 | if (m_ptr_qry.rvals->range_of_expr (vr, realloc_lhs, use_stmt)) | |||
4210 | { | |||
4211 | if (vr.zero_p ()) | |||
4212 | continue; | |||
4213 | ||||
4214 | if (!pointers_related_p (stmt, ptr, realloc_ptr, m_ptr_qry)) | |||
4215 | continue; | |||
4216 | } | |||
4217 | } | |||
4218 | ||||
4219 | if (check_dangling | |||
4220 | && gimple_code (use_stmt) == GIMPLE_RETURN) | |||
4221 | /* Avoid interfering with -Wreturn-local-addr (which runs only | |||
4222 | with optimization enabled so it won't diagnose cases that | |||
4223 | would be caught here when optimization is disabled). */ | |||
4224 | continue; | |||
4225 | ||||
4226 | bool equality = false; | |||
4227 | if (is_gimple_assign (use_stmt)) | |||
4228 | { | |||
4229 | tree_code code = gimple_assign_rhs_code (use_stmt); | |||
4230 | equality = code == EQ_EXPR || code == NE_EXPR; | |||
4231 | } | |||
4232 | else if (gcond *cond = dyn_cast<gcond *>(use_stmt)) | |||
4233 | { | |||
4234 | tree_code code = gimple_cond_code (cond); | |||
4235 | equality = code == EQ_EXPR || code == NE_EXPR; | |||
4236 | } | |||
4237 | else if (gimple_code (use_stmt) == GIMPLE_PHI) | |||
4238 | { | |||
4239 | /* Only add a PHI result to POINTERS if all its | |||
4240 | operands are related to PTR, otherwise continue. */ | |||
4241 | tree lhs = gimple_phi_result (use_stmt); | |||
4242 | if (!pointers_related_p (stmt, lhs, ptr, m_ptr_qry)) | |||
4243 | continue; | |||
4244 | ||||
4245 | if (TREE_CODE (lhs)((enum tree_code) (lhs)->base.code) == SSA_NAME) | |||
4246 | { | |||
4247 | pointers.safe_push (lhs); | |||
4248 | continue; | |||
4249 | } | |||
4250 | } | |||
4251 | ||||
4252 | /* Warn if USE_STMT is dominated by the deallocation STMT. | |||
4253 | Otherwise, add the pointer to POINTERS so that the uses | |||
4254 | of any other pointers derived from it can be checked. */ | |||
4255 | if (use_after_inval_p (stmt, use_stmt, check_dangling)) | |||
4256 | { | |||
4257 | basic_block use_bb = gimple_bb (use_stmt); | |||
4258 | bool this_maybe | |||
4259 | = (maybe | |||
4260 | || !dominated_by_p (CDI_POST_DOMINATORS, stmt_bb, use_bb)); | |||
4261 | warn_invalid_pointer (*use_p->use, use_stmt, stmt, var, | |||
4262 | this_maybe, equality); | |||
4263 | continue; | |||
4264 | } | |||
4265 | ||||
4266 | if (is_gimple_assign (use_stmt)) | |||
4267 | { | |||
4268 | tree lhs = gimple_assign_lhs (use_stmt); | |||
4269 | if (TREE_CODE (lhs)((enum tree_code) (lhs)->base.code) == SSA_NAME) | |||
4270 | { | |||
4271 | tree_code rhs_code = gimple_assign_rhs_code (use_stmt); | |||
4272 | if (rhs_code == POINTER_PLUS_EXPR || rhs_code == SSA_NAME) | |||
4273 | pointers.safe_push (lhs); | |||
4274 | } | |||
4275 | continue; | |||
4276 | } | |||
4277 | ||||
4278 | if (gcall *call = dyn_cast <gcall *>(use_stmt)) | |||
4279 | { | |||
4280 | if (gimple_call_return_arg (call) == ptr) | |||
4281 | if (tree lhs = gimple_call_lhs (call)) | |||
4282 | if (TREE_CODE (lhs)((enum tree_code) (lhs)->base.code) == SSA_NAME) | |||
4283 | pointers.safe_push (lhs); | |||
4284 | continue; | |||
4285 | } | |||
4286 | } | |||
4287 | } | |||
4288 | } | |||
4289 | ||||
4290 | /* Check call STMT for invalid accesses. */ | |||
4291 | ||||
4292 | void | |||
4293 | pass_waccess::check_call (gcall *stmt) | |||
4294 | { | |||
4295 | /* Skip special calls generated by the compiler. */ | |||
4296 | if (gimple_call_from_thunk_p (stmt)) | |||
4297 | return; | |||
4298 | ||||
4299 | /* .ASAN_MARK doesn't access any vars, only modifies shadow memory. */ | |||
4300 | if (gimple_call_internal_p (stmt) | |||
4301 | && gimple_call_internal_fn (stmt) == IFN_ASAN_MARK) | |||
4302 | return; | |||
4303 | ||||
4304 | if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)) | |||
4305 | check_builtin (stmt); | |||
4306 | ||||
4307 | if (tree callee = gimple_call_fndecl (stmt)) | |||
4308 | { | |||
4309 | /* Check for uses of the pointer passed to either a standard | |||
4310 | or a user-defined deallocation function. */ | |||
4311 | unsigned argno = fndecl_dealloc_argno (callee); | |||
4312 | if (argno < (unsigned) call_nargs (stmt)) | |||
4313 | { | |||
4314 | tree arg = call_arg (stmt, argno); | |||
4315 | if (TREE_CODE (arg)((enum tree_code) (arg)->base.code) == SSA_NAME) | |||
4316 | check_pointer_uses (stmt, arg); | |||
4317 | } | |||
4318 | } | |||
4319 | ||||
4320 | check_call_access (stmt); | |||
4321 | check_call_dangling (stmt); | |||
4322 | ||||
4323 | if (m_early_checks_p) | |||
4324 | return; | |||
4325 | ||||
4326 | maybe_check_dealloc_call (stmt); | |||
4327 | check_nonstring_args (stmt); | |||
4328 | } | |||
4329 | ||||
4330 | /* Check non-call STMT for invalid accesses. */ | |||
4331 | ||||
4332 | void | |||
4333 | pass_waccess::check_stmt (gimple *stmt) | |||
4334 | { | |||
4335 | if (m_check_dangling_p | |||
4336 | && gimple_clobber_p (stmt, CLOBBER_EOL)) | |||
4337 | { | |||
4338 | /* Ignore clobber statements in blocks with exceptional edges. */ | |||
4339 | basic_block bb = gimple_bb (stmt); | |||
4340 | edge e = EDGE_PRED (bb, 0)(*(bb)->preds)[(0)]; | |||
4341 | if (e->flags & EDGE_EH) | |||
4342 | return; | |||
4343 | ||||
4344 | tree var = gimple_assign_lhs (stmt); | |||
4345 | m_clobbers.put (var, stmt); | |||
4346 | return; | |||
4347 | } | |||
4348 | ||||
4349 | if (is_gimple_assign (stmt)) | |||
4350 | { | |||
4351 | /* Clobbered unnamed temporaries such as compound literals can be | |||
4352 | revived. Check for an assignment to one and remove it from | |||
4353 | M_CLOBBERS. */ | |||
4354 | tree lhs = gimple_assign_lhs (stmt); | |||
4355 | while (handled_component_p (lhs)) | |||
4356 | lhs = TREE_OPERAND (lhs, 0)(*((const_cast<tree*> (tree_operand_check ((lhs), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4356, __FUNCTION__))))); | |||
4357 | ||||
4358 | if (auto_var_p (lhs)) | |||
4359 | m_clobbers.remove (lhs); | |||
4360 | return; | |||
4361 | } | |||
4362 | ||||
4363 | if (greturn *ret = dyn_cast <greturn *> (stmt)) | |||
4364 | { | |||
4365 | if (optimizeglobal_options.x_optimize && flag_isolate_erroneous_paths_dereferenceglobal_options.x_flag_isolate_erroneous_paths_dereference) | |||
4366 | /* Avoid interfering with -Wreturn-local-addr (which runs only | |||
4367 | with optimization enabled). */ | |||
4368 | return; | |||
4369 | ||||
4370 | tree arg = gimple_return_retval (ret); | |||
4371 | if (!arg || TREE_CODE (arg)((enum tree_code) (arg)->base.code) != ADDR_EXPR) | |||
4372 | return; | |||
4373 | ||||
4374 | arg = TREE_OPERAND (arg, 0)(*((const_cast<tree*> (tree_operand_check ((arg), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4374, __FUNCTION__))))); | |||
4375 | while (handled_component_p (arg)) | |||
4376 | arg = TREE_OPERAND (arg, 0)(*((const_cast<tree*> (tree_operand_check ((arg), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4376, __FUNCTION__))))); | |||
4377 | ||||
4378 | if (!auto_var_p (arg)) | |||
4379 | return; | |||
4380 | ||||
4381 | gimple **pclobber = m_clobbers.get (arg); | |||
4382 | if (!pclobber) | |||
4383 | return; | |||
4384 | ||||
4385 | if (!use_after_inval_p (*pclobber, stmt)) | |||
4386 | return; | |||
4387 | ||||
4388 | warn_invalid_pointer (NULL_TREE(tree) nullptr, stmt, *pclobber, arg, false); | |||
4389 | } | |||
4390 | } | |||
4391 | ||||
4392 | /* Check basic block BB for invalid accesses. */ | |||
4393 | ||||
4394 | void | |||
4395 | pass_waccess::check_block (basic_block bb) | |||
4396 | { | |||
4397 | /* Iterate over statements, looking for function calls. */ | |||
4398 | for (auto si = gsi_start_bb (bb); !gsi_end_p (si); | |||
4399 | gsi_next_nondebug (&si)) | |||
4400 | { | |||
4401 | gimple *stmt = gsi_stmt (si); | |||
4402 | if (gcall *call = dyn_cast <gcall *> (stmt)) | |||
4403 | check_call (call); | |||
4404 | else | |||
4405 | check_stmt (stmt); | |||
4406 | } | |||
4407 | } | |||
4408 | ||||
4409 | /* Return the argument that the call STMT to a built-in function returns | |||
4410 | (including with an offset) or null if it doesn't. */ | |||
4411 | ||||
4412 | tree | |||
4413 | pass_waccess::gimple_call_return_arg (gcall *call) | |||
4414 | { | |||
4415 | /* Check for attribute fn spec to see if the function returns one | |||
4416 | of its arguments. */ | |||
4417 | attr_fnspec fnspec = gimple_call_fnspec (call); | |||
4418 | unsigned int argno; | |||
4419 | if (!fnspec.returns_arg (&argno)) | |||
4420 | { | |||
4421 | if (gimple_call_num_args (call) < 1) | |||
4422 | return NULL_TREE(tree) nullptr; | |||
4423 | ||||
4424 | if (!gimple_call_builtin_p (call, BUILT_IN_NORMAL)) | |||
4425 | return NULL_TREE(tree) nullptr; | |||
4426 | ||||
4427 | tree fndecl = gimple_call_fndecl (call); | |||
4428 | switch (DECL_FUNCTION_CODE (fndecl)) | |||
4429 | { | |||
4430 | case BUILT_IN_MEMPCPY: | |||
4431 | case BUILT_IN_MEMPCPY_CHK: | |||
4432 | case BUILT_IN_MEMCHR: | |||
4433 | case BUILT_IN_STRCHR: | |||
4434 | case BUILT_IN_STRRCHR: | |||
4435 | case BUILT_IN_STRSTR: | |||
4436 | case BUILT_IN_STPCPY: | |||
4437 | case BUILT_IN_STPCPY_CHK: | |||
4438 | case BUILT_IN_STPNCPY: | |||
4439 | case BUILT_IN_STPNCPY_CHK: | |||
4440 | argno = 0; | |||
4441 | break; | |||
4442 | ||||
4443 | default: | |||
4444 | return NULL_TREE(tree) nullptr; | |||
4445 | } | |||
4446 | } | |||
4447 | ||||
4448 | if (gimple_call_num_args (call) <= argno) | |||
4449 | return NULL_TREE(tree) nullptr; | |||
4450 | ||||
4451 | return gimple_call_arg (call, argno); | |||
4452 | } | |||
4453 | ||||
4454 | /* Check for and diagnose all uses of the dangling pointer VAR to the auto | |||
4455 | object DECL whose lifetime has ended. OBJREF is true when VAR denotes | |||
4456 | an access to a DECL that may have been clobbered. */ | |||
4457 | ||||
4458 | void | |||
4459 | pass_waccess::check_dangling_uses (tree var, tree decl, bool maybe /* = false */, | |||
4460 | bool objref /* = false */) | |||
4461 | { | |||
4462 | if (!decl || !auto_var_p (decl)) | |||
4463 | return; | |||
4464 | ||||
4465 | gimple **pclob = m_clobbers.get (decl); | |||
4466 | if (!pclob) | |||
4467 | return; | |||
4468 | ||||
4469 | if (!objref) | |||
4470 | { | |||
4471 | check_pointer_uses (*pclob, var, decl, maybe); | |||
4472 | return; | |||
4473 | } | |||
4474 | ||||
4475 | gimple *use_stmt = SSA_NAME_DEF_STMT (var)(tree_check ((var), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4475, __FUNCTION__, (SSA_NAME)))->ssa_name.def_stmt; | |||
4476 | if (!use_after_inval_p (*pclob, use_stmt, true)) | |||
4477 | return; | |||
4478 | ||||
4479 | basic_block use_bb = gimple_bb (use_stmt); | |||
4480 | basic_block clob_bb = gimple_bb (*pclob); | |||
4481 | maybe = maybe || !dominated_by_p (CDI_POST_DOMINATORS, clob_bb, use_bb); | |||
4482 | warn_invalid_pointer (var, use_stmt, *pclob, decl, maybe, false); | |||
4483 | } | |||
4484 | ||||
4485 | /* Diagnose stores in BB and (recursively) its predecessors of the addresses | |||
4486 | of local variables into nonlocal pointers that are left dangling after | |||
4487 | the function returns. BBS is a bitmap of basic blocks visited. */ | |||
4488 | ||||
4489 | void | |||
4490 | pass_waccess::check_dangling_stores (basic_block bb, | |||
4491 | hash_set<tree> &stores, | |||
4492 | auto_bitmap &bbs) | |||
4493 | { | |||
4494 | if (!bitmap_set_bit (bbs, bb->index)) | |||
4495 | /* Avoid cycles. */ | |||
4496 | return; | |||
4497 | ||||
4498 | /* Iterate backwards over the statements looking for a store of | |||
4499 | the address of a local variable into a nonlocal pointer. */ | |||
4500 | for (auto gsi = gsi_last_nondebug_bb (bb); ; gsi_prev_nondebug (&gsi)) | |||
4501 | { | |||
4502 | gimple *stmt = gsi_stmt (gsi); | |||
4503 | if (!stmt) | |||
4504 | break; | |||
4505 | ||||
4506 | if (warning_suppressed_p (stmt, OPT_Wdangling_pointer_)) | |||
4507 | continue; | |||
4508 | ||||
4509 | if (is_gimple_call (stmt) | |||
4510 | && !(gimple_call_flags (stmt) & (ECF_CONST(1 << 0) | ECF_PURE(1 << 1)))) | |||
4511 | /* Avoid looking before nonconst, nonpure calls since those might | |||
4512 | use the escaped locals. */ | |||
4513 | return; | |||
4514 | ||||
4515 | if (!is_gimple_assign (stmt) || gimple_clobber_p (stmt) | |||
4516 | || !gimple_store_p (stmt)) | |||
4517 | continue; | |||
4518 | ||||
4519 | access_ref lhs_ref; | |||
4520 | tree lhs = gimple_assign_lhs (stmt); | |||
4521 | if (!m_ptr_qry.get_ref (lhs, stmt, &lhs_ref, 0)) | |||
4522 | continue; | |||
4523 | ||||
4524 | if (auto_var_p (lhs_ref.ref)) | |||
4525 | continue; | |||
4526 | ||||
4527 | if (DECL_P (lhs_ref.ref)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (lhs_ref.ref)->base.code))] == tcc_declaration)) | |||
4528 | { | |||
4529 | if (!POINTER_TYPE_P (TREE_TYPE (lhs_ref.ref))(((enum tree_code) (((contains_struct_check ((lhs_ref.ref), ( TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4529, __FUNCTION__))->typed.type))->base.code) == POINTER_TYPE || ((enum tree_code) (((contains_struct_check ((lhs_ref.ref) , (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4529, __FUNCTION__))->typed.type))->base.code) == REFERENCE_TYPE ) | |||
4530 | || lhs_ref.deref > 0) | |||
4531 | continue; | |||
4532 | } | |||
4533 | else if (TREE_CODE (lhs_ref.ref)((enum tree_code) (lhs_ref.ref)->base.code) == SSA_NAME) | |||
4534 | { | |||
4535 | gimple *def_stmt = SSA_NAME_DEF_STMT (lhs_ref.ref)(tree_check ((lhs_ref.ref), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4535, __FUNCTION__, (SSA_NAME)))->ssa_name.def_stmt; | |||
4536 | if (!gimple_nop_p (def_stmt)) | |||
4537 | /* Avoid looking at or before stores into unknown objects. */ | |||
4538 | return; | |||
4539 | ||||
4540 | tree var = SSA_NAME_VAR (lhs_ref.ref)((tree_check ((lhs_ref.ref), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4540, __FUNCTION__, (SSA_NAME)))->ssa_name.var == (tree) nullptr || ((enum tree_code) ((lhs_ref.ref)->ssa_name.var )->base.code) == IDENTIFIER_NODE ? (tree) nullptr : (lhs_ref .ref)->ssa_name.var); | |||
4541 | if (TREE_CODE (var)((enum tree_code) (var)->base.code) == PARM_DECL && DECL_BY_REFERENCE (var)((tree_check3 ((var), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4541, __FUNCTION__, (VAR_DECL), (PARM_DECL), (RESULT_DECL)) )->decl_common.decl_by_reference_flag)) | |||
4542 | /* Avoid by-value arguments transformed into by-reference. */ | |||
4543 | continue; | |||
4544 | ||||
4545 | } | |||
4546 | else if (TREE_CODE (lhs_ref.ref)((enum tree_code) (lhs_ref.ref)->base.code) == MEM_REF) | |||
4547 | { | |||
4548 | tree arg = TREE_OPERAND (lhs_ref.ref, 0)(*((const_cast<tree*> (tree_operand_check ((lhs_ref.ref ), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4548, __FUNCTION__))))); | |||
4549 | if (TREE_CODE (arg)((enum tree_code) (arg)->base.code) == SSA_NAME) | |||
4550 | { | |||
4551 | gimple *def_stmt = SSA_NAME_DEF_STMT (arg)(tree_check ((arg), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4551, __FUNCTION__, (SSA_NAME)))->ssa_name.def_stmt; | |||
4552 | if (!gimple_nop_p (def_stmt)) | |||
4553 | return; | |||
4554 | } | |||
4555 | } | |||
4556 | else | |||
4557 | continue; | |||
4558 | ||||
4559 | if (stores.add (lhs_ref.ref)) | |||
4560 | continue; | |||
4561 | ||||
4562 | /* FIXME: Handle stores of alloca() and VLA. */ | |||
4563 | access_ref rhs_ref; | |||
4564 | tree rhs = gimple_assign_rhs1 (stmt); | |||
4565 | if (!m_ptr_qry.get_ref (rhs, stmt, &rhs_ref, 0) | |||
4566 | || rhs_ref.deref != -1) | |||
4567 | continue; | |||
4568 | ||||
4569 | if (!auto_var_p (rhs_ref.ref)) | |||
4570 | continue; | |||
4571 | ||||
4572 | auto_diagnostic_group d; | |||
4573 | location_t loc = gimple_location (stmt); | |||
4574 | if (warning_at (loc, OPT_Wdangling_pointer_, | |||
4575 | "storing the address of local variable %qD in %qE", | |||
4576 | rhs_ref.ref, lhs)) | |||
4577 | { | |||
4578 | suppress_warning (stmt, OPT_Wdangling_pointer_); | |||
4579 | ||||
4580 | location_t loc = DECL_SOURCE_LOCATION (rhs_ref.ref)((contains_struct_check ((rhs_ref.ref), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4580, __FUNCTION__))->decl_minimal.locus); | |||
4581 | inform (loc, "%qD declared here", rhs_ref.ref); | |||
4582 | ||||
4583 | if (DECL_P (lhs_ref.ref)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (lhs_ref.ref)->base.code))] == tcc_declaration)) | |||
4584 | loc = DECL_SOURCE_LOCATION (lhs_ref.ref)((contains_struct_check ((lhs_ref.ref), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4584, __FUNCTION__))->decl_minimal.locus); | |||
4585 | else if (EXPR_HAS_LOCATION (lhs_ref.ref)(((IS_ADHOC_LOC (((((lhs_ref.ref)) && ((tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) ((lhs_ref .ref))->base.code))]) >= tcc_reference && (tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) ((lhs_ref .ref))->base.code))]) <= tcc_expression)) ? (lhs_ref.ref )->exp.locus : ((location_t) 0)))) ? get_location_from_adhoc_loc (line_table, ((((lhs_ref.ref)) && ((tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) ((lhs_ref .ref))->base.code))]) >= tcc_reference && (tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) ((lhs_ref .ref))->base.code))]) <= tcc_expression)) ? (lhs_ref.ref )->exp.locus : ((location_t) 0))) : (((((lhs_ref.ref)) && ((tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) ((lhs_ref.ref))->base.code))]) >= tcc_reference && (tree_code_type_tmpl <0>::tree_code_type[(int ) (((enum tree_code) ((lhs_ref.ref))->base.code))]) <= tcc_expression )) ? (lhs_ref.ref)->exp.locus : ((location_t) 0)))) != ((location_t ) 0))) | |||
4586 | loc = EXPR_LOCATION (lhs_ref.ref)((((lhs_ref.ref)) && ((tree_code_type_tmpl <0>:: tree_code_type[(int) (((enum tree_code) ((lhs_ref.ref))->base .code))]) >= tcc_reference && (tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code) ((lhs_ref .ref))->base.code))]) <= tcc_expression)) ? (lhs_ref.ref )->exp.locus : ((location_t) 0)); | |||
4587 | ||||
4588 | if (loc != UNKNOWN_LOCATION((location_t) 0)) | |||
4589 | inform (loc, "%qE declared here", lhs_ref.ref); | |||
4590 | } | |||
4591 | } | |||
4592 | ||||
4593 | edge e; | |||
4594 | edge_iterator ei; | |||
4595 | FOR_EACH_EDGE (e, ei, bb->preds)for ((ei) = ei_start_1 (&((bb->preds))); ei_cond ((ei) , &(e)); ei_next (&(ei))) | |||
4596 | { | |||
4597 | basic_block pred = e->src; | |||
4598 | check_dangling_stores (pred, stores, bbs); | |||
4599 | } | |||
4600 | } | |||
4601 | ||||
4602 | /* Diagnose stores of the addresses of local variables into nonlocal | |||
4603 | pointers that are left dangling after the function returns. */ | |||
4604 | ||||
4605 | void | |||
4606 | pass_waccess::check_dangling_stores () | |||
4607 | { | |||
4608 | auto_bitmap bbs; | |||
4609 | hash_set<tree> stores; | |||
4610 | check_dangling_stores (EXIT_BLOCK_PTR_FOR_FN (m_func)((m_func)->cfg->x_exit_block_ptr), stores, bbs); | |||
4611 | } | |||
4612 | ||||
4613 | /* Check for and diagnose uses of dangling pointers to auto objects | |||
4614 | whose lifetime has ended. */ | |||
4615 | ||||
4616 | void | |||
4617 | pass_waccess::check_dangling_uses () | |||
4618 | { | |||
4619 | tree var; | |||
4620 | unsigned i; | |||
4621 | FOR_EACH_SSA_NAME (i, var, m_func)for (i = 1; (m_func)->gimple_df->ssa_names->iterate ( i, &var); ++i) if (var) | |||
4622 | { | |||
4623 | /* For each SSA_NAME pointer VAR find the object it points to. | |||
4624 | If the object is a clobbered local variable, check to see | |||
4625 | if any of VAR's uses (or those of other pointers derived | |||
4626 | from VAR) happens after the clobber. If so, warn. */ | |||
4627 | ||||
4628 | gimple *def_stmt = SSA_NAME_DEF_STMT (var)(tree_check ((var), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4628, __FUNCTION__, (SSA_NAME)))->ssa_name.def_stmt; | |||
4629 | if (is_gimple_assign (def_stmt)) | |||
4630 | { | |||
4631 | tree rhs = gimple_assign_rhs1 (def_stmt); | |||
4632 | if (TREE_CODE (rhs)((enum tree_code) (rhs)->base.code) == ADDR_EXPR) | |||
4633 | { | |||
4634 | if (!POINTER_TYPE_P (TREE_TYPE (var))(((enum tree_code) (((contains_struct_check ((var), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4634, __FUNCTION__))->typed.type))->base.code) == POINTER_TYPE || ((enum tree_code) (((contains_struct_check ((var), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4634, __FUNCTION__))->typed.type))->base.code) == REFERENCE_TYPE )) | |||
4635 | continue; | |||
4636 | check_dangling_uses (var, TREE_OPERAND (rhs, 0)(*((const_cast<tree*> (tree_operand_check ((rhs), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4636, __FUNCTION__)))))); | |||
4637 | } | |||
4638 | else | |||
4639 | { | |||
4640 | /* For other expressions, check the base DECL to see | |||
4641 | if it's been clobbered, most likely as a result of | |||
4642 | inlining a reference to it. */ | |||
4643 | tree decl = get_base_address (rhs); | |||
4644 | if (DECL_P (decl)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (decl)->base.code))] == tcc_declaration)) | |||
4645 | check_dangling_uses (var, decl, false, true); | |||
4646 | } | |||
4647 | } | |||
4648 | else if (POINTER_TYPE_P (TREE_TYPE (var))(((enum tree_code) (((contains_struct_check ((var), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4648, __FUNCTION__))->typed.type))->base.code) == POINTER_TYPE || ((enum tree_code) (((contains_struct_check ((var), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4648, __FUNCTION__))->typed.type))->base.code) == REFERENCE_TYPE )) | |||
4649 | { | |||
4650 | if (gcall *call = dyn_cast<gcall *>(def_stmt)) | |||
4651 | { | |||
4652 | if (tree arg = gimple_call_return_arg (call)) | |||
4653 | { | |||
4654 | access_ref aref; | |||
4655 | if (m_ptr_qry.get_ref (arg, call, &aref, 0) | |||
4656 | && aref.deref < 0) | |||
4657 | check_dangling_uses (var, aref.ref); | |||
4658 | } | |||
4659 | } | |||
4660 | else if (gphi *phi = dyn_cast <gphi *>(def_stmt)) | |||
4661 | { | |||
4662 | unsigned nargs = gimple_phi_num_args (phi); | |||
4663 | for (unsigned i = 0; i != nargs; ++i) | |||
4664 | { | |||
4665 | access_ref aref; | |||
4666 | tree arg = gimple_phi_arg_def (phi, i); | |||
4667 | if (m_ptr_qry.get_ref (arg, phi, &aref, 0) | |||
4668 | && aref.deref < 0) | |||
4669 | check_dangling_uses (var, aref.ref, true); | |||
4670 | } | |||
4671 | } | |||
4672 | } | |||
4673 | } | |||
4674 | } | |||
4675 | ||||
4676 | /* Check CALL arguments for dangling pointers (those that have been | |||
4677 | clobbered) and warn if found. */ | |||
4678 | ||||
4679 | void | |||
4680 | pass_waccess::check_call_dangling (gcall *call) | |||
4681 | { | |||
4682 | unsigned nargs = gimple_call_num_args (call); | |||
4683 | for (unsigned i = 0; i != nargs; ++i) | |||
4684 | { | |||
4685 | tree arg = gimple_call_arg (call, i); | |||
4686 | if (TREE_CODE (arg)((enum tree_code) (arg)->base.code) != ADDR_EXPR) | |||
4687 | continue; | |||
4688 | ||||
4689 | arg = TREE_OPERAND (arg, 0)(*((const_cast<tree*> (tree_operand_check ((arg), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/gimple-ssa-warn-access.cc" , 4689, __FUNCTION__))))); | |||
4690 | if (!DECL_P (arg)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (arg)->base.code))] == tcc_declaration)) | |||
4691 | continue; | |||
4692 | ||||
4693 | gimple **pclobber = m_clobbers.get (arg); | |||
4694 | if (!pclobber) | |||
4695 | continue; | |||
4696 | ||||
4697 | if (!use_after_inval_p (*pclobber, call)) | |||
4698 | continue; | |||
4699 | ||||
4700 | warn_invalid_pointer (NULL_TREE(tree) nullptr, call, *pclobber, arg, false); | |||
4701 | } | |||
4702 | } | |||
4703 | ||||
4704 | /* Check function FUN for invalid accesses. */ | |||
4705 | ||||
4706 | unsigned | |||
4707 | pass_waccess::execute (function *fun) | |||
4708 | { | |||
4709 | calculate_dominance_info (CDI_DOMINATORS); | |||
4710 | calculate_dominance_info (CDI_POST_DOMINATORS); | |||
4711 | ||||
4712 | /* Set or clear EDGE_DFS_BACK bits on back edges. */ | |||
4713 | mark_dfs_back_edges (fun); | |||
4714 | ||||
4715 | /* Create a new ranger instance and associate it with FUN. */ | |||
4716 | m_ptr_qry.rvals = enable_ranger (fun); | |||
4717 | m_func = fun; | |||
4718 | ||||
4719 | /* Check for dangling pointers in the earliest run of the pass. | |||
4720 | The latest point -Wdangling-pointer should run is just before | |||
4721 | loop unrolling which introduces uses after clobbers. Most cases | |||
4722 | can be detected without optimization; cases where the address of | |||
4723 | the local variable is passed to and then returned from a user- | |||
4724 | defined function before its lifetime ends and the returned pointer | |||
4725 | becomes dangling depend on inlining. */ | |||
4726 | m_check_dangling_p = m_early_checks_p; | |||
4727 | ||||
4728 | auto_bitmap bb_uids_set (&bitmap_default_obstack); | |||
4729 | m_bb_uids_set = bb_uids_set; | |||
4730 | ||||
4731 | set_gimple_stmt_max_uid (m_func, 0); | |||
4732 | ||||
4733 | basic_block bb; | |||
4734 | FOR_EACH_BB_FN (bb, fun)for (bb = (fun)->cfg->x_entry_block_ptr->next_bb; bb != (fun)->cfg->x_exit_block_ptr; bb = bb->next_bb) | |||
4735 | check_block (bb); | |||
4736 | ||||
4737 | if (m_check_dangling_p) | |||
4738 | { | |||
4739 | check_dangling_uses (); | |||
4740 | check_dangling_stores (); | |||
4741 | } | |||
4742 | ||||
4743 | if (dump_file) | |||
4744 | m_ptr_qry.dump (dump_file, (dump_flags & TDF_DETAILS) != 0); | |||
4745 | ||||
4746 | m_ptr_qry.flush_cache (); | |||
4747 | ||||
4748 | /* Release the ranger instance and replace it with a global ranger. | |||
4749 | Also reset the pointer since calling disable_ranger() deletes it. */ | |||
4750 | disable_ranger (fun); | |||
4751 | m_ptr_qry.rvals = NULLnullptr; | |||
4752 | ||||
4753 | m_clobbers.empty (); | |||
4754 | m_bb_uids_set = NULLnullptr; | |||
4755 | ||||
4756 | free_dominance_info (CDI_POST_DOMINATORS); | |||
4757 | free_dominance_info (CDI_DOMINATORS); | |||
4758 | return 0; | |||
4759 | } | |||
4760 | ||||
4761 | } // namespace | |||
4762 | ||||
4763 | /* Return a new instance of the pass. */ | |||
4764 | ||||
4765 | gimple_opt_pass * | |||
4766 | make_pass_warn_access (gcc::context *ctxt) | |||
4767 | { | |||
4768 | return new pass_waccess (ctxt); | |||
4769 | } |