Skip to content

Commit 5838df1

Browse files
authored
Merge pull request #2766 from hvitved/csharp/stackalloc
C#: Extract `stackalloc` information
2 parents b9bc216 + 1948446 commit 5838df1

File tree

13 files changed

+3843
-13
lines changed

13 files changed

+3843
-13
lines changed

change-notes/1.24/analysis-csharp.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The following changes in version 1.24 affect C# analysis in all applications.
2929
* Tuple expressions, for example `(int,bool)` in `default((int,bool))` are now extracted correctly.
3030
* Expression nullability flow state is extracted.
3131
* Implicitly typed `stackalloc` expressions are now extracted correctly.
32+
* The difference between `stackalloc` array creations and normal array creations is extracted.
3233

3334
## Changes to libraries
3435

@@ -39,5 +40,6 @@ The following changes in version 1.24 affect C# analysis in all applications.
3940
* The taint tracking library now tracks flow through (implicit or explicit) conversion operator calls.
4041
* [Code contracts](https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/code-contracts) are now recognized, and are treated like any other assertion methods.
4142
* Expression nullability flow state is given by the predicates `Expr.hasNotNullFlowState()` and `Expr.hasMaybeNullFlowState()`.
43+
* `stackalloc` array creations are now represented by the QL class `Stackalloc`. Previously they were represented by the class `ArrayCreation`.
4244

4345
## Changes to autobuilder

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ArrayCreation.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ class StackAllocArrayCreation : ExplicitArrayCreation<StackAllocArrayCreationExp
9090

9191
public override InitializerExpressionSyntax Initializer => Syntax.Initializer;
9292

93+
protected override void PopulateExpression(TextWriter trapFile)
94+
{
95+
base.PopulateExpression(trapFile);
96+
trapFile.stackalloc_array_creation(this);
97+
}
98+
9399
public static Expression Create(ExpressionNodeInfo info) => new StackAllocArrayCreation(info).TryPopulate();
94100
}
95101

@@ -103,6 +109,7 @@ protected override void PopulateExpression(TextWriter trapFile)
103109
{
104110
ArrayInitializer.Create(new ExpressionNodeInfo(cx, Syntax.Initializer, this, -1));
105111
trapFile.implicitly_typed_array_creation(this);
112+
trapFile.stackalloc_array_creation(this);
106113
}
107114
}
108115

csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ internal static void specific_type_parameter_nullability(this TextWriter trapFil
466466
trapFile.WriteTuple("specific_type_parameter_nullability", constraints, baseType, nullability);
467467
}
468468

469+
internal static void stackalloc_array_creation(this TextWriter trapFile, Expression array)
470+
{
471+
trapFile.WriteTuple("stackalloc_array_creation", array);
472+
}
473+
469474
internal static void stmt_location(this TextWriter trapFile, Statement stmt, Location location)
470475
{
471476
trapFile.WriteTuple("stmt_location", stmt, location);

csharp/ql/src/semmle/code/csharp/exprs/Creation.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,13 @@ class ArrayCreation extends Expr, @array_creation_expr {
372372
override string toString() { result = "array creation of type " + this.getType().getName() }
373373
}
374374

375+
/**
376+
* A `stackalloc` array creation, for example `stackalloc char[] { 'x', 'y' }`.
377+
*/
378+
class Stackalloc extends ArrayCreation {
379+
Stackalloc() { stackalloc_array_creation(this) }
380+
}
381+
375382
/**
376383
* An anonymous function. Either a lambda expression (`LambdaExpr`) or an
377384
* anonymous method expression (`AnonymousMethodExpr`).

csharp/ql/src/semmlecode.csharp.dbscheme

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,9 @@ implicitly_typed_array_creation(
10841084
explicitly_sized_array_creation(
10851085
unique int id: @array_creation_expr ref);
10861086

1087+
stackalloc_array_creation(
1088+
unique int id: @array_creation_expr ref);
1089+
10871090
mutator_invocation_mode(
10881091
unique int id: @operator_invocation_expr ref,
10891092
int mode: int ref /* prefix = 1, postfix = 2*/);

csharp/ql/src/semmlecode.csharp.dbscheme.stats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28440,6 +28440,17 @@
2844028440
<dependencies/>
2844128441
</relation>
2844228442
<relation>
28443+
<name>stackalloc_array_creation</name>
28444+
<cardinality>50</cardinality>
28445+
<columnsizes>
28446+
<e>
28447+
<k>id</k>
28448+
<v>50</v>
28449+
</e>
28450+
</columnsizes>
28451+
<dependencies/>
28452+
</relation>
28453+
<relation>
2844328454
<name>mutator_invocation_mode</name>
2844428455
<cardinality>0</cardinality>
2844528456
<columnsizes>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1+
arrayCreation
12
| csharp73.cs:9:20:9:49 | array creation of type Char* | 0 | csharp73.cs:9:20:9:49 | 2 |
23
| csharp73.cs:10:20:10:45 | array creation of type Char* | 0 | csharp73.cs:10:36:10:36 | 1 |
34
| csharp73.cs:11:20:11:37 | array creation of type Char[] | 0 | csharp73.cs:11:20:11:37 | 1 |
45
| csharp73.cs:12:20:12:38 | array creation of type Char* | 0 | csharp73.cs:12:36:12:37 | 10 |
56
| csharp73.cs:13:20:13:31 | array creation of type Char[] | 0 | csharp73.cs:13:29:13:30 | 10 |
67
| csharp73.cs:22:23:22:33 | array creation of type Int32[] | 0 | csharp73.cs:22:31:22:32 | 10 |
8+
arrayElement
9+
| csharp73.cs:9:20:9:49 | array creation of type Char* | 0 | csharp73.cs:9:40:9:42 | x |
10+
| csharp73.cs:9:20:9:49 | array creation of type Char* | 1 | csharp73.cs:9:45:9:47 | y |
11+
| csharp73.cs:10:20:10:45 | array creation of type Char* | 0 | csharp73.cs:10:41:10:43 | x |
12+
| csharp73.cs:11:20:11:37 | array creation of type Char[] | 0 | csharp73.cs:11:33:11:35 | x |
13+
| csharp73.cs:14:20:14:43 | array creation of type Int32* | 0 | csharp73.cs:14:35:14:35 | 1 |
14+
| csharp73.cs:14:20:14:43 | array creation of type Int32* | 1 | csharp73.cs:14:38:14:38 | 2 |
15+
| csharp73.cs:14:20:14:43 | array creation of type Int32* | 2 | csharp73.cs:14:41:14:41 | 3 |
16+
stackalloc
17+
| csharp73.cs:9:20:9:49 | array creation of type Char* |
18+
| csharp73.cs:10:20:10:45 | array creation of type Char* |
19+
| csharp73.cs:12:20:12:38 | array creation of type Char* |
20+
| csharp73.cs:14:20:14:43 | array creation of type Int32* |
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import csharp
22

3-
from ArrayCreation creation, int i
4-
select creation, i, creation.getLengthArgument(i)
3+
query predicate arrayCreation(ArrayCreation creation, int i, Expr length) {
4+
length = creation.getLengthArgument(i)
5+
}
6+
7+
query predicate arrayElement(ArrayCreation array, int i, Expr element) {
8+
element = array.getInitializer().getElement(i)
9+
}
10+
11+
query predicate stackalloc(Stackalloc a) { any() }

csharp/ql/test/library-tests/csharp7.3/ArrayElements.expected

Lines changed: 0 additions & 7 deletions
This file was deleted.

csharp/ql/test/library-tests/csharp7.3/ArrayElements.ql

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)