Print this page
new smatch
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/smatch_db.c
+++ new/usr/src/tools/smatch/src/smatch_db.c
1 1 /*
2 2 * Copyright (C) 2010 Dan Carpenter.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 #include <string.h>
19 19 #include <errno.h>
20 20 #include <unistd.h>
21 21 #include <ctype.h>
22 22 #include "smatch.h"
23 23 #include "smatch_slist.h"
24 24 #include "smatch_extra.h"
25 25
26 26 struct sqlite3 *smatch_db;
27 27 struct sqlite3 *mem_db;
28 28 struct sqlite3 *cache_db;
29 29
30 30 static int return_id;
31 31
32 32 #define SQLITE_CACHE_PAGES 1000
33 33
34 34 struct def_callback {
35 35 int hook_type;
36 36 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
37 37 };
38 38 ALLOCATOR(def_callback, "definition db hook callbacks");
39 39 DECLARE_PTR_LIST(callback_list, struct def_callback);
40 40 static struct callback_list *select_caller_info_callbacks;
41 41
42 42 struct member_info_callback {
43 43 int owner;
44 44 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
45 45 };
46 46 ALLOCATOR(member_info_callback, "caller_info callbacks");
47 47 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
48 48 static struct member_info_cb_list *member_callbacks;
49 49
50 50 struct returned_state_callback {
51 51 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
52 52 };
53 53 ALLOCATOR(returned_state_callback, "returned state callbacks");
54 54 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
55 55 static struct returned_state_cb_list *returned_state_callbacks;
56 56
57 57 struct returned_member_callback {
58 58 int owner;
59 59 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
60 60 };
61 61 ALLOCATOR(returned_member_callback, "returned member callbacks");
62 62 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
63 63 static struct returned_member_cb_list *returned_member_callbacks;
64 64
65 65 struct db_implies_callback {
66 66 int type;
67 67 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
68 68 };
69 69 ALLOCATOR(db_implies_callback, "return_implies callbacks");
70 70 DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
71 71 static struct db_implies_cb_list *return_implies_cb_list;
72 72 static struct db_implies_cb_list *call_implies_cb_list;
73 73
74 74 /* silently truncates if needed. */
75 75 char *escape_newlines(const char *str)
76 76 {
77 77 char buf[1024] = "";
78 78 bool found = false;
79 79 int i, j;
80 80
81 81 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
82 82 if (str[i] != '\r' && str[i] != '\n') {
83 83 buf[j] = str[i];
84 84 continue;
85 85 }
86 86
87 87 found = true;
88 88 buf[j++] = '\\';
89 89 if (j == sizeof(buf))
90 90 break;
91 91 buf[j] = 'n';
92 92 }
93 93
94 94 if (!found)
95 95 return alloc_sname(str);
96 96
97 97 if (j == sizeof(buf))
98 98 buf[j - 1] = '\0';
99 99 return alloc_sname(buf);
100 100 }
101 101
102 102 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
103 103 {
104 104 int i;
105 105
106 106 for (i = 0; i < argc; i++) {
107 107 if (i != 0)
108 108 printf(", ");
109 109 sm_printf("%s", argv[i]);
110 110 }
111 111 sm_printf("\n");
112 112 return 0;
113 113 }
114 114
115 115 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
116 116 {
117 117 char *err = NULL;
118 118 int rc;
119 119
120 120 if (!db)
121 121 return;
122 122
123 123 if (option_debug) {
124 124 sm_msg("%s", sql);
125 125 if (strncasecmp(sql, "select", strlen("select")) == 0)
126 126 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
127 127 }
128 128
129 129 rc = sqlite3_exec(db, sql, callback, data, &err);
130 130 if (rc != SQLITE_OK && !parse_error) {
131 131 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
132 132 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
133 133 parse_error = 1;
134 134 }
135 135 }
136 136
137 137 static int replace_count;
138 138 static char **replace_table;
139 139 static const char *replace_return_ranges(const char *return_ranges)
140 140 {
141 141 int i;
142 142
143 143 if (!get_function()) {
144 144 /* I have no idea why EXPORT_SYMBOL() is here */
145 145 return return_ranges;
146 146 }
147 147 for (i = 0; i < replace_count; i += 3) {
148 148 if (strcmp(replace_table[i + 0], get_function()) == 0) {
149 149 if (strcmp(replace_table[i + 1], return_ranges) == 0)
150 150 return replace_table[i + 2];
151 151 }
152 152 }
153 153 return return_ranges;
154 154 }
155 155
156 156
157 157 static char *use_states;
158 158 static int get_db_state_count(void)
159 159 {
160 160 struct sm_state *sm;
161 161 int count = 0;
162 162
163 163 FOR_EACH_SM(__get_cur_stree(), sm) {
164 164 if (sm->owner == USHRT_MAX)
165 165 continue;
166 166 if (use_states[sm->owner])
167 167 count++;
168 168 } END_FOR_EACH_SM(sm);
169 169 return count;
170 170 }
171 171
172 172 void db_ignore_states(int id)
173 173 {
174 174 use_states[id] = 0;
175 175 }
176 176
177 177 void sql_insert_return_states(int return_id, const char *return_ranges,
178 178 int type, int param, const char *key, const char *value)
179 179 {
180 180 if (key && strlen(key) >= 80)
181 181 return;
182 182 return_ranges = replace_return_ranges(return_ranges);
183 183 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
184 184 get_base_file(), get_function(), (unsigned long)__inline_fn,
185 185 return_id, return_ranges, fn_static(), type, param, key, value);
186 186 }
187 187
188 188 static struct string_list *common_funcs;
189 189 static int is_common_function(const char *fn)
190 190 {
191 191 char *tmp;
192 192
193 193 if (!fn)
194 194 return 0;
195 195
196 196 if (strncmp(fn, "__builtin_", 10) == 0)
197 197 return 1;
198 198
199 199 FOR_EACH_PTR(common_funcs, tmp) {
200 200 if (strcmp(tmp, fn) == 0)
201 201 return 1;
202 202 } END_FOR_EACH_PTR(tmp);
203 203
204 204 return 0;
205 205 }
206 206
207 207 static char *function_signature(void)
208 208 {
209 209 return type_to_str(get_real_base_type(cur_func_sym));
210 210 }
211 211
212 212 void sql_insert_caller_info(struct expression *call, int type,
213 213 int param, const char *key, const char *value)
214 214 {
215 215 FILE *tmp_fd = sm_outfd;
216 216 char *fn;
217 217
218 218 if (!option_info && !__inline_call)
219 219 return;
220 220
221 221 if (key && strlen(key) >= 80)
222 222 return;
223 223
224 224 fn = get_fnptr_name(call->fn);
225 225 if (!fn)
226 226 return;
227 227
228 228 if (__inline_call) {
229 229 mem_sql(NULL, NULL,
230 230 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
231 231 get_base_file(), get_function(), fn, (unsigned long)call,
232 232 is_static(call->fn), type, param, key, value);
233 233 }
234 234
235 235 if (!option_info)
236 236 return;
237 237
238 238 if (strncmp(fn, "__builtin_", 10) == 0)
239 239 return;
240 240 if (type != INTERNAL && is_common_function(fn))
241 241 return;
242 242
243 243 sm_outfd = caller_info_fd;
244 244 sm_msg("SQL_caller_info: insert into caller_info values ("
245 245 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
246 246 get_base_file(), get_function(), fn, is_static(call->fn),
247 247 type, param, key, value);
248 248 sm_outfd = tmp_fd;
249 249
250 250 free_string(fn);
251 251 }
252 252
253 253 void sql_insert_function_ptr(const char *fn, const char *struct_name)
254 254 {
255 255 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
256 256 get_base_file(), fn, struct_name);
257 257 }
258 258
259 259 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
260 260 {
261 261 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
262 262 get_base_file(), get_function(), (unsigned long)__inline_fn,
263 263 fn_static(), type, param, key, value);
264 264 }
265 265
266 266 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
267 267 {
268 268 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
269 269 get_base_file(), get_function(), (unsigned long)__inline_fn,
270 270 fn_static(), type, param, key, value);
271 271 }
272 272
273 273 void sql_insert_function_type_size(const char *member, const char *ranges)
274 274 {
275 275 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
276 276 }
277 277
278 278 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
279 279 {
280 280 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
281 281 }
282 282
283 283 void sql_insert_type_info(int type, const char *member, const char *value)
284 284 {
285 285 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
286 286 }
287 287
288 288 void sql_insert_local_values(const char *name, const char *value)
289 289 {
290 290 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
291 291 }
292 292
293 293 void sql_insert_function_type_value(const char *type, const char *value)
294 294 {
295 295 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
296 296 }
297 297
298 298 void sql_insert_function_type(int param, const char *value)
299 299 {
300 300 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
301 301 get_base_file(), get_function(), fn_static(), param, value);
302 302 }
303 303
304 304 void sql_insert_parameter_name(int param, const char *value)
305 305 {
306 306 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
307 307 get_base_file(), get_function(), fn_static(), param, value);
308 308 }
309 309
310 310 void sql_insert_data_info(struct expression *data, int type, const char *value)
311 311 {
312 312 char *data_name;
313 313
314 314 data_name = get_data_info_name(data);
315 315 if (!data_name)
316 316 return;
317 317 sql_insert(data_info, "'%s', '%s', %d, '%s'",
318 318 is_static(data) ? get_base_file() : "extern",
319 319 data_name, type, value);
320 320 }
321 321
322 322 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
323 323 {
324 324 sql_insert(data_info, "'%s', '%s', %d, '%s'",
325 325 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
326 326 var, type, value);
327 327 }
328 328
329 329 void sql_save_constraint(const char *con)
330 330 {
331 331 if (!option_info)
332 332 return;
333 333
334 334 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
335 335 }
336 336
337 337 void sql_save_constraint_required(const char *data, int op, const char *limit)
338 338 {
339 339 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
340 340 }
341 341
342 342 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
343 343 {
344 344 if (!option_info)
345 345 return;
346 346
347 347 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
348 348 "select constraints_required.data, constraints_required.op, '%s' from "
349 349 "constraints_required where bound = '%s';", new_limit, old_limit);
350 350 }
351 351
352 352 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
353 353 {
354 354 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
355 355 }
356 356
357 357 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
358 358 {
359 359 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
360 360 return;
361 361
362 362 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
363 363 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
364 364 fn->symbol->ident->name,
365 365 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
366 366 type, param, key, value);
367 367 }
368 368
369 369 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
370 370 {
371 371 sql_insert(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
372 372 tag, get_filename(), get_function(), get_lineno(), left_name, right_name);
373 373 }
374 374
375 375 void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container)
376 376 {
377 377 sql_insert(mtag_map, "%lld, %d, %lld", tag, offset, container);
378 378 }
379 379
380 380 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
381 381 {
382 382 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
383 383 }
384 384
385 385 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
386 386 {
387 387 mtag_t *saved_tag = _tag;
388 388 mtag_t new_tag;
389 389
390 390 new_tag = strtoll(argv[0], NULL, 10);
391 391
392 392 if (!*saved_tag)
393 393 *saved_tag = new_tag;
394 394 else if (*saved_tag != new_tag)
395 395 *saved_tag = -1ULL;
396 396
397 397 return 0;
398 398 }
399 399
400 400 int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container)
401 401 {
402 402 mtag_t tmp = 0;
403 403
404 404 run_sql(save_mtag, &tmp,
405 405 "select container from mtag_map where tag = %lld and offset = %d;",
406 406 tag, offset);
407 407
408 408 if (tmp == 0 || tmp == -1ULL)
409 409 return 0;
410 410 *container = tmp;
411 411 return 1;
412 412 }
413 413
414 414 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
415 415 {
416 416 mtag_t tmp = 0;
417 417
418 418 run_sql(save_mtag, &tmp,
419 419 "select tag from mtag_map where container = %lld and offset = %d;",
420 420 container, offset);
421 421
422 422 if (tmp == 0 || tmp == -1ULL)
423 423 return 0;
424 424 *tag = tmp;
425 425 return 1;
426 426 }
427 427
428 428 char *get_static_filter(struct symbol *sym)
429 429 {
430 430 static char sql_filter[1024];
431 431
432 432 /* This can only happen on buggy code. Return invalid SQL. */
433 433 if (!sym) {
434 434 sql_filter[0] = '\0';
435 435 return sql_filter;
436 436 }
437 437
438 438 if (sym->ctype.modifiers & MOD_STATIC) {
439 439 snprintf(sql_filter, sizeof(sql_filter),
440 440 "file = '%s' and function = '%s' and static = '1'",
441 441 get_base_file(), sym->ident->name);
442 442 } else {
443 443 snprintf(sql_filter, sizeof(sql_filter),
444 444 "function = '%s' and static = '0'", sym->ident->name);
445 445 }
446 446
447 447 return sql_filter;
448 448 }
449 449
450 450 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
451 451 {
452 452 int *row_count = _row_count;
453 453
454 454 *row_count = 0;
455 455 if (argc != 1)
456 456 return 0;
457 457 *row_count = atoi(argv[0]);
458 458 return 0;
459 459 }
460 460
461 461 static void mark_call_params_untracked(struct expression *call)
462 462 {
463 463 struct expression *arg;
464 464 int i = 0;
465 465
466 466 FOR_EACH_PTR(call->args, arg) {
467 467 mark_untracked(call, i++, "$", NULL);
468 468 } END_FOR_EACH_PTR(arg);
469 469 }
470 470
471 471 static void sql_select_return_states_pointer(const char *cols,
472 472 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
473 473 {
474 474 char *ptr;
475 475 int return_count = 0;
476 476
477 477 ptr = get_fnptr_name(call->fn);
478 478 if (!ptr)
479 479 return;
480 480
481 481 run_sql(get_row_count, &return_count,
482 482 "select count(*) from return_states join function_ptr "
483 483 "where return_states.function == function_ptr.function and "
484 484 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
485 485 /* The magic number 100 is just from testing on the kernel. */
486 486 if (return_count > 100) {
487 487 mark_call_params_untracked(call);
488 488 return;
489 489 }
490 490
491 491 run_sql(callback, info,
492 492 "select %s from return_states join function_ptr where "
493 493 "return_states.function == function_ptr.function and ptr = '%s' "
494 494 "and searchable = 1 "
495 495 "order by function_ptr.file, return_states.file, return_id, type;",
496 496 cols, ptr);
497 497 }
498 498
499 499 static int is_local_symbol(struct expression *expr)
500 500 {
↓ open down ↓ |
500 lines elided |
↑ open up ↑ |
501 501 if (expr->type != EXPR_SYMBOL)
502 502 return 0;
503 503 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
504 504 return 0;
505 505 return 1;
506 506 }
507 507
508 508 void sql_select_return_states(const char *cols, struct expression *call,
509 509 int (*callback)(void*, int, char**, char**), void *info)
510 510 {
511 + struct expression *fn;
511 512 int row_count = 0;
512 513
513 514 if (is_fake_call(call))
514 515 return;
515 516
516 - if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
517 + fn = strip_expr(call->fn);
518 + if (fn->type != EXPR_SYMBOL || !fn->symbol || is_local_symbol(fn)) {
517 519 sql_select_return_states_pointer(cols, call, callback, info);
518 520 return;
519 521 }
520 522
521 - if (inlinable(call->fn)) {
523 + if (inlinable(fn)) {
522 524 mem_sql(callback, info,
523 525 "select %s from return_states where call_id = '%lu' order by return_id, type;",
524 526 cols, (unsigned long)call);
525 527 return;
526 528 }
527 529
528 530 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
529 - get_static_filter(call->fn->symbol));
531 + get_static_filter(fn->symbol));
530 532 if (row_count > 3000)
531 533 return;
532 534
533 535 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
534 - cols, get_static_filter(call->fn->symbol));
536 + cols, get_static_filter(fn->symbol));
535 537 }
536 538
537 539 #define CALL_IMPLIES 0
538 540 #define RETURN_IMPLIES 1
539 541
540 542 struct implies_info {
541 543 int type;
542 544 struct db_implies_cb_list *cb_list;
543 545 struct expression *expr;
544 546 struct symbol *sym;
545 547 };
546 548
547 549 void sql_select_implies(const char *cols, struct implies_info *info,
548 550 int (*callback)(void*, int, char**, char**))
549 551 {
550 552 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
551 553 mem_sql(callback, info,
552 554 "select %s from return_implies where call_id = '%lu';",
553 555 cols, (unsigned long)info->expr);
554 556 return;
555 557 }
556 558
557 559 run_sql(callback, info, "select %s from %s_implies where %s;",
558 560 cols,
559 561 info->type == CALL_IMPLIES ? "call" : "return",
560 562 get_static_filter(info->sym));
561 563 }
562 564
563 565 struct select_caller_info_data {
564 566 struct stree *final_states;
565 567 struct timeval start_time;
566 568 int prev_func_id;
567 569 int ignore;
568 570 int results;
569 571 };
570 572
571 573 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
572 574
573 575 static void sql_select_caller_info(struct select_caller_info_data *data,
574 576 const char *cols, struct symbol *sym)
575 577 {
576 578 if (__inline_fn) {
577 579 mem_sql(caller_info_callback, data,
578 580 "select %s from caller_info where call_id = %lu;",
579 581 cols, (unsigned long)__inline_fn);
580 582 return;
581 583 }
582 584
583 585 if (sym->ident->name && is_common_function(sym->ident->name))
584 586 return;
585 587 run_sql(caller_info_callback, data,
586 588 "select %s from common_caller_info where %s order by call_id;",
587 589 cols, get_static_filter(sym));
588 590 if (data->results)
589 591 return;
590 592
591 593 run_sql(caller_info_callback, data,
592 594 "select %s from caller_info where %s order by call_id;",
593 595 cols, get_static_filter(sym));
594 596 }
595 597
596 598 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
597 599 {
598 600 struct def_callback *def_callback = __alloc_def_callback(0);
599 601
600 602 def_callback->hook_type = type;
601 603 def_callback->callback = callback;
602 604 add_ptr_list(&select_caller_info_callbacks, def_callback);
603 605 }
604 606
605 607 /*
606 608 * These call backs are used when the --info option is turned on to print struct
607 609 * member information. For example foo->bar could have a state in
608 610 * smatch_extra.c and also check_user.c.
609 611 */
610 612 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
611 613 {
612 614 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
613 615
614 616 member_callback->owner = owner;
615 617 member_callback->callback = callback;
616 618 add_ptr_list(&member_callbacks, member_callback);
617 619 }
618 620
619 621 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
620 622 {
621 623 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
622 624
623 625 callback->callback = fn;
624 626 add_ptr_list(&returned_state_callbacks, callback);
625 627 }
626 628
627 629 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state))
628 630 {
629 631 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
630 632
631 633 member_callback->owner = owner;
632 634 member_callback->callback = callback;
633 635 add_ptr_list(&returned_member_callbacks, member_callback);
634 636 }
635 637
636 638 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
637 639 {
638 640 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
639 641
640 642 cb->type = type;
641 643 cb->callback = callback;
642 644 add_ptr_list(&call_implies_cb_list, cb);
643 645 }
644 646
645 647 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
646 648 {
647 649 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
648 650
649 651 cb->type = type;
650 652 cb->callback = callback;
651 653 add_ptr_list(&return_implies_cb_list, cb);
652 654 }
653 655
654 656 struct return_info {
655 657 struct expression *static_returns_call;
656 658 struct symbol *return_type;
657 659 struct range_list *return_range_list;
658 660 };
659 661
660 662 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
661 663 {
662 664 struct return_info *ret_info = _ret_info;
663 665 struct range_list *rl;
664 666 struct expression *call_expr = ret_info->static_returns_call;
665 667
666 668 if (argc != 1)
667 669 return 0;
668 670 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
669 671 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
670 672 return 0;
671 673 }
672 674
673 675 struct range_list *db_return_vals(struct expression *expr)
674 676 {
675 677 struct return_info ret_info = {};
676 678 char buf[64];
677 679 struct sm_state *sm;
678 680
679 681 if (is_fake_call(expr))
680 682 return NULL;
681 683
682 684 snprintf(buf, sizeof(buf), "return %p", expr);
683 685 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
684 686 if (sm)
685 687 return clone_rl(estate_rl(sm->state));
686 688 ret_info.static_returns_call = expr;
687 689 ret_info.return_type = get_type(expr);
688 690 if (!ret_info.return_type)
689 691 return NULL;
690 692
691 693 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
692 694 return NULL;
693 695
694 696 ret_info.return_range_list = NULL;
695 697 if (inlinable(expr->fn)) {
696 698 mem_sql(db_return_callback, &ret_info,
697 699 "select distinct return from return_states where call_id = '%lu';",
698 700 (unsigned long)expr);
699 701 } else {
700 702 run_sql(db_return_callback, &ret_info,
701 703 "select distinct return from return_states where %s;",
702 704 get_static_filter(expr->fn->symbol));
703 705 }
704 706 return ret_info.return_range_list;
705 707 }
706 708
707 709 struct range_list *db_return_vals_from_str(const char *fn_name)
708 710 {
709 711 struct return_info ret_info;
710 712
↓ open down ↓ |
166 lines elided |
↑ open up ↑ |
711 713 ret_info.static_returns_call = NULL;
712 714 ret_info.return_type = &llong_ctype;
713 715 ret_info.return_range_list = NULL;
714 716
715 717 run_sql(db_return_callback, &ret_info,
716 718 "select distinct return from return_states where function = '%s';",
717 719 fn_name);
718 720 return ret_info.return_range_list;
719 721 }
720 722
723 +/*
724 + * This is used when we have a function that takes a function pointer as a
725 + * parameter. "frob(blah, blah, my_function);" We know that the return values
726 + * from frob() come from my_funcion() so we want to find the possible returns
727 + * of my_function(), but we don't know which arguments are passed to it.
728 + *
729 + */
730 +struct range_list *db_return_vals_no_args(struct expression *expr)
731 +{
732 + struct return_info ret_info = {};
733 +
734 + if (!expr || expr->type != EXPR_SYMBOL)
735 + return NULL;
736 +
737 + ret_info.static_returns_call = expr;
738 + ret_info.return_type = get_type(expr);
739 + ret_info.return_type = get_real_base_type(ret_info.return_type);
740 + if (!ret_info.return_type)
741 + return NULL;
742 +
743 + run_sql(db_return_callback, &ret_info,
744 + "select distinct return from return_states where %s;",
745 + get_static_filter(expr->symbol));
746 +
747 + return ret_info.return_range_list;
748 +}
749 +
721 750 static void match_call_marker(struct expression *expr)
722 751 {
723 752 struct symbol *type;
724 753
725 754 type = get_type(expr->fn);
726 755 if (type && type->type == SYM_PTR)
727 756 type = get_real_base_type(type);
728 757
729 758 /*
730 759 * we just want to record something in the database so that if we have
731 760 * two calls like: frob(4); frob(some_unkown); then on the receiving
732 761 * side we know that sometimes frob is called with unknown parameters.
733 762 */
734 763
735 764 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
736 765 }
737 766
738 767 static char *show_offset(int offset)
739 768 {
740 769 static char buf[64];
741 770
742 771 buf[0] = '\0';
743 772 if (offset != -1)
744 773 snprintf(buf, sizeof(buf), "(-%d)", offset);
745 774 return buf;
746 775 }
747 776
748 777 int is_recursive_member(const char *name)
749 778 {
750 779 char buf[256];
751 780 const char *p, *next;
752 781 int size;
753 782
754 783 p = strchr(name, '>');
755 784 if (!p)
756 785 return 0;
757 786 p++;
758 787 while (true) {
759 788 next = strchr(p, '>');
760 789 if (!next)
761 790 return 0;
762 791 next++;
763 792
764 793 size = next - p;
765 794 if (size >= sizeof(buf))
766 795 return 0;
767 796 memcpy(buf, p, size);
768 797 buf[size] = '\0';
769 798 if (strstr(next, buf))
770 799 return 1;
771 800 p = next;
772 801 }
773 802 }
774 803
775 804 static void print_struct_members(struct expression *call, struct expression *expr, int param, int offset, struct stree *stree,
776 805 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
777 806 {
778 807 struct sm_state *sm;
779 808 const char *sm_name;
780 809 char *name;
781 810 struct symbol *sym;
782 811 int len;
783 812 char printed_name[256];
784 813 int is_address = 0;
785 814 bool add_star;
786 815 struct symbol *type;
787 816
788 817 expr = strip_expr(expr);
789 818 if (!expr)
790 819 return;
791 820 type = get_type(expr);
792 821 if (type && type_bits(type) < type_bits(&ulong_ctype))
793 822 return;
794 823
795 824 if (expr->type == EXPR_PREOP && expr->op == '&') {
796 825 expr = strip_expr(expr->unop);
797 826 is_address = 1;
798 827 }
799 828
800 829 name = expr_to_var_sym(expr, &sym);
801 830 if (!name || !sym)
802 831 goto free;
803 832
804 833 len = strlen(name);
805 834 FOR_EACH_SM(stree, sm) {
806 835 if (sm->sym != sym)
807 836 continue;
808 837 sm_name = sm->name;
809 838 add_star = false;
810 839 if (sm_name[0] == '*') {
811 840 add_star = true;
812 841 sm_name++;
813 842 }
814 843 // FIXME: simplify?
815 844 if (!add_star && strcmp(name, sm_name) == 0) {
816 845 if (is_address)
817 846 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
818 847 else /* these are already handled. fixme: handle them here */
819 848 continue;
820 849 } else if (add_star && strcmp(name, sm_name) == 0) {
821 850 snprintf(printed_name, sizeof(printed_name), "%s*$%s",
822 851 is_address ? "*" : "", show_offset(offset));
823 852 } else if (strncmp(name, sm_name, len) == 0) {
824 853 if (sm_name[len] != '.' && sm_name[len] != '-')
825 854 continue;
826 855 if (is_address)
827 856 snprintf(printed_name, sizeof(printed_name),
828 857 "%s$%s->%s", add_star ? "*" : "",
829 858 show_offset(offset), sm_name + len + 1);
830 859 else
831 860 snprintf(printed_name, sizeof(printed_name),
832 861 "%s$%s%s", add_star ? "*" : "",
833 862 show_offset(offset), sm_name + len);
834 863 } else {
835 864 continue;
836 865 }
837 866 if (is_recursive_member(printed_name))
838 867 continue;
839 868 callback(call, param, printed_name, sm);
840 869 } END_FOR_EACH_SM(sm);
841 870 free:
842 871 free_string(name);
843 872 }
844 873
845 874 static int param_used_callback(void *_container, int argc, char **argv, char **azColName)
846 875 {
847 876 char **container = _container;
848 877 static char buf[256];
849 878
850 879 snprintf(buf, sizeof(buf), "%s", argv[0]);
851 880 *container = buf;
852 881 return 0;
853 882 }
854 883
855 884 static void print_container_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
856 885 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
857 886 {
858 887 struct expression *tmp;
859 888 char *container = NULL;
860 889 int offset;
861 890 int holder_offset;
862 891 char *p;
863 892
864 893 if (!call->fn || call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
865 894 return;
866 895
867 896 /*
868 897 * We can't use the in-mem DB because we have to parse the function
869 898 * first, then we know if it takes a container, then we know to pass it
870 899 * the container data.
871 900 *
872 901 */
873 902 run_sql(¶m_used_callback, &container,
874 903 "select key from return_implies where %s and type = %d and key like '%%$(%%' and parameter = %d limit 1;",
875 904 get_static_filter(call->fn->symbol), CONTAINER, param);
876 905 if (!container)
877 906 return;
878 907
879 908 p = strchr(container, '-');
880 909 if (!p)
881 910 return;
882 911 offset = atoi(p);
883 912 p = strchr(p, ')');
884 913 if (!p)
885 914 return;
886 915 p++;
887 916
888 917 tmp = get_assigned_expr(expr);
889 918 if (tmp)
890 919 expr = tmp;
891 920
892 921 if (expr->type != EXPR_PREOP || expr->op != '&')
893 922 return;
894 923 expr = strip_expr(expr->unop);
895 924 holder_offset = get_member_offset_from_deref(expr);
896 925 if (-holder_offset != offset)
897 926 return;
898 927
899 928 expr = strip_expr(expr->deref);
900 929 if (expr->type == EXPR_PREOP && expr->op == '*')
901 930 expr = strip_expr(expr->unop);
902 931
903 932 print_struct_members(call, expr, param, holder_offset, stree, callback);
904 933 }
905 934
906 935 static void match_call_info(struct expression *call)
907 936 {
908 937 struct member_info_callback *cb;
909 938 struct expression *arg;
910 939 struct stree *stree;
911 940 char *name;
912 941 int i;
913 942
914 943 name = get_fnptr_name(call->fn);
915 944 if (!name)
916 945 return;
917 946
918 947 FOR_EACH_PTR(member_callbacks, cb) {
919 948 stree = get_all_states_stree(cb->owner);
920 949 i = 0;
921 950 FOR_EACH_PTR(call->args, arg) {
922 951 print_struct_members(call, arg, i, -1, stree, cb->callback);
923 952 print_container_struct_members(call, arg, i, stree, cb->callback);
924 953 i++;
925 954 } END_FOR_EACH_PTR(arg);
926 955 free_stree(&stree);
927 956 } END_FOR_EACH_PTR(cb);
928 957
929 958 free_string(name);
930 959 }
931 960
932 961 static int get_param(int param, char **name, struct symbol **sym)
933 962 {
934 963 struct symbol *arg;
935 964 int i;
936 965
937 966 i = 0;
938 967 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
939 968 /*
940 969 * this is a temporary hack to work around a bug (I think in sparse?)
941 970 * 2.6.37-rc1:fs/reiserfs/journal.o
942 971 * If there is a function definition without parameter name found
943 972 * after a function implementation then it causes a crash.
944 973 * int foo() {}
945 974 * int bar(char *);
946 975 */
947 976 if (arg->ident->name < (char *)100)
948 977 continue;
949 978 if (i == param) {
950 979 *name = arg->ident->name;
951 980 *sym = arg;
952 981 return TRUE;
953 982 }
954 983 i++;
955 984 } END_FOR_EACH_PTR(arg);
956 985
957 986 return FALSE;
958 987 }
959 988
960 989 static int function_signature_matches(const char *sig)
961 990 {
962 991 char *my_sig;
963 992
964 993 my_sig = function_signature();
965 994 if (!sig || !my_sig)
966 995 return 1; /* default to matching */
967 996 if (strcmp(my_sig, sig) == 0)
968 997 return 1;
969 998 return 0;
970 999 }
971 1000
972 1001 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
973 1002 {
974 1003 struct select_caller_info_data *data = _data;
975 1004 int func_id;
976 1005 long type;
977 1006 long param;
978 1007 char *key;
979 1008 char *value;
980 1009 char *name = NULL;
981 1010 struct symbol *sym = NULL;
982 1011 struct def_callback *def_callback;
983 1012 struct stree *stree;
984 1013 struct timeval cur_time;
985 1014
986 1015 data->results = 1;
987 1016
988 1017 if (argc != 5)
989 1018 return 0;
990 1019
991 1020 gettimeofday(&cur_time, NULL);
992 1021 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
993 1022 return 0;
994 1023
995 1024 func_id = atoi(argv[0]);
996 1025 errno = 0;
997 1026 type = strtol(argv[1], NULL, 10);
998 1027 param = strtol(argv[2], NULL, 10);
999 1028 if (errno)
1000 1029 return 0;
1001 1030 key = argv[3];
1002 1031 value = argv[4];
1003 1032
1004 1033 if (data->prev_func_id == -1)
1005 1034 data->prev_func_id = func_id;
1006 1035 if (func_id != data->prev_func_id) {
1007 1036 stree = __pop_fake_cur_stree();
1008 1037 if (!data->ignore)
1009 1038 merge_stree(&data->final_states, stree);
1010 1039 free_stree(&stree);
1011 1040 __push_fake_cur_stree();
1012 1041 __unnullify_path();
1013 1042 data->prev_func_id = func_id;
1014 1043 data->ignore = 0;
1015 1044 }
1016 1045
1017 1046 if (data->ignore)
1018 1047 return 0;
1019 1048 if (type == INTERNAL &&
1020 1049 !function_signature_matches(value)) {
1021 1050 data->ignore = 1;
1022 1051 return 0;
1023 1052 }
1024 1053
1025 1054 if (param >= 0 && !get_param(param, &name, &sym))
1026 1055 return 0;
1027 1056
1028 1057 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1029 1058 if (def_callback->hook_type == type)
1030 1059 def_callback->callback(name, sym, key, value);
1031 1060 } END_FOR_EACH_PTR(def_callback);
1032 1061
1033 1062 return 0;
1034 1063 }
1035 1064
1036 1065 static struct string_list *ptr_names_done;
1037 1066 static struct string_list *ptr_names;
1038 1067
1039 1068 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1040 1069 {
1041 1070 insert_string(&ptr_names, alloc_string(argv[0]));
1042 1071 return 0;
1043 1072 }
1044 1073
1045 1074 static char *get_next_ptr_name(void)
1046 1075 {
1047 1076 char *ptr;
1048 1077
1049 1078 FOR_EACH_PTR(ptr_names, ptr) {
1050 1079 if (!insert_string(&ptr_names_done, ptr))
1051 1080 continue;
1052 1081 return ptr;
1053 1082 } END_FOR_EACH_PTR(ptr);
1054 1083 return NULL;
1055 1084 }
1056 1085
1057 1086 static void get_ptr_names(const char *file, const char *name)
1058 1087 {
1059 1088 char sql_filter[1024];
1060 1089 int before, after;
1061 1090
1062 1091 if (file) {
1063 1092 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1064 1093 file, name);
1065 1094 } else {
1066 1095 snprintf(sql_filter, 1024, "function = '%s';", name);
1067 1096 }
1068 1097
1069 1098 before = ptr_list_size((struct ptr_list *)ptr_names);
1070 1099
1071 1100 run_sql(get_ptr_name, NULL,
1072 1101 "select distinct ptr from function_ptr where %s",
1073 1102 sql_filter);
1074 1103
1075 1104 after = ptr_list_size((struct ptr_list *)ptr_names);
1076 1105 if (before == after)
1077 1106 return;
1078 1107
1079 1108 while ((name = get_next_ptr_name()))
1080 1109 get_ptr_names(NULL, name);
1081 1110 }
1082 1111
1083 1112 static void match_data_from_db(struct symbol *sym)
1084 1113 {
1085 1114 struct select_caller_info_data data = { .prev_func_id = -1 };
1086 1115 struct sm_state *sm;
1087 1116 struct stree *stree;
1088 1117 struct timeval end_time;
1089 1118
1090 1119 if (!sym || !sym->ident)
1091 1120 return;
1092 1121
1093 1122 gettimeofday(&data.start_time, NULL);
1094 1123
1095 1124 __push_fake_cur_stree();
1096 1125 __unnullify_path();
1097 1126
1098 1127 if (!__inline_fn) {
↓ open down ↓ |
368 lines elided |
↑ open up ↑ |
1099 1128 char *ptr;
1100 1129
1101 1130 if (sym->ctype.modifiers & MOD_STATIC)
1102 1131 get_ptr_names(get_base_file(), sym->ident->name);
1103 1132 else
1104 1133 get_ptr_names(NULL, sym->ident->name);
1105 1134
1106 1135 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1107 1136 __free_ptr_list((struct ptr_list **)&ptr_names);
1108 1137 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1109 - stree = __pop_fake_cur_stree();
1110 - free_stree(&stree);
1138 + __free_fake_cur_stree();
1111 1139 return;
1112 1140 }
1113 1141
1114 1142 sql_select_caller_info(&data,
1115 1143 "call_id, type, parameter, key, value",
1116 1144 sym);
1117 1145
1118 1146
1119 1147 stree = __pop_fake_cur_stree();
1120 1148 if (!data.ignore)
1121 1149 merge_stree(&data.final_states, stree);
1122 1150 free_stree(&stree);
1123 1151 __push_fake_cur_stree();
1124 1152 __unnullify_path();
1125 1153 data.prev_func_id = -1;
1126 1154 data.ignore = 0;
1155 + data.results = 0;
1127 1156
1128 1157 FOR_EACH_PTR(ptr_names, ptr) {
1129 1158 run_sql(caller_info_callback, &data,
1130 1159 "select call_id, type, parameter, key, value"
1131 1160 " from common_caller_info where function = '%s' order by call_id",
1132 1161 ptr);
1133 1162 } END_FOR_EACH_PTR(ptr);
1134 1163
1135 1164 if (data.results) {
1136 1165 FOR_EACH_PTR(ptr_names, ptr) {
1137 1166 free_string(ptr);
1138 1167 } END_FOR_EACH_PTR(ptr);
1139 1168 goto free_ptr_names;
1140 1169 }
1141 1170
1142 1171 FOR_EACH_PTR(ptr_names, ptr) {
1143 1172 run_sql(caller_info_callback, &data,
1144 1173 "select call_id, type, parameter, key, value"
1145 1174 " from caller_info where function = '%s' order by call_id",
1146 1175 ptr);
1147 1176 free_string(ptr);
1148 1177 } END_FOR_EACH_PTR(ptr);
1149 1178
1150 1179 free_ptr_names:
1151 1180 __free_ptr_list((struct ptr_list **)&ptr_names);
1152 1181 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1153 1182 } else {
1154 1183 sql_select_caller_info(&data,
1155 1184 "call_id, type, parameter, key, value",
1156 1185 sym);
1157 1186 }
1158 1187
1159 1188 stree = __pop_fake_cur_stree();
1160 1189 if (!data.ignore)
1161 1190 merge_stree(&data.final_states, stree);
1162 1191 free_stree(&stree);
1163 1192
1164 1193 gettimeofday(&end_time, NULL);
1165 1194 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1166 1195 FOR_EACH_SM(data.final_states, sm) {
1167 1196 __set_sm(sm);
1168 1197 } END_FOR_EACH_SM(sm);
1169 1198 }
1170 1199
1171 1200 free_stree(&data.final_states);
1172 1201 }
1173 1202
1174 1203 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1175 1204 {
1176 1205 struct implies_info *info = _info;
1177 1206 struct db_implies_callback *cb;
1178 1207 struct expression *arg = NULL;
1179 1208 int type;
1180 1209 int param;
1181 1210
1182 1211 if (argc != 5)
1183 1212 return 0;
1184 1213
1185 1214 type = atoi(argv[1]);
1186 1215 param = atoi(argv[2]);
1187 1216
1188 1217 FOR_EACH_PTR(info->cb_list, cb) {
1189 1218 if (cb->type != type)
1190 1219 continue;
1191 1220 if (param != -1) {
1192 1221 arg = get_argument_from_call_expr(info->expr->args, param);
1193 1222 if (!arg)
1194 1223 continue;
1195 1224 }
1196 1225 cb->callback(info->expr, arg, argv[3], argv[4]);
1197 1226 } END_FOR_EACH_PTR(cb);
1198 1227
1199 1228 return 0;
1200 1229 }
1201 1230
1202 1231 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1203 1232 {
1204 1233 struct implies_info *info = _info;
1205 1234 struct db_implies_callback *cb;
1206 1235 struct expression *arg;
1207 1236 struct symbol *sym;
1208 1237 char *name;
1209 1238 int type;
1210 1239 int param;
1211 1240
1212 1241 if (argc != 5)
1213 1242 return 0;
1214 1243
1215 1244 type = atoi(argv[1]);
1216 1245 param = atoi(argv[2]);
1217 1246
1218 1247 if (!get_param(param, &name, &sym))
1219 1248 return 0;
1220 1249 arg = symbol_expression(sym);
1221 1250 if (!arg)
1222 1251 return 0;
1223 1252
1224 1253 FOR_EACH_PTR(info->cb_list, cb) {
1225 1254 if (cb->type != type)
1226 1255 continue;
1227 1256 cb->callback(info->expr, arg, argv[3], argv[4]);
1228 1257 } END_FOR_EACH_PTR(cb);
1229 1258
1230 1259 return 0;
1231 1260 }
1232 1261
1233 1262 static void match_return_implies(struct expression *expr)
1234 1263 {
1235 1264 struct implies_info info = {
1236 1265 .type = RETURN_IMPLIES,
1237 1266 .cb_list = return_implies_cb_list,
1238 1267 };
1239 1268
1240 1269 if (expr->fn->type != EXPR_SYMBOL ||
1241 1270 !expr->fn->symbol)
1242 1271 return;
1243 1272 info.expr = expr;
1244 1273 info.sym = expr->fn->symbol;
1245 1274 sql_select_implies("function, type, parameter, key, value", &info,
1246 1275 return_implies_callbacks);
1247 1276 }
1248 1277
1249 1278 static void match_call_implies(struct symbol *sym)
1250 1279 {
1251 1280 struct implies_info info = {
1252 1281 .type = CALL_IMPLIES,
1253 1282 .cb_list = call_implies_cb_list,
↓ open down ↓ |
117 lines elided |
↑ open up ↑ |
1254 1283 };
1255 1284
1256 1285 if (!sym || !sym->ident)
1257 1286 return;
1258 1287
1259 1288 info.sym = sym;
1260 1289 sql_select_implies("function, type, parameter, key, value", &info,
1261 1290 call_implies_callbacks);
1262 1291 }
1263 1292
1293 +static char *get_fn_param_str(struct expression *expr)
1294 +{
1295 + struct expression *tmp;
1296 + int param;
1297 + char buf[32];
1298 +
1299 + tmp = get_assigned_expr(expr);
1300 + if (tmp)
1301 + expr = tmp;
1302 + expr = strip_expr(expr);
1303 + if (!expr || expr->type != EXPR_CALL)
1304 + return NULL;
1305 + expr = strip_expr(expr->fn);
1306 + if (!expr || expr->type != EXPR_SYMBOL)
1307 + return NULL;
1308 + param = get_param_num(expr);
1309 + if (param < 0)
1310 + return NULL;
1311 +
1312 + snprintf(buf, sizeof(buf), "[r $%d]", param);
1313 + return alloc_sname(buf);
1314 +}
1315 +
1264 1316 static char *get_return_compare_is_param(struct expression *expr)
1265 1317 {
1266 1318 char *var;
1267 1319 char buf[256];
1268 1320 int comparison;
1269 1321 int param;
1270 1322
1271 1323 param = get_param_num(expr);
1272 1324 if (param < 0)
1273 1325 return NULL;
1274 1326
1275 1327 var = expr_to_var(expr);
1276 1328 if (!var)
1277 1329 return NULL;
1278 1330 snprintf(buf, sizeof(buf), "%s orig", var);
1279 1331 comparison = get_comparison_strings(var, buf);
1280 1332 free_string(var);
1281 1333
1282 1334 if (!comparison)
1283 1335 return NULL;
1284 1336
1285 1337 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1286 1338 return alloc_sname(buf);
1287 1339 }
1288 1340
1289 1341 static char *get_return_compare_str(struct expression *expr)
1290 1342 {
1291 1343 char *compare_str;
1292 1344
1293 1345 compare_str = get_return_compare_is_param(expr);
1294 1346 if (compare_str)
1295 1347 return compare_str;
1296 1348
1297 1349 compare_str = expr_lte_to_param(expr, -1);
1298 1350 if (compare_str)
↓ open down ↓ |
25 lines elided |
↑ open up ↑ |
1299 1351 return compare_str;
1300 1352
1301 1353 return expr_param_comparison(expr, -1);
1302 1354 }
1303 1355
1304 1356 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1305 1357 {
1306 1358 struct range_list *rl;
1307 1359 char *return_ranges;
1308 1360 sval_t sval;
1361 + char *fn_param_str;
1309 1362 char *compare_str;
1310 1363 char *math_str;
1311 1364 char buf[128];
1312 1365
1313 1366 *rl_p = NULL;
1314 1367
1315 1368 if (!expr)
1316 1369 return alloc_sname("");
1317 1370
1318 1371 if (get_implied_value(expr, &sval)) {
1319 1372 sval = sval_cast(cur_func_return_type(), sval);
1320 1373 *rl_p = alloc_rl(sval, sval);
1321 1374 return sval_to_str_or_err_ptr(sval);
1322 1375 }
1323 1376
1377 + fn_param_str = get_fn_param_str(expr);
1324 1378 compare_str = expr_equal_to_param(expr, -1);
1325 1379 math_str = get_value_in_terms_of_parameter_math(expr);
1326 1380
1327 1381 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1328 1382 rl = cast_rl(cur_func_return_type(), rl);
1329 1383 return_ranges = show_rl(rl);
1330 1384 } else if (get_imaginary_absolute(expr, &rl)){
1331 1385 rl = cast_rl(cur_func_return_type(), rl);
1332 1386 return alloc_sname(show_rl(rl));
1333 1387 } else {
1334 1388 get_absolute_rl(expr, &rl);
1335 1389 rl = cast_rl(cur_func_return_type(), rl);
1336 1390 return_ranges = show_rl(rl);
1337 1391 }
1338 1392 *rl_p = rl;
1339 1393
1394 + if (fn_param_str) {
1395 + snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1396 + return alloc_sname(buf);
1397 + }
1340 1398 if (compare_str) {
1341 1399 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1342 1400 return alloc_sname(buf);
1343 1401 }
1344 1402 if (math_str) {
1345 1403 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1346 1404 return alloc_sname(buf);
1347 1405 }
1348 1406 compare_str = get_return_compare_str(expr);
1349 1407 if (compare_str) {
1350 1408 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1351 1409 return alloc_sname(buf);
1352 1410 }
1353 1411
1354 1412 return return_ranges;
1355 1413 }
1356 1414
1357 1415 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1358 1416 {
1359 1417 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1360 1418 }
1361 1419
1362 1420 static void call_return_state_hooks_conditional(struct expression *expr)
1363 1421 {
1364 1422 struct returned_state_callback *cb;
1365 1423 struct range_list *rl;
1366 1424 const char *return_ranges;
1367 1425 int final_pass_orig = final_pass;
1368 1426
1369 1427 __push_fake_cur_stree();
1370 1428
1371 1429 final_pass = 0;
1372 1430 __split_whole_condition(expr->conditional);
1373 1431 final_pass = final_pass_orig;
1374 1432
1375 1433 return_ranges = get_return_ranges_str(expr->cond_true ?: expr->conditional, &rl);
1376 1434
1377 1435 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1378 1436
1379 1437 return_id++;
1380 1438 FOR_EACH_PTR(returned_state_callbacks, cb) {
1381 1439 cb->callback(return_id, (char *)return_ranges, expr->cond_true);
1382 1440 } END_FOR_EACH_PTR(cb);
1383 1441
1384 1442 __push_true_states();
1385 1443 __use_false_states();
1386 1444
1387 1445 return_ranges = get_return_ranges_str(expr->cond_false, &rl);
1388 1446 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1389 1447
1390 1448 return_id++;
1391 1449 FOR_EACH_PTR(returned_state_callbacks, cb) {
1392 1450 cb->callback(return_id, (char *)return_ranges, expr->cond_false);
1393 1451 } END_FOR_EACH_PTR(cb);
1394 1452
1395 1453 __merge_true_states();
1396 1454 __free_fake_cur_stree();
1397 1455 }
1398 1456
1399 1457 static void call_return_state_hooks_compare(struct expression *expr)
1400 1458 {
1401 1459 struct returned_state_callback *cb;
1402 1460 char *return_ranges;
1403 1461 int final_pass_orig = final_pass;
1404 1462 sval_t sval = { .type = &int_ctype };
1405 1463 sval_t ret;
1406 1464
1407 1465 if (!get_implied_value(expr, &ret))
1408 1466 ret.value = -1;
1409 1467
1410 1468 __push_fake_cur_stree();
1411 1469
1412 1470 final_pass = 0;
1413 1471 __split_whole_condition(expr);
1414 1472 final_pass = final_pass_orig;
1415 1473
1416 1474 if (ret.value != 0) {
1417 1475 return_ranges = alloc_sname("1");
1418 1476 sval.value = 1;
1419 1477 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1420 1478
1421 1479 return_id++;
1422 1480 FOR_EACH_PTR(returned_state_callbacks, cb) {
1423 1481 cb->callback(return_id, return_ranges, expr);
1424 1482 } END_FOR_EACH_PTR(cb);
1425 1483 }
1426 1484
1427 1485 __push_true_states();
1428 1486 __use_false_states();
1429 1487
1430 1488 if (ret.value != 1) {
1431 1489 return_ranges = alloc_sname("0");
1432 1490 sval.value = 0;
1433 1491 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1434 1492
1435 1493 return_id++;
1436 1494 FOR_EACH_PTR(returned_state_callbacks, cb) {
1437 1495 cb->callback(return_id, return_ranges, expr);
1438 1496 } END_FOR_EACH_PTR(cb);
1439 1497 }
1440 1498
1441 1499 __merge_true_states();
1442 1500 __free_fake_cur_stree();
1443 1501 }
1444 1502
1445 1503 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1446 1504 {
1447 1505 struct sm_state *tmp;
1448 1506
1449 1507 FOR_EACH_PTR(slist, tmp) {
1450 1508 if (strcmp(tmp->state->name, sm->state->name) == 0)
1451 1509 return 1;
1452 1510 } END_FOR_EACH_PTR(tmp);
1453 1511
1454 1512 return 0;
1455 1513 }
1456 1514
1457 1515 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1458 1516 {
1459 1517 struct returned_state_callback *cb;
1460 1518 struct range_list *rl;
1461 1519 char *return_ranges;
1462 1520 struct sm_state *tmp;
1463 1521 int ret = 0;
1464 1522 int nr_possible, nr_states;
1465 1523 char *compare_str;
1466 1524 char buf[128];
1467 1525 struct state_list *already_handled = NULL;
1468 1526 sval_t sval;
1469 1527
1470 1528 if (!sm || !sm->merged)
1471 1529 return 0;
1472 1530
1473 1531 if (too_many_possible(sm))
1474 1532 return 0;
1475 1533
1476 1534 /* bail if it gets too complicated */
1477 1535 nr_possible = 0;
1478 1536 FOR_EACH_PTR(sm->possible, tmp) {
1479 1537 if (tmp->merged)
1480 1538 continue;
1481 1539 nr_possible++;
1482 1540 } END_FOR_EACH_PTR(tmp);
1483 1541 nr_states = get_db_state_count();
1484 1542 if (nr_states * nr_possible >= 2000)
1485 1543 return 0;
1486 1544
1487 1545 FOR_EACH_PTR(sm->possible, tmp) {
1488 1546 if (tmp->merged)
1489 1547 continue;
1490 1548 if (ptr_in_list(tmp, already_handled))
1491 1549 continue;
1492 1550 add_ptr_list(&already_handled, tmp);
1493 1551
1494 1552 ret = 1;
1495 1553 __push_fake_cur_stree();
1496 1554
1497 1555 overwrite_states_using_pool(sm, tmp);
1498 1556
1499 1557 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1500 1558 return_ranges = show_rl(rl);
1501 1559 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1502 1560 if (!rl_to_sval(rl, &sval)) {
1503 1561 compare_str = get_return_compare_str(expr);
1504 1562 if (compare_str) {
1505 1563 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1506 1564 return_ranges = alloc_sname(buf);
1507 1565 }
1508 1566 }
1509 1567
1510 1568 return_id++;
1511 1569 FOR_EACH_PTR(returned_state_callbacks, cb) {
1512 1570 cb->callback(return_id, return_ranges, expr);
1513 1571 } END_FOR_EACH_PTR(cb);
1514 1572
1515 1573 __free_fake_cur_stree();
1516 1574 } END_FOR_EACH_PTR(tmp);
1517 1575
1518 1576 free_slist(&already_handled);
1519 1577
1520 1578 return ret;
1521 1579 }
1522 1580
1523 1581 static int call_return_state_hooks_split_possible(struct expression *expr)
1524 1582 {
1525 1583 struct sm_state *sm;
1526 1584
1527 1585 if (!expr || expr_equal_to_param(expr, -1))
1528 1586 return 0;
1529 1587
1530 1588 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1531 1589 return split_possible_helper(sm, expr);
1532 1590 }
1533 1591
1534 1592 static bool has_possible_negative(struct sm_state *sm)
1535 1593 {
1536 1594 struct sm_state *tmp;
1537 1595
1538 1596 FOR_EACH_PTR(sm->possible, tmp) {
1539 1597 if (!estate_rl(tmp->state))
1540 1598 continue;
1541 1599 if (sval_is_negative(estate_min(tmp->state)) &&
1542 1600 sval_is_negative(estate_max(tmp->state)))
1543 1601 return true;
1544 1602 } END_FOR_EACH_PTR(tmp);
1545 1603
1546 1604 return false;
1547 1605 }
1548 1606
1549 1607 static bool has_possible_zero_null(struct sm_state *sm)
1550 1608 {
1551 1609 struct sm_state *tmp;
1552 1610 sval_t sval;
1553 1611
1554 1612 FOR_EACH_PTR(sm->possible, tmp) {
1555 1613 if (!estate_get_single_value(tmp->state, &sval))
1556 1614 continue;
1557 1615 if (sval.value == 0)
1558 1616 return true;
1559 1617 } END_FOR_EACH_PTR(tmp);
1560 1618
1561 1619 return false;
1562 1620 }
1563 1621
1564 1622 static int split_positive_from_negative(struct expression *expr)
1565 1623 {
1566 1624 struct sm_state *sm;
1567 1625 struct returned_state_callback *cb;
1568 1626 struct range_list *rl;
1569 1627 const char *return_ranges;
1570 1628 struct range_list *ret_rl;
1571 1629 int undo;
1572 1630 bool has_zero;
1573 1631
1574 1632 /* We're going to print the states 3 times */
1575 1633 if (get_db_state_count() > 10000 / 3)
1576 1634 return 0;
1577 1635
1578 1636 if (!get_implied_rl(expr, &rl) || !rl)
1579 1637 return 0;
1580 1638 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1581 1639 return 0;
1582 1640 /* Forget about INT_MAX and larger */
1583 1641 if (rl_max(rl).value <= 0)
1584 1642 return 0;
1585 1643 if (!sval_is_negative(rl_min(rl)))
1586 1644 return 0;
1587 1645
1588 1646 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1589 1647 if (!sm)
1590 1648 return 0;
1591 1649 if (!has_possible_negative(sm))
1592 1650 return 0;
1593 1651 has_zero = has_possible_zero_null(sm);
1594 1652
1595 1653 if (!assume(compare_expression(expr, has_zero ? '>' : SPECIAL_GTE, zero_expr())))
1596 1654 return 0;
1597 1655
1598 1656 return_id++;
1599 1657 return_ranges = get_return_ranges_str(expr, &ret_rl);
1600 1658 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1601 1659 FOR_EACH_PTR(returned_state_callbacks, cb) {
1602 1660 cb->callback(return_id, (char *)return_ranges, expr);
1603 1661 } END_FOR_EACH_PTR(cb);
1604 1662
1605 1663 end_assume();
1606 1664
1607 1665 if (has_zero) {
1608 1666 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1609 1667
1610 1668 return_id++;
1611 1669 return_ranges = get_return_ranges_str(expr, &ret_rl);
1612 1670 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1613 1671 FOR_EACH_PTR(returned_state_callbacks, cb) {
1614 1672 cb->callback(return_id, (char *)return_ranges, expr);
1615 1673 } END_FOR_EACH_PTR(cb);
1616 1674
1617 1675 if (undo)
1618 1676 end_assume();
1619 1677 }
1620 1678
1621 1679 undo = assume(compare_expression(expr, '<', zero_expr()));
1622 1680
1623 1681 return_id++;
1624 1682 return_ranges = get_return_ranges_str(expr, &ret_rl);
1625 1683 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1626 1684 FOR_EACH_PTR(returned_state_callbacks, cb) {
1627 1685 cb->callback(return_id, (char *)return_ranges, expr);
1628 1686 } END_FOR_EACH_PTR(cb);
1629 1687
1630 1688 if (undo)
1631 1689 end_assume();
1632 1690
1633 1691 return 1;
1634 1692 }
1635 1693
1636 1694 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1637 1695 {
1638 1696 struct returned_state_callback *cb;
1639 1697 struct range_list *rl;
1640 1698 struct range_list *nonnull_rl;
1641 1699 sval_t null_sval;
1642 1700 struct range_list *null_rl = NULL;
1643 1701 char *return_ranges;
1644 1702 struct sm_state *sm;
1645 1703 struct smatch_state *state;
1646 1704 int nr_states;
1647 1705 int final_pass_orig = final_pass;
1648 1706
1649 1707 if (!expr || expr_equal_to_param(expr, -1))
1650 1708 return 0;
1651 1709 if (expr->type == EXPR_CALL)
1652 1710 return 0;
1653 1711
1654 1712 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1655 1713 if (!sm)
1656 1714 return 0;
1657 1715 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1658 1716 return 0;
1659 1717 state = sm->state;
1660 1718 if (!estate_rl(state))
1661 1719 return 0;
1662 1720 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1663 1721 return 0;
1664 1722 if (!has_possible_zero_null(sm))
1665 1723 return 0;
1666 1724
1667 1725 nr_states = get_db_state_count();
1668 1726 if (option_info && nr_states >= 1500)
1669 1727 return 0;
1670 1728
1671 1729 rl = estate_rl(state);
1672 1730
1673 1731 __push_fake_cur_stree();
1674 1732
1675 1733 final_pass = 0;
1676 1734 __split_whole_condition(expr);
1677 1735 final_pass = final_pass_orig;
1678 1736
1679 1737 nonnull_rl = rl_filter(rl, rl_zero());
1680 1738 return_ranges = show_rl(nonnull_rl);
1681 1739 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1682 1740
1683 1741 return_id++;
1684 1742 FOR_EACH_PTR(returned_state_callbacks, cb) {
1685 1743 cb->callback(return_id, return_ranges, expr);
1686 1744 } END_FOR_EACH_PTR(cb);
1687 1745
1688 1746 __push_true_states();
1689 1747 __use_false_states();
1690 1748
1691 1749 return_ranges = alloc_sname("0");
1692 1750 null_sval = sval_type_val(rl_type(rl), 0);
1693 1751 add_range(&null_rl, null_sval, null_sval);
1694 1752 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1695 1753 return_id++;
1696 1754 FOR_EACH_PTR(returned_state_callbacks, cb) {
1697 1755 cb->callback(return_id, return_ranges, expr);
1698 1756 } END_FOR_EACH_PTR(cb);
1699 1757
1700 1758 __merge_true_states();
1701 1759 __free_fake_cur_stree();
1702 1760
1703 1761 return 1;
1704 1762 }
1705 1763
1706 1764 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1707 1765 {
1708 1766 struct sm_state *sm;
1709 1767 struct range_list *rl;
1710 1768 struct range_list *nonzero_rl;
1711 1769 sval_t zero_sval;
1712 1770 struct range_list *zero_rl = NULL;
1713 1771 int nr_states;
1714 1772 struct returned_state_callback *cb;
1715 1773 char *return_ranges;
1716 1774 int final_pass_orig = final_pass;
1717 1775
1718 1776 if (option_project != PROJ_KERNEL)
1719 1777 return 0;
1720 1778
1721 1779 nr_states = get_db_state_count();
1722 1780 if (nr_states > 1500)
1723 1781 return 0;
1724 1782
1725 1783 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1726 1784 if (!sm)
1727 1785 return 0;
1728 1786 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1729 1787 return 0;
1730 1788
1731 1789 rl = estate_rl(sm->state);
1732 1790 if (!rl)
1733 1791 return 0;
1734 1792
1735 1793 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1736 1794 return 0;
1737 1795 if (rl_max(rl).value != 0)
1738 1796 return 0;
1739 1797 if (!has_possible_zero_null(sm))
1740 1798 return 0;
1741 1799
1742 1800 __push_fake_cur_stree();
1743 1801
1744 1802 final_pass = 0;
1745 1803 __split_whole_condition(expr);
1746 1804 final_pass = final_pass_orig;
1747 1805
1748 1806 nonzero_rl = rl_filter(rl, rl_zero());
1749 1807 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1750 1808 return_ranges = show_rl(nonzero_rl);
1751 1809 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1752 1810
1753 1811 return_id++;
1754 1812 FOR_EACH_PTR(returned_state_callbacks, cb) {
1755 1813 cb->callback(return_id, return_ranges, expr);
1756 1814 } END_FOR_EACH_PTR(cb);
1757 1815
1758 1816 __push_true_states();
1759 1817 __use_false_states();
1760 1818
1761 1819 return_ranges = alloc_sname("0");
1762 1820 zero_sval = sval_type_val(rl_type(rl), 0);
1763 1821 add_range(&zero_rl, zero_sval, zero_sval);
1764 1822 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1765 1823 return_id++;
1766 1824 FOR_EACH_PTR(returned_state_callbacks, cb) {
1767 1825 cb->callback(return_id, return_ranges, expr);
1768 1826 } END_FOR_EACH_PTR(cb);
1769 1827
1770 1828 __merge_true_states();
1771 1829 __free_fake_cur_stree();
1772 1830
1773 1831 return 1;
1774 1832 }
1775 1833
1776 1834 static int is_boolean(struct expression *expr)
1777 1835 {
1778 1836 struct range_list *rl;
1779 1837
1780 1838 if (!get_implied_rl(expr, &rl))
1781 1839 return 0;
1782 1840 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1783 1841 return 1;
1784 1842 return 0;
1785 1843 }
1786 1844
1787 1845 static int is_conditional(struct expression *expr)
1788 1846 {
1789 1847 if (!expr)
1790 1848 return 0;
1791 1849 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1792 1850 return 1;
1793 1851 return 0;
1794 1852 }
1795 1853
1796 1854 static int splitable_function_call(struct expression *expr)
1797 1855 {
1798 1856 struct sm_state *sm;
1799 1857 char buf[64];
1800 1858
1801 1859 if (!expr || expr->type != EXPR_CALL)
1802 1860 return 0;
1803 1861 snprintf(buf, sizeof(buf), "return %p", expr);
1804 1862 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1805 1863 return split_possible_helper(sm, expr);
1806 1864 }
1807 1865
1808 1866 static struct sm_state *find_bool_param(void)
1809 1867 {
1810 1868 struct stree *start_states;
1811 1869 struct symbol *arg;
1812 1870 struct sm_state *sm, *tmp;
1813 1871 sval_t sval;
1814 1872
1815 1873 start_states = get_start_states();
1816 1874
1817 1875 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1818 1876 if (!arg->ident)
1819 1877 continue;
1820 1878 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1821 1879 if (!sm)
1822 1880 continue;
1823 1881 if (rl_min(estate_rl(sm->state)).value != 0 ||
1824 1882 rl_max(estate_rl(sm->state)).value != 1)
1825 1883 continue;
1826 1884 goto found;
1827 1885 } END_FOR_EACH_PTR_REVERSE(arg);
1828 1886
1829 1887 return NULL;
1830 1888
1831 1889 found:
1832 1890 /*
1833 1891 * Check if it's splitable. If not, then splitting it up is likely not
1834 1892 * useful for the callers.
1835 1893 */
1836 1894 FOR_EACH_PTR(sm->possible, tmp) {
1837 1895 if (is_merged(tmp))
1838 1896 continue;
1839 1897 if (!estate_get_single_value(tmp->state, &sval))
1840 1898 return NULL;
1841 1899 } END_FOR_EACH_PTR(tmp);
1842 1900
1843 1901 return sm;
↓ open down ↓ |
494 lines elided |
↑ open up ↑ |
1844 1902 }
1845 1903
1846 1904 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1847 1905 {
1848 1906 struct returned_state_callback *cb;
1849 1907 struct range_list *ret_rl;
1850 1908 const char *return_ranges;
1851 1909 struct sm_state *tmp;
1852 1910 int ret = 0;
1853 1911 int nr_possible, nr_states;
1854 - char *compare_str = NULL;
1855 - char buf[128];
1856 1912 struct state_list *already_handled = NULL;
1857 1913
1858 1914 if (!sm || !sm->merged)
1859 1915 return 0;
1860 1916
1861 1917 if (too_many_possible(sm))
1862 1918 return 0;
1863 1919
1864 1920 /* bail if it gets too complicated */
1865 1921 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1866 1922 nr_states = get_db_state_count();
1867 1923 if (nr_states * nr_possible >= 2000)
1868 1924 return 0;
1869 1925
1870 1926 FOR_EACH_PTR(sm->possible, tmp) {
1871 1927 if (tmp->merged)
1872 1928 continue;
1873 1929 if (ptr_in_list(tmp, already_handled))
↓ open down ↓ |
8 lines elided |
↑ open up ↑ |
1874 1930 continue;
1875 1931 add_ptr_list(&already_handled, tmp);
1876 1932
1877 1933 ret = 1;
1878 1934 __push_fake_cur_stree();
1879 1935
1880 1936 overwrite_states_using_pool(sm, tmp);
1881 1937
1882 1938 return_ranges = get_return_ranges_str(expr, &ret_rl);
1883 1939 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1884 - compare_str = get_return_compare_str(expr);
1885 - if (compare_str) {
1886 - snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1887 - return_ranges = alloc_sname(buf);
1888 - }
1889 -
1890 1940 return_id++;
1891 1941 FOR_EACH_PTR(returned_state_callbacks, cb) {
1892 1942 cb->callback(return_id, (char *)return_ranges, expr);
1893 1943 } END_FOR_EACH_PTR(cb);
1894 1944
1895 1945 __free_fake_cur_stree();
1896 1946 } END_FOR_EACH_PTR(tmp);
1897 1947
1898 1948 free_slist(&already_handled);
1899 1949
1900 1950 return ret;
1901 1951 }
1902 1952
1903 1953 static int split_by_bool_param(struct expression *expr)
1904 1954 {
1905 1955 struct sm_state *start_sm, *sm;
1906 1956 sval_t sval;
1907 1957
1908 1958 start_sm = find_bool_param();
1909 1959 if (!start_sm)
1910 1960 return 0;
1911 1961 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1912 1962 if (!sm || estate_get_single_value(sm->state, &sval))
1913 1963 return 0;
1914 1964 return split_on_bool_sm(sm, expr);
1915 1965 }
1916 1966
1917 1967 static int split_by_null_nonnull_param(struct expression *expr)
1918 1968 {
1919 1969 struct symbol *arg;
1920 1970 struct sm_state *sm;
1921 1971 sval_t zero = {
1922 1972 .type = &ulong_ctype,
1923 1973 };
1924 1974
1925 1975 /* function must only take one pointer */
1926 1976 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1927 1977 return 0;
1928 1978 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1929 1979 if (!arg->ident)
1930 1980 return 0;
1931 1981 if (get_real_base_type(arg)->type != SYM_PTR)
1932 1982 return 0;
1933 1983
1934 1984 if (param_was_set_var_sym(arg->ident->name, arg))
1935 1985 return 0;
1936 1986 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1937 1987 if (!sm)
1938 1988 return 0;
1939 1989
1940 1990 if (!rl_has_sval(estate_rl(sm->state), zero))
1941 1991 return 0;
1942 1992
1943 1993 return split_on_bool_sm(sm, expr);
1944 1994 }
1945 1995
1946 1996 struct expression *strip_expr_statement(struct expression *expr)
1947 1997 {
1948 1998 struct expression *orig = expr;
1949 1999 struct statement *stmt, *last_stmt;
1950 2000
1951 2001 if (!expr)
1952 2002 return NULL;
1953 2003 if (expr->type == EXPR_PREOP && expr->op == '(')
1954 2004 expr = expr->unop;
1955 2005 if (expr->type != EXPR_STATEMENT)
1956 2006 return orig;
1957 2007 stmt = expr->statement;
1958 2008 if (!stmt || stmt->type != STMT_COMPOUND)
1959 2009 return orig;
1960 2010
1961 2011 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1962 2012 if (!last_stmt || last_stmt->type == STMT_LABEL)
1963 2013 last_stmt = last_stmt->label_statement;
1964 2014 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
1965 2015 return orig;
1966 2016 return strip_expr(last_stmt->expression);
1967 2017 }
1968 2018
1969 2019 static void call_return_state_hooks(struct expression *expr)
1970 2020 {
1971 2021 struct returned_state_callback *cb;
1972 2022 struct range_list *ret_rl;
1973 2023 const char *return_ranges;
1974 2024 int nr_states;
1975 2025 sval_t sval;
1976 2026
1977 2027 if (__path_is_null())
1978 2028 return;
1979 2029
1980 2030 expr = strip_expr(expr);
1981 2031 expr = strip_expr_statement(expr);
1982 2032
1983 2033 if (is_impossible_path())
1984 2034 goto vanilla;
1985 2035
1986 2036 if (expr && (expr->type == EXPR_COMPARE ||
1987 2037 !get_implied_value(expr, &sval)) &&
1988 2038 (is_condition(expr) || is_boolean(expr))) {
1989 2039 call_return_state_hooks_compare(expr);
1990 2040 return;
1991 2041 } else if (is_conditional(expr)) {
1992 2042 call_return_state_hooks_conditional(expr);
1993 2043 return;
1994 2044 } else if (call_return_state_hooks_split_possible(expr)) {
1995 2045 return;
1996 2046 } else if (split_positive_from_negative(expr)) {
1997 2047 return;
1998 2048 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
1999 2049 return;
2000 2050 } else if (call_return_state_hooks_split_success_fail(expr)) {
2001 2051 return;
2002 2052 } else if (splitable_function_call(expr)) {
2003 2053 return;
2004 2054 } else if (split_by_bool_param(expr)) {
2005 2055 } else if (split_by_null_nonnull_param(expr)) {
2006 2056 return;
↓ open down ↓ |
107 lines elided |
↑ open up ↑ |
2007 2057 }
2008 2058
2009 2059 vanilla:
2010 2060 return_ranges = get_return_ranges_str(expr, &ret_rl);
2011 2061 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2012 2062
2013 2063 return_id++;
2014 2064 nr_states = get_db_state_count();
2015 2065 if (nr_states >= 10000) {
2016 2066 match_return_info(return_id, (char *)return_ranges, expr);
2067 + print_limited_param_set(return_id, (char *)return_ranges, expr);
2017 2068 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2018 2069 return;
2019 2070 }
2020 2071 FOR_EACH_PTR(returned_state_callbacks, cb) {
2021 2072 cb->callback(return_id, (char *)return_ranges, expr);
2022 2073 } END_FOR_EACH_PTR(cb);
2023 2074 }
2024 2075
2025 2076 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2026 2077 {
2027 2078 struct returned_member_callback *cb;
2028 2079 struct stree *stree;
2029 2080 struct sm_state *sm;
2030 2081 struct symbol *type;
2031 2082 char *name;
2032 2083 char member_name[256];
2033 2084 int len;
2034 2085
2035 2086 type = get_type(expr);
2036 2087 if (!type || type->type != SYM_PTR)
2037 2088 return;
2038 2089 name = expr_to_var(expr);
2039 2090 if (!name)
2040 2091 return;
2041 2092
2042 2093 member_name[sizeof(member_name) - 1] = '\0';
2043 2094 strcpy(member_name, "$");
2044 2095
2045 2096 len = strlen(name);
2046 2097 FOR_EACH_PTR(returned_member_callbacks, cb) {
2047 2098 stree = __get_cur_stree();
2048 2099 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2049 2100 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2050 2101 strcpy(member_name, "*$");
2051 2102 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2052 2103 continue;
2053 2104 }
2054 2105 if (strncmp(sm->name, name, len) != 0)
2055 2106 continue;
2056 2107 if (strncmp(sm->name + len, "->", 2) != 0)
2057 2108 continue;
2058 2109 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2059 2110 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2060 2111 } END_FOR_EACH_SM(sm);
2061 2112 } END_FOR_EACH_PTR(cb);
2062 2113
2063 2114 free_string(name);
2064 2115 }
2065 2116
2066 2117 static void reset_memdb(struct symbol *sym)
2067 2118 {
2068 2119 mem_sql(NULL, NULL, "delete from caller_info;");
2069 2120 mem_sql(NULL, NULL, "delete from return_states;");
2070 2121 mem_sql(NULL, NULL, "delete from call_implies;");
2071 2122 mem_sql(NULL, NULL, "delete from return_implies;");
2072 2123 }
2073 2124
2074 2125 static void match_end_func_info(struct symbol *sym)
2075 2126 {
2076 2127 if (__path_is_null())
2077 2128 return;
2078 2129 call_return_state_hooks(NULL);
2079 2130 }
2080 2131
2081 2132 static void match_after_func(struct symbol *sym)
2082 2133 {
2083 2134 if (!__inline_fn)
2084 2135 reset_memdb(sym);
2085 2136 }
2086 2137
2087 2138 static void init_memdb(void)
2088 2139 {
2089 2140 char *err = NULL;
2090 2141 int rc;
2091 2142 const char *schema_files[] = {
2092 2143 "db/db.schema",
2093 2144 "db/caller_info.schema",
2094 2145 "db/common_caller_info.schema",
2095 2146 "db/return_states.schema",
2096 2147 "db/function_type_size.schema",
2097 2148 "db/type_size.schema",
2098 2149 "db/function_type_info.schema",
2099 2150 "db/type_info.schema",
2100 2151 "db/call_implies.schema",
2101 2152 "db/return_implies.schema",
2102 2153 "db/function_ptr.schema",
2103 2154 "db/local_values.schema",
2104 2155 "db/function_type_value.schema",
2105 2156 "db/type_value.schema",
2106 2157 "db/function_type.schema",
2107 2158 "db/data_info.schema",
2108 2159 "db/parameter_name.schema",
2109 2160 "db/constraints.schema",
2110 2161 "db/constraints_required.schema",
2111 2162 "db/fn_ptr_data_link.schema",
2112 2163 "db/fn_data_link.schema",
2113 2164 "db/mtag_about.schema",
2114 2165 "db/mtag_map.schema",
2115 2166 "db/mtag_data.schema",
2116 2167 "db/mtag_alias.schema",
2117 2168 };
2118 2169 static char buf[4096];
2119 2170 int fd;
2120 2171 int ret;
2121 2172 int i;
2122 2173
2123 2174 rc = sqlite3_open(":memory:", &mem_db);
2124 2175 if (rc != SQLITE_OK) {
2125 2176 sm_ierror("starting In-Memory database.");
2126 2177 return;
2127 2178 }
2128 2179
2129 2180 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2130 2181 fd = open_schema_file(schema_files[i]);
2131 2182 if (fd < 0)
2132 2183 continue;
2133 2184 ret = read(fd, buf, sizeof(buf));
2134 2185 if (ret < 0) {
2135 2186 sm_ierror("failed to read: %s", schema_files[i]);
2136 2187 continue;
2137 2188 }
2138 2189 close(fd);
2139 2190 if (ret == sizeof(buf)) {
2140 2191 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2141 2192 schema_files[i], sizeof(buf));
2142 2193 continue;
2143 2194 }
2144 2195 buf[ret] = '\0';
2145 2196 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2146 2197 if (rc != SQLITE_OK) {
2147 2198 sm_ierror("SQL error #2: %s", err);
2148 2199 sm_ierror("%s", buf);
2149 2200 }
2150 2201 }
2151 2202 }
2152 2203
2153 2204 static void init_cachedb(void)
2154 2205 {
2155 2206 char *err = NULL;
2156 2207 int rc;
2157 2208 const char *schema_files[] = {
2158 2209 "db/call_implies.schema",
2159 2210 "db/return_implies.schema",
2160 2211 "db/type_info.schema",
2161 2212 "db/mtag_data.schema",
2162 2213 "db/sink_info.schema",
2163 2214 };
2164 2215 static char buf[4096];
2165 2216 int fd;
2166 2217 int ret;
2167 2218 int i;
2168 2219
2169 2220 rc = sqlite3_open(":memory:", &cache_db);
2170 2221 if (rc != SQLITE_OK) {
2171 2222 sm_ierror("starting In-Memory database.");
2172 2223 return;
2173 2224 }
2174 2225
2175 2226 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2176 2227 fd = open_schema_file(schema_files[i]);
2177 2228 if (fd < 0)
2178 2229 continue;
2179 2230 ret = read(fd, buf, sizeof(buf));
2180 2231 if (ret < 0) {
2181 2232 sm_ierror("failed to read: %s", schema_files[i]);
2182 2233 continue;
2183 2234 }
2184 2235 close(fd);
2185 2236 if (ret == sizeof(buf)) {
2186 2237 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2187 2238 schema_files[i], sizeof(buf));
2188 2239 continue;
2189 2240 }
2190 2241 buf[ret] = '\0';
2191 2242 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2192 2243 if (rc != SQLITE_OK) {
2193 2244 sm_ierror("SQL error #2: %s", err);
2194 2245 sm_ierror("%s", buf);
2195 2246 }
2196 2247 }
2197 2248 }
2198 2249
2199 2250 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2200 2251 {
2201 2252 static char buf[4096];
2202 2253 char tmp[256];
2203 2254 char *p = buf;
2204 2255 char *table = _table;
2205 2256 int i;
2206 2257
2207 2258
2208 2259 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2209 2260 for (i = 0; i < argc; i++) {
2210 2261 if (i)
2211 2262 p += snprintf(p, 4096 - (p - buf), ", ");
2212 2263 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2213 2264 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2214 2265
2215 2266 }
2216 2267 p += snprintf(p, 4096 - (p - buf), ");");
2217 2268 if (p - buf > 4096)
2218 2269 return 0;
2219 2270
2220 2271 sm_msg("SQL: %s", buf);
2221 2272 return 0;
2222 2273 }
2223 2274
2224 2275 static void dump_cache(struct symbol_list *sym_list)
2225 2276 {
2226 2277 if (!option_info)
2227 2278 return;
2228 2279 cache_sql(&save_cache_data, (char *)"type_info", "select * from type_info;");
2229 2280 cache_sql(&save_cache_data, (char *)"return_implies", "select * from return_implies;");
2230 2281 cache_sql(&save_cache_data, (char *)"call_implies", "select * from call_implies;");
2231 2282 cache_sql(&save_cache_data, (char *)"mtag_data", "select * from mtag_data;");
2232 2283 cache_sql(&save_cache_data, (char *)"sink_info", "select * from sink_info;");
2233 2284 }
2234 2285
2235 2286 void open_smatch_db(char *db_file)
2236 2287 {
2237 2288 int rc;
2238 2289
2239 2290 if (option_no_db)
2240 2291 return;
2241 2292
2242 2293 use_states = malloc(num_checks + 1);
2243 2294 memset(use_states, 0xff, num_checks + 1);
2244 2295
2245 2296 init_memdb();
2246 2297 init_cachedb();
2247 2298
2248 2299 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2249 2300 if (rc != SQLITE_OK) {
2250 2301 option_no_db = 1;
2251 2302 return;
2252 2303 }
2253 2304 run_sql(NULL, NULL,
2254 2305 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2255 2306 return;
2256 2307 }
2257 2308
2258 2309 static void register_common_funcs(void)
2259 2310 {
2260 2311 struct token *token;
2261 2312 char *func;
2262 2313 char filename[256];
2263 2314
2264 2315 if (option_project == PROJ_NONE)
2265 2316 strcpy(filename, "common_functions");
2266 2317 else
2267 2318 snprintf(filename, 256, "%s.common_functions", option_project_str);
2268 2319
2269 2320 token = get_tokens_file(filename);
2270 2321 if (!token)
2271 2322 return;
2272 2323 if (token_type(token) != TOKEN_STREAMBEGIN)
2273 2324 return;
2274 2325 token = token->next;
2275 2326 while (token_type(token) != TOKEN_STREAMEND) {
2276 2327 if (token_type(token) != TOKEN_IDENT)
2277 2328 return;
2278 2329 func = alloc_string(show_ident(token->ident));
2279 2330 add_ptr_list(&common_funcs, func);
↓ open down ↓ |
253 lines elided |
↑ open up ↑ |
2280 2331 token = token->next;
2281 2332 }
2282 2333 clear_token_alloc();
2283 2334 }
2284 2335
2285 2336 static char *get_next_string(char **str)
2286 2337 {
2287 2338 static char string[256];
2288 2339 char *start;
2289 2340 char *p = *str;
2290 - int len;
2341 + int len, i, j;
2291 2342
2292 2343 if (*p == '\0')
2293 2344 return NULL;
2294 2345 start = p;
2295 2346
2296 - while (*p != '\0' && *p != ' ' && *p != '\n')
2347 + while (*p != '\0' && *p != '\n') {
2348 + if (*p == '\\' && *(p + 1) == ' ') {
2349 + p += 2;
2350 + continue;
2351 + }
2352 + if (*p == ' ')
2353 + break;
2297 2354 p++;
2355 + }
2298 2356
2299 2357 len = p - start;
2300 - if (len > 256) {
2301 - memcpy(string, start, 255);
2302 - string[255] = '\0';
2358 + if (len >= sizeof(string)) {
2359 + memcpy(string, start, sizeof(string));
2360 + string[sizeof(string) - 1] = '\0';
2303 2361 sm_ierror("return_fix: '%s' too long", string);
2304 2362 **str = '\0';
2305 2363 return NULL;
2306 2364 }
2307 2365 memcpy(string, start, len);
2308 2366 string[len] = '\0';
2367 + for (i = 0; i < sizeof(string) - 1; i++) {
2368 + if (string[i] == '\\' && string[i + 1] == ' ') {
2369 + for (j = i; string[j] != '\0'; j++)
2370 + string[j] = string[j + 1];
2371 + }
2372 + }
2309 2373 if (*p != '\0')
2310 2374 p++;
2311 2375 *str = p;
2312 2376 return string;
2313 2377 }
2314 2378
2315 2379 static void register_return_replacements(void)
2316 2380 {
2317 2381 char *func, *orig, *new;
2318 2382 char filename[256];
2319 2383 char buf[4096];
2320 2384 int fd, ret, i;
2321 2385 char *p;
2322 2386
2323 2387 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2324 2388 fd = open_schema_file(filename);
2325 2389 if (fd < 0)
2326 2390 return;
2327 2391 ret = read(fd, buf, sizeof(buf));
2328 2392 close(fd);
2329 2393 if (ret < 0)
2330 2394 return;
2331 2395 if (ret == sizeof(buf)) {
2332 2396 sm_ierror("file too large: %s (limit %zd bytes)",
2333 2397 filename, sizeof(buf));
2334 2398 return;
2335 2399 }
2336 2400 buf[ret] = '\0';
2337 2401
2338 2402 p = buf;
2339 2403 while (*p) {
2340 2404 get_next_string(&p);
2341 2405 replace_count++;
2342 2406 }
2343 2407 if (replace_count == 0 || replace_count % 3 != 0) {
2344 2408 replace_count = 0;
2345 2409 return;
2346 2410 }
2347 2411 replace_table = malloc(replace_count * sizeof(char *));
2348 2412
2349 2413 p = buf;
2350 2414 i = 0;
2351 2415 while (*p) {
2352 2416 func = alloc_string(get_next_string(&p));
2353 2417 orig = alloc_string(get_next_string(&p));
2354 2418 new = alloc_string(get_next_string(&p));
2355 2419
2356 2420 replace_table[i++] = func;
2357 2421 replace_table[i++] = orig;
2358 2422 replace_table[i++] = new;
2359 2423 }
2360 2424 }
2361 2425
2362 2426 void register_definition_db_callbacks(int id)
2363 2427 {
2364 2428 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2365 2429 add_split_return_callback(match_return_info);
2366 2430 add_split_return_callback(print_returned_struct_members);
2367 2431 add_hook(&call_return_state_hooks, RETURN_HOOK);
2368 2432 add_hook(&match_end_func_info, END_FUNC_HOOK);
2369 2433 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2370 2434
2371 2435 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2372 2436 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2373 2437 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2374 2438
2375 2439 register_common_funcs();
2376 2440 register_return_replacements();
2377 2441
2378 2442 add_hook(&dump_cache, END_FILE_HOOK);
2379 2443 }
2380 2444
2381 2445 void register_db_call_marker(int id)
2382 2446 {
2383 2447 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2384 2448 }
2385 2449
2386 2450 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2387 2451 {
2388 2452 struct expression *arg;
2389 2453 char *name = NULL;
2390 2454 char member_name[256];
2391 2455
2392 2456 *sym = NULL;
2393 2457
2394 2458 if (param == -1) {
2395 2459 const char *star = "";
2396 2460
2397 2461 if (expr->type != EXPR_ASSIGNMENT)
2398 2462 return NULL;
2399 2463 if (get_type(expr->left) == &int_ctype && strcmp(key, "$") != 0)
2400 2464 return NULL;
2401 2465 name = expr_to_var_sym(expr->left, sym);
2402 2466 if (!name)
2403 2467 return NULL;
2404 2468 if (key[0] == '*') {
2405 2469 star = "*";
2406 2470 key++;
2407 2471 }
2408 2472 if (strncmp(key, "$", 1) != 0)
2409 2473 return name;
2410 2474 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2411 2475 free_string(name);
2412 2476 return alloc_string(member_name);
2413 2477 }
2414 2478
2415 2479 while (expr->type == EXPR_ASSIGNMENT)
2416 2480 expr = strip_expr(expr->right);
2417 2481 if (expr->type != EXPR_CALL)
2418 2482 return NULL;
2419 2483
2420 2484 arg = get_argument_from_call_expr(expr->args, param);
2421 2485 if (!arg)
2422 2486 return NULL;
2423 2487
2424 2488 return get_variable_from_key(arg, key, sym);
2425 2489 }
2426 2490
2427 2491 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2428 2492 {
2429 2493 char buf[256];
2430 2494 char *tmp;
2431 2495 bool add_star = false;
2432 2496
2433 2497 if (!arg)
2434 2498 return NULL;
2435 2499
2436 2500 arg = strip_expr(arg);
2437 2501
2438 2502 if (strcmp(key, "$") == 0)
2439 2503 return expr_to_var_sym(arg, sym);
2440 2504
2441 2505 if (strcmp(key, "*$") == 0) {
2442 2506 if (arg->type == EXPR_PREOP && arg->op == '&') {
2443 2507 arg = strip_expr(arg->unop);
2444 2508 return expr_to_var_sym(arg, sym);
2445 2509 } else {
2446 2510 tmp = expr_to_var_sym(arg, sym);
2447 2511 if (!tmp)
2448 2512 return NULL;
2449 2513 snprintf(buf, sizeof(buf), "*%s", tmp);
2450 2514 free_string(tmp);
2451 2515 return alloc_string(buf);
2452 2516 }
2453 2517 }
2454 2518
2455 2519 if (key[0] == '*') {
2456 2520 add_star = true;
2457 2521 key++;
2458 2522 }
2459 2523
2460 2524 if (arg->type == EXPR_PREOP && arg->op == '&') {
2461 2525 arg = strip_expr(arg->unop);
2462 2526 tmp = expr_to_var_sym(arg, sym);
2463 2527 if (!tmp)
2464 2528 return NULL;
2465 2529 snprintf(buf, sizeof(buf), "%s%s.%s",
2466 2530 add_star ? "*" : "", tmp, key + 3);
2467 2531 return alloc_string(buf);
2468 2532 }
2469 2533
2470 2534 tmp = expr_to_var_sym(arg, sym);
2471 2535 if (!tmp)
2472 2536 return NULL;
2473 2537 snprintf(buf, sizeof(buf), "%s%s%s", add_star ? "*" : "", tmp, key + 1);
2474 2538 free_string(tmp);
2475 2539 return alloc_string(buf);
2476 2540 }
2477 2541
2478 2542 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2479 2543 {
2480 2544 *vsl = NULL;
2481 2545
2482 2546 if (strcmp("$", key) == 0)
2483 2547 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2484 2548 return get_variable_from_key(arg, key, sym);
2485 2549 }
2486 2550
2487 2551 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2488 2552 {
2489 2553 int name_len;
2490 2554 static char buf[256];
2491 2555 bool add_star = false;
↓ open down ↓ |
173 lines elided |
↑ open up ↑ |
2492 2556
2493 2557 name_len = strlen(param_name);
2494 2558
2495 2559 if (state_name[0] == '*') {
2496 2560 add_star = true;
2497 2561 state_name++;
2498 2562 }
2499 2563
2500 2564 if (strcmp(state_name, param_name) == 0) {
2501 2565 snprintf(buf, sizeof(buf), "%s$", add_star ? "*" : "");
2502 - return buf;
2566 + return alloc_sname(buf);
2503 2567 }
2504 2568
2505 2569 if (state_name[name_len] == '-' && /* check for '-' from "->" */
2506 2570 strncmp(state_name, param_name, name_len) == 0) {
2507 2571 snprintf(buf, sizeof(buf), "%s$%s",
2508 2572 add_star ? "*" : "", state_name + name_len);
2509 - return buf;
2573 + return alloc_sname(buf);
2510 2574 }
2511 2575 return NULL;
2512 2576 }
2513 2577
2514 2578 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2515 2579 {
2516 2580 if (!sym || !sym->ident)
2517 2581 return NULL;
2518 2582
2519 2583 return state_name_to_param_name(name, sym->ident->name);
2520 2584 }
2521 2585
2522 2586 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2523 2587 {
2524 2588 struct symbol *type;
2525 2589 const char *sym_name;
2526 2590 int name_len;
2527 2591 static char buf[256];
2528 2592
2529 2593 /*
2530 2594 * mtag_name is different from param_name because mtags can be a struct
2531 2595 * instead of a struct pointer. But we want to treat it like a pointer
2532 2596 * because really an mtag is a pointer. Or in other words, if you pass
2533 2597 * a struct foo then you want to talk about foo.bar but with an mtag
2534 2598 * you want to refer to it as foo->bar.
2535 2599 *
2536 2600 */
2537 2601
2538 2602 if (!sym || !sym->ident)
2539 2603 return NULL;
2540 2604
2541 2605 type = get_real_base_type(sym);
2542 2606 if (type && type->type == SYM_BASETYPE)
2543 2607 return "*$";
2544 2608
2545 2609 sym_name = sym->ident->name;
2546 2610 name_len = strlen(sym_name);
2547 2611
2548 2612 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2549 2613 strncmp(state_name, sym_name, name_len) == 0) {
2550 2614 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2551 2615 return buf;
2552 2616 }
2553 2617
2554 2618 return state_name_to_param_name(state_name, sym_name);
2555 2619 }
2556 2620
2557 2621 const char *get_mtag_name_expr(struct expression *expr)
2558 2622 {
2559 2623 char *name;
2560 2624 struct symbol *sym;
2561 2625 const char *ret = NULL;
2562 2626
2563 2627 name = expr_to_var_sym(expr, &sym);
2564 2628 if (!name || !sym)
2565 2629 goto free;
2566 2630
2567 2631 ret = get_mtag_name_var_sym(name, sym);
2568 2632 free:
2569 2633 free_string(name);
2570 2634 return ret;
2571 2635 }
2572 2636
2573 2637 const char *get_param_name(struct sm_state *sm)
2574 2638 {
2575 2639 return get_param_name_var_sym(sm->name, sm->sym);
2576 2640 }
2577 2641
2578 2642 char *get_data_info_name(struct expression *expr)
2579 2643 {
2580 2644 struct symbol *sym;
2581 2645 char *name;
2582 2646 char buf[256];
2583 2647 char *ret = NULL;
2584 2648
2585 2649 expr = strip_expr(expr);
2586 2650 name = get_member_name(expr);
2587 2651 if (name)
2588 2652 return name;
2589 2653 name = expr_to_var_sym(expr, &sym);
2590 2654 if (!name || !sym)
2591 2655 goto free;
2592 2656 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2593 2657 goto free;
2594 2658 if (sym->ctype.modifiers & MOD_STATIC)
2595 2659 snprintf(buf, sizeof(buf), "static %s", name);
2596 2660 else
2597 2661 snprintf(buf, sizeof(buf), "global %s", name);
2598 2662 ret = alloc_sname(buf);
2599 2663 free:
2600 2664 free_string(name);
2601 2665 return ret;
2602 2666 }
↓ open down ↓ |
83 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX