xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/analyzer/sm-signal.cc (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1*4c3eb207Smrg /* An experimental state machine, for tracking bad calls from within
2*4c3eb207Smrg    signal handlers.
3*4c3eb207Smrg 
4*4c3eb207Smrg    Copyright (C) 2019-2020 Free Software Foundation, Inc.
5*4c3eb207Smrg    Contributed by David Malcolm <dmalcolm@redhat.com>.
6*4c3eb207Smrg 
7*4c3eb207Smrg This file is part of GCC.
8*4c3eb207Smrg 
9*4c3eb207Smrg GCC is free software; you can redistribute it and/or modify it
10*4c3eb207Smrg under the terms of the GNU General Public License as published by
11*4c3eb207Smrg the Free Software Foundation; either version 3, or (at your option)
12*4c3eb207Smrg any later version.
13*4c3eb207Smrg 
14*4c3eb207Smrg GCC is distributed in the hope that it will be useful, but
15*4c3eb207Smrg WITHOUT ANY WARRANTY; without even the implied warranty of
16*4c3eb207Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17*4c3eb207Smrg General Public License for more details.
18*4c3eb207Smrg 
19*4c3eb207Smrg You should have received a copy of the GNU General Public License
20*4c3eb207Smrg along with GCC; see the file COPYING3.  If not see
21*4c3eb207Smrg <http://www.gnu.org/licenses/>.  */
22*4c3eb207Smrg 
23*4c3eb207Smrg #include "config.h"
24*4c3eb207Smrg #include "system.h"
25*4c3eb207Smrg #include "coretypes.h"
26*4c3eb207Smrg #include "tree.h"
27*4c3eb207Smrg #include "function.h"
28*4c3eb207Smrg #include "basic-block.h"
29*4c3eb207Smrg #include "gimple.h"
30*4c3eb207Smrg #include "options.h"
31*4c3eb207Smrg #include "bitmap.h"
32*4c3eb207Smrg #include "diagnostic-path.h"
33*4c3eb207Smrg #include "diagnostic-metadata.h"
34*4c3eb207Smrg #include "function.h"
35*4c3eb207Smrg #include "analyzer/analyzer.h"
36*4c3eb207Smrg #include "diagnostic-event-id.h"
37*4c3eb207Smrg #include "analyzer/analyzer-logging.h"
38*4c3eb207Smrg #include "analyzer/sm.h"
39*4c3eb207Smrg #include "analyzer/pending-diagnostic.h"
40*4c3eb207Smrg #include "sbitmap.h"
41*4c3eb207Smrg #include "tristate.h"
42*4c3eb207Smrg #include "ordered-hash-map.h"
43*4c3eb207Smrg #include "selftest.h"
44*4c3eb207Smrg #include "analyzer/region-model.h"
45*4c3eb207Smrg #include "analyzer/program-state.h"
46*4c3eb207Smrg #include "analyzer/checker-path.h"
47*4c3eb207Smrg #include "digraph.h"
48*4c3eb207Smrg #include "cfg.h"
49*4c3eb207Smrg #include "gimple-iterator.h"
50*4c3eb207Smrg #include "cgraph.h"
51*4c3eb207Smrg #include "analyzer/supergraph.h"
52*4c3eb207Smrg #include "analyzer/call-string.h"
53*4c3eb207Smrg #include "analyzer/program-point.h"
54*4c3eb207Smrg #include "alloc-pool.h"
55*4c3eb207Smrg #include "fibonacci_heap.h"
56*4c3eb207Smrg #include "analyzer/diagnostic-manager.h"
57*4c3eb207Smrg #include "shortest-paths.h"
58*4c3eb207Smrg #include "analyzer/exploded-graph.h"
59*4c3eb207Smrg #include "analyzer/function-set.h"
60*4c3eb207Smrg #include "analyzer/analyzer-selftests.h"
61*4c3eb207Smrg 
62*4c3eb207Smrg #if ENABLE_ANALYZER
63*4c3eb207Smrg 
64*4c3eb207Smrg namespace ana {
65*4c3eb207Smrg 
66*4c3eb207Smrg namespace {
67*4c3eb207Smrg 
68*4c3eb207Smrg /* An experimental state machine, for tracking calls to async-signal-unsafe
69*4c3eb207Smrg    functions from within signal handlers.  */
70*4c3eb207Smrg 
71*4c3eb207Smrg class signal_state_machine : public state_machine
72*4c3eb207Smrg {
73*4c3eb207Smrg public:
74*4c3eb207Smrg   signal_state_machine (logger *logger);
75*4c3eb207Smrg 
inherited_state_p() const76*4c3eb207Smrg   bool inherited_state_p () const FINAL OVERRIDE { return false; }
77*4c3eb207Smrg 
78*4c3eb207Smrg   bool on_stmt (sm_context *sm_ctxt,
79*4c3eb207Smrg 		const supernode *node,
80*4c3eb207Smrg 		const gimple *stmt) const FINAL OVERRIDE;
81*4c3eb207Smrg 
82*4c3eb207Smrg   void on_condition (sm_context *sm_ctxt,
83*4c3eb207Smrg 		     const supernode *node,
84*4c3eb207Smrg 		     const gimple *stmt,
85*4c3eb207Smrg 		     tree lhs,
86*4c3eb207Smrg 		     enum tree_code op,
87*4c3eb207Smrg 		     tree rhs) const FINAL OVERRIDE;
88*4c3eb207Smrg 
89*4c3eb207Smrg   bool can_purge_p (state_t s) const FINAL OVERRIDE;
90*4c3eb207Smrg 
91*4c3eb207Smrg   /* These states are "global", rather than per-expression.  */
92*4c3eb207Smrg 
93*4c3eb207Smrg   /* Start state.  */
94*4c3eb207Smrg   state_t m_start;
95*4c3eb207Smrg 
96*4c3eb207Smrg   /* State for when we're in a signal handler.  */
97*4c3eb207Smrg   state_t m_in_signal_handler;
98*4c3eb207Smrg 
99*4c3eb207Smrg   /* Stop state.  */
100*4c3eb207Smrg   state_t m_stop;
101*4c3eb207Smrg };
102*4c3eb207Smrg 
103*4c3eb207Smrg /* Concrete subclass for describing call to an async-signal-unsafe function
104*4c3eb207Smrg    from a signal handler.  */
105*4c3eb207Smrg 
106*4c3eb207Smrg class signal_unsafe_call
107*4c3eb207Smrg   : public pending_diagnostic_subclass<signal_unsafe_call>
108*4c3eb207Smrg {
109*4c3eb207Smrg public:
signal_unsafe_call(const signal_state_machine & sm,const gcall * unsafe_call,tree unsafe_fndecl)110*4c3eb207Smrg   signal_unsafe_call (const signal_state_machine &sm, const gcall *unsafe_call,
111*4c3eb207Smrg 		      tree unsafe_fndecl)
112*4c3eb207Smrg   : m_sm (sm), m_unsafe_call (unsafe_call), m_unsafe_fndecl (unsafe_fndecl)
113*4c3eb207Smrg   {
114*4c3eb207Smrg     gcc_assert (m_unsafe_fndecl);
115*4c3eb207Smrg   }
116*4c3eb207Smrg 
get_kind() const117*4c3eb207Smrg   const char *get_kind () const FINAL OVERRIDE { return "signal_unsafe_call"; }
118*4c3eb207Smrg 
operator ==(const signal_unsafe_call & other) const119*4c3eb207Smrg   bool operator== (const signal_unsafe_call &other) const
120*4c3eb207Smrg   {
121*4c3eb207Smrg     return m_unsafe_call == other.m_unsafe_call;
122*4c3eb207Smrg   }
123*4c3eb207Smrg 
emit(rich_location * rich_loc)124*4c3eb207Smrg   bool emit (rich_location *rich_loc) FINAL OVERRIDE
125*4c3eb207Smrg   {
126*4c3eb207Smrg     diagnostic_metadata m;
127*4c3eb207Smrg     /* CWE-479: Signal Handler Use of a Non-reentrant Function.  */
128*4c3eb207Smrg     m.add_cwe (479);
129*4c3eb207Smrg     return warning_meta (rich_loc, m,
130*4c3eb207Smrg 			 OPT_Wanalyzer_unsafe_call_within_signal_handler,
131*4c3eb207Smrg 			 "call to %qD from within signal handler",
132*4c3eb207Smrg 			 m_unsafe_fndecl);
133*4c3eb207Smrg   }
134*4c3eb207Smrg 
describe_state_change(const evdesc::state_change & change)135*4c3eb207Smrg   label_text describe_state_change (const evdesc::state_change &change)
136*4c3eb207Smrg     FINAL OVERRIDE
137*4c3eb207Smrg   {
138*4c3eb207Smrg     if (change.is_global_p ()
139*4c3eb207Smrg 	&& change.m_new_state == m_sm.m_in_signal_handler)
140*4c3eb207Smrg       {
141*4c3eb207Smrg 	function *handler
142*4c3eb207Smrg 	  = change.m_event.m_dst_state.m_region_model->get_current_function ();
143*4c3eb207Smrg 	return change.formatted_print ("registering %qD as signal handler",
144*4c3eb207Smrg 				       handler->decl);
145*4c3eb207Smrg       }
146*4c3eb207Smrg     return label_text ();
147*4c3eb207Smrg   }
148*4c3eb207Smrg 
describe_final_event(const evdesc::final_event & ev)149*4c3eb207Smrg   label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
150*4c3eb207Smrg   {
151*4c3eb207Smrg     return ev.formatted_print ("call to %qD from within signal handler",
152*4c3eb207Smrg 			       m_unsafe_fndecl);
153*4c3eb207Smrg   }
154*4c3eb207Smrg 
155*4c3eb207Smrg private:
156*4c3eb207Smrg   const signal_state_machine &m_sm;
157*4c3eb207Smrg   const gcall *m_unsafe_call;
158*4c3eb207Smrg   tree m_unsafe_fndecl;
159*4c3eb207Smrg };
160*4c3eb207Smrg 
161*4c3eb207Smrg /* signal_state_machine's ctor.  */
162*4c3eb207Smrg 
signal_state_machine(logger * logger)163*4c3eb207Smrg signal_state_machine::signal_state_machine (logger *logger)
164*4c3eb207Smrg : state_machine ("signal", logger)
165*4c3eb207Smrg {
166*4c3eb207Smrg   m_start = add_state ("start");
167*4c3eb207Smrg   m_in_signal_handler = add_state ("in_signal_handler");
168*4c3eb207Smrg   m_stop = add_state ("stop");
169*4c3eb207Smrg }
170*4c3eb207Smrg 
171*4c3eb207Smrg /* Update MODEL for edges that simulate HANDLER_FUN being called as
172*4c3eb207Smrg    an signal-handler in response to a signal.  */
173*4c3eb207Smrg 
174*4c3eb207Smrg static void
update_model_for_signal_handler(region_model * model,function * handler_fun)175*4c3eb207Smrg update_model_for_signal_handler (region_model *model,
176*4c3eb207Smrg 				 function *handler_fun)
177*4c3eb207Smrg {
178*4c3eb207Smrg   /* Purge all state within MODEL.  */
179*4c3eb207Smrg   *model = region_model ();
180*4c3eb207Smrg   model->push_frame (handler_fun, NULL, NULL);
181*4c3eb207Smrg }
182*4c3eb207Smrg 
183*4c3eb207Smrg /* Custom exploded_edge info: entry into a signal-handler.  */
184*4c3eb207Smrg 
185*4c3eb207Smrg class signal_delivery_edge_info_t : public exploded_edge::custom_info_t
186*4c3eb207Smrg {
187*4c3eb207Smrg public:
print(pretty_printer * pp)188*4c3eb207Smrg   void print (pretty_printer *pp) FINAL OVERRIDE
189*4c3eb207Smrg   {
190*4c3eb207Smrg     pp_string (pp, "signal delivered");
191*4c3eb207Smrg   }
192*4c3eb207Smrg 
update_model(region_model * model,const exploded_edge & eedge)193*4c3eb207Smrg   void update_model (region_model *model,
194*4c3eb207Smrg 		     const exploded_edge &eedge) FINAL OVERRIDE
195*4c3eb207Smrg   {
196*4c3eb207Smrg     update_model_for_signal_handler (model, eedge.m_dest->get_function ());
197*4c3eb207Smrg   }
198*4c3eb207Smrg 
add_events_to_path(checker_path * emission_path,const exploded_edge & eedge ATTRIBUTE_UNUSED)199*4c3eb207Smrg   void add_events_to_path (checker_path *emission_path,
200*4c3eb207Smrg 			   const exploded_edge &eedge ATTRIBUTE_UNUSED)
201*4c3eb207Smrg     FINAL OVERRIDE
202*4c3eb207Smrg   {
203*4c3eb207Smrg     emission_path->add_event
204*4c3eb207Smrg       (new custom_event (UNKNOWN_LOCATION, NULL_TREE, 0,
205*4c3eb207Smrg 			 "later on,"
206*4c3eb207Smrg 			 " when the signal is delivered to the process"));
207*4c3eb207Smrg   }
208*4c3eb207Smrg };
209*4c3eb207Smrg 
210*4c3eb207Smrg /* Concrete subclass of custom_transition for modeling registration of a
211*4c3eb207Smrg    signal handler and the signal handler later being called.  */
212*4c3eb207Smrg 
213*4c3eb207Smrg class register_signal_handler : public custom_transition
214*4c3eb207Smrg {
215*4c3eb207Smrg public:
register_signal_handler(const signal_state_machine & sm,tree fndecl)216*4c3eb207Smrg   register_signal_handler (const signal_state_machine &sm,
217*4c3eb207Smrg 			   tree fndecl)
218*4c3eb207Smrg   : m_sm (sm), m_fndecl (fndecl) {}
219*4c3eb207Smrg 
220*4c3eb207Smrg   /* Model a signal-handler FNDECL being called at some later point
221*4c3eb207Smrg      by injecting an edge to a new function-entry node with an empty
222*4c3eb207Smrg      callstring, setting the 'in-signal-handler' global state
223*4c3eb207Smrg      on the node.  */
impl_transition(exploded_graph * eg,exploded_node * src_enode,int sm_idx)224*4c3eb207Smrg   void impl_transition (exploded_graph *eg,
225*4c3eb207Smrg 			exploded_node *src_enode,
226*4c3eb207Smrg 			int sm_idx) FINAL OVERRIDE
227*4c3eb207Smrg   {
228*4c3eb207Smrg     function *handler_fun = DECL_STRUCT_FUNCTION (m_fndecl);
229*4c3eb207Smrg     if (!handler_fun)
230*4c3eb207Smrg       return;
231*4c3eb207Smrg     program_point entering_handler
232*4c3eb207Smrg       = program_point::from_function_entry (eg->get_supergraph (),
233*4c3eb207Smrg 					    handler_fun);
234*4c3eb207Smrg 
235*4c3eb207Smrg     program_state state_entering_handler (eg->get_ext_state ());
236*4c3eb207Smrg     update_model_for_signal_handler (state_entering_handler.m_region_model,
237*4c3eb207Smrg 				     handler_fun);
238*4c3eb207Smrg     state_entering_handler.m_checker_states[sm_idx]->set_global_state
239*4c3eb207Smrg       (m_sm.m_in_signal_handler);
240*4c3eb207Smrg 
241*4c3eb207Smrg     exploded_node *dst_enode = eg->get_or_create_node (entering_handler,
242*4c3eb207Smrg 						       state_entering_handler,
243*4c3eb207Smrg 						       NULL);
244*4c3eb207Smrg     if (dst_enode)
245*4c3eb207Smrg       eg->add_edge (src_enode, dst_enode, NULL, state_change (),
246*4c3eb207Smrg 		    new signal_delivery_edge_info_t ());
247*4c3eb207Smrg   }
248*4c3eb207Smrg 
249*4c3eb207Smrg   const signal_state_machine &m_sm;
250*4c3eb207Smrg   tree m_fndecl;
251*4c3eb207Smrg };
252*4c3eb207Smrg 
253*4c3eb207Smrg /* Get a set of functions that are known to be unsafe to call from an
254*4c3eb207Smrg    async signal handler.  */
255*4c3eb207Smrg 
256*4c3eb207Smrg static function_set
get_async_signal_unsafe_fns()257*4c3eb207Smrg get_async_signal_unsafe_fns ()
258*4c3eb207Smrg {
259*4c3eb207Smrg   // TODO: populate this list more fully
260*4c3eb207Smrg   static const char * const async_signal_unsafe_fns[] = {
261*4c3eb207Smrg     /* This array must be kept sorted.  */
262*4c3eb207Smrg     "fprintf",
263*4c3eb207Smrg     "free",
264*4c3eb207Smrg     "malloc",
265*4c3eb207Smrg     "printf",
266*4c3eb207Smrg     "snprintf",
267*4c3eb207Smrg     "sprintf",
268*4c3eb207Smrg     "vfprintf",
269*4c3eb207Smrg     "vprintf",
270*4c3eb207Smrg     "vsnprintf",
271*4c3eb207Smrg     "vsprintf"
272*4c3eb207Smrg   };
273*4c3eb207Smrg   const size_t count
274*4c3eb207Smrg     = sizeof(async_signal_unsafe_fns) / sizeof (async_signal_unsafe_fns[0]);
275*4c3eb207Smrg   function_set fs (async_signal_unsafe_fns, count);
276*4c3eb207Smrg   return fs;
277*4c3eb207Smrg };
278*4c3eb207Smrg 
279*4c3eb207Smrg /* Return true if FNDECL is known to be unsafe to call from a signal
280*4c3eb207Smrg    handler.  */
281*4c3eb207Smrg 
282*4c3eb207Smrg static bool
signal_unsafe_p(tree fndecl)283*4c3eb207Smrg signal_unsafe_p (tree fndecl)
284*4c3eb207Smrg {
285*4c3eb207Smrg   function_set fs = get_async_signal_unsafe_fns ();
286*4c3eb207Smrg   return fs.contains_decl_p (fndecl);
287*4c3eb207Smrg }
288*4c3eb207Smrg 
289*4c3eb207Smrg /* Implementation of state_machine::on_stmt vfunc for signal_state_machine.  */
290*4c3eb207Smrg 
291*4c3eb207Smrg bool
on_stmt(sm_context * sm_ctxt,const supernode * node,const gimple * stmt) const292*4c3eb207Smrg signal_state_machine::on_stmt (sm_context *sm_ctxt,
293*4c3eb207Smrg 			       const supernode *node,
294*4c3eb207Smrg 			       const gimple *stmt) const
295*4c3eb207Smrg {
296*4c3eb207Smrg   const state_t global_state = sm_ctxt->get_global_state ();
297*4c3eb207Smrg   if (global_state == m_start)
298*4c3eb207Smrg     {
299*4c3eb207Smrg       if (const gcall *call = dyn_cast <const gcall *> (stmt))
300*4c3eb207Smrg 	if (tree callee_fndecl = sm_ctxt->get_fndecl_for_call (call))
301*4c3eb207Smrg 	  if (is_named_call_p (callee_fndecl, "signal", call, 2))
302*4c3eb207Smrg 	    {
303*4c3eb207Smrg 	      tree handler = gimple_call_arg (call, 1);
304*4c3eb207Smrg 	      if (TREE_CODE (handler) == ADDR_EXPR
305*4c3eb207Smrg 		  && TREE_CODE (TREE_OPERAND (handler, 0)) == FUNCTION_DECL)
306*4c3eb207Smrg 		{
307*4c3eb207Smrg 		  tree fndecl = TREE_OPERAND (handler, 0);
308*4c3eb207Smrg 		  register_signal_handler rsh (*this, fndecl);
309*4c3eb207Smrg 		  sm_ctxt->on_custom_transition (&rsh);
310*4c3eb207Smrg 		}
311*4c3eb207Smrg 	    }
312*4c3eb207Smrg     }
313*4c3eb207Smrg   else if (global_state == m_in_signal_handler)
314*4c3eb207Smrg     {
315*4c3eb207Smrg       if (const gcall *call = dyn_cast <const gcall *> (stmt))
316*4c3eb207Smrg 	if (tree callee_fndecl = sm_ctxt->get_fndecl_for_call (call))
317*4c3eb207Smrg 	  if (signal_unsafe_p (callee_fndecl))
318*4c3eb207Smrg 	    sm_ctxt->warn_for_state (node, stmt, NULL_TREE, m_in_signal_handler,
319*4c3eb207Smrg 				     new signal_unsafe_call (*this, call,
320*4c3eb207Smrg 							     callee_fndecl));
321*4c3eb207Smrg     }
322*4c3eb207Smrg 
323*4c3eb207Smrg   return false;
324*4c3eb207Smrg }
325*4c3eb207Smrg 
326*4c3eb207Smrg /* Implementation of state_machine::on_condition vfunc for
327*4c3eb207Smrg    signal_state_machine.  */
328*4c3eb207Smrg 
329*4c3eb207Smrg void
on_condition(sm_context * sm_ctxt ATTRIBUTE_UNUSED,const supernode * node ATTRIBUTE_UNUSED,const gimple * stmt ATTRIBUTE_UNUSED,tree lhs ATTRIBUTE_UNUSED,enum tree_code op ATTRIBUTE_UNUSED,tree rhs ATTRIBUTE_UNUSED) const330*4c3eb207Smrg signal_state_machine::on_condition (sm_context *sm_ctxt ATTRIBUTE_UNUSED,
331*4c3eb207Smrg 				    const supernode *node ATTRIBUTE_UNUSED,
332*4c3eb207Smrg 				    const gimple *stmt ATTRIBUTE_UNUSED,
333*4c3eb207Smrg 				    tree lhs ATTRIBUTE_UNUSED,
334*4c3eb207Smrg 				    enum tree_code op ATTRIBUTE_UNUSED,
335*4c3eb207Smrg 				    tree rhs ATTRIBUTE_UNUSED) const
336*4c3eb207Smrg {
337*4c3eb207Smrg   // Empty
338*4c3eb207Smrg }
339*4c3eb207Smrg 
340*4c3eb207Smrg bool
can_purge_p(state_t s ATTRIBUTE_UNUSED) const341*4c3eb207Smrg signal_state_machine::can_purge_p (state_t s ATTRIBUTE_UNUSED) const
342*4c3eb207Smrg {
343*4c3eb207Smrg   return true;
344*4c3eb207Smrg }
345*4c3eb207Smrg 
346*4c3eb207Smrg } // anonymous namespace
347*4c3eb207Smrg 
348*4c3eb207Smrg /* Internal interface to this file. */
349*4c3eb207Smrg 
350*4c3eb207Smrg state_machine *
make_signal_state_machine(logger * logger)351*4c3eb207Smrg make_signal_state_machine (logger *logger)
352*4c3eb207Smrg {
353*4c3eb207Smrg   return new signal_state_machine (logger);
354*4c3eb207Smrg }
355*4c3eb207Smrg 
356*4c3eb207Smrg #if CHECKING_P
357*4c3eb207Smrg 
358*4c3eb207Smrg namespace selftest {
359*4c3eb207Smrg 
360*4c3eb207Smrg /* Run all of the selftests within this file.  */
361*4c3eb207Smrg 
362*4c3eb207Smrg void
analyzer_sm_signal_cc_tests()363*4c3eb207Smrg analyzer_sm_signal_cc_tests ()
364*4c3eb207Smrg {
365*4c3eb207Smrg   function_set fs = get_async_signal_unsafe_fns ();
366*4c3eb207Smrg   fs.assert_sorted ();
367*4c3eb207Smrg   fs.assert_sane ();
368*4c3eb207Smrg }
369*4c3eb207Smrg 
370*4c3eb207Smrg } // namespace selftest
371*4c3eb207Smrg 
372*4c3eb207Smrg #endif /* CHECKING_P */
373*4c3eb207Smrg 
374*4c3eb207Smrg } // namespace ana
375*4c3eb207Smrg 
376*4c3eb207Smrg #endif /* #if ENABLE_ANALYZER */
377