Skip to content

Commit 569f3c9

Browse files
committed
C++: Don't do indirect (instruction -> operand) flow when there's a store to the address in between the instruction and the operand.
1 parent f662cce commit 569f3c9

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,25 @@ private module Cached {
15201520
)
15211521
}
15221522

1523+
/**
1524+
* Holds if `operand.getDef() = instr`, but there exists a `StoreInstruction` that
1525+
* writes to an address that is equivalent to the value computed by `instr` in
1526+
* between `instr` and `operand`, and therefore there should not be flow from `*instr`
1527+
* to `*operand`.
1528+
*/
1529+
pragma[nomagic]
1530+
private predicate isStoredToBetween(Instruction instr, Operand operand) {
1531+
simpleOperandLocalFlowStep(pragma[only_bind_into](instr), pragma[only_bind_into](operand)) and
1532+
exists(StoreInstruction store, IRBlock block, int storeIndex, int instrIndex, int operandIndex |
1533+
store.getDestinationAddress() = instr and
1534+
block.getInstruction(storeIndex) = store and
1535+
block.getInstruction(instrIndex) = instr and
1536+
block.getInstruction(operandIndex) = operand.getUse() and
1537+
instrIndex < storeIndex and
1538+
storeIndex < operandIndex
1539+
)
1540+
}
1541+
15231542
private predicate indirectionInstructionFlow(
15241543
RawIndirectInstruction nodeFrom, IndirectOperand nodeTo
15251544
) {
@@ -1529,7 +1548,8 @@ private module Cached {
15291548
simpleOperandLocalFlowStep(pragma[only_bind_into](instr), pragma[only_bind_into](operand))
15301549
|
15311550
hasOperandAndIndex(nodeTo, operand, pragma[only_bind_into](indirectionIndex)) and
1532-
hasInstructionAndIndex(nodeFrom, instr, pragma[only_bind_into](indirectionIndex))
1551+
hasInstructionAndIndex(nodeFrom, instr, pragma[only_bind_into](indirectionIndex)) and
1552+
not isStoredToBetween(instr, operand)
15331553
)
15341554
}
15351555

0 commit comments

Comments
 (0)