db: fix a type bug
[smatch.git] / check_nospec.c
blob29f8c7526360d7e2282ddcbf5faa0bd10af6b1e0
1 /*
2 * Copyright (C) 2018 Oracle.
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
18 #include <stdlib.h>
19 #include "parse.h"
20 #include "smatch.h"
21 #include "smatch_slist.h"
22 #include "smatch_extra.h"
24 static int my_id;
26 STATE(nospec);
28 static bool in_nospec_stmt;
30 static struct smatch_state *unmatched_state(struct sm_state *sm)
32 struct range_list *rl;
34 if (__in_function_def && !get_user_rl_var_sym(sm->name, sm->sym, &rl))
35 return &nospec;
36 return &undefined;
39 bool is_nospec(struct expression *expr)
41 char *macro;
43 if (in_nospec_stmt)
44 return true;
45 if (!expr)
46 return false;
47 if (get_state_expr(my_id, expr) == &nospec)
48 return true;
49 macro = get_macro_name(expr->pos);
50 if (macro && strcmp(macro, "array_index_nospec") == 0)
51 return true;
52 return false;
55 static void nospec_assign(struct expression *expr)
57 if (is_nospec(expr->right))
58 set_state_expr(my_id, expr->left, &nospec);
61 static void set_param_nospec(const char *name, struct symbol *sym, char *key, char *value)
63 char fullname[256];
65 if (strcmp(key, "*$") == 0)
66 snprintf(fullname, sizeof(fullname), "*%s", name);
67 else if (strncmp(key, "$", 1) == 0)
68 snprintf(fullname, 256, "%s%s", name, key + 1);
69 else
70 return;
72 set_state(my_id, fullname, sym, &nospec);
75 static void match_call_info(struct expression *expr)
77 struct expression *arg;
78 int i = 0;
80 FOR_EACH_PTR(expr->args, arg) {
81 if (get_state_expr(my_id, arg) == &nospec)
82 sql_insert_caller_info(expr, NOSPEC, i, "$", "");
83 i++;
84 } END_FOR_EACH_PTR(arg);
87 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
89 sql_insert_caller_info(call, NOSPEC, param, printed_name, "");
92 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
94 struct symbol *returned_sym;
95 struct sm_state *sm;
96 const char *param_name;
97 int param;
99 returned_sym = expr_to_sym(expr);
101 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
102 param = get_param_num_from_sym(sm->sym);
103 if (param < 0) {
104 if (!returned_sym || returned_sym != sm->sym)
105 continue;
106 param = -1;
109 param_name = get_param_name(sm);
110 if (!param_name)
111 continue;
112 if (param != -1 && strcmp(param_name, "$") == 0)
113 continue;
115 sql_insert_return_states(return_id, return_ranges, NOSPEC, param, param_name, "");
116 } END_FOR_EACH_SM(sm);
118 if (is_nospec(expr))
119 sql_insert_return_states(return_id, return_ranges, NOSPEC, -1, "$", "");
122 static int is_return_statement(void)
124 if (__cur_stmt && __cur_stmt->type == STMT_RETURN)
125 return 1;
126 return 0;
129 static void db_returns_nospec(struct expression *expr, int param, char *key, char *value)
131 struct expression *call;
132 struct expression *arg;
133 char *name;
134 struct symbol *sym;
136 call = expr;
137 while (call->type == EXPR_ASSIGNMENT)
138 call = strip_expr(call->right);
139 if (call->type != EXPR_CALL)
140 return;
142 if (param == -1 && expr->type == EXPR_ASSIGNMENT) {
143 name = get_variable_from_key(expr->left, key, &sym);
144 } else if (param == -1 && is_return_statement()) {
145 in_nospec_stmt = true;
146 return;
147 } else {
148 arg = get_argument_from_call_expr(call->args, param);
149 if (!arg)
150 return;
151 name = get_variable_from_key(arg, key, &sym);
153 if (!name || !sym)
154 goto free;
156 set_state(my_id, name, sym, &nospec);
157 free:
158 free_string(name);
161 static int is_nospec_asm(struct statement *stmt)
163 char *macro;
165 if (!stmt || stmt->type != STMT_ASM)
166 return 0;
167 macro = get_macro_name(stmt->asm_string->pos);
168 if (!macro || strcmp(macro, "CALL_NOSPEC") != 0)
169 return 0;
170 return 1;
173 static void match_asm(struct statement *stmt)
175 if (is_nospec_asm(stmt))
176 in_nospec_stmt = true;
179 static void match_after_nospec_asm(struct statement *stmt)
181 in_nospec_stmt = false;
184 static void match_barrier(struct statement *stmt)
186 struct stree *stree;
187 struct sm_state *sm;
188 char *macro;
190 macro = get_macro_name(stmt->pos);
191 if (!macro)
192 return;
193 if (strcmp(macro, "rmb") != 0 &&
194 strcmp(macro, "smp_rmb") != 0 &&
195 strcmp(macro, "barrier_nospec") != 0)
196 return;
198 stree = get_user_stree();
199 FOR_EACH_SM(stree, sm) {
200 if (!is_whole_rl(estate_rl(sm->state)) ||
201 is_capped_var_sym(sm->name, sm->sym))
202 set_state(my_id, sm->name, sm->sym, &nospec);
203 } END_FOR_EACH_SM(sm);
206 void check_nospec(int id)
208 my_id = id;
210 add_hook(&nospec_assign, ASSIGNMENT_HOOK);
212 select_caller_info_hook(set_param_nospec, NOSPEC);
213 add_unmatched_state_hook(my_id, &unmatched_state);
215 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
216 add_member_info_callback(my_id, struct_member_callback);
217 add_split_return_callback(&returned_struct_members);
218 select_return_states_hook(NOSPEC, &db_returns_nospec);
220 add_hook(&match_asm, ASM_HOOK);
221 add_hook(&match_after_nospec_asm, STMT_HOOK_AFTER);
222 add_hook(&match_barrier, ASM_HOOK);