2 * Copyright (C) 2011 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * This is trying to make a list of the variables which
20 * have capped values. Sometimes we don't know what the
21 * cap is, for example if we are comparing variables but
22 * we don't know the values of the variables. In that
23 * case we only know that our variable is capped and we
24 * sort that information here.
28 #include "smatch_slist.h"
29 #include "smatch_extra.h"
36 static void set_uncapped(struct sm_state
*sm
, struct expression
*mod_expr
)
38 set_state(my_id
, sm
->name
, sm
->sym
, &uncapped
);
41 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
43 struct smatch_state
*state
;
45 state
= __get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
46 if (state
&& !estate_is_whole(state
))
51 static int is_capped_macro(struct expression
*expr
)
55 name
= get_macro_name(expr
->pos
);
59 if (strcmp(name
, "min") == 0)
61 if (strcmp(name
, "MIN") == 0)
63 if (strcmp(name
, "min_t") == 0)
69 static bool binop_capped(struct expression
*expr
)
71 bool left_capped
, right_capped
;
74 if (expr
->op
== '&' && !get_value(expr
->right
, &sval
))
76 if (expr
->op
== SPECIAL_RIGHTSHIFT
)
78 if (expr
->op
== '%' &&
79 !get_value(expr
->right
, &sval
) && is_capped(expr
->right
))
82 left_capped
= is_capped(expr
->left
);
83 right_capped
= is_capped(expr
->right
);
85 if (left_capped
&& right_capped
)
87 if (!left_capped
&& !right_capped
)
89 if (left_capped
&& get_hard_max(expr
->right
, &sval
))
91 if (right_capped
&& get_hard_max(expr
->left
, &sval
))
97 int is_capped(struct expression
*expr
)
101 expr
= strip_expr(expr
);
102 while (expr
&& expr
->type
== EXPR_POSTOP
) {
103 expr
= strip_expr(expr
->unop
);
108 type
= get_type(expr
);
109 if (is_ptr_type(type
))
111 if (type
== &bool_ctype
)
113 if (type_bits(type
) >= 0 && type_bits(type
) <= 2)
116 if (is_capped_macro(expr
))
119 if (expr
->type
== EXPR_BINOP
)
120 return binop_capped(expr
);
122 if (get_state_expr(my_id
, expr
) == &capped
)
127 int is_capped_var_sym(const char *name
, struct symbol
*sym
)
129 if (get_state(my_id
, name
, sym
) == &capped
)
134 void set_param_capped_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
138 if (strncmp(key
, "$", 1))
140 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
141 set_state(my_id
, fullname
, sym
, &capped
);
144 static void match_condition(struct expression
*expr
)
146 struct expression
*left
, *right
;
147 struct smatch_state
*left_true
= NULL
;
148 struct smatch_state
*left_false
= NULL
;
149 struct smatch_state
*right_true
= NULL
;
150 struct smatch_state
*right_false
= NULL
;
154 if (expr
->type
!= EXPR_COMPARE
)
157 left
= strip_expr(expr
->left
);
158 right
= strip_expr(expr
->right
);
160 while (left
->type
== EXPR_ASSIGNMENT
)
161 left
= strip_expr(left
->left
);
163 /* If we're dealing with known expressions, that's for smatch_extra.c */
164 if (get_implied_value(left
, &sval
) ||
165 get_implied_value(right
, &sval
))
171 case SPECIAL_UNSIGNED_LT
:
172 case SPECIAL_UNSIGNED_LTE
:
174 right_false
= &capped
;
178 case SPECIAL_UNSIGNED_GT
:
179 case SPECIAL_UNSIGNED_GTE
:
180 left_false
= &capped
;
181 right_true
= &capped
;
185 right_true
= &capped
;
187 case SPECIAL_NOTEQUAL
:
188 left_false
= &capped
;
189 right_false
= &capped
;
196 set_true_false_states_expr(my_id
, left
, left_true
, left_false
);
197 set_true_false_states_expr(my_id
, right
, right_true
, right_false
);
200 static void match_assign(struct expression
*expr
)
204 if (expr
->op
!= '=' && !is_capped(expr
->left
))
207 type
= get_type(expr
);
208 if (is_ptr_type(type
))
210 if (type
== &bool_ctype
)
212 if (type_bits(type
) >= 0 && type_bits(type
) <= 2)
215 if (is_capped(expr
->right
)) {
216 set_state_expr(my_id
, expr
->left
, &capped
);
218 if (get_state_expr(my_id
, expr
->left
))
219 set_state_expr(my_id
, expr
->left
, &uncapped
);
223 static void match_caller_info(struct expression
*expr
)
225 struct expression
*tmp
;
230 FOR_EACH_PTR(expr
->args
, tmp
) {
232 if (get_implied_value(tmp
, &sval
))
236 sql_insert_caller_info(expr
, CAPPED_DATA
, i
, "$", "1");
237 } END_FOR_EACH_PTR(tmp
);
240 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
242 struct smatch_state
*estate
;
245 if (sm
->state
!= &capped
)
247 estate
= __get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
248 if (estate_get_single_value(estate
, &sval
))
250 sql_insert_caller_info(call
, CAPPED_DATA
, param
, printed_name
, "1");
253 static void return_info_callback(int return_id
, char *return_ranges
,
254 struct expression
*returned_expr
,
256 const char *printed_name
,
259 struct smatch_state
*orig
, *estate
;
262 if (param
< -1 || sm
->state
!= &capped
)
264 if (printed_name
[0] == '&')
267 estate
= __get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
268 if (estate_get_single_value(estate
, &sval
))
271 orig
= get_state_stree(get_start_states(), my_id
, sm
->name
, sm
->sym
);
272 if (orig
== &capped
&& !param_was_set_var_sym(sm
->name
, sm
->sym
))
275 sql_insert_return_states(return_id
, return_ranges
, CAPPED_DATA
,
276 param
, printed_name
, "");
279 static void db_return_states_capped(struct expression
*expr
, int param
, char *key
, char *value
)
284 name
= get_name_sym_from_key(expr
, param
, key
, &sym
);
288 set_state(my_id
, name
, sym
, &capped
);
293 void register_capped(int id
)
297 add_unmatched_state_hook(my_id
, &unmatched_state
);
298 select_caller_info_hook(set_param_capped_data
, CAPPED_DATA
);
299 add_hook(&match_condition
, CONDITION_HOOK
);
300 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
301 add_modification_hook(my_id
, &set_uncapped
);
303 add_hook(&match_caller_info
, FUNCTION_CALL_HOOK
);
304 add_member_info_callback(my_id
, struct_member_callback
);
306 add_return_info_callback(my_id
, return_info_callback
);
307 select_return_states_hook(CAPPED_DATA
, &db_return_states_capped
);