sval: cast bitwise negates
[smatch.git] / smatch_extra.c
blobb6693cf174e8f65bd3f295ffbc89bc2bfcd44d14
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 /* if we have the db this is handled in smatch_function_hooks.c */
438 if (!option_no_db)
439 return;
440 if (inlinable(expr->fn))
441 return;
443 slist = get_all_states(SMATCH_EXTRA);
445 FOR_EACH_PTR(expr->args, arg) {
446 tmp = strip_expr(arg);
447 if (tmp->type == EXPR_PREOP && tmp->op == '&')
448 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
449 else
450 clear_the_pointed_at(tmp, slist);
451 } END_FOR_EACH_PTR(arg);
453 free_slist(&slist);
456 static int types_equiv_or_pointer(struct symbol *one, struct symbol *two)
458 if (!one || !two)
459 return 0;
460 if (one->type == SYM_PTR && two->type == SYM_PTR)
461 return 1;
462 return types_equiv(one, two);
465 static void match_vanilla_assign(struct expression *left, struct expression *right)
467 struct range_list *rl = NULL;
468 struct symbol *right_sym;
469 struct symbol *left_type;
470 struct symbol *right_type;
471 char *right_name = NULL;
472 struct symbol *sym;
473 char *name;
474 sval_t tmp;
475 struct smatch_state *state;
477 name = expr_to_var_sym(left, &sym);
478 if (!name)
479 return;
481 left_type = get_type(left);
482 right_type = get_type(right);
484 right_name = expr_to_var_sym(right, &right_sym);
485 if (right_name && right_sym &&
486 types_equiv_or_pointer(left_type, right_type)) {
487 set_equiv(left, right);
488 goto free;
491 if (get_implied_rl(right, &rl)) {
492 rl = cast_rl(left_type, rl);
493 state = alloc_estate_rl(rl);
494 if (get_hard_max(right, &tmp))
495 estate_set_hard_max(state);
496 } else {
497 rl = alloc_whole_rl(right_type);
498 rl = cast_rl(left_type, rl);
499 state = alloc_estate_rl(rl);
501 set_extra_mod(name, sym, state);
502 free:
503 free_string(right_name);
506 static int op_remove_assign(int op)
508 switch (op) {
509 case SPECIAL_ADD_ASSIGN:
510 return '+';
511 case SPECIAL_SUB_ASSIGN:
512 return '-';
513 case SPECIAL_MUL_ASSIGN:
514 return '*';
515 case SPECIAL_DIV_ASSIGN:
516 return '/';
517 case SPECIAL_MOD_ASSIGN:
518 return '%';
519 case SPECIAL_AND_ASSIGN:
520 return '&';
521 case SPECIAL_OR_ASSIGN:
522 return '|';
523 case SPECIAL_XOR_ASSIGN:
524 return '^';
525 case SPECIAL_SHL_ASSIGN:
526 return SPECIAL_LEFTSHIFT;
527 case SPECIAL_SHR_ASSIGN:
528 return SPECIAL_RIGHTSHIFT;
529 default:
530 return op;
534 static void match_assign(struct expression *expr)
536 struct range_list *rl = NULL;
537 struct expression *left;
538 struct expression *right;
539 struct expression *binop_expr;
540 struct symbol *left_type;
541 struct symbol *right_type;
542 struct symbol *sym;
543 char *name;
544 sval_t left_min, left_max;
545 sval_t right_min, right_max;
546 sval_t res_min, res_max;
548 left = strip_expr(expr->left);
550 right = strip_expr(expr->right);
551 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
552 right = strip_expr(right->left);
554 if (is_condition(expr->right))
555 return; /* handled in smatch_condition.c */
556 if (expr->op == '=' && right->type == EXPR_CALL)
557 return; /* handled in smatch_function_hooks.c */
558 if (expr->op == '=') {
559 match_vanilla_assign(left, right);
560 return;
563 name = expr_to_var_sym(left, &sym);
564 if (!name)
565 return;
567 left_type = get_type(left);
568 right_type = get_type(right);
570 res_min = sval_type_min(right_type);
571 res_max = sval_type_max(right_type);
573 switch (expr->op) {
574 case SPECIAL_ADD_ASSIGN:
575 get_absolute_max(left, &left_max);
576 get_absolute_max(right, &right_max);
577 if (sval_binop_overflows(left_max, '+', right_max))
578 break;
579 if (get_implied_min(left, &left_min) &&
580 !sval_is_negative_min(left_min) &&
581 get_implied_min(right, &right_min) &&
582 !sval_is_negative_min(right_min)) {
583 res_min = sval_binop(left_min, '+', right_min);
584 res_min = sval_cast(right_type, res_min);
586 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
587 break;
588 res_max = sval_binop(left_max, '+', right_max);
589 res_max = sval_cast(right_type, res_max);
590 break;
591 case SPECIAL_SUB_ASSIGN:
592 if (get_implied_max(left, &left_max) &&
593 !sval_is_max(left_max) &&
594 get_implied_min(right, &right_min) &&
595 !sval_is_min(right_min)) {
596 res_max = sval_binop(left_max, '-', right_min);
597 res_max = sval_cast(right_type, res_max);
599 if (inside_loop())
600 break;
601 if (get_implied_min(left, &left_min) &&
602 !sval_is_min(left_min) &&
603 get_implied_max(right, &right_max) &&
604 !sval_is_max(right_max)) {
605 res_min = sval_binop(left_min, '-', right_max);
606 res_min = sval_cast(right_type, res_min);
608 break;
609 case SPECIAL_AND_ASSIGN:
610 case SPECIAL_MOD_ASSIGN:
611 binop_expr = binop_expression(expr->left,
612 op_remove_assign(expr->op),
613 expr->right);
614 if (get_implied_rl(binop_expr, &rl)) {
615 rl = cast_rl(left_type, rl);
616 set_extra_mod(name, sym, alloc_estate_rl(rl));
617 goto free;
620 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
621 set_extra_mod(name, sym, alloc_estate_rl(rl));
622 free:
623 free_string(name);
626 static struct smatch_state *increment_state(struct smatch_state *state)
628 sval_t min = estate_min(state);
629 sval_t max = estate_max(state);
631 if (!estate_rl(state))
632 return NULL;
634 if (inside_loop())
635 max = sval_type_max(max.type);
637 if (!sval_is_min(min) && !sval_is_max(min))
638 min.value++;
639 if (!sval_is_min(max) && !sval_is_max(max))
640 max.value++;
641 return alloc_estate_range(min, max);
644 static struct smatch_state *decrement_state(struct smatch_state *state)
646 sval_t min = estate_min(state);
647 sval_t max = estate_max(state);
649 if (!estate_rl(state))
650 return NULL;
652 if (inside_loop())
653 min = sval_type_min(min.type);
655 if (!sval_is_min(min) && !sval_is_max(min))
656 min.value--;
657 if (!sval_is_min(max) && !sval_is_max(max))
658 max.value--;
659 return alloc_estate_range(min, max);
662 static void unop_expr(struct expression *expr)
664 struct smatch_state *state;
666 if (expr->smatch_flags & Handled)
667 return;
669 switch (expr->op) {
670 case SPECIAL_INCREMENT:
671 state = get_state_expr(SMATCH_EXTRA, expr->unop);
672 state = increment_state(state);
673 if (!state)
674 state = alloc_estate_whole(get_type(expr));
675 set_extra_expr_mod(expr->unop, state);
676 break;
677 case SPECIAL_DECREMENT:
678 state = get_state_expr(SMATCH_EXTRA, expr->unop);
679 state = decrement_state(state);
680 if (!state)
681 state = alloc_estate_whole(get_type(expr));
682 set_extra_expr_mod(expr->unop, state);
683 break;
684 default:
685 return;
689 static void asm_expr(struct statement *stmt)
692 struct expression *expr;
693 struct symbol *type;
694 int state = 0;
696 FOR_EACH_PTR(stmt->asm_outputs, expr) {
697 switch (state) {
698 case 0: /* identifier */
699 case 1: /* constraint */
700 state++;
701 continue;
702 case 2: /* expression */
703 state = 0;
704 type = get_type(strip_expr(expr));
705 set_extra_expr_mod(expr, alloc_estate_whole(type));
706 continue;
708 } END_FOR_EACH_PTR(expr);
711 static void delete_state_tracker(struct tracker *t)
713 remove_from_equiv(t->name, t->sym);
714 delete_state(t->owner, t->name, t->sym);
717 static void scoped_state_extra(const char *name, struct symbol *sym)
719 struct tracker *t;
721 t = alloc_tracker(SMATCH_EXTRA, name, sym);
722 add_scope_hook((scope_hook *)&delete_state_tracker, t);
725 static void match_declarations(struct symbol *sym)
727 const char *name;
729 if (sym->ident) {
730 name = sym->ident->name;
731 if (!sym->initializer) {
732 set_state(SMATCH_EXTRA, name, sym, alloc_estate_whole(get_real_base_type(sym)));
733 scoped_state_extra(name, sym);
738 static void check_dereference(struct expression *expr)
740 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
743 static void match_dereferences(struct expression *expr)
745 if (expr->type != EXPR_PREOP)
746 return;
747 check_dereference(expr->unop);
750 static void match_pointer_as_array(struct expression *expr)
752 if (!is_array(expr))
753 return;
754 check_dereference(expr->unop->left);
757 static void set_param_dereferenced(struct expression *arg, char *unused)
759 check_dereference(arg);
762 static void match_function_def(struct symbol *sym)
764 struct symbol *arg;
766 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
767 if (!arg->ident)
768 continue;
769 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
770 } END_FOR_EACH_PTR(arg);
773 static int match_func_comparison(struct expression *expr)
775 struct expression *left = strip_expr(expr->left);
776 struct expression *right = strip_expr(expr->right);
777 sval_t sval;
779 if (left->type == EXPR_CALL) {
780 if (!get_implied_value(right, &sval))
781 return 1;
782 function_comparison(expr->op, left, sval, 1);
783 return 1;
786 if (right->type == EXPR_CALL) {
787 if (!get_implied_value(left, &sval))
788 return 1;
789 function_comparison(expr->op, right, sval, 0);
790 return 1;
793 return 0;
796 static sval_t add_one(sval_t sval)
798 sval.value++;
799 return sval;
802 static sval_t sub_one(sval_t sval)
804 sval.value--;
805 return sval;
808 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
810 struct range_list *left_orig;
811 struct range_list *left_true;
812 struct range_list *left_false;
813 struct range_list *right_orig;
814 struct range_list *right_true;
815 struct range_list *right_false;
816 struct smatch_state *left_true_state;
817 struct smatch_state *left_false_state;
818 struct smatch_state *right_true_state;
819 struct smatch_state *right_false_state;
820 sval_t min, max, dummy;
821 int left_postop = 0;
822 int right_postop = 0;
824 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
825 if (left->type == EXPR_POSTOP) {
826 left->smatch_flags |= Handled;
827 left_postop = left->op;
829 left = strip_expr(left->unop);
831 while (left->type == EXPR_ASSIGNMENT)
832 left = strip_expr(left->left);
834 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
835 if (right->type == EXPR_POSTOP) {
836 right->smatch_flags |= Handled;
837 right_postop = right->op;
839 right = strip_expr(right->unop);
842 if (get_implied_rl(left, &left_orig)) {
843 left_orig = cast_rl(type, left_orig);
844 } else {
845 min = sval_type_min(get_type(left));
846 max = sval_type_max(get_type(left));
847 left_orig = cast_rl(type, alloc_rl(min, max));
850 if (get_implied_rl(right, &right_orig)) {
851 right_orig = cast_rl(type, right_orig);
852 } else {
853 min = sval_type_min(get_type(right));
854 max = sval_type_max(get_type(right));
855 right_orig = cast_rl(type, alloc_rl(min, max));
857 min = sval_type_min(type);
858 max = sval_type_max(type);
860 left_true = clone_rl(left_orig);
861 left_false = clone_rl(left_orig);
862 right_true = clone_rl(right_orig);
863 right_false = clone_rl(right_orig);
865 switch (op) {
866 case '<':
867 case SPECIAL_UNSIGNED_LT:
868 left_true = remove_range(left_orig, rl_max(right_orig), max);
869 if (!sval_is_min(rl_min(right_orig))) {
870 left_false = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
873 right_true = remove_range(right_orig, min, rl_min(left_orig));
874 if (!sval_is_max(rl_max(left_orig)))
875 right_false = remove_range(right_orig, add_one(rl_max(left_orig)), max);
876 break;
877 case SPECIAL_UNSIGNED_LTE:
878 case SPECIAL_LTE:
879 if (!sval_is_max(rl_max(right_orig)))
880 left_true = remove_range(left_orig, add_one(rl_max(right_orig)), max);
881 left_false = remove_range(left_orig, min, rl_min(right_orig));
883 if (!sval_is_min(rl_min(left_orig)))
884 right_true = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
885 right_false = remove_range(right_orig, rl_max(left_orig), max);
887 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
888 left_false = remove_range(left_false, rl_min(left_orig), rl_min(left_orig));
889 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
890 right_false = remove_range(right_false, rl_max(left_orig), rl_max(left_orig));
891 break;
892 case SPECIAL_EQUAL:
893 if (!sval_is_max(rl_max(right_orig))) {
894 left_true = remove_range(left_true, add_one(rl_max(right_orig)), max);
896 if (!sval_is_min(rl_min(right_orig))) {
897 left_true = remove_range(left_true, min, sub_one(rl_min(right_orig)));
899 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
900 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
902 if (!sval_is_max(rl_max(left_orig)))
903 right_true = remove_range(right_true, add_one(rl_max(left_orig)), max);
904 if (!sval_is_min(rl_min(left_orig)))
905 right_true = remove_range(right_true, min, sub_one(rl_min(left_orig)));
906 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
907 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
908 break;
909 case SPECIAL_UNSIGNED_GTE:
910 case SPECIAL_GTE:
911 if (!sval_is_min(rl_min(right_orig)))
912 left_true = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
913 left_false = remove_range(left_orig, rl_max(right_orig), max);
915 if (!sval_is_max(rl_max(left_orig)))
916 right_true = remove_range(right_orig, add_one(rl_max(left_orig)), max);
917 right_false = remove_range(right_orig, min, rl_min(left_orig));
919 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
920 right_false = remove_range(right_false, rl_min(left_orig), rl_min(left_orig));
921 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
922 left_false = remove_range(left_false, rl_max(left_orig), rl_max(left_orig));
923 break;
924 case '>':
925 case SPECIAL_UNSIGNED_GT:
926 left_true = remove_range(left_orig, min, rl_min(right_orig));
927 if (!sval_is_max(rl_max(right_orig)))
928 left_false = remove_range(left_orig, add_one(rl_max(right_orig)), max);
930 right_true = remove_range(right_orig, rl_max(left_orig), max);
931 if (!sval_is_min(rl_min(left_orig)))
932 right_false = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
933 break;
934 case SPECIAL_NOTEQUAL:
935 if (!sval_is_max(rl_max(right_orig)))
936 left_false = remove_range(left_false, add_one(rl_max(right_orig)), max);
937 if (!sval_is_min(rl_min(right_orig)))
938 left_false = remove_range(left_false, min, sub_one(rl_min(right_orig)));
939 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
940 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
942 if (!sval_is_max(rl_max(left_orig)))
943 right_false = remove_range(right_false, add_one(rl_max(left_orig)), max);
944 if (!sval_is_min(rl_min(left_orig)))
945 right_false = remove_range(right_false, min, sub_one(rl_min(left_orig)));
946 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
947 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
948 break;
949 default:
950 return;
953 left_true = rl_truncate_cast(get_type(left), left_true);
954 left_false = rl_truncate_cast(get_type(left), left_false);
955 right_true = rl_truncate_cast(get_type(right), right_true);
956 right_false = rl_truncate_cast(get_type(right), right_false);
958 left_true_state = alloc_estate_rl(left_true);
959 left_false_state = alloc_estate_rl(left_false);
960 right_true_state = alloc_estate_rl(right_true);
961 right_false_state = alloc_estate_rl(right_false);
963 switch (op) {
964 case '<':
965 case SPECIAL_UNSIGNED_LT:
966 case SPECIAL_UNSIGNED_LTE:
967 case SPECIAL_LTE:
968 if (get_hard_max(right, &dummy))
969 estate_set_hard_max(left_true_state);
970 if (get_hard_max(left, &dummy))
971 estate_set_hard_max(right_false_state);
972 break;
973 case '>':
974 case SPECIAL_UNSIGNED_GT:
975 case SPECIAL_UNSIGNED_GTE:
976 case SPECIAL_GTE:
977 if (get_hard_max(left, &dummy))
978 estate_set_hard_max(right_true_state);
979 if (get_hard_max(right, &dummy))
980 estate_set_hard_max(left_false_state);
981 break;
984 if (get_hard_max(left, &dummy)) {
985 estate_set_hard_max(left_true_state);
986 estate_set_hard_max(left_false_state);
988 if (get_hard_max(right, &dummy)) {
989 estate_set_hard_max(right_true_state);
990 estate_set_hard_max(right_false_state);
993 if (left_postop == SPECIAL_INCREMENT) {
994 left_true_state = increment_state(left_true_state);
995 left_false_state = increment_state(left_false_state);
997 if (left_postop == SPECIAL_DECREMENT) {
998 left_true_state = decrement_state(left_true_state);
999 left_false_state = decrement_state(left_false_state);
1001 if (right_postop == SPECIAL_INCREMENT) {
1002 right_true_state = increment_state(right_true_state);
1003 right_false_state = increment_state(right_false_state);
1005 if (right_postop == SPECIAL_DECREMENT) {
1006 right_true_state = decrement_state(right_true_state);
1007 right_false_state = decrement_state(right_false_state);
1010 set_extra_expr_true_false(left, left_true_state, left_false_state);
1011 set_extra_expr_true_false(right, right_true_state, right_false_state);
1014 static int opposite_op(int op)
1016 switch (op) {
1017 case '+':
1018 return '-';
1019 case '-':
1020 return '+';
1022 return 0;
1025 static int is_simple_math(struct expression *expr)
1027 if (!expr)
1028 return 0;
1029 if (expr->type != EXPR_BINOP)
1030 return 0;
1031 if (!opposite_op(expr->op))
1032 return 0;
1033 return 1;
1036 static void move_known_values(struct expression **left_p, struct expression **right_p)
1038 struct expression *left = *left_p;
1039 struct expression *right = *right_p;
1040 sval_t sval;
1042 if (get_value(left, &sval)) {
1043 if (!is_simple_math(right))
1044 return;
1045 if (right->op == '+' && get_value(right->left, &sval)) {
1046 *left_p = binop_expression(left, opposite_op(right->op), right->left);
1047 *right_p = right->right;
1048 return;
1050 if (get_value(right->right, &sval)) {
1051 *left_p = binop_expression(left, opposite_op(right->op), right->right);
1052 *right_p = right->left;
1053 return;
1055 return;
1057 if (get_value(right, &sval)) {
1058 if (!is_simple_math(left))
1059 return;
1060 if (left->op == '+' && get_value(left->left, &sval)) {
1061 *right_p = binop_expression(right, opposite_op(left->op), left->left);
1062 *left_p = left->right;
1063 return;
1066 if (get_value(left->right, &sval)) {
1067 *right_p = binop_expression(right, opposite_op(left->op), left->right);
1068 *left_p = left->left;
1069 return;
1071 return;
1075 static void match_comparison(struct expression *expr)
1077 struct expression *left_orig = strip_expr(expr->left);
1078 struct expression *right_orig = strip_expr(expr->right);
1079 struct expression *left, *right;
1080 struct expression *prev;
1081 struct symbol *type;
1083 if (match_func_comparison(expr))
1084 return;
1086 type = get_type(expr);
1087 if (!type)
1088 type = &llong_ctype;
1090 left = left_orig;
1091 right = right_orig;
1092 move_known_values(&left, &right);
1093 handle_comparison(type, left, expr->op, right);
1095 prev = get_assigned_expr(left_orig);
1096 if (is_simple_math(prev)) {
1097 left = prev;
1098 right = right_orig;
1099 move_known_values(&left, &right);
1100 handle_comparison(type, left, expr->op, right);
1103 prev = get_assigned_expr(right_orig);
1104 if (is_simple_math(prev)) {
1105 left = left_orig;
1106 right = prev;
1107 move_known_values(&left, &right);
1108 handle_comparison(type, left, expr->op, right);
1114 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1115 void __extra_match_condition(struct expression *expr)
1117 struct symbol *sym;
1118 char *name;
1119 struct smatch_state *pre_state;
1120 struct smatch_state *true_state;
1121 struct smatch_state *false_state;
1123 expr = strip_expr(expr);
1124 switch (expr->type) {
1125 case EXPR_CALL:
1126 function_comparison(SPECIAL_NOTEQUAL, expr, ll_to_sval(0), 1);
1127 return;
1128 case EXPR_PREOP:
1129 case EXPR_SYMBOL:
1130 case EXPR_DEREF: {
1131 sval_t zero;
1133 zero = sval_blank(expr);
1134 zero.value = 0;
1136 name = expr_to_var_sym(expr, &sym);
1137 if (!name)
1138 return;
1139 pre_state = get_state(my_id, name, sym);
1140 true_state = estate_filter_sval(pre_state, zero);
1141 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1142 false_state = alloc_estate_sval(zero);
1143 else
1144 false_state = alloc_estate_empty();
1145 set_extra_true_false(name, sym, true_state, false_state);
1146 free_string(name);
1147 return;
1149 case EXPR_COMPARE:
1150 match_comparison(expr);
1151 return;
1152 case EXPR_ASSIGNMENT:
1153 __extra_match_condition(expr->left);
1154 return;
1158 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1159 int implied_not_equal(struct expression *expr, long long val)
1161 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1164 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct smatch_state *state)
1166 if (estate_is_whole(state))
1167 return;
1168 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, state->name);
1171 static void db_limited_before(void)
1173 unmatched_slist = clone_slist(__get_cur_slist());
1176 static void db_limited_after(void)
1178 free_slist(&unmatched_slist);
1181 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, int mod)
1183 struct expression *arg;
1184 char *name;
1185 struct symbol *sym;
1186 struct sm_state *sm;
1187 struct symbol *type;
1188 struct range_list *rl;
1189 struct range_list *limit;
1190 struct range_list *new;
1192 while (expr->type == EXPR_ASSIGNMENT)
1193 expr = strip_expr(expr->right);
1194 if (expr->type != EXPR_CALL)
1195 return;
1197 arg = get_argument_from_call_expr(expr->args, param);
1198 if (!arg)
1199 return;
1201 name = get_variable_from_key(arg, key, &sym);
1202 if (!name || !sym)
1203 goto free;
1205 sm = get_sm_state(SMATCH_EXTRA, name, sym);
1206 type = get_member_type_from_key(arg, key);
1207 if (sm)
1208 rl = estate_rl(sm->state);
1209 else
1210 rl = alloc_whole_rl(type);
1212 call_results_to_rl(expr, type, value, &limit);
1213 new = rl_intersection(rl, limit);
1215 /* We want to preserve the implications here */
1216 if (sm && rl_equiv(estate_rl(sm->state), new)) {
1217 __set_sm(sm);
1218 } else {
1219 if (mod)
1220 set_extra_mod(name, sym, alloc_estate_rl(new));
1221 else
1222 set_extra_nomod(name, sym, alloc_estate_rl(new));
1225 free:
1226 free_string(name);
1229 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
1231 db_param_limit_filter(expr, param, key, value, 0);
1234 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
1236 db_param_limit_filter(expr, param, key, value, 1);
1239 static void db_param_add(struct expression *expr, int param, char *key, char *value)
1241 struct expression *arg;
1242 char *name;
1243 struct symbol *sym;
1244 struct symbol *type;
1245 struct smatch_state *state;
1246 struct range_list *added = NULL;
1247 struct range_list *new;
1249 while (expr->type == EXPR_ASSIGNMENT)
1250 expr = strip_expr(expr->right);
1251 if (expr->type != EXPR_CALL)
1252 return;
1254 arg = get_argument_from_call_expr(expr->args, param);
1255 if (!arg)
1256 return;
1257 name = get_variable_from_key(arg, key, &sym);
1258 if (!name || !sym)
1259 goto free;
1261 type = get_member_type_from_key(arg, key);
1262 state = get_state(SMATCH_EXTRA, name, sym);
1263 if (state) {
1264 call_results_to_rl(expr, type, value, &added);
1265 new = rl_union(estate_rl(state), added);
1266 } else {
1267 new = alloc_whole_rl(type);
1270 set_extra_mod(name, sym, alloc_estate_rl(new));
1271 free:
1272 free_string(name);
1275 static void db_returned_member_info(struct expression *expr, int param, char *key, char *value)
1277 struct symbol *type;
1278 struct symbol *sym;
1279 char *name;
1280 char member_name[256];
1281 struct range_list *rl;
1283 if (expr->type != EXPR_ASSIGNMENT)
1284 return;
1286 name = expr_to_var_sym(expr->left, &sym);
1287 if (!name || !sym)
1288 goto free;
1289 snprintf(member_name, sizeof(member_name), "%s%s", name, key + 2);
1290 type = get_member_type_from_key(expr->left, key);
1291 if (!type)
1292 return;
1293 call_results_to_rl(expr->right, type, value, &rl);
1294 set_extra_mod(member_name, sym, alloc_estate_rl(rl));
1296 free:
1297 free_string(name);
1300 static void returned_member_callback(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state)
1302 if (estate_is_whole(state))
1303 return;
1305 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1306 printed_name, state->name);
1309 static void match_call_info(struct expression *expr)
1311 struct range_list *rl = NULL;
1312 struct expression *arg;
1313 struct symbol *type;
1314 int i = 0;
1316 FOR_EACH_PTR(expr->args, arg) {
1317 type = get_arg_type(expr->fn, i);
1319 if (get_implied_rl(arg, &rl))
1320 rl = cast_rl(type, rl);
1321 else
1322 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
1324 sql_insert_caller_info(expr, PARAM_VALUE, i, "$$", show_rl(rl));
1325 i++;
1326 } END_FOR_EACH_PTR(arg);
1329 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1331 struct range_list *rl = NULL;
1332 struct smatch_state *state;
1333 struct symbol *type;
1334 char fullname[256];
1336 if (strcmp(key, "*$$") == 0)
1337 snprintf(fullname, sizeof(fullname), "*%s", name);
1338 else if (strncmp(key, "$$", 2) == 0)
1339 snprintf(fullname, 256, "%s%s", name, key + 2);
1340 else
1341 return;
1343 type = get_member_type_from_key(symbol_expression(sym), key);
1344 str_to_rl(type, value, &rl);
1345 state = alloc_estate_rl(rl);
1346 set_state(SMATCH_EXTRA, fullname, sym, state);
1349 void register_smatch_extra(int id)
1351 my_id = id;
1353 add_merge_hook(my_id, &merge_estates);
1354 add_unmatched_state_hook(my_id, &unmatched_state);
1355 add_hook(&match_function_def, FUNC_DEF_HOOK);
1356 add_hook(&match_declarations, DECLARATION_HOOK);
1357 add_definition_db_callback(set_param_value, PARAM_VALUE);
1358 add_db_return_states_callback(RETURN_VALUE, &db_returned_member_info);
1359 add_db_return_states_before(&db_limited_before);
1360 add_db_return_states_callback(LIMITED_VALUE, &db_param_limit);
1361 add_db_return_states_callback(FILTER_VALUE, &db_param_filter);
1362 add_db_return_states_callback(ADDED_VALUE, &db_param_add);
1363 add_db_return_states_after(&db_limited_after);
1366 void register_smatch_extra_late(int id)
1368 add_hook(&match_dereferences, DEREF_HOOK);
1369 add_hook(&match_pointer_as_array, OP_HOOK);
1370 add_db_fn_call_callback(DEREFERENCE, &set_param_dereferenced);
1371 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1372 add_hook(&match_assign, ASSIGNMENT_HOOK);
1373 add_hook(&unop_expr, OP_HOOK);
1374 add_hook(&asm_expr, ASM_HOOK);
1376 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1377 add_member_info_callback(my_id, struct_member_callback);
1378 add_returned_member_callback(my_id, returned_member_callback);