2 * sparse/check_wine_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
},
92 {"__raw_spin_lock", LOCK
, "spin_lock", 0, ret_any
},
93 {"__raw_spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
94 {"_raw_spin_lock", LOCK
, "spin_lock", 0, ret_any
},
95 {"_raw_spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
96 {"_spin_lock_nested", LOCK
, "spin_lock", 0, ret_any
},
97 {"__raw_spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
98 {"_raw_spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
99 {"_spin_trylock", LOCK
, "spin_lock", 0, ret_non_zero
},
100 {"_spin_lock", LOCK
, "spin_lock", 0, ret_any
},
101 {"_spin_unlock", UNLOCK
, "spin_lock", 0, ret_any
},
103 {"_spin_lock_irqsave_nested", LOCK
, "spin_lock", 0, ret_any
},
104 {"_spin_lock_irqsave", LOCK
, "spin_lock", 0, ret_any
},
105 {"_spin_unlock_irqrestore", UNLOCK
, "spin_lock", 0, ret_any
},
106 {"_spin_lock_irq", LOCK
, "spin_lock", 0, ret_any
},
107 {"_spin_unlock_irq", UNLOCK
, "spin_lock", 0, ret_any
},
109 {"__raw_local_irq_save", LOCK
, "irqsave", RETURN_VAL
, ret_any
},
110 {"_spin_lock_irqsave_nested", LOCK
, "irqsave", 1, ret_any
},
111 {"_spin_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
112 {"_spin_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
113 {"_spin_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
114 {"_spin_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
116 {"_spin_trylock_bh", LOCK
, "spin_lock_bh", 0, ret_non_zero
},
117 {"_spin_lock_bh", LOCK
, "spin_lock_bh", 0, ret_any
},
118 {"_spin_unlock_bh", UNLOCK
, "spin_lock_bh", 0, ret_any
},
119 {"generic__raw_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
120 {"__raw_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
121 {"_raw_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
122 {"_read_trylock", LOCK
, "read_lock", 0, ret_non_zero
},
123 {"_read_lock", LOCK
, "read_lock", 0, ret_any
},
124 {"_read_unlock", UNLOCK
, "read_lock", 0, ret_any
},
126 {"_read_lock_irqsave", LOCK
, "read_lock", 0, ret_any
},
127 {"_read_unlock_irqrestore", UNLOCK
, "read_lock", 0, ret_any
},
128 {"_read_lock_irq", LOCK
, "read_lock", 0, ret_any
},
129 {"_read_unlock_irq", UNLOCK
, "read_lock", 0, ret_any
},
131 {"_read_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
132 {"_read_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
133 {"_read_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
134 {"_read_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
136 {"_read_lock_bh", LOCK
, "read_lock_bh", 0, ret_any
},
137 {"_read_unlock_bh", UNLOCK
, "read_lock_bh", 0, ret_any
},
138 {"_write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
139 {"__raw_write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
140 {"_raw_write_trylock", LOCK
, "write_lock", 0, ret_non_zero
},
141 {"_write_lock", LOCK
, "write_lock", 0, ret_any
},
142 {"_write_unlock", UNLOCK
, "write_lock", 0, ret_any
},
144 {"_write_lock_irqsave", LOCK
, "write_lock", 0, ret_any
},
145 {"_write_unlock_irqrestore", UNLOCK
, "write_lock", 0, ret_any
},
146 {"_write_lock_irq", LOCK
, "write_lock", 0, ret_any
},
147 {"_write_unlock_irq", UNLOCK
, "write_lock", 0, ret_any
},
149 {"_write_lock_irqsave", LOCK
, "irqsave", 1, ret_any
},
150 {"_write_unlock_irqrestore", UNLOCK
, "irqsave", 1, ret_any
},
151 {"_write_lock_irq", LOCK
, "irq", NO_ARG
, ret_any
},
152 {"_write_unlock_irq", UNLOCK
, "irq", NO_ARG
, ret_any
},
154 {"_write_lock_bh", LOCK
, "write_lock_bh", 0, ret_any
},
155 {"_write_unlock_bh", UNLOCK
, "write_lock_bh", 0, ret_any
},
156 {"down_trylock", LOCK
, "sem", 0, ret_zero
},
157 {"down_interruptible", LOCK
, "sem", 0, ret_zero
},
158 {"down", LOCK
, "sem", 0, ret_any
},
159 {"up", UNLOCK
, "sem", 0, ret_any
},
160 {"mutex_trylock", LOCK
, "mutex", 0, ret_non_zero
},
161 {"mutex_lock_interruptible", LOCK
, "mutex", 0, ret_zero
},
162 {"mutex_lock_interruptible_nested", LOCK
, "mutex", 0, ret_zero
},
163 {"mutex_lock_killable", LOCK
, "mutex", 0, ret_zero
},
164 {"mutex_lock_killable_nested", LOCK
, "mutex", 0, ret_zero
},
165 {"mutex_lock", LOCK
, "mutex", 0, ret_any
},
166 {"mutex_lock_nested", LOCK
, "mutex", 0, ret_any
},
167 {"mutex_unlock", UNLOCK
, "mutex", 0, ret_any
},
170 static struct lock_info
*lock_table
;
172 static struct tracker_list
*starts_locked
;
173 static struct tracker_list
*starts_unlocked
;
175 struct locks_on_return
{
177 struct tracker_list
*locked
;
178 struct tracker_list
*unlocked
;
180 DECLARE_PTR_LIST(return_list
, struct locks_on_return
);
181 static struct return_list
*all_returns
;
183 static char *make_full_name(const char *lock
, const char *var
)
185 static char tmp_buf
[512];
187 snprintf(tmp_buf
, 512, "%s:%s", lock
, var
);
189 return alloc_string(tmp_buf
);
192 static char *get_full_name(struct expression
*expr
, int index
)
194 struct expression
*arg
;
196 char *full_name
= NULL
;
197 struct lock_info
*lock
= &lock_table
[index
];
199 if (lock
->arg
== RETURN_VAL
) {
200 name
= get_variable_from_expr(expr
->left
, NULL
);
203 full_name
= make_full_name(lock
->name
, name
);
204 } else if (lock
->arg
== NO_ARG
) {
205 full_name
= make_full_name(lock
->name
, "");
207 arg
= get_argument_from_call_expr(expr
->args
, lock
->arg
);
208 name
= get_variable_from_expr(arg
, NULL
);
211 full_name
= make_full_name(lock
->name
, name
);
218 static struct smatch_state
*get_start_state(struct sm_state
*sm
)
223 if (in_tracker_list(starts_locked
, my_id
, sm
->name
, sm
->sym
))
225 if (in_tracker_list(starts_unlocked
, my_id
, sm
->name
, sm
->sym
))
227 if (is_locked
&& is_unlocked
)
236 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
241 static void do_lock(const char *name
)
245 sm
= get_sm_state(my_id
, name
, NULL
);
247 add_tracker(&starts_unlocked
, my_id
, name
, NULL
);
248 if (sm
&& slist_has_state(sm
->possible
, &locked
))
249 sm_msg("error: double lock '%s'", name
);
250 set_state(my_id
, name
, NULL
, &locked
);
253 static void do_lock_failed(const char *name
)
257 sm
= get_sm_state(my_id
, name
, NULL
);
259 add_tracker(&starts_unlocked
, my_id
, name
, NULL
);
260 set_state(my_id
, name
, NULL
, &unlocked
);
263 static void do_unlock(const char *name
)
267 sm
= get_sm_state(my_id
, name
, NULL
);
269 add_tracker(&starts_locked
, my_id
, name
, NULL
);
270 if (sm
&& slist_has_state(sm
->possible
, &unlocked
))
271 sm_msg("error: double unlock '%s'", name
);
272 set_state(my_id
, name
, NULL
, &unlocked
);
276 static void match_lock_held(const char *fn
, struct expression
*call_expr
,
277 struct expression
*assign_expr
, void *_index
)
279 int index
= (int)_index
;
281 struct lock_info
*lock
= &lock_table
[index
];
283 if (lock
->arg
== NO_ARG
) {
284 lock_name
= get_full_name(NULL
, index
);
285 } else if (lock
->arg
== RETURN_VAL
) {
288 lock_name
= get_full_name(assign_expr
, index
);
290 lock_name
= get_full_name(call_expr
, index
);
295 free_string(lock_name
);
298 static void match_lock_failed(const char *fn
, struct expression
*call_expr
,
299 struct expression
*assign_expr
, void *_index
)
301 int index
= (int)_index
;
303 struct lock_info
*lock
= &lock_table
[index
];
305 if (lock
->arg
== NO_ARG
) {
306 lock_name
= get_full_name(NULL
, index
);
307 } else if (lock
->arg
== RETURN_VAL
) {
310 lock_name
= get_full_name(assign_expr
, index
);
312 lock_name
= get_full_name(call_expr
, index
);
316 do_lock_failed(lock_name
);
317 free_string(lock_name
);
320 static void match_lock_unlock(const char *fn
, struct expression
*expr
, void *_index
)
322 char *full_name
= NULL
;
323 int index
= (int)_index
;
324 struct lock_info
*lock
= &lock_table
[index
];
326 full_name
= get_full_name(expr
, index
);
329 if (lock
->action
== LOCK
)
332 do_unlock(full_name
);
333 free_string(full_name
);
336 static struct locks_on_return
*alloc_return(int line
)
338 struct locks_on_return
*ret
;
340 ret
= malloc(sizeof(*ret
));
343 ret
->unlocked
= NULL
;
347 static void check_possible(struct sm_state
*sm
)
349 struct sm_state
*tmp
;
354 FOR_EACH_PTR(sm
->possible
, tmp
) {
355 if (tmp
->state
== &locked
)
357 if (tmp
->state
== &unlocked
)
359 if (tmp
->state
== &start_state
) {
360 struct smatch_state
*s
;
362 s
= get_start_state(tmp
);
365 else if (s
== &unlocked
)
370 if (tmp
->state
== &undefined
)
371 undef
= 1; // i don't think this is possible any more.
372 } END_FOR_EACH_PTR(tmp
);
373 if ((islocked
&& isunlocked
) || undef
)
374 sm_msg("warn: '%s' is sometimes locked here and "
375 "sometimes unlocked.", sm
->name
);
378 static void match_return(struct expression
*ret_value
)
380 struct locks_on_return
*ret
;
381 struct state_list
*slist
;
382 struct sm_state
*tmp
;
387 ret
= alloc_return(get_lineno());
389 slist
= get_all_states(my_id
);
390 FOR_EACH_PTR(slist
, tmp
) {
391 if (tmp
->state
== &locked
) {
392 add_tracker(&ret
->locked
, tmp
->owner
, tmp
->name
,
394 } else if (tmp
->state
== &unlocked
) {
395 add_tracker(&ret
->unlocked
, tmp
->owner
, tmp
->name
,
397 } else if (tmp
->state
== &start_state
) {
398 struct smatch_state
*s
;
400 s
= get_start_state(tmp
);
402 add_tracker(&ret
->locked
, tmp
->owner
, tmp
->name
,
405 add_tracker(&ret
->unlocked
, tmp
->owner
,tmp
->name
,
410 } END_FOR_EACH_PTR(tmp
);
412 add_ptr_list(&all_returns
, ret
);
415 static void print_inconsistent_returns(struct tracker
*lock
,
416 struct smatch_state
*start
)
418 struct locks_on_return
*tmp
;
421 sm_printf("%s +%d %s(%d) ", get_filename(), get_lineno(), get_function(), get_func_pos());
422 sm_printf("warn: inconsistent returns %s:", lock
->name
);
423 sm_printf(" locked (");
425 FOR_EACH_PTR(all_returns
, tmp
) {
426 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
, lock
->sym
))
428 if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
, lock
->sym
)) {
431 sm_printf("%d", tmp
->line
);
434 if (start
== &locked
) {
437 sm_printf("%d", tmp
->line
);
439 } END_FOR_EACH_PTR(tmp
);
441 sm_printf(") unlocked (");
443 FOR_EACH_PTR(all_returns
, tmp
) {
444 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
, lock
->sym
)) {
447 sm_printf("%d", tmp
->line
);
450 if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
, lock
->sym
)) {
453 if (start
== &unlocked
) {
456 sm_printf("%d", tmp
->line
);
458 } END_FOR_EACH_PTR(tmp
);
462 static void check_returns_consistently(struct tracker
*lock
,
463 struct smatch_state
*start
)
465 int returns_locked
= 0;
466 int returns_unlocked
= 0;
467 struct locks_on_return
*tmp
;
469 FOR_EACH_PTR(all_returns
, tmp
) {
470 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
,
472 returns_unlocked
= tmp
->line
;
473 else if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
,
475 returns_locked
= tmp
->line
;
476 else if (start
== &locked
)
477 returns_locked
= tmp
->line
;
478 else if (start
== &unlocked
)
479 returns_unlocked
= tmp
->line
;
480 } END_FOR_EACH_PTR(tmp
);
482 if (returns_locked
&& returns_unlocked
)
483 print_inconsistent_returns(lock
, start
);
486 static void check_consistency(struct symbol
*sym
)
493 FOR_EACH_PTR(starts_locked
, tmp
) {
494 if (in_tracker_list(starts_unlocked
, tmp
->owner
, tmp
->name
,
496 sm_msg("error: locking inconsistency. We assume "
497 "'%s' is both locked and unlocked at the "
500 } END_FOR_EACH_PTR(tmp
);
502 FOR_EACH_PTR(starts_locked
, tmp
) {
503 check_returns_consistently(tmp
, &locked
);
504 } END_FOR_EACH_PTR(tmp
);
506 FOR_EACH_PTR(starts_unlocked
, tmp
) {
507 check_returns_consistently(tmp
, &unlocked
);
508 } END_FOR_EACH_PTR(tmp
);
512 static void clear_lists(void)
514 struct locks_on_return
*tmp
;
516 free_trackers_and_list(&starts_locked
);
517 free_trackers_and_list(&starts_unlocked
);
519 FOR_EACH_PTR(all_returns
, tmp
) {
520 free_trackers_and_list(&tmp
->locked
);
521 free_trackers_and_list(&tmp
->unlocked
);
523 } END_FOR_EACH_PTR(tmp
);
524 __free_ptr_list((struct ptr_list
**)&all_returns
);
527 static void match_func_end(struct symbol
*sym
)
529 check_consistency(sym
);
533 static void register_lock(int index
)
535 struct lock_info
*lock
= &lock_table
[index
];
536 void *idx
= (void *)index
;
538 if (lock
->return_type
== ret_non_zero
) {
539 return_implies_state(lock
->function
, 1, POINTER_MAX
, &match_lock_held
, idx
);
540 return_implies_state(lock
->function
, 0, 0, &match_lock_failed
, idx
);
541 } else if (lock
->return_type
== ret_any
) {
542 add_function_hook(lock
->function
, &match_lock_unlock
, idx
);
543 } else if (lock
->return_type
== ret_zero
) {
544 return_implies_state(lock
->function
, 0, 0, &match_lock_held
, idx
);
545 return_implies_state(lock
->function
, whole_range
.min
, -1, &match_lock_failed
, idx
);
549 static void load_table(struct lock_info
*_lock_table
, int size
)
553 lock_table
= _lock_table
;
555 for (i
= 0; i
< size
; i
++) {
556 if (lock_table
[i
].action
== LOCK
)
559 add_function_hook(lock_table
[i
].function
, &match_lock_unlock
, (void *)i
);
563 void check_wine_locking(int id
)
567 if (option_project
== PROJ_WINE
)
568 load_table(wine_lock_table
, ARRAY_SIZE(wine_lock_table
));
569 else if (option_project
== PROJ_KERNEL
&& option_spammy
)
570 load_table(kernel_lock_table
, ARRAY_SIZE(kernel_lock_table
));
574 add_unmatched_state_hook(my_id
, &unmatched_state
);
575 add_hook(&match_return
, RETURN_HOOK
);
576 add_hook(&match_func_end
, END_FUNC_HOOK
);