type: handle strings better
[smatch.git] / smatch_extra.c
blobab06f4b2418527cd4fb2dd13ce9dfa38fa97fa24
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 static void call_extra_mod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
59 mod_hook **fn;
61 FOR_EACH_PTR(extra_mod_hooks, fn) {
62 (*fn)(name, sym, state);
63 } END_FOR_EACH_PTR(fn);
66 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
68 if (in_warn_on_macro())
69 return NULL;
70 remove_from_equiv(name, sym);
71 call_extra_mod_hooks(name, sym, state);
72 return set_state(SMATCH_EXTRA, name, sym, state);
75 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
77 struct symbol *sym;
78 char *name;
79 struct sm_state *ret = NULL;
81 expr = strip_expr(expr);
82 name = expr_to_var_sym(expr, &sym);
83 if (!name || !sym)
84 goto free;
85 ret = set_extra_mod(name, sym, state);
86 free:
87 free_string(name);
88 return ret;
91 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
93 struct relation *rel;
94 struct smatch_state *orig_state;
96 orig_state = get_state(SMATCH_EXTRA, name, sym);
98 if (!estate_related(orig_state)) {
99 set_state(SMATCH_EXTRA, name, sym, state);
100 return;
103 set_related(state, estate_related(orig_state));
104 FOR_EACH_PTR(estate_related(orig_state), rel) {
105 if (option_debug_related)
106 sm_msg("updating related %s to %s", rel->name, state->name);
107 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
108 } END_FOR_EACH_PTR(rel);
112 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
114 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
116 char *name;
117 struct symbol *sym;
119 name = expr_to_var_sym(expr, &sym);
120 if (!name || !sym)
121 goto free;
122 set_extra_nomod(name, sym, state);
123 free:
124 free_string(name);
128 static void set_extra_true_false(const char *name, struct symbol *sym,
129 struct smatch_state *true_state,
130 struct smatch_state *false_state)
132 struct relation *rel;
133 struct smatch_state *orig_state;
135 if (in_warn_on_macro())
136 return;
138 orig_state = get_state(SMATCH_EXTRA, name, sym);
140 if (!estate_related(orig_state)) {
141 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
142 return;
145 if (true_state)
146 set_related(true_state, estate_related(orig_state));
147 if (false_state)
148 set_related(false_state, estate_related(orig_state));
150 FOR_EACH_PTR(estate_related(orig_state), rel) {
151 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
152 true_state, false_state);
153 } END_FOR_EACH_PTR(rel);
156 static void set_extra_expr_true_false(struct expression *expr,
157 struct smatch_state *true_state,
158 struct smatch_state *false_state)
160 char *name;
161 struct symbol *sym;
163 expr = strip_expr(expr);
164 name = expr_to_var_sym(expr, &sym);
165 if (!name || !sym)
166 goto free;
167 set_extra_true_false(name, sym, true_state, false_state);
168 free:
169 free_string(name);
172 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
174 struct expression *iter_var;
175 struct expression *condition;
176 struct sm_state *sm;
177 struct smatch_state *estate;
178 sval_t start;
180 condition = strip_expr(loop->iterator_pre_condition);
181 if (!condition)
182 return NULL;
183 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
184 return NULL;
185 if (condition->op != SPECIAL_DECREMENT)
186 return NULL;
188 iter_var = condition->unop;
189 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
190 if (!sm)
191 return NULL;
192 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
193 return NULL;
194 start = estate_max(sm->state);
195 if (sval_cmp_val(start, 0) <= 0)
196 return NULL;
197 if (!sval_is_max(start))
198 start.value--;
200 if (condition->type == EXPR_PREOP) {
201 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
202 if (estate_has_hard_max(sm->state))
203 estate_set_hard_max(estate);
204 set_extra_expr_mod(iter_var, estate);
206 if (condition->type == EXPR_POSTOP) {
207 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
208 if (estate_has_hard_max(sm->state))
209 estate_set_hard_max(estate);
210 set_extra_expr_mod(iter_var, estate);
212 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
215 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
216 struct expression *condition)
218 struct expression *iter_var;
219 struct sm_state *sm;
220 struct smatch_state *estate;
221 sval_t start, end, dummy;
223 iter_var = iter_expr->unop;
224 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
225 if (!sm)
226 return NULL;
227 if (!estate_get_single_value(sm->state, &start))
228 return NULL;
229 if (!get_implied_max(condition->right, &end))
230 end = sval_type_max(get_type(iter_var));
232 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
233 return NULL;
235 switch (condition->op) {
236 case SPECIAL_NOTEQUAL:
237 case '<':
238 if (!sval_is_min(end))
239 end.value--;
240 break;
241 case SPECIAL_LTE:
242 break;
243 default:
244 return NULL;
246 if (sval_cmp(end, start) < 0)
247 return NULL;
248 estate = alloc_estate_range(start, end);
249 if (get_hard_max(condition->right, &dummy))
250 estate_set_hard_max(estate);
251 set_extra_expr_mod(iter_var, estate);
252 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
255 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
256 struct expression *condition)
258 struct expression *iter_var;
259 struct sm_state *sm;
260 struct smatch_state *estate;
261 sval_t start, end;
263 iter_var = iter_expr->unop;
264 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
265 if (!sm)
266 return NULL;
267 if (!estate_get_single_value(sm->state, &start))
268 return NULL;
269 if (!get_implied_min(condition->right, &end))
270 end = sval_type_min(get_type(iter_var));
271 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
272 return NULL;
274 switch (condition->op) {
275 case SPECIAL_NOTEQUAL:
276 case '>':
277 if (!sval_is_min(end) && !sval_is_max(end))
278 end.value++;
279 break;
280 case SPECIAL_GTE:
281 break;
282 default:
283 return NULL;
285 if (sval_cmp(end, start) > 0)
286 return NULL;
287 estate = alloc_estate_range(end, start);
288 estate_set_hard_max(estate);
289 set_extra_expr_mod(iter_var, estate);
290 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
293 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
295 struct expression *iter_expr;
296 struct expression *condition;
298 if (!loop->iterator_post_statement)
299 return NULL;
300 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
301 return NULL;
302 iter_expr = loop->iterator_post_statement->expression;
303 if (!loop->iterator_pre_condition)
304 return NULL;
305 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
306 return NULL;
307 condition = loop->iterator_pre_condition;
309 if (iter_expr->op == SPECIAL_INCREMENT)
310 return handle_canonical_for_inc(iter_expr, condition);
311 if (iter_expr->op == SPECIAL_DECREMENT)
312 return handle_canonical_for_dec(iter_expr, condition);
313 return NULL;
316 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct state_list **slist)
318 struct sm_state *ret;
320 __push_fake_cur_slist();
321 if (!loop->iterator_post_statement)
322 ret = handle_canonical_while_count_down(loop);
323 else
324 ret = handle_canonical_for_loops(loop);
325 *slist = __pop_fake_cur_slist();
326 return ret;
329 int __iterator_unchanged(struct sm_state *sm)
331 if (!sm)
332 return 0;
333 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
334 return 1;
335 return 0;
338 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
340 sval_t after_value;
342 /* paranoid checking. prolly not needed */
343 condition = strip_expr(condition);
344 if (!condition)
345 return;
346 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
347 return;
348 if (condition->op != SPECIAL_DECREMENT)
349 return;
350 after_value = estate_min(sm->state);
351 after_value.value--;
352 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
355 void __extra_pre_loop_hook_after(struct sm_state *sm,
356 struct statement *iterator,
357 struct expression *condition)
359 struct expression *iter_expr;
360 sval_t limit;
361 struct smatch_state *state;
363 if (!iterator) {
364 while_count_down_after(sm, condition);
365 return;
368 iter_expr = iterator->expression;
370 if (condition->type != EXPR_COMPARE)
371 return;
372 if (iter_expr->op == SPECIAL_INCREMENT) {
373 limit = sval_binop(estate_max(sm->state), '+',
374 sval_type_val(estate_type(sm->state), 1));
375 } else {
376 limit = sval_binop(estate_min(sm->state), '-',
377 sval_type_val(estate_type(sm->state), 1));
379 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
380 if (iter_expr->op == SPECIAL_INCREMENT)
381 state = alloc_estate_range(estate_min(sm->state), limit);
382 else
383 state = alloc_estate_range(limit, estate_max(sm->state));
384 } else {
385 state = alloc_estate_sval(limit);
387 if (!estate_has_hard_max(sm->state))
388 estate_clear_hard_max(state);
389 set_extra_mod(sm->name, sm->sym, state);
392 static struct state_list *unmatched_slist;
393 static struct smatch_state *unmatched_state(struct sm_state *sm)
395 struct smatch_state *state;
397 if (unmatched_slist) {
398 state = get_state_slist(unmatched_slist, SMATCH_EXTRA, sm->name, sm->sym);
399 if (state)
400 return state;
402 return alloc_estate_whole(estate_type(sm->state));
405 static void clear_the_pointed_at(struct expression *expr, struct state_list *slist)
407 char *name;
408 struct symbol *sym;
409 struct sm_state *tmp;
411 name = expr_to_var_sym(expr, &sym);
412 if (!name || !sym)
413 goto free;
415 FOR_EACH_PTR(slist, tmp) {
416 if (tmp->name[0] != '*')
417 continue;
418 if (tmp->sym != sym)
419 continue;
420 if (strcmp(tmp->name + 1, name) != 0)
421 continue;
422 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
423 } END_FOR_EACH_PTR(tmp);
425 free:
426 free_string(name);
429 static void match_function_call(struct expression *expr)
431 struct expression *arg;
432 struct expression *tmp;
433 struct state_list *slist;
435 slist = get_all_states(SMATCH_EXTRA);
437 FOR_EACH_PTR(expr->args, arg) {
438 tmp = strip_expr(arg);
439 if (tmp->type == EXPR_PREOP && tmp->op == '&')
440 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
441 else
442 clear_the_pointed_at(tmp, slist);
443 } END_FOR_EACH_PTR(arg);
445 free_slist(&slist);
448 static int types_equiv_or_pointer(struct symbol *one, struct symbol *two)
450 if (!one || !two)
451 return 0;
452 if (one->type == SYM_PTR && two->type == SYM_PTR)
453 return 1;
454 return types_equiv(one, two);
457 static void match_assign(struct expression *expr)
459 struct range_list *rl = NULL;
460 struct expression *left;
461 struct expression *right;
462 struct symbol *right_sym;
463 char *right_name = NULL;
464 struct symbol *sym;
465 char *name;
466 sval_t value;
467 int known;
468 sval_t right_min, right_max;
469 sval_t tmp;
471 if (is_condition(expr->right))
472 return;
473 left = strip_expr(expr->left);
474 name = expr_to_var_sym(left, &sym);
475 if (!name)
476 return;
477 right = strip_parens(expr->right);
478 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
479 right = strip_parens(right->left);
481 if (expr->op == '=' && right->type == EXPR_CALL)
482 goto free; /* these are handled in smatch_function_hooks.c */
484 right_name = expr_to_var_sym(right, &right_sym);
485 if (expr->op == '=' && right_name && right_sym &&
486 types_equiv_or_pointer(get_type(expr->left), get_type(expr->right))) {
487 set_equiv(left, right);
488 goto free;
491 right_min = sval_type_min(get_type(expr->right));
492 right_max = sval_type_max(get_type(expr->right));
494 known = get_implied_value(right, &value);
495 switch (expr->op) {
496 case '=': {
497 struct smatch_state *state;
499 if (get_implied_rl(right, &rl)) {
500 rl = cast_rl(get_type(expr->left), rl);
501 state = alloc_estate_rl(rl);
502 if (get_hard_max(right, &tmp))
503 estate_set_hard_max(state);
504 } else {
505 rl = alloc_whole_rl(get_type(right));
506 rl = cast_rl(get_type(expr->left), rl);
507 state = alloc_estate_rl(rl);
509 set_extra_mod(name, sym, state);
510 goto free;
512 case SPECIAL_ADD_ASSIGN:
513 if (get_implied_min(left, &tmp)) {
514 if (known)
515 right_min = sval_binop(tmp, '+', value);
516 else
517 right_min = tmp;
519 if (!inside_loop() && known && get_implied_max(left, &tmp))
520 right_max = sval_binop(tmp, '+', value);
521 break;
522 case SPECIAL_SUB_ASSIGN:
523 if (get_implied_max(left, &tmp)) {
524 if (known)
525 right_max = sval_binop(tmp, '-', value);
526 else
527 right_max = tmp;
529 if (!inside_loop() && known && get_implied_min(left, &tmp))
530 right_min = sval_binop(tmp, '-', value);
531 break;
533 if (!sval_is_min(right_min) || !sval_is_max(right_max))
534 rl = cast_rl(get_type(expr->left), alloc_rl(right_min, right_max));
535 else
536 rl = alloc_whole_rl(get_type(expr->left));
537 set_extra_mod(name, sym, alloc_estate_rl(rl));
538 free:
539 free_string(right_name);
540 free_string(name);
543 static void reset_struct_members(struct sm_state *sm)
545 set_extra_mod(sm->name, sm->sym, alloc_estate_whole(estate_type(sm->state)));
548 static struct smatch_state *increment_state(struct smatch_state *state)
550 sval_t min = estate_min(state);
551 sval_t max = estate_max(state);
553 if (!estate_rl(state))
554 return NULL;
556 if (inside_loop())
557 max = sval_type_max(max.type);
559 if (!sval_is_min(min) && !sval_is_max(min))
560 min.value++;
561 if (!sval_is_min(max) && !sval_is_max(max))
562 max.value++;
563 return alloc_estate_range(min, max);
566 static struct smatch_state *decrement_state(struct smatch_state *state)
568 sval_t min = estate_min(state);
569 sval_t max = estate_max(state);
571 if (!estate_rl(state))
572 return NULL;
574 if (inside_loop())
575 min = sval_type_min(min.type);
577 if (!sval_is_min(min) && !sval_is_max(min))
578 min.value--;
579 if (!sval_is_min(max) && !sval_is_max(max))
580 max.value--;
581 return alloc_estate_range(min, max);
584 static void unop_expr(struct expression *expr)
586 struct smatch_state *state;
588 if (expr->smatch_flags & Handled)
589 return;
591 switch (expr->op) {
592 case SPECIAL_INCREMENT:
593 state = get_state_expr(SMATCH_EXTRA, expr->unop);
594 state = increment_state(state);
595 if (!state)
596 state = alloc_estate_whole(get_type(expr));
597 set_extra_expr_mod(expr->unop, state);
598 break;
599 case SPECIAL_DECREMENT:
600 state = get_state_expr(SMATCH_EXTRA, expr->unop);
601 state = decrement_state(state);
602 if (!state)
603 state = alloc_estate_whole(get_type(expr));
604 set_extra_expr_mod(expr->unop, state);
605 break;
606 default:
607 return;
611 static void asm_expr(struct statement *stmt)
614 struct expression *expr;
615 int state = 0;
617 FOR_EACH_PTR(stmt->asm_outputs, expr) {
618 switch (state) {
619 case 0: /* identifier */
620 case 1: /* constraint */
621 state++;
622 continue;
623 case 2: /* expression */
624 state = 0;
625 set_extra_expr_mod(expr, alloc_estate_whole(get_type(expr)));
626 continue;
628 } END_FOR_EACH_PTR(expr);
631 static void delete_state_tracker(struct tracker *t)
633 remove_from_equiv(t->name, t->sym);
634 delete_state(t->owner, t->name, t->sym);
637 static void scoped_state_extra(const char *name, struct symbol *sym)
639 struct tracker *t;
641 t = alloc_tracker(SMATCH_EXTRA, name, sym);
642 add_scope_hook((scope_hook *)&delete_state_tracker, t);
645 static void match_declarations(struct symbol *sym)
647 const char *name;
649 if (sym->ident) {
650 name = sym->ident->name;
651 if (!sym->initializer) {
652 set_state(SMATCH_EXTRA, name, sym, alloc_estate_whole(get_real_base_type(sym)));
653 scoped_state_extra(name, sym);
658 static void check_dereference(struct expression *expr)
660 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
663 static void match_dereferences(struct expression *expr)
665 if (expr->type != EXPR_PREOP)
666 return;
667 check_dereference(expr->unop);
670 static void match_pointer_as_array(struct expression *expr)
672 if (!is_array(expr))
673 return;
674 check_dereference(expr->unop->left);
677 static void set_param_dereferenced(struct expression *arg, char *unused)
679 check_dereference(arg);
682 static void match_function_def(struct symbol *sym)
684 struct symbol *arg;
686 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
687 if (!arg->ident)
688 continue;
689 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
690 } END_FOR_EACH_PTR(arg);
693 static int match_func_comparison(struct expression *expr)
695 struct expression *left = strip_expr(expr->left);
696 struct expression *right = strip_expr(expr->right);
697 sval_t sval;
699 if (left->type == EXPR_CALL) {
700 if (!get_implied_value(right, &sval))
701 return 1;
702 function_comparison(expr->op, left, sval, 1);
703 return 1;
706 if (right->type == EXPR_CALL) {
707 if (!get_implied_value(left, &sval))
708 return 1;
709 function_comparison(expr->op, right, sval, 0);
710 return 1;
713 return 0;
716 static sval_t add_one(sval_t sval)
718 sval.value++;
719 return sval;
722 static sval_t sub_one(sval_t sval)
724 sval.value--;
725 return sval;
728 static void match_comparison(struct expression *expr)
730 struct expression *left = strip_expr(expr->left);
731 struct expression *right = strip_expr(expr->right);
732 struct symbol *type;
733 struct range_list *left_orig;
734 struct range_list *left_true;
735 struct range_list *left_false;
736 struct range_list *right_orig;
737 struct range_list *right_true;
738 struct range_list *right_false;
739 struct smatch_state *left_true_state;
740 struct smatch_state *left_false_state;
741 struct smatch_state *right_true_state;
742 struct smatch_state *right_false_state;
743 sval_t min, max, dummy;
744 int left_postop = 0;
745 int right_postop = 0;
747 if (match_func_comparison(expr))
748 return;
750 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
751 if (left->type == EXPR_POSTOP) {
752 left->smatch_flags |= Handled;
753 left_postop = left->op;
755 left = strip_expr(left->unop);
757 while (left->type == EXPR_ASSIGNMENT)
758 left = strip_expr(left->left);
760 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
761 if (right->type == EXPR_POSTOP) {
762 right->smatch_flags |= Handled;
763 right_postop = right->op;
765 right = strip_expr(right->unop);
768 type = get_type(expr);
769 if (!type)
770 type = &llong_ctype;
772 if (get_implied_rl(left, &left_orig)) {
773 left_orig = cast_rl(type, left_orig);
774 } else {
775 min = sval_type_min(get_type(left));
776 max = sval_type_max(get_type(left));
777 left_orig = cast_rl(type, alloc_rl(min, max));
780 if (get_implied_rl(right, &right_orig)) {
781 right_orig = cast_rl(type, right_orig);
782 } else {
783 min = sval_type_min(get_type(right));
784 max = sval_type_max(get_type(right));
785 right_orig = cast_rl(type, alloc_rl(min, max));
787 min = sval_type_min(type);
788 max = sval_type_max(type);
790 left_true = clone_rl(left_orig);
791 left_false = clone_rl(left_orig);
792 right_true = clone_rl(right_orig);
793 right_false = clone_rl(right_orig);
795 switch (expr->op) {
796 case '<':
797 case SPECIAL_UNSIGNED_LT:
798 left_true = remove_range(left_orig, rl_max(right_orig), max);
799 if (!sval_is_min(rl_min(right_orig))) {
800 left_false = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
803 right_true = remove_range(right_orig, min, rl_min(left_orig));
804 if (!sval_is_max(rl_max(left_orig)))
805 right_false = remove_range(right_orig, add_one(rl_max(left_orig)), max);
806 break;
807 case SPECIAL_UNSIGNED_LTE:
808 case SPECIAL_LTE:
809 if (!sval_is_max(rl_max(right_orig)))
810 left_true = remove_range(left_orig, add_one(rl_max(right_orig)), max);
811 left_false = remove_range(left_orig, min, rl_min(right_orig));
813 if (!sval_is_min(rl_min(left_orig)))
814 right_true = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
815 right_false = remove_range(right_orig, rl_max(left_orig), max);
817 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
818 left_false = remove_range(left_false, rl_min(left_orig), rl_min(left_orig));
819 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
820 right_false = remove_range(right_false, rl_max(left_orig), rl_max(left_orig));
821 break;
822 case SPECIAL_EQUAL:
823 if (!sval_is_max(rl_max(right_orig))) {
824 left_true = remove_range(left_true, add_one(rl_max(right_orig)), max);
826 if (!sval_is_min(rl_min(right_orig))) {
827 left_true = remove_range(left_true, min, sub_one(rl_min(right_orig)));
829 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
830 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
832 if (!sval_is_max(rl_max(left_orig)))
833 right_true = remove_range(right_true, add_one(rl_max(left_orig)), max);
834 if (!sval_is_min(rl_min(left_orig)))
835 right_true = remove_range(right_true, min, sub_one(rl_min(left_orig)));
836 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
837 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
838 break;
839 case SPECIAL_UNSIGNED_GTE:
840 case SPECIAL_GTE:
841 if (!sval_is_min(rl_min(right_orig)))
842 left_true = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
843 left_false = remove_range(left_orig, rl_max(right_orig), max);
845 if (!sval_is_max(rl_max(left_orig)))
846 right_true = remove_range(right_orig, add_one(rl_max(left_orig)), max);
847 right_false = remove_range(right_orig, min, rl_min(left_orig));
849 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
850 right_false = remove_range(right_false, rl_min(left_orig), rl_min(left_orig));
851 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
852 left_false = remove_range(left_false, rl_max(left_orig), rl_max(left_orig));
853 break;
854 case '>':
855 case SPECIAL_UNSIGNED_GT:
856 left_true = remove_range(left_orig, min, rl_min(right_orig));
857 if (!sval_is_max(rl_max(right_orig)))
858 left_false = remove_range(left_orig, add_one(rl_max(right_orig)), max);
860 right_true = remove_range(right_orig, rl_max(left_orig), max);
861 if (!sval_is_min(rl_min(left_orig)))
862 right_false = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
863 break;
864 case SPECIAL_NOTEQUAL:
865 if (!sval_is_max(rl_max(right_orig)))
866 left_false = remove_range(left_false, add_one(rl_max(right_orig)), max);
867 if (!sval_is_min(rl_min(right_orig)))
868 left_false = remove_range(left_false, min, sub_one(rl_min(right_orig)));
869 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
870 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
872 if (!sval_is_max(rl_max(left_orig)))
873 right_false = remove_range(right_false, add_one(rl_max(left_orig)), max);
874 if (!sval_is_min(rl_min(left_orig)))
875 right_false = remove_range(right_false, min, sub_one(rl_min(left_orig)));
876 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
877 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
878 break;
879 default:
880 return;
883 left_true_state = alloc_estate_rl(cast_rl(get_type(left), left_true));
884 left_false_state = alloc_estate_rl(cast_rl(get_type(left), left_false));
885 right_true_state = alloc_estate_rl(cast_rl(get_type(right), right_true));
886 right_false_state = alloc_estate_rl(cast_rl(get_type(right), right_false));
888 switch (expr->op) {
889 case '<':
890 case SPECIAL_UNSIGNED_LT:
891 case SPECIAL_UNSIGNED_LTE:
892 case SPECIAL_LTE:
893 if (get_hard_max(right, &dummy))
894 estate_set_hard_max(left_true_state);
895 if (get_hard_max(left, &dummy))
896 estate_set_hard_max(right_false_state);
897 break;
898 case '>':
899 case SPECIAL_UNSIGNED_GT:
900 case SPECIAL_UNSIGNED_GTE:
901 case SPECIAL_GTE:
902 if (get_hard_max(left, &dummy))
903 estate_set_hard_max(right_true_state);
904 if (get_hard_max(right, &dummy))
905 estate_set_hard_max(left_false_state);
906 break;
909 if (get_hard_max(left, &dummy)) {
910 estate_set_hard_max(left_true_state);
911 estate_set_hard_max(left_false_state);
913 if (get_hard_max(right, &dummy)) {
914 estate_set_hard_max(right_true_state);
915 estate_set_hard_max(right_false_state);
918 if (left_postop == SPECIAL_INCREMENT) {
919 left_true_state = increment_state(left_true_state);
920 left_false_state = increment_state(left_false_state);
922 if (left_postop == SPECIAL_DECREMENT) {
923 left_true_state = decrement_state(left_true_state);
924 left_false_state = decrement_state(left_false_state);
926 if (right_postop == SPECIAL_INCREMENT) {
927 right_true_state = increment_state(right_true_state);
928 right_false_state = increment_state(right_false_state);
930 if (right_postop == SPECIAL_DECREMENT) {
931 right_true_state = decrement_state(right_true_state);
932 right_false_state = decrement_state(right_false_state);
935 set_extra_expr_true_false(left, left_true_state, left_false_state);
936 set_extra_expr_true_false(right, right_true_state, right_false_state);
939 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
940 void __extra_match_condition(struct expression *expr)
942 struct symbol *sym;
943 char *name;
944 struct smatch_state *pre_state;
945 struct smatch_state *true_state;
946 struct smatch_state *false_state;
948 expr = strip_expr(expr);
949 switch (expr->type) {
950 case EXPR_CALL:
951 function_comparison(SPECIAL_NOTEQUAL, expr, ll_to_sval(0), 1);
952 return;
953 case EXPR_PREOP:
954 case EXPR_SYMBOL:
955 case EXPR_DEREF: {
956 sval_t zero;
958 zero = sval_blank(expr);
959 zero.value = 0;
961 name = expr_to_var_sym(expr, &sym);
962 if (!name)
963 return;
964 pre_state = get_state(my_id, name, sym);
965 true_state = estate_filter_sval(pre_state, zero);
966 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
967 false_state = alloc_estate_sval(zero);
968 else
969 false_state = alloc_estate_empty();
970 set_extra_true_false(name, sym, true_state, false_state);
971 free_string(name);
972 return;
974 case EXPR_COMPARE:
975 match_comparison(expr);
976 return;
977 case EXPR_ASSIGNMENT:
978 __extra_match_condition(expr->left);
979 return;
983 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
984 int implied_not_equal(struct expression *expr, long long val)
986 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
989 static void struct_member_callback(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state)
991 if (estate_is_whole(state))
992 return;
993 sm_msg("info: passes param_value '%s' %d '%s' %s %s", fn, param, printed_name, state->name, global_static);
996 static void db_limited_before(void)
998 unmatched_slist = clone_slist(__get_cur_slist());
1001 static void db_limited_after(void)
1003 free_slist(&unmatched_slist);
1006 static char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1008 char buf[256];
1009 char *tmp;
1011 if (strcmp(key, "$$") == 0)
1012 return expr_to_var_sym(arg, sym);
1014 if (strcmp(key, "*$$") == 0) {
1015 if (arg->type == EXPR_PREOP && arg->op == '&') {
1016 arg = strip_expr(arg->unop);
1017 return expr_to_var_sym(arg, sym);
1018 } else {
1019 tmp = expr_to_var_sym(arg, sym);
1020 if (!tmp)
1021 return NULL;
1022 snprintf(buf, sizeof(buf), "*%s", tmp);
1023 free_string(tmp);
1024 return alloc_string(buf);
1028 if (arg->type == EXPR_PREOP && arg->op == '&') {
1029 arg = strip_expr(arg->unop);
1030 tmp = expr_to_var_sym(arg, sym);
1031 if (!tmp)
1032 return NULL;
1033 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
1034 return alloc_string(buf);
1037 tmp = expr_to_var_sym(arg, sym);
1038 if (!tmp)
1039 return NULL;
1040 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
1041 free_string(tmp);
1042 return alloc_string(buf);
1045 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value)
1047 struct expression *arg;
1048 char *name;
1049 struct symbol *sym;
1050 struct sm_state *sm;
1051 struct symbol *type;
1052 struct range_list *rl;
1053 struct range_list *limit;
1054 struct range_list *new;
1056 while (expr->type == EXPR_ASSIGNMENT)
1057 expr = strip_expr(expr->right);
1058 if (expr->type != EXPR_CALL)
1059 return;
1061 arg = get_argument_from_call_expr(expr->args, param);
1062 if (!arg)
1063 return;
1065 name = get_variable_from_key(arg, key, &sym);
1066 if (!name || !sym)
1067 goto free;
1069 sm = get_sm_state(SMATCH_EXTRA, name, sym);
1070 type = get_member_type_from_key(arg, key);
1071 if (sm)
1072 rl = estate_rl(sm->state);
1073 else
1074 rl = alloc_whole_rl(type);
1076 str_to_rl(type, value, &limit);
1077 new = rl_intersection(rl, limit);
1079 /* We want to preserve the implications here */
1080 if (sm && rl_equiv(estate_rl(sm->state), new))
1081 __set_sm(sm);
1082 else
1083 set_extra_nomod(name, sym, alloc_estate_rl(new));
1085 free:
1086 free_string(name);
1089 static void db_param_add(struct expression *expr, int param, char *key, char *value)
1091 struct expression *arg;
1092 char *name;
1093 struct symbol *sym;
1094 struct symbol *type;
1095 struct smatch_state *state;
1096 struct range_list *added = NULL;
1097 struct range_list *new;
1099 while (expr->type == EXPR_ASSIGNMENT)
1100 expr = strip_expr(expr->right);
1101 if (expr->type != EXPR_CALL)
1102 return;
1104 arg = get_argument_from_call_expr(expr->args, param);
1105 if (!arg)
1106 return;
1107 name = get_variable_from_key(arg, key, &sym);
1108 if (!name || !sym)
1109 goto free;
1111 type = get_member_type_from_key(arg, key);
1112 state = get_state(SMATCH_EXTRA, name, sym);
1113 if (state) {
1114 str_to_rl(type, value, &added);
1115 new = rl_union(estate_rl(state), added);
1116 } else {
1117 new = alloc_whole_rl(type);
1120 set_extra_mod(name, sym, alloc_estate_rl(new));
1121 free:
1122 free_string(name);
1125 static void db_returned_states_param(struct expression *expr, int param, char *key, char *value)
1127 struct expression *arg;
1128 struct symbol *type;
1129 struct range_list *rl;
1130 char member_name[256];
1131 char *name;
1132 struct symbol *sym;
1134 while (expr->type == EXPR_ASSIGNMENT)
1135 expr = strip_expr(expr->right);
1136 if (expr->type != EXPR_CALL)
1137 return;
1138 arg = get_argument_from_call_expr(expr->args, param);
1139 arg = strip_expr(arg);
1140 if (!arg)
1141 return;
1143 if (strcmp(key, "*$$") == 0) {
1144 struct symbol *sym;
1145 char buf[256];
1146 char *name;
1148 if (arg->type == EXPR_PREOP && arg->op == '&') {
1149 arg = strip_expr(arg->unop);
1150 name = expr_to_var_sym(arg, &sym);
1151 } else {
1152 char *tmp = expr_to_var_sym(arg, &sym);
1154 if (!tmp)
1155 return;
1156 snprintf(buf, sizeof(buf), "*%s", tmp);
1157 name = alloc_string(buf);
1158 free_string(tmp);
1160 if (!name || !sym) {
1161 free_string(name);
1162 return;
1164 type = get_type(arg);
1165 str_to_rl(type, value, &rl);
1166 set_extra_mod(name, sym, alloc_estate_rl(rl));
1167 free_string(name);
1168 return;
1171 if (arg->type == EXPR_PREOP && arg->op == '&') {
1172 arg = strip_expr(arg->unop);
1173 name = expr_to_var_sym(arg, &sym);
1174 if (!name || !sym)
1175 goto free;
1176 snprintf(member_name, sizeof(member_name), "%s.%s", name, key + 4);
1177 } else {
1178 name = expr_to_var_sym(arg, &sym);
1179 if (!name || !sym)
1180 goto free;
1181 snprintf(member_name, sizeof(member_name), "%s%s", name, key + 2);
1183 type = get_member_type_from_key(arg, key);
1184 if (!type)
1185 goto free;
1186 str_to_rl(type, value, &rl);
1187 set_extra_mod(member_name, sym, alloc_estate_rl(rl));
1188 free:
1189 free_string(name);
1192 static void db_returned_member_info(struct expression *expr, int param, char *key, char *value)
1194 struct symbol *type;
1195 struct symbol *sym;
1196 char *name;
1197 char member_name[256];
1198 struct range_list *rl;
1200 if (expr->type != EXPR_ASSIGNMENT)
1201 return;
1203 name = expr_to_var_sym(expr->left, &sym);
1204 if (!name || !sym)
1205 goto free;
1206 snprintf(member_name, sizeof(member_name), "%s%s", name, key + 2);
1207 type = get_member_type_from_key(expr->left, key);
1208 if (!type)
1209 return;
1210 str_to_rl(type, value, &rl);
1211 set_extra_mod(member_name, sym, alloc_estate_rl(rl));
1213 free:
1214 free_string(name);
1217 static void returned_member_callback(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state)
1219 if (estate_is_whole(state))
1220 return;
1221 sm_msg("info: return_value %d '%s' '%s' '%s' %s", return_id,
1222 return_ranges, printed_name, state->name, global_static());
1225 static void match_call_info(struct expression *expr)
1227 struct range_list *rl = NULL;
1228 struct expression *arg;
1229 struct symbol *type;
1230 char *name;
1231 int i = 0;
1233 name = get_fnptr_name(expr->fn);
1234 if (!name)
1235 return;
1237 FOR_EACH_PTR(expr->args, arg) {
1238 type = get_arg_type(expr->fn, i);
1240 if (get_implied_rl(arg, &rl))
1241 rl = cast_rl(type, rl);
1242 else
1243 rl = alloc_whole_rl(type);
1245 sm_msg("info: passes param_value '%s' %d '$$' %s %s",
1246 name, i, show_rl(rl),
1247 is_static(expr->fn) ? "static" : "global");
1248 i++;
1249 } END_FOR_EACH_PTR(arg);
1251 free_string(name);
1254 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1256 struct range_list *rl = NULL;
1257 struct smatch_state *state;
1258 struct symbol *type;
1259 char fullname[256];
1261 if (strncmp(key, "$$", 2))
1262 return;
1264 snprintf(fullname, 256, "%s%s", name, key + 2);
1265 type = get_member_type_from_key(symbol_expression(sym), key);
1266 str_to_rl(type, value, &rl);
1267 state = alloc_estate_rl(rl);
1268 set_state(SMATCH_EXTRA, fullname, sym, state);
1271 void register_smatch_extra(int id)
1273 my_id = id;
1275 add_merge_hook(my_id, &merge_estates);
1276 add_unmatched_state_hook(my_id, &unmatched_state);
1277 add_hook(&match_function_def, FUNC_DEF_HOOK);
1278 add_hook(&match_declarations, DECLARATION_HOOK);
1279 add_definition_db_callback(set_param_value, PARAM_VALUE);
1280 add_db_return_states_callback(RETURN_VALUE, &db_returned_member_info);
1281 add_db_return_states_callback(PARAM_VALUE, &db_returned_states_param);
1282 add_db_return_states_before(&db_limited_before);
1283 add_db_return_states_callback(LIMITED_VALUE, &db_param_limit_filter);
1284 add_db_return_states_callback(FILTER_VALUE, &db_param_limit_filter);
1285 add_db_return_states_callback(ADDED_VALUE, &db_param_add);
1286 add_db_return_states_after(&db_limited_after);
1289 void register_smatch_extra_late(int id)
1291 add_hook(&match_dereferences, DEREF_HOOK);
1292 add_hook(&match_pointer_as_array, OP_HOOK);
1293 add_db_fn_call_callback(DEREFERENCE, &set_param_dereferenced);
1294 add_indirect_modification_hook(SMATCH_EXTRA, reset_struct_members);
1295 if (option_no_db)
1296 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1297 add_hook(&match_assign, ASSIGNMENT_HOOK);
1298 add_hook(&unop_expr, OP_HOOK);
1299 add_hook(&asm_expr, ASM_HOOK);
1301 if (option_info) {
1302 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1303 add_member_info_callback(my_id, struct_member_callback);
1304 add_returned_member_callback(my_id, returned_member_callback);