math: remove the get_implied_value_low_overhead() function
[smatch.git] / check_spectre_second_half.c
blob158fc81da28d75c89b8b9e007a5a2d6b2008b27a
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 "smatch.h"
19 #include "smatch_extra.h"
20 #include "smatch_slist.h"
22 /* New chips will probably be able to speculate further ahead */
23 #define MAX_SPEC_STMT 200
25 static int my_id;
27 struct stree *first_halfs;
29 struct expression *recently_set;
31 void set_spectre_first_half(struct expression *expr)
33 char buf[64];
34 char *name;
36 name = expr_to_str(expr);
37 snprintf(buf, sizeof(buf), "%p %s", expr, name);
38 free_string(name);
40 set_state_stree(&first_halfs, my_id, buf, NULL, alloc_state_num(get_stmt_cnt()));
43 void clear_spectre_second_halfs(void)
45 struct sm_state *sm;
47 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
48 set_state(my_id, sm->name, sm->sym, alloc_state_num(-MAX_SPEC_STMT));
49 } END_FOR_EACH_SM(sm);
52 static struct smatch_state *get_spectre_first_half(struct expression *expr)
54 char buf[64];
55 char *name;
57 name = expr_to_str(expr);
58 snprintf(buf, sizeof(buf), "%p %s", expr, name);
59 free_string(name);
61 return get_state_stree(first_halfs, my_id, buf, NULL);
64 static void match_assign(struct expression *expr)
66 struct smatch_state *state;
68 if (expr->op == SPECIAL_AND_ASSIGN)
69 return;
71 state = get_spectre_first_half(expr->right);
72 if (state) {
73 set_state_expr(my_id, expr->left, state);
74 recently_set = expr->left;
75 return;
77 state = get_state_expr(my_id, expr->right);
78 if (!state)
79 return;
80 set_state_expr(my_id, expr->left, state);
81 recently_set = expr->left;
84 static void match_done(struct expression *expr)
86 struct smatch_state *state;
87 char *name;
89 if (expr == recently_set)
90 return;
92 state = get_state_expr(my_id, expr);
93 if (!state)
94 return;
96 if (get_stmt_cnt() - (long)state->data > MAX_SPEC_STMT)
97 return;
99 name = expr_to_str(expr);
100 sm_msg("warn: possible spectre second half. '%s'", name);
101 free_string(name);
103 set_state_expr(my_id, expr, alloc_state_num(-MAX_SPEC_STMT));
106 static void match_end_func(struct symbol *sym)
108 if (__inline_fn)
109 return;
110 free_stree(&first_halfs);
113 void check_spectre_second_half(int id)
115 my_id = id;
117 if (option_project != PROJ_KERNEL)
118 return;
119 set_dynamic_states(my_id);
120 add_hook(&match_assign, ASSIGNMENT_HOOK);
121 add_hook(&match_done, SYM_HOOK);
122 add_hook(&match_done, DEREF_HOOK);
124 add_hook(&match_end_func, END_FUNC_HOOK);