kernel, db: ignore calls to (struct file_operations)->open()
[smatch.git] / smatch_param_limit.c
bloba704e2981b6e6af5c9434cdac1c9c46eff2b57e1
1 /*
2 * sparse/smatch_param_limit.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This is for functions like this:
13 * int foo(int a)
14 * {
15 * if (a >= 0 && a < 10) {
16 * a = 42;
17 * return 1;
18 * }
19 * return 0;
20 * }
22 * If we pass in 5, it returns 1.
24 * It's a bit complicated because we can't just consider the final value, we
25 * have to always consider the passed in value.
29 #include "scope.h"
30 #include "smatch.h"
31 #include "smatch_extra.h"
32 #include "smatch_slist.h"
34 static int my_id;
36 STATE(original);
38 static struct state_list *start_states;
39 static struct state_list_stack *saved_stack;
41 static void save_start_states(struct statement *stmt)
43 start_states = get_all_states(SMATCH_EXTRA);
46 static void match_end_func(void)
48 free_slist(&start_states);
51 static struct smatch_state *unmatched_state(struct sm_state *sm)
53 return &original;
56 static struct smatch_state *filter_my_sm(struct sm_state *sm)
58 struct range_list *ret = NULL;
59 struct sm_state *tmp;
60 struct smatch_state *estate;
62 FOR_EACH_PTR(sm->possible, tmp) {
63 if (tmp->state == &merged)
64 continue;
65 if (tmp->state == &original) {
66 estate = get_state_slist(tmp->pool, SMATCH_EXTRA, tmp->name, tmp->sym);
67 if (!estate) {
68 // sm_msg("debug: no value found in pool %p", tmp->pool);
69 continue;
71 } else {
72 estate = tmp->state;
74 ret = rl_union(ret, estate_rl(estate));
75 } END_FOR_EACH_PTR(tmp);
77 return alloc_estate_rl(ret);
80 struct smatch_state *get_orig_estate(const char *name, struct symbol *sym)
82 struct sm_state *sm;
83 struct smatch_state *state;
85 sm = get_sm_state(my_id, name, sym);
86 if (sm)
87 return filter_my_sm(sm);
89 state = get_state(SMATCH_EXTRA, name, sym);
90 if (state)
91 return state;
92 return alloc_estate_rl(alloc_whole_rl(get_real_base_type(sym)));
95 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
97 struct state_list *extra_slist;
98 struct sm_state *tmp;
99 struct sm_state *my_sm;
100 struct smatch_state *state;
101 int param;
103 extra_slist = get_all_states(SMATCH_EXTRA);
105 FOR_EACH_PTR(extra_slist, tmp) {
106 if (!tmp->sym || !tmp->sym->ident || strcmp(tmp->name, tmp->sym->ident->name) != 0)
107 continue;
109 param = get_param_num_from_sym(tmp->sym);
110 if (param < 0)
111 continue;
113 my_sm = get_sm_state(my_id, tmp->name, tmp->sym);
114 if (!my_sm) {
115 struct smatch_state *old;
117 if (estate_is_whole(tmp->state))
118 continue;
119 old = get_state_slist(start_states, SMATCH_EXTRA, tmp->name, tmp->sym);
120 if (old && estates_equiv(old, tmp->state))
121 continue;
122 sql_insert_return_states(return_id, return_ranges,
123 LIMITED_VALUE, param, "$$",
124 tmp->state->name);
125 continue;
128 state = filter_my_sm(my_sm);
129 if (!state)
130 continue;
131 /* This represents an impossible state. I screwd up. Bail. */
132 if (!estate_rl(state))
133 continue;
134 if (estate_is_whole(state))
135 continue;
136 sql_insert_return_states(return_id, return_ranges,
137 LIMITED_VALUE, param, "$$",
138 state->name);
139 } END_FOR_EACH_PTR(tmp);
141 free_slist(&extra_slist);
144 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
146 struct smatch_state *orig_vals;
147 int param;
149 param = get_param_num_from_sym(sym);
150 if (param < 0)
151 return;
153 /* we are only saving params for now */
154 if (!sym->ident || strcmp(name, sym->ident->name) != 0)
155 return;
157 orig_vals = get_orig_estate(name, sym);
158 set_state(my_id, name, sym, orig_vals);
161 static void match_save_states(struct expression *expr)
163 push_slist(&saved_stack, start_states);
164 start_states = NULL;
167 static void match_restore_states(struct expression *expr)
169 start_states = pop_slist(&saved_stack);
172 void register_param_limit(int id)
174 my_id = id;
176 add_hook(&save_start_states, AFTER_DEF_HOOK);
177 add_extra_mod_hook(&extra_mod_hook);
178 add_unmatched_state_hook(my_id, &unmatched_state);
179 add_split_return_callback(&print_return_value_param);
180 add_hook(&match_end_func, END_FUNC_HOOK);
181 add_hook(&match_save_states, INLINE_FN_START);
182 add_hook(&match_restore_states, INLINE_FN_END);