Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2bea0ad

Browse files
MathiasVPgeoffw0
authored andcommitted
C++: Solve non-monotonic issue by unfolding dataflow-related predicates until we get to the SSA implementations of them.
1 parent cfc1a3d commit 2bea0ad

3 files changed

Lines changed: 35 additions & 22 deletions

File tree

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,22 +436,21 @@ class IndirectionPosition extends Position, TIndirectionPosition {
436436
newtype TPosition =
437437
TDirectPosition(int index) { exists(any(CallInstruction c).getArgument(index)) } or
438438
TIndirectionPosition(int argumentIndex, int indirectionIndex) {
439-
hasOperandAndIndex(_, any(CallInstruction call).getArgumentOperand(argumentIndex),
439+
Ssa::hasIndirectOperand(any(CallInstruction call).getArgumentOperand(argumentIndex),
440440
indirectionIndex)
441441
}
442442

443443
private newtype TReturnKind =
444444
TNormalReturnKind(int indirectionIndex) {
445-
exists(IndirectReturnNode return |
446-
return.isNormalReturn() and
447-
indirectionIndex = return.getIndirectionIndex() - 1 // We subtract one because the return loads the value.
448-
)
449-
or indirectionIndex = 0 // TODO: very much a bodge so that it works on the test that has no return statements
445+
Ssa::hasIndirectOperand(any(ReturnValueInstruction ret).getReturnAddressOperand(),
446+
indirectionIndex + 1) // We subtract one because the return loads the value.
447+
or
448+
indirectionIndex = 0 // TODO: very much a bodge so that it works on the test that has no return statements
450449
} or
451450
TIndirectReturnKind(int argumentIndex, int indirectionIndex) {
452-
exists(IndirectReturnNode return |
453-
return.isParameterReturn(argumentIndex) and
454-
indirectionIndex = return.getIndirectionIndex()
451+
exists(Ssa::FinalParameterUse use |
452+
hasFinalParameterNode(use, _, indirectionIndex) and
453+
use.getArgumentIndex() = argumentIndex
455454
)
456455
}
457456

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ private newtype TIRDataFlowNode =
5353
Ssa::hasRawIndirectInstruction(node.asInstruction(), indirectionIndex)
5454
} or
5555
TFinalParameterNode(Parameter p, int indirectionIndex) {
56-
exists(Ssa::FinalParameterUse use |
57-
use.getParameter() = p and
58-
use.getIndirectionIndex() = indirectionIndex and
59-
parameterIsRedefined(p)
60-
)
56+
hasFinalParameterNode(_, p, indirectionIndex)
6157
} or
6258
TFinalGlobalValue(Ssa::GlobalUse globalUse) or
63-
TInitialGlobalValue(Ssa::GlobalDef globalUse)
59+
TInitialGlobalValue(Ssa::GlobalDef globalUse) or
60+
FlowSummaryNode(FlowSummaryImpl::Private::SummaryNode sn)
61+
62+
/**
63+
* Holds if `(p, indirectionIndex)` should define a `TFinalParameterNode`
64+
* entry because `use` represents the final use of a parameter that has been
65+
* written to in the enclosing function of `p`.
66+
*/
67+
predicate hasFinalParameterNode(Ssa::FinalParameterUse use, Parameter p, int indirectionIndex) {
68+
use.getParameter() = p and
69+
use.getIndirectionIndex() = indirectionIndex and
70+
parameterIsRedefined(p)
71+
}
6472

6573
/**
6674
* Holds if the value of `*p` (or `**p`, `***p`, etc.) is redefined somewhere in the body

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,24 @@ private module SourceVariables {
7070

7171
import SourceVariables
7272

73-
/**
74-
* Holds if the `(operand, indirectionIndex)` columns should be
75-
* assigned a `RawIndirectOperand` value.
76-
*/
77-
predicate hasRawIndirectOperand(Operand op, int indirectionIndex) {
73+
predicate hasIndirectOperand(Operand op, int indirectionIndex) {
7874
exists(CppType type, int m |
7975
not ignoreOperand(op) and
8076
type = getLanguageType(op) and
8177
m = countIndirectionsForCppType(type) and
82-
indirectionIndex = [1 .. m] and
83-
not hasIRRepresentationOfIndirectOperand(op, indirectionIndex, _, _)
78+
indirectionIndex = [1 .. m]
8479
)
8580
}
8681

82+
/**
83+
* Holds if the `(operand, indirectionIndex)` columns should be
84+
* assigned a `RawIndirectOperand` value.
85+
*/
86+
predicate hasRawIndirectOperand(Operand op, int indirectionIndex) {
87+
hasIndirectOperand(op, indirectionIndex) and
88+
not hasIRRepresentationOfIndirectOperand(op, indirectionIndex, _, _)
89+
}
90+
8791
/**
8892
* Holds if the `(instr, indirectionIndex)` columns should be
8993
* assigned a `RawIndirectInstruction` value.
@@ -403,6 +407,8 @@ class FinalParameterUse extends UseImpl, TFinalParameterUse {
403407

404408
Parameter getParameter() { result = p }
405409

410+
int getArgumentIndex() { result = p.getIndex() }
411+
406412
override Node getNode() { finalParameterNodeHasParameterAndIndex(result, p, ind) }
407413

408414
override int getIndirection() { result = ind + 1 }

0 commit comments

Comments
 (0)