Line data Source code
1 : /* setjmp/longjmp handling
2 : Copyright (C) 2019-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 "diagnostics/markup.h"
24 :
25 : #include "analyzer/region-model.h"
26 : #include "analyzer/checker-path.h"
27 : #include "analyzer/checker-event.h"
28 : #include "analyzer/exploded-graph.h"
29 : #include "analyzer/constraint-manager.h"
30 :
31 : #if ENABLE_ANALYZER
32 :
33 : /* Return true if stmt is a setjmp or sigsetjmp call. */
34 :
35 : bool
36 44943 : is_setjmp_call_p (const gcall &call)
37 : {
38 44943 : if (is_special_named_call_p (call, "setjmp", 1)
39 44943 : || is_special_named_call_p (call, "sigsetjmp", 2))
40 : /* region_model::on_setjmp requires a pointer. */
41 33 : if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (&call, 0))))
42 29 : return true;
43 :
44 : return false;
45 : }
46 :
47 : /* Return true if stmt is a longjmp or siglongjmp call. */
48 :
49 : bool
50 44914 : is_longjmp_call_p (const gcall &call)
51 : {
52 44914 : if (is_special_named_call_p (call, "longjmp", 2)
53 44914 : || is_special_named_call_p (call, "siglongjmp", 2))
54 : /* exploded_node::on_longjmp requires a pointer for the initial
55 : argument. */
56 45 : if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (&call, 0))))
57 41 : return true;
58 :
59 : return false;
60 : }
61 :
62 : namespace ana {
63 :
64 : /* struct setjmp_record. */
65 :
66 : int
67 36 : setjmp_record::cmp (const setjmp_record &rec1, const setjmp_record &rec2)
68 : {
69 36 : if (int cmp_enode = rec1.m_enode->m_index - rec2.m_enode->m_index)
70 : return cmp_enode;
71 0 : gcc_assert (&rec1 == &rec2);
72 : return 0;
73 : }
74 :
75 : /* class setjmp_svalue : public svalue. */
76 :
77 : bool
78 36 : setjmp_svalue::key_t::operator< (const key_t &other) const
79 : {
80 36 : if (int c = setjmp_record::cmp (m_record, other.m_record))
81 36 : return c < 0;
82 0 : return cmp_types (m_type, other.m_type) < 0;
83 : }
84 :
85 : /* Implementation of svalue::accept vfunc for setjmp_svalue. */
86 :
87 : void
88 0 : setjmp_svalue::accept (visitor *v) const
89 : {
90 0 : v->visit_setjmp_svalue (this);
91 0 : }
92 :
93 : /* Implementation of svalue::dump_to_pp vfunc for setjmp_svalue. */
94 :
95 : void
96 0 : setjmp_svalue::dump_to_pp (pretty_printer *pp, bool simple) const
97 : {
98 0 : if (simple)
99 0 : pp_printf (pp, "SETJMP(EN: %i)", get_enode_index ());
100 : else
101 0 : pp_printf (pp, "setjmp_svalue(EN%i)", get_enode_index ());
102 0 : }
103 :
104 : /* Implementation of svalue::print_dump_widget_label vfunc for
105 : setjmp_svalue. */
106 :
107 : void
108 0 : setjmp_svalue::print_svalue_for_dump_widget_label (pretty_printer *pp) const
109 : {
110 0 : pp_printf (pp, "setjmp_svalue(EN: %i)", get_enode_index ());
111 0 : }
112 :
113 : /* Implementation of svalue::add_dump_widget_children vfunc for
114 : setjmp_svalue. */
115 :
116 : void
117 0 : setjmp_svalue::
118 : add_dump_widget_children (text_art::tree_widget &,
119 : symbolic::node_dump_state &) const
120 : {
121 : /* No children. */
122 0 : }
123 :
124 : void
125 4170 : setjmp_svalue::accept_node_visitor (symbolic::node_visitor &v) const
126 : {
127 4170 : v.visit_svalue (*this);
128 4170 : }
129 :
130 : std::unique_ptr<json::object>
131 0 : setjmp_svalue::to_json () const
132 : {
133 0 : auto obj = svalue::make_json_obj ("setjmp_svalue");
134 0 : obj->set_integer ("enode_index", get_enode_index ());
135 0 : return obj;
136 : }
137 :
138 : std::unique_ptr<diagnostics::markup::node>
139 0 : setjmp_svalue::make_markup_node (symbolic::markup_context &) const
140 : {
141 0 : return diagnostics::markup::make_text_node ("setjmp_svalue");
142 : }
143 :
144 : /* Get the index of the stored exploded_node. */
145 :
146 : int
147 0 : setjmp_svalue::get_enode_index () const
148 : {
149 0 : return m_setjmp_record.m_enode->m_index;
150 : }
151 :
152 : /* Verify that the stack at LONGJMP_POINT is still valid, given a call
153 : to "setjmp" at SETJMP_POINT - the stack frame that "setjmp" was
154 : called in must still be valid.
155 :
156 : Caveat: this merely checks the call_strings in the points; it doesn't
157 : detect the case where a frame returns and is then called again. */
158 :
159 : static bool
160 111 : valid_longjmp_stack_p (const program_point &longjmp_point,
161 : const program_point &setjmp_point)
162 : {
163 111 : const call_string &cs_at_longjmp = longjmp_point.get_call_string ();
164 111 : const call_string &cs_at_setjmp = setjmp_point.get_call_string ();
165 :
166 282 : if (cs_at_longjmp.length () < cs_at_setjmp.length ())
167 : return false;
168 :
169 : /* Check that the call strings match, up to the depth of the
170 : setjmp point. */
171 150 : for (unsigned depth = 0; depth < cs_at_setjmp.length (); depth++)
172 65 : if (cs_at_longjmp[depth] != cs_at_setjmp[depth])
173 : return false;
174 :
175 : return true;
176 : }
177 :
178 : /* A pending_diagnostic subclass for complaining about bad longjmps,
179 : where the enclosing function of the "setjmp" has returned (and thus
180 : the stack frame no longer exists). */
181 :
182 : class stale_jmp_buf : public pending_diagnostic_subclass<stale_jmp_buf>
183 : {
184 : public:
185 5 : stale_jmp_buf (const gcall &setjmp_call, const gcall &longjmp_call,
186 : const program_point &setjmp_point)
187 5 : : m_setjmp_call (setjmp_call), m_longjmp_call (longjmp_call),
188 5 : m_setjmp_point (setjmp_point), m_stack_pop_event (nullptr)
189 : {}
190 :
191 10 : int get_controlling_option () const final override
192 : {
193 10 : return OPT_Wanalyzer_stale_setjmp_buffer;
194 : }
195 :
196 5 : bool emit (diagnostic_emission_context &ctxt) final override
197 : {
198 5 : return ctxt.warn ("%qs called after enclosing function of %qs has returned",
199 : get_user_facing_name (m_longjmp_call),
200 5 : get_user_facing_name (m_setjmp_call));
201 : }
202 :
203 25 : const char *get_kind () const final override
204 25 : { return "stale_jmp_buf"; }
205 :
206 5 : bool operator== (const stale_jmp_buf &other) const
207 : {
208 5 : return (&m_setjmp_call == &other.m_setjmp_call
209 5 : && &m_longjmp_call == &other.m_longjmp_call);
210 : }
211 :
212 : bool
213 51 : maybe_add_custom_events_for_eedge (const exploded_edge &eedge,
214 : checker_path *emission_path)
215 : final override
216 : {
217 : /* Detect exactly when the stack first becomes invalid,
218 : and issue an event then. */
219 51 : if (m_stack_pop_event)
220 : return false;
221 51 : const exploded_node *src_node = eedge.m_src;
222 51 : const program_point &src_point = src_node->get_point ();
223 51 : const exploded_node *dst_node = eedge.m_dest;
224 51 : const program_point &dst_point = dst_node->get_point ();
225 51 : if (valid_longjmp_stack_p (src_point, m_setjmp_point)
226 51 : && !valid_longjmp_stack_p (dst_point, m_setjmp_point))
227 : {
228 : /* Compare with diagnostic_manager::add_events_for_superedge. */
229 5 : const int src_stack_depth = src_point.get_stack_depth ();
230 10 : m_stack_pop_event = new precanned_custom_event
231 5 : (event_loc_info (src_point.get_location (),
232 : src_point.get_fndecl (),
233 10 : src_stack_depth),
234 10 : "stack frame is popped here, invalidating saved environment");
235 5 : emission_path->add_event
236 5 : (std::unique_ptr<custom_event> (m_stack_pop_event));
237 5 : return false;
238 : }
239 : return false;
240 : }
241 :
242 : bool
243 10 : describe_final_event (pretty_printer &pp,
244 : const evdesc::final_event &) final override
245 : {
246 10 : if (m_stack_pop_event)
247 10 : pp_printf (&pp,
248 : "%qs called after enclosing function of %qs returned at %@",
249 : get_user_facing_name (m_longjmp_call),
250 : get_user_facing_name (m_setjmp_call),
251 : m_stack_pop_event->get_id_ptr ());
252 : else
253 0 : pp_printf (&pp,
254 : "%qs called after enclosing function of %qs has returned",
255 : get_user_facing_name (m_longjmp_call),
256 : get_user_facing_name (m_setjmp_call));
257 10 : return true;
258 : }
259 :
260 :
261 : private:
262 : const gcall &m_setjmp_call;
263 : const gcall &m_longjmp_call;
264 : program_point m_setjmp_point;
265 : custom_event *m_stack_pop_event;
266 : };
267 :
268 : /* Update this model for a call and return of setjmp/sigsetjmp at CALL within
269 : ENODE, using CTXT to report any diagnostics.
270 :
271 : This is for the initial direct invocation of setjmp/sigsetjmp (which returns
272 : 0), as opposed to any second return due to longjmp/sigsetjmp. */
273 :
274 : void
275 34 : region_model::on_setjmp (const gcall &call,
276 : const exploded_node &enode,
277 : const superedge &sedge,
278 : region_model_context *ctxt)
279 : {
280 34 : const svalue *buf_ptr = get_rvalue (gimple_call_arg (&call, 0), ctxt);
281 34 : const region *buf_reg = deref_rvalue (buf_ptr, gimple_call_arg (&call, 0),
282 : ctxt);
283 :
284 : /* Create a setjmp_svalue for this call and store it in BUF_REG's
285 : region. */
286 34 : if (buf_reg)
287 : {
288 34 : setjmp_record r (&enode, &sedge, call);
289 34 : const svalue *sval
290 34 : = m_mgr->get_or_create_setjmp_svalue (r, buf_reg->get_type ());
291 34 : set_value (buf_reg, sval, ctxt);
292 : }
293 :
294 : /* Direct calls to setjmp return 0. */
295 34 : if (tree lhs = gimple_call_lhs (&call))
296 : {
297 16 : const svalue *new_sval
298 16 : = m_mgr->get_or_create_int_cst (TREE_TYPE (lhs), 0);
299 16 : const region *lhs_reg = get_lvalue (lhs, ctxt);
300 16 : set_value (lhs_reg, new_sval, ctxt);
301 : }
302 34 : }
303 :
304 : /* Update this region_model for rewinding from a "longjmp" at LONGJMP_CALL
305 : to a "setjmp" at SETJMP_CALL where the final stack depth should be
306 : SETJMP_STACK_DEPTH. Pop any stack frames. Leak detection is *not*
307 : done, and should be done by the caller. */
308 :
309 : void
310 42 : region_model::on_longjmp (const gcall &longjmp_call, const gcall &setjmp_call,
311 : int setjmp_stack_depth, region_model_context *ctxt)
312 : {
313 : /* Evaluate the val, using the frame of the "longjmp". */
314 42 : tree fake_retval = gimple_call_arg (&longjmp_call, 1);
315 42 : const svalue *fake_retval_sval = get_rvalue (fake_retval, ctxt);
316 :
317 : /* Pop any frames until we reach the stack depth of the function where
318 : setjmp was called. */
319 42 : gcc_assert (get_stack_depth () >= setjmp_stack_depth);
320 80 : while (get_stack_depth () > setjmp_stack_depth)
321 38 : pop_frame (nullptr, nullptr, ctxt, nullptr, false);
322 :
323 42 : gcc_assert (get_stack_depth () == setjmp_stack_depth);
324 :
325 : /* Assign to LHS of "setjmp" in new_state. */
326 42 : if (tree lhs = gimple_call_lhs (&setjmp_call))
327 : {
328 : /* Passing 0 as the val to longjmp leads to setjmp returning 1. */
329 38 : const svalue *zero_sval
330 38 : = m_mgr->get_or_create_int_cst (TREE_TYPE (fake_retval), 0);
331 38 : tristate eq_zero = eval_condition (fake_retval_sval, EQ_EXPR, zero_sval);
332 : /* If we have 0, use 1. */
333 38 : if (eq_zero.is_true ())
334 : {
335 3 : const svalue *one_sval
336 3 : = m_mgr->get_or_create_int_cst (TREE_TYPE (fake_retval), 1);
337 3 : fake_retval_sval = one_sval;
338 : }
339 : else
340 : {
341 : /* Otherwise note that the value is nonzero. */
342 35 : m_constraints->add_constraint (fake_retval_sval, NE_EXPR, zero_sval);
343 : }
344 :
345 : /* Decorate the return value from setjmp as being unmergeable,
346 : so that we don't attempt to merge states with it as zero
347 : with states in which it's nonzero, leading to a clean distinction
348 : in the exploded_graph between the first return and the second
349 : return. */
350 38 : fake_retval_sval = m_mgr->get_or_create_unmergeable (fake_retval_sval);
351 :
352 38 : const region *lhs_reg = get_lvalue (lhs, ctxt);
353 38 : set_value (lhs_reg, fake_retval_sval, ctxt);
354 : }
355 42 : }
356 :
357 : /* Handle LONGJMP_CALL, a call to longjmp or siglongjmp.
358 :
359 : Attempt to locate where setjmp/sigsetjmp was called on the jmp_buf and build
360 : an exploded_node and exploded_edge to it representing a rewind to that frame,
361 : handling the various kinds of failure that can occur. */
362 :
363 : void
364 63 : exploded_node::on_longjmp (exploded_graph &eg,
365 : const gcall &longjmp_call,
366 : program_state *new_state,
367 : region_model_context *ctxt)
368 : {
369 63 : tree buf_ptr = gimple_call_arg (&longjmp_call, 0);
370 63 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (buf_ptr)));
371 :
372 63 : region_model *new_region_model = new_state->m_region_model;
373 63 : const svalue *buf_ptr_sval = new_region_model->get_rvalue (buf_ptr, ctxt);
374 63 : const region *buf = new_region_model->deref_rvalue (buf_ptr_sval, buf_ptr,
375 : ctxt);
376 :
377 63 : const svalue *buf_content_sval
378 63 : = new_region_model->get_store_value (buf, ctxt);
379 63 : const setjmp_svalue *setjmp_sval
380 63 : = buf_content_sval->dyn_cast_setjmp_svalue ();
381 63 : if (!setjmp_sval)
382 43 : return;
383 :
384 25 : const setjmp_record tmp_setjmp_record = setjmp_sval->get_setjmp_record ();
385 :
386 : /* Build a custom enode and eedge for rewinding from the longjmp/siglongjmp
387 : call back to the setjmp/sigsetjmp. */
388 25 : rewind_info_t rewind_info (tmp_setjmp_record, longjmp_call);
389 :
390 25 : const gcall &setjmp_call = rewind_info.get_setjmp_call ();
391 25 : const program_point point_before_setjmp = rewind_info.get_point_before_setjmp ();
392 25 : const program_point point_after_setjmp = rewind_info.get_point_after_setjmp ();
393 :
394 25 : const program_point &longjmp_point = get_point ();
395 :
396 : /* Verify that the setjmp's call_stack hasn't been popped. */
397 25 : if (!valid_longjmp_stack_p (longjmp_point, point_after_setjmp))
398 : {
399 5 : ctxt->warn (std::make_unique<stale_jmp_buf> (setjmp_call,
400 : longjmp_call,
401 : point_before_setjmp));
402 5 : return;
403 : }
404 :
405 60 : gcc_assert (longjmp_point.get_stack_depth ()
406 : >= point_after_setjmp.get_stack_depth ());
407 :
408 : /* Update the state for use by the destination node. */
409 :
410 : /* Stash the current number of diagnostics so that we can update
411 : any that this adds to show where the longjmp is rewinding to. */
412 :
413 20 : diagnostic_manager *dm = &eg.get_diagnostic_manager ();
414 20 : unsigned prev_num_diagnostics = dm->get_num_diagnostics ();
415 :
416 40 : new_region_model->on_longjmp (longjmp_call, setjmp_call,
417 : point_after_setjmp.get_stack_depth (), ctxt);
418 :
419 : /* Detect leaks in the new state relative to the old state. */
420 20 : program_state::detect_leaks (get_state (), *new_state, nullptr,
421 : eg.get_ext_state (), ctxt);
422 20 : exploded_node *next
423 20 : = eg.get_or_create_node (point_after_setjmp, *new_state, this);
424 :
425 : /* Create custom exploded_edge for a longjmp. */
426 20 : if (next)
427 : {
428 20 : exploded_edge *eedge
429 20 : = eg.add_edge (const_cast<exploded_node *> (this), next, nullptr, true,
430 20 : std::make_unique<rewind_info_t> (tmp_setjmp_record,
431 : longjmp_call));
432 :
433 : /* For any diagnostics that were queued here (such as leaks) we want
434 : the checker_path to show the rewinding events after the "final event"
435 : so that the user sees where the longjmp is rewinding to (otherwise the
436 : path is meaningless).
437 :
438 : For example, we want to emit something like:
439 : | NN | {
440 : | NN | longjmp (env, 1);
441 : | | ~~~~~~~~~~~~~~~~
442 : | | |
443 : | | (10) 'ptr' leaks here; was allocated at (7)
444 : | | (11) rewinding from 'longjmp' in 'inner'...
445 : |
446 : <-------------+
447 : |
448 : 'outer': event 12
449 : |
450 : | NN | i = setjmp(env);
451 : | | ^~~~~~
452 : | | |
453 : | | (12) ...to 'setjmp' in 'outer' (saved at (2))
454 :
455 : where the "final" event above is event (10), but we want to append
456 : events (11) and (12) afterwards.
457 :
458 : Do this by setting m_trailing_eedge on any diagnostics that were
459 : just saved. */
460 20 : unsigned num_diagnostics = dm->get_num_diagnostics ();
461 28 : for (unsigned i = prev_num_diagnostics; i < num_diagnostics; i++)
462 : {
463 8 : saved_diagnostic *sd = dm->get_saved_diagnostic (i);
464 8 : sd->m_trailing_eedge = eedge;
465 : }
466 : }
467 25 : }
468 :
469 : /* class rewind_info_t : public custom_edge_info. */
470 :
471 : /* Implementation of custom_edge_info::update_model vfunc
472 : for rewind_info_t.
473 :
474 : Update state for the special-case of a rewind of a longjmp
475 : to a setjmp (which doesn't have a superedge, but does affect
476 : state). */
477 :
478 : bool
479 22 : rewind_info_t::update_model (region_model *model,
480 : const exploded_edge *eedge,
481 : region_model_context *) const
482 : {
483 22 : gcc_assert (eedge);
484 22 : const program_point &longjmp_point = eedge->m_src->get_point ();
485 22 : const program_point &setjmp_point = eedge->m_dest->get_point ();
486 :
487 66 : gcc_assert (longjmp_point.get_stack_depth ()
488 : >= setjmp_point.get_stack_depth ());
489 :
490 44 : model->on_longjmp (get_longjmp_call (),
491 : get_setjmp_call (),
492 : setjmp_point.get_stack_depth (), nullptr);
493 22 : return true;
494 : }
495 :
496 : /* Implementation of custom_edge_info::add_events_to_path vfunc
497 : for rewind_info_t. */
498 :
499 : void
500 15 : rewind_info_t::add_events_to_path (checker_path *emission_path,
501 : const exploded_edge &eedge,
502 : pending_diagnostic &,
503 : const state_transition *) const
504 : {
505 15 : const exploded_node *src_node = eedge.m_src;
506 15 : const program_point &src_point = src_node->get_point ();
507 15 : const int src_stack_depth = src_point.get_stack_depth ();
508 15 : const exploded_node *dst_node = eedge.m_dest;
509 15 : const program_point &dst_point = dst_node->get_point ();
510 15 : const int dst_stack_depth = dst_point.get_stack_depth ();
511 :
512 15 : emission_path->add_event
513 15 : (std::make_unique<rewind_from_longjmp_event>
514 15 : (&eedge,
515 15 : event_loc_info (get_longjmp_call ().location,
516 : src_point.get_fndecl (),
517 15 : src_stack_depth),
518 15 : this));
519 15 : emission_path->add_event
520 15 : (std::make_unique<rewind_to_setjmp_event>
521 15 : (&eedge,
522 15 : event_loc_info (get_setjmp_call ().location,
523 : dst_point.get_fndecl (),
524 15 : dst_stack_depth),
525 15 : this));
526 15 : }
527 :
528 : } // namespace ana
529 :
530 : #endif /* #if ENABLE_ANALYZER */
|