2 * sparse/check_locking.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * This test checks that locks are held the same across all returns.
13 * Of course, some functions are designed to only hold the locks on success.
14 * Oh well... We can rewrite it later if we want.
16 * The list of wine locking functions came from an earlier script written
17 * by Michael Stefaniuc.
23 #include "smatch_slist.h"
27 static int func_has_transition
;
52 enum return_type return_type
;
55 static struct lock_info wine_lock_table
[] = {
56 {"create_window_handle", LOCK
, "create_window_handle", RETURN_VAL
, ret_non_zero
},
57 {"WIN_GetPtr", LOCK
, "create_window_handle", RETURN_VAL
, ret_non_zero
},
58 {"WIN_ReleasePtr", UNLOCK
, "create_window_handle", 0, ret_any
},
59 {"EnterCriticalSection", LOCK
, "CriticalSection", 0, ret_any
},
60 {"LeaveCriticalSection", UNLOCK
, "CriticalSection", 0, ret_any
},
61 {"RtlEnterCriticalSection", LOCK
, "RtlCriticalSection", 0, ret_any
},
62 {"RtlLeaveCriticalSection", UNLOCK
, "RtlCriticalSection", 0, ret_any
},
63 {"GDI_GetObjPtr", LOCK
, "GDI_Get", 0, ret_non_zero
},
64 {"GDI_ReleaseObj", UNLOCK
, "GDI_Get", 0, ret_any
},
65 {"LdrLockLoaderLock", LOCK
, "LdrLockLoaderLock", 2, ret_any
},
66 {"LdrUnlockLoaderLock", UNLOCK
, "LdrLockLoaderLock", 1, ret_any
},
67 {"_lock", LOCK
, "_lock", 0, ret_any
},
68 {"_unlock", UNLOCK
, "_lock", 0, ret_any
},
69 {"msiobj_lock", LOCK
, "msiobj_lock", 0, ret_any
},
70 {"msiobj_unlock", UNLOCK
, "msiobj_lock", 0, ret_any
},
71 {"RtlAcquirePebLock", LOCK
, "PebLock", NO_ARG
, ret_any
},
72 {"RtlReleasePebLock", UNLOCK
, "PebLock", NO_ARG
, ret_any
},
73 {"server_enter_uninterrupted_section", LOCK
, "server_uninterrupted_section", 0, ret_any
},
74 {"server_leave_uninterrupted_section", UNLOCK
, "server_uninterrupted_section", 0, ret_any
},
75 {"RtlLockHeap", LOCK
, "RtlLockHeap", 0, ret_any
},
76 {"RtlUnlockHeap", UNLOCK
, "RtlLockHeap", 0, ret_any
},
77 {"_EnterSysLevel", LOCK
, "SysLevel", 0, ret_any
},
78 {"_LeaveSysLevel", UNLOCK
, "SysLevel", 0, ret_any
},
79 {"USER_Lock", LOCK
, "USER_Lock", NO_ARG
, ret_any
},
80 {"USER_Unlock", UNLOCK
, "USER_Lock", NO_ARG
, ret_any
},
81 {"wine_tsx11_lock", LOCK
, "wine_tsx11_lock", NO_ARG
, ret_any
},
82 {"wine_tsx11_unlock", UNLOCK
, "wine_tsx11_lock", NO_ARG
, ret_any
},
83 {"wine_tsx11_lock_ptr", LOCK
, "wine_tsx11_lock_ptr", NO_ARG
, ret_any
},
84 {"wine_tsx11_unlock_ptr", UNLOCK
, "wine_tsx11_lock_ptr", NO_ARG
, ret_any
},
85 {"wined3d_mutex_lock", LOCK
, "wined3d_mutex_lock", NO_ARG
, ret_any
},
86 {"wined3d_mutex_unlock", UNLOCK
, "wined3d_mutex_lock", NO_ARG
, ret_any
},
87 {"X11DRV_DIB_Lock", LOCK
, "X11DRV_DIB_Lock", 0, ret_any
},
88 {"X11DRV_DIB_Unlock", UNLOCK
, "X11DRV_DIB_Lock", 0, ret_any
},
91 static struct lock_info kernel_lock_table
[] = {
92 {"lock_kernel", LOCK
, "BKL", NO_ARG
, ret_any
},
93 {"unlock_kernel", UNLOCK
, "BKL", NO_ARG
, ret_any
},
95 {"spin_lock", LOCK
, "spin_lock", 0, ret_any
},
96 {"spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
97 {"spin_lock_nested", LOCK
, "spin_lock", 0, ret_any
},
98 {"_spin_lock", LOCK
, "spin_lock", 0, ret_any
},
99 {"_spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
100 {"_spin_lock_nested", LOCK
, "spin_lock", 0, ret_any
},
101 {"__spin_lock", LOCK
, "spin_lock", 0, ret_any
},
102 {"__spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
103 {"__spin_lock_nested", LOCK
, "spin_lock", 0, ret_any
},
104 {"raw_spin_lock", LOCK
, "spin_lock", 0, ret_any
},
105 {"raw_spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
106 {"_raw_spin_lock", LOCK
, "spin_lock", 0, ret_any
},
107 {"_raw_spin_lock_nested", LOCK
, "spin_lock", 0, ret_any
},
108 {"_raw_spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
109 {"__raw_spin_lock", LOCK
, "spin_lock", 0, ret_any
},
110 {"__raw_spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
112 {"spin_lock_irq", LOCK
, "spin_lock", 0, ret_any
},
113 {"spin_unlock_irq", UNLOCK
, "spin_lock", 0, ret_any
},
114 {"_spin_lock_irq", LOCK
, "spin_lock", 0, ret_any
},
115 {"_spin_unlock_irq", UNLOCK
, "spin_lock", 0, ret_any
},
116 {"__spin_lock_irq", LOCK
, "spin_lock", 0, ret_any
},
117 {"__spin_unlock_irq", UNLOCK
, "spin_lock", 0, ret_any
},
118 {"_raw_spin_lock_irq", LOCK
, "spin_lock", 0, ret_any
},
119 {"_raw_spin_unlock_irq", UNLOCK
, "spin_lock", 0, ret_any
},
120 {"__raw_spin_unlock_irq", UNLOCK
, "spin_lock", 0, ret_any
},
121 {"spin_lock_irqsave", LOCK
, "spin_lock", 0, ret_any
},
122 {"spin_unlock_irqrestore", UNLOCK
, "spin_lock", 0, ret_any
},
123 {"_spin_lock_irqsave", LOCK
, "spin_lock", 0, ret_any
},
124 {"_spin_unlock_irqrestore", UNLOCK
, "spin_lock", 0, ret_any
},
125 {"__spin_lock_irqsave", LOCK
, "spin_lock", 0, ret_any
},
126 {"__spin_unlock_irqrestore", UNLOCK
, "spin_lock", 0, ret_any
},
127 {"_raw_spin_lock_irqsave", LOCK
, "spin_lock", 0, ret_any
},
128 {"_raw_spin_unlock_irqrestore", UNLOCK
, "spin_lock", 0, ret_any
},
129 {"__raw_spin_lock_irqsave", LOCK
, "spin_lock", 0, ret_any
},
130 {"__raw_spin_unlock_irqrestore", UNLOCK
, "spin_lock", 0, ret_any
},
131 {"spin_lock_irqsave_nested", LOCK
, "spin_lock", 0, ret_any
},
132 {"_spin_lock_irqsave_nested", LOCK
, "spin_lock", 0, ret_any
},
133 {"__spin_lock_irqsave_nested", LOCK
, "spin_lock", 0, ret_any
},
134 {"_raw_spin_lock_irqsave_nested", LOCK
, "spin_lock", 0, ret_any
},
135 {"spin_lock_bh", LOCK
, "spin_lock", 0, ret_any
},
136 {"spin_unlock_bh", UNLOCK
, "spin_lock", 0, ret_any
},
137 {"_spin_lock_bh", LOCK
, "spin_lock", 0, ret_any
},
138 {"_spin_unlock_bh", UNLOCK
, "spin_lock", 0, ret_any
},
139 {"__spin_lock_bh", LOCK
, "spin_lock", 0, ret_any
},
140 {"__spin_unlock_bh", UNLOCK
, "spin_lock", 0, ret_any
},
142 {"spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
143 {"_spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
144 {"__spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
145 {"raw_spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
146 {"_raw_spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
147 {"spin_trylock_irq", LOCK
, "spin_lock", 0, ret_non_zero
},
148 {"spin_trylock_irqsave", LOCK
, "spin_lock", 0, ret_non_zero
},
149 {"spin_trylock_bh", LOCK
, "spin_lock", 0, ret_non_zero
},
150 {"_spin_trylock_bh", LOCK
, "spin_lock", 0, ret_non_zero
},
151 {"__spin_trylock_bh", LOCK
, "spin_lock", 0, ret_non_zero
},
152 {"__raw_spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
153 {"_atomic_dec_and_lock", LOCK
, "spin_lock", 1, ret_non_zero
},
155 {"read_lock", LOCK
, "read_lock", 0, ret_any
},
156 {"read_unlock", UNLOCK
, "read_lock", 0, ret_any
},
157 {"_read_lock", LOCK
, "read_lock", 0, ret_any
},
158 {"_read_unlock", UNLOCK
, "read_lock", 0, ret_any
},
159 {"__read_lock", LOCK
, "read_lock", 0, ret_any
},
160 {"__read_unlock", UNLOCK
, "read_lock", 0, ret_any
},
161 {"_raw_read_lock", LOCK
, "read_lock", 0, ret_any
},
162 {"_raw_read_unlock", UNLOCK
, "read_lock", 0, ret_any
},
163 {"read_lock_irq", LOCK
, "read_lock", 0, ret_any
},
164 {"read_unlock_irq" , UNLOCK
, "read_lock", 0, ret_any
},
165 {"_read_lock_irq", LOCK
, "read_lock", 0, ret_any
},
166 {"_read_unlock_irq", UNLOCK
, "read_lock", 0, ret_any
},
167 {"__read_lock_irq", LOCK
, "read_lock", 0, ret_any
},
168 {"__read_unlock_irq", UNLOCK
, "read_lock", 0, ret_any
},
169 {"read_lock_irqsave", LOCK
, "read_lock", 0, ret_any
},
170 {"read_unlock_irqrestore", UNLOCK
, "read_lock", 0, ret_any
},
171 {"_read_lock_irqsave", LOCK
, "read_lock", 0, ret_any
},
172 {"_read_unlock_irqrestore", UNLOCK
, "read_lock", 0, ret_any
},
173 {"__read_lock_irqsave", LOCK
, "read_lock", 0, ret_any
},
174 {"__read_unlock_irqrestore", UNLOCK
, "read_lock", 0, ret_any
},
175 {"read_lock_bh", LOCK
, "read_lock", 0, ret_any
},
176 {"read_unlock_bh", UNLOCK
, "read_lock", 0, ret_any
},
177 {"_read_lock_bh", LOCK
, "read_lock", 0, ret_any
},
178 {"_read_unlock_bh", UNLOCK
, "read_lock", 0, ret_any
},
179 {"__read_lock_bh", LOCK
, "read_lock", 0, ret_any
},
180 {"__read_unlock_bh", UNLOCK
, "read_lock", 0, ret_any
},
181 {"_raw_read_lock_bh", LOCK
, "read_lock", 0, ret_any
},
182 {"_raw_read_unlock_bh", UNLOCK
, "read_lock", 0, ret_any
},
184 {"generic__raw_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
185 {"read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
186 {"_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
187 {"raw_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
188 {"_raw_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
189 {"__raw_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
190 {"__read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
192 {"write_lock", LOCK
, "write_lock", 0, ret_any
},
193 {"write_unlock", UNLOCK
, "write_lock", 0, ret_any
},
194 {"_write_lock", LOCK
, "write_lock", 0, ret_any
},
195 {"_write_unlock", UNLOCK
, "write_lock", 0, ret_any
},
196 {"__write_lock", LOCK
, "write_lock", 0, ret_any
},
197 {"__write_unlock", UNLOCK
, "write_lock", 0, ret_any
},
198 {"write_lock_irq", LOCK
, "write_lock", 0, ret_any
},
199 {"write_unlock_irq", UNLOCK
, "write_lock", 0, ret_any
},
200 {"_write_lock_irq", LOCK
, "write_lock", 0, ret_any
},
201 {"_write_unlock_irq", UNLOCK
, "write_lock", 0, ret_any
},
202 {"__write_lock_irq", LOCK
, "write_lock", 0, ret_any
},
203 {"__write_unlock_irq", UNLOCK
, "write_lock", 0, ret_any
},
204 {"write_lock_irqsave", LOCK
, "write_lock", 0, ret_any
},
205 {"write_unlock_irqrestore", UNLOCK
, "write_lock", 0, ret_any
},
206 {"_write_lock_irqsave", LOCK
, "write_lock", 0, ret_any
},
207 {"_write_unlock_irqrestore", UNLOCK
, "write_lock", 0, ret_any
},
208 {"__write_lock_irqsave", LOCK
, "write_lock", 0, ret_any
},
209 {"__write_unlock_irqrestore", UNLOCK
, "write_lock", 0, ret_any
},
210 {"write_lock_bh", LOCK
, "write_lock", 0, ret_any
},
211 {"write_unlock_bh", UNLOCK
, "write_lock", 0, ret_any
},
212 {"_write_lock_bh", LOCK
, "write_lock", 0, ret_any
},
213 {"_write_unlock_bh", UNLOCK
, "write_lock", 0, ret_any
},
214 {"__write_lock_bh", LOCK
, "write_lock", 0, ret_any
},
215 {"__write_unlock_bh", UNLOCK
, "write_lock", 0, ret_any
},
217 {"write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
218 {"_write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
219 {"raw_write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
220 {"_raw_write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
221 {"__write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
222 {"__raw_write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
224 {"down", LOCK
, "sem", 0, ret_any
},
225 {"up", UNLOCK
, "sem", 0, ret_any
},
226 {"down_trylock", LOCK
, "sem", 0, ret_zero
},
227 {"down_interruptible", LOCK
, "sem", 0, ret_zero
},
229 {"mutex_lock", LOCK
, "mutex", 0, ret_any
},
230 {"mutex_unlock", UNLOCK
, "mutex", 0, ret_any
},
231 {"mutex_lock_nested", LOCK
, "mutex", 0, ret_any
},
233 {"mutex_lock_interruptible", LOCK
, "mutex", 0, ret_zero
},
234 {"mutex_lock_interruptible_nested", LOCK
, "mutex", 0, ret_zero
},
235 {"mutex_lock_killable", LOCK
, "mutex", 0, ret_zero
},
236 {"mutex_lock_killable_nested", LOCK
, "mutex", 0, ret_zero
},
238 {"mutex_trylock", LOCK
, "mutex", 0, ret_non_zero
},
240 {"raw_local_irq_disable", LOCK
, "irq", NO_ARG
, ret_any
},
241 {"raw_local_irq_enable", UNLOCK
, "irq", NO_ARG
, ret_any
},
242 {"spin_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
243 {"spin_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
244 {"_spin_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
245 {"_spin_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
246 {"__spin_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
247 {"__spin_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
248 {"_raw_spin_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
249 {"_raw_spin_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
250 {"__raw_spin_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
251 {"spin_trylock_irq", LOCK
, "irq", NO_ARG
, ret_non_zero
},
252 {"read_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
253 {"read_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
254 {"_read_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
255 {"_read_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
256 {"__read_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
257 {"__read_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
258 {"write_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
259 {"write_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
260 {"_write_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
261 {"_write_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
262 {"__write_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
263 {"__write_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
265 {"arch_local_irq_save", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
266 {"arch_local_irq_restore", UNLOCK
, "irqsave", 0, ret_any
},
267 {"__raw_local_irq_save", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
268 {"raw_local_irq_restore", UNLOCK
, "irqsave", 0, ret_any
},
269 {"spin_lock_irqsave_nested", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
270 {"spin_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
271 {"spin_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
272 {"spin_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
273 {"_spin_lock_irqsave_nested", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
274 {"_spin_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
275 {"_spin_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
276 {"_spin_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
277 {"__spin_lock_irqsave_nested", LOCK
, "irqsave", 1, ret_any
},
278 {"__spin_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
279 {"__spin_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
280 {"_raw_spin_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
281 {"_raw_spin_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
282 {"_raw_spin_unlock_irqrestore",UNLOCK
, "irqsave", 1, ret_any
},
283 {"__raw_spin_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
284 {"__raw_spin_unlock_irqrestore",UNLOCK
, "irqsave", 1, ret_any
},
285 {"_raw_spin_lock_irqsave_nested", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
286 {"spin_trylock_irqsave", LOCK
, "irqsave", 1, ret_non_zero
},
287 {"read_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
288 {"read_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
289 {"read_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
290 {"_read_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
291 {"_read_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
292 {"_read_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
293 {"__read_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
294 {"__read_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
295 {"write_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
296 {"write_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
297 {"write_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
298 {"_write_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
299 {"_write_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
300 {"_write_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
301 {"__write_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
302 {"__write_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
304 {"spin_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
305 {"spin_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
306 {"_spin_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
307 {"_spin_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
308 {"__spin_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
309 {"__spin_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
310 {"read_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
311 {"read_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
312 {"_read_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
313 {"_read_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
314 {"__read_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
315 {"__read_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
316 {"_raw_read_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
317 {"_raw_read_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
318 {"write_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
319 {"write_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
320 {"_write_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
321 {"_write_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
322 {"__write_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
323 {"__write_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
324 {"spin_trylock_bh", LOCK
, "bottom_half", NO_ARG
, ret_non_zero
},
325 {"_spin_trylock_bh", LOCK
, "bottom_half", NO_ARG
, ret_non_zero
},
326 {"__spin_trylock_bh", LOCK
, "bottom_half", NO_ARG
, ret_non_zero
},
329 static struct lock_info
*lock_table
;
331 static struct tracker_list
*starts_locked
;
332 static struct tracker_list
*starts_unlocked
;
334 struct locks_on_return
{
336 struct tracker_list
*locked
;
337 struct tracker_list
*unlocked
;
339 DECLARE_PTR_LIST(return_list
, struct locks_on_return
);
340 static struct return_list
*all_returns
;
342 static char *make_full_name(const char *lock
, const char *var
)
344 static char tmp_buf
[512];
346 snprintf(tmp_buf
, 512, "%s:%s", lock
, var
);
348 return alloc_string(tmp_buf
);
351 static struct expression
*remove_spinlock_check(struct expression
*expr
)
353 if (expr
->type
!= EXPR_CALL
)
355 if (expr
->fn
->type
!= EXPR_SYMBOL
)
357 if (strcmp(expr
->fn
->symbol_name
->name
, "spinlock_check"))
359 expr
= get_argument_from_call_expr(expr
->args
, 0);
363 static char *get_full_name(struct expression
*expr
, int index
)
365 struct expression
*arg
;
367 char *full_name
= NULL
;
368 struct lock_info
*lock
= &lock_table
[index
];
370 if (lock
->arg
== RETURN_VAL
) {
371 name
= get_variable_from_expr(expr
->left
, NULL
);
372 full_name
= make_full_name(lock
->name
, name
);
373 } else if (lock
->arg
== NO_ARG
) {
374 full_name
= make_full_name(lock
->name
, "");
376 arg
= get_argument_from_call_expr(expr
->args
, lock
->arg
);
379 arg
= remove_spinlock_check(arg
);
380 name
= get_variable_from_expr_complex(arg
, NULL
);
383 full_name
= make_full_name(lock
->name
, name
);
390 static struct smatch_state
*get_start_state(struct sm_state
*sm
)
395 if (in_tracker_list(starts_locked
, my_id
, sm
->name
, sm
->sym
))
397 if (in_tracker_list(starts_unlocked
, my_id
, sm
->name
, sm
->sym
))
399 if (is_locked
&& is_unlocked
)
408 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
413 static void do_lock(const char *name
)
417 sm
= get_sm_state(my_id
, name
, NULL
);
419 add_tracker(&starts_unlocked
, my_id
, name
, NULL
);
420 if (sm
&& slist_has_state(sm
->possible
, &locked
))
421 sm_msg("error: double lock '%s'", name
);
423 func_has_transition
= TRUE
;
424 set_state(my_id
, name
, NULL
, &locked
);
427 static void do_lock_failed(const char *name
)
431 sm
= get_sm_state(my_id
, name
, NULL
);
433 add_tracker(&starts_unlocked
, my_id
, name
, NULL
);
434 set_state(my_id
, name
, NULL
, &unlocked
);
437 static void do_unlock(const char *name
)
441 if (__path_is_null())
443 sm
= get_sm_state(my_id
, name
, NULL
);
445 add_tracker(&starts_locked
, my_id
, name
, NULL
);
446 if (sm
&& slist_has_state(sm
->possible
, &unlocked
))
447 sm_msg("error: double unlock '%s'", name
);
449 func_has_transition
= TRUE
;
450 set_state(my_id
, name
, NULL
, &unlocked
);
453 static void match_lock_held(const char *fn
, struct expression
*call_expr
,
454 struct expression
*assign_expr
, void *_index
)
456 int index
= PTR_INT(_index
);
458 struct lock_info
*lock
= &lock_table
[index
];
460 if (lock
->arg
== NO_ARG
) {
461 lock_name
= get_full_name(NULL
, index
);
462 } else if (lock
->arg
== RETURN_VAL
) {
465 lock_name
= get_full_name(assign_expr
, index
);
467 lock_name
= get_full_name(call_expr
, index
);
472 free_string(lock_name
);
475 static void match_lock_failed(const char *fn
, struct expression
*call_expr
,
476 struct expression
*assign_expr
, void *_index
)
478 int index
= PTR_INT(_index
);
480 struct lock_info
*lock
= &lock_table
[index
];
482 if (lock
->arg
== NO_ARG
) {
483 lock_name
= get_full_name(NULL
, index
);
484 } else if (lock
->arg
== RETURN_VAL
) {
487 lock_name
= get_full_name(assign_expr
, index
);
489 lock_name
= get_full_name(call_expr
, index
);
493 do_lock_failed(lock_name
);
494 free_string(lock_name
);
497 static void match_returns_locked(const char *fn
, struct expression
*expr
,
500 char *full_name
= NULL
;
501 int index
= PTR_INT(_index
);
502 struct lock_info
*lock
= &lock_table
[index
];
504 if (lock
->arg
!= RETURN_VAL
)
506 full_name
= get_full_name(expr
, index
);
510 static void match_lock_unlock(const char *fn
, struct expression
*expr
, void *_index
)
512 char *full_name
= NULL
;
513 int index
= PTR_INT(_index
);
514 struct lock_info
*lock
= &lock_table
[index
];
516 full_name
= get_full_name(expr
, index
);
519 if (lock
->action
== LOCK
)
522 do_unlock(full_name
);
523 free_string(full_name
);
526 static struct locks_on_return
*alloc_return(int line
)
528 struct locks_on_return
*ret
;
530 ret
= malloc(sizeof(*ret
));
533 ret
->unlocked
= NULL
;
537 static void check_possible(struct sm_state
*sm
)
539 struct sm_state
*tmp
;
547 FOR_EACH_PTR(sm
->possible
, tmp
) {
548 if (tmp
->state
== &locked
)
550 if (tmp
->state
== &unlocked
)
552 if (tmp
->state
== &start_state
) {
553 struct smatch_state
*s
;
555 s
= get_start_state(tmp
);
558 else if (s
== &unlocked
)
563 if (tmp
->state
== &undefined
)
564 undef
= 1; // i don't think this is possible any more.
565 } END_FOR_EACH_PTR(tmp
);
566 if ((islocked
&& isunlocked
) || undef
)
567 sm_msg("warn: '%s' is sometimes locked here and sometimes unlocked.", sm
->name
);
570 static void match_return(struct expression
*ret_value
)
572 struct locks_on_return
*ret
;
573 struct state_list
*slist
;
574 struct sm_state
*tmp
;
579 ret
= alloc_return(get_lineno());
581 slist
= get_all_states(my_id
);
582 FOR_EACH_PTR(slist
, tmp
) {
583 if (tmp
->state
== &locked
) {
584 add_tracker(&ret
->locked
, tmp
->owner
, tmp
->name
,
586 } else if (tmp
->state
== &unlocked
) {
587 add_tracker(&ret
->unlocked
, tmp
->owner
, tmp
->name
,
589 } else if (tmp
->state
== &start_state
) {
590 struct smatch_state
*s
;
592 s
= get_start_state(tmp
);
594 add_tracker(&ret
->locked
, tmp
->owner
, tmp
->name
,
597 add_tracker(&ret
->unlocked
, tmp
->owner
,tmp
->name
,
602 } END_FOR_EACH_PTR(tmp
);
604 add_ptr_list(&all_returns
, ret
);
607 static void print_inconsistent_returns(struct tracker
*lock
,
608 struct smatch_state
*start
)
610 struct locks_on_return
*tmp
;
614 sm_printf("warn: inconsistent returns %s:", lock
->name
);
615 sm_printf(" locked (");
617 FOR_EACH_PTR(all_returns
, tmp
) {
618 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
, lock
->sym
))
620 if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
, lock
->sym
)) {
623 sm_printf("%d", tmp
->line
);
626 if (start
== &locked
) {
629 sm_printf("%d", tmp
->line
);
631 } END_FOR_EACH_PTR(tmp
);
633 sm_printf(") unlocked (");
635 FOR_EACH_PTR(all_returns
, tmp
) {
636 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
, lock
->sym
)) {
639 sm_printf("%d", tmp
->line
);
642 if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
, lock
->sym
)) {
645 if (start
== &unlocked
) {
648 sm_printf("%d", tmp
->line
);
650 } END_FOR_EACH_PTR(tmp
);
654 static void check_returns_consistently(struct tracker
*lock
,
655 struct smatch_state
*start
)
657 int returns_locked
= 0;
658 int returns_unlocked
= 0;
659 struct locks_on_return
*tmp
;
661 FOR_EACH_PTR(all_returns
, tmp
) {
662 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
,
664 returns_unlocked
= tmp
->line
;
665 else if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
,
667 returns_locked
= tmp
->line
;
668 else if (start
== &locked
)
669 returns_locked
= tmp
->line
;
670 else if (start
== &unlocked
)
671 returns_unlocked
= tmp
->line
;
672 } END_FOR_EACH_PTR(tmp
);
674 if (returns_locked
&& returns_unlocked
)
675 print_inconsistent_returns(lock
, start
);
678 static void check_consistency(struct symbol
*sym
)
685 FOR_EACH_PTR(starts_locked
, tmp
) {
686 if (in_tracker_list(starts_unlocked
, tmp
->owner
, tmp
->name
,
688 sm_msg("error: locking inconsistency. We assume "
689 "'%s' is both locked and unlocked at the "
692 } END_FOR_EACH_PTR(tmp
);
694 FOR_EACH_PTR(starts_locked
, tmp
) {
695 check_returns_consistently(tmp
, &locked
);
696 } END_FOR_EACH_PTR(tmp
);
698 FOR_EACH_PTR(starts_unlocked
, tmp
) {
699 check_returns_consistently(tmp
, &unlocked
);
700 } END_FOR_EACH_PTR(tmp
);
703 static void clear_lists(void)
705 struct locks_on_return
*tmp
;
707 func_has_transition
= FALSE
;
709 free_trackers_and_list(&starts_locked
);
710 free_trackers_and_list(&starts_unlocked
);
712 FOR_EACH_PTR(all_returns
, tmp
) {
713 free_trackers_and_list(&tmp
->locked
);
714 free_trackers_and_list(&tmp
->unlocked
);
716 } END_FOR_EACH_PTR(tmp
);
717 __free_ptr_list((struct ptr_list
**)&all_returns
);
720 static void match_func_end(struct symbol
*sym
)
722 if (func_has_transition
)
723 check_consistency(sym
);
727 static void register_lock(int index
)
729 struct lock_info
*lock
= &lock_table
[index
];
730 void *idx
= INT_PTR(index
);
732 if (lock
->return_type
== ret_non_zero
) {
733 return_implies_state(lock
->function
, valid_ptr_min
, valid_ptr_max
, &match_lock_held
, idx
);
734 return_implies_state(lock
->function
, 0, 0, &match_lock_failed
, idx
);
735 } else if (lock
->return_type
== ret_any
&& lock
->arg
== RETURN_VAL
) {
736 add_function_assign_hook(lock
->function
, &match_returns_locked
, idx
);
737 } else if (lock
->return_type
== ret_any
) {
738 add_function_hook(lock
->function
, &match_lock_unlock
, idx
);
739 } else if (lock
->return_type
== ret_zero
) {
740 return_implies_state(lock
->function
, 0, 0, &match_lock_held
, idx
);
741 return_implies_state(lock
->function
, whole_range
.min
, -1, &match_lock_failed
, idx
);
745 static void load_table(struct lock_info
*_lock_table
, int size
)
749 lock_table
= _lock_table
;
751 for (i
= 0; i
< size
; i
++) {
752 if (lock_table
[i
].action
== LOCK
)
755 add_function_hook(lock_table
[i
].function
, &match_lock_unlock
, INT_PTR(i
));
759 /* print_held_locks() is used in check_call_tree.c */
760 void print_held_locks()
762 struct state_list
*slist
;
766 slist
= get_all_states(my_id
);
767 FOR_EACH_PTR(slist
, sm
) {
768 if (sm
->state
!= &locked
)
772 sm_printf("'%s'", sm
->name
);
773 } END_FOR_EACH_PTR(sm
);
777 void check_locking(int id
)
781 if (option_project
== PROJ_WINE
)
782 load_table(wine_lock_table
, ARRAY_SIZE(wine_lock_table
));
783 else if (option_project
== PROJ_KERNEL
)
784 load_table(kernel_lock_table
, ARRAY_SIZE(kernel_lock_table
));
788 add_unmatched_state_hook(my_id
, &unmatched_state
);
789 add_hook(&match_return
, RETURN_HOOK
);
790 add_hook(&match_func_end
, END_FUNC_HOOK
);