2 * Copyright (C) 2017 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * One problem that I have is that it's really hard to track how pointers are
20 * passed around. For example, it would be nice to know that the probe() and
21 * remove() functions get the same pci_dev pointer. It would be good to know
22 * what pointers we're passing to the open() and close() functions. But that
23 * information gets lost in a call tree full of function pointer calls.
25 * I think the first step is to start naming specific pointers. So when a
26 * pointer is allocated, then it gets a tag. So calls to kmalloc() generate a
27 * tag. But we might not use that, because there might be a better name like
28 * framebuffer_alloc(). The framebuffer_alloc() is interesting because there is
29 * one per driver and it's passed around to all the file operations.
31 * Perhaps we could make a list of functions like framebuffer_alloc() which take
32 * a size and say that those are the interesting alloc functions.
34 * Another place where we would maybe name the pointer is when they are passed
35 * to the probe(). Because that's an important pointer, since there is one
36 * per driver (sort of).
38 * My vision is that you could take a pointer and trace it back to a global. So
39 * I'm going to track that pointer_tag - 28 bytes takes you to another pointer
40 * tag. You could follow that one back and so on. Also when we pass a pointer
41 * to a function that would be recorded as sort of a link or path or something.
46 #include "smatch_slist.h"
47 #include "smatch_extra.h"
49 #include <openssl/md5.h>
53 mtag_t
str_to_mtag(const char *str
)
55 unsigned char c
[MD5_DIGEST_LENGTH
];
56 unsigned long long *tag
= (unsigned long long *)&c
;
62 MD5_Update(&mdContext
, str
, len
);
63 MD5_Final(c
, &mdContext
);
65 *tag
&= ~MTAG_ALIAS_BIT
;
66 *tag
&= ~MTAG_OFFSET_MASK
;
71 static int save_allocator(void *_allocator
, int argc
, char **argv
, char **azColName
)
73 char **allocator
= _allocator
;
76 if (strcmp(*allocator
, argv
[0]) == 0)
78 /* should be impossible */
79 free_string(*allocator
);
80 *allocator
= alloc_string("unknown");
83 *allocator
= alloc_string(argv
[0]);
87 char *get_allocator_info_from_tag(mtag_t tag
)
89 char *allocator
= NULL
;
91 run_sql(save_allocator
, &allocator
,
92 "select value from mtag_info where tag = %lld and type = %d;",
98 static char *get_allocator_info(struct expression
*expr
, struct smatch_state
*state
)
102 if (expr
->type
!= EXPR_ASSIGNMENT
)
104 if (estate_get_single_value(state
, &sval
))
105 return get_allocator_info_from_tag(sval
.value
);
107 expr
= strip_expr(expr
->right
);
108 if (expr
->type
!= EXPR_CALL
||
110 expr
->fn
->type
!= EXPR_SYMBOL
)
112 return expr_to_str(expr
->fn
);
115 static void update_mtag_info(struct expression
*expr
, mtag_t tag
,
116 const char *left_name
, const char *tag_info
,
117 struct smatch_state
*state
)
121 sql_insert_mtag_about(tag
, left_name
, tag_info
);
123 allocator
= get_allocator_info(expr
, state
);
125 sql_insert_mtag_info(tag
, ALLOCATOR
, allocator
);
128 struct smatch_state
*get_mtag_return(struct expression
*expr
, struct smatch_state
*state
)
130 struct expression
*left
, *right
;
131 char *left_name
, *right_name
;
132 struct symbol
*left_sym
;
133 struct range_list
*rl
;
138 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
|| expr
->op
!= '=')
140 if (!is_fresh_alloc(expr
->right
))
142 if (!rl_intersection(estate_rl(state
), valid_ptr_rl
))
145 left
= strip_expr(expr
->left
);
146 right
= strip_expr(expr
->right
);
148 left_name
= expr_to_str_sym(left
, &left_sym
);
149 if (!left_name
|| !left_sym
)
151 right_name
= expr_to_str(right
);
153 snprintf(buf
, sizeof(buf
), "%s %s %s %s", get_filename(), get_function(),
154 left_name
, right_name
);
155 tag
= str_to_mtag(buf
);
156 tag_sval
.type
= estate_type(state
);
157 tag_sval
.uvalue
= tag
;
159 rl
= rl_filter(estate_rl(state
), valid_ptr_rl
);
161 add_range(&rl
, tag_sval
, tag_sval
);
163 update_mtag_info(expr
, tag
, left_name
, buf
, state
);
165 free_string(left_name
);
166 free_string(right_name
);
168 return alloc_estate_rl(rl
);
171 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
)
175 if (expr
->type
!= EXPR_STRING
|| !expr
->string
)
178 /* I was worried about collisions so I added a xor */
179 xor = str_to_mtag("__smatch string");
180 *tag
= str_to_mtag(expr
->string
->data
);
186 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
)
194 !(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
197 snprintf(buf
, sizeof(buf
), "%s %s",
198 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_filename() : "extern",
200 *tag
= str_to_mtag(buf
);
204 bool get_symbol_mtag(struct symbol
*sym
, mtag_t
*tag
)
208 if (!sym
|| !sym
->ident
)
211 if (get_toplevel_mtag(sym
, tag
))
214 if (get_param_num_from_sym(sym
) >= 0)
217 snprintf(buf
, sizeof(buf
), "%s %s %s",
218 get_filename(), get_function(), sym
->ident
->name
);
219 *tag
= str_to_mtag(buf
);
223 static void global_variable(struct symbol
*sym
)
227 if (!get_toplevel_mtag(sym
, &tag
))
230 sql_insert_mtag_about(tag
,
232 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_filename() : "extern");
235 static int get_array_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
)
237 struct expression
*array
, *offset_expr
;
245 array
= get_array_base(expr
);
248 type
= get_type(array
);
249 if (!type
|| type
->type
!= SYM_ARRAY
)
251 type
= get_real_base_type(type
);
252 if (!type_bytes(type
))
255 if (!expr_to_mtag_offset(array
, tag
, &start_offset
))
258 offset_expr
= get_array_offset(expr
);
259 if (!get_value(offset_expr
, &sval
))
261 *offset
= start_offset
+ sval
.value
* type_bytes(type
);
266 struct range_list
*swap_mtag_seed(struct expression
*expr
, struct range_list
*rl
)
273 if (!rl_to_sval(rl
, &sval
))
275 if (sval
.type
->type
!= SYM_PTR
|| sval
.uvalue
!= MTAG_SEED
)
278 name
= expr_to_str(expr
);
279 snprintf(buf
, sizeof(buf
), "%s %s %s", get_filename(), get_function(), name
);
281 tag
= str_to_mtag(buf
);
283 return alloc_rl(sval
, sval
);
286 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new)
289 int lines_from_start
;
293 * We need the alias to be unique. It's not totally required that it
294 * be the same from one DB build to then next, but it makes debugging
302 lines_from_start
= expr
->pos
.line
- cur_func_sym
->pos
.line
;
303 str
= expr_to_str(expr
);
304 snprintf(buf
, sizeof(buf
), "%lld %d %s", tag
, lines_from_start
, str
);
307 *new = str_to_mtag(buf
);
308 sql_insert_mtag_alias(tag
, *new);
313 static int get_implied_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
)
315 struct smatch_state
*state
;
319 type
= get_type(expr
);
320 if (!type_is_ptr(type
))
322 state
= get_extra_state(expr
);
323 if (!state
|| !estate_get_single_value(state
, &sval
) || sval
.value
== 0)
326 *tag
= sval
.uvalue
& ~MTAG_OFFSET_MASK
;
327 *offset
= sval
.uvalue
& MTAG_OFFSET_MASK
;
332 * The point of this function is to give you the mtag and the offset so
333 * you can look up the data in the DB. It takes an expression.
335 * So say you give it "foo->bar". Then it would give you the offset of "bar"
336 * and the implied value of "foo". Or if you lookup "*foo" then the offset is
337 * zero and we look up the implied value of "foo. But if the expression is
338 * foo, then if "foo" is a global variable, then we get the mtag and the offset
339 * is zero. If "foo" is a local variable, then there is nothing to look up in
340 * the mtag_data table because that's handled by smatch_extra.c to this returns
344 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
)
349 if (bits_in_pointer
!= 64)
352 expr
= strip_expr(expr
);
357 return get_array_mtag_offset(expr
, tag
, offset
);
359 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
360 expr
= strip_expr(expr
->unop
);
361 return get_implied_mtag_offset(expr
, tag
, offset
);
362 } else if (expr
->type
== EXPR_DEREF
) {
363 int tmp
, tmp_offset
= 0;
365 while (expr
->type
== EXPR_DEREF
) {
366 tmp
= get_member_offset_from_deref(expr
);
370 expr
= strip_expr(expr
->deref
);
372 *offset
= tmp_offset
;
373 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
374 expr
= strip_expr(expr
->unop
);
376 if (get_implied_mtag_offset(expr
, tag
, &tmp_offset
)) {
377 // FIXME: look it up recursively?
383 } else if (expr
->type
== EXPR_SYMBOL
) {
384 return get_symbol_mtag(expr
->symbol
, tag
);
387 } else if (expr
->type
== EXPR_SYMBOL
) {
388 return get_symbol_mtag(expr
->symbol
, tag
);
394 * This function takes an address and returns an sval. Let's take some
395 * example things you might pass to it:
397 * If we were only called from smatch_math, we wouldn't need to bother with
398 * this because it's already been looked up in smatch_extra.c but this is
399 * also called from other places so we have to check smatch_extra.c.
401 * If "foo" is global return the mtag for "foo".
403 * If "foo" is global return the mtag for "foo" + the offset of ".bar".
404 * It also handles string literals.
407 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
)
413 if (bits_in_pointer
!= 64)
416 expr
= strip_expr(expr
);
418 type
= get_type(expr
);
419 if (!type_is_ptr(type
))
422 * There are several options:
424 * If the expr is a string literal, that's an address/mtag.
425 * SYM_ARRAY and SYM_FN are mtags. There are "&foo" type addresses.
426 * And there are saved pointers "p = &foo;"
430 if (expr
->type
== EXPR_STRING
&& get_string_mtag(expr
, &tag
))
433 if (expr
->type
== EXPR_SYMBOL
&&
434 (type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
) &&
435 get_toplevel_mtag(expr
->symbol
, &tag
))
438 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
439 expr
= strip_expr(expr
->unop
);
440 if (expr_to_mtag_offset(expr
, &tag
, &offset
))
445 if (get_implied_mtag_offset(expr
, &tag
, &offset
))
450 if (offset
>= MTAG_OFFSET_MASK
)
454 sval
->uvalue
= tag
| offset
;
459 void register_mtag(int id
)
465 * The mtag stuff only works on 64 systems because we store the
466 * information in the pointer itself.
467 * bit 63 : set for alias mtags
468 * bit 62-12: mtag hash
473 add_hook(&global_variable
, BASE_HOOK
);