locking: remove release_sock()
[smatch.git] / smatch_dereference.c
blob0e9dcc23b87921e5b2cbd4288beeb700c24bd203
1 /*
2 * Copyright (C) 2021 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
18 #include "smatch.h"
19 #include "smatch_extra.h"
21 static int my_id;
22 static struct expr_fn_list *deref_hooks;
24 struct deref_info {
25 const char *name;
26 int param;
27 const char *key;
28 const sval_t *implies_start, *implies_end;
30 static struct deref_info fn_deref_table[] = {
31 { "nla_data", 0, "$" },
32 { "strlen", 0, "$" },
33 { "__builtin_strlen", 0, "$" },
34 { "__fortify_strlen", 0, "$" },
37 void add_dereference_hook(expr_func *fn)
39 add_ptr_list(&deref_hooks, fn);
42 static void call_deref_hooks(struct expression *expr)
44 if (__in_fake_assign || __in_fake_parameter_assign)
45 return;
46 if (!expr)
47 return;
49 call_expr_fns(deref_hooks, expr);
52 static void match_dereference(struct expression *expr)
54 struct expression *p, *tmp;
55 struct symbol *type;
57 if (expr->type != EXPR_PREOP ||
58 expr->op != '*')
59 return;
60 p = strip_expr(expr->unop);
61 type = get_type(p);
62 if (!type || type->type != SYM_PTR)
63 return;
64 call_deref_hooks(p);
66 tmp = get_assigned_expr(p);
67 if (!tmp)
68 return;
70 * Imagine we have:
71 * p = &foo->bar;
72 * x = p->whatever;
74 * Note that we only care about address assignments because other
75 * dereferences would have been handled already.
77 if (tmp->type != EXPR_PREOP || tmp->op != '&')
78 return;
79 p = strip_expr(tmp->unop);
80 if (p->type != EXPR_DEREF)
81 return;
82 p = strip_expr(p->deref);
83 if (p->type != EXPR_PREOP || p->op != '*')
84 return;
85 p = strip_expr(p->unop);
86 type = get_type(p);
87 if (!type || type->type != SYM_PTR)
88 return;
89 call_deref_hooks(p);
92 static void match_pointer_as_array(struct expression *expr)
94 struct expression *array;
95 struct symbol *type;
97 if (!is_array(expr))
98 return;
99 if (getting_address(expr))
100 return;
102 array = get_array_base(expr);
103 type = get_type(array);
104 if (!type || type->type != SYM_PTR)
105 return;
107 call_deref_hooks(array);
110 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
112 struct expression *deref;
114 deref = gen_expression_from_key(arg, key);
115 if (!deref)
116 return;
118 call_deref_hooks(deref);
121 static void param_deref(struct expression *expr)
123 call_deref_hooks(expr);
126 void register_dereferences(int id)
128 struct deref_info *info;
129 int i;
131 my_id = id;
133 add_hook(&match_dereference, DEREF_HOOK);
134 add_hook(&match_pointer_as_array, OP_HOOK);
135 select_return_implies_hook_early(DEREFERENCE, &set_param_dereferenced);
137 for (i = 0; i < ARRAY_SIZE(fn_deref_table); i++) {
138 info = &fn_deref_table[i];
139 add_param_key_expr_hook(info->name, &param_deref, info->param, info->key, info);