units: anything can be saved to a register
[smatch.git] / smatch_kernel_kref_put.c
blob6b3a9ab6877e788b53fef2cced3084d872ecdeee
1 /*
2 * Copyright 2023 Linaro Ltd.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include "smatch.h"
19 #include "smatch_extra.h"
20 #include "smatch_slist.h"
22 static int my_id;
24 static bool has_inc_state(const char *name, struct symbol *sym)
26 static int refcount_id;
27 struct sm_state *sm, *tmp;
29 if (!refcount_id)
30 refcount_id = id_from_name("check_refcount_info");
32 sm = get_sm_state(refcount_id, name, sym);
33 if (!sm)
34 return false;
36 FOR_EACH_PTR(sm->possible, tmp) {
37 if (strcmp(tmp->state->name, "inc") == 0)
38 return true;
40 * &ignore counts as an inc, because that's what happens when
41 * you double increment. Not ideal.
43 if (strcmp(tmp->state->name, "ignore") == 0)
44 return true;
45 } END_FOR_EACH_PTR(tmp);
47 return false;
50 static void match_kref_put(const char *fn, struct expression *call_expr,
51 struct expression *expr, void *_unused)
53 struct expression *data, *release, *fake_call;
54 struct expression_list *args = NULL;
55 struct symbol *sym;
56 char *ref;
59 if (call_expr->type != EXPR_CALL)
60 return;
62 data = get_argument_from_call_expr(call_expr->args, 0);
64 ref = get_name_sym_from_param_key(call_expr, 0, "$->refcount.refs.counter", &sym);
65 if (has_inc_state(ref, sym))
66 return;
68 release = get_argument_from_call_expr(call_expr->args, 1);
70 add_ptr_list(&args, data);
71 fake_call = call_expression(release, args);
72 add_fake_call_after_return(fake_call);
75 void register_kernel_kref_put(int id)
77 if (option_project != PROJ_KERNEL)
78 return;
80 my_id = id;
81 return_implies_state_sval("kref_put", int_one, int_one, &match_kref_put, NULL);