Skip to content

Commit 078bb9a

Browse files
committed
[CPP-435] A much-improved IR query, still some false negatives.
1 parent f1a19d3 commit 078bb9a

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

cpp/ql/src/Likely Bugs/Memory Management/MemsetMayBeDeleted.ql

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ class MemsetCallInstruction extends CallInstruction {
2222

2323
Instruction getAUseInstruction(Instruction insn) { result = insn.getAUse().getUse() }
2424

25+
predicate pointsIntoStack(Instruction instr) {
26+
instr.(VariableAddressInstruction).getIRVariable() instanceof IRAutomaticVariable
27+
or
28+
pointsIntoStack(instr.(CopyInstruction).getSourceValue())
29+
or
30+
pointsIntoStack(instr.(ConvertInstruction).getUnary())
31+
}
32+
2533
from MemsetCallInstruction memset, SizedBufferMustWriteSideEffectInstruction sei
2634
where
2735
sei.getPrimaryInstruction() = memset and
28-
forall(Instruction use | use = getAUseInstruction+(sei) | use instanceof ChiInstruction) and
29-
exists(Instruction def | memset.getPositionalArgument(0) = getAUseInstruction+(def) |
30-
def instanceof UninitializedInstruction
31-
)
36+
// The first argument to memset must reside on the stack
37+
pointsIntoStack(valueNumber(memset.getPositionalArgument(0)).getAnInstruction()) and
38+
// The result of memset may not be subsequently used
39+
forall(Instruction use | use = getAUseInstruction+(sei) | use instanceof ChiInstruction)
3240
select memset,
3341
"Call to " + memset.getStaticCallTarget().getName() + " may be deleted by the compiler."

cpp/ql/test/query-tests/Likely Bugs/Memory Management/MemsetMayBeDeleted/MemsetMayBeDeleted.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int func3(void) {
8585
int func4(void) {
8686
char pw1a[PW_SIZE];
8787
use_pw(pw1a);
88-
__builtin_memset(pw1a + 3, 0, PW_SIZE - 3); // BAD
88+
__builtin_memset(pw1a + 3, 0, PW_SIZE - 3); // BAD [NOT DETECTED]
8989
return 0;
9090
}
9191

@@ -115,7 +115,7 @@ int func5(void) {
115115
int func7(void) {
116116
char pw1a[PW_SIZE];
117117
use_pw(pw1a);
118-
__builtin_memset(&pw1a[3], 0, PW_SIZE - 5); // BAD
118+
__builtin_memset(&pw1a[3], 0, PW_SIZE - 5); // BAD [NOT DETECTED]
119119
return 0;
120120
}
121121

cpp/ql/test/query-tests/Likely Bugs/Memory Management/MemsetMayBeDeleted/MemsetMayBeDeleted.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void func3(unsigned long long sz) {
4848
// x64 msvc v19.22: deleted
4949
void func4(unsigned long long sz) {
5050
char buff[128];
51-
memset(buff, 0, PW_SIZE); // BAD
51+
memset(buff, 0, PW_SIZE); // BAD [NOT DETECTED]
5252
strcpy(buff, "Hello");
5353
}
5454

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
WARNING: Unused predicate insnDominates (/mnt/c/code/ql/cpp/ql/src/Likely Bugs/Memory Management/MemsetMayBeDeleted.ql:27,19-32)
21
| MemsetMayBeDeleted.c:19:2:19:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
32
| MemsetMayBeDeleted.c:29:2:29:17 | Call: call to __builtin_memset | Call to __builtin_memset may be deleted by the compiler. |
43
| MemsetMayBeDeleted.c:39:2:39:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
5-
| MemsetMayBeDeleted.c:59:2:59:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
6-
| MemsetMayBeDeleted.c:79:2:79:17 | Call: call to __builtin_memset | Call to __builtin_memset may be deleted by the compiler. |
7-
| MemsetMayBeDeleted.c:109:2:109:17 | Call: call to __builtin_memset | Call to __builtin_memset may be deleted by the compiler. |
8-
| MemsetMayBeDeleted.c:129:2:129:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
4+
| MemsetMayBeDeleted.c:68:2:68:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
5+
| MemsetMayBeDeleted.c:138:2:138:7 | Call: call to memset | Call to memset may be deleted by the compiler. |
96
| MemsetMayBeDeleted.cpp:43:5:43:10 | Call: call to memset | Call to memset may be deleted by the compiler. |
10-
| MemsetMayBeDeleted.cpp:51:5:51:10 | Call: call to memset | Call to memset may be deleted by the compiler. |
117
| MemsetMayBeDeleted.cpp:71:5:71:10 | Call: call to memset | Call to memset may be deleted by the compiler. |
128
| MemsetMayBeDeleted.cpp:79:5:79:10 | Call: call to memset | Call to memset may be deleted by the compiler. |

0 commit comments

Comments
 (0)