Skip to content

Commit 3fcb825

Browse files
committed
C++: Change a few indirectionIndex ranges from '[0 .. n - 1]' to '[1 .. n]'. This simplifies some arithmetic in a few predicates.
1 parent a856bc8 commit 3fcb825

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

cpp/ql/lib/experimental/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private newtype TReturnKind =
137137
exists(IndirectReturnNode return, ReturnIndirectionInstruction returnInd |
138138
returnInd.hasIndex(argumentIndex) and
139139
return.getAddressOperand() = returnInd.getSourceAddressOperand() and
140-
indirectionIndex = return.getIndirectionIndex() - 1 // We subtract one because the return loads the value.
140+
indirectionIndex = return.getIndirectionIndex()
141141
)
142142
}
143143

@@ -197,7 +197,7 @@ class ReturnIndirectionNode extends IndirectReturnNode, ReturnNode {
197197
exists(int argumentIndex, ReturnIndirectionInstruction returnInd |
198198
returnInd.hasIndex(argumentIndex) and
199199
this.getAddressOperand() = returnInd.getSourceAddressOperand() and
200-
result = TIndirectReturnKind(argumentIndex, this.getIndirectionIndex() - 1) and
200+
result = TIndirectReturnKind(argumentIndex, this.getIndirectionIndex()) and
201201
hasNonInitializeParameterDef(returnInd.getIRVariable())
202202
)
203203
or
@@ -365,7 +365,7 @@ predicate jumpStep(Node n1, Node n2) {
365365
predicate storeStep(Node node1, Content c, PostFieldUpdateNode node2) {
366366
exists(int indirectionIndex1, int numberOfLoads, StoreInstruction store |
367367
nodeHasInstruction(node1, store, pragma[only_bind_into](indirectionIndex1)) and
368-
node2.getIndirectionIndex() = 0 and
368+
node2.getIndirectionIndex() = 1 and
369369
numberOfLoadsFromOperand(node2.getFieldAddress(), store.getDestinationAddressOperand(),
370370
numberOfLoads)
371371
|

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ private module Cached {
3838
TVariableNode(Variable var) or
3939
TPostFieldUpdateNode(FieldAddress operand, int indirectionIndex) {
4040
indirectionIndex =
41-
[0 .. Ssa::countIndirectionsForCppType(operand.getObjectAddress().getResultLanguageType()) -
42-
1]
41+
[1 .. Ssa::countIndirectionsForCppType(operand.getObjectAddress().getResultLanguageType())]
4342
} or
4443
TSsaPhiNode(Ssa::PhiNode phi) or
4544
TIndirectArgumentOutNode(ArgumentOperand operand, int indirectionIndex) {
4645
Ssa::isModifiableByCall(operand) and
47-
indirectionIndex = [0 .. Ssa::countIndirectionsForCppType(operand.getLanguageType()) - 1]
46+
indirectionIndex = [1 .. Ssa::countIndirectionsForCppType(operand.getLanguageType())]
4847
} or
4948
TIndirectOperand(Operand op, int indirectionIndex) {
5049
Ssa::hasIndirectOperand(op, indirectionIndex)
@@ -370,19 +369,15 @@ class PostFieldUpdateNode extends TPostFieldUpdateNode, PartialDefinitionNode {
370369

371370
override Declaration getEnclosingCallable() { result = this.getFunction() }
372371

373-
override IRType getType() { result = fieldAddress.getIRType() }
374-
375372
FieldAddress getFieldAddress() { result = fieldAddress }
376373

377374
Field getUpdatedField() { result = fieldAddress.getField() }
378375

379376
int getIndirectionIndex() { result = indirectionIndex }
380377

381378
override Node getPreUpdateNode() {
382-
// + 1 because we're storing into an lvalue, and the original node should be the rvalue of
383-
// the same address.
384379
hasOperandAndIndex(result, pragma[only_bind_into](fieldAddress).getObjectAddressOperand(),
385-
indirectionIndex + 1)
380+
indirectionIndex)
386381
}
387382

388383
override Expr getDefinedExpr() {
@@ -536,9 +531,7 @@ class IndirectArgumentOutNode extends Node, TIndirectArgumentOutNode, PostUpdate
536531

537532
override Function getFunction() { result = this.getCallInstruction().getEnclosingFunction() }
538533

539-
override IRType getType() { result instanceof IRVoidType }
540-
541-
override Node getPreUpdateNode() { hasOperandAndIndex(result, operand, indirectionIndex + 1) }
534+
override Node getPreUpdateNode() { hasOperandAndIndex(result, operand, indirectionIndex) }
542535

543536
override string toStringImpl() {
544537
// This string should be unique enough to be helpful but common enough to
@@ -1075,7 +1068,7 @@ predicate simpleLocalFlowStep(Node nodeFrom, Node nodeTo) {
10751068
store.getDestinationAddressOperand() = address
10761069
)
10771070
or
1078-
Ssa::outNodeHasAddressAndIndex(nodeFrom, address, indirectionIndex - 1)
1071+
Ssa::outNodeHasAddressAndIndex(nodeFrom, address, indirectionIndex)
10791072
)
10801073
}
10811074

cpp/ql/lib/experimental/semmle/code/cpp/ir/dataflow/internal/ModelUtil.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Node callOutput(CallInstruction call, FunctionOutput output) {
4141
// The side effect of a call on the value pointed to by an argument or qualifier
4242
exists(int index, int indirectionIndex |
4343
result.(IndirectArgumentOutNode).getArgumentIndex() = index and
44-
result.(IndirectArgumentOutNode).getIndirectionIndex() + 1 = indirectionIndex and
44+
result.(IndirectArgumentOutNode).getIndirectionIndex() = indirectionIndex and
4545
result.(IndirectArgumentOutNode).getCallInstruction() = call and
4646
output.isParameterDerefOrQualifierObject(index, indirectionIndex)
4747
)

0 commit comments

Comments
 (0)