Line data Source code
1 : /* Implementation of <stdarg.h> within analyzer.
2 : Copyright (C) 2022-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 "analyzer/analyzer-logging.h"
24 : #include "analyzer/sm.h"
25 : #include "analyzer/pending-diagnostic.h"
26 : #include "analyzer/call-string.h"
27 : #include "analyzer/program-point.h"
28 : #include "analyzer/store.h"
29 : #include "analyzer/region-model.h"
30 : #include "analyzer/program-state.h"
31 : #include "analyzer/checker-path.h"
32 : #include "analyzer/supergraph.h"
33 : #include "analyzer/diagnostic-manager.h"
34 : #include "analyzer/exploded-graph.h"
35 : #include "analyzer/call-details.h"
36 :
37 : #if ENABLE_ANALYZER
38 :
39 : namespace ana {
40 :
41 : /* Implementation of <stdarg.h> within analyzer.
42 :
43 : Objectives:
44 : - detection of interprocedural type errors involving va_arg
45 : - tracking of symbolic values interprocedurally from variadic call
46 : through to va_arg unpacking
47 : - detection of missing va_end
48 : - detection of va_arg outside of a va_start/va_end pair
49 : - detection of uses of a va_list after the frame in containing the
50 : va_start has returned
51 :
52 : The analyzer runs *before* the "stdarg" and "lower_vaarg" gimple
53 : passes, which have target-dependent effects.
54 :
55 : This file implements a state machine on svalues for tracking when
56 : va_start has been called, so that we can detect missing va_end,
57 : and misplaced va_arg, etc.
58 : To do this requires an svalue that can have state, so we implement va_start
59 : by creating a stack-allocated region, and use a pointer to that region
60 : as the svalue that has state.
61 :
62 : We call this stack-allocated region the "impl_reg". Allocating it on
63 : the stack ensures that it is invalidated when the frame containing
64 : the va_start returns, leading to
65 : -Wanalyzer-use-of-pointer-in-stale-stack-frame on attempts to use such
66 : a va_list.
67 :
68 : To track svalues from variadic calls interprocedurally, we implement
69 : variadic arguments via new child regions of the callee's frame_region,
70 : var_arg_region, each one representing a storage slot for one of the
71 : variadic arguments, accessed by index.
72 :
73 : We have:
74 :
75 : stack frame:
76 : va_list: &impl_reg at start, then zero fill
77 : 'impl_reg': pointer to next var_arg_region
78 : var_arg_region for arg 0
79 : ...
80 : var_arg_region for arg N-1
81 :
82 : Hence given test_1 in stdarg-1.c, at the call to:
83 :
84 : __analyzer_called_by_test_1 (int placeholder, ...);
85 :
86 : here:
87 :
88 : __analyzer_called_by_test_1 (42, "foo", 1066, '@');
89 :
90 : we push this frame for the called function:
91 : clusters within frame: ‘__analyzer_called_by_test_1’@2
92 : cluster for: placeholder: (int)42
93 : cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 0): &"foo" (TOUCHED)
94 : cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 1): (int)1066 (TOUCHED)
95 : cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 2): (int)64 (TOUCHED)
96 : where the called function's frame has been populated with both the value
97 : of the regular argument "placeholder", and with values for 3 variadic
98 : arguments.
99 :
100 : At the call to
101 : va_start (ap, placeholder);
102 : we allocate a region ALLOCA_REGION for ap to point to, populate that
103 : region with the address of variadic argument 0, and set sm-state of
104 : &ALLOCA_REGION to "started":
105 : clusters within frame: ‘__analyzer_called_by_test_1’@2
106 : cluster for: placeholder: (int)42
107 : cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 0): &"foo" (TOUCHED)
108 : cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 1): (int)1066 (TOUCHED)
109 : cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 2): (int)64 (TOUCHED)
110 : cluster for: ap: &ALLOCA_REGION
111 : cluster for: ALLOCA_REGION: &VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 0) (TOUCHED)
112 : va_list:
113 : 0x4c83700: &ALLOCA_REGION: started
114 :
115 : At each call to
116 : va_arg (ap, TYPE);
117 : we can look within *ap, locate the region holding the next variadic
118 : argument to be extracted, extract the svalue, and advance the index
119 : by effectively updating *ap.
120 :
121 : At the va_end, we can set &ALLOCA_REGION's state to "ended".
122 :
123 : The various __builtin_va_* accept ap by pointer, so we have e.g.:
124 :
125 : __builtin_va_start (&ap, [...]);
126 :
127 : except for the 2nd param of __builtin_va_copy, where the type
128 : is already target-dependent (see the discussion of get_va_copy_arg
129 : below). */
130 :
131 : /* Get a tree for diagnostics.
132 : Typically we have "&ap", but it will make more sense to
133 : the user as just "ap", so strip off the ADDR_EXPR. */
134 :
135 : static tree
136 1226 : get_va_list_diag_arg (tree va_list_tree)
137 : {
138 1226 : if (TREE_CODE (va_list_tree) == ADDR_EXPR)
139 998 : va_list_tree = TREE_OPERAND (va_list_tree, 0);
140 1226 : return va_list_tree;
141 : }
142 :
143 : /* Extract a pointer from argument ARG_IDX of va_copy.
144 :
145 : builtin-types.def has:
146 : DEF_PRIMITIVE_TYPE (BT_VALIST_ARG, va_list_arg_type_node)
147 :
148 : and c_common_nodes_and_builtins initializes va_list_arg_type_node
149 : based on whether TREE_CODE (va_list_type_node) is of ARRAY_TYPE or
150 : not, giving either one or zero levels of indirection.
151 :
152 : Alternatively we could be dealing with __builtin_ms_va_copy or
153 : __builtin_sysv_va_copy.
154 :
155 : Handle this by looking at the types of the argument in question. */
156 :
157 : static const svalue *
158 111 : get_va_copy_arg (const region_model *model,
159 : region_model_context *ctxt,
160 : const gcall &call,
161 : unsigned arg_idx)
162 : {
163 111 : tree arg = gimple_call_arg (&call, arg_idx);
164 111 : auto *rmm = model->get_manager ();
165 111 : const region *src_reg;
166 111 : if (TREE_CODE (TREE_TYPE (arg)) == POINTER_TYPE
167 111 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE)
168 : {
169 : /* va_list_arg_type_node is a pointer to a va_list. */
170 91 : const svalue *arg_sval = model->get_rvalue (arg, ctxt);
171 91 : if (const svalue *cast = arg_sval->maybe_undo_cast ())
172 0 : arg_sval = cast;
173 91 : src_reg = model->deref_rvalue (arg_sval, arg, ctxt);
174 : }
175 : else
176 : {
177 : /* va_list_arg_type_node is a va_list.
178 : Read the pointer out of the first pointer-sized chunk of
179 : ARG's region. */
180 20 : src_reg = model->get_lvalue (arg, ctxt);
181 : }
182 :
183 111 : const region *cast_reg = rmm->get_cast_region (src_reg, ptr_type_node);
184 111 : const svalue *src_reg_sval = model->get_store_value (cast_reg, ctxt);
185 111 : if (const svalue *cast = src_reg_sval->maybe_undo_cast ())
186 0 : src_reg_sval = cast;
187 111 : return src_reg_sval;
188 : }
189 :
190 : namespace {
191 :
192 : /* A state machine for tracking the state of a va_list, so that
193 : we can enforce that each va_start is paired with a va_end,
194 : and va_arg only happens within a va_start/va_end pair.
195 : Specifically, this tracks the state of the &ALLOCA_BUFFER
196 : that va_start/va_copy allocate. */
197 :
198 : class va_list_state_machine : public state_machine
199 : {
200 : public:
201 : va_list_state_machine (logger *logger);
202 :
203 482140 : bool inherited_state_p () const final override { return false; }
204 :
205 : bool on_stmt (sm_context &sm_ctxt,
206 : const gimple *stmt) const final override;
207 :
208 480924 : bool can_purge_p (state_t s) const final override
209 : {
210 480924 : return s != m_started;
211 : }
212 :
213 : std::unique_ptr<pending_diagnostic>
214 : on_leak (tree var,
215 : const program_state *old_state,
216 : const program_state *new_state) const final override;
217 :
218 : /* State for a va_list that is the result of a va_start or va_copy. */
219 : state_t m_started;
220 :
221 : /* State for a va_list that has had va_end called on it. */
222 : state_t m_ended;
223 :
224 : private:
225 : void on_va_start (sm_context &sm_ctxt, const gcall &call) const;
226 : void on_va_copy (sm_context &sm_ctxt, const gcall &call) const;
227 : void on_va_arg (sm_context &sm_ctxt, const gcall &call) const;
228 : void on_va_end (sm_context &sm_ctxt, const gcall &call) const;
229 : void check_for_ended_va_list (sm_context &sm_ctxt,
230 : const svalue *arg,
231 : const char *usage_fnname) const;
232 : };
233 :
234 : /* va_list_state_machine's ctor. */
235 :
236 3573 : va_list_state_machine::va_list_state_machine (logger *logger)
237 : : state_machine ("va_list", logger),
238 7146 : m_started (add_state ("started")),
239 3573 : m_ended (add_state ("ended"))
240 : {
241 3573 : }
242 :
243 : /* Implementation of the various "va_*" functions for
244 : va_list_state_machine. */
245 :
246 : bool
247 228511 : va_list_state_machine::on_stmt (sm_context &sm_ctxt,
248 : const gimple *stmt) const
249 : {
250 228511 : if (const gcall *call_stmt = dyn_cast <const gcall *> (stmt))
251 : {
252 49557 : const gcall &call = *call_stmt;
253 :
254 49557 : if (gimple_call_internal_p (call_stmt)
255 49557 : && gimple_call_internal_fn (call_stmt) == IFN_VA_ARG)
256 : {
257 727 : on_va_arg (sm_ctxt, call);
258 727 : return false;
259 : }
260 :
261 48830 : if (tree callee_fndecl = sm_ctxt.get_fndecl_for_call (call))
262 45677 : if (fndecl_built_in_p (callee_fndecl, BUILT_IN_NORMAL)
263 45677 : && gimple_builtin_call_types_compatible_p (&call, callee_fndecl))
264 22463 : switch (DECL_UNCHECKED_FUNCTION_CODE (callee_fndecl))
265 : {
266 : default:
267 : break;
268 :
269 365 : case BUILT_IN_VA_START:
270 365 : on_va_start (sm_ctxt, call);
271 365 : break;
272 :
273 45 : case BUILT_IN_VA_COPY:
274 45 : on_va_copy (sm_ctxt, call);
275 45 : break;
276 :
277 452 : case BUILT_IN_VA_END:
278 452 : on_va_end (sm_ctxt, call);
279 452 : break;
280 : }
281 : }
282 : return false;
283 : }
284 :
285 : /* Get the svalue for which va_list_state_machine holds state on argument ARG_
286 : IDX to CALL. */
287 :
288 : static const svalue *
289 1589 : get_stateful_arg (sm_context &sm_ctxt, const gcall &call, unsigned arg_idx)
290 : {
291 1589 : tree ap = gimple_call_arg (&call, arg_idx);
292 1589 : if (ap
293 1589 : && POINTER_TYPE_P (TREE_TYPE (ap)))
294 : {
295 1589 : if (const program_state *new_state = sm_ctxt.get_new_program_state ())
296 : {
297 1589 : const region_model *new_model = new_state->m_region_model;
298 1589 : const svalue *ptr_sval = new_model->get_rvalue (ap, nullptr);
299 1589 : const region *reg = new_model->deref_rvalue (ptr_sval, ap, nullptr);
300 1589 : auto *rmm = new_model->get_manager ();
301 1589 : const region *cast_reg = rmm->get_cast_region (reg, ptr_type_node);
302 1589 : const svalue *impl_sval = new_model->get_store_value (cast_reg, nullptr);
303 1589 : if (const svalue *cast = impl_sval->maybe_undo_cast ())
304 0 : impl_sval = cast;
305 : return impl_sval;
306 : }
307 : }
308 : return nullptr;
309 : }
310 :
311 : /* Abstract class for diagnostics relating to va_list_state_machine. */
312 :
313 0 : class va_list_sm_diagnostic : public pending_diagnostic
314 : {
315 : public:
316 74 : bool subclass_equal_p (const pending_diagnostic &base_other) const override
317 : {
318 74 : const va_list_sm_diagnostic &other
319 : = (const va_list_sm_diagnostic &)base_other;
320 74 : return (m_ap_sval == other.m_ap_sval
321 74 : && same_tree_p (m_ap_tree, other.m_ap_tree));
322 : }
323 :
324 : bool
325 96 : describe_state_change (pretty_printer &pp,
326 : const evdesc::state_change &change) override
327 : {
328 96 : if (const char *fnname = maybe_get_fnname (change))
329 : {
330 96 : pp_printf (&pp, "%qs called here", fnname);
331 96 : return true;
332 : }
333 : return false;
334 : }
335 :
336 : diagnostics::paths::event::meaning
337 8 : get_meaning_for_state_change (const evdesc::state_change &change)
338 : const final override
339 : {
340 8 : if (change.m_new_state == m_sm.m_started)
341 8 : return diagnostics::paths::event::meaning (diagnostics::paths::event::verb::acquire,
342 8 : diagnostics::paths::event::noun::resource);
343 0 : if (change.m_new_state == m_sm.m_ended)
344 0 : return diagnostics::paths::event::meaning (diagnostics::paths::event::verb::release,
345 0 : diagnostics::paths::event::noun::resource);
346 0 : return diagnostics::paths::event::meaning ();
347 : }
348 :
349 : protected:
350 74 : va_list_sm_diagnostic (const va_list_state_machine &sm,
351 : const svalue *ap_sval, tree ap_tree)
352 74 : : m_sm (sm), m_ap_sval (ap_sval), m_ap_tree (ap_tree)
353 : {}
354 :
355 144 : static const char *maybe_get_fnname (const evdesc::state_change &change)
356 : {
357 144 : if (change.m_event.m_stmt)
358 144 : if (const gcall *call = as_a <const gcall *> (change.m_event.m_stmt))
359 144 : if (tree callee_fndecl = gimple_call_fndecl (call))
360 : {
361 144 : if (fndecl_built_in_p (callee_fndecl, BUILT_IN_NORMAL))
362 144 : switch (DECL_UNCHECKED_FUNCTION_CODE (callee_fndecl))
363 : {
364 92 : case BUILT_IN_VA_START:
365 92 : return "va_start";
366 28 : case BUILT_IN_VA_COPY:
367 28 : return "va_copy";
368 24 : case BUILT_IN_VA_END:
369 24 : return "va_end";
370 : }
371 : }
372 : return nullptr;
373 : }
374 :
375 : const va_list_state_machine &m_sm;
376 : const svalue *m_ap_sval;
377 : tree m_ap_tree;
378 : };
379 :
380 : /* Concrete class for -Wanalyzer-va-list-use-after-va-end:
381 : complain about use of a va_list after va_end has been called on it. */
382 :
383 0 : class va_list_use_after_va_end : public va_list_sm_diagnostic
384 : {
385 : public:
386 12 : va_list_use_after_va_end (const va_list_state_machine &sm,
387 : const svalue *ap_sval, tree ap_tree,
388 : const char *usage_fnname)
389 : : va_list_sm_diagnostic (sm, ap_sval, ap_tree),
390 12 : m_usage_fnname (usage_fnname)
391 : {
392 : }
393 :
394 24 : int get_controlling_option () const final override
395 : {
396 24 : return OPT_Wanalyzer_va_list_use_after_va_end;
397 : }
398 :
399 : bool operator== (const va_list_use_after_va_end &other) const
400 : {
401 : return (va_list_sm_diagnostic::subclass_equal_p (other)
402 : && 0 == strcmp (m_usage_fnname, other.m_usage_fnname));
403 : }
404 :
405 12 : bool emit (diagnostic_emission_context &ctxt) final override
406 : {
407 12 : return ctxt.warn ("%qs after %qs", m_usage_fnname, "va_end");
408 : }
409 :
410 388 : const char *get_kind () const final override
411 : {
412 388 : return "va_list_use_after_va_end";
413 : }
414 :
415 : bool
416 48 : describe_state_change (pretty_printer &pp,
417 : const evdesc::state_change &change) final override
418 : {
419 48 : if (change.m_new_state == m_sm.m_ended)
420 24 : m_va_end_event = change.m_event_id;
421 48 : return va_list_sm_diagnostic::describe_state_change (pp, change);
422 : }
423 :
424 : bool
425 24 : describe_final_event (pretty_printer &pp,
426 : const evdesc::final_event &ev) final override
427 : {
428 24 : if (ev.m_expr)
429 : {
430 0 : if (m_va_end_event.known_p ())
431 0 : pp_printf (&pp,
432 : "%qs on %qE after %qs at %@",
433 : m_usage_fnname, ev.m_expr, "va_end", &m_va_end_event);
434 : else
435 0 : pp_printf (&pp,
436 : "%qs on %qE after %qs",
437 : m_usage_fnname, ev.m_expr, "va_end");
438 : }
439 : else
440 : {
441 24 : if (m_va_end_event.known_p ())
442 24 : pp_printf (&pp,
443 : "%qs after %qs at %@",
444 : m_usage_fnname, "va_end", &m_va_end_event);
445 : else
446 0 : pp_printf (&pp,
447 : "%qs after %qs",
448 : m_usage_fnname, "va_end");
449 : }
450 24 : return true;
451 : }
452 :
453 : private:
454 : diagnostics::paths::event_id_t m_va_end_event;
455 : const char *m_usage_fnname;
456 : };
457 :
458 : /* Concrete class for -Wanalyzer-va-list-leak:
459 : complain about a va_list in the "started" state that doesn't get after
460 : va_end called on it. */
461 :
462 : class va_list_leak : public va_list_sm_diagnostic
463 : {
464 : public:
465 62 : va_list_leak (const va_list_state_machine &sm,
466 : const svalue *ap_sval, tree ap_tree,
467 : const program_state *final_state)
468 : : va_list_sm_diagnostic (sm, ap_sval, ap_tree),
469 62 : m_start_event_fnname (nullptr),
470 62 : m_final_state ()
471 : {
472 62 : if (final_state)
473 62 : m_final_state = std::make_unique<program_state> (*final_state);
474 62 : }
475 :
476 86 : int get_controlling_option () const final override
477 : {
478 86 : return OPT_Wanalyzer_va_list_leak;
479 : }
480 :
481 : bool operator== (const va_list_leak &other) const
482 : {
483 : return va_list_sm_diagnostic::subclass_equal_p (other);
484 : }
485 :
486 24 : bool emit (diagnostic_emission_context &ctxt) final override
487 : {
488 24 : return ctxt.warn ("missing call to %qs", "va_end");
489 : }
490 :
491 822 : const char *get_kind () const final override { return "va_list_leak"; }
492 :
493 : bool
494 48 : describe_state_change (pretty_printer &pp,
495 : const evdesc::state_change &change) final override
496 : {
497 48 : if (change.m_new_state == m_sm.m_started)
498 : {
499 48 : m_start_event = change.m_event_id;
500 48 : m_start_event_fnname = maybe_get_fnname (change);
501 : }
502 48 : return va_list_sm_diagnostic::describe_state_change (pp, change);
503 : }
504 :
505 : bool
506 48 : describe_final_event (pretty_printer &pp,
507 : const evdesc::final_event &ev) final override
508 : {
509 48 : if (ev.m_expr)
510 : {
511 10 : if (m_start_event.known_p () && m_start_event_fnname)
512 10 : pp_printf (&pp,
513 : "missing call to %qs on %qE to match %qs at %@",
514 : "va_end", ev.m_expr, m_start_event_fnname, &m_start_event);
515 : else
516 0 : pp_printf (&pp,
517 : "missing call to %qs on %qE",
518 : "va_end", ev.m_expr);
519 : }
520 : else
521 : {
522 38 : if (m_start_event.known_p () && m_start_event_fnname)
523 38 : pp_printf (&pp,
524 : "missing call to %qs to match %qs at %@",
525 : "va_end", m_start_event_fnname, &m_start_event);
526 : else
527 0 : pp_printf (&pp,
528 : "missing call to %qs",
529 : "va_end");
530 : }
531 48 : return true;
532 : }
533 :
534 : const program_state *
535 24 : get_final_state () const final override
536 : {
537 24 : return m_final_state.get ();
538 : }
539 :
540 : private:
541 : diagnostics::paths::event_id_t m_start_event;
542 : const char *m_start_event_fnname;
543 : std::unique_ptr<program_state> m_final_state;
544 : };
545 :
546 : /* Update state machine for a "va_start" call. */
547 :
548 : void
549 365 : va_list_state_machine::on_va_start (sm_context &sm_ctxt,
550 : const gcall &call) const
551 : {
552 365 : const svalue *arg = get_stateful_arg (sm_ctxt, call, 0);
553 365 : if (arg)
554 : {
555 : /* Transition from start state to "started". */
556 365 : if (sm_ctxt.get_state (arg) == m_start)
557 365 : sm_ctxt.set_next_state (arg, m_started);
558 : }
559 365 : }
560 :
561 : /* Complain if ARG is in the "ended" state. */
562 :
563 : void
564 776 : va_list_state_machine::check_for_ended_va_list (sm_context &sm_ctxt,
565 : const svalue *arg,
566 : const char *usage_fnname) const
567 : {
568 776 : if (sm_ctxt.get_state (arg) == m_ended)
569 12 : sm_ctxt.warn (arg,
570 : std::make_unique<va_list_use_after_va_end>
571 12 : (*this, arg, NULL_TREE, usage_fnname));
572 776 : }
573 :
574 : /* Get the svalue with associated va_list_state_machine state for
575 : ARG_IDX of CALL to va_copy, if SM_CTXT supports this,
576 : or nullptr otherwise. */
577 :
578 : static const svalue *
579 45 : get_stateful_va_copy_arg (sm_context &sm_ctxt,
580 : const gcall &call,
581 : unsigned arg_idx)
582 : {
583 45 : if (const program_state *new_state = sm_ctxt.get_new_program_state ())
584 : {
585 45 : const region_model *new_model = new_state->m_region_model;
586 45 : const svalue *arg = get_va_copy_arg (new_model, nullptr, call, arg_idx);
587 45 : return arg;
588 : }
589 : return nullptr;
590 : }
591 :
592 : /* Update state machine for a "va_copy" call. */
593 :
594 : void
595 45 : va_list_state_machine::on_va_copy (sm_context &sm_ctxt,
596 : const gcall &call) const
597 : {
598 45 : const svalue *src_arg = get_stateful_va_copy_arg (sm_ctxt, call, 1);
599 45 : if (src_arg)
600 45 : check_for_ended_va_list (sm_ctxt, src_arg, "va_copy");
601 :
602 45 : const svalue *dst_arg = get_stateful_arg (sm_ctxt, call, 0);
603 45 : if (dst_arg)
604 : {
605 : /* Transition from start state to "started". */
606 45 : if (sm_ctxt.get_state (dst_arg) == m_start)
607 45 : sm_ctxt.set_next_state (dst_arg, m_started);
608 : }
609 45 : }
610 :
611 : /* Update state machine for a "va_arg" call. */
612 :
613 : void
614 727 : va_list_state_machine::on_va_arg (sm_context &sm_ctxt,
615 : const gcall &call) const
616 : {
617 727 : const svalue *arg = get_stateful_arg (sm_ctxt, call, 0);
618 727 : if (arg)
619 727 : check_for_ended_va_list (sm_ctxt, arg, "va_arg");
620 727 : }
621 :
622 : /* Update state machine for a "va_end" call. */
623 :
624 : void
625 452 : va_list_state_machine::on_va_end (sm_context &sm_ctxt,
626 : const gcall &call) const
627 : {
628 452 : const svalue *arg = get_stateful_arg (sm_ctxt, call, 0);
629 452 : if (arg)
630 : {
631 452 : state_t s = sm_ctxt.get_state (arg);
632 : /* Transition from "started" to "ended". */
633 452 : if (s == m_started)
634 407 : sm_ctxt.set_next_state (arg, m_ended);
635 45 : else if (s == m_ended)
636 4 : check_for_ended_va_list (sm_ctxt, arg, "va_end");
637 : }
638 452 : }
639 :
640 : /* Implementation of state_machine::on_leak vfunc for va_list_state_machine
641 : (for complaining about leaks of values in state 'started'). */
642 :
643 : std::unique_ptr<pending_diagnostic>
644 62 : va_list_state_machine::on_leak (tree var,
645 : const program_state *,
646 : const program_state *new_state) const
647 : {
648 62 : return std::make_unique<va_list_leak> (*this, nullptr, var, new_state);
649 : }
650 :
651 : } // anonymous namespace
652 :
653 : /* Internal interface to this file. */
654 :
655 : std::unique_ptr<state_machine>
656 3573 : make_va_list_state_machine (logger *logger)
657 : {
658 3573 : return std::make_unique<va_list_state_machine> (logger);
659 : }
660 :
661 : /* Handler for "__builtin_va_start". */
662 :
663 3573 : class kf_va_start : public known_function
664 : {
665 : public:
666 0 : bool matches_call_types_p (const call_details &) const final override
667 : {
668 0 : return true;
669 : }
670 : void impl_call_pre (const call_details &cd) const final override;
671 : };
672 :
673 : void
674 542 : kf_va_start::impl_call_pre (const call_details &cd) const
675 : {
676 542 : region_model *model = cd.get_model ();
677 542 : region_model_manager *mgr = cd.get_manager ();
678 542 : const svalue *out_ptr = cd.get_arg_svalue (0);
679 542 : const region *out_reg
680 542 : = model->deref_rvalue (out_ptr, cd.get_arg_tree (0), cd.get_ctxt ());
681 542 : const frame_region *frame = model->get_current_frame ();
682 :
683 : /* "*out_ptr = &IMPL_REGION;". */
684 542 : const region *impl_reg = mgr->create_region_for_alloca (frame);
685 :
686 : /* We abuse the types here, since va_list_type isn't
687 : necessarily anything to do with a pointer. */
688 542 : const svalue *ptr_to_impl_reg = mgr->get_ptr_svalue (ptr_type_node, impl_reg);
689 542 : model->zero_fill_region (out_reg, cd.get_ctxt ());
690 542 : model->set_value (out_reg, ptr_to_impl_reg, cd.get_ctxt ());
691 :
692 542 : if (model->get_stack_depth () > 1)
693 : {
694 : /* The interprocedural case: the frame containing the va_start call
695 : will have been populated with any variadic aruguments.
696 : Initialize IMPL_REGION with a ptr to var_arg_region 0. */
697 278 : const region *init_var_arg_reg = mgr->get_var_arg_region (frame, 0);
698 278 : const svalue *ap_sval
699 278 : = mgr->get_ptr_svalue (ptr_type_node, init_var_arg_reg);
700 278 : model->set_value (impl_reg, ap_sval, cd.get_ctxt ());
701 : }
702 : else
703 : {
704 : /* The frame containing va_start is an entry-point to the analysis,
705 : so there won't be any specific var_arg_regions populated within it. */
706 264 : model->mark_region_as_unknown (impl_reg, nullptr);
707 : }
708 542 : }
709 :
710 : /* Handler for "__builtin_va_copy". */
711 :
712 3573 : class kf_va_copy : public known_function
713 : {
714 : public:
715 0 : bool matches_call_types_p (const call_details &) const final override
716 : {
717 0 : return true;
718 : }
719 : void impl_call_pre (const call_details &cd) const final override;
720 : };
721 :
722 : void
723 66 : kf_va_copy::impl_call_pre (const call_details &cd) const
724 : {
725 66 : region_model *model = cd.get_model ();
726 66 : region_model_manager *mgr = cd.get_manager ();
727 66 : const svalue *out_dst_ptr = cd.get_arg_svalue (0);
728 66 : const svalue *in_impl_ptr
729 66 : = get_va_copy_arg (model, cd.get_ctxt (), cd.get_call_stmt (), 1);
730 66 : in_impl_ptr
731 66 : = model->check_for_poison (in_impl_ptr,
732 : get_va_list_diag_arg (cd.get_arg_tree (1)),
733 : nullptr,
734 : cd.get_ctxt ());
735 :
736 66 : const region *out_dst_reg
737 66 : = model->deref_rvalue (out_dst_ptr, cd.get_arg_tree (0), cd.get_ctxt ());
738 :
739 : /* "*out_dst_ptr = &NEW_IMPL_REGION;". */
740 66 : const region *new_impl_reg
741 66 : = mgr->create_region_for_alloca (model->get_current_frame ());
742 66 : const svalue *ptr_to_new_impl_reg
743 66 : = mgr->get_ptr_svalue (ptr_type_node, new_impl_reg);
744 66 : model->set_value (out_dst_reg, ptr_to_new_impl_reg, cd.get_ctxt ());
745 :
746 66 : const region *old_impl_reg = model->deref_rvalue (in_impl_ptr,
747 : NULL_TREE,
748 : cd.get_ctxt ());
749 66 : old_impl_reg = mgr->get_cast_region (old_impl_reg, ptr_type_node);
750 66 : new_impl_reg = mgr->get_cast_region (new_impl_reg, ptr_type_node);
751 :
752 : /* "(NEW_IMPL_REGION) = (OLD_IMPL_REGION);". */
753 66 : const svalue *existing_sval
754 66 : = model->get_store_value (old_impl_reg, cd.get_ctxt ());
755 66 : model->set_value (new_impl_reg, existing_sval, cd.get_ctxt ());
756 66 : }
757 :
758 : /* Get the number of variadic arguments to CALLEE_FNDECL at CALL_STMT. */
759 :
760 : static int
761 34 : get_num_variadic_arguments (tree callee_fndecl,
762 : const gcall &call_stmt)
763 : {
764 34 : int num_positional = 0;
765 68 : for (tree iter_parm = DECL_ARGUMENTS (callee_fndecl); iter_parm;
766 34 : iter_parm = DECL_CHAIN (iter_parm))
767 34 : num_positional++;
768 34 : return gimple_call_num_args (&call_stmt) - num_positional;
769 : }
770 :
771 : /* An abstract subclass of pending_diagnostic for diagnostics relating
772 : to bad va_arg invocations.
773 :
774 : This shows the number of variadic arguments at the call of interest.
775 : Ideally we'd also be able to highlight individual arguments, but
776 : that location information isn't generally available from the middle end. */
777 :
778 : class va_arg_diagnostic : public pending_diagnostic
779 : {
780 : public:
781 : /* Override of pending_diagnostic::add_call_event,
782 : adding a custom call_event subclass. */
783 42 : void add_call_event (const exploded_edge &eedge,
784 : const gcall &call_stmt,
785 : checker_path &emission_path,
786 : const state_transition_at_call *state_trans) override
787 : {
788 : /* As per call_event, but show the number of variadic arguments
789 : in the call. */
790 0 : class va_arg_call_event : public call_event
791 : {
792 : public:
793 34 : va_arg_call_event (const exploded_edge &eedge,
794 : const event_loc_info &loc_info,
795 : int num_variadic_arguments)
796 : : call_event (eedge, loc_info, nullptr),
797 34 : m_num_variadic_arguments (num_variadic_arguments)
798 : {
799 : }
800 :
801 68 : void print_desc (pretty_printer &pp) const override
802 : {
803 68 : pp_printf_n (&pp,
804 68 : m_num_variadic_arguments,
805 : "calling %qE from %qE with %i variadic argument",
806 : "calling %qE from %qE with %i variadic arguments",
807 : get_callee_fndecl (),
808 : get_caller_fndecl (),
809 68 : m_num_variadic_arguments);
810 68 : }
811 : private:
812 : int m_num_variadic_arguments;
813 : };
814 :
815 42 : const frame_region *frame_reg = m_var_arg_reg->get_frame_region ();
816 42 : const exploded_node *dst_node = eedge.m_dest;
817 42 : if (dst_node->get_state ().m_region_model->get_current_frame ()
818 : == frame_reg)
819 : {
820 34 : int num_variadic_arguments
821 34 : = get_num_variadic_arguments (dst_node->get_function ()->decl,
822 34 : call_stmt);
823 34 : emission_path.add_event
824 34 : (std::make_unique<va_arg_call_event>
825 34 : (eedge,
826 68 : event_loc_info (eedge.m_src),
827 : num_variadic_arguments));
828 : }
829 : else
830 8 : pending_diagnostic::add_call_event (eedge, call_stmt, emission_path,
831 : state_trans);
832 42 : }
833 :
834 : protected:
835 51 : va_arg_diagnostic (tree va_list_tree, const var_arg_region *var_arg_reg)
836 51 : : m_va_list_tree (va_list_tree), m_var_arg_reg (var_arg_reg)
837 : {}
838 :
839 37 : bool subclass_equal_p (const pending_diagnostic &base_other) const override
840 : {
841 37 : const va_arg_diagnostic &other = (const va_arg_diagnostic &)base_other;
842 37 : return (same_tree_p (m_va_list_tree, other.m_va_list_tree)
843 37 : && m_var_arg_reg == other.m_var_arg_reg);
844 : }
845 :
846 : /* Get the number of arguments consumed so far from the va_list
847 : (*before* this va_arg call). */
848 102 : unsigned get_num_consumed () const
849 : {
850 102 : return m_var_arg_reg->get_index ();
851 : }
852 :
853 : /* Get a 1-based index of which variadic argument is being consumed. */
854 63 : unsigned get_variadic_index_for_diagnostic () const
855 : {
856 63 : return get_num_consumed () + 1;
857 : }
858 :
859 : /* User-readable expr for the va_list argument to va_arg. */
860 : tree m_va_list_tree;
861 :
862 : /* The region that the va_arg attempted to access. */
863 : const var_arg_region *m_var_arg_reg;
864 : };
865 :
866 : /* A subclass of pending_diagnostic for complaining about a type mismatch
867 : between the result of:
868 : va_arg (AP);
869 : and the type of the argument that was passed to the variadic call. */
870 :
871 : class va_arg_type_mismatch : public va_arg_diagnostic
872 : {
873 : public:
874 25 : va_arg_type_mismatch (tree va_list_tree, const var_arg_region *var_arg_reg,
875 : tree expected_type, tree actual_type)
876 : : va_arg_diagnostic (va_list_tree, var_arg_reg),
877 25 : m_expected_type (expected_type), m_actual_type (actual_type)
878 : {}
879 :
880 498 : const char *get_kind () const final override
881 : {
882 498 : return "va_arg_type_mismatch";
883 : }
884 :
885 24 : bool subclass_equal_p (const pending_diagnostic &base_other)
886 : const final override
887 : {
888 24 : if (!va_arg_diagnostic::subclass_equal_p (base_other))
889 : return false;
890 21 : const va_arg_type_mismatch &other
891 : = (const va_arg_type_mismatch &)base_other;
892 21 : return (same_tree_p (m_expected_type, other.m_expected_type)
893 21 : && same_tree_p (m_actual_type, other.m_actual_type));
894 : }
895 :
896 42 : int get_controlling_option () const final override
897 : {
898 42 : return OPT_Wanalyzer_va_arg_type_mismatch;
899 : }
900 :
901 21 : bool emit (diagnostic_emission_context &ctxt) final override
902 : {
903 : /* "CWE-686: Function Call With Incorrect Argument Type". */
904 21 : ctxt.add_cwe (686);
905 21 : bool warned
906 21 : = ctxt.warn ("%<va_arg%> expected %qT but received %qT"
907 : " for variadic argument %i of %qE",
908 : m_expected_type, m_actual_type,
909 : get_variadic_index_for_diagnostic (), m_va_list_tree);
910 21 : return warned;
911 : }
912 :
913 : bool
914 42 : describe_final_event (pretty_printer &pp,
915 : const evdesc::final_event &) final override
916 : {
917 42 : pp_printf (&pp,
918 : "%<va_arg%> expected %qT but received %qT"
919 : " for variadic argument %i of %qE",
920 : m_expected_type, m_actual_type,
921 : get_variadic_index_for_diagnostic (),
922 : m_va_list_tree);
923 42 : return true;
924 : }
925 :
926 : private:
927 : tree m_expected_type;
928 : tree m_actual_type;
929 : };
930 :
931 : /* A subclass of pending_diagnostic for complaining about a
932 : va_arg (AP);
933 : after all of the args in AP have been consumed. */
934 :
935 : class va_list_exhausted : public va_arg_diagnostic
936 : {
937 : public:
938 26 : va_list_exhausted (tree va_list_tree, const var_arg_region *var_arg_reg)
939 26 : : va_arg_diagnostic (va_list_tree, var_arg_reg)
940 : {}
941 :
942 134 : const char *get_kind () const final override
943 : {
944 134 : return "va_list_exhausted";
945 : }
946 :
947 26 : int get_controlling_option () const final override
948 : {
949 26 : return OPT_Wanalyzer_va_list_exhausted;
950 : }
951 :
952 13 : bool emit (diagnostic_emission_context &ctxt) final override
953 : {
954 : /* CWE-685: Function Call With Incorrect Number of Arguments. */
955 13 : ctxt.add_cwe (685);
956 13 : bool warned = ctxt.warn ("%qE has no more arguments (%i consumed)",
957 : m_va_list_tree, get_num_consumed ());
958 13 : return warned;
959 : }
960 :
961 : bool
962 26 : describe_final_event (pretty_printer &pp,
963 : const evdesc::final_event &) final override
964 : {
965 26 : pp_printf (&pp,
966 : "%qE has no more arguments (%i consumed)",
967 : m_va_list_tree, get_num_consumed ());
968 26 : return true;
969 : }
970 : };
971 :
972 : static bool
973 8 : representable_in_integral_type_p (const svalue &sval, const_tree type)
974 : {
975 8 : gcc_assert (INTEGRAL_TYPE_P (type));
976 :
977 8 : if (tree cst = sval.maybe_get_constant ())
978 8 : return wi::fits_to_tree_p (wi::to_wide (cst), type);
979 :
980 : return true;
981 : }
982 :
983 : /* Return true if it's OK to copy ARG_SVAL from ARG_TYPE to LHS_TYPE via
984 : va_arg (where argument promotion has already happened). */
985 :
986 : static bool
987 557 : va_arg_compatible_types_p (tree lhs_type, tree arg_type, const svalue &arg_sval)
988 : {
989 557 : if (compat_types_p (arg_type, lhs_type))
990 : return true;
991 :
992 : /* It's OK if both types are integer types, where one is signed and the
993 : other type the corresponding unsigned type, when the value is
994 : representable in both types. */
995 29 : if (INTEGRAL_TYPE_P (lhs_type)
996 17 : && INTEGRAL_TYPE_P (arg_type)
997 10 : && TYPE_UNSIGNED (lhs_type) != TYPE_UNSIGNED (arg_type)
998 4 : && TYPE_PRECISION (lhs_type) == TYPE_PRECISION (arg_type)
999 4 : && representable_in_integral_type_p (arg_sval, lhs_type)
1000 33 : && representable_in_integral_type_p (arg_sval, arg_type))
1001 : return true;
1002 :
1003 : /* It's OK if one type is a pointer to void and the other is a
1004 : pointer to a character type.
1005 : This is handled by compat_types_p. */
1006 :
1007 : /* Otherwise the types are not compatible. */
1008 : return false;
1009 : }
1010 :
1011 : /* If AP_SVAL is a pointer to a var_arg_region, return that var_arg_region.
1012 : Otherwise return nullptr. */
1013 :
1014 : static const var_arg_region *
1015 1160 : maybe_get_var_arg_region (region_model *model,
1016 : const svalue *ap_sval)
1017 : {
1018 1160 : const region *reg = model->deref_rvalue (ap_sval, NULL_TREE, nullptr, false);
1019 1160 : return reg->dyn_cast_var_arg_region ();
1020 : }
1021 :
1022 : /* Attempt to extract a typed svalue from VAR_ARG_REG in MODEL, returning
1023 : nullptr if unsuccessful.
1024 : Normally the store has no conception of types; this relies on matching
1025 : the shape of how interprocedural arguments are recorded in frames so
1026 : that we can reconstruct the types of variadic arguments. */
1027 :
1028 : static const svalue *
1029 583 : maybe_get_var_arg_svalue (region_model *model,
1030 : const var_arg_region *var_arg_reg)
1031 : {
1032 : /* A var_arg_region has no inherent size.
1033 : */
1034 583 : auto *cluster = model->get_store ()->get_cluster (var_arg_reg);
1035 583 : if (!cluster)
1036 : return nullptr;
1037 557 : auto &contents = cluster->get_bit_contents ();
1038 557 : auto *conc_bit_overlay = contents.dyn_cast_concrete_bit_overlay ();
1039 557 : if (!conc_bit_overlay)
1040 : return nullptr;
1041 :
1042 557 : return conc_bit_overlay->maybe_get_var_arg_svalue ();
1043 : }
1044 :
1045 : /* Handler for "__builtin_va_arg". */
1046 :
1047 3573 : class kf_va_arg : public internal_known_function
1048 : {
1049 : public:
1050 : void impl_call_pre (const call_details &cd) const final override;
1051 : };
1052 :
1053 : void
1054 1160 : kf_va_arg::impl_call_pre (const call_details &cd) const
1055 : {
1056 1160 : region_model_context *ctxt = cd.get_ctxt ();
1057 1160 : region_model *model = cd.get_model ();
1058 1160 : region_model_manager *mgr = cd.get_manager ();
1059 :
1060 1160 : const svalue *in_ptr = cd.get_arg_svalue (0);
1061 1160 : const region *ap_reg
1062 1160 : = model->deref_rvalue (in_ptr, cd.get_arg_tree (0), ctxt);
1063 :
1064 1160 : ap_reg = mgr->get_cast_region (ap_reg, ptr_type_node);
1065 :
1066 1160 : const svalue *ap_sval = model->get_store_value (ap_reg, ctxt);
1067 1160 : if (const svalue *cast = ap_sval->maybe_undo_cast ())
1068 0 : ap_sval = cast;
1069 :
1070 1160 : tree va_list_tree = get_va_list_diag_arg (cd.get_arg_tree (0));
1071 1160 : ap_sval = model->check_for_poison (ap_sval, va_list_tree, ap_reg, ctxt);
1072 :
1073 1160 : cd.set_any_lhs_with_defaults ();
1074 :
1075 1160 : if (!(ap_sval->get_type ()
1076 1160 : && POINTER_TYPE_P (ap_sval->get_type ())))
1077 577 : return;
1078 :
1079 1160 : const region *impl_reg
1080 1160 : = model->deref_rvalue (ap_sval, NULL_TREE, ctxt, false);
1081 1160 : impl_reg = mgr->get_cast_region (impl_reg, ptr_type_node);
1082 1160 : const svalue *old_impl_sval = model->get_store_value (impl_reg, ctxt);
1083 1160 : const var_arg_region *arg_reg = maybe_get_var_arg_region (model,
1084 1160 : old_impl_sval);
1085 1160 : if (!arg_reg)
1086 : return;
1087 :
1088 583 : bool saw_problem = false;
1089 :
1090 583 : const frame_region *frame_reg = arg_reg->get_frame_region ();
1091 583 : unsigned next_arg_idx = arg_reg->get_index ();
1092 :
1093 583 : if (frame_reg->get_stack_depth () > 1)
1094 : {
1095 : /* The interprocedural case: the called frame will have been
1096 : populated with any variadic aruguments.
1097 : Attempt to extract arg_reg to cd's return region (which already
1098 : has a conjured_svalue), or warn if there's a problem
1099 : (incompatible types, or if we've run out of args). */
1100 583 : if (const svalue *arg_sval = maybe_get_var_arg_svalue (model,
1101 : arg_reg))
1102 : {
1103 557 : tree lhs_type = cd.get_lhs_type ();
1104 557 : tree arg_type = arg_sval->get_type ();
1105 557 : if (va_arg_compatible_types_p (lhs_type, arg_type, *arg_sval))
1106 532 : cd.maybe_set_lhs (arg_sval);
1107 : else
1108 : {
1109 25 : if (ctxt)
1110 25 : ctxt->warn (std::make_unique <va_arg_type_mismatch>
1111 25 : (va_list_tree,
1112 : arg_reg,
1113 : lhs_type,
1114 : arg_type));
1115 : saw_problem = true;
1116 : }
1117 : }
1118 : else
1119 : {
1120 26 : if (ctxt)
1121 26 : ctxt->warn
1122 26 : (std::make_unique <va_list_exhausted> (va_list_tree,
1123 : arg_reg));
1124 : saw_problem = true;
1125 : }
1126 : }
1127 : else
1128 : {
1129 : /* This frame is an entry-point to the analysis, so there won't be
1130 : any specific var_arg_regions populated within it.
1131 : We already have a conjured_svalue for the result, so leave
1132 : it untouched. */
1133 0 : gcc_assert (frame_reg->get_stack_depth () == 1);
1134 : }
1135 :
1136 583 : if (saw_problem)
1137 : {
1138 : /* Set impl_reg to UNKNOWN to suppress further warnings. */
1139 51 : const svalue *new_ap_sval
1140 51 : = mgr->get_or_create_unknown_svalue (impl_reg->get_type ());
1141 51 : model->set_value (impl_reg, new_ap_sval, ctxt);
1142 : }
1143 : else
1144 : {
1145 : /* Update impl_reg to advance to the next arg. */
1146 532 : const region *next_var_arg_region
1147 532 : = mgr->get_var_arg_region (frame_reg, next_arg_idx + 1);
1148 532 : const svalue *new_ap_sval
1149 532 : = mgr->get_ptr_svalue (ptr_type_node, next_var_arg_region);
1150 532 : model->set_value (impl_reg, new_ap_sval, ctxt);
1151 : }
1152 : }
1153 :
1154 : /* Handler for "__builtin_va_end". */
1155 :
1156 3573 : class kf_va_end : public known_function
1157 : {
1158 : public:
1159 0 : bool matches_call_types_p (const call_details &) const
1160 : {
1161 0 : return true;
1162 : }
1163 : };
1164 :
1165 : /* Populate KFM with instances of known functions relating to varargs. */
1166 :
1167 : void
1168 3573 : register_varargs_builtins (known_function_manager &kfm)
1169 : {
1170 3573 : kfm.add (BUILT_IN_VA_START, std::make_unique<kf_va_start> ());
1171 3573 : kfm.add (BUILT_IN_VA_COPY, std::make_unique<kf_va_copy> ());
1172 3573 : kfm.add (IFN_VA_ARG, std::make_unique<kf_va_arg> ());
1173 3573 : kfm.add (BUILT_IN_VA_END, std::make_unique<kf_va_end> ());
1174 3573 : }
1175 :
1176 : } // namespace ana
1177 :
1178 : #endif /* #if ENABLE_ANALYZER */
|