ranges: in cast_rl() sometimes it's better to just give up
[smatch.git] / smatch_extra.c
blob293b18a6f588172e192004006ee4c35181fbfb8b
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_cast(get_type(iter_var), end);
231 else
232 end = sval_type_max(get_type(iter_var));
234 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
235 return NULL;
237 switch (condition->op) {
238 case SPECIAL_NOTEQUAL:
239 case '<':
240 if (!sval_is_min(end))
241 end.value--;
242 break;
243 case SPECIAL_LTE:
244 break;
245 default:
246 return NULL;
248 if (sval_cmp(end, start) < 0)
249 return NULL;
250 estate = alloc_estate_range(start, end);
251 if (get_hard_max(condition->right, &dummy))
252 estate_set_hard_max(estate);
253 set_extra_expr_mod(iter_var, estate);
254 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
257 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
258 struct expression *condition)
260 struct expression *iter_var;
261 struct sm_state *sm;
262 struct smatch_state *estate;
263 sval_t start, end;
265 iter_var = iter_expr->unop;
266 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
267 if (!sm)
268 return NULL;
269 if (!estate_get_single_value(sm->state, &start))
270 return NULL;
271 if (!get_implied_min(condition->right, &end))
272 end = sval_type_min(get_type(iter_var));
273 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
274 return NULL;
276 switch (condition->op) {
277 case SPECIAL_NOTEQUAL:
278 case '>':
279 if (!sval_is_min(end) && !sval_is_max(end))
280 end.value++;
281 break;
282 case SPECIAL_GTE:
283 break;
284 default:
285 return NULL;
287 if (sval_cmp(end, start) > 0)
288 return NULL;
289 estate = alloc_estate_range(end, start);
290 estate_set_hard_max(estate);
291 set_extra_expr_mod(iter_var, estate);
292 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
295 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
297 struct expression *iter_expr;
298 struct expression *condition;
300 if (!loop->iterator_post_statement)
301 return NULL;
302 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
303 return NULL;
304 iter_expr = loop->iterator_post_statement->expression;
305 if (!loop->iterator_pre_condition)
306 return NULL;
307 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
308 return NULL;
309 condition = loop->iterator_pre_condition;
311 if (iter_expr->op == SPECIAL_INCREMENT)
312 return handle_canonical_for_inc(iter_expr, condition);
313 if (iter_expr->op == SPECIAL_DECREMENT)
314 return handle_canonical_for_dec(iter_expr, condition);
315 return NULL;
318 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct state_list **slist)
320 struct sm_state *ret;
322 __push_fake_cur_slist();
323 if (!loop->iterator_post_statement)
324 ret = handle_canonical_while_count_down(loop);
325 else
326 ret = handle_canonical_for_loops(loop);
327 *slist = __pop_fake_cur_slist();
328 return ret;
331 int __iterator_unchanged(struct sm_state *sm)
333 if (!sm)
334 return 0;
335 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
336 return 1;
337 return 0;
340 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
342 sval_t after_value;
344 /* paranoid checking. prolly not needed */
345 condition = strip_expr(condition);
346 if (!condition)
347 return;
348 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
349 return;
350 if (condition->op != SPECIAL_DECREMENT)
351 return;
352 after_value = estate_min(sm->state);
353 after_value.value--;
354 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
357 void __extra_pre_loop_hook_after(struct sm_state *sm,
358 struct statement *iterator,
359 struct expression *condition)
361 struct expression *iter_expr;
362 sval_t limit;
363 struct smatch_state *state;
365 if (!iterator) {
366 while_count_down_after(sm, condition);
367 return;
370 iter_expr = iterator->expression;
372 if (condition->type != EXPR_COMPARE)
373 return;
374 if (iter_expr->op == SPECIAL_INCREMENT) {
375 limit = sval_binop(estate_max(sm->state), '+',
376 sval_type_val(estate_type(sm->state), 1));
377 } else {
378 limit = sval_binop(estate_min(sm->state), '-',
379 sval_type_val(estate_type(sm->state), 1));
381 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
382 if (iter_expr->op == SPECIAL_INCREMENT)
383 state = alloc_estate_range(estate_min(sm->state), limit);
384 else
385 state = alloc_estate_range(limit, estate_max(sm->state));
386 } else {
387 state = alloc_estate_sval(limit);
389 if (!estate_has_hard_max(sm->state))
390 estate_clear_hard_max(state);
391 set_extra_mod(sm->name, sm->sym, state);
394 static struct state_list *unmatched_slist;
395 static struct smatch_state *unmatched_state(struct sm_state *sm)
397 struct smatch_state *state;
399 if (unmatched_slist) {
400 state = get_state_slist(unmatched_slist, SMATCH_EXTRA, sm->name, sm->sym);
401 if (state)
402 return state;
404 return alloc_estate_whole(estate_type(sm->state));
407 static void clear_the_pointed_at(struct expression *expr, struct state_list *slist)
409 char *name;
410 struct symbol *sym;
411 struct sm_state *tmp;
413 name = expr_to_var_sym(expr, &sym);
414 if (!name || !sym)
415 goto free;
417 FOR_EACH_PTR(slist, tmp) {
418 if (tmp->name[0] != '*')
419 continue;
420 if (tmp->sym != sym)
421 continue;
422 if (strcmp(tmp->name + 1, name) != 0)
423 continue;
424 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
425 } END_FOR_EACH_PTR(tmp);
427 free:
428 free_string(name);
431 static void match_function_call(struct expression *expr)
433 struct expression *arg;
434 struct expression *tmp;
435 struct state_list *slist;
437 slist = get_all_states(SMATCH_EXTRA);
439 FOR_EACH_PTR(expr->args, arg) {
440 tmp = strip_expr(arg);
441 if (tmp->type == EXPR_PREOP && tmp->op == '&')
442 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
443 else
444 clear_the_pointed_at(tmp, slist);
445 } END_FOR_EACH_PTR(arg);
447 free_slist(&slist);
450 static int types_equiv_or_pointer(struct symbol *one, struct symbol *two)
452 if (!one || !two)
453 return 0;
454 if (one->type == SYM_PTR && two->type == SYM_PTR)
455 return 1;
456 return types_equiv(one, two);
459 static int op_remove_assign(int op)
461 switch (op) {
462 case SPECIAL_ADD_ASSIGN:
463 return '+';
464 case SPECIAL_SUB_ASSIGN:
465 return '-';
466 case SPECIAL_MUL_ASSIGN:
467 return '*';
468 case SPECIAL_DIV_ASSIGN:
469 return '/';
470 case SPECIAL_MOD_ASSIGN:
471 return '%';
472 case SPECIAL_AND_ASSIGN:
473 return '&';
474 case SPECIAL_OR_ASSIGN:
475 return '|';
476 case SPECIAL_XOR_ASSIGN:
477 return '^';
478 case SPECIAL_SHL_ASSIGN:
479 return SPECIAL_LEFTSHIFT;
480 case SPECIAL_SHR_ASSIGN:
481 return SPECIAL_RIGHTSHIFT;
482 default:
483 return op;
487 static void match_assign(struct expression *expr)
489 struct range_list *rl = NULL;
490 struct expression *left;
491 struct expression *right;
492 struct expression *binop_expr;
493 struct symbol *right_sym;
494 struct symbol *left_type;
495 struct symbol *right_type;
496 char *right_name = NULL;
497 struct symbol *sym;
498 char *name;
499 sval_t value;
500 int known;
501 sval_t right_min, right_max;
502 sval_t tmp;
504 left = strip_expr(expr->left);
506 right = strip_expr(expr->right);
507 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
508 right = strip_expr(right->left);
510 if (is_condition(expr->right))
511 return; /* handled in smatch_condition.c */
512 if (expr->op == '=' && right->type == EXPR_CALL)
513 return; /* handled in smatch_function_hooks.c */
515 name = expr_to_var_sym(left, &sym);
516 if (!name)
517 return;
519 left_type = get_type(left);
520 right_type = get_type(right);
522 right_name = expr_to_var_sym(right, &right_sym);
523 if (expr->op == '=' && right_name && right_sym &&
524 types_equiv_or_pointer(left_type, right_type)) {
525 set_equiv(left, right);
526 goto free;
529 right_min = sval_type_min(right_type);
530 right_max = sval_type_max(right_type);
532 known = get_implied_value(right, &value);
533 switch (expr->op) {
534 case '=': {
535 struct smatch_state *state;
537 if (get_implied_rl(right, &rl)) {
538 rl = cast_rl(left_type, rl);
539 state = alloc_estate_rl(rl);
540 if (get_hard_max(right, &tmp))
541 estate_set_hard_max(state);
542 } else {
543 rl = alloc_whole_rl(right_type);
544 rl = cast_rl(left_type, rl);
545 state = alloc_estate_rl(rl);
547 set_extra_mod(name, sym, state);
548 goto free;
550 case SPECIAL_ADD_ASSIGN:
551 if (get_implied_min(left, &tmp)) {
552 if (known)
553 right_min = sval_binop(tmp, '+', value);
554 else
555 right_min = tmp;
557 if (!inside_loop() && known && get_implied_max(left, &tmp))
558 right_max = sval_binop(tmp, '+', value);
559 break;
560 case SPECIAL_SUB_ASSIGN:
561 if (get_implied_max(left, &tmp)) {
562 if (known)
563 right_max = sval_binop(tmp, '-', value);
564 else
565 right_max = tmp;
567 if (!inside_loop() && known && get_implied_min(left, &tmp))
568 right_min = sval_binop(tmp, '-', value);
569 break;
570 case SPECIAL_AND_ASSIGN:
571 case SPECIAL_MOD_ASSIGN:
572 binop_expr = binop_expression(expr->left,
573 op_remove_assign(expr->op),
574 expr->right);
575 if (get_implied_rl(binop_expr, &rl)) {
576 rl = cast_rl(left_type, rl);
577 set_extra_mod(name, sym, alloc_estate_rl(rl));
578 goto free;
581 if (!sval_is_min(right_min) || !sval_is_max(right_max))
582 rl = cast_rl(left_type, alloc_rl(right_min, right_max));
583 else
584 rl = alloc_whole_rl(left_type);
585 set_extra_mod(name, sym, alloc_estate_rl(rl));
586 free:
587 free_string(right_name);
588 free_string(name);
591 static void reset_struct_members(struct sm_state *sm)
593 if (!estate_rl(sm->state))
594 return;
595 set_extra_mod(sm->name, sm->sym, alloc_estate_whole(estate_type(sm->state)));
598 static struct smatch_state *increment_state(struct smatch_state *state)
600 sval_t min = estate_min(state);
601 sval_t max = estate_max(state);
603 if (!estate_rl(state))
604 return NULL;
606 if (inside_loop())
607 max = sval_type_max(max.type);
609 if (!sval_is_min(min) && !sval_is_max(min))
610 min.value++;
611 if (!sval_is_min(max) && !sval_is_max(max))
612 max.value++;
613 return alloc_estate_range(min, max);
616 static struct smatch_state *decrement_state(struct smatch_state *state)
618 sval_t min = estate_min(state);
619 sval_t max = estate_max(state);
621 if (!estate_rl(state))
622 return NULL;
624 if (inside_loop())
625 min = sval_type_min(min.type);
627 if (!sval_is_min(min) && !sval_is_max(min))
628 min.value--;
629 if (!sval_is_min(max) && !sval_is_max(max))
630 max.value--;
631 return alloc_estate_range(min, max);
634 static void unop_expr(struct expression *expr)
636 struct smatch_state *state;
638 if (expr->smatch_flags & Handled)
639 return;
641 switch (expr->op) {
642 case SPECIAL_INCREMENT:
643 state = get_state_expr(SMATCH_EXTRA, expr->unop);
644 state = increment_state(state);
645 if (!state)
646 state = alloc_estate_whole(get_type(expr));
647 set_extra_expr_mod(expr->unop, state);
648 break;
649 case SPECIAL_DECREMENT:
650 state = get_state_expr(SMATCH_EXTRA, expr->unop);
651 state = decrement_state(state);
652 if (!state)
653 state = alloc_estate_whole(get_type(expr));
654 set_extra_expr_mod(expr->unop, state);
655 break;
656 default:
657 return;
661 static void asm_expr(struct statement *stmt)
664 struct expression *expr;
665 struct symbol *type;
666 int state = 0;
668 FOR_EACH_PTR(stmt->asm_outputs, expr) {
669 switch (state) {
670 case 0: /* identifier */
671 case 1: /* constraint */
672 state++;
673 continue;
674 case 2: /* expression */
675 state = 0;
676 type = get_type(strip_expr(expr));
677 set_extra_expr_mod(expr, alloc_estate_whole(type));
678 continue;
680 } END_FOR_EACH_PTR(expr);
683 static void delete_state_tracker(struct tracker *t)
685 remove_from_equiv(t->name, t->sym);
686 delete_state(t->owner, t->name, t->sym);
689 static void scoped_state_extra(const char *name, struct symbol *sym)
691 struct tracker *t;
693 t = alloc_tracker(SMATCH_EXTRA, name, sym);
694 add_scope_hook((scope_hook *)&delete_state_tracker, t);
697 static void match_declarations(struct symbol *sym)
699 const char *name;
701 if (sym->ident) {
702 name = sym->ident->name;
703 if (!sym->initializer) {
704 set_state(SMATCH_EXTRA, name, sym, alloc_estate_whole(get_real_base_type(sym)));
705 scoped_state_extra(name, sym);
710 static void check_dereference(struct expression *expr)
712 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
715 static void match_dereferences(struct expression *expr)
717 if (expr->type != EXPR_PREOP)
718 return;
719 check_dereference(expr->unop);
722 static void match_pointer_as_array(struct expression *expr)
724 if (!is_array(expr))
725 return;
726 check_dereference(expr->unop->left);
729 static void set_param_dereferenced(struct expression *arg, char *unused)
731 check_dereference(arg);
734 static void match_function_def(struct symbol *sym)
736 struct symbol *arg;
738 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
739 if (!arg->ident)
740 continue;
741 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
742 } END_FOR_EACH_PTR(arg);
745 static int match_func_comparison(struct expression *expr)
747 struct expression *left = strip_expr(expr->left);
748 struct expression *right = strip_expr(expr->right);
749 sval_t sval;
751 if (left->type == EXPR_CALL) {
752 if (!get_implied_value(right, &sval))
753 return 1;
754 function_comparison(expr->op, left, sval, 1);
755 return 1;
758 if (right->type == EXPR_CALL) {
759 if (!get_implied_value(left, &sval))
760 return 1;
761 function_comparison(expr->op, right, sval, 0);
762 return 1;
765 return 0;
768 static sval_t add_one(sval_t sval)
770 sval.value++;
771 return sval;
774 static sval_t sub_one(sval_t sval)
776 sval.value--;
777 return sval;
780 static void match_comparison(struct expression *expr)
782 struct expression *left = strip_expr(expr->left);
783 struct expression *right = strip_expr(expr->right);
784 struct symbol *type;
785 struct range_list *left_orig;
786 struct range_list *left_true;
787 struct range_list *left_false;
788 struct range_list *right_orig;
789 struct range_list *right_true;
790 struct range_list *right_false;
791 struct smatch_state *left_true_state;
792 struct smatch_state *left_false_state;
793 struct smatch_state *right_true_state;
794 struct smatch_state *right_false_state;
795 sval_t min, max, dummy;
796 int left_postop = 0;
797 int right_postop = 0;
799 if (match_func_comparison(expr))
800 return;
802 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
803 if (left->type == EXPR_POSTOP) {
804 left->smatch_flags |= Handled;
805 left_postop = left->op;
807 left = strip_expr(left->unop);
809 while (left->type == EXPR_ASSIGNMENT)
810 left = strip_expr(left->left);
812 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
813 if (right->type == EXPR_POSTOP) {
814 right->smatch_flags |= Handled;
815 right_postop = right->op;
817 right = strip_expr(right->unop);
820 type = get_type(expr);
821 if (!type)
822 type = &llong_ctype;
824 if (get_implied_rl(left, &left_orig)) {
825 left_orig = cast_rl(type, left_orig);
826 } else {
827 min = sval_type_min(get_type(left));
828 max = sval_type_max(get_type(left));
829 left_orig = cast_rl(type, alloc_rl(min, max));
832 if (get_implied_rl(right, &right_orig)) {
833 right_orig = cast_rl(type, right_orig);
834 } else {
835 min = sval_type_min(get_type(right));
836 max = sval_type_max(get_type(right));
837 right_orig = cast_rl(type, alloc_rl(min, max));
839 min = sval_type_min(type);
840 max = sval_type_max(type);
842 left_true = clone_rl(left_orig);
843 left_false = clone_rl(left_orig);
844 right_true = clone_rl(right_orig);
845 right_false = clone_rl(right_orig);
847 switch (expr->op) {
848 case '<':
849 case SPECIAL_UNSIGNED_LT:
850 left_true = remove_range(left_orig, rl_max(right_orig), max);
851 if (!sval_is_min(rl_min(right_orig))) {
852 left_false = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
855 right_true = remove_range(right_orig, min, rl_min(left_orig));
856 if (!sval_is_max(rl_max(left_orig)))
857 right_false = remove_range(right_orig, add_one(rl_max(left_orig)), max);
858 break;
859 case SPECIAL_UNSIGNED_LTE:
860 case SPECIAL_LTE:
861 if (!sval_is_max(rl_max(right_orig)))
862 left_true = remove_range(left_orig, add_one(rl_max(right_orig)), max);
863 left_false = remove_range(left_orig, min, rl_min(right_orig));
865 if (!sval_is_min(rl_min(left_orig)))
866 right_true = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
867 right_false = remove_range(right_orig, rl_max(left_orig), max);
869 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
870 left_false = remove_range(left_false, rl_min(left_orig), rl_min(left_orig));
871 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
872 right_false = remove_range(right_false, rl_max(left_orig), rl_max(left_orig));
873 break;
874 case SPECIAL_EQUAL:
875 if (!sval_is_max(rl_max(right_orig))) {
876 left_true = remove_range(left_true, add_one(rl_max(right_orig)), max);
878 if (!sval_is_min(rl_min(right_orig))) {
879 left_true = remove_range(left_true, min, sub_one(rl_min(right_orig)));
881 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
882 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
884 if (!sval_is_max(rl_max(left_orig)))
885 right_true = remove_range(right_true, add_one(rl_max(left_orig)), max);
886 if (!sval_is_min(rl_min(left_orig)))
887 right_true = remove_range(right_true, min, sub_one(rl_min(left_orig)));
888 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
889 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
890 break;
891 case SPECIAL_UNSIGNED_GTE:
892 case SPECIAL_GTE:
893 if (!sval_is_min(rl_min(right_orig)))
894 left_true = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
895 left_false = remove_range(left_orig, rl_max(right_orig), max);
897 if (!sval_is_max(rl_max(left_orig)))
898 right_true = remove_range(right_orig, add_one(rl_max(left_orig)), max);
899 right_false = remove_range(right_orig, min, rl_min(left_orig));
901 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
902 right_false = remove_range(right_false, rl_min(left_orig), rl_min(left_orig));
903 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
904 left_false = remove_range(left_false, rl_max(left_orig), rl_max(left_orig));
905 break;
906 case '>':
907 case SPECIAL_UNSIGNED_GT:
908 left_true = remove_range(left_orig, min, rl_min(right_orig));
909 if (!sval_is_max(rl_max(right_orig)))
910 left_false = remove_range(left_orig, add_one(rl_max(right_orig)), max);
912 right_true = remove_range(right_orig, rl_max(left_orig), max);
913 if (!sval_is_min(rl_min(left_orig)))
914 right_false = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
915 break;
916 case SPECIAL_NOTEQUAL:
917 if (!sval_is_max(rl_max(right_orig)))
918 left_false = remove_range(left_false, add_one(rl_max(right_orig)), max);
919 if (!sval_is_min(rl_min(right_orig)))
920 left_false = remove_range(left_false, min, sub_one(rl_min(right_orig)));
921 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
922 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
924 if (!sval_is_max(rl_max(left_orig)))
925 right_false = remove_range(right_false, add_one(rl_max(left_orig)), max);
926 if (!sval_is_min(rl_min(left_orig)))
927 right_false = remove_range(right_false, min, sub_one(rl_min(left_orig)));
928 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
929 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
930 break;
931 default:
932 return;
935 left_true = rl_truncate_cast(get_type(left), left_true);
936 left_false = rl_truncate_cast(get_type(left), left_false);
937 right_true = rl_truncate_cast(get_type(right), right_true);
938 right_false = rl_truncate_cast(get_type(right), right_false);
940 left_true_state = alloc_estate_rl(left_true);
941 left_false_state = alloc_estate_rl(left_false);
942 right_true_state = alloc_estate_rl(right_true);
943 right_false_state = alloc_estate_rl(right_false);
945 switch (expr->op) {
946 case '<':
947 case SPECIAL_UNSIGNED_LT:
948 case SPECIAL_UNSIGNED_LTE:
949 case SPECIAL_LTE:
950 if (get_hard_max(right, &dummy))
951 estate_set_hard_max(left_true_state);
952 if (get_hard_max(left, &dummy))
953 estate_set_hard_max(right_false_state);
954 break;
955 case '>':
956 case SPECIAL_UNSIGNED_GT:
957 case SPECIAL_UNSIGNED_GTE:
958 case SPECIAL_GTE:
959 if (get_hard_max(left, &dummy))
960 estate_set_hard_max(right_true_state);
961 if (get_hard_max(right, &dummy))
962 estate_set_hard_max(left_false_state);
963 break;
966 if (get_hard_max(left, &dummy)) {
967 estate_set_hard_max(left_true_state);
968 estate_set_hard_max(left_false_state);
970 if (get_hard_max(right, &dummy)) {
971 estate_set_hard_max(right_true_state);
972 estate_set_hard_max(right_false_state);
975 if (left_postop == SPECIAL_INCREMENT) {
976 left_true_state = increment_state(left_true_state);
977 left_false_state = increment_state(left_false_state);
979 if (left_postop == SPECIAL_DECREMENT) {
980 left_true_state = decrement_state(left_true_state);
981 left_false_state = decrement_state(left_false_state);
983 if (right_postop == SPECIAL_INCREMENT) {
984 right_true_state = increment_state(right_true_state);
985 right_false_state = increment_state(right_false_state);
987 if (right_postop == SPECIAL_DECREMENT) {
988 right_true_state = decrement_state(right_true_state);
989 right_false_state = decrement_state(right_false_state);
992 set_extra_expr_true_false(left, left_true_state, left_false_state);
993 set_extra_expr_true_false(right, right_true_state, right_false_state);
996 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
997 void __extra_match_condition(struct expression *expr)
999 struct symbol *sym;
1000 char *name;
1001 struct smatch_state *pre_state;
1002 struct smatch_state *true_state;
1003 struct smatch_state *false_state;
1005 expr = strip_expr(expr);
1006 switch (expr->type) {
1007 case EXPR_CALL:
1008 function_comparison(SPECIAL_NOTEQUAL, expr, ll_to_sval(0), 1);
1009 return;
1010 case EXPR_PREOP:
1011 case EXPR_SYMBOL:
1012 case EXPR_DEREF: {
1013 sval_t zero;
1015 zero = sval_blank(expr);
1016 zero.value = 0;
1018 name = expr_to_var_sym(expr, &sym);
1019 if (!name)
1020 return;
1021 pre_state = get_state(my_id, name, sym);
1022 true_state = estate_filter_sval(pre_state, zero);
1023 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1024 false_state = alloc_estate_sval(zero);
1025 else
1026 false_state = alloc_estate_empty();
1027 set_extra_true_false(name, sym, true_state, false_state);
1028 free_string(name);
1029 return;
1031 case EXPR_COMPARE:
1032 match_comparison(expr);
1033 return;
1034 case EXPR_ASSIGNMENT:
1035 __extra_match_condition(expr->left);
1036 return;
1040 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1041 int implied_not_equal(struct expression *expr, long long val)
1043 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1046 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct smatch_state *state)
1048 if (estate_is_whole(state))
1049 return;
1050 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, state->name);
1053 static void db_limited_before(void)
1055 unmatched_slist = clone_slist(__get_cur_slist());
1058 static void db_limited_after(void)
1060 free_slist(&unmatched_slist);
1063 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value)
1065 struct expression *arg;
1066 char *name;
1067 struct symbol *sym;
1068 struct sm_state *sm;
1069 struct symbol *type;
1070 struct range_list *rl;
1071 struct range_list *limit;
1072 struct range_list *new;
1074 while (expr->type == EXPR_ASSIGNMENT)
1075 expr = strip_expr(expr->right);
1076 if (expr->type != EXPR_CALL)
1077 return;
1079 arg = get_argument_from_call_expr(expr->args, param);
1080 if (!arg)
1081 return;
1083 name = get_variable_from_key(arg, key, &sym);
1084 if (!name || !sym)
1085 goto free;
1087 sm = get_sm_state(SMATCH_EXTRA, name, sym);
1088 type = get_member_type_from_key(arg, key);
1089 if (sm)
1090 rl = estate_rl(sm->state);
1091 else
1092 rl = alloc_whole_rl(type);
1094 str_to_rl(type, value, &limit);
1095 new = rl_intersection(rl, limit);
1097 /* We want to preserve the implications here */
1098 if (sm && rl_equiv(estate_rl(sm->state), new))
1099 __set_sm(sm);
1100 else
1101 set_extra_nomod(name, sym, alloc_estate_rl(new));
1103 free:
1104 free_string(name);
1107 static void db_param_add(struct expression *expr, int param, char *key, char *value)
1109 struct expression *arg;
1110 char *name;
1111 struct symbol *sym;
1112 struct symbol *type;
1113 struct smatch_state *state;
1114 struct range_list *added = NULL;
1115 struct range_list *new;
1117 while (expr->type == EXPR_ASSIGNMENT)
1118 expr = strip_expr(expr->right);
1119 if (expr->type != EXPR_CALL)
1120 return;
1122 arg = get_argument_from_call_expr(expr->args, param);
1123 if (!arg)
1124 return;
1125 name = get_variable_from_key(arg, key, &sym);
1126 if (!name || !sym)
1127 goto free;
1129 type = get_member_type_from_key(arg, key);
1130 state = get_state(SMATCH_EXTRA, name, sym);
1131 if (state) {
1132 str_to_rl(type, value, &added);
1133 new = rl_union(estate_rl(state), added);
1134 } else {
1135 new = alloc_whole_rl(type);
1138 set_extra_mod(name, sym, alloc_estate_rl(new));
1139 free:
1140 free_string(name);
1143 static void db_returned_states_param(struct expression *expr, int param, char *key, char *value)
1145 struct expression *arg;
1146 struct symbol *type;
1147 struct range_list *rl;
1148 char member_name[256];
1149 char *name;
1150 struct symbol *sym;
1152 while (expr->type == EXPR_ASSIGNMENT)
1153 expr = strip_expr(expr->right);
1154 if (expr->type != EXPR_CALL)
1155 return;
1156 arg = get_argument_from_call_expr(expr->args, param);
1157 arg = strip_expr(arg);
1158 if (!arg)
1159 return;
1161 if (strcmp(key, "*$$") == 0) {
1162 struct symbol *sym;
1163 char buf[256];
1164 char *name;
1166 if (arg->type == EXPR_PREOP && arg->op == '&') {
1167 arg = strip_expr(arg->unop);
1168 name = expr_to_var_sym(arg, &sym);
1169 } else {
1170 char *tmp = expr_to_var_sym(arg, &sym);
1172 if (!tmp)
1173 return;
1174 snprintf(buf, sizeof(buf), "*%s", tmp);
1175 name = alloc_string(buf);
1176 free_string(tmp);
1178 if (!name || !sym) {
1179 free_string(name);
1180 return;
1182 type = get_type(arg);
1183 str_to_rl(type, value, &rl);
1184 set_extra_mod(name, sym, alloc_estate_rl(rl));
1185 free_string(name);
1186 return;
1189 if (arg->type == EXPR_PREOP && arg->op == '&') {
1190 arg = strip_expr(arg->unop);
1191 name = expr_to_var_sym(arg, &sym);
1192 if (!name || !sym)
1193 goto free;
1194 snprintf(member_name, sizeof(member_name), "%s.%s", name, key + 4);
1195 } else {
1196 name = expr_to_var_sym(arg, &sym);
1197 if (!name || !sym)
1198 goto free;
1199 snprintf(member_name, sizeof(member_name), "%s%s", name, key + 2);
1201 type = get_member_type_from_key(arg, key);
1202 if (!type)
1203 goto free;
1204 str_to_rl(type, value, &rl);
1205 set_extra_mod(member_name, sym, alloc_estate_rl(rl));
1206 free:
1207 free_string(name);
1210 static void db_returned_member_info(struct expression *expr, int param, char *key, char *value)
1212 struct symbol *type;
1213 struct symbol *sym;
1214 char *name;
1215 char member_name[256];
1216 struct range_list *rl;
1218 if (expr->type != EXPR_ASSIGNMENT)
1219 return;
1221 name = expr_to_var_sym(expr->left, &sym);
1222 if (!name || !sym)
1223 goto free;
1224 snprintf(member_name, sizeof(member_name), "%s%s", name, key + 2);
1225 type = get_member_type_from_key(expr->left, key);
1226 if (!type)
1227 return;
1228 str_to_rl(type, value, &rl);
1229 set_extra_mod(member_name, sym, alloc_estate_rl(rl));
1231 free:
1232 free_string(name);
1235 static void returned_member_callback(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state)
1237 if (estate_is_whole(state))
1238 return;
1240 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1241 printed_name, state->name);
1244 static void match_call_info(struct expression *expr)
1246 struct range_list *rl = NULL;
1247 struct expression *arg;
1248 struct symbol *type;
1249 int i = 0;
1251 FOR_EACH_PTR(expr->args, arg) {
1252 type = get_arg_type(expr->fn, i);
1254 if (get_implied_rl(arg, &rl))
1255 rl = cast_rl(type, rl);
1256 else
1257 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
1259 sql_insert_caller_info(expr, PARAM_VALUE, i, "$$", show_rl(rl));
1260 i++;
1261 } END_FOR_EACH_PTR(arg);
1264 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1266 struct range_list *rl = NULL;
1267 struct smatch_state *state;
1268 struct symbol *type;
1269 char fullname[256];
1271 if (strcmp(key, "*$$") == 0)
1272 snprintf(fullname, sizeof(fullname), "*%s", name);
1273 else if (strncmp(key, "$$", 2) == 0)
1274 snprintf(fullname, 256, "%s%s", name, key + 2);
1275 else
1276 return;
1278 type = get_member_type_from_key(symbol_expression(sym), key);
1279 str_to_rl(type, value, &rl);
1280 state = alloc_estate_rl(rl);
1281 set_state(SMATCH_EXTRA, fullname, sym, state);
1284 void register_smatch_extra(int id)
1286 my_id = id;
1288 add_merge_hook(my_id, &merge_estates);
1289 add_unmatched_state_hook(my_id, &unmatched_state);
1290 add_hook(&match_function_def, FUNC_DEF_HOOK);
1291 add_hook(&match_declarations, DECLARATION_HOOK);
1292 add_definition_db_callback(set_param_value, PARAM_VALUE);
1293 add_db_return_states_callback(RETURN_VALUE, &db_returned_member_info);
1294 add_db_return_states_callback(PARAM_VALUE, &db_returned_states_param);
1295 add_db_return_states_before(&db_limited_before);
1296 add_db_return_states_callback(LIMITED_VALUE, &db_param_limit_filter);
1297 add_db_return_states_callback(FILTER_VALUE, &db_param_limit_filter);
1298 add_db_return_states_callback(ADDED_VALUE, &db_param_add);
1299 add_db_return_states_after(&db_limited_after);
1302 void register_smatch_extra_late(int id)
1304 add_hook(&match_dereferences, DEREF_HOOK);
1305 add_hook(&match_pointer_as_array, OP_HOOK);
1306 add_db_fn_call_callback(DEREFERENCE, &set_param_dereferenced);
1307 add_indirect_modification_hook(SMATCH_EXTRA, reset_struct_members);
1308 if (option_no_db)
1309 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1310 add_hook(&match_assign, ASSIGNMENT_HOOK);
1311 add_hook(&unop_expr, OP_HOOK);
1312 add_hook(&asm_expr, ASM_HOOK);
1314 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1315 add_member_info_callback(my_id, struct_member_callback);
1316 add_returned_member_callback(my_id, returned_member_callback);