sval: pass type information to extra_undefined()
[smatch.git] / smatch_extra.c
blobb76ff23f372885fd794b249043e0d01b79636f1e
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 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
49 if (in_warn_on_macro())
50 return NULL;
51 remove_from_equiv(name, sym);
52 return set_state(SMATCH_EXTRA, name, sym, state);
55 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
57 if (in_warn_on_macro())
58 return NULL;
59 remove_from_equiv_expr(expr);
60 return set_state_expr(SMATCH_EXTRA, expr, state);
64 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
66 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
68 struct relation *rel;
69 struct smatch_state *orig_state;
71 orig_state = get_state_expr(SMATCH_EXTRA, expr);
73 if (!estate_related(orig_state)) {
74 set_state_expr(SMATCH_EXTRA, expr, state);
75 return;
78 set_related(&state, estate_related(orig_state));
79 FOR_EACH_PTR(estate_related(orig_state), rel) {
80 if (option_debug_related)
81 sm_msg("updating related %s to %s", rel->name, state->name);
82 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
83 } END_FOR_EACH_PTR(rel);
86 static void set_extra_true_false(const char *name, struct symbol *sym,
87 struct smatch_state *true_state,
88 struct smatch_state *false_state)
90 struct relation *rel;
91 struct smatch_state *orig_state;
93 if (in_warn_on_macro())
94 return;
96 orig_state = get_state(SMATCH_EXTRA, name, sym);
98 if (!estate_related(orig_state)) {
99 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
100 return;
103 if (true_state)
104 set_related(&true_state, estate_related(orig_state));
105 if (false_state)
106 set_related(&false_state, estate_related(orig_state));
108 FOR_EACH_PTR(estate_related(orig_state), rel) {
109 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
110 true_state, false_state);
111 } END_FOR_EACH_PTR(rel);
114 static void set_extra_expr_true_false(struct expression *expr,
115 struct smatch_state *true_state,
116 struct smatch_state *false_state)
118 char *name;
119 struct symbol *sym;
121 expr = strip_expr(expr);
122 name = get_variable_from_expr(expr, &sym);
123 if (!name || !sym)
124 goto free;
125 set_extra_true_false(name, sym, true_state, false_state);
126 free:
127 free_string(name);
130 struct smatch_state *filter_range(struct smatch_state *orig,
131 sval_t filter_min, sval_t filter_max)
133 struct range_list_sval *rl;
135 if (!orig)
136 orig = extra_undefined(filter_min.type);
138 rl = remove_range_sval(estate_ranges_sval(orig), filter_min, filter_max);
139 return alloc_estate_range_list_sval(rl);
142 struct smatch_state *add_filter(struct smatch_state *orig, sval_t sval)
144 return filter_range(orig, sval, sval);
147 static struct smatch_state *merge_func(struct smatch_state *s1, struct smatch_state *s2)
149 struct smatch_state *tmp;
150 struct range_list_sval *value_ranges;
151 struct related_list *rlist;
153 if (estates_equiv(s1, s2))
154 return s1;
156 value_ranges = range_list_union_sval(estate_ranges_sval(s1), estate_ranges_sval(s2));
157 tmp = alloc_estate_range_list_sval(value_ranges);
158 rlist = get_shared_relations(estate_related(s1), estate_related(s2));
159 set_related(&tmp, rlist);
161 return tmp;
164 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
166 struct expression *iter_var;
167 struct expression *condition;
168 struct sm_state *sm;
169 sval_t start;
171 condition = strip_expr(loop->iterator_pre_condition);
172 if (!condition)
173 return NULL;
174 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
175 return NULL;
176 if (condition->op != SPECIAL_DECREMENT)
177 return NULL;
179 iter_var = condition->unop;
180 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
181 if (!sm)
182 return NULL;
183 if (sval_cmp_val(estate_min_sval(sm->state), 0) < 0)
184 return NULL;
185 start = estate_max_sval(sm->state);
186 if (sval_cmp_val(start, 0) <= 0)
187 return NULL;
188 if (!sval_is_max(start))
189 start.value--;
191 if (condition->type == EXPR_PREOP)
192 set_extra_expr_mod(iter_var, alloc_estate_range_sval(ll_to_sval(1), start));
193 if (condition->type == EXPR_POSTOP)
194 set_extra_expr_mod(iter_var, alloc_estate_range_sval(ll_to_sval(0), start));
195 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
198 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
199 struct expression *condition)
201 struct expression *iter_var;
202 struct sm_state *sm;
203 sval_t start, end;
205 iter_var = iter_expr->unop;
206 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
207 if (!sm)
208 return NULL;
209 if (!estate_get_single_value_sval(sm->state, &start))
210 return NULL;
211 if (!get_implied_value_sval(condition->right, &end))
212 end = sval_type_max(get_type(iter_var));
214 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
215 return NULL;
217 switch (condition->op) {
218 case SPECIAL_NOTEQUAL:
219 case '<':
220 if (!sval_is_max(end))
221 end.value--;
222 break;
223 case SPECIAL_LTE:
224 break;
225 default:
226 return NULL;
228 if (sval_cmp(end, start) < 0)
229 return NULL;
230 set_extra_expr_mod(iter_var, alloc_estate_range_sval(start, end));
231 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
234 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
235 struct expression *condition)
237 struct expression *iter_var;
238 struct sm_state *sm;
239 sval_t start, end;
241 iter_var = iter_expr->unop;
242 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
243 if (!sm)
244 return NULL;
245 if (!estate_get_single_value_sval(sm->state, &start))
246 return NULL;
247 if (!get_implied_value_sval(condition->right, &end))
248 end = sval_type_min(get_type(iter_var));
249 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
250 return NULL;
252 switch (condition->op) {
253 case SPECIAL_NOTEQUAL:
254 case '>':
255 if (!sval_is_min(end) && !sval_is_max(end))
256 end.value++;
257 break;
258 case SPECIAL_GTE:
259 break;
260 default:
261 return NULL;
263 if (sval_cmp(end, start) > 0)
264 return NULL;
265 set_extra_expr_mod(iter_var, alloc_estate_range_sval(end, start));
266 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
269 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
271 struct expression *iter_expr;
272 struct expression *condition;
274 if (!loop->iterator_post_statement)
275 return NULL;
276 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
277 return NULL;
278 iter_expr = loop->iterator_post_statement->expression;
279 if (!loop->iterator_pre_condition)
280 return NULL;
281 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
282 return NULL;
283 condition = loop->iterator_pre_condition;
285 if (iter_expr->op == SPECIAL_INCREMENT)
286 return handle_canonical_for_inc(iter_expr, condition);
287 if (iter_expr->op == SPECIAL_DECREMENT)
288 return handle_canonical_for_dec(iter_expr, condition);
289 return NULL;
292 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct state_list **slist)
294 struct sm_state *ret;
296 __push_fake_cur_slist();
297 if (!loop->iterator_post_statement)
298 ret = handle_canonical_while_count_down(loop);
299 else
300 ret = handle_canonical_for_loops(loop);
301 *slist = __pop_fake_cur_slist();
302 return ret;
305 int __iterator_unchanged(struct sm_state *sm)
307 if (!sm)
308 return 0;
309 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
310 return 1;
311 return 0;
314 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
316 sval_t after_value;
318 /* paranoid checking. prolly not needed */
319 condition = strip_expr(condition);
320 if (!condition)
321 return;
322 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
323 return;
324 if (condition->op != SPECIAL_DECREMENT)
325 return;
326 after_value = estate_min_sval(sm->state);
327 after_value.value--;
328 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
331 void __extra_pre_loop_hook_after(struct sm_state *sm,
332 struct statement *iterator,
333 struct expression *condition)
335 struct expression *iter_expr;
336 char *name;
337 struct symbol *sym;
338 sval_t value;
339 int left = 0;
340 struct smatch_state *state;
341 sval_t min, max;
343 if (!iterator) {
344 while_count_down_after(sm, condition);
345 return;
348 iter_expr = iterator->expression;
350 if (condition->type != EXPR_COMPARE)
351 return;
352 if (!get_value_sval(condition->left, &value)) {
353 if (!get_value_sval(condition->right, &value))
354 return;
355 left = 1;
357 if (left)
358 name = get_variable_from_expr(condition->left, &sym);
359 else
360 name = get_variable_from_expr(condition->right, &sym);
361 if (!name || !sym)
362 goto free;
363 if (sym != sm->sym || strcmp(name, sm->name))
364 goto free;
365 state = get_state(my_id, name, sym);
366 min = estate_min_sval(state);
367 max = estate_max_sval(state);
368 if (iter_expr->op == SPECIAL_INCREMENT &&
369 !sval_is_min(min) && sval_is_max(max))
370 set_extra_mod(name, sym, alloc_estate_sval(min));
371 else if (sval_is_min(min) && !sval_is_max(max))
372 set_extra_mod(name, sym, alloc_estate_sval(max));
374 free:
375 free_string(name);
376 return;
379 static struct smatch_state *unmatched_state(struct sm_state *sm)
381 return extra_undefined(estate_type(sm->state));
384 static void match_function_call(struct expression *expr)
386 struct expression *arg;
387 struct expression *tmp;
389 FOR_EACH_PTR(expr->args, arg) {
390 tmp = strip_expr(arg);
391 if (tmp->type == EXPR_PREOP && tmp->op == '&') {
392 remove_from_equiv_expr(tmp->unop);
393 set_state_expr(SMATCH_EXTRA, tmp->unop, extra_undefined(get_type(tmp->unop)));
395 } END_FOR_EACH_PTR(arg);
398 static void match_assign(struct expression *expr)
400 struct range_list_sval *rl = NULL;
401 struct expression *left;
402 struct expression *right;
403 struct symbol *right_sym;
404 char *right_name;
405 struct symbol *sym;
406 char *name;
407 sval_t value;
408 int known;
409 sval_t right_min, right_max;
410 sval_t tmp;
412 if (__is_condition_assign(expr))
413 return;
414 left = strip_expr(expr->left);
415 name = get_variable_from_expr(left, &sym);
416 if (!name)
417 return;
418 right = strip_parens(expr->right);
419 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
420 right = strip_parens(right->left);
422 if (expr->op == '=' && right->type == EXPR_CALL)
423 return; /* these are handled in match_call_assign() */
425 right_name = get_variable_from_expr(right, &right_sym);
426 if (expr->op == '=' && right_name && right_sym &&
427 types_equiv(get_type(expr->left), get_type(expr->right))) {
428 set_equiv(left, right);
429 goto free;
432 right_min = sval_type_min(get_type(expr->right));
433 right_max = sval_type_max(get_type(expr->right));
435 known = get_implied_value_sval(right, &value);
436 switch (expr->op) {
437 case '=': {
438 if (get_implied_range_list_sval(right, &rl)) {
439 rl = cast_rl(rl, get_type(expr->left));
440 set_extra_mod(name, sym, alloc_estate_range_list_sval(rl));
441 goto free;
444 break;
446 case SPECIAL_ADD_ASSIGN:
447 if (get_implied_min_sval(left, &tmp)) {
448 if (known)
449 right_min = sval_binop(tmp, '+', value);
450 else
451 right_min = tmp;
453 if (!inside_loop() && known && get_implied_max_sval(left, &tmp))
454 right_max = sval_binop(tmp, '+', value);
455 break;
456 case SPECIAL_SUB_ASSIGN:
457 if (get_implied_max_sval(left, &tmp)) {
458 if (known)
459 right_max = sval_binop(tmp, '-', value);
460 else
461 right_max = tmp;
463 if (!inside_loop() && known && get_implied_min_sval(left, &tmp))
464 right_min = sval_binop(tmp, '-', value);
465 break;
467 if (!sval_is_min(right_min) || !sval_is_max(right_max))
468 rl = cast_rl(alloc_range_list_sval(right_min, right_max), get_type(expr->left));
469 else
470 rl = whole_range_list_sval(get_type(expr->left));
471 set_extra_mod(name, sym, alloc_estate_range_list_sval(rl));
472 free:
473 free_string(right_name);
474 free_string(name);
477 static void reset_struct_members(struct sm_state *sm)
479 set_extra_mod(sm->name, sm->sym, extra_undefined(estate_type(sm->state)));
482 static struct smatch_state *increment_state(struct smatch_state *state)
484 sval_t min = estate_min_sval(state);
485 sval_t max = estate_max_sval(state);
487 if (inside_loop())
488 max = sval_type_max(max.type);
490 if (!sval_is_min(min))
491 min.value++;
492 if (!sval_is_max(max))
493 max.value++;
494 return alloc_estate_range_sval(min, max);
497 static struct smatch_state *decrement_state(struct smatch_state *state)
499 sval_t min = estate_min_sval(state);
500 sval_t max = estate_max_sval(state);
502 if (inside_loop())
503 min = sval_type_min(min.type);
505 if (!sval_is_min(min))
506 min.value--;
507 if (!sval_is_max(max))
508 max.value--;
509 return alloc_estate_range_sval(min, max);
512 static void unop_expr(struct expression *expr)
514 struct smatch_state *state;
516 if (expr->smatch_flags & Handled)
517 return;
519 switch (expr->op) {
520 case SPECIAL_INCREMENT:
521 state = get_state_expr(SMATCH_EXTRA, expr->unop);
522 state = increment_state(state);
523 set_extra_expr_mod(expr->unop, state);
524 break;
525 case SPECIAL_DECREMENT:
526 state = get_state_expr(SMATCH_EXTRA, expr->unop);
527 state = decrement_state(state);
528 set_extra_expr_mod(expr->unop, state);
529 break;
530 default:
531 return;
535 static void asm_expr(struct statement *stmt)
538 struct expression *expr;
539 int state = 0;
541 FOR_EACH_PTR(stmt->asm_outputs, expr) {
542 switch (state) {
543 case 0: /* identifier */
544 case 1: /* constraint */
545 state++;
546 continue;
547 case 2: /* expression */
548 state = 0;
549 set_extra_expr_mod(expr, extra_undefined(get_type(expr)));
550 continue;
552 } END_FOR_EACH_PTR(expr);
555 static void delete_state_tracker(struct tracker *t)
557 remove_from_equiv(t->name, t->sym);
558 delete_state(t->owner, t->name, t->sym);
561 static void scoped_state_extra(const char *name, struct symbol *sym)
563 struct tracker *t;
565 t = alloc_tracker(SMATCH_EXTRA, name, sym);
566 add_scope_hook((scope_hook *)&delete_state_tracker, t);
569 static void match_declarations(struct symbol *sym)
571 const char *name;
573 if (sym->ident) {
574 name = sym->ident->name;
575 if (!sym->initializer) {
576 set_state(SMATCH_EXTRA, name, sym, extra_undefined(get_real_base_type(sym)));
577 scoped_state_extra(name, sym);
582 static void check_dereference(struct expression *expr)
584 set_extra_expr_nomod(expr, alloc_estate_range_sval(valid_ptr_min_sval, valid_ptr_max_sval));
587 static void match_dereferences(struct expression *expr)
589 if (expr->type != EXPR_PREOP)
590 return;
591 check_dereference(expr->unop);
594 static void match_pointer_as_array(struct expression *expr)
596 if (!is_array(expr))
597 return;
598 check_dereference(expr->unop->left);
601 static void set_param_dereferenced(struct expression *arg, char *unused)
603 check_dereference(arg);
606 static void match_function_def(struct symbol *sym)
608 struct symbol *arg;
610 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
611 if (!arg->ident)
612 continue;
613 set_state(my_id, arg->ident->name, arg, extra_undefined(get_real_base_type(arg)));
614 } END_FOR_EACH_PTR(arg);
617 static int match_func_comparison(struct expression *expr)
619 struct expression *left = strip_expr(expr->left);
620 struct expression *right = strip_expr(expr->right);
621 sval_t sval;
623 if (left->type == EXPR_CALL) {
624 if (!get_implied_value_sval(right, &sval))
625 return 1;
626 function_comparison(expr->op, left, sval, 1);
627 return 1;
630 if (right->type == EXPR_CALL) {
631 if (!get_implied_value_sval(left, &sval))
632 return 1;
633 function_comparison(expr->op, right, sval, 0);
634 return 1;
637 return 0;
640 static sval_t add_one(sval_t sval)
642 sval.value++;
643 return sval;
646 static sval_t sub_one(sval_t sval)
648 sval.value--;
649 return sval;
652 static void match_comparison(struct expression *expr)
654 struct expression *left = strip_expr(expr->left);
655 struct expression *right = strip_expr(expr->right);
656 struct symbol *type;
657 struct range_list_sval *left_orig;
658 struct range_list_sval *left_true;
659 struct range_list_sval *left_false;
660 struct range_list_sval *right_orig;
661 struct range_list_sval *right_true;
662 struct range_list_sval *right_false;
663 struct smatch_state *left_true_state;
664 struct smatch_state *left_false_state;
665 struct smatch_state *right_true_state;
666 struct smatch_state *right_false_state;
667 sval_t min, max;
668 int left_postop = 0;
669 int right_postop = 0;
671 if (match_func_comparison(expr))
672 return;
674 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
675 if (left->type == EXPR_POSTOP) {
676 left->smatch_flags |= Handled;
677 left_postop = left->op;
679 left = strip_expr(left->unop);
682 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
683 if (right->type == EXPR_POSTOP) {
684 right->smatch_flags |= Handled;
685 right_postop = right->op;
687 right = strip_expr(right->unop);
690 type = get_type(expr);
691 if (!type) {
692 sm_msg("debug: failed to get type for '%s'", get_variable_from_expr_complex(expr, NULL));
693 type = &int_ctype;
695 min = sval_type_min(type);
696 max = sval_type_max(type);
697 if (get_implied_range_list_sval(left, &left_orig))
698 left_orig = cast_rl(left_orig, type);
699 else
700 left_orig = alloc_range_list_sval(min, max);
702 if (get_implied_range_list_sval(right, &right_orig))
703 right_orig = cast_rl(right_orig, type);
704 else
705 right_orig = alloc_range_list_sval(min, max);
707 left_true = clone_range_list_sval(left_orig);
708 left_false = clone_range_list_sval(left_orig);
709 right_true = clone_range_list_sval(right_orig);
710 right_false = clone_range_list_sval(right_orig);
712 switch (expr->op) {
713 case '<':
714 case SPECIAL_UNSIGNED_LT:
715 if (!sval_is_max(rl_max_sval(right_orig))) {
716 left_true = remove_range_sval(left_orig, rl_max_sval(right_orig), max);
718 if (!sval_is_min(rl_min_sval(right_orig))) {
719 left_false = remove_range_sval(left_orig, min, sub_one(rl_min_sval(right_orig)));
722 if (!sval_is_min(rl_min_sval(left_orig)))
723 right_true = remove_range_sval(right_orig, min, rl_min_sval(left_orig));
724 if (!sval_is_max(rl_max_sval(left_orig)))
725 right_false = remove_range_sval(right_orig, add_one(rl_max_sval(left_orig)), max);
726 break;
727 case SPECIAL_UNSIGNED_LTE:
728 case SPECIAL_LTE:
729 if (!sval_is_max(rl_max_sval(right_orig)))
730 left_true = remove_range_sval(left_orig, add_one(rl_max_sval(right_orig)), max);
731 if (!sval_is_min(rl_min_sval(right_orig)))
732 left_false = remove_range_sval(left_orig, min, rl_min_sval(right_orig));
734 if (!sval_is_min(rl_min_sval(left_orig)))
735 right_true = remove_range_sval(right_orig, min, sub_one(rl_min_sval(left_orig)));
736 if (!sval_is_max(rl_max_sval(left_orig)))
737 right_false = remove_range_sval(right_orig, rl_max_sval(left_orig), max);
738 break;
739 case SPECIAL_EQUAL:
740 if (!sval_is_max(rl_max_sval(right_orig))) {
741 left_true = remove_range_sval(left_true, add_one(rl_max_sval(right_orig)), max);
743 if (!sval_is_min(rl_min_sval(right_orig))) {
744 left_true = remove_range_sval(left_true, min, sub_one(rl_min_sval(right_orig)));
746 if (sval_cmp(rl_min_sval(right_orig), rl_max_sval(right_orig)) == 0)
747 left_false = remove_range_sval(left_orig, rl_min_sval(right_orig), rl_min_sval(right_orig));
749 if (!sval_is_max(rl_max_sval(left_orig)))
750 right_true = remove_range_sval(right_true, add_one(rl_max_sval(left_orig)), max);
751 if (!sval_is_min(rl_min_sval(left_orig)))
752 right_true = remove_range_sval(right_true, min, sub_one(rl_min_sval(left_orig)));
753 if (sval_cmp(rl_min_sval(left_orig), rl_max_sval(left_orig)) == 0)
754 right_false = remove_range_sval(right_orig, rl_min_sval(left_orig), rl_min_sval(left_orig));
755 break;
756 case SPECIAL_UNSIGNED_GTE:
757 case SPECIAL_GTE:
758 if (!sval_is_min(rl_min_sval(right_orig)))
759 left_true = remove_range_sval(left_orig, min, sub_one(rl_min_sval(right_orig)));
760 if (!sval_is_max(rl_max_sval(right_orig)))
761 left_false = remove_range_sval(left_orig, rl_max_sval(right_orig), max);
763 if (!sval_is_max(rl_max_sval(left_orig)))
764 right_true = remove_range_sval(right_orig, add_one(rl_max_sval(left_orig)), max);
765 if (!sval_is_min(rl_min_sval(left_orig)))
766 right_false = remove_range_sval(right_orig, min, rl_min_sval(left_orig));
767 break;
768 case '>':
769 case SPECIAL_UNSIGNED_GT:
770 if (!sval_is_min(rl_min_sval(right_orig)))
771 left_true = remove_range_sval(left_orig, min, rl_min_sval(right_orig));
772 if (!sval_is_max(rl_max_sval(right_orig)))
773 left_false = remove_range_sval(left_orig, add_one(rl_max_sval(right_orig)), max);
775 if (!sval_is_max(rl_max_sval(left_orig)))
776 right_true = remove_range_sval(right_orig, rl_max_sval(left_orig), max);
777 if (!sval_is_min(rl_min_sval(left_orig)))
778 right_false = remove_range_sval(right_orig, min, sub_one(rl_min_sval(left_orig)));
779 break;
780 case SPECIAL_NOTEQUAL:
781 if (!sval_is_max(rl_max_sval(right_orig)))
782 left_false = remove_range_sval(left_false, add_one(rl_max_sval(right_orig)), max);
783 if (!sval_is_min(rl_min_sval(right_orig)))
784 left_false = remove_range_sval(left_false, min, sub_one(rl_min_sval(right_orig)));
785 if (sval_cmp(rl_min_sval(right_orig), rl_max_sval(right_orig)) == 0)
786 left_true = remove_range_sval(left_orig, rl_min_sval(right_orig), rl_min_sval(right_orig));
788 if (!sval_is_max(rl_max_sval(left_orig)))
789 right_false = remove_range_sval(right_false, add_one(rl_max_sval(left_orig)), max);
790 if (!sval_is_min(rl_min_sval(left_orig)))
791 right_false = remove_range_sval(right_false, min, sub_one(rl_min_sval(left_orig)));
792 if (sval_cmp(rl_min_sval(left_orig), rl_max_sval(left_orig)) == 0)
793 right_true = remove_range_sval(right_orig, rl_min_sval(left_orig), rl_min_sval(left_orig));
794 break;
795 default:
796 return;
799 left_true_state = alloc_estate_range_list_sval(cast_rl(left_true, get_type(left)));
800 left_false_state = alloc_estate_range_list_sval(cast_rl(left_false, get_type(left)));
801 right_true_state = alloc_estate_range_list_sval(cast_rl(right_true, get_type(right)));
802 right_false_state = alloc_estate_range_list_sval(cast_rl(right_false, get_type(right)));
804 if (left_postop == SPECIAL_INCREMENT) {
805 left_true_state = increment_state(left_true_state);
806 left_false_state = increment_state(left_false_state);
808 if (left_postop == SPECIAL_DECREMENT) {
809 left_true_state = decrement_state(left_true_state);
810 left_false_state = decrement_state(left_false_state);
812 if (right_postop == SPECIAL_INCREMENT) {
813 right_true_state = increment_state(right_true_state);
814 right_false_state = increment_state(right_false_state);
816 if (right_postop == SPECIAL_DECREMENT) {
817 right_true_state = decrement_state(right_true_state);
818 right_false_state = decrement_state(right_false_state);
821 set_extra_expr_true_false(left, left_true_state, left_false_state);
822 set_extra_expr_true_false(right, right_true_state, right_false_state);
825 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
826 void __extra_match_condition(struct expression *expr)
828 struct symbol *sym;
829 char *name;
830 struct smatch_state *pre_state;
831 struct smatch_state *true_state;
832 struct smatch_state *false_state;
834 expr = strip_expr(expr);
835 switch (expr->type) {
836 case EXPR_CALL:
837 function_comparison(SPECIAL_NOTEQUAL, expr, ll_to_sval(0), 1);
838 return;
839 case EXPR_PREOP:
840 case EXPR_SYMBOL:
841 case EXPR_DEREF:
842 name = get_variable_from_expr(expr, &sym);
843 if (!name)
844 return;
845 pre_state = get_state(my_id, name, sym);
846 true_state = add_filter(pre_state, ll_to_sval(0));
847 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
848 false_state = alloc_estate_sval(ll_to_sval(0));
849 else
850 false_state = alloc_estate_empty();
851 set_extra_true_false(name, sym, true_state, false_state);
852 free_string(name);
853 return;
854 case EXPR_COMPARE:
855 match_comparison(expr);
856 return;
857 case EXPR_ASSIGNMENT:
858 __extra_match_condition(expr->left);
859 return;
863 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
864 int implied_not_equal(struct expression *expr, long long val)
866 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
869 int get_implied_range_list_sval(struct expression *expr, struct range_list_sval **rl)
871 sval_t sval;
872 struct smatch_state *state;
873 sval_t min, max;
875 *rl = NULL;
877 expr = strip_parens(expr);
878 if (!expr)
879 return 0;
881 state = get_state_expr(my_id, expr);
882 if (state) {
883 *rl = clone_range_list_sval(estate_ranges_sval(state));
884 goto out;
887 if (expr->type == EXPR_CALL) {
888 if (get_implied_return_sval(expr, rl))
889 goto out;
890 *rl = db_return_vals(expr);
891 goto out;
894 if (get_implied_value_sval(expr, &sval)) {
895 add_range_sval(rl, sval, sval);
896 goto out;
899 if (expr->type == EXPR_BINOP && expr->op == '%') {
900 if (!get_implied_value_sval(expr->right, &sval))
901 return 0;
902 add_range_sval(rl, ll_to_sval(0), ll_to_sval(sval.value - 1));
903 goto out;
906 if (!get_implied_min_sval(expr, &min))
907 return 0;
908 if (!get_implied_max_sval(expr, &max))
909 return 0;
911 *rl = alloc_range_list_sval(min, max);
913 out:
914 if (is_whole_range_rl_sval(*rl))
915 return 0;
916 return 1;
919 int is_whole_range(struct smatch_state *state)
921 return is_whole_range_rl_sval(estate_ranges_sval(state));
924 static void struct_member_callback(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state)
926 if (is_whole_range(state))
927 return;
928 sm_msg("info: passes param_value '%s' %d '%s' %s %s", fn, param, printed_name, state->name, global_static);
931 static void match_call_info(struct expression *expr)
933 struct range_list_sval *rl = NULL;
934 struct expression *arg;
935 char *name;
936 int i = 0;
938 name = get_fnptr_name(expr->fn);
939 if (!name)
940 return;
942 FOR_EACH_PTR(expr->args, arg) {
943 if (get_implied_range_list_sval(arg, &rl) && !is_whole_range_rl_sval(rl)) {
944 sm_msg("info: passes param_value '%s' %d '$$' %s %s",
945 name, i, show_ranges_sval(rl),
946 is_static(expr->fn) ? "static" : "global");
948 i++;
949 } END_FOR_EACH_PTR(arg);
951 free_string(name);
954 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
956 struct range_list_sval *rl = NULL;
957 struct smatch_state *state;
958 char fullname[256];
960 if (strncmp(key, "$$", 2))
961 return;
963 snprintf(fullname, 256, "%s%s", name, key + 2);
964 get_value_ranges_sval(value, &rl);
965 state = alloc_estate_range_list_sval(rl);
966 set_state(SMATCH_EXTRA, fullname, sym, state);
969 static void match_call_assign(struct expression *expr)
971 if (expr->op != '=')
972 return;
974 /* if we have a db set up this gets set in smatch_function_hooks.c */
975 if (option_no_db)
976 set_extra_expr_mod(expr->left, extra_undefined(get_type(expr->left)));
979 void register_smatch_extra(int id)
981 my_id = id;
983 alloc_estate_undefined();
984 add_merge_hook(my_id, &merge_func);
985 add_unmatched_state_hook(my_id, &unmatched_state);
986 add_hook(&unop_expr, OP_HOOK);
987 add_hook(&asm_expr, ASM_HOOK);
988 add_hook(&match_function_def, FUNC_DEF_HOOK);
989 add_hook(&match_declarations, DECLARATION_HOOK);
990 if (option_info) {
991 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
992 add_member_info_callback(my_id, struct_member_callback);
994 add_definition_db_callback(set_param_value, PARAM_VALUE);
997 void register_smatch_extra_late(int id)
999 add_hook(&match_dereferences, DEREF_HOOK);
1000 add_hook(&match_pointer_as_array, OP_HOOK);
1001 add_db_fn_call_callback(DEREFERENCE, &set_param_dereferenced);
1002 add_indirect_modification_hook(SMATCH_EXTRA, reset_struct_members);
1003 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1004 add_hook(&match_assign, ASSIGNMENT_HOOK);
1005 add_hook(&match_call_assign, CALL_ASSIGNMENT_HOOK);