2 * smatch/smatch_absolute.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 * This is to track the absolute max that variables can be. It's a bit like
9 * smatch_extra.c but it only tracks the absolute max and min. So for example,
10 * if you have "int x = (unsigned char)y;" then the absolute max of x is 255.
12 * I imagine this will be useful for find integer overflows.
17 #include "smatch_slist.h"
18 #include "smatch_extra.h"
22 static const char *show_range(sval_t min
, sval_t max
)
26 if (sval_cmp(min
, max
) == 0)
27 return sval_to_str(min
);
28 snprintf(buf
, sizeof(buf
), "%s-%s", sval_to_str(min
), sval_to_str(max
));
33 static struct smatch_state
*alloc_absolute(sval_t min
, sval_t max
)
35 struct smatch_state
*state
;
37 if (sval_is_min(min
) && sval_is_max(max
))
40 state
= __alloc_smatch_state(0);
41 state
->name
= alloc_string(show_range(min
, max
));
42 state
->data
= alloc_range(min
, max
);
46 static struct smatch_state
*merge_func(struct smatch_state
*s1
, struct smatch_state
*s2
)
48 struct data_range
*r1
, *r2
;
51 if (!s1
->data
|| !s2
->data
)
57 if (r1
->min
.value
== r2
->min
.value
&& r1
->max
.value
== r2
->max
.value
)
61 if (sval_cmp(r2
->min
, min
) < 0)
64 if (sval_cmp(r2
->max
, max
) > 0)
67 return alloc_absolute(min
, max
);
70 static void reset_state(struct sm_state
*sm
)
72 set_state(my_id
, sm
->name
, sm
->sym
, &undefined
);
75 static void match_assign(struct expression
*expr
)
77 struct symbol
*left_type
;
79 struct range_list
*rl
;
81 if (expr
->op
!= '=') {
82 set_state_expr(my_id
, expr
->left
, &undefined
);
86 left_type
= get_type(expr
->left
);
90 get_absolute_min(expr
->right
, &min
);
91 get_absolute_max(expr
->right
, &max
);
93 rl
= alloc_range_list(min
, max
);
94 rl
= cast_rl(left_type
, rl
);
98 set_state_expr(my_id
, expr
->left
, alloc_absolute(min
, max
));
101 static void struct_member_callback(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
)
103 struct data_range
*range
;
108 if (sval_is_min(range
->min
) && sval_is_max(range
->max
))
110 sm_msg("info: passes absolute_limits '%s' %d '%s' %s %s", fn
, param
, printed_name
, state
->name
, global_static
);
113 static void match_call_info(struct expression
*expr
)
115 struct expression
*arg
;
119 name
= get_fnptr_name(expr
->fn
);
124 FOR_EACH_PTR(expr
->args
, arg
) {
129 if (!get_absolute_min(arg
, &min
))
131 if (!get_absolute_max(arg
, &max
))
133 if (sval_is_min(min
) && sval_is_max(max
))
136 /* fixme: determine the type of the paramter */
137 sm_msg("info: passes absolute_limits '%s' %d '$$' %s %s",
138 name
, i
, show_range(min
, max
),
139 is_static(expr
->fn
) ? "static" : "global");
140 } END_FOR_EACH_PTR(arg
);
145 static void set_param_limits(const char *name
, struct symbol
*sym
, char *key
, char *value
)
147 struct range_list
*rl
= NULL
;
151 if (strncmp(key
, "$$", 2))
154 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
155 parse_value_ranges_type(get_real_base_type(sym
), value
, &rl
);
158 set_state(my_id
, fullname
, sym
, alloc_absolute(min
, max
));
161 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
)
163 struct smatch_state
*state
;
164 struct data_range
*range
;
166 if (get_implied_min(expr
, sval
))
169 state
= get_state_expr(my_id
, expr
);
170 if (!state
|| !state
->data
)
178 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
)
180 struct smatch_state
*state
;
181 struct data_range
*range
;
183 if (get_implied_max(expr
, sval
))
186 state
= get_state_expr(my_id
, expr
);
187 if (!state
|| !state
->data
)
195 void register_absolute(int id
)
199 add_merge_hook(my_id
, &merge_func
);
200 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
202 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
203 add_member_info_callback(my_id
, struct_member_callback
);
205 add_definition_db_callback(set_param_limits
, ABSOLUTE_LIMITS
);
208 void register_absolute_late(int id
)
210 add_modification_hook(my_id
, reset_state
);