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
19 #include "smatch_extra.h"
23 static int suppress_multiple
= 1;
25 static int is_write(struct expression
*expr
)
30 static int is_read(struct expression
*expr
)
32 struct expression
*parent
, *last_parent
;
33 struct statement
*stmt
;
39 while ((parent
= expr_get_parent_expr(expr
))){
43 /* If we pass a value as a parameter that's a read, probably? */
44 // if (parent->type == EXPR_CALL)
47 if (parent
->type
== EXPR_ASSIGNMENT
) {
48 if (parent
->right
== expr
)
50 if (parent
->left
== expr
)
56 stmt
= expr_get_parent_stmt(last_parent
);
57 if (stmt
&& stmt
->type
== STMT_RETURN
)
63 static unsigned long long get_max_by_type(struct expression
*expr
)
66 .type
= &ullong_ctype
,
73 expr
= strip_parens(expr
);
74 type
= get_type(expr
);
75 if (type
&& sval_type_max(type
).uvalue
< max
.uvalue
)
76 max
= sval_type_max(type
);
77 if (expr
->type
== EXPR_PREOP
) {
79 } else if (expr
->type
== EXPR_BINOP
) {
80 if (expr
->op
== '%' || expr
->op
== '&')
85 expr
= get_assigned_expr(expr
);
96 static unsigned long long get_mask(struct expression
*expr
)
98 struct expression
*tmp
;
102 expr
= strip_expr(expr
);
104 tmp
= get_assigned_expr(expr
);
109 tmp
= get_assigned_expr(expr
);
112 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '&') {
113 if (get_value(expr
->right
, &mask
)) /* right is the common case */
115 if (get_value(expr
->left
, &mask
))
122 static void array_check(struct expression
*expr
)
124 struct expression_list
*conditions
;
125 struct expression
*array_expr
, *offset
;
126 unsigned long long mask
;
130 expr
= strip_expr(expr
);
134 if (is_impossible_path())
141 array_expr
= get_array_base(expr
);
142 if (suppress_multiple
&& is_ignored_expr(my_id
, array_expr
))
145 offset
= get_array_offset(expr
);
146 if (!is_user_rl(offset
))
148 if (is_nospec(offset
))
151 array_size
= get_array_size(array_expr
);
152 if (array_size
> 0 && get_max_by_type(offset
) < array_size
)
154 // binfo = get_bit_info(offset);
155 // if (array_size > 0 && binfo && binfo->possible < array_size)
158 mask
= get_mask(offset
);
159 if (mask
<= array_size
)
162 conditions
= get_conditions(offset
);
164 name
= expr_to_str(array_expr
);
165 sm_msg("warn: potential spectre issue '%s'%s",
166 name
, conditions
? " (local cap)" : "");
167 if (suppress_multiple
)
168 add_ignore_expr(my_id
, array_expr
);
172 void check_spectre(int id
)
176 suppress_multiple
= getenv("FULL_SPECTRE") == NULL
;
178 if (option_project
!= PROJ_KERNEL
)
181 add_hook(&array_check
, OP_HOOK
);