Print this page
12724 update smatch to 0.6.1-rc1-il-5
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/smatch_real_absolute.c
+++ new/usr/src/tools/smatch/src/smatch_real_absolute.c
1 1 /*
2 2 * Copyright (C) 2015 Oracle.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 /*
19 19 * Say we have a line like:
20 20 * foo = bar / 8;
21 21 * Assume we don't know anything about bar. Well, now we know that foo is less
22 22 * than UINT_MAX / 8. Which might be useful, but it probably is misleading
23 23 * useless knowledge. Up to now we have ignored those but now we have said to
24 24 * store them.
25 25 *
26 26 * It also works if you have something like "foo = (int)(char)unknown_var;".
27 27 *
28 28 * I feel like this data doesn't have to be perfect, it just has to be better
↓ open down ↓ |
28 lines elided |
↑ open up ↑ |
29 29 * than nothing and that will help eliminate some false positives.
30 30 *
31 31 */
32 32
33 33 #include "smatch.h"
34 34 #include "smatch_slist.h"
35 35 #include "smatch_extra.h"
36 36
37 37 static int my_id;
38 38
39 +void set_real_absolute(struct expression *expr, struct smatch_state *state)
40 +{
41 + set_state_expr(my_id, expr, clone_estate(state));
42 +}
43 +
39 44 static void extra_mod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
40 45 {
41 46 struct smatch_state *abs;
42 47 struct range_list *rl;
43 48
44 49 abs = get_state(my_id, name, sym);
45 50 if (!abs || !estate_rl(abs))
46 51 return;
47 52 rl = rl_intersection(estate_rl(abs), estate_rl(state));
48 53 set_state(my_id, name, sym, alloc_estate_rl(clone_rl(rl)));
49 54 }
50 55
51 56 static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
52 57 {
53 58 struct smatch_state *extra;
54 59 struct range_list *rl;
55 60
56 61 extra = get_state(SMATCH_EXTRA, cur->name, cur->sym);
57 62 if (!extra || !estate_rl(extra))
58 63 return;
59 64 if (!estate_rl(cur->state)) {
60 65 set_state(my_id, cur->name, cur->sym, clone_estate(extra));
61 66 return;
62 67 }
63 68 rl = rl_intersection(estate_rl(cur->state), estate_rl(extra));
64 69 set_state(my_id, cur->name, cur->sym, alloc_estate_rl(clone_rl(rl)));
65 70 }
66 71
67 72 static struct smatch_state *empty_state(struct sm_state *sm)
68 73 {
69 74 return alloc_estate_empty();
70 75 }
71 76
72 77 static int in_iterator_pre_statement(void)
73 78 {
74 79 struct statement *stmt;
75 80
76 81 /*
77 82 * we can't use __cur_stmt because that isn't set for
78 83 * iterator_pre_statement. Kind of a mess.
79 84 *
80 85 */
81 86
82 87 stmt = last_ptr_list((struct ptr_list *)big_statement_stack);
83 88
84 89 if (!stmt || !stmt->parent)
85 90 return 0;
86 91 if (stmt->parent->type != STMT_ITERATOR)
87 92 return 0;
88 93 if (stmt->parent->iterator_pre_statement != stmt)
89 94 return 0;
90 95 return 1;
91 96 }
92 97
93 98 static void match_assign(struct expression *expr)
94 99 {
95 100 struct range_list *rl;
96 101 struct symbol *type;
97 102 sval_t sval;
98 103
99 104 if (expr->op != '=')
100 105 return;
101 106 if (is_fake_call(expr->right))
102 107 return;
103 108 if (in_iterator_pre_statement())
104 109 return;
105 110
106 111 get_real_absolute_rl(expr->right, &rl);
107 112
108 113 type = get_type(expr->left);
109 114 if (!type)
110 115 return;
111 116 if (type->type != SYM_PTR && type->type != SYM_BASETYPE &&
112 117 type->type != SYM_ENUM)
113 118 return;
114 119
115 120 rl = cast_rl(type, rl);
116 121 if (is_whole_rl(rl) && !get_state_expr(my_id, expr->left))
117 122 return;
118 123 /* These are handled by smatch_extra.c */
119 124 if (rl_to_sval(rl, &sval) && !get_state_expr(my_id, expr->left))
120 125 return;
121 126
122 127 set_state_expr(my_id, expr->left, alloc_estate_rl(clone_rl(rl)));
123 128 }
124 129
125 130 struct smatch_state *get_real_absolute_state(struct expression *expr)
126 131 {
127 132 return get_state_expr(my_id, expr);
128 133 }
129 134
130 135 struct smatch_state *get_real_absolute_state_var_sym(const char *name, struct symbol *sym)
131 136 {
132 137 return __get_state(my_id, name, sym);
133 138 }
134 139
135 140 void register_real_absolute(int id)
136 141 {
137 142 my_id = id;
138 143
139 144 set_dynamic_states(my_id);
140 145 add_pre_merge_hook(my_id, &pre_merge_hook);
141 146 add_unmatched_state_hook(my_id, &empty_state);
142 147 add_merge_hook(my_id, &merge_estates);
143 148 add_extra_mod_hook(&extra_mod_hook);
144 149
145 150 add_hook(&match_assign, ASSIGNMENT_HOOK);
146 151 }
147 152
↓ open down ↓ |
99 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX