Line data Source code
1 : /* Subclasses of custom_edge_info for describing outcomes of function calls.
2 : Copyright (C) 2021-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 "intl.h"
24 : #include "ordered-hash-map.h"
25 : #include "cfg.h"
26 : #include "digraph.h"
27 : #include "sbitmap.h"
28 : #include "diagnostics/event-id.h"
29 :
30 : #include "analyzer/analyzer-logging.h"
31 : #include "analyzer/supergraph.h"
32 : #include "analyzer/call-string.h"
33 : #include "analyzer/program-point.h"
34 : #include "analyzer/store.h"
35 : #include "analyzer/region-model.h"
36 : #include "analyzer/constraint-manager.h"
37 : #include "analyzer/sm.h"
38 : #include "analyzer/pending-diagnostic.h"
39 : #include "analyzer/region-model-reachability.h"
40 : #include "analyzer/analyzer-selftests.h"
41 : #include "analyzer/program-state.h"
42 : #include "analyzer/checker-path.h"
43 : #include "analyzer/diagnostic-manager.h"
44 : #include "analyzer/exploded-graph.h"
45 : #include "analyzer/call-details.h"
46 : #include "analyzer/call-info.h"
47 : #include "analyzer/state-transition.h"
48 :
49 : #if ENABLE_ANALYZER
50 :
51 : namespace ana {
52 :
53 : /* class custom_edge_info. */
54 :
55 : bool
56 8004 : custom_edge_info::update_state (program_state *state,
57 : const exploded_edge *eedge,
58 : region_model_context *ctxt) const
59 : {
60 8004 : return update_model (state->m_region_model, eedge, ctxt);
61 : }
62 :
63 : void
64 6 : custom_edge_info::get_dot_attrs (const char *&out_style,
65 : const char *&out_color) const
66 : {
67 6 : out_color = "red";
68 6 : out_style = "\"dotted\"";
69 6 : }
70 :
71 : /* Base implementation of custom_edge_info::create_enode vfunc. */
72 :
73 : exploded_node *
74 2398 : custom_edge_info::create_enode (exploded_graph &eg,
75 : const program_point &point,
76 : program_state &&state,
77 : exploded_node *enode_for_diag,
78 : region_model_context *) const
79 : {
80 2398 : return eg.get_or_create_node (point, state, enode_for_diag);
81 : }
82 :
83 : /* class call_info : public custom_edge_info. */
84 :
85 : /* Implementation of custom_edge_info::print vfunc for call_info. */
86 :
87 : void
88 0 : call_info::print (pretty_printer *pp) const
89 : {
90 0 : gcc_assert (pp);
91 0 : print_desc (*pp);
92 0 : }
93 :
94 : /* Implementation of custom_edge_info::add_events_to_path vfunc for
95 : call_info: add a custom_event using call_info::get_desc as its
96 : description. */
97 :
98 : void
99 402 : call_info::add_events_to_path (checker_path *emission_path,
100 : const exploded_edge &eedge,
101 : pending_diagnostic &,
102 : const state_transition *) const
103 : {
104 0 : class call_event : public custom_event
105 : {
106 : public:
107 402 : call_event (const event_loc_info &loc_info,
108 : const call_info *call_info)
109 : : custom_event (loc_info),
110 402 : m_call_info (call_info)
111 : {}
112 :
113 795 : void print_desc (pretty_printer &pp) const final override
114 : {
115 795 : m_call_info->print_desc (pp);
116 795 : }
117 :
118 : private:
119 : const call_info *m_call_info;
120 : };
121 :
122 402 : const exploded_node *src_node = eedge.m_src;
123 402 : const program_point &src_point = src_node->get_point ();
124 402 : tree caller_fndecl = src_point.get_fndecl ();
125 402 : const int stack_depth = src_point.get_stack_depth ();
126 :
127 : /* TODO: we don't yet make use of the state_transition (if any), as
128 : doing so presumably requires a combinatorial explosion of
129 : known functions vs pending diagnostics. */
130 :
131 402 : emission_path->add_event
132 402 : (std::make_unique<call_event> (event_loc_info (get_call_stmt ().location,
133 : caller_fndecl,
134 402 : stack_depth),
135 402 : this));
136 402 : }
137 :
138 : /* Recreate a call_details instance from this call_info. */
139 :
140 : call_details
141 4894 : call_info::get_call_details (region_model *model,
142 : region_model_context *ctxt) const
143 : {
144 4894 : return call_details (m_call_stmt, model, ctxt);
145 : }
146 :
147 : /* call_info's ctor.
148 :
149 : The call_info instance will outlive the call_details instance;
150 : call_details instances are typically created on the stack. */
151 :
152 2948 : call_info::call_info (const call_details &cd)
153 2948 : : m_call_stmt (cd.get_call_stmt ()),
154 2948 : m_fndecl (cd.get_fndecl_for_call ())
155 : {
156 2948 : gcc_assert (m_fndecl);
157 2948 : }
158 :
159 414 : call_info::call_info (const call_details &cd,
160 : const function &called_fn)
161 414 : : m_call_stmt (cd.get_call_stmt ()),
162 414 : m_fndecl (called_fn.decl)
163 : {
164 414 : gcc_assert (m_fndecl);
165 414 : }
166 :
167 : /* class succeed_or_fail_call_info : public call_info. */
168 :
169 : void
170 379 : succeed_or_fail_call_info::print_desc (pretty_printer &pp) const
171 : {
172 758 : if (const char *msg_detail_str
173 379 : = get_string_for_message_detail (get_message_detail ()))
174 : {
175 335 : if (m_success)
176 269 : pp_printf (&pp, "when %qE succeeds (%s)", get_fndecl (),
177 : msg_detail_str);
178 : else
179 66 : pp_printf (&pp, "when %qE fails (%s)", get_fndecl (),
180 : msg_detail_str);
181 : }
182 : else
183 : {
184 44 : if (m_success)
185 0 : pp_printf (&pp, "when %qE succeeds", get_fndecl ());
186 : else
187 44 : pp_printf (&pp, "when %qE fails", get_fndecl ());
188 : }
189 379 : }
190 :
191 : /* Get a translated string giving extra detail to appear in parentheses after
192 : 'when FNDECL succeeds' or 'when FNDECL fails', such as
193 : "when 'listen' fails (returning -1)".
194 : ^^^^^^^^^^^^
195 : Return nullptr if there is no extra detail. */
196 :
197 : const char *
198 379 : succeed_or_fail_call_info::
199 : get_string_for_message_detail (message_detail msg_detail)
200 : {
201 379 : switch (msg_detail)
202 : {
203 0 : default:
204 0 : gcc_unreachable ();
205 :
206 : case message_detail::none:
207 : return nullptr;
208 :
209 52 : case message_detail::returning_zero:
210 52 : return _("returning 0");
211 :
212 8 : case message_detail::returning_one:
213 8 : return _("returning 1");
214 :
215 58 : case message_detail::returning_minus_one:
216 58 : return _("returning -1");
217 :
218 216 : case message_detail::returning_non_negative:
219 216 : return _("returning >= 0");
220 :
221 1 : case message_detail::returning_non_null:
222 1 : return _("returning non-NULL");
223 :
224 0 : case message_detail::returning_null:
225 0 : return _("returning NULL");
226 : }
227 : }
228 :
229 : } // namespace ana
230 :
231 : #endif /* #if ENABLE_ANALYZER */
|