Debian Codegraph @

gcc/testsuite/gcc.dg/plugin/analyzer_kernel_plugin.cc

raw
1 /* Proof-of-concept of a -fanalyzer plugin for the Linux kernel. */
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 /* Implementation of "copy_from_user" and "copy_to_user". */
56
57 class copy_across_boundary_fn : public known_function
58 {
59 public:
60 virtual bool untrusted_source_p () const = 0;
61 virtual bool untrusted_destination_p () const = 0;
62
63 bool matches_call_types_p (const call_details &cd) const final override
64 {
65 return cd.num_args () == 3;
66 }
67
68 void impl_call_pre (const call_details &cd) const final override
69 {
70 region_model_manager *mgr = cd.get_manager ();
71 region_model *model = cd.get_model ();
72 region_model_context *ctxt = cd.get_ctxt ();
73
74 const svalue *dest_sval = cd.get_arg_svalue (0);
75 const svalue *src_sval = cd.get_arg_svalue (1);
76 const svalue *num_bytes_sval = cd.get_arg_svalue (2);
77
78 const region *dest_reg = model->deref_rvalue (dest_sval,
79 cd.get_arg_tree (0),
80 ctxt);
81 const region *src_reg = model->deref_rvalue (src_sval,
82 cd.get_arg_tree (1),
83 ctxt);
84 if (const svalue *bounded_sval
85 = model->maybe_get_copy_bounds (src_reg, num_bytes_sval))
86 num_bytes_sval = bounded_sval;
87
88 if (tree cst = num_bytes_sval->maybe_get_constant ())
89 if (zerop (cst))
90 {
91 /* No-op. */
92 model->update_for_zero_return (cd, true);
93 return;
94 }
95
96 const region *sized_src_reg = mgr->get_sized_region (src_reg,
97 NULL_TREE,
98 num_bytes_sval);
99
100 const svalue *copied_sval
101 = model->get_store_value (sized_src_reg, ctxt);
102 const region *sized_dest_reg = mgr->get_sized_region (dest_reg,
103 NULL_TREE,
104 num_bytes_sval);
105
106 if (ctxt)
107 {
108 /* Bifurcate state, creating a "failure" out-edge. */
109 ctxt->bifurcate (make_unique<copy_failure> (cd));
110
111 /* The "unbifurcated" state is the "success" case. */
112 copy_success success (cd,
113 sized_dest_reg,
114 copied_sval,
115 sized_src_reg,
116 untrusted_source_p (),
117 untrusted_destination_p ());
118 success.update_model (model, NULL, ctxt);
119 }
120 }
121
122 private:
123 class copy_success : public success_call_info
124 {
125 public:
126 copy_success (const call_details &cd,
127 const region *sized_dest_reg,
128 const svalue *copied_sval,
129 const region *sized_src_reg,
130 bool untrusted_source,
131 bool untrusted_destination)
132 : success_call_info (cd),
133 m_sized_dest_reg (sized_dest_reg),
134 m_copied_sval (copied_sval),
135 m_sized_src_reg (sized_src_reg),
136 m_untrusted_source (untrusted_source),
137 m_untrusted_destination (untrusted_destination)
138 {}
139
140 bool update_model (region_model *model,
141 const exploded_edge *,
142 region_model_context *ctxt) const final override
143 {
144 call_details cd (get_call_details (model, ctxt));
145 model->update_for_zero_return (cd, true);
146 model->set_value (m_sized_dest_reg, m_copied_sval, ctxt);
147 if (ctxt && m_untrusted_source)
148 model->mark_as_tainted (m_copied_sval, ctxt);
149 if (m_untrusted_destination)
150 model->maybe_complain_about_infoleak (m_sized_dest_reg,
151 m_copied_sval,
152 m_sized_src_reg,
153 ctxt);
154 return true;
155 }
156
157 const region *m_sized_dest_reg;
158 const svalue *m_copied_sval;
159 const region *m_sized_src_reg;
160 bool m_untrusted_source;
161 bool m_untrusted_destination;
162 };
163
164 class copy_failure : public failed_call_info
165 {
166 public:
167 copy_failure (const call_details &cd)
168 : failed_call_info (cd)
169 {}
170
171 bool update_model (region_model *model,
172 const exploded_edge *,
173 region_model_context *ctxt) const final override
174 {
175 call_details cd (get_call_details (model, ctxt));
176 model->update_for_nonzero_return (cd);
177 /* Leave the destination region untouched. */
178 return true;
179 }
180 };
181 };
182
183 /* "copy_from_user". */
184
185 class known_function_copy_from_user : public copy_across_boundary_fn
186 {
187 public:
188 bool untrusted_source_p () const final override
189 {
190 return true;
191 }
192 bool untrusted_destination_p () const final override
193 {
194 return false;
195 }
196 };
197
198 /* "copy_to_user". */
199
200 class known_function_copy_to_user : public copy_across_boundary_fn
201 {
202 public:
203 bool untrusted_source_p () const final override
204 {
205 return false;
206 }
207 bool untrusted_destination_p () const final override
208 {
209 return true;
210 }
211 };
212
213 /* Implementation of "__check_object_size". */
214
215 class known_function___check_object_size : public known_function
216 {
217 public:
218 bool matches_call_types_p (const call_details &cd) const final override
219 {
220 return cd.num_args () == 2;
221 }
222
223 void impl_call_pre (const call_details &) const final override
224 {
225 /* No-op. */
226 }
227 };
228
229 /* Callback handler for the PLUGIN_ANALYZER_INIT event. */
230
231 static void
232 kernel_analyzer_init_cb (void *gcc_data, void */*user_data*/)
233 {
234 ana::plugin_analyzer_init_iface *iface
235 = (ana::plugin_analyzer_init_iface *)gcc_data;
236 LOG_SCOPE (iface->get_logger ());
237 if (0)
238 inform (input_location, "got here: kernel_analyzer_init_cb");
239 iface->register_known_function
240 ("copy_from_user",
241 make_unique<known_function_copy_from_user> ());
242 iface->register_known_function ("copy_to_user",
243 make_unique<known_function_copy_to_user> ());
244 iface->register_known_function ("__check_object_size",
245 make_unique<known_function___check_object_size> ());
246 }
247
248 } // namespace ana
249
250 #endif /* #if ENABLE_ANALYZER */
251
252 int
253 plugin_init (struct plugin_name_args *plugin_info,
254 struct plugin_gcc_version *version)
255 {
256 #if ENABLE_ANALYZER
257 const char *plugin_name = plugin_info->base_name;
258 if (0)
259 inform (input_location, "got here; %qs", plugin_name);
260 register_callback (plugin_info->base_name,
261 PLUGIN_ANALYZER_INIT,
262 ana::kernel_analyzer_init_cb,
263 NULL); /* void *user_data */
264 #else
265 sorry_no_analyzer ();
266 #endif
267 return 0;
268 }