Skip to content

Commit a61aec9

Browse files
committed
C++: Fix ValueNumbering for CopyInstruction
Querying for overlap type wasn't possible when this library was first written. This change fixes FPs in `RedundantNullCheckSimple.ql` on Wireshark and other real-world projects.
1 parent 120df60 commit a61aec9

File tree

5 files changed

+4
-32
lines changed

5 files changed

+4
-32
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/gvn/ValueNumbering.qll

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,10 @@ class ValueNumber extends TValueNumber {
105105
* definition because it accesses the exact same memory.
106106
* The use of `p.x` on line 3 is linked to the definition of `p` on line 1 as well, but is not
107107
* congruent to that definition because `p.x` accesses only a subset of the memory defined by `p`.
108-
*
109-
* This concept should probably be exposed in the public IR API.
110108
*/
111109
private class CongruentCopyInstruction extends CopyInstruction {
112110
CongruentCopyInstruction() {
113-
exists(Instruction def |
114-
def = this.getSourceValue() and
115-
(
116-
def.getResultMemoryAccess() instanceof IndirectMemoryAccess or
117-
def.getResultMemoryAccess() instanceof PhiMemoryAccess or
118-
not def.hasMemoryResult()
119-
)
120-
)
111+
this.getSourceValueOperand().getDefinitionOverlap() instanceof MustExactlyOverlap
121112
}
122113
}
123114

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/gvn/ValueNumbering.qll

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,10 @@ class ValueNumber extends TValueNumber {
105105
* definition because it accesses the exact same memory.
106106
* The use of `p.x` on line 3 is linked to the definition of `p` on line 1 as well, but is not
107107
* congruent to that definition because `p.x` accesses only a subset of the memory defined by `p`.
108-
*
109-
* This concept should probably be exposed in the public IR API.
110108
*/
111109
private class CongruentCopyInstruction extends CopyInstruction {
112110
CongruentCopyInstruction() {
113-
exists(Instruction def |
114-
def = this.getSourceValue() and
115-
(
116-
def.getResultMemoryAccess() instanceof IndirectMemoryAccess or
117-
def.getResultMemoryAccess() instanceof PhiMemoryAccess or
118-
not def.hasMemoryResult()
119-
)
120-
)
111+
this.getSourceValueOperand().getDefinitionOverlap() instanceof MustExactlyOverlap
121112
}
122113
}
123114

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/gvn/ValueNumbering.qll

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,10 @@ class ValueNumber extends TValueNumber {
105105
* definition because it accesses the exact same memory.
106106
* The use of `p.x` on line 3 is linked to the definition of `p` on line 1 as well, but is not
107107
* congruent to that definition because `p.x` accesses only a subset of the memory defined by `p`.
108-
*
109-
* This concept should probably be exposed in the public IR API.
110108
*/
111109
private class CongruentCopyInstruction extends CopyInstruction {
112110
CongruentCopyInstruction() {
113-
exists(Instruction def |
114-
def = this.getSourceValue() and
115-
(
116-
def.getResultMemoryAccess() instanceof IndirectMemoryAccess or
117-
def.getResultMemoryAccess() instanceof PhiMemoryAccess or
118-
not def.hasMemoryResult()
119-
)
120-
)
111+
this.getSourceValueOperand().getDefinitionOverlap() instanceof MustExactlyOverlap
121112
}
122113
}
123114

cpp/ql/test/query-tests/Likely Bugs/RedundantNullCheckSimple/RedundantNullCheckSimple.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ struct S {
105105
long **pplong;
106106

107107
void test_phi() {
108-
while (*pplong != nullptr) { // GOOD [FALSE POSITIVE]
108+
while (*pplong != nullptr) { // GOOD
109109
pplong++;
110110
}
111111
}

cpp/ql/test/query-tests/Likely Bugs/RedundantNullCheckSimple/RedundantNullCheckSimple.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
| RedundantNullCheckSimple.cpp:48:12:48:12 | Load: p | This null check is redundant because the value is $@ in any case | RedundantNullCheckSimple.cpp:51:10:51:11 | Load: * ... | dereferenced here |
44
| RedundantNullCheckSimple.cpp:79:7:79:9 | Load: * ... | This null check is redundant because the value is $@ in any case | RedundantNullCheckSimple.cpp:78:7:78:10 | Load: * ... | dereferenced here |
55
| RedundantNullCheckSimple.cpp:93:13:93:13 | Load: p | This null check is redundant because the value is $@ in any case | RedundantNullCheckSimple.cpp:92:13:92:18 | Load: * ... | dereferenced here |
6-
| RedundantNullCheckSimple.cpp:108:12:108:18 | Load: * ... | This null check is redundant because the value is $@ in any case | RedundantNullCheckSimple.cpp:108:12:108:18 | Load: * ... | dereferenced here |

0 commit comments

Comments
 (0)