db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / check_list_add.c
blobba7c7b43d62b3076599ec5b3501aafb984c5887a
1 /*
2 * Copyright (C) 2020 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 * This is the "strict" version which is more daring and ambitious than
20 * the check_free.c file. The difference is that this looks at split
21 * returns and the other only looks at if every path frees a parameter.
22 * Also this has a bunch of kernel specific things to do with reference
23 * counted memory.
26 #include <string.h>
27 #include "smatch.h"
28 #include "smatch_slist.h"
29 #include "smatch_extra.h"
31 static int my_id;
32 static int next_id;
34 STATE(added);
35 STATE(trigger);
37 static unsigned long silenced;
39 struct statement *ignored_stmt;
40 static void trigger_list_del(struct sm_state *sm, struct expression *mod_expr)
42 char buf[64];
43 char *p;
45 if (__cur_stmt == ignored_stmt)
46 return;
48 snprintf(buf, sizeof(buf), "&%s", sm->name);
49 p = strstr(buf, ".next");
50 if (!p)
51 return;
52 *p = '\0';
53 set_state(my_id, buf, sm->sym, &undefined);
56 static void set_up_next_trigger(struct expression *expr)
58 struct symbol *type, *member;
59 struct expression *next;
61 type = get_pointer_type(expr);
62 if (!type || type->type != SYM_STRUCT)
63 return;
64 member = first_ptr_list((struct ptr_list *)type->symbol_list);
65 if (!member || !member->ident ||
66 strcmp(member->ident->name, "next") != 0)
67 return;
69 if (expr->type == EXPR_PREOP && expr->op == '&')
70 expr = expr->unop;
72 next = member_expression(expr, '.', member->ident);
73 set_state_expr(next_id, next, &trigger);
74 ignored_stmt = __cur_stmt;
77 static void match_add(const char *fn, struct expression *expr, void *param)
79 struct expression *arg;
81 arg = get_argument_from_call_expr(expr->args, 0);
82 set_state_expr(my_id, arg, &added);
83 set_up_next_trigger(arg);
86 static void match_del(const char *fn, struct expression *expr, void *param)
88 struct expression *arg;
90 arg = get_argument_from_call_expr(expr->args, 0);
91 if (!get_state_expr(my_id, arg)) {
92 silenced = true;
93 return;
95 set_state_expr(my_id, arg, &undefined);
98 static void match_free(const char *fn, struct expression *expr, void *param)
100 struct expression *arg;
101 struct sm_state *sm;
102 char *name;
103 struct symbol *sym;
105 if (silenced)
106 return;
108 arg = get_argument_from_call_expr(expr->args, PTR_INT(param));
109 if (!arg)
110 return;
111 name = expr_to_var_sym(arg, &sym);
112 if (!name || !sym)
113 goto free;
115 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
116 if (sm->state == &undefined)
117 continue;
118 if (sm->sym != sym)
119 continue;
120 if (strncmp(sm->name + 1, name, strlen(name)) != 0)
121 continue;
122 sm_warning("'%s' not removed from list", sm->name);
123 goto free;
124 } END_FOR_EACH_SM(sm);
125 free:
126 free_string(name);
129 void check_list_add(int id)
131 my_id = id;
133 if (option_project != PROJ_KERNEL)
134 return;
136 add_function_data(&silenced);
138 add_function_hook("list_add", &match_add, NULL);
139 add_function_hook("list_add_tail", &match_add, NULL);
140 add_function_hook("list_del", &match_del, NULL);
142 add_modification_hook(my_id, &set_undefined);
144 add_function_hook("kfree", &match_free, 0);
147 void check_list_add_late(int id)
149 next_id = id;
150 add_modification_hook(next_id, &trigger_list_del);