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

Skip to content

Commit a8863e4

Browse files
committed
C++: Port implementation to CPP.
1 parent 3aacc5f commit a8863e4

7 files changed

Lines changed: 119 additions & 133 deletions

File tree

cpp/ql/lib/semmle/code/cpp/dataflow/ExternalFlow.qll

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
*
1414
* The interpretation of a row is similar to API-graphs with a left-to-right
1515
* reading.
16-
* 1. The `namespace` column selects a package.
17-
* 2. The `type` column selects a type within that package.
16+
* 1. The `namespace` column selects a namespace.
17+
* 2. The `type` column selects a type within that namespace.
1818
* 3. The `subtypes` is a boolean that indicates whether to jump to an
1919
* arbitrary subtype of that type. Set this to `false` if leaving the `type`
2020
* blank (for example, a free function).
@@ -65,16 +65,14 @@
6565
* globally applicable value-preserving step.
6666
*/
6767

68-
import swift
69-
private import internal.DataFlowDispatch
70-
private import internal.DataFlowPrivate
71-
private import internal.DataFlowPublic
68+
import cpp
69+
private import new.DataFlow
7270
private import internal.FlowSummaryImpl
7371
private import internal.FlowSummaryImpl::Public
7472
private import internal.FlowSummaryImpl::Private
7573
private import internal.FlowSummaryImpl::Private::External
76-
private import FlowSummary as FlowSummary
7774
private import codeql.mad.ModelValidation as SharedModelVal
75+
private import codeql.util.Unit
7876

7977
/**
8078
* A unit class for adding additional source model rows.
@@ -354,13 +352,13 @@ private predicate elementSpec(
354352
private string paramsStringPart(Function c, int i) {
355353
i = -1 and result = "(" and exists(c)
356354
or
357-
exists(int n, string p | c.getParam(n).getType().toString() = p |
355+
exists(int n, string p | c.getParameter(n).getType().toString() = p |
358356
i = 2 * n and result = p
359357
or
360358
i = 2 * n - 1 and result = "," and n != 0
361359
)
362360
or
363-
i = 2 * c.getNumberOfParams() and result = ")"
361+
i = 2 * c.getNumberOfParameters() and result = ")"
364362
}
365363

366364
/**
@@ -401,42 +399,42 @@ private Element interpretElement0(
401399
type = "" and
402400
matchesSignature(func, signature) and
403401
subtypes = false and
404-
not result instanceof Method and
402+
not exists(func.getDeclaringType()) and
405403
result = func
406404
)
407405
or
408406
// Member functions
409-
exists(NominalTypeDecl namedTypeDecl, Decl declWithMethod, Method method |
407+
exists(Class namedClass, Class classWithMethod, Function method |
410408
method.getName() = name and
411-
method = declWithMethod.getAMember() and
412-
namedTypeDecl.getFullName() = type and
409+
method = classWithMethod.getAMember() and
410+
namedClass.getName() = type and
413411
matchesSignature(method, signature) and
414412
result = method
415413
|
416-
// member declared in the named type or a subtype of it (or an extension of any)
414+
// member declared in the named type or a subtype of it
417415
subtypes = true and
418-
declWithMethod.asNominalTypeDecl() = namedTypeDecl.getADerivedTypeDecl*()
416+
classWithMethod = namedClass.getADerivedClass*()
419417
or
420-
// member declared directly in the named type (or an extension of it)
418+
// member declared directly in the named type
421419
subtypes = false and
422-
declWithMethod.asNominalTypeDecl() = namedTypeDecl
420+
classWithMethod = namedClass
423421
)
424422
or
425-
// Fields
423+
// Member variables
426424
signature = "" and
427-
exists(NominalTypeDecl namedTypeDecl, Decl declWithField, FieldDecl field |
428-
field.getName() = name and
429-
field = declWithField.getAMember() and
430-
namedTypeDecl.getFullName() = type and
431-
result = field
425+
exists(Class namedClass, Class classWithMember, MemberVariable member |
426+
member.getName() = name and
427+
member = classWithMember.getAMember() and
428+
namedClass.getName() = type and
429+
result = member
432430
|
433431
// field declared in the named type or a subtype of it (or an extension of any)
434432
subtypes = true and
435-
declWithField.asNominalTypeDecl() = namedTypeDecl.getADerivedTypeDecl*()
433+
classWithMember = namedClass.getADerivedClass*()
436434
or
437435
// field declared directly in the named type (or an extension of it)
438436
subtypes = false and
439-
declWithField.asNominalTypeDecl() = namedTypeDecl
437+
classWithMember = namedClass
440438
)
441439
)
442440
}
@@ -451,52 +449,14 @@ Element interpretElement(
451449
)
452450
}
453451

454-
deprecated private predicate parseField(AccessPathToken c, Content::FieldContent f) {
455-
exists(string fieldRegex, string name |
456-
c.getName() = "Field" and
457-
fieldRegex = "^([^.]+)$" and
458-
name = c.getAnArgument().regexpCapture(fieldRegex, 1) and
459-
f.getField().getName() = name
460-
)
461-
}
462-
463-
deprecated private predicate parseTuple(AccessPathToken c, Content::TupleContent t) {
464-
c.getName() = "TupleElement" and
465-
t.getIndex() = c.getAnArgument().toInt()
466-
}
467-
468-
deprecated private predicate parseEnum(AccessPathToken c, Content::EnumContent e) {
469-
c.getName() = "EnumElement" and
470-
c.getAnArgument() = e.getSignature()
471-
or
472-
c.getName() = "OptionalSome" and
473-
e.getSignature() = "some:0"
474-
}
475-
476-
/** Holds if the specification component parses as a `Content`. */
477-
deprecated predicate parseContent(AccessPathToken component, Content content) {
478-
parseField(component, content)
479-
or
480-
parseTuple(component, content)
481-
or
482-
parseEnum(component, content)
483-
or
484-
// map legacy "ArrayElement" specification components to `CollectionContent`
485-
component.getName() = "ArrayElement" and
486-
content instanceof Content::CollectionContent
487-
or
488-
component.getName() = "CollectionElement" and
489-
content instanceof Content::CollectionContent
490-
}
491-
492452
cached
493453
private module Cached {
494454
/**
495455
* Holds if `node` is specified as a source with the given kind in a CSV flow
496456
* model.
497457
*/
498458
cached
499-
predicate sourceNode(Node node, string kind) {
459+
predicate sourceNode(DataFlow::Node node, string kind) {
500460
exists(SourceSinkInterpretationInput::InterpretNode n |
501461
isSourceNode(n, kind) and n.asNode() = node
502462
)
@@ -507,7 +467,7 @@ private module Cached {
507467
* model.
508468
*/
509469
cached
510-
predicate sinkNode(Node node, string kind) {
470+
predicate sinkNode(DataFlow::Node node, string kind) {
511471
exists(SourceSinkInterpretationInput::InterpretNode n |
512472
isSinkNode(n, kind) and n.asNode() = node
513473
)
@@ -567,7 +527,7 @@ private class NeutralCallableAdapter extends NeutralCallable {
567527
string provenance_;
568528

569529
NeutralCallableAdapter() {
570-
// Neutral models have not been implemented for Swift.
530+
// Neutral models have not been implemented for CPP.
571531
none() and
572532
exists(this) and
573533
exists(kind) and

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

Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
* Provides classes and predicates for defining flow summaries.
33
*/
44

5-
private import swift
5+
private import cpp
66
private import codeql.dataflow.internal.FlowSummaryImpl
77
private import codeql.dataflow.internal.AccessPathSyntax as AccessPath
8-
private import DataFlowImplSpecific as DataFlowImplSpecific
9-
private import DataFlowImplSpecific::Private
10-
private import DataFlowImplSpecific::Public
11-
private import DataFlowImplCommon
12-
private import codeql.swift.dataflow.ExternalFlow
8+
private import semmle.code.cpp.ir.dataflow.internal.DataFlowPrivate
9+
private import semmle.code.cpp.ir.dataflow.internal.DataFlowUtil
10+
private import semmle.code.cpp.ir.dataflow.internal.DataFlowImplSpecific as DataFlowImplSpecific
11+
private import semmle.code.cpp.dataflow.ExternalFlow
1312

14-
module Input implements InputSig<DataFlowImplSpecific::SwiftDataFlow> {
13+
module Input implements InputSig<DataFlowImplSpecific::CppDataFlow> {
1514
class SummarizedCallableBase = Function;
1615

17-
ArgumentPosition callbackSelfParameterPosition() { result instanceof ThisArgumentPosition }
16+
ArgumentPosition callbackSelfParameterPosition() { result = TDirectPosition(-1) }
1817

19-
ReturnKind getStandardReturnValueKind() { result instanceof NormalReturnKind }
18+
ReturnKind getStandardReturnValueKind() { result.(NormalReturnKind).getIndirectionIndex() = 0 }
2019

2120
string encodeParameterPosition(ParameterPosition pos) { result = pos.toString() }
2221

@@ -29,63 +28,54 @@ module Input implements InputSig<DataFlowImplSpecific::SwiftDataFlow> {
2928
}
3029

3130
string encodeContent(ContentSet cs, string arg) {
32-
exists(Content::FieldContent c |
31+
exists(FieldContent c |
3332
cs.isSingleton(c) and
3433
result = "Field" and
3534
arg = c.getField().getName()
3635
)
37-
or
38-
exists(Content::TupleContent c |
39-
cs.isSingleton(c) and
40-
result = "TupleElement" and
41-
arg = c.getIndex().toString()
42-
)
43-
or
44-
exists(Content::EnumContent c, string sig |
45-
cs.isSingleton(c) and
46-
sig = c.getSignature()
47-
|
48-
if sig = "some:0"
49-
then
50-
result = "OptionalSome" and
51-
arg = ""
52-
else (
53-
result = "EnumElement" and
54-
arg = sig
55-
)
56-
)
57-
or
58-
exists(Content::CollectionContent c |
59-
cs.isSingleton(c) and
60-
result = "CollectionElement" and
61-
arg = ""
62-
)
63-
}
36+
/*
37+
* or
38+
* exists(Content::TupleContent c |
39+
* cs.isSingleton(c) and
40+
* result = "TupleElement" and
41+
* arg = c.getIndex().toString()
42+
* )
43+
* or
44+
* exists(Content::EnumContent c, string sig |
45+
* cs.isSingleton(c) and
46+
* sig = c.getSignature()
47+
* |
48+
* if sig = "some:0"
49+
* then
50+
* result = "OptionalSome" and
51+
* arg = ""
52+
* else (
53+
* result = "EnumElement" and
54+
* arg = sig
55+
* )
56+
* )
57+
* or
58+
* exists(Content::CollectionContent c |
59+
* cs.isSingleton(c) and
60+
* result = "CollectionElement" and
61+
* arg = ""
62+
* )
63+
*/
64+
65+
}
6466

6567
string encodeWithoutContent(ContentSet c, string arg) {
6668
result = "WithoutContent" + c and arg = ""
6769
}
6870

6971
string encodeWithContent(ContentSet c, string arg) { result = "WithContent" + c and arg = "" }
7072

71-
bindingset[token]
72-
ContentSet decodeUnknownContent(AccessPath::AccessPathTokenBase token) {
73-
// map legacy "ArrayElement" specification components to `CollectionContent`
74-
token.getName() = "ArrayElement" and
75-
result.isSingleton(any(Content::CollectionContent c))
76-
or
77-
token.getName() = "CollectionElement" and
78-
result.isSingleton(any(Content::CollectionContent c))
79-
}
80-
8173
bindingset[token]
8274
ParameterPosition decodeUnknownParameterPosition(AccessPath::AccessPathTokenBase token) {
8375
// needed to support `Argument[x..y]` ranges and `Argument[-1]`
8476
token.getName() = "Argument" and
8577
exists(int pos | pos = AccessPath::parseInt(token.getAnArgument()) |
86-
result.(PositionalParameterPosition).getIndex() = pos
87-
or
88-
pos = -1 and result instanceof ThisParameterPosition
78+
result = TDirectPosition(pos)
8979
)
9080
}
9181

@@ -94,24 +84,21 @@ module Input implements InputSig<DataFlowImplSpecific::SwiftDataFlow> {
9484
// needed to support `Parameter[x..y]` ranges and `Parameter[-1]`
9585
token.getName() = "Parameter" and
9686
exists(int pos | pos = AccessPath::parseInt(token.getAnArgument()) |
97-
result.(PositionalArgumentPosition).getIndex() = pos
98-
or
99-
pos = -1 and
100-
result instanceof ThisArgumentPosition
87+
result = TDirectPosition(pos)
10188
)
10289
}
10390
}
10491

105-
private import Make<DataFlowImplSpecific::SwiftDataFlow, Input> as Impl
92+
/*private*/ import Make<DataFlowImplSpecific::CppDataFlow, Input> as Impl
10693

10794
private module StepsInput implements Impl::Private::StepsInputSig {
108-
DataFlowCall getACall(Public::SummarizedCallable sc) { result.asCall().getStaticTarget() = sc }
95+
DataFlowCall getACall(Public::SummarizedCallable sc) { result.getStaticCallTarget() = sc }
10996
}
11097

11198
module SourceSinkInterpretationInput implements
11299
Impl::Private::External::SourceSinkInterpretationInputSig<Location>
113100
{
114-
class Element = AstNode;
101+
class Element = Element;
115102

116103
class SourceOrSinkElement = Element;
117104

@@ -158,10 +145,12 @@ module SourceSinkInterpretationInput implements
158145
DataFlowCall asCall() { this = TDataFlowCall_(result) }
159146

160147
/** Gets the callable that this node corresponds to, if any. */
161-
DataFlowCallable asCallable() { result.getUnderlyingCallable() = this.asElement() }
148+
DataFlowCallable asCallable() { result.(Function) = this.asElement() }
162149

163150
/** Gets the target of this call, if any. */
164-
Element getCallTarget() { result = this.asCall().asCall().getStaticTarget() }
151+
Element getCallTarget() {
152+
result = this.asNode().asExpr().(Call).getTarget()
153+
}
165154

166155
/** Gets a textual representation of this node. */
167156
string toString() {
@@ -186,31 +175,31 @@ module SourceSinkInterpretationInput implements
186175
bindingset[c]
187176
predicate interpretOutput(string c, InterpretNode mid, InterpretNode node) {
188177
// Allow fields to be picked as output nodes.
189-
exists(Node n, AstNode ast |
178+
exists(Node n, Element ast |
190179
n = node.asNode() and
191180
ast = mid.asElement()
192181
|
193182
c = "" and
194-
n.asExpr().(MemberRefExpr).getMember() = ast
183+
n.asExpr().(VariableAccess).getTarget() = ast
195184
)
196185
}
197186

198187
/** Provides additional source specification logic. */
199188
bindingset[c]
200189
predicate interpretInput(string c, InterpretNode mid, InterpretNode node) {
201-
exists(Node n, AstNode ast, MemberRefExpr e |
190+
exists(Node n, Element ast, VariableAccess e |
202191
n = node.asNode() and
203192
ast = mid.asElement() and
204-
e.getMember() = ast
193+
e.getTarget() = ast
205194
|
206195
// Allow fields to be picked as input nodes.
207196
c = "" and
208-
e.getBase() = n.asExpr()
197+
e.getQualifier() = n.asExpr()
209198
or
210199
// Allow post update nodes to be picked as input nodes when the `input` column
211200
// of the row is `PostUpdate`.
212201
c = "PostUpdate" and
213-
e.getBase() = n.(PostUpdateNode).getPreUpdateNode().asExpr()
202+
e.getQualifier() = n.(PostUpdateNode).getPreUpdateNode().asExpr()
214203
)
215204
}
216205
}

0 commit comments

Comments
 (0)