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

Skip to content

Commit 8faad92

Browse files
committed
C++: Define DataFlowCallable.getUnderlyingCallable and use it to fix some issues.
1 parent 0df10bd commit 8faad92

4 files changed

Lines changed: 39 additions & 20 deletions

File tree

cpp/ql/lib/semmle/code/cpp/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ private import Make<DataFlowImplSpecific::CppDataFlow, Input> as Impl
6666

6767
private module StepsInput implements Impl::Private::StepsInputSig {
6868
DataFlowCall getACall(Public::SummarizedCallable sc) {
69-
result.getStaticCallTarget().asSourceCallable() = sc or
70-
result.getStaticCallTarget().asSummarizedCallable() = sc // TODO: this should be the only case
69+
result.getStaticCallTarget().getUnderlyingCallable() = sc
7170
}
7271
}
7372

@@ -123,13 +122,10 @@ module SourceSinkInterpretationInput implements
123122
}
124123

125124
/** Gets the callable that this node corresponds to, if any. */
126-
DataFlowCallable asCallable() {
127-
result.asSourceCallable() = this.asElement()
128-
// TODO: or summary callable?
129-
}
125+
DataFlowCallable asCallable() { result.getUnderlyingCallable() = this.asElement() }
130126

131127
/** Gets the target of this call, if any. */
132-
Element getCallTarget() { result = this.asCall().getStaticCallTarget().asSourceCallable() } // TODO: summarized target???
128+
Element getCallTarget() { result = this.asCall().getStaticCallTarget().getUnderlyingCallable() }
133129

134130
/** Gets a textual representation of this node. */
135131
string toString() {

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ DataFlowCallable defaultViableCallable(DataFlowCall call) {
2424
// that as a potential callee.
2525
exists(string qualifiedName, int nparams |
2626
callSignatureWithoutBody(qualifiedName, nparams, call.asCallInstruction()) and
27-
functionSignatureWithBody(qualifiedName, nparams, result.asSourceCallable()) and
27+
functionSignatureWithBody(qualifiedName, nparams, result.getUnderlyingCallable()) and
2828
strictcount(Function other | functionSignatureWithBody(qualifiedName, nparams, other)) = 1
2929
)
3030
or
@@ -40,7 +40,9 @@ DataFlowCallable viableCallable(DataFlowCall call) {
4040
result = defaultViableCallable(call)
4141
or
4242
// Additional call targets
43-
result = any(AdditionalCallTarget additional).viableTarget(call.asCallInstruction().getUnconvertedResultExpression())
43+
result =
44+
any(AdditionalCallTarget additional)
45+
.viableTarget(call.asCallInstruction().getUnconvertedResultExpression())
4446
}
4547

4648
/**
@@ -176,7 +178,12 @@ private module VirtualDispatch {
176178
/** Call to a virtual function. */
177179
private class DataSensitiveOverriddenFunctionCall extends DataSensitiveCall {
178180
DataSensitiveOverriddenFunctionCall() {
179-
exists(this.getStaticCallTarget().asSourceCallable().(VirtualFunction).getAnOverridingFunction())
181+
exists(
182+
this.getStaticCallTarget()
183+
.getUnderlyingCallable()
184+
.(VirtualFunction)
185+
.getAnOverridingFunction()
186+
)
180187
}
181188

182189
override DataFlow::Node getDispatchValue() { result.asInstruction() = this.getArgument(-1) }
@@ -194,7 +201,8 @@ private module VirtualDispatch {
194201
*/
195202
pragma[noinline]
196203
private predicate overrideMayAffectCall(Class overridingClass, MemberFunction overridingFunction) {
197-
overridingFunction.getAnOverriddenFunction+() = this.getStaticCallTarget().asSourceCallable().(VirtualFunction) and
204+
overridingFunction.getAnOverriddenFunction+() =
205+
this.getStaticCallTarget().getUnderlyingCallable().(VirtualFunction) and
198206
overridingFunction.getDeclaringType() = overridingClass
199207
}
200208

@@ -261,7 +269,7 @@ private predicate mayBenefitFromCallContext(
261269
f = pragma[only_bind_out](call).getEnclosingCallable() and
262270
exists(InitializeParameterInstruction init |
263271
not exists(call.getStaticCallTarget()) and
264-
init.getEnclosingFunction() = f.asSourceCallable() and
272+
init.getEnclosingFunction() = f.getUnderlyingCallable() and
265273
call.flowsFrom(DataFlow::instructionNode(init), _) and
266274
init.getParameter().getIndex() = arg
267275
)
@@ -276,7 +284,11 @@ DataFlowCallable viableImplInCallContext(DataFlowCall call, DataFlowCall ctx) {
276284
exists(int i, DataFlowCallable f |
277285
mayBenefitFromCallContext(pragma[only_bind_into](call), f, i) and
278286
f = ctx.getStaticCallTarget() and
279-
result = TSourceCallable(ctx.getArgument(i).getUnconvertedResultExpression().(FunctionAccess).getTarget())
287+
result =
288+
TSourceCallable(ctx.getArgument(i)
289+
.getUnconvertedResultExpression()
290+
.(FunctionAccess)
291+
.getTarget())
280292
)
281293
}
282294

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,11 @@ class DataFlowCallable extends TDataFlowCallable {
978978
Cpp::Declaration asSourceCallable() { this = TSourceCallable(result) }
979979

980980
FlowSummaryImpl::Public::SummarizedCallable asSummarizedCallable() { this = TSummarizedCallable(result) }
981+
982+
Cpp::Declaration getUnderlyingCallable() {
983+
result = this.asSummarizedCallable() or // SummarizedCallable = Function (in CPP)
984+
result = this.asSourceCallable()
985+
}
981986
}
982987

983988
private class SourceCallable extends DataFlowCallable, TSourceCallable {
@@ -1031,7 +1036,7 @@ class DataFlowCall extends TDataFlowCall {
10311036
/**
10321037
* Gets the `Function` that the call targets, if this is statically known.
10331038
*/
1034-
DataFlowCallable getStaticCallTarget() { none() } // TODO: should this return DataFlowCallable?
1039+
DataFlowCallable getStaticCallTarget() { none() }
10351040

10361041
/**
10371042
* Gets the `index`'th argument operand. The qualifier is considered to have index `-1`.
@@ -1104,7 +1109,7 @@ class SummaryCall extends DataFlowCall, TSummaryCall {
11041109

11051110
// override ArgumentOperand getArgumentOperand(int index) TODO
11061111

1107-
override DataFlowCallable getEnclosingCallable() { result = TSummarizedCallable(c) }
1112+
override DataFlowCallable getEnclosingCallable() { result = TSummarizedCallable(c) } // TODO: is this right?
11081113

11091114
override string toString() { result = "[summary] call to " + receiver + " in " + c }
11101115

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,9 @@ private module RawIndirectNodes {
10101010
result = this.getOperand().getDef().getEnclosingFunction()
10111011
}
10121012

1013-
override DataFlowCallable getEnclosingCallable() { result = TSourceCallable(this.getFunction()) }
1013+
override DataFlowCallable getEnclosingCallable() {
1014+
result = TSourceCallable(this.getFunction())
1015+
}
10141016

10151017
override predicate isGLValue() { this.getOperand().isGLValue() }
10161018

@@ -1054,7 +1056,9 @@ private module RawIndirectNodes {
10541056

10551057
override Declaration getFunction() { result = this.getInstruction().getEnclosingFunction() }
10561058

1057-
override DataFlowCallable getEnclosingCallable() { result = TSourceCallable(this.getFunction()) }
1059+
override DataFlowCallable getEnclosingCallable() {
1060+
result = TSourceCallable(this.getFunction())
1061+
}
10581062

10591063
override predicate isGLValue() { this.getInstruction().isGLValue() }
10601064

@@ -1672,7 +1676,8 @@ private class ExplicitParameterNode extends ParameterNode, DirectParameterNode {
16721676
ExplicitParameterNode() { exists(instr.getParameter()) }
16731677

16741678
override predicate isParameterOf(DataFlowCallable f, ParameterPosition pos) {
1675-
f.asSourceCallable().(Function).getParameter(pos.(DirectPosition).getIndex()) = instr.getParameter()
1679+
f.getUnderlyingCallable().(Function).getParameter(pos.(DirectPosition).getIndex()) =
1680+
instr.getParameter()
16761681
}
16771682

16781683
override string toStringImpl() { result = instr.getParameter().toString() }
@@ -1685,7 +1690,8 @@ class ThisParameterNode extends ParameterNode, DirectParameterNode {
16851690
ThisParameterNode() { instr.getIRVariable() instanceof IRThisVariable }
16861691

16871692
override predicate isParameterOf(DataFlowCallable f, ParameterPosition pos) {
1688-
pos.(DirectPosition).getIndex() = -1 and instr.getEnclosingFunction() = f.asSourceCallable()
1693+
pos.(DirectPosition).getIndex() = -1 and
1694+
instr.getEnclosingFunction() = f.getUnderlyingCallable()
16891695
}
16901696

16911697
override string toStringImpl() { result = "this" }
@@ -1704,7 +1710,7 @@ class SummaryParameterNode extends ParameterNode, FlowSummaryNode {
17041710
}
17051711

17061712
override predicate isParameterOf(DataFlowCallable c, ParameterPosition p) {
1707-
c.asSummarizedCallable() = this.getSummarizedCallable() and
1713+
c.getUnderlyingCallable() = this.getSummarizedCallable() and
17081714
p = this.getPosition()
17091715
}
17101716
}

0 commit comments

Comments
 (0)