Skip to content

Commit f5d8a70

Browse files
committed
import
1 parent 27874f0 commit f5d8a70

File tree

6 files changed

+84
-72
lines changed

6 files changed

+84
-72
lines changed

cpp/common/src/codingstandards/cpp/exclusions/c/Concurrency3.qll

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,12 @@ import RuleMetadata
44
import codingstandards.cpp.exclusions.RuleMetadata
55

66
newtype Concurrency3Query =
7-
TCleanUpThreadSpecificStorageQuery() or
87
TDoNotAllowAMutexToGoOutOfScopeWhileLockedQuery() or
98
TDoNotDestroyAMutexWhileItIsLockedQuery() or
10-
TDeclareThreadsWithAppropriateStorageDurationsQuery() or
119
TPreserveSafetyWhenUsingConditionVariablesQuery() or
12-
TThreadPreviouslyJoinedOrDetachedQuery() or
13-
TDoNotReferToAnAtomicVariableTwiceInExpressionQuery() or
1410
TWrapFunctionsThatCanFailSpuriouslyInLoopQuery()
1511

1612
predicate isConcurrency3QueryMetadata(Query query, string queryId, string ruleId) {
17-
query =
18-
// `Query` instance for the `cleanUpThreadSpecificStorage` query
19-
Concurrency3Package::cleanUpThreadSpecificStorageQuery() and
20-
queryId =
21-
// `@id` for the `cleanUpThreadSpecificStorage` query
22-
"c/cert/clean-up-thread-specific-storage" and
23-
ruleId = "CON30-C"
24-
or
2513
query =
2614
// `Query` instance for the `doNotAllowAMutexToGoOutOfScopeWhileLocked` query
2715
Concurrency3Package::doNotAllowAMutexToGoOutOfScopeWhileLockedQuery() and
@@ -38,14 +26,6 @@ predicate isConcurrency3QueryMetadata(Query query, string queryId, string ruleId
3826
"c/cert/do-not-destroy-a-mutex-while-it-is-locked" and
3927
ruleId = "CON31-C"
4028
or
41-
query =
42-
// `Query` instance for the `declareThreadsWithAppropriateStorageDurations` query
43-
Concurrency3Package::declareThreadsWithAppropriateStorageDurationsQuery() and
44-
queryId =
45-
// `@id` for the `declareThreadsWithAppropriateStorageDurations` query
46-
"c/cert/declare-threads-with-appropriate-storage-durations" and
47-
ruleId = "CON34-C"
48-
or
4929
query =
5030
// `Query` instance for the `preserveSafetyWhenUsingConditionVariables` query
5131
Concurrency3Package::preserveSafetyWhenUsingConditionVariablesQuery() and
@@ -54,22 +34,6 @@ predicate isConcurrency3QueryMetadata(Query query, string queryId, string ruleId
5434
"c/cert/preserve-safety-when-using-condition-variables" and
5535
ruleId = "CON38-C"
5636
or
57-
query =
58-
// `Query` instance for the `threadPreviouslyJoinedOrDetached` query
59-
Concurrency3Package::threadPreviouslyJoinedOrDetachedQuery() and
60-
queryId =
61-
// `@id` for the `threadPreviouslyJoinedOrDetached` query
62-
"c/cert/thread-previously-joined-or-detached" and
63-
ruleId = "CON39-C"
64-
or
65-
query =
66-
// `Query` instance for the `doNotReferToAnAtomicVariableTwiceInExpression` query
67-
Concurrency3Package::doNotReferToAnAtomicVariableTwiceInExpressionQuery() and
68-
queryId =
69-
// `@id` for the `doNotReferToAnAtomicVariableTwiceInExpression` query
70-
"c/cert/do-not-refer-to-an-atomic-variable-twice-in-expression" and
71-
ruleId = "CON40-C"
72-
or
7337
query =
7438
// `Query` instance for the `wrapFunctionsThatCanFailSpuriouslyInLoop` query
7539
Concurrency3Package::wrapFunctionsThatCanFailSpuriouslyInLoopQuery() and
@@ -80,13 +44,6 @@ predicate isConcurrency3QueryMetadata(Query query, string queryId, string ruleId
8044
}
8145

8246
module Concurrency3Package {
83-
Query cleanUpThreadSpecificStorageQuery() {
84-
//autogenerate `Query` type
85-
result =
86-
// `Query` type for `cleanUpThreadSpecificStorage` query
87-
TQueryC(TConcurrency3PackageQuery(TCleanUpThreadSpecificStorageQuery()))
88-
}
89-
9047
Query doNotAllowAMutexToGoOutOfScopeWhileLockedQuery() {
9148
//autogenerate `Query` type
9249
result =
@@ -101,34 +58,13 @@ module Concurrency3Package {
10158
TQueryC(TConcurrency3PackageQuery(TDoNotDestroyAMutexWhileItIsLockedQuery()))
10259
}
10360

104-
Query declareThreadsWithAppropriateStorageDurationsQuery() {
105-
//autogenerate `Query` type
106-
result =
107-
// `Query` type for `declareThreadsWithAppropriateStorageDurations` query
108-
TQueryC(TConcurrency3PackageQuery(TDeclareThreadsWithAppropriateStorageDurationsQuery()))
109-
}
110-
11161
Query preserveSafetyWhenUsingConditionVariablesQuery() {
11262
//autogenerate `Query` type
11363
result =
11464
// `Query` type for `preserveSafetyWhenUsingConditionVariables` query
11565
TQueryC(TConcurrency3PackageQuery(TPreserveSafetyWhenUsingConditionVariablesQuery()))
11666
}
11767

118-
Query threadPreviouslyJoinedOrDetachedQuery() {
119-
//autogenerate `Query` type
120-
result =
121-
// `Query` type for `threadPreviouslyJoinedOrDetached` query
122-
TQueryC(TConcurrency3PackageQuery(TThreadPreviouslyJoinedOrDetachedQuery()))
123-
}
124-
125-
Query doNotReferToAnAtomicVariableTwiceInExpressionQuery() {
126-
//autogenerate `Query` type
127-
result =
128-
// `Query` type for `doNotReferToAnAtomicVariableTwiceInExpression` query
129-
TQueryC(TConcurrency3PackageQuery(TDoNotReferToAnAtomicVariableTwiceInExpressionQuery()))
130-
}
131-
13268
Query wrapFunctionsThatCanFailSpuriouslyInLoopQuery() {
13369
//autogenerate `Query` type
13470
result =

cpp/common/src/codingstandards/cpp/exclusions/cpp/Concurrency.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ newtype ConcurrencyQuery =
1010
TPreventBitFieldAccessFromMultipleThreadsQuery() or
1111
TDeadlockByLockingInPredefinedOrderQuery() or
1212
TWrapFunctionsThatCanSpuriouslyWakeUpInLoopQuery() or
13-
TPreserveThreadSafetyAndLivenessWhenUsingConditionVariablesQuery() or
13+
TPreserveSafetyWhenUsingConditionVariablesQuery() or
1414
TDoNotSpeculativelyLockALockedNonRecursiveMutexQuery() or
1515
TLockedALockedNonRecursiveMutexAuditQuery()
1616

@@ -64,11 +64,11 @@ predicate isConcurrencyQueryMetadata(Query query, string queryId, string ruleId)
6464
ruleId = "CON54-CPP"
6565
or
6666
query =
67-
// `Query` instance for the `preserveThreadSafetyAndLivenessWhenUsingConditionVariables` query
68-
ConcurrencyPackage::preserveThreadSafetyAndLivenessWhenUsingConditionVariablesQuery() and
67+
// `Query` instance for the `preserveSafetyWhenUsingConditionVariables` query
68+
ConcurrencyPackage::preserveSafetyWhenUsingConditionVariablesQuery() and
6969
queryId =
70-
// `@id` for the `preserveThreadSafetyAndLivenessWhenUsingConditionVariables` query
71-
"cpp/cert/preserve-thread-safety-and-liveness-when-using-condition-variables" and
70+
// `@id` for the `preserveSafetyWhenUsingConditionVariables` query
71+
"cpp/cert/preserve-safety-when-using-condition-variables" and
7272
ruleId = "CON55-CPP"
7373
or
7474
query =
@@ -131,11 +131,11 @@ module ConcurrencyPackage {
131131
TQueryCPP(TConcurrencyPackageQuery(TWrapFunctionsThatCanSpuriouslyWakeUpInLoopQuery()))
132132
}
133133

134-
Query preserveThreadSafetyAndLivenessWhenUsingConditionVariablesQuery() {
134+
Query preserveSafetyWhenUsingConditionVariablesQuery() {
135135
//autogenerate `Query` type
136136
result =
137-
// `Query` type for `preserveThreadSafetyAndLivenessWhenUsingConditionVariables` query
138-
TQueryCPP(TConcurrencyPackageQuery(TPreserveThreadSafetyAndLivenessWhenUsingConditionVariablesQuery()))
137+
// `Query` type for `preserveSafetyWhenUsingConditionVariables` query
138+
TQueryCPP(TConcurrencyPackageQuery(TPreserveSafetyWhenUsingConditionVariablesQuery()))
139139
}
140140

141141
Query doNotSpeculativelyLockALockedNonRecursiveMutexQuery() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Provides a library which includes a `problems` predicate for reporting....
3+
*/
4+
5+
import cpp
6+
import codingstandards.cpp.Customizations
7+
import codingstandards.cpp.Exclusions
8+
import codingstandards.cpp.Concurrency
9+
10+
abstract class PreserveSafetyWhenUsingConditionVariablesSharedQuery extends Query { }
11+
12+
Query getQuery() { result instanceof PreserveSafetyWhenUsingConditionVariablesSharedQuery }
13+
14+
/**
15+
* Models a notification arising from a conditional variable.
16+
*/
17+
abstract class ConditionalNotification extends FunctionCall {
18+
abstract predicate isNotifyOne();
19+
}
20+
21+
class CPPConditionalNotification extends ConditionalNotification {
22+
string name;
23+
24+
CPPConditionalNotification() {
25+
exists(MemberFunction mf |
26+
mf = getTarget() and
27+
mf.getDeclaringType().hasQualifiedName("std", "condition_variable") and
28+
mf.getName() = name
29+
)
30+
}
31+
32+
override predicate isNotifyOne() { name in ["notify_one"] }
33+
}
34+
35+
class C11ConditionalNotification extends ConditionalNotification {
36+
string name;
37+
38+
C11ConditionalNotification() {
39+
exists(Function mf |
40+
mf = getTarget() and
41+
mf.getName() = ["cnd_signal", "cnd_broadcast"] and
42+
mf.getName() = name
43+
)
44+
}
45+
46+
override predicate isNotifyOne() { name in ["cnd_signal"] }
47+
}
48+
49+
/*
50+
* This query works by looking for single dispatch notifications in the context of a
51+
* function that is used in a thread.
52+
*
53+
* To avoid this problem a programmer may use `notify_all` or `cnd_broadcast` or use unique
54+
* condition variables. The problem of checking for correct usage of multiple
55+
* condition variables is especially non-trivial and thus this query
56+
* conservatively over-approximates potential issues with condition variables.
57+
*
58+
* Note that the check for using conditional variables within a loop is covered
59+
* by CON54-CPP
60+
*/
61+
62+
query predicate problems(ConditionalNotification cn, string message) {
63+
not isExcluded(cn, getQuery()) and
64+
exists(ThreadedFunction tf |
65+
// the problematic types of uses of conditional variables
66+
// are the cases where single dispatch notification is used.
67+
cn.isNotifyOne() and
68+
// to be problematic this function should actually be used in a thread
69+
cn.getEnclosingFunction() = tf
70+
) and
71+
message =
72+
"Possible unsafe usage of single dispatch notification which can lead to deadlocking of threads."
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No expected results have yet been specified
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.preservesafetywhenusingconditionvariables.PreserveSafetyWhenUsingConditionVariables

0 commit comments

Comments
 (0)