-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_inconsistent_locking.c
164 lines (132 loc) · 4.03 KB
/
check_inconsistent_locking.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (C) 2009 Dan Carpenter.
* Copyright (C) 2019 Oracle.
* Copyright 2024 Linaro Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include <ctype.h>
#include "parse.h"
#include "smatch.h"
#include "smatch_extra.h"
#include "smatch_slist.h"
static int my_id;
STATE(locked);
STATE(unlocked);
static void lock_hook(struct lock_info *info, struct expression *expr, const char *name, struct symbol *sym)
{
set_state(my_id, name, sym, &locked);
}
static void unlock_hook(struct lock_info *info, struct expression *expr, const char *name, struct symbol *sym)
{
set_state(my_id, name, sym, &unlocked);
}
static void clear_hook(struct lock_info *info, struct expression *expr, const char *name, struct symbol *sym)
{
if (!get_state(my_id, name, sym))
return;
set_state(my_id, name, sym, &undefined);
}
static bool is_EINTR(struct range_list *rl)
{
sval_t sval;
if (!rl_to_sval(rl, &sval))
return false;
return sval.value == -4;
}
#define NUM_BUCKETS (RET_UNKNOWN + 1)
static void check_lock(const char *name, struct symbol *sym)
{
struct range_list *locked_lines = NULL;
struct range_list *unlocked_lines = NULL;
int locked_buckets[NUM_BUCKETS] = {};
int unlocked_buckets[NUM_BUCKETS] = {};
struct stree *stree, *orig;
struct sm_state *return_sm;
struct sm_state *sm;
sval_t line = sval_type_val(&int_ctype, 0);
int bucket;
int i;
if (is_locking_primitive_sym(cur_func_sym))
return;
if (strchr(name, '$'))
return;
FOR_EACH_PTR(get_all_return_strees(), stree) {
orig = __swap_cur_stree(stree);
if (is_impossible_path())
goto swap_stree;
return_sm = get_sm_state(RETURN_ID, "return_ranges", NULL);
if (!return_sm)
goto swap_stree;
line.value = return_sm->line;
sm = get_sm_state(my_id, name, sym);
if (!sm)
goto swap_stree;
if (parent_is_gone_var_sym(sm->name, sm->sym))
goto swap_stree;
#if 0
if (sm_both_locked_and_unlocked(sm) && !both_warned) {
sm_printf("%s:%lld %s() warn: XXX '%s' both locked and unlocked.\n",
get_filename(), line.value, get_function(), sm->name);
sm_msg("%s: sm = '%s'", __func__, show_sm(sm));
both_warned = 1;
goto swap_stree;
}
#endif
if (sm->state != &locked && sm->state != &unlocked)
goto swap_stree;
if (sm->state == &unlocked && is_EINTR(estate_rl(return_sm->state)))
goto swap_stree;
bucket = success_fail_return(estate_rl(return_sm->state));
if (sm->state == &locked) {
add_range(&locked_lines, line, line);
locked_buckets[bucket] = true;
} else {
add_range(&unlocked_lines, line, line);
unlocked_buckets[bucket] = true;
}
swap_stree:
__swap_cur_stree(orig);
} END_FOR_EACH_PTR(stree);
if (!locked_lines || !unlocked_lines)
return;
for (i = 0; i < NUM_BUCKETS; i++) {
if (locked_buckets[i] && unlocked_buckets[i])
goto complain;
}
if (locked_buckets[RET_FAIL])
goto complain;
return;
complain:
sm_warning("inconsistent returns '%s'.", name);
sm_printf(" Locked on : %s\n", show_rl(locked_lines));
sm_printf(" Unlocked on: %s\n", show_rl(unlocked_lines));
}
static void match_func_end(struct symbol *sym)
{
struct sm_state *sm;
FOR_EACH_MY_SM(my_id, get_all_return_states(), sm) {
check_lock(sm->name, sm->sym);
} END_FOR_EACH_SM(sm);
}
void check_inconsistent_locking(int id)
{
my_id = id;
add_lock_hook(&lock_hook);
add_unlock_hook(&unlock_hook);
add_restore_hook(&unlock_hook);
add_clear_hook(&clear_hook);
add_modification_hook(my_id, &set_undefined);
add_hook(&match_func_end, END_FUNC_HOOK);
}