Skip to content

Commit 4193fa6

Browse files
committed
CON40-C
1 parent 4081fc8 commit 4193fa6

15 files changed

+266
-0
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"files.associations": {
3+
"atomic": "c",
4+
"memory": "c"
5+
}
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# CON39-C: Do not join or detach a thread that was previously joined or detached
2+
3+
This query implements the CERT-C rule CON39-C:
4+
5+
> Do not join or detach a thread that was previously joined or detached
6+
7+
8+
## CERT
9+
10+
** REPLACE THIS BY RUNNING THE SCRIPT `scripts/help/cert-help-extraction.py` **
11+
12+
## Implementation notes
13+
14+
None
15+
16+
## References
17+
18+
* CERT-C: [CON39-C: Do not join or detach a thread that was previously joined or detached](https://wiki.sei.cmu.edu/confluence/display/c)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @id c/cert/thread-was-previously-joined-or-detached
3+
* @name CON39-C: Do not join or detach a thread that was previously joined or detached
4+
* @description Joining or detaching a previously joined or detached thread can lead to undefined
5+
* program behavior.
6+
* @kind problem
7+
* @precision high
8+
* @problem.severity error
9+
* @tags external/cert/id/con39-c
10+
* correctness
11+
* concurrency
12+
* external/cert/obligation/rule
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.cert
17+
18+
19+
20+
/**
21+
* Strategy for this one is to ensure that there are not two sinks to thrd_join
22+
or thrd_detach for a given
23+
24+
25+
Truth table:
26+
27+
Error if:
28+
29+
thread calls detach, parent calls join
30+
thread calls
31+
32+
Make sure there aren't multiple calls to join? Very had to do in practice.
33+
34+
You should call join OR detach, but not both.
35+
*/
36+
37+
from
38+
where
39+
not isExcluded(x, Concurrency5Package::threadWasPreviouslyJoinedOrDetachedQuery()) and
40+
select
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# CON40-C: Do not refer to an atomic variable twice in an expression
2+
3+
This query implements the CERT-C rule CON40-C:
4+
5+
> Do not refer to an atomic variable twice in an expression
6+
7+
8+
## CERT
9+
10+
** REPLACE THIS BY RUNNING THE SCRIPT `scripts/help/cert-help-extraction.py` **
11+
12+
## Implementation notes
13+
14+
None
15+
16+
## References
17+
18+
* CERT-C: [CON40-C: Do not refer to an atomic variable twice in an expression](https://wiki.sei.cmu.edu/confluence/display/c)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @id c/cert/atomic-variable-twice-in-expression
3+
* @name CON40-C: Do not refer to an atomic variable twice in an expression
4+
* @description Atomic variables that are referred to twice in the same expression can produce
5+
* unpredictable program behavior.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/cert/id/con40-c
10+
* correctness
11+
* concurrency
12+
* external/cert/obligation/rule
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.cert
17+
18+
from MacroInvocation mi, Variable v, Locatable whereFound
19+
where
20+
not isExcluded(whereFound, Concurrency5Package::atomicVariableTwiceInExpressionQuery()) and
21+
(
22+
// There isn't a way to safely use this construct in a away that is also
23+
// possible the reliably detect so advise against using it.
24+
(
25+
mi.getMacroName() = ["atomic_store", "atomic_store_explicit"]
26+
or
27+
// This construct is generally safe, but must be used in a loop. To lower
28+
// the false positive rate we don't look at the conditions of the loop and
29+
// instead assume if it is found in a looping construct that it is likely
30+
// related to the safety property.
31+
mi.getMacroName() = ["atomic_compare_exchange_weak", "atomic_compare_exchange_weak_explicit"] and
32+
not exists(Loop l | mi.getAGeneratedElement().(Expr).getParent*() = l)
33+
) and
34+
whereFound = mi
35+
)
36+
or
37+
mi.getMacroName() = "ATOMIC_VAR_INIT" and
38+
exists(Expr av |
39+
av = mi.getAGeneratedElement() and
40+
av = v.getAnAssignedValue() and
41+
exists(Assignment m |
42+
not m instanceof AssignXorExpr and
43+
m.getLValue().(VariableAccess).getTarget() = v and
44+
whereFound = m
45+
)
46+
)
47+
select mi, "Atomic variable possibly referred to twice in an $@.", whereFound, "expression"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No expected results have yet been specified
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/CON39-C/ThreadWasPreviouslyJoinedOrDetached.ql

c/cert/test/rules/CON39-C/test.c

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| test.c:6:19:6:40 | ATOMIC_VAR_INIT(value) | Atomic variable possibly referred to twice in an $@. | test.c:32:3:32:10 | ... += ... | expression |
2+
| test.c:6:19:6:40 | ATOMIC_VAR_INIT(value) | Atomic variable possibly referred to twice in an $@. | test.c:33:3:33:13 | ... = ... | expression |
3+
| test.c:10:3:10:23 | atomic_store(a,b) | Atomic variable possibly referred to twice in an $@. | test.c:10:3:10:23 | atomic_store(a,b) | expression |
4+
| test.c:11:3:11:35 | atomic_store_explicit(a,b,c) | Atomic variable possibly referred to twice in an $@. | test.c:11:3:11:35 | atomic_store_explicit(a,b,c) | expression |
5+
| test.c:24:3:24:48 | atomic_compare_exchange_weak(a,b,c) | Atomic variable possibly referred to twice in an $@. | test.c:24:3:24:48 | atomic_compare_exchange_weak(a,b,c) | expression |
6+
| test.c:25:3:26:45 | atomic_compare_exchange_weak_explicit(a,b,c,d,e) | Atomic variable possibly referred to twice in an $@. | test.c:25:3:26:45 | atomic_compare_exchange_weak_explicit(a,b,c,d,e) | expression |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/CON40-C/AtomicVariableTwiceInExpression.ql

0 commit comments

Comments
 (0)