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 locking functions came from an earlier script written
17 * by Michael Stefaniuc.
23 #include "smatch_slist.h"
50 enum return_type return_type
;
52 {"create_window_handle", LOCK
, "create_window_handle", RETURN_VAL
, ret_non_zero
},
53 {"WIN_GetPtr", LOCK
, "create_window_handle", RETURN_VAL
, ret_non_zero
},
54 {"WIN_ReleasePtr", UNLOCK
, "create_window_handle", 0, ret_any
},
55 {"EnterCriticalSection", LOCK
, "CriticalSection", 0, ret_any
},
56 {"LeaveCriticalSection", UNLOCK
, "CriticalSection", 0, ret_any
},
57 {"RtlEnterCriticalSection", LOCK
, "RtlCriticalSection", 0, ret_any
},
58 {"RtlLeaveCriticalSection", UNLOCK
, "RtlCriticalSection", 0, ret_any
},
59 {"GDI_GetObjPtr", LOCK
, "GDI_Get", 0, ret_non_zero
},
60 {"GDI_ReleaseObj", UNLOCK
, "GDI_Get", 0, ret_any
},
61 {"LdrLockLoaderLock", LOCK
, "LdrLockLoaderLock", 2, ret_any
},
62 {"LdrUnlockLoaderLock", UNLOCK
, "LdrLockLoaderLock", 1, ret_any
},
63 {"_lock", LOCK
, "_lock", 0, ret_any
},
64 {"_unlock", UNLOCK
, "_lock", 0, ret_any
},
65 {"msiobj_lock", LOCK
, "msiobj_lock", 0, ret_any
},
66 {"msiobj_unlock", UNLOCK
, "msiobj_lock", 0, ret_any
},
67 {"RtlAcquirePebLock", LOCK
, "PebLock", NO_ARG
, ret_any
},
68 {"RtlReleasePebLock", UNLOCK
, "PebLock", NO_ARG
, ret_any
},
69 {"server_enter_uninterrupted_section", LOCK
, "server_uninterrupted_section", 0, ret_any
},
70 {"server_leave_uninterrupted_section", UNLOCK
, "server_uninterrupted_section", 0, ret_any
},
71 {"RtlLockHeap", LOCK
, "RtlLockHeap", 0, ret_any
},
72 {"RtlUnlockHeap", UNLOCK
, "RtlLockHeap", 0, ret_any
},
73 {"_EnterSysLevel", LOCK
, "SysLevel", 0, ret_any
},
74 {"_LeaveSysLevel", UNLOCK
, "SysLevel", 0, ret_any
},
75 {"USER_Lock", LOCK
, "USER_Lock", NO_ARG
, ret_any
},
76 {"USER_Unlock", UNLOCK
, "USER_Lock", NO_ARG
, ret_any
},
77 {"wine_tsx11_lock", LOCK
, "wine_tsx11_lock", NO_ARG
, ret_any
},
78 {"wine_tsx11_unlock", UNLOCK
, "wine_tsx11_lock", NO_ARG
, ret_any
},
79 {"wine_tsx11_lock_ptr", LOCK
, "wine_tsx11_lock_ptr", NO_ARG
, ret_any
},
80 {"wine_tsx11_unlock_ptr", UNLOCK
, "wine_tsx11_lock_ptr", NO_ARG
, ret_any
},
81 {"wined3d_mutex_lock", LOCK
, "wined3d_mutex_lock", NO_ARG
, ret_any
},
82 {"wined3d_mutex_unlock", UNLOCK
, "wined3d_mutex_lock", NO_ARG
, ret_any
},
83 {"X11DRV_DIB_Lock", LOCK
, "X11DRV_DIB_Lock", 0, ret_any
},
84 {"X11DRV_DIB_Unlock", UNLOCK
, "X11DRV_DIB_Lock", 0, ret_any
},
87 static struct tracker_list
*starts_locked
;
88 static struct tracker_list
*starts_unlocked
;
90 struct locks_on_return
{
92 struct tracker_list
*locked
;
93 struct tracker_list
*unlocked
;
95 DECLARE_PTR_LIST(return_list
, struct locks_on_return
);
96 static struct return_list
*all_returns
;
98 static char *make_full_name(const char *lock
, const char *var
)
100 static char tmp_buf
[512];
102 snprintf(tmp_buf
, 512, "%s:%s", lock
, var
);
104 return alloc_string(tmp_buf
);
107 static char *get_full_name(struct expression
*expr
, int index
)
109 struct expression
*arg
;
111 char *full_name
= NULL
;
112 struct lock_info
*lock
= &lock_table
[index
];
114 if (lock
->arg
== RETURN_VAL
) {
115 name
= get_variable_from_expr(expr
->left
, NULL
);
118 full_name
= make_full_name(lock
->name
, name
);
119 } else if (lock
->arg
== NO_ARG
) {
120 full_name
= make_full_name(lock
->name
, "");
122 arg
= get_argument_from_call_expr(expr
->args
, lock
->arg
);
123 name
= get_variable_from_expr(arg
, NULL
);
126 full_name
= make_full_name(lock
->name
, name
);
133 static struct smatch_state
*get_start_state(struct sm_state
*sm
)
138 if (in_tracker_list(starts_locked
, my_id
, sm
->name
, sm
->sym
))
140 if (in_tracker_list(starts_unlocked
, my_id
, sm
->name
, sm
->sym
))
142 if (is_locked
&& is_unlocked
)
151 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
156 static void do_lock(const char *name
)
160 sm
= get_sm_state(my_id
, name
, NULL
);
162 add_tracker(&starts_unlocked
, my_id
, name
, NULL
);
163 if (sm
&& slist_has_state(sm
->possible
, &locked
))
164 sm_msg("error: double lock '%s'", name
);
165 set_state(my_id
, name
, NULL
, &locked
);
168 static void do_lock_failed(const char *name
)
172 sm
= get_sm_state(my_id
, name
, NULL
);
174 add_tracker(&starts_unlocked
, my_id
, name
, NULL
);
175 set_state(my_id
, name
, NULL
, &unlocked
);
178 static void do_unlock(const char *name
)
182 sm
= get_sm_state(my_id
, name
, NULL
);
184 add_tracker(&starts_locked
, my_id
, name
, NULL
);
185 if (sm
&& slist_has_state(sm
->possible
, &unlocked
))
186 sm_msg("error: double unlock '%s'", name
);
187 set_state(my_id
, name
, NULL
, &unlocked
);
191 static void match_lock_held(const char *fn
, struct expression
*call_expr
,
192 struct expression
*assign_expr
, void *_index
)
194 int index
= (int)_index
;
196 struct lock_info
*lock
= &lock_table
[index
];
198 if (lock
->arg
== NO_ARG
) {
199 lock_name
= get_full_name(NULL
, index
);
200 } else if (lock
->arg
== RETURN_VAL
) {
203 lock_name
= get_full_name(assign_expr
, index
);
205 lock_name
= get_full_name(call_expr
, index
);
210 free_string(lock_name
);
213 static void match_lock_failed(const char *fn
, struct expression
*call_expr
,
214 struct expression
*assign_expr
, void *_index
)
216 int index
= (int)_index
;
218 struct lock_info
*lock
= &lock_table
[index
];
220 if (lock
->arg
== NO_ARG
) {
221 lock_name
= get_full_name(NULL
, index
);
222 } else if (lock
->arg
== RETURN_VAL
) {
225 lock_name
= get_full_name(assign_expr
, index
);
227 lock_name
= get_full_name(call_expr
, index
);
231 do_lock_failed(lock_name
);
232 free_string(lock_name
);
235 static void match_lock_unlock(const char *fn
, struct expression
*expr
, void *_index
)
237 char *full_name
= NULL
;
238 int index
= (int)_index
;
239 struct lock_info
*lock
= &lock_table
[index
];
241 full_name
= get_full_name(expr
, index
);
244 if (lock
->action
== LOCK
)
247 do_unlock(full_name
);
248 free_string(full_name
);
251 static struct locks_on_return
*alloc_return(int line
)
253 struct locks_on_return
*ret
;
255 ret
= malloc(sizeof(*ret
));
258 ret
->unlocked
= NULL
;
262 static void check_possible(struct sm_state
*sm
)
264 struct sm_state
*tmp
;
269 FOR_EACH_PTR(sm
->possible
, tmp
) {
270 if (tmp
->state
== &locked
)
272 if (tmp
->state
== &unlocked
)
274 if (tmp
->state
== &start_state
) {
275 struct smatch_state
*s
;
277 s
= get_start_state(tmp
);
280 else if (s
== &unlocked
)
285 if (tmp
->state
== &undefined
)
286 undef
= 1; // i don't think this is possible any more.
287 } END_FOR_EACH_PTR(tmp
);
288 if ((islocked
&& isunlocked
) || undef
)
289 sm_msg("warn: '%s' is sometimes locked here and "
290 "sometimes unlocked.", sm
->name
);
293 static void match_return(struct expression
*ret_value
)
295 struct locks_on_return
*ret
;
296 struct state_list
*slist
;
297 struct sm_state
*tmp
;
302 ret
= alloc_return(get_lineno());
304 slist
= get_all_states(my_id
);
305 FOR_EACH_PTR(slist
, tmp
) {
306 if (tmp
->state
== &locked
) {
307 add_tracker(&ret
->locked
, tmp
->owner
, tmp
->name
,
309 } else if (tmp
->state
== &unlocked
) {
310 add_tracker(&ret
->unlocked
, tmp
->owner
, tmp
->name
,
312 } else if (tmp
->state
== &start_state
) {
313 struct smatch_state
*s
;
315 s
= get_start_state(tmp
);
317 add_tracker(&ret
->locked
, tmp
->owner
, tmp
->name
,
320 add_tracker(&ret
->unlocked
, tmp
->owner
,tmp
->name
,
325 } END_FOR_EACH_PTR(tmp
);
327 add_ptr_list(&all_returns
, ret
);
330 static void print_inconsistent_returns(struct tracker
*lock
,
331 struct smatch_state
*start
)
333 struct locks_on_return
*tmp
;
336 sm_printf("%s +%d %s(%d) ", get_filename(), get_lineno(), get_function(), get_func_pos());
337 sm_printf("warn: inconsistent returns %s:", lock
->name
);
338 sm_printf(" locked (");
340 FOR_EACH_PTR(all_returns
, tmp
) {
341 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
, lock
->sym
))
343 if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
, lock
->sym
)) {
346 sm_printf("%d", tmp
->line
);
349 if (start
== &locked
) {
352 sm_printf("%d", tmp
->line
);
354 } END_FOR_EACH_PTR(tmp
);
356 sm_printf(") unlocked (");
358 FOR_EACH_PTR(all_returns
, tmp
) {
359 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
, lock
->sym
)) {
362 sm_printf("%d", tmp
->line
);
365 if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
, lock
->sym
)) {
368 if (start
== &unlocked
) {
371 sm_printf("%d", tmp
->line
);
373 } END_FOR_EACH_PTR(tmp
);
377 static void check_returns_consistently(struct tracker
*lock
,
378 struct smatch_state
*start
)
380 int returns_locked
= 0;
381 int returns_unlocked
= 0;
382 struct locks_on_return
*tmp
;
384 FOR_EACH_PTR(all_returns
, tmp
) {
385 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
,
387 returns_unlocked
= tmp
->line
;
388 else if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
,
390 returns_locked
= tmp
->line
;
391 else if (start
== &locked
)
392 returns_locked
= tmp
->line
;
393 else if (start
== &unlocked
)
394 returns_unlocked
= tmp
->line
;
395 } END_FOR_EACH_PTR(tmp
);
397 if (returns_locked
&& returns_unlocked
)
398 print_inconsistent_returns(lock
, start
);
401 static void check_consistency(struct symbol
*sym
)
408 FOR_EACH_PTR(starts_locked
, tmp
) {
409 if (in_tracker_list(starts_unlocked
, tmp
->owner
, tmp
->name
,
411 sm_msg("error: locking inconsistency. We assume "
412 "'%s' is both locked and unlocked at the "
415 } END_FOR_EACH_PTR(tmp
);
417 FOR_EACH_PTR(starts_locked
, tmp
) {
418 check_returns_consistently(tmp
, &locked
);
419 } END_FOR_EACH_PTR(tmp
);
421 FOR_EACH_PTR(starts_unlocked
, tmp
) {
422 check_returns_consistently(tmp
, &unlocked
);
423 } END_FOR_EACH_PTR(tmp
);
427 static void clear_lists(void)
429 struct locks_on_return
*tmp
;
431 free_trackers_and_list(&starts_locked
);
432 free_trackers_and_list(&starts_unlocked
);
434 FOR_EACH_PTR(all_returns
, tmp
) {
435 free_trackers_and_list(&tmp
->locked
);
436 free_trackers_and_list(&tmp
->unlocked
);
438 } END_FOR_EACH_PTR(tmp
);
439 __free_ptr_list((struct ptr_list
**)&all_returns
);
442 static void match_func_end(struct symbol
*sym
)
444 check_consistency(sym
);
448 static void register_lock(int index
)
450 struct lock_info
*lock
= &lock_table
[index
];
451 void *idx
= (void *)index
;
453 if (lock
->return_type
== ret_non_zero
) {
454 return_implies_state(lock
->function
, 1, POINTER_MAX
, &match_lock_held
, idx
);
455 return_implies_state(lock
->function
, 0, 0, &match_lock_failed
, idx
);
456 } else if (lock
->return_type
== ret_any
) {
457 add_function_hook(lock
->function
, &match_lock_unlock
, idx
);
459 printf("Error: Unhandled lock: %s\n", lock
->function
);
463 void check_wine_locking(int id
)
467 if (option_project
!= PROJ_WINE
)
472 add_unmatched_state_hook(my_id
, &unmatched_state
);
473 add_hook(&match_return
, RETURN_HOOK
);
474 add_hook(&match_func_end
, END_FUNC_HOOK
);
476 for (i
= 0; i
< sizeof(lock_table
)/sizeof(lock_table
[0]); i
++) {
477 if (lock_table
[i
].action
== LOCK
)
480 add_function_hook(lock_table
[i
].function
, &match_lock_unlock
, (void *)i
);