Skip to content

Commit a1bf303

Browse files
committed
A20-8-1/MEM56-CPP: Fix compiler compat issues
Fix false negative issues related to the library structure of smart pointers. This commit makes the following changes: * Update `memory` stubs to move more functions to the __shared_ptr base class * Add dataflow summaries for smart pointer constructor calls and smart pointer get calls. * Add sanitizers to prevent flow into library code for the dataflow summaries added above.
1 parent 7e49686 commit a1bf303

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

cpp/common/src/codingstandards/cpp/SmartPointers.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ abstract class AutosarSmartPointer extends Class {
2929
)
3030
}
3131

32+
FunctionCall getAGetCall() {
33+
result.getTarget().hasName("get") and
34+
result.getQualifier().getType().stripType() = this
35+
}
36+
3237
FunctionCall getAnInitializerExpr() {
3338
result =
3439
any(FunctionCall fc |

cpp/common/src/codingstandards/cpp/rules/ownedpointervaluestoredinunrelatedsmartpointer/OwnedPointerValueStoredInUnrelatedSmartPointer.qll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,34 @@ private class PointerToSmartPointerConstructorFlowConfig extends TaintTracking::
2929
cc.getArgument(0) = sink.asExpr()
3030
)
3131
}
32+
33+
override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) {
34+
// Summarize flow through constructor calls
35+
exists(AutosarSmartPointer sp, ConstructorCall cc |
36+
sp.getAConstructorCall() = cc and
37+
cc = node2.asExpr() and
38+
cc.getArgument(0) = node1.asExpr()
39+
)
40+
or
41+
// Summarize flow through get() calls
42+
exists(AutosarSmartPointer sp, FunctionCall fc |
43+
sp.getAGetCall() = fc and
44+
fc = node2.asExpr() and
45+
fc.getQualifier() = node1.asExpr()
46+
)
47+
}
48+
49+
override predicate isSanitizerIn(DataFlow::Node node) {
50+
// Exclude flow into header files outside the source archive which are summarized by the
51+
// additional taint steps above.
52+
exists(AutosarSmartPointer sp |
53+
sp.getAConstructorCall().getTarget().getAParameter() = node.asParameter()
54+
or
55+
sp.getAGetCall().getTarget().getAParameter() = node.asParameter()
56+
|
57+
not exists(node.getLocation().getFile().getRelativePath())
58+
)
59+
}
3260
}
3361

3462
query predicate problems(

cpp/common/test/includes/standard-library/memory.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ template <typename T> class __shared_ptr {
7575
template <class Y> void reset(Y *p);
7676
template <class Y, class D> void reset(Y *p, D d);
7777
template <class Y, class D, class A> void reset(Y *p, D d, A a);
78+
79+
long use_count() const noexcept;
80+
T *get() const noexcept;
7881
};
7982

8083
template <typename T> class shared_ptr : public __shared_ptr<T> {
@@ -90,8 +93,6 @@ template <typename T> class shared_ptr : public __shared_ptr<T> {
9093
T &operator*() const noexcept;
9194
T *operator->() const noexcept;
9295

93-
long use_count() const noexcept { return 0; }
94-
T *get() const noexcept { return ptr; }
9596
shared_ptr<T> &operator=(const shared_ptr &) {}
9697
shared_ptr<T> &operator=(shared_ptr &&) { return *this; }
9798
template <typename S> shared_ptr &operator=(shared_ptr<T> &&) {

cpp/common/test/rules/ownedpointervaluestoredinunrelatedsmartpointer/OwnedPointerValueStoredInUnrelatedSmartPointer.expected

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,18 @@ problems
66
| test.cpp:12:28:12:29 | v2 | test.cpp:10:8:10:17 | new | test.cpp:12:28:12:29 | v2 | Raw pointer flows to initialize multiple unrelated smart pointers. |
77
| test.cpp:17:27:17:28 | v1 | test.cpp:16:13:16:22 | new | test.cpp:17:27:17:28 | v1 | Raw pointer flows to initialize multiple unrelated smart pointers. |
88
edges
9-
| ../../includes/standard-library/memory.h:76:17:76:19 | ptr | ../../includes/standard-library/memory.h:76:17:76:19 | ptr |
109
| test.cpp:3:14:3:15 | v1 | test.cpp:5:27:5:28 | v1 |
1110
| test.cpp:3:14:3:15 | v1 | test.cpp:6:31:6:33 | call to get |
1211
| test.cpp:3:14:3:15 | v1 | test.cpp:7:28:7:29 | v2 |
1312
| test.cpp:4:13:4:14 | v1 | test.cpp:7:28:7:29 | v2 |
1413
| test.cpp:5:27:5:29 | call to shared_ptr | test.cpp:6:31:6:33 | call to get |
1514
| test.cpp:8:8:8:14 | 0 | test.cpp:9:28:9:29 | v2 |
1615
| test.cpp:10:8:10:17 | new | test.cpp:11:28:11:29 | v2 |
17-
| test.cpp:10:8:10:17 | new | test.cpp:11:28:11:29 | v2 |
1816
| test.cpp:10:8:10:17 | new | test.cpp:12:28:12:29 | v2 |
19-
| test.cpp:11:28:11:29 | ref arg v2 | test.cpp:12:28:12:29 | v2 |
20-
| test.cpp:11:28:11:29 | v2 | ../../includes/standard-library/memory.h:76:17:76:19 | ptr |
21-
| test.cpp:11:28:11:29 | v2 | test.cpp:11:28:11:29 | ref arg v2 |
22-
| test.cpp:16:13:16:22 | new | test.cpp:17:27:17:28 | v1 |
2317
| test.cpp:16:13:16:22 | new | test.cpp:17:27:17:28 | v1 |
2418
| test.cpp:16:13:16:22 | new | test.cpp:19:6:19:7 | v1 |
25-
| test.cpp:17:27:17:28 | ref arg v1 | test.cpp:19:6:19:7 | v1 |
26-
| test.cpp:17:27:17:28 | v1 | ../../includes/standard-library/memory.h:76:17:76:19 | ptr |
27-
| test.cpp:17:27:17:28 | v1 | test.cpp:17:27:17:28 | ref arg v1 |
2819
| test.cpp:19:6:19:7 | v1 | test.cpp:3:14:3:15 | v1 |
2920
nodes
30-
| ../../includes/standard-library/memory.h:76:17:76:19 | ptr | semmle.label | ptr |
31-
| ../../includes/standard-library/memory.h:76:17:76:19 | ptr | semmle.label | ptr |
3221
| test.cpp:3:14:3:15 | v1 | semmle.label | v1 |
3322
| test.cpp:4:13:4:14 | v1 | semmle.label | v1 |
3423
| test.cpp:5:27:5:28 | v1 | semmle.label | v1 |
@@ -38,15 +27,9 @@ nodes
3827
| test.cpp:8:8:8:14 | 0 | semmle.label | 0 |
3928
| test.cpp:9:28:9:29 | v2 | semmle.label | v2 |
4029
| test.cpp:10:8:10:17 | new | semmle.label | new |
41-
| test.cpp:11:28:11:29 | ref arg v2 | semmle.label | ref arg v2 |
42-
| test.cpp:11:28:11:29 | v2 | semmle.label | v2 |
4330
| test.cpp:11:28:11:29 | v2 | semmle.label | v2 |
4431
| test.cpp:12:28:12:29 | v2 | semmle.label | v2 |
4532
| test.cpp:16:13:16:22 | new | semmle.label | new |
46-
| test.cpp:17:27:17:28 | ref arg v1 | semmle.label | ref arg v1 |
47-
| test.cpp:17:27:17:28 | v1 | semmle.label | v1 |
4833
| test.cpp:17:27:17:28 | v1 | semmle.label | v1 |
4934
| test.cpp:19:6:19:7 | v1 | semmle.label | v1 |
5035
subpaths
51-
| test.cpp:11:28:11:29 | v2 | ../../includes/standard-library/memory.h:76:17:76:19 | ptr | ../../includes/standard-library/memory.h:76:17:76:19 | ptr | test.cpp:11:28:11:29 | ref arg v2 |
52-
| test.cpp:17:27:17:28 | v1 | ../../includes/standard-library/memory.h:76:17:76:19 | ptr | ../../includes/standard-library/memory.h:76:17:76:19 | ptr | test.cpp:17:27:17:28 | ref arg v1 |

0 commit comments

Comments
 (0)