Skip to content

Commit d7f839a

Browse files
authored
Swift: use ExitNode as CFG for InoutReturnNode
1 parent 9f2abf3 commit d7f839a

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowPrivate.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ private module Cached {
6262
newtype TNode =
6363
TExprNode(ExprCfgNode e) or
6464
TSsaDefinitionNode(Ssa::Definition def) or
65-
TInoutReturnNode(ParamDecl param, ControlFlowNode exit) {
66-
param.isInout() and
67-
exit.getScope() = param.getDeclaringFunction() and
68-
exit.getNode().asAstNode() instanceof ReturnStmt
69-
} or
65+
TInoutReturnNode(ParamDecl param) { param.isInout() } or
7066
TInOutUpdateNode(ParamDecl param, CallExpr call) {
7167
param.isInout() and
7268
call.getStaticTarget() = param.getDeclaringFunction()
@@ -216,7 +212,11 @@ private module ReturnNodes {
216212
ParamDecl param;
217213
ControlFlowNode exit;
218214

219-
InoutReturnNodeImpl() { this = TInoutReturnNode(param, exit) }
215+
InoutReturnNodeImpl() {
216+
this = TInoutReturnNode(param) and
217+
exit instanceof ExitNode and
218+
exit.getScope() = param.getDeclaringFunction()
219+
}
220220

221221
override ReturnKind getKind() { result.(ParamReturnKind).getIndex() = param.getIndex() }
222222

swift/ql/lib/codeql/swift/dataflow/internal/SsaImplSpecific.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
private import swift
44
private import codeql.swift.controlflow.BasicBlocks as BasicBlocks
55
private import codeql.swift.controlflow.ControlFlowGraph
6+
private import codeql.swift.controlflow.CfgNodes
67

78
class BasicBlock = BasicBlocks::BasicBlock;
89

@@ -51,8 +52,8 @@ predicate variableRead(BasicBlock bb, int i, SourceVariable v, boolean certain)
5152
certain = true
5253
)
5354
or
54-
exists(ReturnStmt return, AbstractFunctionDecl func |
55-
bb.getNode(i).getNode().asAstNode() = return and
55+
exists(ExitNode exit, AbstractFunctionDecl func |
56+
bb.getNode(i) = exit and
5657
v.(ParamDecl).isInout() and
5758
func.getAParam() = v and
5859
bb.getScope() = func and

swift/ql/test/library-tests/dataflow/dataflow/DataFlow.expected

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ edges
1212
| test.swift:29:26:29:29 | y : | test.swift:31:15:31:15 | y |
1313
| test.swift:35:12:35:19 | call to source : | test.swift:39:15:39:29 | call to callee_source |
1414
| test.swift:43:19:43:26 | call to source : | test.swift:50:15:50:15 | t |
15-
| test.swift:54:11:54:18 | call to source : | test.swift:55:5:55:5 | arg[return] : |
16-
| test.swift:55:5:55:5 | arg[return] : | test.swift:61:5:61:24 | arg : |
15+
| test.swift:53:1:56:1 | arg[return] : | test.swift:61:5:61:24 | arg : |
16+
| test.swift:54:11:54:18 | call to source : | test.swift:53:1:56:1 | arg[return] : |
1717
| test.swift:61:5:61:24 | arg : | test.swift:62:15:62:15 | x |
18-
| test.swift:65:16:65:28 | WriteDef : | test.swift:69:5:69:5 | arg2[return] : |
19-
| test.swift:65:16:65:28 | arg1 : | test.swift:69:5:69:5 | arg2[return] : |
18+
| test.swift:65:16:65:28 | WriteDef : | test.swift:65:1:70:1 | arg2[return] : |
19+
| test.swift:65:16:65:28 | arg1 : | test.swift:65:1:70:1 | arg2[return] : |
2020
| test.swift:73:18:73:25 | call to source : | test.swift:75:21:75:22 | &... : |
2121
| test.swift:75:5:75:33 | arg2 : | test.swift:77:15:77:15 | y |
2222
| test.swift:75:21:75:22 | &... : | test.swift:65:16:65:28 | WriteDef : |
2323
| test.swift:75:21:75:22 | &... : | test.swift:65:16:65:28 | arg1 : |
2424
| test.swift:75:21:75:22 | &... : | test.swift:75:5:75:33 | arg2 : |
25-
| test.swift:86:15:86:22 | call to source : | test.swift:87:9:87:9 | arg[return] : |
26-
| test.swift:87:9:87:9 | arg[return] : | test.swift:104:9:104:54 | arg : |
25+
| test.swift:80:1:82:1 | arg[return] : | test.swift:97:9:97:41 | arg : |
26+
| test.swift:81:11:81:18 | call to source : | test.swift:80:1:82:1 | arg[return] : |
27+
| test.swift:84:1:91:1 | arg[return] : | test.swift:104:9:104:54 | arg : |
28+
| test.swift:86:15:86:22 | call to source : | test.swift:84:1:91:1 | arg[return] : |
29+
| test.swift:89:15:89:22 | call to source : | test.swift:84:1:91:1 | arg[return] : |
30+
| test.swift:97:9:97:41 | arg : | test.swift:98:19:98:19 | x |
2731
| test.swift:104:9:104:54 | arg : | test.swift:105:19:105:19 | x |
2832
nodes
2933
| test.swift:6:19:6:26 | call to source : | semmle.label | call to source : |
@@ -46,26 +50,31 @@ nodes
4650
| test.swift:39:15:39:29 | call to callee_source | semmle.label | call to callee_source |
4751
| test.swift:43:19:43:26 | call to source : | semmle.label | call to source : |
4852
| test.swift:50:15:50:15 | t | semmle.label | t |
53+
| test.swift:53:1:56:1 | arg[return] : | semmle.label | arg[return] : |
4954
| test.swift:54:11:54:18 | call to source : | semmle.label | call to source : |
50-
| test.swift:55:5:55:5 | arg[return] : | semmle.label | arg[return] : |
5155
| test.swift:61:5:61:24 | arg : | semmle.label | arg : |
5256
| test.swift:62:15:62:15 | x | semmle.label | x |
57+
| test.swift:65:1:70:1 | arg2[return] : | semmle.label | arg2[return] : |
5358
| test.swift:65:16:65:28 | WriteDef : | semmle.label | WriteDef : |
5459
| test.swift:65:16:65:28 | WriteDef : | semmle.label | arg1 : |
5560
| test.swift:65:16:65:28 | arg1 : | semmle.label | WriteDef : |
5661
| test.swift:65:16:65:28 | arg1 : | semmle.label | arg1 : |
57-
| test.swift:69:5:69:5 | arg2[return] : | semmle.label | arg2[return] : |
5862
| test.swift:73:18:73:25 | call to source : | semmle.label | call to source : |
5963
| test.swift:75:5:75:33 | arg2 : | semmle.label | arg2 : |
6064
| test.swift:75:21:75:22 | &... : | semmle.label | &... : |
6165
| test.swift:77:15:77:15 | y | semmle.label | y |
66+
| test.swift:80:1:82:1 | arg[return] : | semmle.label | arg[return] : |
67+
| test.swift:81:11:81:18 | call to source : | semmle.label | call to source : |
68+
| test.swift:84:1:91:1 | arg[return] : | semmle.label | arg[return] : |
6269
| test.swift:86:15:86:22 | call to source : | semmle.label | call to source : |
63-
| test.swift:87:9:87:9 | arg[return] : | semmle.label | arg[return] : |
70+
| test.swift:89:15:89:22 | call to source : | semmle.label | call to source : |
71+
| test.swift:97:9:97:41 | arg : | semmle.label | arg : |
72+
| test.swift:98:19:98:19 | x | semmle.label | x |
6473
| test.swift:104:9:104:54 | arg : | semmle.label | arg : |
6574
| test.swift:105:19:105:19 | x | semmle.label | x |
6675
subpaths
67-
| test.swift:75:21:75:22 | &... : | test.swift:65:16:65:28 | WriteDef : | test.swift:69:5:69:5 | arg2[return] : | test.swift:75:5:75:33 | arg2 : |
68-
| test.swift:75:21:75:22 | &... : | test.swift:65:16:65:28 | arg1 : | test.swift:69:5:69:5 | arg2[return] : | test.swift:75:5:75:33 | arg2 : |
76+
| test.swift:75:21:75:22 | &... : | test.swift:65:16:65:28 | WriteDef : | test.swift:65:1:70:1 | arg2[return] : | test.swift:75:5:75:33 | arg2 : |
77+
| test.swift:75:21:75:22 | &... : | test.swift:65:16:65:28 | arg1 : | test.swift:65:1:70:1 | arg2[return] : | test.swift:75:5:75:33 | arg2 : |
6978
#select
7079
| test.swift:7:15:7:15 | t1 | test.swift:6:19:6:26 | call to source : | test.swift:7:15:7:15 | t1 | result |
7180
| test.swift:9:15:9:15 | t1 | test.swift:6:19:6:26 | call to source : | test.swift:9:15:9:15 | t1 | result |
@@ -76,4 +85,6 @@ subpaths
7685
| test.swift:50:15:50:15 | t | test.swift:43:19:43:26 | call to source : | test.swift:50:15:50:15 | t | result |
7786
| test.swift:62:15:62:15 | x | test.swift:54:11:54:18 | call to source : | test.swift:62:15:62:15 | x | result |
7887
| test.swift:77:15:77:15 | y | test.swift:73:18:73:25 | call to source : | test.swift:77:15:77:15 | y | result |
88+
| test.swift:98:19:98:19 | x | test.swift:81:11:81:18 | call to source : | test.swift:98:19:98:19 | x | result |
7989
| test.swift:105:19:105:19 | x | test.swift:86:15:86:22 | call to source : | test.swift:105:19:105:19 | x | result |
90+
| test.swift:105:19:105:19 | x | test.swift:89:15:89:22 | call to source : | test.swift:105:19:105:19 | x | result |

swift/ql/test/library-tests/dataflow/dataflow/LocalFlow.expected

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
| test.swift:48:9:48:13 | WriteDef | test.swift:50:5:50:5 | Phi |
2424
| test.swift:48:13:48:13 | 1 | test.swift:48:9:48:13 | WriteDef |
2525
| test.swift:50:5:50:5 | Phi | test.swift:50:15:50:15 | t |
26-
| test.swift:54:5:54:18 | WriteDef | test.swift:55:5:55:5 | arg[return] |
26+
| test.swift:54:5:54:18 | WriteDef | test.swift:53:1:56:1 | arg[return] |
2727
| test.swift:54:11:54:18 | call to source | test.swift:54:5:54:18 | WriteDef |
2828
| test.swift:59:9:59:12 | WriteDef | test.swift:60:15:60:15 | x |
2929
| test.swift:59:18:59:18 | 0 | test.swift:59:9:59:12 | WriteDef |
@@ -37,9 +37,9 @@
3737
| test.swift:65:33:65:45 | arg2 | test.swift:67:12:67:12 | arg2 |
3838
| test.swift:66:9:66:15 | WriteDef | test.swift:68:12:68:12 | temp |
3939
| test.swift:66:21:66:21 | arg1 | test.swift:66:9:66:15 | WriteDef |
40-
| test.swift:67:5:67:12 | WriteDef | test.swift:69:5:69:5 | arg1[return] |
40+
| test.swift:67:5:67:12 | WriteDef | test.swift:65:1:70:1 | arg1[return] |
4141
| test.swift:67:12:67:12 | arg2 | test.swift:67:5:67:12 | WriteDef |
42-
| test.swift:68:5:68:12 | WriteDef | test.swift:69:5:69:5 | arg2[return] |
42+
| test.swift:68:5:68:12 | WriteDef | test.swift:65:1:70:1 | arg2[return] |
4343
| test.swift:68:12:68:12 | temp | test.swift:68:5:68:12 | WriteDef |
4444
| test.swift:73:9:73:12 | WriteDef | test.swift:75:22:75:22 | x |
4545
| test.swift:73:18:73:25 | call to source | test.swift:73:9:73:12 | WriteDef |
@@ -51,10 +51,15 @@
5151
| test.swift:75:5:75:33 | arg2 | test.swift:75:5:75:33 | WriteDef |
5252
| test.swift:75:22:75:22 | x | test.swift:75:21:75:22 | &... |
5353
| test.swift:75:32:75:32 | y | test.swift:75:31:75:32 | &... |
54+
| test.swift:81:5:81:18 | WriteDef | test.swift:80:1:82:1 | arg[return] |
55+
| test.swift:81:11:81:18 | call to source | test.swift:81:5:81:18 | WriteDef |
56+
| test.swift:84:1:91:1 | Phi | test.swift:84:1:91:1 | arg[return] |
5457
| test.swift:84:48:84:54 | WriteDef | test.swift:85:8:85:8 | bool |
5558
| test.swift:84:48:84:54 | bool | test.swift:85:8:85:8 | bool |
56-
| test.swift:86:9:86:22 | WriteDef | test.swift:87:9:87:9 | arg[return] |
59+
| test.swift:86:9:86:22 | WriteDef | test.swift:84:1:91:1 | Phi |
5760
| test.swift:86:15:86:22 | call to source | test.swift:86:9:86:22 | WriteDef |
61+
| test.swift:89:9:89:22 | WriteDef | test.swift:84:1:91:1 | Phi |
62+
| test.swift:89:15:89:22 | call to source | test.swift:89:9:89:22 | WriteDef |
5863
| test.swift:93:17:93:23 | WriteDef | test.swift:104:50:104:50 | bool |
5964
| test.swift:93:17:93:23 | bool | test.swift:104:50:104:50 | bool |
6065
| test.swift:95:13:95:16 | WriteDef | test.swift:96:19:96:19 | x |

0 commit comments

Comments
 (0)