-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathBusyWait.ql
More file actions
69 lines (60 loc) · 1.96 KB
/
BusyWait.ql
File metadata and controls
69 lines (60 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @name Busy wait
* @description Calling 'Thread.sleep' to control thread interaction is
* less effective than waiting for a notification and may also
* result in race conditions. Merely synchronizing over shared
* variables in a loop to control thread interaction
* may waste system resources and cause performance problems.
* @kind problem
* @problem.severity warning
* @precision low
* @id java/busy-wait
* @tags reliability
* correctness
* concurrency
*/
import java
class SleepMethod extends Method {
SleepMethod() {
this.getName() = "sleep" and
this.getDeclaringType().hasQualifiedName("java.lang", "Thread")
}
}
class SleepMethodCall extends MethodCall {
SleepMethodCall() { this.getMethod() instanceof SleepMethod }
}
class WaitMethod extends Method {
WaitMethod() {
this.getName() = "wait" and
this.getDeclaringType() instanceof TypeObject
}
}
class ConcurrentMethod extends Method {
ConcurrentMethod() { this.getDeclaringType().getQualifiedName().matches("java.util.concurrent%") }
}
class CommunicationMethod extends Method {
CommunicationMethod() {
this instanceof WaitMethod or
this instanceof ConcurrentMethod
}
}
predicate callsCommunicationMethod(Method source) {
source instanceof CommunicationMethod
or
exists(MethodCall a, Method overridingMethod, Method target |
callsCommunicationMethod(overridingMethod) and
overridingMethod.overridesOrInstantiates*(target) and
target = a.getMethod() and
a.getEnclosingCallable() = source
)
}
class DangerStmt extends Stmt {
DangerStmt() { exists(SleepMethodCall sleep | sleep.getEnclosingStmt() = this) }
}
from WhileStmt s, DangerStmt d
where
d.getEnclosingStmt+() = s and
not exists(MethodCall call | callsCommunicationMethod(call.getMethod()) |
call.getEnclosingStmt().getEnclosingStmt*() = s
)
select d, "Prefer wait/notify or java.util.concurrent to communicate between threads."