Debian Codegraph @

gcc/testsuite/gcc.dg/plugin/analyzer_known_fns_plugin.cc

raw
1 /* Proof-of-concept of a -fanalyzer plugin to handle known functions. */
2 /* { dg-options "-g" } */
3
4 #define INCLUDE_MEMORY
5 #define INCLUDE_VECTOR
6 #include "gcc-plugin.h"
7 #include "config.h"
8 #include "system.h"
9 #include "coretypes.h"
10 #include "tree.h"
11 #include "function.h"
12 #include "basic-block.h"
13 #include "gimple.h"
14 #include "gimple-iterator.h"
15 #include "diagnostic-core.h"
16 #include "graphviz.h"
17 #include "options.h"
18 #include "cgraph.h"
19 #include "tree-dfa.h"
20 #include "stringpool.h"
21 #include "convert.h"
22 #include "target.h"
23 #include "fold-const.h"
24 #include "tree-pretty-print.h"
25 #include "diagnostic-color.h"
26 #include "diagnostic-metadata.h"
27 #include "tristate.h"
28 #include "bitmap.h"
29 #include "selftest.h"
30 #include "function.h"
31 #include "json.h"
32 #include "analyzer/analyzer.h"
33 #include "analyzer/analyzer-logging.h"
34 #include "ordered-hash-map.h"
35 #include "options.h"
36 #include "cgraph.h"
37 #include "cfg.h"
38 #include "digraph.h"
39 #include "analyzer/supergraph.h"
40 #include "sbitmap.h"
41 #include "analyzer/call-string.h"
42 #include "analyzer/program-point.h"
43 #include "analyzer/store.h"
44 #include "analyzer/region-model.h"
45 #include "analyzer/call-details.h"
46 #include "analyzer/call-info.h"
47 #include "make-unique.h"
48
49 int plugin_is_GPL_compatible;
50
51 #if ENABLE_ANALYZER
52
53 namespace ana {
54
55 /* Basic example of known fn behavior. */
56
57 class known_function_returns_42 : public known_function
58 {
59 public:
60 bool matches_call_types_p (const call_details &) const final override
61 {
62 return true;
63 }
64
65 void impl_call_pre (const call_details &cd) const final override
66 {
67 if (cd.get_lhs_type ())
68 {
69 const svalue *result
70 = cd.get_manager ()->get_or_create_int_cst (cd.get_lhs_type (), 42);
71 cd.maybe_set_lhs (result);
72 }
73 }
74 };
75
76 /* Example of bifurcation, with a copy that can fail. */
77
78 class known_function_attempt_to_copy : public known_function
79 {
80 public:
81 class copy_success : public success_call_info
82 {
83 public:
84 copy_success (const call_details &cd,
85 const region *sized_dest_reg,
86 const svalue *copied_sval)
87 : success_call_info (cd),
88 m_sized_dest_reg (sized_dest_reg),
89 m_copied_sval (copied_sval)
90 {}
91
92 bool update_model (region_model *model,
93 const exploded_edge *,
94 region_model_context *ctxt) const final override
95 {
96 call_details cd (get_call_details (model, ctxt));
97 model->update_for_zero_return (cd, true);
98 model->set_value (m_sized_dest_reg, m_copied_sval, ctxt);
99 return true;
100 }
101
102 const region *m_sized_dest_reg;
103 const svalue *m_copied_sval;
104 const region *m_sized_src_reg;
105 };
106
107 class copy_failure : public failed_call_info
108 {
109 public:
110 copy_failure (const call_details &cd)
111 : failed_call_info (cd)
112 {}
113
114 bool update_model (region_model *model,
115 const exploded_edge *,
116 region_model_context *ctxt) const final override
117 {
118 call_details cd (get_call_details (model, ctxt));
119 model->update_for_nonzero_return (cd);
120 /* Leave the destination region untouched. */
121 return true;
122 }
123 };
124
125 bool matches_call_types_p (const call_details &cd) const
126 {
127 return cd.num_args () == 3;
128 }
129
130 void impl_call_pre (const call_details &cd) const final override
131 {
132 region_model_manager *mgr = cd.get_manager ();
133 region_model *model = cd.get_model ();
134
135 const svalue *dest_sval = cd.get_arg_svalue (0);
136 const svalue *src_sval = cd.get_arg_svalue (1);
137 const svalue *num_bytes_sval = cd.get_arg_svalue (2);
138
139 const region *dest_reg = model->deref_rvalue (dest_sval,
140 cd.get_arg_tree (0),
141 cd.get_ctxt ());
142 const region *src_reg = model->deref_rvalue (src_sval,
143 cd.get_arg_tree (1),
144 cd.get_ctxt ());
145 if (const svalue * bounded_sval
146 = model->maybe_get_copy_bounds (src_reg, num_bytes_sval))
147 num_bytes_sval = bounded_sval;
148
149 if (tree cst = num_bytes_sval->maybe_get_constant ())
150 if (zerop (cst))
151 {
152 /* No-op. */
153 cd.set_any_lhs_with_defaults ();
154 return;
155 }
156
157 const region *sized_src_reg = mgr->get_sized_region (src_reg,
158 NULL_TREE,
159 num_bytes_sval);
160
161 const svalue *copied_sval
162 = model->get_store_value (sized_src_reg, cd.get_ctxt ());
163
164 const region *sized_dest_reg = mgr->get_sized_region (dest_reg,
165 NULL_TREE,
166 num_bytes_sval);
167
168 if (cd.get_ctxt ())
169 {
170 /* Bifurcate state, creating a "failure" out-edge. */
171 cd.get_ctxt ()->bifurcate (make_unique<copy_failure> (cd));
172
173 /* The "unbifurcated" state is the "success" case. */
174 copy_success success (cd,
175 sized_dest_reg,
176 copied_sval);
177 success.update_model (model, NULL, cd.get_ctxt ());
178 }
179 }
180 };
181
182 /* Callback handler for the PLUGIN_ANALYZER_INIT event. */
183
184 static void
185 known_fn_analyzer_init_cb (void *gcc_data, void */*user_data*/)
186 {
187 ana::plugin_analyzer_init_iface *iface
188 = (ana::plugin_analyzer_init_iface *)gcc_data;
189 LOG_SCOPE (iface->get_logger ());
190 if (0)
191 inform (input_location, "got here: known_fn_analyzer_init_cb");
192 iface->register_known_function ("returns_42",
193 make_unique<known_function_returns_42> ());
194 iface->register_known_function
195 ("attempt_to_copy",
196 make_unique<known_function_attempt_to_copy> ());
197 }
198
199 } // namespace ana
200
201 #endif /* #if ENABLE_ANALYZER */
202
203 int
204 plugin_init (struct plugin_name_args *plugin_info,
205 struct plugin_gcc_version *version)
206 {
207 #if ENABLE_ANALYZER
208 const char *plugin_name = plugin_info->base_name;
209 if (0)
210 inform (input_location, "got here; %qs", plugin_name);
211 register_callback (plugin_info->base_name,
212 PLUGIN_ANALYZER_INIT,
213 ana::known_fn_analyzer_init_cb,
214 NULL); /* void *user_data */
215 #else
216 sorry_no_analyzer ();
217 #endif
218 return 0;
219 }