2 * Copyright (C) 2016 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 * What we're doing here is saving all the possible values for static variables.
20 * Later on we might do globals as well.
25 #include "smatch_slist.h"
26 #include "smatch_extra.h"
29 static struct stree
*vals
;
31 static int save_rl(void *_rl
, int argc
, char **argv
, char **azColName
)
33 unsigned long *rl
= _rl
;
35 *rl
= strtoul(argv
[0], NULL
, 10);
39 static struct range_list
*select_orig_rl(mtag_t tag
, int offset
)
41 struct range_list
*rl
= NULL
;
43 mem_sql(&save_rl
, &rl
, "select value from mtag_data where tag = %lld and offset = %d;",
48 static int is_kernel_param(const char *name
)
54 * I'm ignoring these because otherwise Smatch thinks that kernel
55 * parameters are always set to the default.
59 if (option_project
!= PROJ_KERNEL
)
62 snprintf(buf
, sizeof(buf
), "__param_%s.arg", name
);
64 FOR_EACH_SM(vals
, tmp
) {
65 if (strcmp(tmp
->name
, buf
) == 0)
67 } END_FOR_EACH_SM(tmp
);
72 void insert_mtag_data(mtag_t tag
, int offset
, struct range_list
*rl
)
74 rl
= clone_rl_permanent(rl
);
76 mem_sql(NULL
, NULL
, "insert into mtag_data values (%lld, %d, %d, '%lu');",
77 tag
, offset
, DATA_VALUE
, (unsigned long)rl
);
80 void update_mtag_data(struct expression
*expr
)
82 struct range_list
*orig
, *new, *rl
;
88 name
= expr_to_var(expr
);
89 if (is_kernel_param(name
)) {
95 if (!expr_to_mtag_name_offset(expr
, &tag
, &data_name
, &offset
))
98 get_absolute_rl(expr
, &rl
);
100 orig
= select_orig_rl(tag
, offset
);
101 new = rl_union(orig
, rl
);
102 insert_mtag_data(tag
, offset
, new);
105 static void match_global_assign(struct expression
*expr
)
107 struct range_list
*rl
;
113 name
= expr_to_var(expr
->left
);
114 if (is_kernel_param(name
)) {
120 if (!expr_to_mtag_name_offset(expr
->left
, &tag
, &data_name
, &offset
))
123 get_absolute_rl(expr
->right
, &rl
);
124 insert_mtag_data(tag
, offset
, rl
);
127 static int save_mtag_data(void *_unused
, int argc
, char **argv
, char **azColName
)
129 struct range_list
*rl
;
132 sm_msg("Error saving mtag data");
136 rl
= (struct range_list
*)strtoul(argv
[3], NULL
, 10);
139 sm_msg("SQL: insert into mtag_data values ('%s', '%s', '%s', '%s');",
140 argv
[0], argv
[1], argv
[2], show_rl(rl
));
146 static void match_end_file(struct symbol_list
*sym_list
)
148 mem_sql(&save_mtag_data
, NULL
, "select * from mtag_data;");
153 struct range_list
*rl
;
156 static int get_vals(void *_db_info
, int argc
, char **argv
, char **azColName
)
158 struct db_info
*db_info
= _db_info
;
159 struct range_list
*tmp
;
161 str_to_rl(db_info
->type
, argv
[0], &tmp
);
163 db_info
->rl
= rl_union(db_info
->rl
, tmp
);
170 struct db_cache_results
{
172 struct range_list
*rl
;
174 static struct db_cache_results cached_results
[8];
176 static int get_rl_from_mtag_sval(sval_t sval
, struct symbol
*type
, struct range_list
**rl
)
178 struct db_info db_info
= {};
185 for (i
= 0; i
< ARRAY_SIZE(cached_results
); i
++) {
186 if (sval
.uvalue
== cached_results
[i
].sval
.uvalue
) {
187 if (cached_results
[i
].rl
) {
188 *rl
= cached_results
[i
].rl
;
195 tag
= sval
.uvalue
& ~MTAG_OFFSET_MASK
;
196 offset
= sval
.uvalue
& MTAG_OFFSET_MASK
;
197 if (offset
== MTAG_OFFSET_MASK
) {
203 run_sql(get_vals
, &db_info
,
204 "select value from mtag_data where tag = %lld and offset = %d and type = %d;",
205 tag
, offset
, DATA_VALUE
);
206 if (!db_info
.rl
|| is_whole_rl(db_info
.rl
)) {
216 cached_results
[idx
].sval
= sval
;
217 cached_results
[idx
].rl
= db_info
.rl
;
218 idx
= (idx
+ 1) % ARRAY_SIZE(cached_results
);
223 static void clear_cache(struct symbol
*sym
)
225 memset(cached_results
, 0, sizeof(cached_results
));
228 static struct expression
*remove_dereference(struct expression
*expr
)
230 expr
= strip_expr(expr
);
232 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
233 return strip_expr(expr
->unop
);
234 return preop_expression(expr
, '&');
237 int get_mtag_rl(struct expression
*expr
, struct range_list
**rl
)
242 if (!get_mtag_sval(remove_dereference(expr
), &sval
))
245 type
= get_type(expr
);
249 return get_rl_from_mtag_sval(sval
, type
, rl
);
252 void register_mtag_data(int id
)
256 add_hook(&clear_cache
, END_FUNC_HOOK
);
261 add_hook(&match_global_assign
, GLOBAL_ASSIGNMENT_HOOK
);
262 add_hook(&match_end_file
, END_FILE_HOOK
);