Skip to content

Commit 9d1f64d

Browse files
committed
C#: Avoid overlapping SSA definitions for ref/out captured variables
1 parent 723699a commit 9d1f64d

File tree

12 files changed

+15
-21
lines changed

12 files changed

+15
-21
lines changed

csharp/ql/src/semmle/code/csharp/dataflow/SSA.qll

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,14 @@ module Ssa {
14301430
) {
14311431
possiblyLiveAtAllNodes(bb, v) and
14321432
callAt(bb, i, call) and
1433-
relevantDefinition(_, v.getAssignable(), _)
1433+
exists(Assignable a |
1434+
a = v.getAssignable() and
1435+
relevantDefinition(_, a, _) and
1436+
not exists(AssignableDefinitions::OutRefDefinition def |
1437+
def.getCall() = call and
1438+
def.getTarget() = a
1439+
)
1440+
)
14341441
}
14351442

14361443
/**

csharp/ql/test/library-tests/dataflow/ssa/Capture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ void CaptureWrite()
245245
i = 1;
246246
}
247247

248-
void CaptureAndRef(ref int i)
248+
void CaptureAndRef(ref int j)
249249
{
250250
CaptureWrite();
251-
i = 2;
251+
j = 2;
252252
}
253253

254254
CaptureAndRef(ref i); // explicit definition only (no call definition)

csharp/ql/test/library-tests/dataflow/ssa/IsLiveOutRefParameterDefinition.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
| Capture.cs:81:28:81:28 | i | Capture.cs:81:34:81:36 | SSA def(i) |
2-
| Capture.cs:248:36:248:36 | i | Capture.cs:251:13:251:17 | SSA def(i) |
2+
| Capture.cs:248:36:248:36 | j | Capture.cs:251:13:251:17 | SSA def(j) |
33
| Consistency.cs:30:30:30:30 | c | Consistency.cs:32:9:32:29 | SSA def(c) |
44
| DefUse.cs:114:42:114:42 | i | DefUse.cs:114:47:114:52 | SSA def(i) |
55
| DefUse.cs:116:42:116:42 | i | DefUse.cs:116:47:116:51 | SSA def(i) |

csharp/ql/test/library-tests/dataflow/ssa/SsaCapturedVariableDef.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,4 @@
3737
| out | Capture.cs:139:13:139:13 | d | Capture.cs:142:13:142:17 | SSA def(d) | Capture.cs:144:9:144:12 | SSA call def(d) | Capture.cs:144:9:144:12 | call to local function M4 | false |
3838
| out | Capture.cs:168:13:168:13 | h | Capture.cs:174:17:174:21 | SSA def(h) | Capture.cs:176:13:176:16 | SSA call def(h) | Capture.cs:176:13:176:16 | call to local function M9 | false |
3939
| out | Capture.cs:229:13:229:13 | i | Capture.cs:235:21:235:25 | SSA def(i) | Capture.cs:236:9:236:12 | SSA call def(i) | Capture.cs:236:9:236:12 | call to local function M3 | false |
40-
| out | Capture.cs:242:13:242:13 | i | Capture.cs:245:13:245:17 | SSA def(i) | Capture.cs:254:9:254:28 | SSA call def(i) | Capture.cs:254:9:254:28 | call to local function CaptureAndRef | true |
4140
| out | DefUse.cs:167:23:167:23 | i | DefUse.cs:173:13:173:17 | SSA def(i) | DefUse.cs:181:9:181:11 | SSA call def(i) | DefUse.cs:181:9:181:11 | delegate call | false |
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
| Capture.cs:255:34:255:34 | access to local variable i | Read is associated with multiple SSA definitions |

csharp/ql/test/library-tests/dataflow/ssa/SsaDef.expected

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@
8383
| Capture.cs:229:13:229:13 | i | Capture.cs:235:21:235:25 | SSA def(i) |
8484
| Capture.cs:229:13:229:13 | i | Capture.cs:236:9:236:12 | SSA call def(i) |
8585
| Capture.cs:242:13:242:13 | i | Capture.cs:242:13:242:17 | SSA def(i) |
86-
| Capture.cs:242:13:242:13 | i | Capture.cs:245:13:245:17 | SSA def(i) |
87-
| Capture.cs:242:13:242:13 | i | Capture.cs:254:9:254:28 | SSA call def(i) |
8886
| Capture.cs:242:13:242:13 | i | Capture.cs:254:27:254:27 | SSA def(i) |
89-
| Capture.cs:248:36:248:36 | i | Capture.cs:251:13:251:17 | SSA def(i) |
87+
| Capture.cs:248:36:248:36 | j | Capture.cs:251:13:251:17 | SSA def(j) |
9088
| Consistency.cs:7:25:7:25 | b | Consistency.cs:7:25:7:25 | SSA param(b) |
9189
| Consistency.cs:15:17:15:17 | i | Consistency.cs:15:17:15:21 | SSA def(i) |
9290
| Consistency.cs:15:17:15:17 | i | Consistency.cs:15:17:15:21 | [finally: exception(Exception)] SSA def(i) |

csharp/ql/test/library-tests/dataflow/ssa/SsaDefElement.expected

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@
8383
| Capture.cs:235:21:235:25 | SSA def(i) | Capture.cs:235:21:235:25 | ... = ... |
8484
| Capture.cs:236:9:236:12 | SSA call def(i) | Capture.cs:236:9:236:12 | call to local function M3 |
8585
| Capture.cs:242:13:242:17 | SSA def(i) | Capture.cs:242:13:242:17 | Int32 i = ... |
86-
| Capture.cs:245:13:245:17 | SSA def(i) | Capture.cs:245:13:245:17 | ... = ... |
87-
| Capture.cs:251:13:251:17 | SSA def(i) | Capture.cs:251:13:251:17 | ... = ... |
88-
| Capture.cs:254:9:254:28 | SSA call def(i) | Capture.cs:254:9:254:28 | call to local function CaptureAndRef |
86+
| Capture.cs:251:13:251:17 | SSA def(j) | Capture.cs:251:13:251:17 | ... = ... |
8987
| Capture.cs:254:27:254:27 | SSA def(i) | Capture.cs:254:9:254:28 | call to local function CaptureAndRef |
9088
| Consistency.cs:7:25:7:25 | SSA param(b) | Consistency.cs:7:25:7:25 | b |
9189
| Consistency.cs:15:17:15:21 | SSA def(i) | Consistency.cs:15:17:15:21 | Int32 i = ... |

csharp/ql/test/library-tests/dataflow/ssa/SsaDefLastRead.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
| Capture.cs:229:13:229:13 | i | Capture.cs:231:9:231:49 | SSA capture def(i) | Capture.cs:231:47:231:47 | access to local variable i |
6060
| Capture.cs:229:13:229:13 | i | Capture.cs:236:9:236:12 | SSA call def(i) | Capture.cs:237:34:237:34 | access to local variable i |
6161
| Capture.cs:242:13:242:13 | i | Capture.cs:242:13:242:17 | SSA def(i) | Capture.cs:254:27:254:27 | access to local variable i |
62-
| Capture.cs:242:13:242:13 | i | Capture.cs:254:9:254:28 | SSA call def(i) | Capture.cs:255:34:255:34 | access to local variable i |
6362
| Capture.cs:242:13:242:13 | i | Capture.cs:254:27:254:27 | SSA def(i) | Capture.cs:255:34:255:34 | access to local variable i |
6463
| Consistency.cs:7:25:7:25 | b | Consistency.cs:7:25:7:25 | SSA param(b) | Consistency.cs:11:17:11:17 | access to parameter b |
6564
| Consistency.cs:15:17:15:17 | i | Consistency.cs:15:17:15:21 | SSA def(i) | Consistency.cs:16:17:16:17 | access to local variable i |

csharp/ql/test/library-tests/dataflow/ssa/SsaExplicitDef.expected

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@
5555
| Capture.cs:229:13:229:13 | i | Capture.cs:232:9:232:13 | SSA def(i) | Capture.cs:232:9:232:13 | ... = ... |
5656
| Capture.cs:229:13:229:13 | i | Capture.cs:235:21:235:25 | SSA def(i) | Capture.cs:235:21:235:25 | ... = ... |
5757
| Capture.cs:242:13:242:13 | i | Capture.cs:242:13:242:17 | SSA def(i) | Capture.cs:242:13:242:17 | Int32 i = ... |
58-
| Capture.cs:242:13:242:13 | i | Capture.cs:245:13:245:17 | SSA def(i) | Capture.cs:245:13:245:17 | ... = ... |
5958
| Capture.cs:242:13:242:13 | i | Capture.cs:254:27:254:27 | SSA def(i) | Capture.cs:254:27:254:27 | access to local variable i |
60-
| Capture.cs:248:36:248:36 | i | Capture.cs:251:13:251:17 | SSA def(i) | Capture.cs:251:13:251:17 | ... = ... |
59+
| Capture.cs:248:36:248:36 | j | Capture.cs:251:13:251:17 | SSA def(j) | Capture.cs:251:13:251:17 | ... = ... |
6160
| Consistency.cs:7:25:7:25 | b | Consistency.cs:7:25:7:25 | SSA param(b) | Consistency.cs:7:25:7:25 | b |
6261
| Consistency.cs:15:17:15:17 | i | Consistency.cs:15:17:15:21 | SSA def(i) | Consistency.cs:15:17:15:21 | Int32 i = ... |
6362
| Consistency.cs:15:17:15:17 | i | Consistency.cs:15:17:15:21 | [finally: exception(Exception)] SSA def(i) | Consistency.cs:15:17:15:21 | Int32 i = ... |

csharp/ql/test/library-tests/dataflow/ssa/SsaImplicitCall.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
| Capture.cs:139:13:139:13 | d | Capture.cs:144:9:144:12 | SSA call def(d) | Capture.cs:142:13:142:17 | ... = ... |
1010
| Capture.cs:168:13:168:13 | h | Capture.cs:176:13:176:16 | SSA call def(h) | Capture.cs:174:17:174:21 | ... = ... |
1111
| Capture.cs:229:13:229:13 | i | Capture.cs:236:9:236:12 | SSA call def(i) | Capture.cs:235:21:235:25 | ... = ... |
12-
| Capture.cs:242:13:242:13 | i | Capture.cs:254:9:254:28 | SSA call def(i) | Capture.cs:245:13:245:17 | ... = ... |
1312
| DefUse.cs:167:23:167:23 | i | DefUse.cs:181:9:181:11 | SSA call def(i) | DefUse.cs:173:13:173:17 | ... = ... |
1413
| DefUse.cs:184:9:184:14 | this.Field5 | DefUse.cs:191:9:191:11 | SSA call def(this.Field5) | DefUse.cs:188:13:188:22 | ... = ... |
1514
| Example.cs:8:9:8:18 | this.Field | Example.cs:13:13:13:23 | SSA call def(this.Field) | Example.cs:8:9:8:22 | ... = ... |

0 commit comments

Comments
 (0)