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"
50 enum return_type return_type
;
53 static struct lock_info wine_lock_table
[] = {
54 {"create_window_handle", LOCK
, "create_window_handle", RETURN_VAL
, ret_non_zero
},
55 {"WIN_GetPtr", LOCK
, "create_window_handle", RETURN_VAL
, ret_non_zero
},
56 {"WIN_ReleasePtr", UNLOCK
, "create_window_handle", 0, ret_any
},
57 {"EnterCriticalSection", LOCK
, "CriticalSection", 0, ret_any
},
58 {"LeaveCriticalSection", UNLOCK
, "CriticalSection", 0, ret_any
},
59 {"RtlEnterCriticalSection", LOCK
, "RtlCriticalSection", 0, ret_any
},
60 {"RtlLeaveCriticalSection", UNLOCK
, "RtlCriticalSection", 0, ret_any
},
61 {"GDI_GetObjPtr", LOCK
, "GDI_Get", 0, ret_non_zero
},
62 {"GDI_ReleaseObj", UNLOCK
, "GDI_Get", 0, ret_any
},
63 {"LdrLockLoaderLock", LOCK
, "LdrLockLoaderLock", 2, ret_any
},
64 {"LdrUnlockLoaderLock", UNLOCK
, "LdrLockLoaderLock", 1, ret_any
},
65 {"_lock", LOCK
, "_lock", 0, ret_any
},
66 {"_unlock", UNLOCK
, "_lock", 0, ret_any
},
67 {"msiobj_lock", LOCK
, "msiobj_lock", 0, ret_any
},
68 {"msiobj_unlock", UNLOCK
, "msiobj_lock", 0, ret_any
},
69 {"RtlAcquirePebLock", LOCK
, "PebLock", NO_ARG
, ret_any
},
70 {"RtlReleasePebLock", UNLOCK
, "PebLock", NO_ARG
, ret_any
},
71 {"server_enter_uninterrupted_section", LOCK
, "server_uninterrupted_section", 0, ret_any
},
72 {"server_leave_uninterrupted_section", UNLOCK
, "server_uninterrupted_section", 0, ret_any
},
73 {"RtlLockHeap", LOCK
, "RtlLockHeap", 0, ret_any
},
74 {"RtlUnlockHeap", UNLOCK
, "RtlLockHeap", 0, ret_any
},
75 {"_EnterSysLevel", LOCK
, "SysLevel", 0, ret_any
},
76 {"_LeaveSysLevel", UNLOCK
, "SysLevel", 0, ret_any
},
77 {"USER_Lock", LOCK
, "USER_Lock", NO_ARG
, ret_any
},
78 {"USER_Unlock", UNLOCK
, "USER_Lock", NO_ARG
, ret_any
},
79 {"wine_tsx11_lock", LOCK
, "wine_tsx11_lock", NO_ARG
, ret_any
},
80 {"wine_tsx11_unlock", UNLOCK
, "wine_tsx11_lock", NO_ARG
, ret_any
},
81 {"wine_tsx11_lock_ptr", LOCK
, "wine_tsx11_lock_ptr", NO_ARG
, ret_any
},
82 {"wine_tsx11_unlock_ptr", UNLOCK
, "wine_tsx11_lock_ptr", NO_ARG
, ret_any
},
83 {"wined3d_mutex_lock", LOCK
, "wined3d_mutex_lock", NO_ARG
, ret_any
},
84 {"wined3d_mutex_unlock", UNLOCK
, "wined3d_mutex_lock", NO_ARG
, ret_any
},
85 {"X11DRV_DIB_Lock", LOCK
, "X11DRV_DIB_Lock", 0, ret_any
},
86 {"X11DRV_DIB_Unlock", UNLOCK
, "X11DRV_DIB_Lock", 0, ret_any
},
89 static struct lock_info kernel_lock_table
[] = {
90 {"lock_kernel", LOCK
, "BKL", NO_ARG
, ret_any
},
91 {"unlock_kernel", UNLOCK
, "BKL", NO_ARG
, ret_any
},
93 {"spin_lock", LOCK
, "spin_lock", 0, ret_any
},
94 {"spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
95 {"spin_lock_nested", LOCK
, "spin_lock", 0, ret_any
},
96 {"_spin_lock", LOCK
, "spin_lock", 0, ret_any
},
97 {"_spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
98 {"_spin_lock_nested", LOCK
, "spin_lock", 0, ret_any
},
99 {"__spin_lock", LOCK
, "spin_lock", 0, ret_any
},
100 {"__spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
101 {"__spin_lock_nested", LOCK
, "spin_lock", 0, ret_any
},
102 {"raw_spin_lock", LOCK
, "spin_lock", 0, ret_any
},
103 {"raw_spin_unlock", UNLOCK
, "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_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
109 {"spin_lock_irq", LOCK
, "spin_lock", 0, ret_any
},
110 {"spin_unlock_irq", UNLOCK
, "spin_lock", 0, ret_any
},
111 {"_spin_lock_irq", LOCK
, "spin_lock", 0, ret_any
},
112 {"_spin_unlock_irq", UNLOCK
, "spin_lock", 0, ret_any
},
113 {"__spin_lock_irq", LOCK
, "spin_lock", 0, ret_any
},
114 {"__spin_unlock_irq", UNLOCK
, "spin_lock", 0, ret_any
},
115 {"_raw_spin_lock_irq", LOCK
, "spin_lock", 0, ret_any
},
116 {"_raw_spin_unlock_irq", UNLOCK
, "spin_lock", 0, ret_any
},
117 {"spin_lock_irqsave", LOCK
, "spin_lock", 0, ret_any
},
118 {"spin_unlock_irqrestore", UNLOCK
, "spin_lock", 0, ret_any
},
119 {"_spin_lock_irqsave", LOCK
, "spin_lock", 0, ret_any
},
120 {"_spin_unlock_irqrestore", 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 {"_raw_spin_lock_irqsave", LOCK
, "spin_lock", 0, ret_any
},
124 {"_raw_spin_unlock_irqrestore", UNLOCK
, "spin_lock", 0, ret_any
},
125 {"__raw_spin_lock_irqsave", LOCK
, "spin_lock", 0, ret_any
},
126 {"__raw_spin_unlock_irqrestore", UNLOCK
, "spin_lock", 0, ret_any
},
127 {"spin_lock_irqsave_nested", LOCK
, "spin_lock", 0, ret_any
},
128 {"_spin_lock_irqsave_nested", LOCK
, "spin_lock", 0, ret_any
},
129 {"__spin_lock_irqsave_nested", LOCK
, "spin_lock", 0, ret_any
},
130 {"_raw_spin_lock_irqsave_nested", LOCK
, "spin_lock", 0, ret_any
},
131 {"spin_lock_bh", LOCK
, "spin_lock", 0, ret_any
},
132 {"spin_unlock_bh", UNLOCK
, "spin_lock", 0, ret_any
},
133 {"_spin_lock_bh", LOCK
, "spin_lock", 0, ret_any
},
134 {"_spin_unlock_bh", UNLOCK
, "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
},
138 {"spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
139 {"_spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
140 {"__spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
141 {"raw_spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
142 {"_raw_spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
143 {"spin_trylock_irq", LOCK
, "spin_lock", 0, ret_non_zero
},
144 {"spin_trylock_irqsave", LOCK
, "spin_lock", 0, ret_non_zero
},
145 {"spin_trylock_bh", LOCK
, "spin_lock", 0, ret_non_zero
},
146 {"_spin_trylock_bh", LOCK
, "spin_lock", 0, ret_non_zero
},
147 {"__spin_trylock_bh", LOCK
, "spin_lock", 0, ret_non_zero
},
148 {"__raw_spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
150 {"read_lock", LOCK
, "read_lock", 0, ret_any
},
151 {"read_unlock", UNLOCK
, "read_lock", 0, ret_any
},
152 {"_read_lock", LOCK
, "read_lock", 0, ret_any
},
153 {"_read_unlock", UNLOCK
, "read_lock", 0, ret_any
},
154 {"__read_lock", LOCK
, "read_lock", 0, ret_any
},
155 {"__read_unlock", UNLOCK
, "read_lock", 0, ret_any
},
156 {"read_lock_irq", LOCK
, "read_lock", 0, ret_any
},
157 {"read_unlock_irq" , UNLOCK
, "read_lock", 0, ret_any
},
158 {"_read_lock_irq", LOCK
, "read_lock", 0, ret_any
},
159 {"_read_unlock_irq", UNLOCK
, "read_lock", 0, ret_any
},
160 {"__read_lock_irq", LOCK
, "read_lock", 0, ret_any
},
161 {"__read_unlock_irq", UNLOCK
, "read_lock", 0, ret_any
},
162 {"read_lock_irqsave", LOCK
, "read_lock", 0, ret_any
},
163 {"read_unlock_irqrestore", UNLOCK
, "read_lock", 0, ret_any
},
164 {"_read_lock_irqsave", LOCK
, "read_lock", 0, ret_any
},
165 {"_read_unlock_irqrestore", UNLOCK
, "read_lock", 0, ret_any
},
166 {"__read_lock_irqsave", LOCK
, "read_lock", 0, ret_any
},
167 {"__read_unlock_irqrestore", UNLOCK
, "read_lock", 0, ret_any
},
168 {"read_lock_bh", LOCK
, "read_lock", 0, ret_any
},
169 {"read_unlock_bh", UNLOCK
, "read_lock", 0, ret_any
},
170 {"_read_lock_bh", LOCK
, "read_lock", 0, ret_any
},
171 {"_read_unlock_bh", UNLOCK
, "read_lock", 0, ret_any
},
172 {"__read_lock_bh", LOCK
, "read_lock", 0, ret_any
},
173 {"__read_unlock_bh", UNLOCK
, "read_lock", 0, ret_any
},
175 {"generic__raw_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
176 {"read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
177 {"_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
178 {"raw_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
179 {"_raw_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
180 {"__raw_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
181 {"__read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
183 {"write_lock", LOCK
, "write_lock", 0, ret_any
},
184 {"write_unlock", UNLOCK
, "write_lock", 0, ret_any
},
185 {"_write_lock", LOCK
, "write_lock", 0, ret_any
},
186 {"_write_unlock", UNLOCK
, "write_lock", 0, ret_any
},
187 {"__write_lock", LOCK
, "write_lock", 0, ret_any
},
188 {"__write_unlock", UNLOCK
, "write_lock", 0, ret_any
},
189 {"write_lock_irq", LOCK
, "write_lock", 0, ret_any
},
190 {"write_unlock_irq", UNLOCK
, "write_lock", 0, ret_any
},
191 {"_write_lock_irq", LOCK
, "write_lock", 0, ret_any
},
192 {"_write_unlock_irq", UNLOCK
, "write_lock", 0, ret_any
},
193 {"__write_lock_irq", LOCK
, "write_lock", 0, ret_any
},
194 {"__write_unlock_irq", UNLOCK
, "write_lock", 0, ret_any
},
195 {"write_lock_irqsave", LOCK
, "write_lock", 0, ret_any
},
196 {"write_unlock_irqrestore", UNLOCK
, "write_lock", 0, ret_any
},
197 {"_write_lock_irqsave", LOCK
, "write_lock", 0, ret_any
},
198 {"_write_unlock_irqrestore", UNLOCK
, "write_lock", 0, ret_any
},
199 {"__write_lock_irqsave", LOCK
, "write_lock", 0, ret_any
},
200 {"__write_unlock_irqrestore", UNLOCK
, "write_lock", 0, ret_any
},
201 {"write_lock_bh", LOCK
, "write_lock", 0, ret_any
},
202 {"write_unlock_bh", UNLOCK
, "write_lock", 0, ret_any
},
203 {"_write_lock_bh", LOCK
, "write_lock", 0, ret_any
},
204 {"_write_unlock_bh", UNLOCK
, "write_lock", 0, ret_any
},
205 {"__write_lock_bh", LOCK
, "write_lock", 0, ret_any
},
206 {"__write_unlock_bh", UNLOCK
, "write_lock", 0, ret_any
},
208 {"write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
209 {"_write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
210 {"raw_write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
211 {"_raw_write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
212 {"__write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
213 {"__raw_write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
215 {"down", LOCK
, "sem", 0, ret_any
},
216 {"up", UNLOCK
, "sem", 0, ret_any
},
217 {"down_trylock", LOCK
, "sem", 0, ret_zero
},
218 {"down_interruptible", LOCK
, "sem", 0, ret_zero
},
220 {"mutex_lock", LOCK
, "mutex", 0, ret_any
},
221 {"mutex_unlock", UNLOCK
, "mutex", 0, ret_any
},
222 {"mutex_lock_nested", LOCK
, "mutex", 0, ret_any
},
224 {"mutex_lock_interruptible", LOCK
, "mutex", 0, ret_zero
},
225 {"mutex_lock_interruptible_nested", LOCK
, "mutex", 0, ret_zero
},
226 {"mutex_lock_killable", LOCK
, "mutex", 0, ret_zero
},
227 {"mutex_lock_killable_nested", LOCK
, "mutex", 0, ret_zero
},
229 {"mutex_trylock", LOCK
, "mutex", 0, ret_non_zero
},
231 {"raw_local_irq_disable", LOCK
, "irq", NO_ARG
, ret_any
},
232 {"raw_local_irq_enable", UNLOCK
, "irq", NO_ARG
, ret_any
},
233 {"spin_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
234 {"spin_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
235 {"_spin_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
236 {"_spin_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
237 {"__spin_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
238 {"__spin_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
239 {"_raw_spin_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
240 {"_raw_spin_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
241 {"spin_trylock_irq", LOCK
, "irq", NO_ARG
, ret_non_zero
},
242 {"read_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
243 {"read_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
244 {"_read_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
245 {"_read_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
246 {"__read_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
247 {"__read_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
248 {"write_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
249 {"write_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
250 {"_write_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
251 {"_write_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
252 {"__write_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
253 {"__write_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
255 {"__raw_local_irq_save", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
256 {"raw_local_irq_restore", UNLOCK
, "irqsave", 0, ret_any
},
257 {"spin_lock_irqsave_nested", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
258 {"spin_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
259 {"spin_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
260 {"spin_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
261 {"_spin_lock_irqsave_nested", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
262 {"_spin_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
263 {"_spin_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
264 {"_spin_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
265 {"__spin_lock_irqsave_nested", LOCK
, "irqsave", 1, ret_any
},
266 {"__spin_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
267 {"__spin_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
268 {"_raw_spin_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
269 {"_raw_spin_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
270 {"_raw_spin_unlock_irqrestore",UNLOCK
, "irqsave", 1, ret_any
},
271 {"__raw_spin_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
272 {"__raw_spin_unlock_irqrestore",UNLOCK
, "irqsave", 1, ret_any
},
273 {"_raw_spin_lock_irqsave_nested", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
274 {"spin_trylock_irqsave", LOCK
, "irqsave", 1, ret_non_zero
},
275 {"read_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
276 {"read_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
277 {"read_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
278 {"_read_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
279 {"_read_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
280 {"_read_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
281 {"__read_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
282 {"__read_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
283 {"write_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
284 {"write_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
285 {"write_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
286 {"_write_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
287 {"_write_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
288 {"_write_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
289 {"__write_lock_irqsave", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
290 {"__write_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
292 {"spin_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
293 {"spin_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
294 {"_spin_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
295 {"_spin_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
296 {"__spin_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
297 {"__spin_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
298 {"read_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
299 {"read_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
300 {"_read_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
301 {"_read_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
302 {"__read_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
303 {"__read_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
304 {"write_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
305 {"write_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
306 {"_write_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
307 {"_write_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
308 {"__write_lock_bh", LOCK
, "bottom_half", NO_ARG
, ret_any
},
309 {"__write_unlock_bh", UNLOCK
, "bottom_half", NO_ARG
, ret_any
},
310 {"spin_trylock_bh", LOCK
, "bottom_half", NO_ARG
, ret_non_zero
},
311 {"_spin_trylock_bh", LOCK
, "bottom_half", NO_ARG
, ret_non_zero
},
312 {"__spin_trylock_bh", LOCK
, "bottom_half", NO_ARG
, ret_non_zero
},
315 static struct lock_info
*lock_table
;
317 static struct tracker_list
*starts_locked
;
318 static struct tracker_list
*starts_unlocked
;
320 struct locks_on_return
{
322 struct tracker_list
*locked
;
323 struct tracker_list
*unlocked
;
325 DECLARE_PTR_LIST(return_list
, struct locks_on_return
);
326 static struct return_list
*all_returns
;
328 static char *make_full_name(const char *lock
, const char *var
)
330 static char tmp_buf
[512];
332 snprintf(tmp_buf
, 512, "%s:%s", lock
, var
);
334 return alloc_string(tmp_buf
);
337 static struct expression
*remove_spinlock_check(struct expression
*expr
)
339 if (expr
->type
!= EXPR_CALL
)
341 if (expr
->fn
->type
!= EXPR_SYMBOL
)
343 if (strcmp(expr
->fn
->symbol_name
->name
, "spinlock_check"))
345 expr
= get_argument_from_call_expr(expr
->args
, 0);
349 static char *get_full_name(struct expression
*expr
, int index
)
351 struct expression
*arg
;
353 char *full_name
= NULL
;
354 struct lock_info
*lock
= &lock_table
[index
];
356 if (lock
->arg
== RETURN_VAL
) {
357 name
= get_variable_from_expr(expr
->left
, NULL
);
358 full_name
= make_full_name(lock
->name
, name
);
359 } else if (lock
->arg
== NO_ARG
) {
360 full_name
= make_full_name(lock
->name
, "");
362 arg
= get_argument_from_call_expr(expr
->args
, lock
->arg
);
365 arg
= remove_spinlock_check(arg
);
366 name
= get_variable_from_expr(arg
, NULL
);
369 full_name
= make_full_name(lock
->name
, name
);
376 static struct smatch_state
*get_start_state(struct sm_state
*sm
)
381 if (in_tracker_list(starts_locked
, my_id
, sm
->name
, sm
->sym
))
383 if (in_tracker_list(starts_unlocked
, my_id
, sm
->name
, sm
->sym
))
385 if (is_locked
&& is_unlocked
)
394 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
399 static void do_lock(const char *name
)
403 sm
= get_sm_state(my_id
, name
, NULL
);
405 add_tracker(&starts_unlocked
, my_id
, name
, NULL
);
406 if (sm
&& slist_has_state(sm
->possible
, &locked
))
407 sm_msg("error: double lock '%s'", name
);
408 set_state(my_id
, name
, NULL
, &locked
);
411 static void do_lock_failed(const char *name
)
415 sm
= get_sm_state(my_id
, name
, NULL
);
417 add_tracker(&starts_unlocked
, my_id
, name
, NULL
);
418 set_state(my_id
, name
, NULL
, &unlocked
);
421 static void do_unlock(const char *name
)
425 sm
= get_sm_state(my_id
, name
, NULL
);
427 add_tracker(&starts_locked
, my_id
, name
, NULL
);
428 if (sm
&& slist_has_state(sm
->possible
, &unlocked
))
429 sm_msg("error: double unlock '%s'", name
);
430 set_state(my_id
, name
, NULL
, &unlocked
);
434 static void match_lock_held(const char *fn
, struct expression
*call_expr
,
435 struct expression
*assign_expr
, void *_index
)
437 int index
= (int)_index
;
439 struct lock_info
*lock
= &lock_table
[index
];
441 if (lock
->arg
== NO_ARG
) {
442 lock_name
= get_full_name(NULL
, index
);
443 } else if (lock
->arg
== RETURN_VAL
) {
446 lock_name
= get_full_name(assign_expr
, index
);
448 lock_name
= get_full_name(call_expr
, index
);
453 free_string(lock_name
);
456 static void match_lock_failed(const char *fn
, struct expression
*call_expr
,
457 struct expression
*assign_expr
, void *_index
)
459 int index
= (int)_index
;
461 struct lock_info
*lock
= &lock_table
[index
];
463 if (lock
->arg
== NO_ARG
) {
464 lock_name
= get_full_name(NULL
, index
);
465 } else if (lock
->arg
== RETURN_VAL
) {
468 lock_name
= get_full_name(assign_expr
, index
);
470 lock_name
= get_full_name(call_expr
, index
);
474 do_lock_failed(lock_name
);
475 free_string(lock_name
);
478 static void match_returns_locked(const char *fn
, struct expression
*expr
,
481 char *full_name
= NULL
;
482 int index
= (int)_index
;
483 struct lock_info
*lock
= &lock_table
[index
];
485 if (lock
->arg
!= RETURN_VAL
)
487 full_name
= get_full_name(expr
, index
);
491 static void match_lock_unlock(const char *fn
, struct expression
*expr
, void *_index
)
493 char *full_name
= NULL
;
494 int index
= (int)_index
;
495 struct lock_info
*lock
= &lock_table
[index
];
497 full_name
= get_full_name(expr
, index
);
500 if (lock
->action
== LOCK
)
503 do_unlock(full_name
);
504 free_string(full_name
);
507 static struct locks_on_return
*alloc_return(int line
)
509 struct locks_on_return
*ret
;
511 ret
= malloc(sizeof(*ret
));
514 ret
->unlocked
= NULL
;
518 static void check_possible(struct sm_state
*sm
)
520 struct sm_state
*tmp
;
528 FOR_EACH_PTR(sm
->possible
, tmp
) {
529 if (tmp
->state
== &locked
)
531 if (tmp
->state
== &unlocked
)
533 if (tmp
->state
== &start_state
) {
534 struct smatch_state
*s
;
536 s
= get_start_state(tmp
);
539 else if (s
== &unlocked
)
544 if (tmp
->state
== &undefined
)
545 undef
= 1; // i don't think this is possible any more.
546 } END_FOR_EACH_PTR(tmp
);
547 if ((islocked
&& isunlocked
) || undef
)
548 sm_msg("warn: '%s' is sometimes locked here and sometimes unlocked.", sm
->name
);
551 static void match_return(struct expression
*ret_value
)
553 struct locks_on_return
*ret
;
554 struct state_list
*slist
;
555 struct sm_state
*tmp
;
560 ret
= alloc_return(get_lineno());
562 slist
= get_all_states(my_id
);
563 FOR_EACH_PTR(slist
, tmp
) {
564 if (tmp
->state
== &locked
) {
565 add_tracker(&ret
->locked
, tmp
->owner
, tmp
->name
,
567 } else if (tmp
->state
== &unlocked
) {
568 add_tracker(&ret
->unlocked
, tmp
->owner
, tmp
->name
,
570 } else if (tmp
->state
== &start_state
) {
571 struct smatch_state
*s
;
573 s
= get_start_state(tmp
);
575 add_tracker(&ret
->locked
, tmp
->owner
, tmp
->name
,
578 add_tracker(&ret
->unlocked
, tmp
->owner
,tmp
->name
,
583 } END_FOR_EACH_PTR(tmp
);
585 add_ptr_list(&all_returns
, ret
);
588 static void print_inconsistent_returns(struct tracker
*lock
,
589 struct smatch_state
*start
)
591 struct locks_on_return
*tmp
;
594 sm_printf("%s +%d %s(%d) ", get_filename(), get_lineno(), get_function(), get_func_pos());
595 sm_printf("warn: inconsistent returns %s:", lock
->name
);
596 sm_printf(" locked (");
598 FOR_EACH_PTR(all_returns
, tmp
) {
599 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
, lock
->sym
))
601 if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
, lock
->sym
)) {
604 sm_printf("%d", tmp
->line
);
607 if (start
== &locked
) {
610 sm_printf("%d", tmp
->line
);
612 } END_FOR_EACH_PTR(tmp
);
614 sm_printf(") unlocked (");
616 FOR_EACH_PTR(all_returns
, tmp
) {
617 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
, lock
->sym
)) {
620 sm_printf("%d", tmp
->line
);
623 if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
, lock
->sym
)) {
626 if (start
== &unlocked
) {
629 sm_printf("%d", tmp
->line
);
631 } END_FOR_EACH_PTR(tmp
);
635 static void check_returns_consistently(struct tracker
*lock
,
636 struct smatch_state
*start
)
638 int returns_locked
= 0;
639 int returns_unlocked
= 0;
640 struct locks_on_return
*tmp
;
642 FOR_EACH_PTR(all_returns
, tmp
) {
643 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
,
645 returns_unlocked
= tmp
->line
;
646 else if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
,
648 returns_locked
= tmp
->line
;
649 else if (start
== &locked
)
650 returns_locked
= tmp
->line
;
651 else if (start
== &unlocked
)
652 returns_unlocked
= tmp
->line
;
653 } END_FOR_EACH_PTR(tmp
);
655 if (returns_locked
&& returns_unlocked
)
656 print_inconsistent_returns(lock
, start
);
659 static void check_consistency(struct symbol
*sym
)
666 FOR_EACH_PTR(starts_locked
, tmp
) {
667 if (in_tracker_list(starts_unlocked
, tmp
->owner
, tmp
->name
,
669 sm_msg("error: locking inconsistency. We assume "
670 "'%s' is both locked and unlocked at the "
673 } END_FOR_EACH_PTR(tmp
);
675 FOR_EACH_PTR(starts_locked
, tmp
) {
676 check_returns_consistently(tmp
, &locked
);
677 } END_FOR_EACH_PTR(tmp
);
679 FOR_EACH_PTR(starts_unlocked
, tmp
) {
680 check_returns_consistently(tmp
, &unlocked
);
681 } END_FOR_EACH_PTR(tmp
);
685 static void clear_lists(void)
687 struct locks_on_return
*tmp
;
689 free_trackers_and_list(&starts_locked
);
690 free_trackers_and_list(&starts_unlocked
);
692 FOR_EACH_PTR(all_returns
, tmp
) {
693 free_trackers_and_list(&tmp
->locked
);
694 free_trackers_and_list(&tmp
->unlocked
);
696 } END_FOR_EACH_PTR(tmp
);
697 __free_ptr_list((struct ptr_list
**)&all_returns
);
700 static void match_func_end(struct symbol
*sym
)
702 check_consistency(sym
);
706 static void register_lock(int index
)
708 struct lock_info
*lock
= &lock_table
[index
];
709 void *idx
= (void *)index
;
711 if (lock
->return_type
== ret_non_zero
) {
712 return_implies_state(lock
->function
, 1, POINTER_MAX
, &match_lock_held
, idx
);
713 return_implies_state(lock
->function
, 0, 0, &match_lock_failed
, idx
);
714 } else if (lock
->return_type
== ret_any
&& lock
->arg
== RETURN_VAL
) {
715 add_function_assign_hook(lock
->function
, &match_returns_locked
, idx
);
716 } else if (lock
->return_type
== ret_any
) {
717 add_function_hook(lock
->function
, &match_lock_unlock
, idx
);
718 } else if (lock
->return_type
== ret_zero
) {
719 return_implies_state(lock
->function
, 0, 0, &match_lock_held
, idx
);
720 return_implies_state(lock
->function
, whole_range
.min
, -1, &match_lock_failed
, idx
);
724 static void load_table(struct lock_info
*_lock_table
, int size
)
728 lock_table
= _lock_table
;
730 for (i
= 0; i
< size
; i
++) {
731 if (lock_table
[i
].action
== LOCK
)
734 add_function_hook(lock_table
[i
].function
, &match_lock_unlock
, (void *)i
);
738 /* print_held_locks() is used in check_call_tree.c */
739 void print_held_locks()
741 struct state_list
*slist
;
745 slist
= get_all_states(my_id
);
746 FOR_EACH_PTR(slist
, sm
) {
747 if (sm
->state
!= &locked
)
751 sm_printf("'%s'", sm
->name
);
752 } END_FOR_EACH_PTR(sm
);
756 void check_locking(int id
)
760 if (option_project
== PROJ_WINE
)
761 load_table(wine_lock_table
, ARRAY_SIZE(wine_lock_table
));
762 else if (option_project
== PROJ_KERNEL
)
763 load_table(kernel_lock_table
, ARRAY_SIZE(kernel_lock_table
));
767 add_unmatched_state_hook(my_id
, &unmatched_state
);
768 add_hook(&match_return
, RETURN_HOOK
);
769 add_hook(&match_func_end
, END_FUNC_HOOK
);