Line data Source code
1 : /* Helper class for handling a call with specific arguments.
2 : Copyright (C) 2020-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "analyzer/common.h"
22 :
23 : #include "diagnostic.h"
24 : #include "tree-diagnostic.h" /* for default_tree_printer. */
25 : #include "gimple-pretty-print.h"
26 : #include "stringpool.h"
27 : #include "attribs.h"
28 : #include "diagnostics/sarif-sink.h"
29 :
30 : #include "analyzer/analyzer-logging.h"
31 : #include "analyzer/region-model.h"
32 : #include "analyzer/call-details.h"
33 :
34 : #if ENABLE_ANALYZER
35 :
36 : namespace ana {
37 :
38 : /* class call_details. */
39 :
40 : /* call_details's ctor. */
41 :
42 369201 : call_details::call_details (const gcall &call, region_model *model,
43 : region_model_context *ctxt)
44 369201 : : m_call (call), m_model (model), m_ctxt (ctxt),
45 369201 : m_lhs_type (NULL_TREE), m_lhs_region (nullptr)
46 : {
47 369201 : m_lhs_type = NULL_TREE;
48 369201 : if (tree lhs = gimple_call_lhs (&call))
49 : {
50 137839 : m_lhs_region = model->get_lvalue (lhs, ctxt);
51 137839 : m_lhs_type = TREE_TYPE (lhs);
52 : }
53 369201 : }
54 :
55 : /* call_details's ctor: copy CD, but override the context,
56 : using CTXT instead. */
57 :
58 1072 : call_details::call_details (const call_details &cd,
59 : region_model_context *ctxt)
60 1072 : : m_call (cd.m_call), m_model (cd.m_model),
61 1072 : m_ctxt (ctxt),
62 1072 : m_lhs_type (cd.m_lhs_type),
63 1072 : m_lhs_region (cd.m_lhs_region)
64 : {
65 1072 : }
66 :
67 : /* Get the manager from m_model. */
68 :
69 : region_model_manager *
70 101760 : call_details::get_manager () const
71 : {
72 101760 : return m_model->get_manager ();
73 : }
74 :
75 : /* Get any logger associated with this object. */
76 :
77 : logger *
78 0 : call_details::get_logger () const
79 : {
80 0 : if (m_ctxt)
81 0 : return m_ctxt->get_logger ();
82 : else
83 : return nullptr;
84 : }
85 :
86 : /* Get any uncertainty_t associated with the region_model_context. */
87 :
88 : uncertainty_t *
89 184 : call_details::get_uncertainty () const
90 : {
91 184 : if (m_ctxt)
92 148 : return m_ctxt->get_uncertainty ();
93 : else
94 : return nullptr;
95 : }
96 :
97 : /* If the callsite has a left-hand-side region, set it to RESULT
98 : and return true.
99 : Otherwise do nothing and return false. */
100 :
101 : bool
102 32817 : call_details::maybe_set_lhs (const svalue *result) const
103 : {
104 32817 : gcc_assert (result);
105 32817 : if (m_lhs_region)
106 : {
107 30648 : m_model->set_value (m_lhs_region, result, m_ctxt);
108 30648 : return true;
109 : }
110 : else
111 : return false;
112 : }
113 :
114 : /* Return true if CD is known to be a call to a function with
115 : __attribute__((const)). */
116 :
117 : static bool
118 12900 : const_fn_p (const call_details &cd)
119 : {
120 12900 : tree fndecl = cd.get_fndecl_for_call ();
121 12900 : if (!fndecl)
122 : return false;
123 11454 : gcc_assert (DECL_P (fndecl));
124 11454 : return TREE_READONLY (fndecl);
125 : }
126 :
127 : /* If this CD is known to be a call to a function with
128 : __attribute__((const)), attempt to get a const_fn_result_svalue
129 : based on the arguments, or return nullptr otherwise. */
130 :
131 : static const svalue *
132 12900 : maybe_get_const_fn_result (const call_details &cd)
133 : {
134 12900 : if (!const_fn_p (cd))
135 : return nullptr;
136 :
137 708 : unsigned num_args = cd.num_args ();
138 708 : if (num_args > const_fn_result_svalue::MAX_INPUTS)
139 : /* Too many arguments. */
140 : return nullptr;
141 :
142 676 : auto_vec<const svalue *> inputs (num_args);
143 1905 : for (unsigned arg_idx = 0; arg_idx < num_args; arg_idx++)
144 : {
145 599 : const svalue *arg_sval = cd.get_arg_svalue (arg_idx);
146 599 : if (!arg_sval->can_have_associated_state_p ())
147 46 : return nullptr;
148 553 : inputs.quick_push (arg_sval);
149 : }
150 :
151 630 : region_model_manager *mgr = cd.get_manager ();
152 630 : const svalue *sval
153 630 : = mgr->get_or_create_const_fn_result_svalue (cd.get_lhs_type (),
154 : cd.get_fndecl_for_call (),
155 : inputs);
156 630 : return sval;
157 676 : }
158 :
159 : /* Look for attribute "alloc_size" on the called function and, if found,
160 : return a symbolic value of type size_type_node for the allocation size
161 : based on the call's parameters.
162 : Otherwise, return null. */
163 :
164 : static const svalue *
165 12270 : get_result_size_in_bytes (const call_details &cd)
166 : {
167 12270 : const tree attr = cd.lookup_function_attribute ("alloc_size");
168 12270 : if (!attr)
169 : return nullptr;
170 :
171 169 : const tree atval_1 = TREE_VALUE (attr);
172 169 : if (!atval_1)
173 : return nullptr;
174 :
175 169 : unsigned argidx1 = TREE_INT_CST_LOW (TREE_VALUE (atval_1)) - 1;
176 169 : if (cd.num_args () <= argidx1)
177 : return nullptr;
178 :
179 169 : const svalue *sval_arg1 = cd.get_arg_svalue (argidx1);
180 :
181 169 : if (const tree atval_2 = TREE_CHAIN (atval_1))
182 : {
183 : /* Two arguments. */
184 61 : unsigned argidx2 = TREE_INT_CST_LOW (TREE_VALUE (atval_2)) - 1;
185 61 : if (cd.num_args () <= argidx2)
186 : return nullptr;
187 61 : const svalue *sval_arg2 = cd.get_arg_svalue (argidx2);
188 : /* TODO: ideally we shouldn't need this cast here;
189 : see PR analyzer/110902. */
190 61 : return cd.get_manager ()->get_or_create_cast
191 61 : (size_type_node,
192 : cd.get_manager ()->get_or_create_binop (size_type_node,
193 : MULT_EXPR,
194 61 : sval_arg1, sval_arg2));
195 : }
196 : else
197 : /* Single argument. */
198 108 : return cd.get_manager ()->get_or_create_cast (size_type_node, sval_arg1);
199 : }
200 :
201 : /* If this call has an LHS, assign a value to it based on attributes
202 : of the function:
203 : - if __attribute__((const)), use a const_fn_result_svalue,
204 : - if __attribute__((malloc)), use a heap-allocated region with
205 : unknown content
206 : - otherwise, use a conjured_svalue.
207 :
208 : If __attribute__((alloc_size), set the dynamic extents on the region
209 : pointed to. */
210 :
211 : void
212 27145 : call_details::set_any_lhs_with_defaults () const
213 : {
214 27145 : if (!m_lhs_region)
215 : return;
216 :
217 12900 : const svalue *sval = maybe_get_const_fn_result (*this);
218 12900 : if (!sval)
219 : {
220 12270 : region_model_manager *mgr = get_manager ();
221 12270 : if (lookup_function_attribute ("malloc"))
222 : {
223 0 : const region *new_reg
224 0 : = m_model->get_or_create_region_for_heap_alloc (nullptr, m_ctxt);
225 0 : m_model->mark_region_as_unknown (new_reg, nullptr);
226 0 : sval = mgr->get_ptr_svalue (get_lhs_type (), new_reg);
227 : }
228 : else
229 : /* For the common case of functions without __attribute__((const)),
230 : use a conjured value, and purge any prior state involving that
231 : value (in case this is in a loop). */
232 12270 : sval = get_or_create_conjured_svalue (m_lhs_region);
233 12270 : if (const svalue *size_in_bytes = get_result_size_in_bytes (*this))
234 : {
235 169 : const region *reg
236 169 : = m_model->deref_rvalue (sval, NULL_TREE, m_ctxt, false);
237 169 : m_model->set_dynamic_extents (reg, size_in_bytes, m_ctxt);
238 : }
239 : }
240 12900 : maybe_set_lhs (sval);
241 : }
242 :
243 : /* Return the number of arguments used by the call statement. */
244 :
245 : unsigned
246 395121 : call_details::num_args () const
247 : {
248 395121 : return gimple_call_num_args (&m_call);
249 : }
250 :
251 : /* Return true if argument IDX is a size_t (or compatible with it). */
252 :
253 : bool
254 63880 : call_details::arg_is_size_p (unsigned idx) const
255 : {
256 63880 : return types_compatible_p (get_arg_type (idx), size_type_node);
257 : }
258 :
259 : /* Get the location of the call statement. */
260 :
261 : location_t
262 5705 : call_details::get_location () const
263 : {
264 5705 : return m_call.location;
265 : }
266 :
267 : /* Get argument IDX at the callsite as a tree. */
268 :
269 : tree
270 164074 : call_details::get_arg_tree (unsigned idx) const
271 : {
272 164074 : return gimple_call_arg (&m_call, idx);
273 : }
274 :
275 : /* Get the type of argument IDX. */
276 :
277 : tree
278 201958 : call_details::get_arg_type (unsigned idx) const
279 : {
280 201958 : return TREE_TYPE (gimple_call_arg (&m_call, idx));
281 : }
282 :
283 : /* Get argument IDX at the callsite as an svalue. */
284 :
285 : const svalue *
286 116830 : call_details::get_arg_svalue (unsigned idx) const
287 : {
288 116830 : tree arg = get_arg_tree (idx);
289 116830 : return m_model->get_rvalue (arg, m_ctxt);
290 : }
291 :
292 : /* Get argument IDX at the callsite as a symbolic::pointer. */
293 :
294 : symbolic::pointer
295 2584 : call_details::get_arg_as_sym_ptr (unsigned idx) const
296 : {
297 2584 : gcc_assert (arg_is_pointer_p (idx));
298 :
299 2584 : auto *ptr_sval = get_arg_svalue (idx);
300 2584 : const region *dest_reg
301 2584 : = m_model->deref_rvalue (ptr_sval, get_arg_tree (idx), m_ctxt);
302 2584 : return dest_reg->get_pointer (*get_manager ());
303 : }
304 :
305 : const symbolic_byte_size &
306 766 : call_details::get_arg_as_num_bytes (unsigned idx) const
307 : {
308 766 : gcc_assert (arg_is_size_p (idx));
309 :
310 766 : auto *sval = get_arg_svalue (idx);
311 :
312 766 : auto &node_mgr = get_manager ()->get_node_manager ();
313 766 : auto &bytes_mgr = node_mgr.get_scalars ().get_bytes ();
314 766 : return bytes_mgr.get_scalar_from_svalue (*sval);
315 : }
316 :
317 : /* If argument IDX's svalue at the callsite is of pointer type,
318 : return the region it points to.
319 : Otherwise return nullptr. */
320 :
321 : const region *
322 126 : call_details::deref_ptr_arg (unsigned idx) const
323 : {
324 126 : const svalue *ptr_sval = get_arg_svalue (idx);
325 126 : return m_model->deref_rvalue (ptr_sval, get_arg_tree (idx), m_ctxt);
326 : }
327 :
328 : /* Attempt to get the string literal for argument IDX, or return nullptr
329 : otherwise.
330 : For use when implementing "__analyzer_*" functions that take
331 : string literals. */
332 :
333 : const char *
334 597 : call_details::get_arg_string_literal (unsigned idx) const
335 : {
336 597 : const svalue *str_arg = get_arg_svalue (idx);
337 597 : if (auto *sym_ptr = str_arg->maybe_get_symbolic_pointer ())
338 563 : if (const string_region *string_reg = sym_ptr->get_base_region ().dyn_cast_string_region ())
339 : {
340 490 : tree string_cst = string_reg->get_string_cst ();
341 490 : return TREE_STRING_POINTER (string_cst);
342 : }
343 : return nullptr;
344 : }
345 :
346 : /* Attempt to get the fndecl used at this call, if known, or NULL_TREE
347 : otherwise. */
348 :
349 : tree
350 267245 : call_details::get_fndecl_for_call () const
351 : {
352 267245 : return m_model->get_fndecl_for_call (m_call, m_ctxt);
353 : }
354 :
355 : /* Dump a tree-like representation of this call to stderr. */
356 :
357 : DEBUG_FUNCTION void
358 0 : call_details::dump () const
359 : {
360 0 : symbolic::dump_node_tree_to_stderr<call_details> (*this);
361 0 : }
362 :
363 : std::unique_ptr<text_art::tree_widget>
364 0 : call_details::make_dump_widget (symbolic::node_dump_state &nds) const
365 : {
366 0 : using text_art::tree_widget;
367 0 : std::unique_ptr<tree_widget> cd_widget
368 0 : (tree_widget::from_fmt (nds, nullptr, "Call Details"));
369 :
370 0 : {
371 0 : pretty_printer the_pp;
372 0 : pretty_printer * const pp = &the_pp;
373 0 : pp_format_decoder (pp) = default_tree_printer;
374 0 : pp_string (pp, "gcall: ");
375 0 : pp_gimple_stmt_1 (pp, &m_call, 0 /* spc */, TDF_NONE /* flags */);
376 0 : cd_widget->add_child (tree_widget::make (nds, pp));
377 0 : }
378 0 : {
379 0 : pretty_printer the_pp;
380 0 : pretty_printer * const pp = &the_pp;
381 0 : pp_format_decoder (pp) = default_tree_printer;
382 0 : pp_string (pp, "return region: ");
383 0 : if (m_lhs_region)
384 0 : m_lhs_region->dump_to_pp (pp, true);
385 : else
386 0 : pp_string (pp, "NULL");
387 0 : auto w = tree_widget::make (nds, pp);
388 0 : if (m_lhs_region)
389 0 : w->add_child (m_lhs_region->make_dump_widget (nds));
390 0 : cd_widget->add_child (std::move (w));
391 0 : }
392 0 : if (gimple_call_num_args (&m_call) > 0)
393 : {
394 0 : std::unique_ptr<tree_widget> args_widget
395 0 : (tree_widget::from_fmt (nds, nullptr, "Arguments"));
396 0 : for (unsigned i = 0; i < gimple_call_num_args (&m_call); i++)
397 : {
398 0 : pretty_printer the_pp;
399 0 : pretty_printer * const pp = &the_pp;
400 0 : pp_format_decoder (pp) = default_tree_printer;
401 0 : const svalue *arg_sval = get_arg_svalue (i);
402 0 : pp_printf (pp, "%i: ", i);
403 0 : arg_sval->dump_to_pp (pp, true);
404 0 : auto w = tree_widget::make (nds, pp);
405 0 : w->add_child (arg_sval->make_dump_widget (nds));
406 0 : args_widget->add_child (std::move (w));
407 0 : }
408 0 : cd_widget->add_child (std::move (args_widget));
409 0 : }
410 :
411 0 : return cd_widget;
412 : }
413 :
414 : /* Get a conjured_svalue for this call for REG,
415 : and purge any state already relating to that conjured_svalue. */
416 :
417 : const svalue *
418 13365 : call_details::get_or_create_conjured_svalue (const region *reg) const
419 : {
420 13365 : region_model_manager *mgr = m_model->get_manager ();
421 13365 : return mgr->get_or_create_conjured_svalue (reg->get_type (), &m_call, reg,
422 13365 : conjured_purge (m_model, m_ctxt));
423 : }
424 :
425 : /* Look for a function attribute with name ATTR_NAME on the called
426 : function (or on its type).
427 : Return the attribute if one is found, otherwise return NULL_TREE. */
428 :
429 : tree
430 73306 : call_details::lookup_function_attribute (const char *attr_name) const
431 : {
432 73306 : tree allocfntype;
433 73306 : if (tree fndecl = get_fndecl_for_call ())
434 68779 : allocfntype = TREE_TYPE (fndecl);
435 : else
436 4527 : allocfntype = gimple_call_fntype (&m_call);
437 :
438 69668 : if (!allocfntype)
439 : return NULL_TREE;
440 :
441 69668 : return lookup_attribute (attr_name, TYPE_ATTRIBUTES (allocfntype));
442 : }
443 :
444 : void
445 2695 : call_details::check_for_null_terminated_string_arg (unsigned arg_idx) const
446 : {
447 2695 : check_for_null_terminated_string_arg (arg_idx, false, nullptr);
448 2695 : }
449 :
450 : const svalue *
451 5563 : call_details::
452 : check_for_null_terminated_string_arg (unsigned arg_idx,
453 : bool include_terminator,
454 : const symbolic::bit_vector **out_bv) const
455 : {
456 5563 : region_model *model = get_model ();
457 5563 : return model->check_for_null_terminated_string_arg (*this,
458 : arg_idx,
459 : include_terminator,
460 5563 : out_bv);
461 : }
462 :
463 : /* A subclass of pending_diagnostic for complaining about overlapping
464 : buffers. */
465 :
466 : class overlapping_buffers
467 : : public pending_diagnostic_subclass<overlapping_buffers>
468 : {
469 : public:
470 36 : overlapping_buffers (tree fndecl,
471 : const symbolic_byte_range &byte_range_a,
472 : const symbolic_byte_range &byte_range_b,
473 : const svalue *num_bytes_read_sval)
474 36 : : m_fndecl (fndecl),
475 36 : m_byte_range_a (byte_range_a),
476 36 : m_byte_range_b (byte_range_b),
477 36 : m_num_bytes_read_sval (num_bytes_read_sval)
478 : {
479 : }
480 :
481 428 : const char *get_kind () const final override
482 : {
483 428 : return "overlapping_buffers";
484 : }
485 :
486 36 : bool operator== (const overlapping_buffers &other) const
487 : {
488 36 : return m_fndecl == other.m_fndecl;
489 : }
490 :
491 68 : int get_controlling_option () const final override
492 : {
493 68 : return OPT_Wanalyzer_overlapping_buffers;
494 : }
495 :
496 32 : bool emit (diagnostic_emission_context &ctxt) final override
497 : {
498 32 : auto_diagnostic_group d;
499 :
500 32 : bool warned = ctxt.warn ("overlapping buffers passed as arguments to %qD",
501 : m_fndecl);
502 :
503 : // TODO: draw a picture?
504 :
505 32 : if (warned)
506 32 : inform (DECL_SOURCE_LOCATION (m_fndecl),
507 : "the behavior of %qD is undefined for overlapping buffers",
508 : m_fndecl);
509 :
510 64 : return warned;
511 32 : }
512 :
513 : bool
514 64 : describe_final_event (pretty_printer &pp,
515 : const evdesc::final_event &) final override
516 : {
517 64 : pp_printf (&pp,
518 : "overlapping buffers passed as arguments to %qD",
519 : m_fndecl);
520 64 : return true;
521 : }
522 :
523 0 : void maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
524 : const final override
525 : {
526 0 : auto &props = result_obj.get_or_create_properties ();
527 : #define PROPERTY_PREFIX "gcc/analyzer/overlapping_buffers/"
528 0 : props.set (PROPERTY_PREFIX "bytes_range_a",
529 0 : m_byte_range_a.to_json ());
530 0 : props.set (PROPERTY_PREFIX "bytes_range_b",
531 0 : m_byte_range_b.to_json ());
532 0 : props.set (PROPERTY_PREFIX "num_bytes_read_sval",
533 0 : m_num_bytes_read_sval->to_json ());
534 : #undef PROPERTY_PREFIX
535 0 : }
536 :
537 : private:
538 : tree m_fndecl;
539 : symbolic_byte_range m_byte_range_a;
540 : symbolic_byte_range m_byte_range_b;
541 : const svalue *m_num_bytes_read_sval;
542 : };
543 :
544 :
545 : /* Check if the buffers pointed to by arguments ARG_IDX_A and ARG_IDX_B
546 : (zero-based) overlap, when considering them both to be of size
547 : NUM_BYTES_READ_SVAL.
548 :
549 : If they do overlap, complain to the context. */
550 :
551 : void
552 1593 : call_details::complain_about_overlap (unsigned arg_idx_a,
553 : unsigned arg_idx_b,
554 : const svalue *num_bytes_read_sval) const
555 : {
556 1593 : region_model_context *ctxt = get_ctxt ();
557 1593 : if (!ctxt)
558 1557 : return;
559 :
560 908 : region_model *model = get_model ();
561 908 : region_model_manager *mgr = model->get_manager ();
562 :
563 908 : const svalue *arg_a_ptr_sval = get_arg_svalue (arg_idx_a);
564 908 : if (arg_a_ptr_sval->get_kind () == SK_UNKNOWN)
565 : return;
566 872 : const region *arg_a_reg = model->deref_rvalue (arg_a_ptr_sval,
567 : get_arg_tree (arg_idx_a),
568 : ctxt);
569 872 : const svalue *arg_b_ptr_sval = get_arg_svalue (arg_idx_b);
570 872 : if (arg_b_ptr_sval->get_kind () == SK_UNKNOWN)
571 : return;
572 864 : const region *arg_b_reg = model->deref_rvalue (arg_b_ptr_sval,
573 : get_arg_tree (arg_idx_b),
574 : ctxt);
575 864 : if (arg_a_reg->get_base_region () != arg_b_reg->get_base_region ())
576 : return;
577 :
578 72 : auto &byte_mgr = mgr->get_node_manager ().get_scalars ().get_bytes ();
579 72 : auto &num_bytes_read = byte_mgr.get_scalar_from_svalue (*num_bytes_read_sval);
580 :
581 : /* Are they within NUM_BYTES_READ_SVAL of each other? */
582 72 : symbolic_byte_range byte_range_a
583 72 : (arg_a_reg->get_pointer (*mgr).get_byte_offset (),
584 72 : num_bytes_read);
585 72 : symbolic_byte_range byte_range_b
586 72 : (arg_b_reg->get_pointer (*mgr).get_byte_offset (),
587 72 : num_bytes_read);
588 72 : if (!byte_range_a.intersects (byte_range_b, *model).is_true ())
589 : return;
590 :
591 36 : ctxt->warn (std::make_unique<overlapping_buffers> (get_fndecl_for_call (),
592 : byte_range_a,
593 : byte_range_b,
594 : num_bytes_read_sval));
595 : }
596 :
597 : } // namespace ana
598 :
599 : #endif /* #if ENABLE_ANALYZER */
|