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"
51 static void store_hash(const char *str
, unsigned long long hash
)
53 sql_insert_cache_or_ignore(hash_string
, "0x%llx, '%s'", hash
, str
);
56 unsigned long long str_to_llu_hash(const char *str
)
58 unsigned long long hash
;
60 hash
= str_to_llu_hash_helper(str
);
61 store_hash(str
, hash
);
66 mtag_t
str_to_mtag(const char *str
)
68 unsigned long long tag
;
70 tag
= str_to_llu_hash_helper(str
);
72 tag
&= ~MTAG_OFFSET_MASK
;
77 static int save_allocator(void *_allocator
, int argc
, char **argv
, char **azColName
)
79 char **allocator
= _allocator
;
82 if (strcmp(*allocator
, argv
[0]) == 0)
84 /* should be impossible */
85 free_string(*allocator
);
86 *allocator
= alloc_string("unknown");
89 *allocator
= alloc_string(argv
[0]);
93 char *get_allocator_info_from_tag(mtag_t tag
)
95 char *allocator
= NULL
;
97 run_sql(save_allocator
, &allocator
,
98 "select value from mtag_info where tag = %lld and type = %d;",
104 static char *get_allocator_info(struct expression
*expr
, struct smatch_state
*state
)
108 if (expr
->type
!= EXPR_ASSIGNMENT
)
110 if (estate_get_single_value(state
, &sval
))
111 return get_allocator_info_from_tag(sval
.value
);
113 expr
= strip_expr(expr
->right
);
114 if (expr
->type
!= EXPR_CALL
||
116 expr
->fn
->type
!= EXPR_SYMBOL
)
118 return expr_to_str(expr
->fn
);
121 static void update_mtag_info(struct expression
*expr
, mtag_t tag
,
122 const char *left_name
, const char *tag_info
,
123 struct smatch_state
*state
)
127 sql_insert_mtag_about(tag
, left_name
, tag_info
);
129 allocator
= get_allocator_info(expr
, state
);
131 sql_insert_mtag_info(tag
, ALLOCATOR
, allocator
);
134 struct smatch_state
*get_mtag_return(struct expression
*expr
, struct smatch_state
*state
)
136 struct expression
*left
, *right
;
137 char *left_name
, *right_name
;
138 struct symbol
*left_sym
;
139 struct range_list
*rl
;
144 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
|| expr
->op
!= '=')
146 if (!is_fresh_alloc(expr
->right
))
148 if (!rl_intersection(estate_rl(state
), valid_ptr_rl
))
151 left
= strip_expr(expr
->left
);
152 right
= strip_expr(expr
->right
);
154 left_name
= expr_to_str_sym(left
, &left_sym
);
155 if (!left_name
|| !left_sym
)
157 right_name
= expr_to_str(right
);
159 snprintf(buf
, sizeof(buf
), "%s %s %s %s", get_filename(), get_function(),
160 left_name
, right_name
);
161 tag
= str_to_mtag(buf
);
162 tag_sval
.type
= estate_type(state
);
163 tag_sval
.uvalue
= tag
;
165 rl
= rl_filter(estate_rl(state
), valid_ptr_rl
);
167 add_range(&rl
, tag_sval
, tag_sval
);
169 update_mtag_info(expr
, tag
, left_name
, buf
, state
);
171 free_string(left_name
);
172 free_string(right_name
);
174 return alloc_estate_rl(rl
);
177 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
)
181 if (expr
->type
!= EXPR_STRING
|| !expr
->string
)
184 /* I was worried about collisions so I added a xor */
185 xor = str_to_mtag("__smatch string");
186 *tag
= str_to_mtag(expr
->string
->data
);
192 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
)
200 !(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
203 snprintf(buf
, sizeof(buf
), "%s %s",
204 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_filename() : "extern",
206 *tag
= str_to_mtag(buf
);
210 bool get_symbol_mtag(struct symbol
*sym
, mtag_t
*tag
)
214 if (!sym
|| !sym
->ident
)
217 if (get_toplevel_mtag(sym
, tag
))
220 if (get_param_num_from_sym(sym
) >= 0)
223 snprintf(buf
, sizeof(buf
), "%s %s %s",
224 get_filename(), get_function(), sym
->ident
->name
);
225 *tag
= str_to_mtag(buf
);
229 static void global_variable(struct symbol
*sym
)
233 if (!get_toplevel_mtag(sym
, &tag
))
236 sql_insert_mtag_about(tag
,
238 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_filename() : "extern");
241 static int get_array_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
)
243 struct expression
*array
, *offset_expr
;
251 array
= get_array_base(expr
);
254 type
= get_type(array
);
255 if (!type
|| type
->type
!= SYM_ARRAY
)
257 type
= get_real_base_type(type
);
258 if (!type_bytes(type
))
261 if (!expr_to_mtag_offset(array
, tag
, &start_offset
))
264 offset_expr
= get_array_offset(expr
);
265 if (!get_value(offset_expr
, &sval
))
267 *offset
= start_offset
+ sval
.value
* type_bytes(type
);
272 struct range_list
*swap_mtag_seed(struct expression
*expr
, struct range_list
*rl
)
279 if (!rl_to_sval(rl
, &sval
))
281 if (sval
.type
->type
!= SYM_PTR
|| sval
.uvalue
!= MTAG_SEED
)
284 name
= expr_to_str(expr
);
285 snprintf(buf
, sizeof(buf
), "%s %s %s", get_filename(), get_function(), name
);
287 tag
= str_to_mtag(buf
);
289 return alloc_rl(sval
, sval
);
292 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new)
295 int lines_from_start
;
299 * We need the alias to be unique. It's not totally required that it
300 * be the same from one DB build to then next, but it makes debugging
308 lines_from_start
= expr
->pos
.line
- cur_func_sym
->pos
.line
;
309 str
= expr_to_str(expr
);
310 snprintf(buf
, sizeof(buf
), "%lld %d %s", tag
, lines_from_start
, str
);
313 *new = str_to_mtag(buf
);
314 sql_insert_mtag_alias(tag
, *new);
319 static int get_implied_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
)
321 struct smatch_state
*state
;
325 type
= get_type(expr
);
326 if (!type_is_ptr(type
))
328 state
= get_extra_state(expr
);
329 if (!state
|| !estate_get_single_value(state
, &sval
) || sval
.value
== 0)
332 *tag
= sval
.uvalue
& ~MTAG_OFFSET_MASK
;
333 *offset
= sval
.uvalue
& MTAG_OFFSET_MASK
;
338 * The point of this function is to give you the mtag and the offset so
339 * you can look up the data in the DB. It takes an expression.
341 * So say you give it "foo->bar". Then it would give you the offset of "bar"
342 * and the implied value of "foo". Or if you lookup "*foo" then the offset is
343 * zero and we look up the implied value of "foo. But if the expression is
344 * foo, then if "foo" is a global variable, then we get the mtag and the offset
345 * is zero. If "foo" is a local variable, then there is nothing to look up in
346 * the mtag_data table because that's handled by smatch_extra.c to this returns
350 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
)
355 if (bits_in_pointer
!= 64)
358 expr
= strip_expr(expr
);
363 return get_array_mtag_offset(expr
, tag
, offset
);
365 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
366 expr
= strip_expr(expr
->unop
);
367 return get_implied_mtag_offset(expr
, tag
, offset
);
368 } else if (expr
->type
== EXPR_DEREF
) {
369 int tmp
, tmp_offset
= 0;
371 while (expr
->type
== EXPR_DEREF
) {
372 tmp
= get_member_offset_from_deref(expr
);
376 expr
= strip_expr(expr
->deref
);
378 *offset
= tmp_offset
;
379 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
380 expr
= strip_expr(expr
->unop
);
382 if (get_implied_mtag_offset(expr
, tag
, &tmp_offset
)) {
383 // FIXME: look it up recursively?
389 } else if (expr
->type
== EXPR_SYMBOL
) {
390 return get_symbol_mtag(expr
->symbol
, tag
);
393 } else if (expr
->type
== EXPR_SYMBOL
) {
394 return get_symbol_mtag(expr
->symbol
, tag
);
400 * This function takes an address and returns an sval. Let's take some
401 * example things you might pass to it:
403 * If we were only called from smatch_math, we wouldn't need to bother with
404 * this because it's already been looked up in smatch_extra.c but this is
405 * also called from other places so we have to check smatch_extra.c.
407 * If "foo" is global return the mtag for "foo".
409 * If "foo" is global return the mtag for "foo" + the offset of ".bar".
410 * It also handles string literals.
413 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
)
419 if (bits_in_pointer
!= 64)
422 expr
= strip_expr(expr
);
424 type
= get_type(expr
);
425 if (!type_is_ptr(type
))
428 * There are several options:
430 * If the expr is a string literal, that's an address/mtag.
431 * SYM_ARRAY and SYM_FN are mtags. There are "&foo" type addresses.
432 * And there are saved pointers "p = &foo;"
436 if (expr
->type
== EXPR_STRING
&& get_string_mtag(expr
, &tag
))
439 if (expr
->type
== EXPR_SYMBOL
&&
440 (type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
) &&
441 get_toplevel_mtag(expr
->symbol
, &tag
))
444 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
445 expr
= strip_expr(expr
->unop
);
446 if (expr_to_mtag_offset(expr
, &tag
, &offset
))
451 if (get_implied_mtag_offset(expr
, &tag
, &offset
))
456 if (offset
>= MTAG_OFFSET_MASK
)
460 sval
->uvalue
= tag
| offset
;
465 void register_mtag(int id
)
471 * The mtag stuff only works on 64 systems because we store the
472 * information in the pointer itself.
473 * bit 63 : set for alias mtags
474 * bit 62-12: mtag hash
479 add_hook(&global_variable
, BASE_HOOK
);