db: move return_states to raw SQL
[smatch.git] / smatch_extra.c
blob0c405de702264d61a0f8dae6cb4e72c81637a4ff
1 /*
2 * sparse/smatch_extra.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * smatch_extra.c is supposed to track the value of every variable.
15 #include <stdlib.h>
16 #include <errno.h>
17 #ifndef __USE_ISOC99
18 #define __USE_ISOC99
19 #endif
20 #include <limits.h>
21 #include "parse.h"
22 #include "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 static int my_id;
28 struct string_list *__ignored_macros = NULL;
29 static int in_warn_on_macro()
31 struct statement *stmt;
32 char *tmp;
33 char *macro;
35 stmt = get_current_statement();
36 macro = get_macro_name(stmt->pos);
37 if (!macro)
38 return 0;
40 FOR_EACH_PTR(__ignored_macros, tmp) {
41 if (!strcmp(tmp, macro))
42 return 1;
43 } END_FOR_EACH_PTR(tmp);
44 return 0;
47 typedef void (mod_hook)(const char *name, struct symbol *sym, struct smatch_state *state);
48 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
49 static struct void_fn_list *extra_mod_hooks;
50 void add_extra_mod_hook(mod_hook *fn)
52 mod_hook **p = malloc(sizeof(mod_hook *));
53 *p = fn;
54 add_ptr_list(&extra_mod_hooks, p);
57 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
59 mod_hook **fn;
61 FOR_EACH_PTR(extra_mod_hooks, fn) {
62 (*fn)(name, sym, state);
63 } END_FOR_EACH_PTR(fn);
66 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
68 if (in_warn_on_macro())
69 return NULL;
70 remove_from_equiv(name, sym);
71 call_extra_mod_hooks(name, sym, state);
72 return set_state(SMATCH_EXTRA, name, sym, state);
75 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
77 struct symbol *sym;
78 char *name;
79 struct sm_state *ret = NULL;
81 expr = strip_expr(expr);
82 name = expr_to_var_sym(expr, &sym);
83 if (!name || !sym)
84 goto free;
85 ret = set_extra_mod(name, sym, state);
86 free:
87 free_string(name);
88 return ret;
91 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
93 struct relation *rel;
94 struct smatch_state *orig_state;
96 orig_state = get_state(SMATCH_EXTRA, name, sym);
98 if (!estate_related(orig_state)) {
99 set_state(SMATCH_EXTRA, name, sym, state);
100 return;
103 set_related(state, estate_related(orig_state));
104 FOR_EACH_PTR(estate_related(orig_state), rel) {
105 if (option_debug_related)
106 sm_msg("updating related %s to %s", rel->name, state->name);
107 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
108 } END_FOR_EACH_PTR(rel);
112 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
114 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
116 char *name;
117 struct symbol *sym;
119 name = expr_to_var_sym(expr, &sym);
120 if (!name || !sym)
121 goto free;
122 set_extra_nomod(name, sym, state);
123 free:
124 free_string(name);
128 static void set_extra_true_false(const char *name, struct symbol *sym,
129 struct smatch_state *true_state,
130 struct smatch_state *false_state)
132 struct relation *rel;
133 struct smatch_state *orig_state;
135 if (in_warn_on_macro())
136 return;
138 orig_state = get_state(SMATCH_EXTRA, name, sym);
140 if (!estate_related(orig_state)) {
141 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
142 return;
145 if (true_state)
146 set_related(true_state, estate_related(orig_state));
147 if (false_state)
148 set_related(false_state, estate_related(orig_state));
150 FOR_EACH_PTR(estate_related(orig_state), rel) {
151 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
152 true_state, false_state);
153 } END_FOR_EACH_PTR(rel);
156 static void set_extra_expr_true_false(struct expression *expr,
157 struct smatch_state *true_state,
158 struct smatch_state *false_state)
160 char *name;
161 struct symbol *sym;
163 expr = strip_expr(expr);
164 name = expr_to_var_sym(expr, &sym);
165 if (!name || !sym)
166 goto free;
167 set_extra_true_false(name, sym, true_state, false_state);
168 free:
169 free_string(name);
172 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
174 struct expression *iter_var;
175 struct expression *condition;
176 struct sm_state *sm;
177 struct smatch_state *estate;
178 sval_t start;
180 condition = strip_expr(loop->iterator_pre_condition);
181 if (!condition)
182 return NULL;
183 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
184 return NULL;
185 if (condition->op != SPECIAL_DECREMENT)
186 return NULL;
188 iter_var = condition->unop;
189 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
190 if (!sm)
191 return NULL;
192 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
193 return NULL;
194 start = estate_max(sm->state);
195 if (sval_cmp_val(start, 0) <= 0)
196 return NULL;
197 if (!sval_is_max(start))
198 start.value--;
200 if (condition->type == EXPR_PREOP) {
201 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
202 if (estate_has_hard_max(sm->state))
203 estate_set_hard_max(estate);
204 set_extra_expr_mod(iter_var, estate);
206 if (condition->type == EXPR_POSTOP) {
207 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
208 if (estate_has_hard_max(sm->state))
209 estate_set_hard_max(estate);
210 set_extra_expr_mod(iter_var, estate);
212 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
215 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
216 struct expression *condition)
218 struct expression *iter_var;
219 struct sm_state *sm;
220 struct smatch_state *estate;
221 sval_t start, end, dummy;
223 iter_var = iter_expr->unop;
224 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
225 if (!sm)
226 return NULL;
227 if (!estate_get_single_value(sm->state, &start))
228 return NULL;
229 if (!get_implied_max(condition->right, &end))
230 end = sval_type_max(get_type(iter_var));
232 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
233 return NULL;
235 switch (condition->op) {
236 case SPECIAL_NOTEQUAL:
237 case '<':
238 if (!sval_is_min(end))
239 end.value--;
240 break;
241 case SPECIAL_LTE:
242 break;
243 default:
244 return NULL;
246 if (sval_cmp(end, start) < 0)
247 return NULL;
248 estate = alloc_estate_range(start, end);
249 if (get_hard_max(condition->right, &dummy))
250 estate_set_hard_max(estate);
251 set_extra_expr_mod(iter_var, estate);
252 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
255 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
256 struct expression *condition)
258 struct expression *iter_var;
259 struct sm_state *sm;
260 struct smatch_state *estate;
261 sval_t start, end;
263 iter_var = iter_expr->unop;
264 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
265 if (!sm)
266 return NULL;
267 if (!estate_get_single_value(sm->state, &start))
268 return NULL;
269 if (!get_implied_min(condition->right, &end))
270 end = sval_type_min(get_type(iter_var));
271 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
272 return NULL;
274 switch (condition->op) {
275 case SPECIAL_NOTEQUAL:
276 case '>':
277 if (!sval_is_min(end) && !sval_is_max(end))
278 end.value++;
279 break;
280 case SPECIAL_GTE:
281 break;
282 default:
283 return NULL;
285 if (sval_cmp(end, start) > 0)
286 return NULL;
287 estate = alloc_estate_range(end, start);
288 estate_set_hard_max(estate);
289 set_extra_expr_mod(iter_var, estate);
290 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
293 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
295 struct expression *iter_expr;
296 struct expression *condition;
298 if (!loop->iterator_post_statement)
299 return NULL;
300 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
301 return NULL;
302 iter_expr = loop->iterator_post_statement->expression;
303 if (!loop->iterator_pre_condition)
304 return NULL;
305 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
306 return NULL;
307 condition = loop->iterator_pre_condition;
309 if (iter_expr->op == SPECIAL_INCREMENT)
310 return handle_canonical_for_inc(iter_expr, condition);
311 if (iter_expr->op == SPECIAL_DECREMENT)
312 return handle_canonical_for_dec(iter_expr, condition);
313 return NULL;
316 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct state_list **slist)
318 struct sm_state *ret;
320 __push_fake_cur_slist();
321 if (!loop->iterator_post_statement)
322 ret = handle_canonical_while_count_down(loop);
323 else
324 ret = handle_canonical_for_loops(loop);
325 *slist = __pop_fake_cur_slist();
326 return ret;
329 int __iterator_unchanged(struct sm_state *sm)
331 if (!sm)
332 return 0;
333 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
334 return 1;
335 return 0;
338 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
340 sval_t after_value;
342 /* paranoid checking. prolly not needed */
343 condition = strip_expr(condition);
344 if (!condition)
345 return;
346 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
347 return;
348 if (condition->op != SPECIAL_DECREMENT)
349 return;
350 after_value = estate_min(sm->state);
351 after_value.value--;
352 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
355 void __extra_pre_loop_hook_after(struct sm_state *sm,
356 struct statement *iterator,
357 struct expression *condition)
359 struct expression *iter_expr;
360 sval_t limit;
361 struct smatch_state *state;
363 if (!iterator) {
364 while_count_down_after(sm, condition);
365 return;
368 iter_expr = iterator->expression;
370 if (condition->type != EXPR_COMPARE)
371 return;
372 if (iter_expr->op == SPECIAL_INCREMENT) {
373 limit = sval_binop(estate_max(sm->state), '+',
374 sval_type_val(estate_type(sm->state), 1));
375 } else {
376 limit = sval_binop(estate_min(sm->state), '-',
377 sval_type_val(estate_type(sm->state), 1));
379 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
380 if (iter_expr->op == SPECIAL_INCREMENT)
381 state = alloc_estate_range(estate_min(sm->state), limit);
382 else
383 state = alloc_estate_range(limit, estate_max(sm->state));
384 } else {
385 state = alloc_estate_sval(limit);
387 if (!estate_has_hard_max(sm->state))
388 estate_clear_hard_max(state);
389 set_extra_mod(sm->name, sm->sym, state);
392 static struct state_list *unmatched_slist;
393 static struct smatch_state *unmatched_state(struct sm_state *sm)
395 struct smatch_state *state;
397 if (unmatched_slist) {
398 state = get_state_slist(unmatched_slist, SMATCH_EXTRA, sm->name, sm->sym);
399 if (state)
400 return state;
402 return alloc_estate_whole(estate_type(sm->state));
405 static void clear_the_pointed_at(struct expression *expr, struct state_list *slist)
407 char *name;
408 struct symbol *sym;
409 struct sm_state *tmp;
411 name = expr_to_var_sym(expr, &sym);
412 if (!name || !sym)
413 goto free;
415 FOR_EACH_PTR(slist, tmp) {
416 if (tmp->name[0] != '*')
417 continue;
418 if (tmp->sym != sym)
419 continue;
420 if (strcmp(tmp->name + 1, name) != 0)
421 continue;
422 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
423 } END_FOR_EACH_PTR(tmp);
425 free:
426 free_string(name);
429 static void match_function_call(struct expression *expr)
431 struct expression *arg;
432 struct expression *tmp;
433 struct state_list *slist;
435 slist = get_all_states(SMATCH_EXTRA);
437 FOR_EACH_PTR(expr->args, arg) {
438 tmp = strip_expr(arg);
439 if (tmp->type == EXPR_PREOP && tmp->op == '&')
440 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
441 else
442 clear_the_pointed_at(tmp, slist);
443 } END_FOR_EACH_PTR(arg);
445 free_slist(&slist);
448 static int types_equiv_or_pointer(struct symbol *one, struct symbol *two)
450 if (!one || !two)
451 return 0;
452 if (one->type == SYM_PTR && two->type == SYM_PTR)
453 return 1;
454 return types_equiv(one, two);
457 static void match_assign(struct expression *expr)
459 struct range_list *rl = NULL;
460 struct expression *left;
461 struct expression *right;
462 struct symbol *right_sym;
463 char *right_name = NULL;
464 struct symbol *sym;
465 char *name;
466 sval_t value;
467 int known;
468 sval_t right_min, right_max;
469 sval_t tmp;
471 if (is_condition(expr->right))
472 return;
473 left = strip_expr(expr->left);
474 name = expr_to_var_sym(left, &sym);
475 if (!name)
476 return;
477 right = strip_parens(expr->right);
478 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
479 right = strip_parens(right->left);
481 if (expr->op == '=' && right->type == EXPR_CALL)
482 goto free; /* these are handled in smatch_function_hooks.c */
484 right_name = expr_to_var_sym(right, &right_sym);
485 if (expr->op == '=' && right_name && right_sym &&
486 types_equiv_or_pointer(get_type(expr->left), get_type(expr->right))) {
487 set_equiv(left, right);
488 goto free;
491 right_min = sval_type_min(get_type(expr->right));
492 right_max = sval_type_max(get_type(expr->right));
494 known = get_implied_value(right, &value);
495 switch (expr->op) {
496 case '=': {
497 struct smatch_state *state;
499 if (get_implied_rl(right, &rl)) {
500 rl = cast_rl(get_type(expr->left), rl);
501 state = alloc_estate_rl(rl);
502 if (get_hard_max(right, &tmp))
503 estate_set_hard_max(state);
504 } else {
505 rl = alloc_whole_rl(get_type(right));
506 rl = cast_rl(get_type(expr->left), rl);
507 state = alloc_estate_rl(rl);
509 set_extra_mod(name, sym, state);
510 goto free;
512 case SPECIAL_ADD_ASSIGN:
513 if (get_implied_min(left, &tmp)) {
514 if (known)
515 right_min = sval_binop(tmp, '+', value);
516 else
517 right_min = tmp;
519 if (!inside_loop() && known && get_implied_max(left, &tmp))
520 right_max = sval_binop(tmp, '+', value);
521 break;
522 case SPECIAL_SUB_ASSIGN:
523 if (get_implied_max(left, &tmp)) {
524 if (known)
525 right_max = sval_binop(tmp, '-', value);
526 else
527 right_max = tmp;
529 if (!inside_loop() && known && get_implied_min(left, &tmp))
530 right_min = sval_binop(tmp, '-', value);
531 break;
533 if (!sval_is_min(right_min) || !sval_is_max(right_max))
534 rl = cast_rl(get_type(expr->left), alloc_rl(right_min, right_max));
535 else
536 rl = alloc_whole_rl(get_type(expr->left));
537 set_extra_mod(name, sym, alloc_estate_rl(rl));
538 free:
539 free_string(right_name);
540 free_string(name);
543 static void reset_struct_members(struct sm_state *sm)
545 if (!estate_rl(sm->state))
546 return;
547 set_extra_mod(sm->name, sm->sym, alloc_estate_whole(estate_type(sm->state)));
550 static struct smatch_state *increment_state(struct smatch_state *state)
552 sval_t min = estate_min(state);
553 sval_t max = estate_max(state);
555 if (!estate_rl(state))
556 return NULL;
558 if (inside_loop())
559 max = sval_type_max(max.type);
561 if (!sval_is_min(min) && !sval_is_max(min))
562 min.value++;
563 if (!sval_is_min(max) && !sval_is_max(max))
564 max.value++;
565 return alloc_estate_range(min, max);
568 static struct smatch_state *decrement_state(struct smatch_state *state)
570 sval_t min = estate_min(state);
571 sval_t max = estate_max(state);
573 if (!estate_rl(state))
574 return NULL;
576 if (inside_loop())
577 min = sval_type_min(min.type);
579 if (!sval_is_min(min) && !sval_is_max(min))
580 min.value--;
581 if (!sval_is_min(max) && !sval_is_max(max))
582 max.value--;
583 return alloc_estate_range(min, max);
586 static void unop_expr(struct expression *expr)
588 struct smatch_state *state;
590 if (expr->smatch_flags & Handled)
591 return;
593 switch (expr->op) {
594 case SPECIAL_INCREMENT:
595 state = get_state_expr(SMATCH_EXTRA, expr->unop);
596 state = increment_state(state);
597 if (!state)
598 state = alloc_estate_whole(get_type(expr));
599 set_extra_expr_mod(expr->unop, state);
600 break;
601 case SPECIAL_DECREMENT:
602 state = get_state_expr(SMATCH_EXTRA, expr->unop);
603 state = decrement_state(state);
604 if (!state)
605 state = alloc_estate_whole(get_type(expr));
606 set_extra_expr_mod(expr->unop, state);
607 break;
608 default:
609 return;
613 static void asm_expr(struct statement *stmt)
616 struct expression *expr;
617 int state = 0;
619 FOR_EACH_PTR(stmt->asm_outputs, expr) {
620 switch (state) {
621 case 0: /* identifier */
622 case 1: /* constraint */
623 state++;
624 continue;
625 case 2: /* expression */
626 state = 0;
627 set_extra_expr_mod(expr, alloc_estate_whole(get_type(expr)));
628 continue;
630 } END_FOR_EACH_PTR(expr);
633 static void delete_state_tracker(struct tracker *t)
635 remove_from_equiv(t->name, t->sym);
636 delete_state(t->owner, t->name, t->sym);
639 static void scoped_state_extra(const char *name, struct symbol *sym)
641 struct tracker *t;
643 t = alloc_tracker(SMATCH_EXTRA, name, sym);
644 add_scope_hook((scope_hook *)&delete_state_tracker, t);
647 static void match_declarations(struct symbol *sym)
649 const char *name;
651 if (sym->ident) {
652 name = sym->ident->name;
653 if (!sym->initializer) {
654 set_state(SMATCH_EXTRA, name, sym, alloc_estate_whole(get_real_base_type(sym)));
655 scoped_state_extra(name, sym);
660 static void check_dereference(struct expression *expr)
662 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
665 static void match_dereferences(struct expression *expr)
667 if (expr->type != EXPR_PREOP)
668 return;
669 check_dereference(expr->unop);
672 static void match_pointer_as_array(struct expression *expr)
674 if (!is_array(expr))
675 return;
676 check_dereference(expr->unop->left);
679 static void set_param_dereferenced(struct expression *arg, char *unused)
681 check_dereference(arg);
684 static void match_function_def(struct symbol *sym)
686 struct symbol *arg;
688 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
689 if (!arg->ident)
690 continue;
691 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
692 } END_FOR_EACH_PTR(arg);
695 static int match_func_comparison(struct expression *expr)
697 struct expression *left = strip_expr(expr->left);
698 struct expression *right = strip_expr(expr->right);
699 sval_t sval;
701 if (left->type == EXPR_CALL) {
702 if (!get_implied_value(right, &sval))
703 return 1;
704 function_comparison(expr->op, left, sval, 1);
705 return 1;
708 if (right->type == EXPR_CALL) {
709 if (!get_implied_value(left, &sval))
710 return 1;
711 function_comparison(expr->op, right, sval, 0);
712 return 1;
715 return 0;
718 static sval_t add_one(sval_t sval)
720 sval.value++;
721 return sval;
724 static sval_t sub_one(sval_t sval)
726 sval.value--;
727 return sval;
730 static void match_comparison(struct expression *expr)
732 struct expression *left = strip_expr(expr->left);
733 struct expression *right = strip_expr(expr->right);
734 struct symbol *type;
735 struct range_list *left_orig;
736 struct range_list *left_true;
737 struct range_list *left_false;
738 struct range_list *right_orig;
739 struct range_list *right_true;
740 struct range_list *right_false;
741 struct smatch_state *left_true_state;
742 struct smatch_state *left_false_state;
743 struct smatch_state *right_true_state;
744 struct smatch_state *right_false_state;
745 sval_t min, max, dummy;
746 int left_postop = 0;
747 int right_postop = 0;
749 if (match_func_comparison(expr))
750 return;
752 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
753 if (left->type == EXPR_POSTOP) {
754 left->smatch_flags |= Handled;
755 left_postop = left->op;
757 left = strip_expr(left->unop);
759 while (left->type == EXPR_ASSIGNMENT)
760 left = strip_expr(left->left);
762 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
763 if (right->type == EXPR_POSTOP) {
764 right->smatch_flags |= Handled;
765 right_postop = right->op;
767 right = strip_expr(right->unop);
770 type = get_type(expr);
771 if (!type)
772 type = &llong_ctype;
774 if (get_implied_rl(left, &left_orig)) {
775 left_orig = cast_rl(type, left_orig);
776 } else {
777 min = sval_type_min(get_type(left));
778 max = sval_type_max(get_type(left));
779 left_orig = cast_rl(type, alloc_rl(min, max));
782 if (get_implied_rl(right, &right_orig)) {
783 right_orig = cast_rl(type, right_orig);
784 } else {
785 min = sval_type_min(get_type(right));
786 max = sval_type_max(get_type(right));
787 right_orig = cast_rl(type, alloc_rl(min, max));
789 min = sval_type_min(type);
790 max = sval_type_max(type);
792 left_true = clone_rl(left_orig);
793 left_false = clone_rl(left_orig);
794 right_true = clone_rl(right_orig);
795 right_false = clone_rl(right_orig);
797 switch (expr->op) {
798 case '<':
799 case SPECIAL_UNSIGNED_LT:
800 left_true = remove_range(left_orig, rl_max(right_orig), max);
801 if (!sval_is_min(rl_min(right_orig))) {
802 left_false = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
805 right_true = remove_range(right_orig, min, rl_min(left_orig));
806 if (!sval_is_max(rl_max(left_orig)))
807 right_false = remove_range(right_orig, add_one(rl_max(left_orig)), max);
808 break;
809 case SPECIAL_UNSIGNED_LTE:
810 case SPECIAL_LTE:
811 if (!sval_is_max(rl_max(right_orig)))
812 left_true = remove_range(left_orig, add_one(rl_max(right_orig)), max);
813 left_false = remove_range(left_orig, min, rl_min(right_orig));
815 if (!sval_is_min(rl_min(left_orig)))
816 right_true = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
817 right_false = remove_range(right_orig, rl_max(left_orig), max);
819 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
820 left_false = remove_range(left_false, rl_min(left_orig), rl_min(left_orig));
821 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
822 right_false = remove_range(right_false, rl_max(left_orig), rl_max(left_orig));
823 break;
824 case SPECIAL_EQUAL:
825 if (!sval_is_max(rl_max(right_orig))) {
826 left_true = remove_range(left_true, add_one(rl_max(right_orig)), max);
828 if (!sval_is_min(rl_min(right_orig))) {
829 left_true = remove_range(left_true, min, sub_one(rl_min(right_orig)));
831 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
832 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
834 if (!sval_is_max(rl_max(left_orig)))
835 right_true = remove_range(right_true, add_one(rl_max(left_orig)), max);
836 if (!sval_is_min(rl_min(left_orig)))
837 right_true = remove_range(right_true, min, sub_one(rl_min(left_orig)));
838 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
839 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
840 break;
841 case SPECIAL_UNSIGNED_GTE:
842 case SPECIAL_GTE:
843 if (!sval_is_min(rl_min(right_orig)))
844 left_true = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
845 left_false = remove_range(left_orig, rl_max(right_orig), max);
847 if (!sval_is_max(rl_max(left_orig)))
848 right_true = remove_range(right_orig, add_one(rl_max(left_orig)), max);
849 right_false = remove_range(right_orig, min, rl_min(left_orig));
851 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
852 right_false = remove_range(right_false, rl_min(left_orig), rl_min(left_orig));
853 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
854 left_false = remove_range(left_false, rl_max(left_orig), rl_max(left_orig));
855 break;
856 case '>':
857 case SPECIAL_UNSIGNED_GT:
858 left_true = remove_range(left_orig, min, rl_min(right_orig));
859 if (!sval_is_max(rl_max(right_orig)))
860 left_false = remove_range(left_orig, add_one(rl_max(right_orig)), max);
862 right_true = remove_range(right_orig, rl_max(left_orig), max);
863 if (!sval_is_min(rl_min(left_orig)))
864 right_false = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
865 break;
866 case SPECIAL_NOTEQUAL:
867 if (!sval_is_max(rl_max(right_orig)))
868 left_false = remove_range(left_false, add_one(rl_max(right_orig)), max);
869 if (!sval_is_min(rl_min(right_orig)))
870 left_false = remove_range(left_false, min, sub_one(rl_min(right_orig)));
871 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
872 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
874 if (!sval_is_max(rl_max(left_orig)))
875 right_false = remove_range(right_false, add_one(rl_max(left_orig)), max);
876 if (!sval_is_min(rl_min(left_orig)))
877 right_false = remove_range(right_false, min, sub_one(rl_min(left_orig)));
878 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
879 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
880 break;
881 default:
882 return;
885 left_true_state = alloc_estate_rl(cast_rl(get_type(left), left_true));
886 left_false_state = alloc_estate_rl(cast_rl(get_type(left), left_false));
887 right_true_state = alloc_estate_rl(cast_rl(get_type(right), right_true));
888 right_false_state = alloc_estate_rl(cast_rl(get_type(right), right_false));
890 switch (expr->op) {
891 case '<':
892 case SPECIAL_UNSIGNED_LT:
893 case SPECIAL_UNSIGNED_LTE:
894 case SPECIAL_LTE:
895 if (get_hard_max(right, &dummy))
896 estate_set_hard_max(left_true_state);
897 if (get_hard_max(left, &dummy))
898 estate_set_hard_max(right_false_state);
899 break;
900 case '>':
901 case SPECIAL_UNSIGNED_GT:
902 case SPECIAL_UNSIGNED_GTE:
903 case SPECIAL_GTE:
904 if (get_hard_max(left, &dummy))
905 estate_set_hard_max(right_true_state);
906 if (get_hard_max(right, &dummy))
907 estate_set_hard_max(left_false_state);
908 break;
911 if (get_hard_max(left, &dummy)) {
912 estate_set_hard_max(left_true_state);
913 estate_set_hard_max(left_false_state);
915 if (get_hard_max(right, &dummy)) {
916 estate_set_hard_max(right_true_state);
917 estate_set_hard_max(right_false_state);
920 if (left_postop == SPECIAL_INCREMENT) {
921 left_true_state = increment_state(left_true_state);
922 left_false_state = increment_state(left_false_state);
924 if (left_postop == SPECIAL_DECREMENT) {
925 left_true_state = decrement_state(left_true_state);
926 left_false_state = decrement_state(left_false_state);
928 if (right_postop == SPECIAL_INCREMENT) {
929 right_true_state = increment_state(right_true_state);
930 right_false_state = increment_state(right_false_state);
932 if (right_postop == SPECIAL_DECREMENT) {
933 right_true_state = decrement_state(right_true_state);
934 right_false_state = decrement_state(right_false_state);
937 set_extra_expr_true_false(left, left_true_state, left_false_state);
938 set_extra_expr_true_false(right, right_true_state, right_false_state);
941 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
942 void __extra_match_condition(struct expression *expr)
944 struct symbol *sym;
945 char *name;
946 struct smatch_state *pre_state;
947 struct smatch_state *true_state;
948 struct smatch_state *false_state;
950 expr = strip_expr(expr);
951 switch (expr->type) {
952 case EXPR_CALL:
953 function_comparison(SPECIAL_NOTEQUAL, expr, ll_to_sval(0), 1);
954 return;
955 case EXPR_PREOP:
956 case EXPR_SYMBOL:
957 case EXPR_DEREF: {
958 sval_t zero;
960 zero = sval_blank(expr);
961 zero.value = 0;
963 name = expr_to_var_sym(expr, &sym);
964 if (!name)
965 return;
966 pre_state = get_state(my_id, name, sym);
967 true_state = estate_filter_sval(pre_state, zero);
968 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
969 false_state = alloc_estate_sval(zero);
970 else
971 false_state = alloc_estate_empty();
972 set_extra_true_false(name, sym, true_state, false_state);
973 free_string(name);
974 return;
976 case EXPR_COMPARE:
977 match_comparison(expr);
978 return;
979 case EXPR_ASSIGNMENT:
980 __extra_match_condition(expr->left);
981 return;
985 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
986 int implied_not_equal(struct expression *expr, long long val)
988 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
991 static void struct_member_callback(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state)
993 if (estate_is_whole(state))
994 return;
995 sm_msg("info: passes param_value '%s' %d '%s' %s %s", fn, param, printed_name, state->name, global_static);
998 static void db_limited_before(void)
1000 unmatched_slist = clone_slist(__get_cur_slist());
1003 static void db_limited_after(void)
1005 free_slist(&unmatched_slist);
1008 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value)
1010 struct expression *arg;
1011 char *name;
1012 struct symbol *sym;
1013 struct sm_state *sm;
1014 struct symbol *type;
1015 struct range_list *rl;
1016 struct range_list *limit;
1017 struct range_list *new;
1019 while (expr->type == EXPR_ASSIGNMENT)
1020 expr = strip_expr(expr->right);
1021 if (expr->type != EXPR_CALL)
1022 return;
1024 arg = get_argument_from_call_expr(expr->args, param);
1025 if (!arg)
1026 return;
1028 name = get_variable_from_key(arg, key, &sym);
1029 if (!name || !sym)
1030 goto free;
1032 sm = get_sm_state(SMATCH_EXTRA, name, sym);
1033 type = get_member_type_from_key(arg, key);
1034 if (sm)
1035 rl = estate_rl(sm->state);
1036 else
1037 rl = alloc_whole_rl(type);
1039 str_to_rl(type, value, &limit);
1040 new = rl_intersection(rl, limit);
1042 /* We want to preserve the implications here */
1043 if (sm && rl_equiv(estate_rl(sm->state), new))
1044 __set_sm(sm);
1045 else
1046 set_extra_nomod(name, sym, alloc_estate_rl(new));
1048 free:
1049 free_string(name);
1052 static void db_param_add(struct expression *expr, int param, char *key, char *value)
1054 struct expression *arg;
1055 char *name;
1056 struct symbol *sym;
1057 struct symbol *type;
1058 struct smatch_state *state;
1059 struct range_list *added = NULL;
1060 struct range_list *new;
1062 while (expr->type == EXPR_ASSIGNMENT)
1063 expr = strip_expr(expr->right);
1064 if (expr->type != EXPR_CALL)
1065 return;
1067 arg = get_argument_from_call_expr(expr->args, param);
1068 if (!arg)
1069 return;
1070 name = get_variable_from_key(arg, key, &sym);
1071 if (!name || !sym)
1072 goto free;
1074 type = get_member_type_from_key(arg, key);
1075 state = get_state(SMATCH_EXTRA, name, sym);
1076 if (state) {
1077 str_to_rl(type, value, &added);
1078 new = rl_union(estate_rl(state), added);
1079 } else {
1080 new = alloc_whole_rl(type);
1083 set_extra_mod(name, sym, alloc_estate_rl(new));
1084 free:
1085 free_string(name);
1088 static void db_returned_states_param(struct expression *expr, int param, char *key, char *value)
1090 struct expression *arg;
1091 struct symbol *type;
1092 struct range_list *rl;
1093 char member_name[256];
1094 char *name;
1095 struct symbol *sym;
1097 while (expr->type == EXPR_ASSIGNMENT)
1098 expr = strip_expr(expr->right);
1099 if (expr->type != EXPR_CALL)
1100 return;
1101 arg = get_argument_from_call_expr(expr->args, param);
1102 arg = strip_expr(arg);
1103 if (!arg)
1104 return;
1106 if (strcmp(key, "*$$") == 0) {
1107 struct symbol *sym;
1108 char buf[256];
1109 char *name;
1111 if (arg->type == EXPR_PREOP && arg->op == '&') {
1112 arg = strip_expr(arg->unop);
1113 name = expr_to_var_sym(arg, &sym);
1114 } else {
1115 char *tmp = expr_to_var_sym(arg, &sym);
1117 if (!tmp)
1118 return;
1119 snprintf(buf, sizeof(buf), "*%s", tmp);
1120 name = alloc_string(buf);
1121 free_string(tmp);
1123 if (!name || !sym) {
1124 free_string(name);
1125 return;
1127 type = get_type(arg);
1128 str_to_rl(type, value, &rl);
1129 set_extra_mod(name, sym, alloc_estate_rl(rl));
1130 free_string(name);
1131 return;
1134 if (arg->type == EXPR_PREOP && arg->op == '&') {
1135 arg = strip_expr(arg->unop);
1136 name = expr_to_var_sym(arg, &sym);
1137 if (!name || !sym)
1138 goto free;
1139 snprintf(member_name, sizeof(member_name), "%s.%s", name, key + 4);
1140 } else {
1141 name = expr_to_var_sym(arg, &sym);
1142 if (!name || !sym)
1143 goto free;
1144 snprintf(member_name, sizeof(member_name), "%s%s", name, key + 2);
1146 type = get_member_type_from_key(arg, key);
1147 if (!type)
1148 goto free;
1149 str_to_rl(type, value, &rl);
1150 set_extra_mod(member_name, sym, alloc_estate_rl(rl));
1151 free:
1152 free_string(name);
1155 static void db_returned_member_info(struct expression *expr, int param, char *key, char *value)
1157 struct symbol *type;
1158 struct symbol *sym;
1159 char *name;
1160 char member_name[256];
1161 struct range_list *rl;
1163 if (expr->type != EXPR_ASSIGNMENT)
1164 return;
1166 name = expr_to_var_sym(expr->left, &sym);
1167 if (!name || !sym)
1168 goto free;
1169 snprintf(member_name, sizeof(member_name), "%s%s", name, key + 2);
1170 type = get_member_type_from_key(expr->left, key);
1171 if (!type)
1172 return;
1173 str_to_rl(type, value, &rl);
1174 set_extra_mod(member_name, sym, alloc_estate_rl(rl));
1176 free:
1177 free_string(name);
1180 static void returned_member_callback(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state)
1182 if (estate_is_whole(state))
1183 return;
1185 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1186 printed_name, state->name);
1189 static void match_call_info(struct expression *expr)
1191 struct range_list *rl = NULL;
1192 struct expression *arg;
1193 struct symbol *type;
1194 char *name;
1195 int i = 0;
1197 name = get_fnptr_name(expr->fn);
1198 if (!name)
1199 return;
1201 FOR_EACH_PTR(expr->args, arg) {
1202 type = get_arg_type(expr->fn, i);
1204 if (get_implied_rl(arg, &rl))
1205 rl = cast_rl(type, rl);
1206 else
1207 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
1209 sm_msg("info: passes param_value '%s' %d '$$' %s %s",
1210 name, i, show_rl(rl),
1211 is_static(expr->fn) ? "static" : "global");
1212 i++;
1213 } END_FOR_EACH_PTR(arg);
1215 free_string(name);
1218 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1220 struct range_list *rl = NULL;
1221 struct smatch_state *state;
1222 struct symbol *type;
1223 char fullname[256];
1225 if (strncmp(key, "$$", 2))
1226 return;
1228 snprintf(fullname, 256, "%s%s", name, key + 2);
1229 type = get_member_type_from_key(symbol_expression(sym), key);
1230 str_to_rl(type, value, &rl);
1231 state = alloc_estate_rl(rl);
1232 set_state(SMATCH_EXTRA, fullname, sym, state);
1235 void register_smatch_extra(int id)
1237 my_id = id;
1239 add_merge_hook(my_id, &merge_estates);
1240 add_unmatched_state_hook(my_id, &unmatched_state);
1241 add_hook(&match_function_def, FUNC_DEF_HOOK);
1242 add_hook(&match_declarations, DECLARATION_HOOK);
1243 add_definition_db_callback(set_param_value, PARAM_VALUE);
1244 add_db_return_states_callback(RETURN_VALUE, &db_returned_member_info);
1245 add_db_return_states_callback(PARAM_VALUE, &db_returned_states_param);
1246 add_db_return_states_before(&db_limited_before);
1247 add_db_return_states_callback(LIMITED_VALUE, &db_param_limit_filter);
1248 add_db_return_states_callback(FILTER_VALUE, &db_param_limit_filter);
1249 add_db_return_states_callback(ADDED_VALUE, &db_param_add);
1250 add_db_return_states_after(&db_limited_after);
1253 void register_smatch_extra_late(int id)
1255 add_hook(&match_dereferences, DEREF_HOOK);
1256 add_hook(&match_pointer_as_array, OP_HOOK);
1257 add_db_fn_call_callback(DEREFERENCE, &set_param_dereferenced);
1258 add_indirect_modification_hook(SMATCH_EXTRA, reset_struct_members);
1259 if (option_no_db)
1260 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1261 add_hook(&match_assign, ASSIGNMENT_HOOK);
1262 add_hook(&unop_expr, OP_HOOK);
1263 add_hook(&asm_expr, ASM_HOOK);
1265 if (option_info) {
1266 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1267 add_member_info_callback(my_id, struct_member_callback);
1268 add_returned_member_callback(my_id, returned_member_callback);