implied: re-order where we calculate implications
[smatch.git] / check_locking.c
blobd74ba9e032de2d49ce0a55766af520b6dd6dfa3d
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
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 test checks that locks are held the same across all returns.
21 * Of course, some functions are designed to only hold the locks on success.
22 * Oh well... We can rewrite it later if we want.
24 * The list of wine locking functions came from an earlier script written
25 * by Michael Stefaniuc.
29 #include "parse.h"
30 #include "smatch.h"
31 #include "smatch_extra.h"
32 #include "smatch_slist.h"
34 static int my_id;
36 static int func_has_transition;
38 STATE(locked);
39 STATE(start_state);
40 STATE(unlocked);
42 enum action {
43 LOCK,
44 UNLOCK,
47 enum return_type {
48 ret_any,
49 ret_non_zero,
50 ret_zero,
51 ret_negative,
52 ret_positive,
55 #define RETURN_VAL -1
56 #define NO_ARG -2
58 struct lock_info {
59 const char *function;
60 enum action action;
61 const char *name;
62 int arg;
63 enum return_type return_type;
66 static struct lock_info wine_lock_table[] = {
67 {"create_window_handle", LOCK, "create_window_handle", RETURN_VAL, ret_non_zero},
68 {"WIN_GetPtr", LOCK, "create_window_handle", RETURN_VAL, ret_non_zero},
69 {"WIN_ReleasePtr", UNLOCK, "create_window_handle", 0, ret_any},
70 {"EnterCriticalSection", LOCK, "CriticalSection", 0, ret_any},
71 {"LeaveCriticalSection", UNLOCK, "CriticalSection", 0, ret_any},
72 {"RtlEnterCriticalSection", LOCK, "RtlCriticalSection", 0, ret_any},
73 {"RtlLeaveCriticalSection", UNLOCK, "RtlCriticalSection", 0, ret_any},
74 {"GDI_GetObjPtr", LOCK, "GDI_Get", 0, ret_non_zero},
75 {"GDI_ReleaseObj", UNLOCK, "GDI_Get", 0, ret_any},
76 {"LdrLockLoaderLock", LOCK, "LdrLockLoaderLock", 2, ret_any},
77 {"LdrUnlockLoaderLock", UNLOCK, "LdrLockLoaderLock", 1, ret_any},
78 {"_lock", LOCK, "_lock", 0, ret_any},
79 {"_unlock", UNLOCK, "_lock", 0, ret_any},
80 {"msiobj_lock", LOCK, "msiobj_lock", 0, ret_any},
81 {"msiobj_unlock", UNLOCK, "msiobj_lock", 0, ret_any},
82 {"RtlAcquirePebLock", LOCK, "PebLock", NO_ARG, ret_any},
83 {"RtlReleasePebLock", UNLOCK, "PebLock", NO_ARG, ret_any},
84 {"server_enter_uninterrupted_section", LOCK, "server_uninterrupted_section", 0, ret_any},
85 {"server_leave_uninterrupted_section", UNLOCK, "server_uninterrupted_section", 0, ret_any},
86 {"RtlLockHeap", LOCK, "RtlLockHeap", 0, ret_any},
87 {"RtlUnlockHeap", UNLOCK, "RtlLockHeap", 0, ret_any},
88 {"_EnterSysLevel", LOCK, "SysLevel", 0, ret_any},
89 {"_LeaveSysLevel", UNLOCK, "SysLevel", 0, ret_any},
90 {"USER_Lock", LOCK, "USER_Lock", NO_ARG, ret_any},
91 {"USER_Unlock", UNLOCK, "USER_Lock", NO_ARG, ret_any},
92 {"wine_tsx11_lock", LOCK, "wine_tsx11_lock", NO_ARG, ret_any},
93 {"wine_tsx11_unlock", UNLOCK, "wine_tsx11_lock", NO_ARG, ret_any},
94 {"wine_tsx11_lock_ptr", LOCK, "wine_tsx11_lock_ptr", NO_ARG, ret_any},
95 {"wine_tsx11_unlock_ptr", UNLOCK, "wine_tsx11_lock_ptr", NO_ARG, ret_any},
96 {"wined3d_mutex_lock", LOCK, "wined3d_mutex_lock", NO_ARG, ret_any},
97 {"wined3d_mutex_unlock", UNLOCK, "wined3d_mutex_lock", NO_ARG, ret_any},
98 {"X11DRV_DIB_Lock", LOCK, "X11DRV_DIB_Lock", 0, ret_any},
99 {"X11DRV_DIB_Unlock", UNLOCK, "X11DRV_DIB_Lock", 0, ret_any},
102 static struct lock_info kernel_lock_table[] = {
103 {"lock_kernel", LOCK, "BKL", NO_ARG, ret_any},
104 {"unlock_kernel", UNLOCK, "BKL", NO_ARG, ret_any},
106 {"spin_lock", LOCK, "spin_lock", 0, ret_any},
107 {"spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
108 {"spin_lock_nested", LOCK, "spin_lock", 0, ret_any},
109 {"_spin_lock", LOCK, "spin_lock", 0, ret_any},
110 {"_spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
111 {"_spin_lock_nested", LOCK, "spin_lock", 0, ret_any},
112 {"__spin_lock", LOCK, "spin_lock", 0, ret_any},
113 {"__spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
114 {"__spin_lock_nested", LOCK, "spin_lock", 0, ret_any},
115 {"raw_spin_lock", LOCK, "spin_lock", 0, ret_any},
116 {"raw_spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
117 {"_raw_spin_lock", LOCK, "spin_lock", 0, ret_any},
118 {"_raw_spin_lock_nested", LOCK, "spin_lock", 0, ret_any},
119 {"_raw_spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
120 {"__raw_spin_lock", LOCK, "spin_lock", 0, ret_any},
121 {"__raw_spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
123 {"spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
124 {"spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
125 {"_spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
126 {"_spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
127 {"__spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
128 {"__spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
129 {"_raw_spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
130 {"_raw_spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
131 {"__raw_spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
132 {"spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
133 {"spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
134 {"_spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
135 {"_spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
136 {"__spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
137 {"__spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
138 {"_raw_spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
139 {"_raw_spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
140 {"__raw_spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
141 {"__raw_spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
142 {"spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
143 {"_spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
144 {"__spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
145 {"_raw_spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
146 {"spin_lock_bh", LOCK, "spin_lock", 0, ret_any},
147 {"spin_unlock_bh", UNLOCK, "spin_lock", 0, ret_any},
148 {"_spin_lock_bh", LOCK, "spin_lock", 0, ret_any},
149 {"_spin_unlock_bh", UNLOCK, "spin_lock", 0, ret_any},
150 {"__spin_lock_bh", LOCK, "spin_lock", 0, ret_any},
151 {"__spin_unlock_bh", UNLOCK, "spin_lock", 0, ret_any},
153 {"spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
154 {"_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
155 {"__spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
156 {"raw_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
157 {"_raw_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
158 {"spin_trylock_irq", LOCK, "spin_lock", 0, ret_non_zero},
159 {"spin_trylock_irqsave", LOCK, "spin_lock", 0, ret_non_zero},
160 {"spin_trylock_bh", LOCK, "spin_lock", 0, ret_non_zero},
161 {"_spin_trylock_bh", LOCK, "spin_lock", 0, ret_non_zero},
162 {"__spin_trylock_bh", LOCK, "spin_lock", 0, ret_non_zero},
163 {"__raw_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
164 {"_atomic_dec_and_lock", LOCK, "spin_lock", 1, ret_non_zero},
166 {"read_lock", LOCK, "read_lock", 0, ret_any},
167 {"read_unlock", UNLOCK, "read_lock", 0, ret_any},
168 {"_read_lock", LOCK, "read_lock", 0, ret_any},
169 {"_read_unlock", UNLOCK, "read_lock", 0, ret_any},
170 {"__read_lock", LOCK, "read_lock", 0, ret_any},
171 {"__read_unlock", UNLOCK, "read_lock", 0, ret_any},
172 {"_raw_read_lock", LOCK, "read_lock", 0, ret_any},
173 {"_raw_read_unlock", UNLOCK, "read_lock", 0, ret_any},
174 {"__raw_read_lock", LOCK, "read_lock", 0, ret_any},
175 {"__raw_read_unlock", UNLOCK, "read_lock", 0, ret_any},
176 {"read_lock_irq", LOCK, "read_lock", 0, ret_any},
177 {"read_unlock_irq" , UNLOCK, "read_lock", 0, ret_any},
178 {"_read_lock_irq", LOCK, "read_lock", 0, ret_any},
179 {"_read_unlock_irq", UNLOCK, "read_lock", 0, ret_any},
180 {"__read_lock_irq", LOCK, "read_lock", 0, ret_any},
181 {"__read_unlock_irq", UNLOCK, "read_lock", 0, ret_any},
182 {"read_lock_irqsave", LOCK, "read_lock", 0, ret_any},
183 {"read_unlock_irqrestore", UNLOCK, "read_lock", 0, ret_any},
184 {"_read_lock_irqsave", LOCK, "read_lock", 0, ret_any},
185 {"_read_unlock_irqrestore", UNLOCK, "read_lock", 0, ret_any},
186 {"__read_lock_irqsave", LOCK, "read_lock", 0, ret_any},
187 {"__read_unlock_irqrestore", UNLOCK, "read_lock", 0, ret_any},
188 {"read_lock_bh", LOCK, "read_lock", 0, ret_any},
189 {"read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
190 {"_read_lock_bh", LOCK, "read_lock", 0, ret_any},
191 {"_read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
192 {"__read_lock_bh", LOCK, "read_lock", 0, ret_any},
193 {"__read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
194 {"_raw_read_lock_bh", LOCK, "read_lock", 0, ret_any},
195 {"_raw_read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
196 {"__raw_read_lock_bh", LOCK, "read_lock", 0, ret_any},
197 {"__raw_read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
199 {"generic__raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
200 {"read_trylock", LOCK, "read_lock", 0, ret_non_zero},
201 {"_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
202 {"raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
203 {"_raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
204 {"__raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
205 {"__read_trylock", LOCK, "read_lock", 0, ret_non_zero},
207 {"write_lock", LOCK, "write_lock", 0, ret_any},
208 {"write_unlock", UNLOCK, "write_lock", 0, ret_any},
209 {"_write_lock", LOCK, "write_lock", 0, ret_any},
210 {"_write_unlock", UNLOCK, "write_lock", 0, ret_any},
211 {"__write_lock", LOCK, "write_lock", 0, ret_any},
212 {"__write_unlock", UNLOCK, "write_lock", 0, ret_any},
213 {"write_lock_irq", LOCK, "write_lock", 0, ret_any},
214 {"write_unlock_irq", UNLOCK, "write_lock", 0, ret_any},
215 {"_write_lock_irq", LOCK, "write_lock", 0, ret_any},
216 {"_write_unlock_irq", UNLOCK, "write_lock", 0, ret_any},
217 {"__write_lock_irq", LOCK, "write_lock", 0, ret_any},
218 {"__write_unlock_irq", UNLOCK, "write_lock", 0, ret_any},
219 {"write_lock_irqsave", LOCK, "write_lock", 0, ret_any},
220 {"write_unlock_irqrestore", UNLOCK, "write_lock", 0, ret_any},
221 {"_write_lock_irqsave", LOCK, "write_lock", 0, ret_any},
222 {"_write_unlock_irqrestore", UNLOCK, "write_lock", 0, ret_any},
223 {"__write_lock_irqsave", LOCK, "write_lock", 0, ret_any},
224 {"__write_unlock_irqrestore", UNLOCK, "write_lock", 0, ret_any},
225 {"write_lock_bh", LOCK, "write_lock", 0, ret_any},
226 {"write_unlock_bh", UNLOCK, "write_lock", 0, ret_any},
227 {"_write_lock_bh", LOCK, "write_lock", 0, ret_any},
228 {"_write_unlock_bh", UNLOCK, "write_lock", 0, ret_any},
229 {"__write_lock_bh", LOCK, "write_lock", 0, ret_any},
230 {"__write_unlock_bh", UNLOCK, "write_lock", 0, ret_any},
231 {"_raw_write_lock", LOCK, "write_lock", 0, ret_any},
232 {"__raw_write_lock", LOCK, "write_lock", 0, ret_any},
233 {"_raw_write_unlock", UNLOCK, "write_lock", 0, ret_any},
234 {"__raw_write_unlock", UNLOCK, "write_lock", 0, ret_any},
236 {"write_trylock", LOCK, "write_lock", 0, ret_non_zero},
237 {"_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
238 {"raw_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
239 {"_raw_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
240 {"__write_trylock", LOCK, "write_lock", 0, ret_non_zero},
241 {"__raw_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
243 {"down", LOCK, "sem", 0, ret_any},
244 {"up", UNLOCK, "sem", 0, ret_any},
245 {"down_trylock", LOCK, "sem", 0, ret_zero},
246 {"down_timeout", LOCK, "sem", 0, ret_zero},
247 {"down_interruptible", LOCK, "sem", 0, ret_zero},
249 {"mutex_lock", LOCK, "mutex", 0, ret_any},
250 {"mutex_unlock", UNLOCK, "mutex", 0, ret_any},
251 {"mutex_lock_nested", LOCK, "mutex", 0, ret_any},
253 {"mutex_lock_interruptible", LOCK, "mutex", 0, ret_zero},
254 {"mutex_lock_interruptible_nested", LOCK, "mutex", 0, ret_zero},
255 {"mutex_lock_killable", LOCK, "mutex", 0, ret_zero},
256 {"mutex_lock_killable_nested", LOCK, "mutex", 0, ret_zero},
258 {"mutex_trylock", LOCK, "mutex", 0, ret_non_zero},
260 {"raw_local_irq_disable", LOCK, "irq", NO_ARG, ret_any},
261 {"raw_local_irq_enable", UNLOCK, "irq", NO_ARG, ret_any},
262 {"spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
263 {"spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
264 {"_spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
265 {"_spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
266 {"__spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
267 {"__spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
268 {"_raw_spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
269 {"_raw_spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
270 {"__raw_spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
271 {"spin_trylock_irq", LOCK, "irq", NO_ARG, ret_non_zero},
272 {"read_lock_irq", LOCK, "irq", NO_ARG, ret_any},
273 {"read_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
274 {"_read_lock_irq", LOCK, "irq", NO_ARG, ret_any},
275 {"_read_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
276 {"__read_lock_irq", LOCK, "irq", NO_ARG, ret_any},
277 {"__read_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
278 {"write_lock_irq", LOCK, "irq", NO_ARG, ret_any},
279 {"write_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
280 {"_write_lock_irq", LOCK, "irq", NO_ARG, ret_any},
281 {"_write_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
282 {"__write_lock_irq", LOCK, "irq", NO_ARG, ret_any},
283 {"__write_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
285 {"arch_local_irq_save", LOCK, "irqsave", RETURN_VAL, ret_any},
286 {"arch_local_irq_restore", UNLOCK, "irqsave", 0, ret_any},
287 {"__raw_local_irq_save", LOCK, "irqsave", RETURN_VAL, ret_any},
288 {"raw_local_irq_restore", UNLOCK, "irqsave", 0, ret_any},
289 {"spin_lock_irqsave_nested", LOCK, "irqsave", RETURN_VAL, ret_any},
290 {"spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
291 {"spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
292 {"spin_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
293 {"_spin_lock_irqsave_nested", LOCK, "irqsave", RETURN_VAL, ret_any},
294 {"_spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
295 {"_spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
296 {"_spin_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
297 {"__spin_lock_irqsave_nested", LOCK, "irqsave", 1, ret_any},
298 {"__spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
299 {"__spin_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
300 {"_raw_spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
301 {"_raw_spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
302 {"_raw_spin_unlock_irqrestore",UNLOCK, "irqsave", 1, ret_any},
303 {"__raw_spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
304 {"__raw_spin_unlock_irqrestore",UNLOCK, "irqsave", 1, ret_any},
305 {"_raw_spin_lock_irqsave_nested", LOCK, "irqsave", RETURN_VAL, ret_any},
306 {"spin_trylock_irqsave", LOCK, "irqsave", 1, ret_non_zero},
307 {"read_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
308 {"read_lock_irqsave", LOCK, "irqsave", 1, ret_any},
309 {"read_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
310 {"_read_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
311 {"_read_lock_irqsave", LOCK, "irqsave", 1, ret_any},
312 {"_read_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
313 {"__read_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
314 {"__read_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
315 {"write_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
316 {"write_lock_irqsave", LOCK, "irqsave", 1, ret_any},
317 {"write_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
318 {"_write_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
319 {"_write_lock_irqsave", LOCK, "irqsave", 1, ret_any},
320 {"_write_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
321 {"__write_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
322 {"__write_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
324 {"local_bh_disable", LOCK, "bottom_half", NO_ARG, ret_any},
325 {"_local_bh_disable", LOCK, "bottom_half", NO_ARG, ret_any},
326 {"__local_bh_disable", LOCK, "bottom_half", NO_ARG, ret_any},
327 {"local_bh_enable", UNLOCK, "bottom_half", NO_ARG, ret_any},
328 {"_local_bh_enable", UNLOCK, "bottom_half", NO_ARG, ret_any},
329 {"__local_bh_enable", UNLOCK, "bottom_half", NO_ARG, ret_any},
330 {"spin_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
331 {"spin_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
332 {"_spin_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
333 {"_spin_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
334 {"__spin_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
335 {"__spin_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
336 {"read_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
337 {"read_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
338 {"_read_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
339 {"_read_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
340 {"__read_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
341 {"__read_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
342 {"_raw_read_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
343 {"_raw_read_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
344 {"write_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
345 {"write_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
346 {"_write_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
347 {"_write_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
348 {"__write_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
349 {"__write_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
350 {"spin_trylock_bh", LOCK, "bottom_half", NO_ARG, ret_non_zero},
351 {"_spin_trylock_bh", LOCK, "bottom_half", NO_ARG, ret_non_zero},
352 {"__spin_trylock_bh", LOCK, "bottom_half", NO_ARG, ret_non_zero},
354 {"ffs_mutex_lock", LOCK, "mutex", 0, ret_zero},
357 static struct lock_info *lock_table;
359 static struct tracker_list *starts_locked;
360 static struct tracker_list *starts_unlocked;
362 struct locks_on_return {
363 int line;
364 struct tracker_list *locked;
365 struct tracker_list *unlocked;
366 struct range_list *return_values;
368 DECLARE_PTR_LIST(return_list, struct locks_on_return);
369 static struct return_list *all_returns;
371 static char *make_full_name(const char *lock, const char *var)
373 static char tmp_buf[512];
375 snprintf(tmp_buf, sizeof(tmp_buf), "%s:%s", lock, var);
376 remove_parens(tmp_buf);
377 return alloc_string(tmp_buf);
380 static struct expression *remove_spinlock_check(struct expression *expr)
382 if (expr->type != EXPR_CALL)
383 return expr;
384 if (expr->fn->type != EXPR_SYMBOL)
385 return expr;
386 if (strcmp(expr->fn->symbol_name->name, "spinlock_check"))
387 return expr;
388 expr = get_argument_from_call_expr(expr->args, 0);
389 return expr;
392 static char *get_full_name(struct expression *expr, int index)
394 struct expression *arg;
395 char *name = NULL;
396 char *full_name = NULL;
397 struct lock_info *lock = &lock_table[index];
399 if (lock->arg == RETURN_VAL) {
400 name = expr_to_var(expr->left);
401 full_name = make_full_name(lock->name, name);
402 } else if (lock->arg == NO_ARG) {
403 full_name = make_full_name(lock->name, "");
404 } else {
405 arg = get_argument_from_call_expr(expr->args, lock->arg);
406 if (!arg)
407 goto free;
408 arg = remove_spinlock_check(arg);
409 name = expr_to_str(arg);
410 if (!name)
411 goto free;
412 full_name = make_full_name(lock->name, name);
414 free:
415 free_string(name);
416 return full_name;
419 static struct smatch_state *get_start_state(struct sm_state *sm)
421 int is_locked = 0;
422 int is_unlocked = 0;
424 if (in_tracker_list(starts_locked, my_id, sm->name, sm->sym))
425 is_locked = 1;
426 if (in_tracker_list(starts_unlocked, my_id, sm->name, sm->sym))
427 is_unlocked = 1;
428 if (is_locked && is_unlocked)
429 return &undefined;
430 if (is_locked)
431 return &locked;
432 if (is_unlocked)
433 return &unlocked;
434 return &undefined;
437 static struct smatch_state *unmatched_state(struct sm_state *sm)
439 return &start_state;
442 static void do_lock(const char *name)
444 struct sm_state *sm;
446 if (__inline_fn)
447 return;
449 sm = get_sm_state(my_id, name, NULL);
450 if (!sm)
451 add_tracker(&starts_unlocked, my_id, name, NULL);
452 if (sm && slist_has_state(sm->possible, &locked) &&
453 strcmp(name, "bottom_half:") != 0)
454 sm_msg("error: double lock '%s'", name);
455 if (sm)
456 func_has_transition = TRUE;
457 set_state(my_id, name, NULL, &locked);
460 static void do_lock_failed(const char *name)
462 struct sm_state *sm;
464 if (__inline_fn)
465 return;
467 sm = get_sm_state(my_id, name, NULL);
468 if (!sm)
469 add_tracker(&starts_unlocked, my_id, name, NULL);
470 set_state(my_id, name, NULL, &unlocked);
473 static void do_unlock(const char *name)
475 struct sm_state *sm;
477 if (__inline_fn)
478 return;
479 if (__path_is_null())
480 return;
481 sm = get_sm_state(my_id, name, NULL);
482 if (!sm)
483 add_tracker(&starts_locked, my_id, name, NULL);
484 if (sm && slist_has_state(sm->possible, &unlocked) &&
485 strcmp(name, "bottom_half:") != 0)
486 sm_msg("error: double unlock '%s'", name);
487 if (sm)
488 func_has_transition = TRUE;
489 set_state(my_id, name, NULL, &unlocked);
492 static void match_lock_held(const char *fn, struct expression *call_expr,
493 struct expression *assign_expr, void *_index)
495 int index = PTR_INT(_index);
496 char *lock_name;
497 struct lock_info *lock = &lock_table[index];
499 if (lock->arg == NO_ARG) {
500 lock_name = get_full_name(NULL, index);
501 } else if (lock->arg == RETURN_VAL) {
502 if (!assign_expr)
503 return;
504 lock_name = get_full_name(assign_expr, index);
505 } else {
506 lock_name = get_full_name(call_expr, index);
508 if (!lock_name)
509 return;
510 do_lock(lock_name);
511 free_string(lock_name);
514 static void match_lock_failed(const char *fn, struct expression *call_expr,
515 struct expression *assign_expr, void *_index)
517 int index = PTR_INT(_index);
518 char *lock_name;
519 struct lock_info *lock = &lock_table[index];
521 if (lock->arg == NO_ARG) {
522 lock_name = get_full_name(NULL, index);
523 } else if (lock->arg == RETURN_VAL) {
524 if (!assign_expr)
525 return;
526 lock_name = get_full_name(assign_expr, index);
527 } else {
528 lock_name = get_full_name(call_expr, index);
530 if (!lock_name)
531 return;
532 do_lock_failed(lock_name);
533 free_string(lock_name);
536 static void match_returns_locked(const char *fn, struct expression *expr,
537 void *_index)
539 char *full_name = NULL;
540 int index = PTR_INT(_index);
541 struct lock_info *lock = &lock_table[index];
543 if (lock->arg != RETURN_VAL)
544 return;
545 full_name = get_full_name(expr, index);
546 do_lock(full_name);
549 static void match_lock_unlock(const char *fn, struct expression *expr, void *_index)
551 char *full_name = NULL;
552 int index = PTR_INT(_index);
553 struct lock_info *lock = &lock_table[index];
555 if (__inline_fn)
556 return;
558 full_name = get_full_name(expr, index);
559 if (!full_name)
560 return;
561 if (lock->action == LOCK)
562 do_lock(full_name);
563 else
564 do_unlock(full_name);
565 free_string(full_name);
568 static struct locks_on_return *alloc_return(struct expression *expr)
570 struct locks_on_return *ret;
572 ret = malloc(sizeof(*ret));
573 if (!get_implied_rl(expr, &ret->return_values))
574 ret->return_values = NULL;
575 ret->line = get_lineno();
576 ret->locked = NULL;
577 ret->unlocked = NULL;
578 return ret;
581 static int check_possible(struct sm_state *sm)
583 struct sm_state *tmp;
584 int islocked = 0;
585 int isunlocked = 0;
586 int undef = 0;
588 if (!option_spammy)
589 return 0;
591 FOR_EACH_PTR(sm->possible, tmp) {
592 if (tmp->state == &locked)
593 islocked = 1;
594 if (tmp->state == &unlocked)
595 isunlocked = 1;
596 if (tmp->state == &start_state) {
597 struct smatch_state *s;
599 s = get_start_state(tmp);
600 if (s == &locked)
601 islocked = 1;
602 else if (s == &unlocked)
603 isunlocked = 1;
604 else
605 undef = 1;
607 if (tmp->state == &undefined)
608 undef = 1; // i don't think this is possible any more.
609 } END_FOR_EACH_PTR(tmp);
610 if ((islocked && isunlocked) || undef) {
611 sm_msg("warn: '%s' is sometimes locked here and sometimes unlocked.", sm->name);
612 return 1;
614 return 0;
617 static struct position warned_pos;
619 static void match_return(int return_id, char *return_ranges, struct expression *expr)
621 struct locks_on_return *ret;
622 struct stree *stree;
623 struct sm_state *tmp;
625 if (!final_pass)
626 return;
627 if (__inline_fn)
628 return;
630 if (expr && cmp_pos(expr->pos, warned_pos) == 0)
631 return;
633 ret = alloc_return(expr);
635 stree = __get_cur_stree();
636 FOR_EACH_MY_SM(my_id, stree, tmp) {
637 if (tmp->state == &locked) {
638 add_tracker(&ret->locked, tmp->owner, tmp->name,
639 tmp->sym);
640 } else if (tmp->state == &unlocked) {
641 add_tracker(&ret->unlocked, tmp->owner, tmp->name,
642 tmp->sym);
643 } else if (tmp->state == &start_state) {
644 struct smatch_state *s;
646 s = get_start_state(tmp);
647 if (s == &locked)
648 add_tracker(&ret->locked, tmp->owner, tmp->name,
649 tmp->sym);
650 if (s == &unlocked)
651 add_tracker(&ret->unlocked, tmp->owner,tmp->name,
652 tmp->sym);
653 } else {
654 if (check_possible(tmp)) {
655 if (expr)
656 warned_pos = expr->pos;
659 } END_FOR_EACH_SM(tmp);
660 add_ptr_list(&all_returns, ret);
663 static void add_line(struct range_list **rl, int line)
665 sval_t sval = sval_type_val(&int_ctype, line);
667 add_range(rl, sval, sval);
670 static int line_printed(struct range_list *rl, int line)
672 sval_t sval = sval_type_val(&int_ctype, line);
674 return rl_has_sval(rl, sval);
677 static void print_inconsistent_returns(struct tracker *lock,
678 struct smatch_state *start)
680 struct locks_on_return *tmp;
681 struct range_list *printed = NULL;
682 int i;
684 sm_msg("warn: inconsistent returns '%s'.", lock->name);
685 sm_printf(" Locked on: ");
687 i = 0;
688 FOR_EACH_PTR(all_returns, tmp) {
689 if (line_printed(printed, tmp->line))
690 continue;
691 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name, lock->sym))
692 continue;
693 if (in_tracker_list(tmp->locked, lock->owner, lock->name, lock->sym)) {
694 if (i++)
695 sm_printf(" ");
696 sm_printf("line %d\n", tmp->line);
697 add_line(&printed, tmp->line);
698 continue;
700 if (start == &locked) {
701 if (i++)
702 sm_printf(" ");
703 sm_printf("line %d\n", tmp->line);
704 add_line(&printed, tmp->line);
706 } END_FOR_EACH_PTR(tmp);
708 sm_printf(" Unlocked on: ");
709 printed = NULL;
710 i = 0;
711 FOR_EACH_PTR(all_returns, tmp) {
712 if (line_printed(printed, tmp->line))
713 continue;
714 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name, lock->sym)) {
715 if (i++)
716 sm_printf(" ");
717 sm_printf("line %d\n", tmp->line);
718 add_line(&printed, tmp->line);
719 continue;
721 if (in_tracker_list(tmp->locked, lock->owner, lock->name, lock->sym))
722 continue;
723 if (start == &unlocked) {
724 if (i++)
725 sm_printf(" ");
726 sm_printf("line %d\n", tmp->line);
727 add_line(&printed, tmp->line);
729 } END_FOR_EACH_PTR(tmp);
732 static int matches_return_type(struct range_list *rl, enum return_type type)
734 sval_t zero_sval = ll_to_sval(0);
736 /* All these double negatives are super ugly! */
738 switch (type) {
739 case ret_zero:
740 return !possibly_true_rl(rl, SPECIAL_NOTEQUAL, alloc_rl(zero_sval, zero_sval));
741 case ret_non_zero:
742 return !possibly_true_rl(rl, SPECIAL_EQUAL, alloc_rl(zero_sval, zero_sval));
743 case ret_negative:
744 return !possibly_true_rl(rl, SPECIAL_GTE, alloc_rl(zero_sval, zero_sval));
745 case ret_positive:
746 return !possibly_true_rl(rl, '<', alloc_rl(zero_sval, zero_sval));
747 case ret_any:
748 default:
749 return 1;
753 static int match_held(struct tracker *lock, struct locks_on_return *this_return, struct smatch_state *start)
755 if (in_tracker_list(this_return->unlocked, lock->owner, lock->name, lock->sym))
756 return 0;
757 if (in_tracker_list(this_return->locked, lock->owner, lock->name, lock->sym))
758 return 1;
759 if (start == &unlocked)
760 return 0;
761 return 1;
764 static int match_released(struct tracker *lock, struct locks_on_return *this_return, struct smatch_state *start)
766 if (in_tracker_list(this_return->unlocked, lock->owner, lock->name, lock->sym))
767 return 1;
768 if (in_tracker_list(this_return->locked, lock->owner, lock->name, lock->sym))
769 return 0;
770 if (start == &unlocked)
771 return 1;
772 return 0;
775 static int held_on_return(struct tracker *lock, struct smatch_state *start, enum return_type type)
777 struct locks_on_return *tmp;
779 FOR_EACH_PTR(all_returns, tmp) {
780 if (!matches_return_type(tmp->return_values, type))
781 continue;
782 if (match_held(lock, tmp, start))
783 return 1;
784 } END_FOR_EACH_PTR(tmp);
785 return 0;
788 static int released_on_return(struct tracker *lock, struct smatch_state *start, enum return_type type)
790 struct locks_on_return *tmp;
792 FOR_EACH_PTR(all_returns, tmp) {
793 if (!matches_return_type(tmp->return_values, type))
794 continue;
795 if (match_released(lock, tmp, start))
796 return 1;
797 } END_FOR_EACH_PTR(tmp);
798 return 0;
801 static void check_returns_consistently(struct tracker *lock,
802 struct smatch_state *start)
804 struct symbol *type;
806 if (!held_on_return(lock, start, ret_any) ||
807 !released_on_return(lock, start, ret_any))
808 return;
810 if (held_on_return(lock, start, ret_zero) &&
811 !held_on_return(lock, start, ret_non_zero))
812 return;
814 if (held_on_return(lock, start, ret_positive) &&
815 !held_on_return(lock, start, ret_zero))
816 return;
818 if (held_on_return(lock, start, ret_positive) &&
819 !held_on_return(lock, start, ret_negative))
820 return;
822 type = cur_func_return_type();
823 if (type && type->type == SYM_PTR) {
824 if (held_on_return(lock, start, ret_non_zero) &&
825 !held_on_return(lock, start, ret_zero))
826 return;
829 print_inconsistent_returns(lock, start);
832 static void check_consistency(struct symbol *sym)
834 struct tracker *tmp;
836 FOR_EACH_PTR(starts_locked, tmp) {
837 if (in_tracker_list(starts_unlocked, tmp->owner, tmp->name,
838 tmp->sym))
839 sm_msg("error: locking inconsistency. We assume "
840 "'%s' is both locked and unlocked at the "
841 "start.",
842 tmp->name);
843 } END_FOR_EACH_PTR(tmp);
845 FOR_EACH_PTR(starts_locked, tmp) {
846 check_returns_consistently(tmp, &locked);
847 } END_FOR_EACH_PTR(tmp);
849 FOR_EACH_PTR(starts_unlocked, tmp) {
850 check_returns_consistently(tmp, &unlocked);
851 } END_FOR_EACH_PTR(tmp);
854 static void clear_lists(void)
856 struct locks_on_return *tmp;
858 func_has_transition = FALSE;
860 free_trackers_and_list(&starts_locked);
861 free_trackers_and_list(&starts_unlocked);
863 FOR_EACH_PTR(all_returns, tmp) {
864 free_trackers_and_list(&tmp->locked);
865 free_trackers_and_list(&tmp->unlocked);
866 free(tmp);
867 } END_FOR_EACH_PTR(tmp);
868 __free_ptr_list((struct ptr_list **)&all_returns);
871 static void match_func_end(struct symbol *sym)
873 if (__inline_fn)
874 return;
876 if (func_has_transition)
877 check_consistency(sym);
878 clear_lists();
881 static void register_lock(int index)
883 struct lock_info *lock = &lock_table[index];
884 void *idx = INT_PTR(index);
886 if (lock->return_type == ret_non_zero) {
887 return_implies_state(lock->function, valid_ptr_min, valid_ptr_max, &match_lock_held, idx);
888 return_implies_state(lock->function, 0, 0, &match_lock_failed, idx);
889 } else if (lock->return_type == ret_any && lock->arg == RETURN_VAL) {
890 add_function_assign_hook(lock->function, &match_returns_locked, idx);
891 } else if (lock->return_type == ret_any) {
892 add_function_hook(lock->function, &match_lock_unlock, idx);
893 } else if (lock->return_type == ret_zero) {
894 return_implies_state(lock->function, 0, 0, &match_lock_held, idx);
895 return_implies_state(lock->function, -4095, -1, &match_lock_failed, idx);
899 static void load_table(struct lock_info *_lock_table, int size)
901 int i;
903 lock_table = _lock_table;
905 for (i = 0; i < size; i++) {
906 if (lock_table[i].action == LOCK)
907 register_lock(i);
908 else
909 add_function_hook(lock_table[i].function, &match_lock_unlock, INT_PTR(i));
913 /* print_held_locks() is used in check_call_tree.c */
914 void print_held_locks(void)
916 struct stree *stree;
917 struct sm_state *sm;
918 int i = 0;
920 stree = __get_cur_stree();
921 FOR_EACH_MY_SM(my_id, stree, sm) {
922 if (sm->state != &locked)
923 continue;
924 if (i++)
925 sm_printf(" ");
926 sm_printf("'%s'", sm->name);
927 } END_FOR_EACH_SM(sm);
930 void check_locking(int id)
932 my_id = id;
934 if (option_project == PROJ_WINE)
935 load_table(wine_lock_table, ARRAY_SIZE(wine_lock_table));
936 else if (option_project == PROJ_KERNEL)
937 load_table(kernel_lock_table, ARRAY_SIZE(kernel_lock_table));
938 else
939 return;
941 add_unmatched_state_hook(my_id, &unmatched_state);
942 add_split_return_callback(match_return);
943 add_hook(&match_func_end, END_FUNC_HOOK);