Skip to content

Commit 405850e

Browse files
author
Dave Bartolomeo
authored
Merge pull request #2805 from jbj/dataflow-sideeffect-join
C++: IR DataFlowUtil::modelFlow join order fix
2 parents 2b2bb5d + 58bba86 commit 405850e

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,10 @@ private predicate modelFlow(Instruction iFrom, Instruction iTo) {
365365
modelOut.isReturnValueDeref() and
366366
iTo = call
367367
or
368-
exists(WriteSideEffectInstruction outNode |
369-
modelOut.isParameterDeref(outNode.getIndex()) and
368+
exists(int index, WriteSideEffectInstruction outNode |
369+
modelOut.isParameterDeref(index) and
370370
iTo = outNode and
371-
outNode.getPrimaryInstruction() = call
371+
outNode = getSideEffectFor(call, index)
372372
)
373373
// TODO: add write side effects for qualifiers
374374
) and
@@ -380,8 +380,7 @@ private predicate modelFlow(Instruction iFrom, Instruction iTo) {
380380
or
381381
exists(int index, ReadSideEffectInstruction read |
382382
modelIn.isParameterDeref(index) and
383-
read.getIndex() = index and
384-
read.getPrimaryInstruction() = call and
383+
read = getSideEffectFor(call, index) and
385384
iFrom = read.getSideEffectOperand().getAnyDef()
386385
)
387386
or
@@ -392,6 +391,18 @@ private predicate modelFlow(Instruction iFrom, Instruction iTo) {
392391
)
393392
}
394393

394+
/**
395+
* Holds if the result is a side effect for instruction `call` on argument
396+
* index `argument`. This helper predicate makes it easy to join on both of
397+
* these columns at once, avoiding pathological join orders in case the
398+
* argument index should get joined first.
399+
*/
400+
pragma[noinline]
401+
SideEffectInstruction getSideEffectFor(CallInstruction call, int argument) {
402+
call = result.getPrimaryInstruction() and
403+
argument = result.(IndexedInstruction).getIndex()
404+
}
405+
395406
/**
396407
* Holds if data flows from `source` to `sink` in zero or more local
397408
* (intra-procedural) steps.

0 commit comments

Comments
 (0)