param_key: fix container of when no struct member is referenced
[smatch.git] / smatch_buf_comparison.c
blob0563c33709085b49e9cacf35b06b431432b7a272
1 /*
2 * Copyright (C) 2012 Oracle.
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 * The point here is to store that a buffer has x bytes even if we don't know
20 * the value of x.
24 #include "smatch.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 static int size_id;
29 static int link_id;
32 * There is a bunch of code which does this:
34 * if (size)
35 * foo = malloc(size);
37 * So if "size" is non-zero then the size of "foo" is size. But really it's
38 * also true if size is zero. It's just better to assume to not trample over
39 * the data that we have by merging &undefined states.
42 static struct smatch_state *unmatched_state(struct sm_state *sm)
44 return sm->state;
47 static struct smatch_state *merge_links(struct smatch_state *s1, struct smatch_state *s2)
49 struct expression *expr1, *expr2;
51 expr1 = s1->data;
52 expr2 = s2->data;
54 if (expr1 && expr2 && expr_equiv(expr1, expr2))
55 return s1;
56 return &merged;
59 static struct expression *ignore_link_mod;
60 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
62 struct expression *expr;
63 struct sm_state *tmp;
65 if (mod_expr == ignore_link_mod)
66 return;
67 ignore_link_mod = NULL;
69 expr = sm->state->data;
70 if (expr) {
71 set_state_expr(size_id, expr, &undefined);
72 set_state(link_id, sm->name, sm->sym, &undefined);
73 return;
76 FOR_EACH_PTR(sm->possible, tmp) {
77 expr = tmp->state->data;
78 if (expr)
79 set_state_expr(size_id, expr, &undefined);
80 } END_FOR_EACH_PTR(tmp);
81 set_state(link_id, sm->name, sm->sym, &undefined);
84 static void add_link(struct expression *size, struct expression *buf, struct expression *mod_expr)
86 ignore_link_mod = mod_expr;
87 set_state_expr(link_id, size, alloc_state_expr(buf));
90 static const char *limit_map[] = {
91 "byte_count",
92 "elem_count",
93 "elem_last",
94 "used_count",
95 "used_last",
98 int state_to_limit(struct smatch_state *state)
100 int i;
102 if (!state || !state->data)
103 return -1;
105 for (i = 0; i < ARRAY_SIZE(limit_map); i++) {
106 if (strncmp(state->name, limit_map[i], strlen(limit_map[i])) == 0)
107 return i + BYTE_COUNT;
110 return -1;
113 const char *limit_type_str(unsigned int limit_type)
115 if (limit_type - BYTE_COUNT >= ARRAY_SIZE(limit_map)) {
116 sm_msg("internal: wrong size type %u", limit_type);
117 return "unknown";
120 return limit_map[limit_type - BYTE_COUNT];
123 static struct smatch_state *alloc_compare_size(int limit_type, struct expression *expr)
125 struct smatch_state *state;
126 char *name;
127 char buf[256];
129 state = __alloc_smatch_state(0);
130 expr = strip_expr(expr);
131 name = expr_to_str(expr);
132 snprintf(buf, sizeof(buf), "%s %s", limit_type_str(limit_type), name);
133 state->name = alloc_sname(buf);
134 free_string(name);
135 state->data = expr;
136 return state;
139 static int bytes_per_element(struct expression *expr)
141 struct symbol *type;
143 type = get_type(expr);
144 if (!type)
145 return 0;
147 if (type->type != SYM_PTR && type->type != SYM_ARRAY)
148 return 0;
150 type = get_base_type(type);
151 return type_bytes(type);
154 static void db_save_type_links(struct expression *array, int type_limit, struct expression *size)
156 const char *array_name;
158 array_name = get_data_info_name(array);
159 if (!array_name)
160 array_name = "";
161 sql_insert_data_info(size, type_limit, array_name);
164 static void match_alloc_helper(struct expression *pointer, struct expression *size, struct expression *mod_expr)
166 struct smatch_state *state;
167 struct expression *tmp;
168 struct sm_state *sm;
169 int limit_type = BYTE_COUNT;
170 sval_t sval;
172 pointer = strip_expr(pointer);
173 size = strip_expr(size);
174 if (!size || !pointer)
175 return;
177 tmp = get_assigned_expr_recurse(size);
178 if (tmp && tmp->op == EXPR_BINOP)
179 size = strip_expr(tmp);
181 if (size->type == EXPR_BINOP && size->op == '*') {
182 struct expression *mult_left, *mult_right;
184 mult_left = strip_expr(size->left);
185 mult_right = strip_expr(size->right);
187 if (get_implied_value(mult_left, &sval) &&
188 sval.value == bytes_per_element(pointer))
189 size = mult_right;
190 else if (get_implied_value(mult_right, &sval) &&
191 sval.value == bytes_per_element(pointer))
192 size = mult_left;
193 else
194 return;
195 limit_type = ELEM_COUNT;
198 /* Only save links to variables, not fixed sizes */
199 if (get_value(size, &sval))
200 return;
202 if (size->type == EXPR_BINOP && size->op == '+' &&
203 get_value(size->right, &sval) && sval.value == 1) {
204 size = size->left;
205 limit_type = ELEM_LAST;
208 db_save_type_links(pointer, limit_type, size);
209 state = alloc_compare_size(limit_type, size);
210 sm = set_state_expr(size_id, pointer, state);
211 if (!sm)
212 return;
213 add_link(size, pointer, mod_expr);
216 static void match_alloc(const char *fn, struct expression *expr, void *_size_arg)
218 int size_arg = PTR_INT(_size_arg);
219 struct expression *pointer, *call, *arg;
221 pointer = strip_expr(expr->left);
222 call = strip_expr(expr->right);
223 arg = get_argument_from_call_expr(call->args, size_arg);
224 match_alloc_helper(pointer, arg, expr);
227 static void match_calloc(const char *fn, struct expression *expr, void *_start_arg)
229 struct smatch_state *state;
230 int start_arg = PTR_INT(_start_arg);
231 struct expression *pointer, *call, *arg;
232 struct sm_state *tmp;
233 int limit_type = ELEM_COUNT;
234 sval_t sval;
236 pointer = strip_expr(expr->left);
237 call = strip_expr(expr->right);
238 arg = get_argument_from_call_expr(call->args, start_arg);
239 if (get_implied_value(arg, &sval) &&
240 sval.value == bytes_per_element(pointer))
241 arg = get_argument_from_call_expr(call->args, start_arg + 1);
243 if (arg->type == EXPR_BINOP && arg->op == '+' &&
244 get_value(arg->right, &sval) && sval.value == 1) {
245 arg = arg->left;
246 limit_type = ELEM_LAST;
249 state = alloc_compare_size(limit_type, arg);
250 db_save_type_links(pointer, limit_type, arg);
251 tmp = set_state_expr(size_id, pointer, state);
252 if (!tmp)
253 return;
254 add_link(arg, pointer, expr);
257 static struct expression *get_size_variable_from_binop(struct expression *expr, int *limit_type)
259 struct smatch_state *state;
260 struct expression *ret;
261 struct symbol *type;
262 sval_t orig_fixed;
263 int offset_bytes;
264 sval_t offset;
266 if (!get_value(expr->right, &offset))
267 return NULL;
268 state = get_state_expr(size_id, expr->left);
269 if (!state || !state->data)
270 return NULL;
272 type = get_type(expr->left);
273 if (!type_is_ptr(type))
274 return NULL;
275 type = get_real_base_type(type);
276 if (!type_bytes(type))
277 return NULL;
278 offset_bytes = offset.value * type_bytes(type);
280 ret = state->data;
281 if (ret->type != EXPR_BINOP || ret->op != '+')
282 return NULL;
284 *limit_type = state_to_limit(state);
286 if (get_value(ret->left, &orig_fixed) && orig_fixed.value == offset_bytes)
287 return ret->right;
288 if (get_value(ret->right, &orig_fixed) && orig_fixed.value == offset_bytes)
289 return ret->left;
291 return NULL;
294 struct expression *get_size_variable(struct expression *buf, int *limit_type)
296 struct smatch_state *state;
297 struct expression *ret;
299 buf = strip_expr(buf);
300 if (!buf)
301 return NULL;
302 if (buf->type == EXPR_BINOP && buf->op == '+') {
303 ret = get_size_variable_from_binop(buf, limit_type);
304 if (ret)
305 return ret;
308 state = get_state_expr(size_id, buf);
309 if (!state)
310 return NULL;
311 *limit_type = state_to_limit(state);
312 return state->data;
315 struct expression *get_array_variable(struct expression *size)
317 struct smatch_state *state;
319 state = get_state_expr(link_id, size);
320 if (state)
321 return state->data;
322 return NULL;
325 static void array_check(struct expression *expr)
327 struct expression *array;
328 struct expression *size;
329 struct expression *offset;
330 char *array_str, *offset_str;
331 int limit_type;
333 expr = strip_expr(expr);
334 if (!is_array(expr))
335 return;
337 array = get_array_base(expr);
338 size = get_size_variable(array, &limit_type);
339 if (!size)
340 return;
341 if (limit_type != ELEM_COUNT)
342 return;
343 offset = get_array_offset(expr);
344 if (!possible_comparison(size, SPECIAL_EQUAL, offset))
345 return;
346 if (getting_address(expr))
347 return;
349 array_str = expr_to_str(array);
350 offset_str = expr_to_str(offset);
351 sm_warning("potentially one past the end of array '%s[%s]'", array_str, offset_str);
352 free_string(array_str);
353 free_string(offset_str);
356 struct db_info {
357 char *name;
358 int ret;
361 static int db_limitter_callback(void *_info, int argc, char **argv, char **azColName)
363 struct db_info *info = _info;
366 * If possible the limitters are tied to the struct they limit. If we
367 * aren't sure which struct they limit then we use them as limitters for
368 * everything.
370 if (!info->name || argv[0][0] == '\0' || strcmp(info->name, argv[0]) == 0)
371 info->ret = 1;
372 return 0;
375 static char *vsl_to_data_info_name(const char *name, struct var_sym_list *vsl)
377 struct var_sym *vs;
378 struct symbol *type;
379 static char buf[80];
380 const char *p;
382 if (ptr_list_size((struct ptr_list *)vsl) != 1)
383 return NULL;
384 vs = first_ptr_list((struct ptr_list *)vsl);
386 type = get_real_base_type(vs->sym);
387 if (!type || type->type != SYM_PTR)
388 goto top_level_name;
389 type = get_real_base_type(type);
390 if (!type || type->type != SYM_STRUCT)
391 goto top_level_name;
392 if (!type->ident)
393 goto top_level_name;
395 p = name;
396 while ((name = strstr(p, "->")))
397 p = name + 2;
399 snprintf(buf, sizeof(buf),"(struct %s)->%s", type->ident->name, p);
400 return alloc_sname(buf);
402 top_level_name:
403 if (!(vs->sym->ctype.modifiers & MOD_TOPLEVEL))
404 return NULL;
405 if (vs->sym->ctype.modifiers & MOD_STATIC)
406 snprintf(buf, sizeof(buf),"static %s", name);
407 else
408 snprintf(buf, sizeof(buf),"global %s", name);
409 return alloc_sname(buf);
412 int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl)
414 char *size_name;
415 char *array_name = get_data_info_name(array);
416 struct db_info db_info = {.name = array_name,};
418 size_name = vsl_to_data_info_name(name, vsl);
419 if (!size_name)
420 return 0;
422 run_sql(db_limitter_callback, &db_info,
423 "select value from data_info where type = %d and data = '%s';",
424 ARRAY_LEN, size_name);
426 return db_info.ret;
429 int buf_comparison_index_ok(struct expression *expr)
431 struct expression *array;
432 struct expression *size;
433 struct expression *offset;
434 int limit_type;
435 int comparison;
437 array = get_array_base(expr);
438 size = get_size_variable(array, &limit_type);
439 if (!size)
440 return 0;
441 offset = get_array_offset(expr);
442 comparison = get_comparison(offset, size);
443 if (!comparison)
444 return 0;
446 if ((limit_type == ELEM_COUNT || limit_type == ELEM_LAST) &&
447 (comparison == '<' || comparison == SPECIAL_UNSIGNED_LT))
448 return 1;
449 if (limit_type == ELEM_LAST &&
450 (comparison == SPECIAL_LTE ||
451 comparison == SPECIAL_UNSIGNED_LTE ||
452 comparison == SPECIAL_EQUAL))
453 return 1;
455 return 0;
458 bool buf_comp_has_bytes(struct expression *buf, struct expression *var)
460 struct expression *size;
461 int limit_type;
462 int comparison;
464 if (buf_comp2_has_bytes(buf, var))
465 return true;
467 size = get_size_variable(buf, &limit_type);
468 if (!size)
469 return false;
470 comparison = get_comparison(size, var);
471 if (comparison == UNKNOWN_COMPARISON ||
472 comparison == IMPOSSIBLE_COMPARISON)
473 return false;
475 if (show_special(comparison)[0] == '<' ||
476 show_special(comparison)[0] == '=')
477 return true;
479 return false;
482 static int known_access_ok_numbers(struct expression *expr)
484 struct expression *array;
485 struct expression *offset;
486 sval_t max;
487 int size;
489 array = get_array_base(expr);
490 offset = get_array_offset(expr);
492 size = get_array_size(array);
493 if (size <= 0)
494 return 0;
496 get_absolute_max(offset, &max);
497 if (max.uvalue < size)
498 return 1;
499 return 0;
502 static void array_check_data_info(struct expression *expr)
504 struct expression *array;
505 struct expression *offset;
506 struct state_list *slist;
507 struct sm_state *sm;
508 struct compare_data *comp;
509 char *offset_name;
510 const char *equal_name = NULL;
512 expr = strip_expr(expr);
513 if (!is_array(expr))
514 return;
516 if (known_access_ok_numbers(expr))
517 return;
518 if (buf_comparison_index_ok(expr))
519 return;
521 array = get_array_base(expr);
522 offset = get_array_offset(expr);
523 offset_name = expr_to_var(offset);
524 if (!offset_name)
525 return;
526 slist = get_all_possible_equal_comparisons(offset);
527 if (!slist)
528 goto free;
530 FOR_EACH_PTR(slist, sm) {
531 comp = sm->state->data;
532 if (strcmp(comp->left_var, offset_name) == 0) {
533 if (db_var_is_array_limit(array, comp->right_var, comp->right_vsl)) {
534 equal_name = comp->right_var;
535 break;
537 } else if (strcmp(comp->right_var, offset_name) == 0) {
538 if (db_var_is_array_limit(array, comp->left_var, comp->left_vsl)) {
539 equal_name = comp->left_var;
540 break;
543 } END_FOR_EACH_PTR(sm);
545 if (equal_name) {
546 char *array_name = expr_to_str(array);
548 sm_warning("potential off by one '%s[]' limit '%s'", array_name, equal_name);
549 free_string(array_name);
552 free:
553 free_slist(&slist);
554 free_string(offset_name);
557 static void add_allocation_function(const char *func, void *call_back, int param)
559 add_function_assign_hook(func, call_back, INT_PTR(param));
562 static int is_sizeof(struct expression *expr)
564 const char *name;
566 if (expr->type == EXPR_SIZEOF)
567 return 1;
568 name = pos_ident(expr->pos);
569 if (name && strcmp(name, "sizeof") == 0)
570 return 1;
571 return 0;
574 static int match_size_binop(struct expression *size, struct expression *expr, int *limit_type)
576 int orig_type = *limit_type;
577 struct expression *left;
578 sval_t sval;
580 left = expr->left;
581 if (!expr_equiv(size, left))
582 return 0;
584 if (expr->op == '-' &&
585 get_value(expr->right, &sval) &&
586 sval.value == 1 &&
587 orig_type == ELEM_COUNT) {
588 *limit_type = ELEM_LAST;
589 return 1;
592 if (expr->op == '+' &&
593 get_value(expr->right, &sval) &&
594 sval.value == 1 &&
595 orig_type == ELEM_LAST) {
596 *limit_type = ELEM_COUNT;
597 return 1;
600 if (expr->op == '*' &&
601 is_sizeof(expr->right) &&
602 orig_type == ELEM_COUNT) {
603 *limit_type = BYTE_COUNT;
604 return 1;
607 if (expr->op == '/' &&
608 is_sizeof(expr->right) &&
609 orig_type == BYTE_COUNT) {
610 *limit_type = ELEM_COUNT;
611 return 1;
614 return 0;
617 static char *buf_size_param_comparison(struct expression *array, struct expression_list *args, int *limit_type)
619 struct expression *tmp, *arg;
620 struct expression *size;
621 static char buf[32];
622 int i;
624 size = get_size_variable(array, limit_type);
625 if (!size)
626 return NULL;
628 if (*limit_type == USED_LAST)
629 *limit_type = ELEM_LAST;
630 if (*limit_type == USED_COUNT)
631 *limit_type = ELEM_COUNT;
633 i = -1;
634 FOR_EACH_PTR(args, tmp) {
635 i++;
636 arg = tmp;
637 if (arg == array)
638 continue;
639 if (expr_equiv(arg, size) ||
640 (arg->type == EXPR_BINOP &&
641 match_size_binop(size, arg, limit_type))) {
642 snprintf(buf, sizeof(buf), "==$%d", i);
643 return buf;
645 } END_FOR_EACH_PTR(tmp);
647 return NULL;
650 static void match_call(struct expression *call)
652 struct expression *arg;
653 char *compare;
654 int param;
655 char buf[5];
656 int limit_type;
658 param = -1;
659 FOR_EACH_PTR(call->args, arg) {
660 param++;
661 if (!is_pointer(arg))
662 continue;
663 compare = buf_size_param_comparison(arg, call->args, &limit_type);
664 if (!compare)
665 continue;
666 snprintf(buf, sizeof(buf), "%d", limit_type);
667 sql_insert_caller_info(call, limit_type, param, compare, buf);
668 } END_FOR_EACH_PTR(arg);
671 static int get_param(int param, char **name, struct symbol **sym)
673 struct symbol *arg;
674 int i;
676 i = 0;
677 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
679 * this is a temporary hack to work around a bug (I think in sparse?)
680 * 2.6.37-rc1:fs/reiserfs/journal.o
681 * If there is a function definition without parameter name found
682 * after a function implementation then it causes a crash.
683 * int foo() {}
684 * int bar(char *);
686 if (arg->ident->name < (char *)100)
687 continue;
688 if (i == param) {
689 *name = arg->ident->name;
690 *sym = arg;
691 return TRUE;
693 i++;
694 } END_FOR_EACH_PTR(arg);
696 return FALSE;
699 static void set_param_compare(const char *array_name, struct symbol *array_sym, char *key, char *value)
701 struct smatch_state *state;
702 struct expression *array_expr;
703 struct expression *size_expr;
704 struct symbol *size_sym;
705 char *size_name;
706 long param;
707 struct sm_state *tmp;
708 int limit_type;
710 if (strncmp(key, "==$", 3) != 0)
711 return;
712 param = strtol(key + 3, NULL, 10);
713 if (!get_param(param, &size_name, &size_sym))
714 return;
715 array_expr = symbol_expression(array_sym);
716 size_expr = symbol_expression(size_sym);
717 limit_type = strtol(value, NULL, 10);
719 state = alloc_compare_size(limit_type, size_expr);
720 tmp = set_state_expr(size_id, array_expr, state);
721 if (!tmp)
722 return;
723 add_link(size_expr, array_expr, NULL);
726 static void set_implied(struct expression *call, struct expression *array_expr, char *key, char *value)
728 struct expression *size_expr;
729 struct symbol *size_sym;
730 char *size_name;
731 long param;
732 struct sm_state *tmp;
733 int limit_type;
735 if (strncmp(key, "==$", 3) != 0)
736 return;
737 param = strtol(key + 3, NULL, 10);
738 if (!get_param(param, &size_name, &size_sym))
739 return;
740 size_expr = symbol_expression(size_sym);
742 limit_type = strtol(value, NULL, 10);
743 tmp = set_state_expr(size_id, array_expr, alloc_compare_size(limit_type, size_expr));
744 if (!tmp)
745 return;
746 add_link(size_expr, array_expr, call);
749 static void munge_start_states(struct statement *stmt)
751 struct state_list *slist = NULL;
752 struct sm_state *sm;
753 struct sm_state *poss;
755 FOR_EACH_MY_SM(size_id, __get_cur_stree(), sm) {
756 if (sm->state != &merged)
757 continue;
759 * screw it. let's just assume that if one caller passes the
760 * size then they all do.
762 FOR_EACH_PTR(sm->possible, poss) {
763 if (poss->state != &merged &&
764 poss->state != &undefined) {
765 add_ptr_list(&slist, poss);
766 break;
768 } END_FOR_EACH_PTR(poss);
769 } END_FOR_EACH_SM(sm);
771 FOR_EACH_PTR(slist, sm) {
772 set_state(size_id, sm->name, sm->sym, sm->state);
773 } END_FOR_EACH_PTR(sm);
775 free_slist(&slist);
778 static void set_used(struct expression *expr)
780 struct expression *parent;
781 struct expression *array;
782 struct expression *offset;
783 struct sm_state *tmp;
784 int limit_type;
786 if (expr->op != SPECIAL_INCREMENT)
787 return;
789 limit_type = USED_LAST;
790 if (expr->type == EXPR_POSTOP)
791 limit_type = USED_COUNT;
793 parent = expr_get_parent_expr(expr);
794 if (!parent || parent->type != EXPR_BINOP)
795 return;
796 parent = expr_get_parent_expr(parent);
797 if (!parent || !is_array(parent))
798 return;
800 array = get_array_base(parent);
801 offset = get_array_offset(parent);
802 if (offset != expr)
803 return;
805 tmp = set_state_expr(size_id, array, alloc_compare_size(limit_type, offset->unop));
806 if (!tmp)
807 return;
808 add_link(offset->unop, array, expr);
811 static int match_assign_array(struct expression *expr)
813 // FIXME: implement
814 return 0;
817 static int match_assign_size(struct expression *expr)
819 struct expression *right, *size, *array;
820 struct smatch_state *state;
821 struct sm_state *tmp;
822 int limit_type;
824 right = expr->right;
825 size = right;
826 if (size->type == EXPR_BINOP)
827 size = size->left;
829 array = get_array_variable(size);
830 if (!array)
831 return 0;
832 state = get_state_expr(size_id, array);
833 if (!state || !state->data)
834 return 0;
836 limit_type = state_to_limit(state);
837 if (limit_type < 0)
838 return 0;
840 if (right->type == EXPR_BINOP && !match_size_binop(size, right, &limit_type))
841 return 0;
843 tmp = set_state_expr(size_id, array, alloc_compare_size(limit_type, expr->left));
844 if (!tmp)
845 return 0;
846 add_link(expr->left, array, expr);
847 return 1;
850 static bool match_assign_smaller(struct expression *expr)
852 struct expression *array;
853 int comparison;
854 sval_t sval;
856 array = get_array_variable(expr->left);
857 if (!array)
858 return false;
860 if (get_value(expr->right, &sval))
861 return false;
863 comparison = get_comparison(expr->left, expr->right);
864 if (comparison == UNKNOWN_COMPARISON ||
865 comparison == IMPOSSIBLE_COMPARISON)
866 return false;
868 /* This is assigning a smaller value to the variable than what it was. */
869 if (show_special(comparison)[0] != '>')
870 return false;
873 * This module doesn't have a way to say that it's less than the limit
874 * only that it *is* the limit. So you might expect to set a state here
875 * but there is already a state so all we can do is ignore the
876 * assignment.
878 ignore_link_mod = expr;
879 return true;
882 static void match_assign(struct expression *expr)
884 if (expr->op != '=')
885 return;
887 if (is_fake_var_assign(expr))
888 return;
890 if (match_assign_array(expr))
891 return;
892 if (match_assign_size(expr))
893 return;
894 if (match_assign_smaller(expr))
895 return;
898 static void match_copy(const char *fn, struct expression *expr, void *unused)
900 struct expression *src, *size;
901 int src_param, size_param;
903 src = get_argument_from_call_expr(expr->args, 1);
904 size = get_argument_from_call_expr(expr->args, 2);
905 src = strip_expr(src);
906 size = strip_expr(size);
907 if (!src || !size)
908 return;
909 if (src->type != EXPR_SYMBOL || size->type != EXPR_SYMBOL)
910 return;
912 src_param = get_param_num_from_sym(src->symbol);
913 size_param = get_param_num_from_sym(size->symbol);
914 if (src_param < 0 || size_param < 0)
915 return;
917 sql_insert_cache(call_implies, "'%s', '%s', 0, %d, %d, %d, '==$%d', '%d'",
918 get_base_file(), get_function(), fn_static(),
919 BYTE_COUNT, src_param, size_param, BYTE_COUNT);
922 void register_buf_comparison(int id)
924 int i;
926 size_id = id;
928 set_dynamic_states(size_id);
930 add_unmatched_state_hook(size_id, &unmatched_state);
932 add_allocation_function("malloc", &match_alloc, 0);
933 add_allocation_function("memdup", &match_alloc, 1);
934 add_allocation_function("realloc", &match_alloc, 1);
935 if (option_project == PROJ_KERNEL) {
936 add_allocation_function("kmalloc", &match_alloc, 0);
937 add_allocation_function("kzalloc", &match_alloc, 0);
938 add_allocation_function("vmalloc", &match_alloc, 0);
939 add_allocation_function("__vmalloc", &match_alloc, 0);
940 add_allocation_function("sock_kmalloc", &match_alloc, 1);
941 add_allocation_function("kmemdup", &match_alloc, 1);
942 add_allocation_function("memdup_user", &match_alloc, 1);
943 add_allocation_function("dma_alloc_attrs", &match_alloc, 1);
944 add_allocation_function("dma_alloc_coherent", &match_alloc, 1);
945 add_allocation_function("devm_kmalloc", &match_alloc, 1);
946 add_allocation_function("devm_kzalloc", &match_alloc, 1);
947 add_allocation_function("kcalloc", &match_calloc, 0);
948 add_allocation_function("devm_kcalloc", &match_calloc, 1);
949 add_allocation_function("kmalloc_array", &match_calloc, 0);
950 add_allocation_function("krealloc", &match_alloc, 1);
952 add_function_hook("copy_from_user", &match_copy, NULL);
953 add_function_hook("__copy_from_user", &match_copy, NULL);
956 add_hook(&array_check, OP_HOOK);
957 add_hook(&array_check_data_info, OP_HOOK);
958 add_hook(&set_used, OP_HOOK);
960 add_hook(&match_call, FUNCTION_CALL_HOOK);
961 add_hook(&munge_start_states, AFTER_DEF_HOOK);
963 add_hook(&match_assign, ASSIGNMENT_HOOK);
965 for (i = BYTE_COUNT; i <= USED_COUNT; i++) {
966 select_call_implies_hook(i, &set_implied);
967 select_caller_info_hook(set_param_compare, i);
968 select_return_implies_hook(i, &set_implied);
972 void register_buf_comparison_links(int id)
974 link_id = id;
975 set_dynamic_states(link_id);
976 add_merge_hook(link_id, &merge_links);
977 add_modification_hook_late(link_id, &match_link_modify);