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

Skip to content

Commit 0ee5fe8

Browse files
committed
CIL: Account for multiple VariableUpdate::getSource()s in nullness analysis
For methods compiled without optimization (and possibly also with optimization), it is possible for a variable update to have multiple possible assigned values. For example, the non-optimized CIL for ``` return cond ? null : "not null" ``` is ``` 0: nop 1: ldarg.0 2: ldfld cond 3: brtrue.s 6: 4: ldstr "not null" 5: br.s 7: 6: ldnull 7: stloc.0 L0 // stores either `null` or "not null" 8: br.s 9: 9: ldloc.0 10: ret ``` Consequently, an existential in `CallableReturns.qll` must be a `forex`.
1 parent 4fc61eb commit 0ee5fe8

3 files changed

Lines changed: 4 additions & 8 deletions

File tree

csharp/ql/src/semmle/code/cil/CallableReturns.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ private predicate alwaysNullExpr(Expr expr) {
3636
or
3737
alwaysNullMethod(expr.(StaticCall).getTarget())
3838
or
39-
forex(VariableUpdate vu | DefUse::variableUpdateUse(_, vu, expr) | alwaysNullExpr(vu.getSource()))
39+
forex(VariableUpdate vu | DefUse::variableUpdateUse(_, vu, expr) |
40+
forex(Expr src | src = vu.getSource() | alwaysNullExpr(src))
41+
)
4042
}
4143

4244
/** Holds if expression `expr` always evaluates to non-null. */
@@ -48,6 +50,6 @@ private predicate alwaysNotNullExpr(Expr expr) {
4850
alwaysNotNullMethod(expr.(StaticCall).getTarget())
4951
or
5052
forex(VariableUpdate vu | DefUse::variableUpdateUse(_, vu, expr) |
51-
alwaysNotNullExpr(vu.getSource())
53+
forex(Expr src | src = vu.getSource() | alwaysNotNullExpr(src))
5254
)
5355
}

csharp/ql/test/library-tests/cil/dataflow/CallableReturns.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ alwaysNull
77
| System.Object Dataflow.NullMethods.get_NullProperty() | 0: ldnull, 1: ret |
88
| System.Object Dataflow.NullMethods.get_VirtualNullProperty() | 0: ldnull, 1: ret |
99
| System.Object System.Collections.EmptyReadOnlyDictionaryInternal.get_Item(System.Object) | 0: ldarg.1, 1: brtrue.s 6:, 2: ldstr "key", 3: call System.SR.get_ArgumentNull_Key, 4: newobj System.ArgumentNullException..ctor, 5: throw, 6: ldnull, 7: ret |
10-
| System.String DataflowUnoptimized.MaybeNullMethods.MaybeNull2() | 0: nop, 1: ldarg.0, 2: ldfld cond, 3: brtrue.s 6:, 4: ldstr "not null", 5: br.s 7:, 6: ldnull, 7: stloc.0 L0, 8: br.s 9:, 9: ldloc.0, 10: ret |
1110
alwaysNonNull
1211
| System.ArgumentException System.ThrowHelper.GetAddingDuplicateWithKeyArgumentException(System.Object) |
1312
| System.ArgumentException System.ThrowHelper.GetArgumentException(System.ExceptionResource) |
@@ -25,7 +24,6 @@ alwaysNonNull
2524
| System.Object Dataflow.NonNullMethods.get_VirtualNonNull() |
2625
| System.Object Dataflow.NonNullMethods.get_VirtualNonNullProperty() |
2726
| System.String Dataflow.NonNullMethods.get_NonNullProperty2() |
28-
| System.String DataflowUnoptimized.MaybeNullMethods.MaybeNull2() |
2927
| System.Text.Encoder System.Text.ASCIIEncoding.GetEncoder() |
3028
| System.Text.Encoder System.Text.Encoding.GetEncoder() |
3129
| System.Text.Encoder System.Text.EncodingNLS.GetEncoder() |

csharp/ql/test/library-tests/cil/dataflow/Nullness.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ alwaysNull
66
| dataflow.cs:79:21:79:46 | call to method ReturnsNull2 |
77
| dataflow.cs:80:21:80:44 | access to property NullProperty |
88
| dataflow.cs:89:31:89:44 | call to method NullFunction |
9-
| dataflow.cs:99:9:99:50 | ... = ... |
10-
| dataflow.cs:99:22:99:50 | call to method MaybeNull2 |
119
alwaysNotNull
1210
| dataflow.cs:71:24:71:35 | default(...) |
1311
| dataflow.cs:72:27:72:30 | this access |
@@ -36,6 +34,4 @@ alwaysNotNull
3634
| dataflow.cs:96:26:96:32 | access to local variable nonNull |
3735
| dataflow.cs:97:32:97:73 | object creation of type MaybeNullMethods |
3836
| dataflow.cs:98:21:98:36 | access to local variable maybeNullMethods |
39-
| dataflow.cs:99:9:99:50 | ... = ... |
4037
| dataflow.cs:99:22:99:37 | access to local variable maybeNullMethods |
41-
| dataflow.cs:99:22:99:50 | call to method MaybeNull2 |

0 commit comments

Comments
 (0)