Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions actions/ql/lib/codeql/actions/controlflow/internal/Cfg.qll
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,28 @@ module CfgImpl {
boolean getValue() { none() }
}

class Pattern extends AstNode {
Pattern() { none() }
}

class AndPattern extends Pattern {
AndPattern() { none() }
}

class OrPattern extends Pattern {
OrPattern() { none() }
}

class NotPattern extends Pattern {
NotPattern() { none() }

Pattern getNegatedPattern() { none() }
}

class BindingPattern extends Pattern {
BindingPattern() { none() }
}

class PatternMatchExpr extends Expr {
PatternMatchExpr() { none() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal static Expression Create(Context cx, PatternSyntax syntax, IExpressionP
if (cx.GetModel(syntax).GetDeclaredSymbol(singleDesignation) is ILocalSymbol symbol)
{
var type = symbol.GetAnnotatedType();
return VariableDeclaration.Create(cx, symbol, type, declPattern.Type, cx.CreateLocation(syntax.GetLocation()), false, parent, child);
return VariableDeclaration.Create(cx, symbol, type, declPattern.Type, cx.CreateLocation(syntax.GetLocation()), false, parent, child, isPattern: true);
}
throw new InternalError(singleDesignation, "Unable to get the declared symbol of the declaration pattern designation.");
case DiscardDesignationSyntax _:
Expand All @@ -59,7 +59,7 @@ internal static Expression Create(Context cx, PatternSyntax syntax, IExpressionP
if (cx.GetModel(syntax).GetDeclaredSymbol(varDesignation) is ILocalSymbol symbol)
{
var type = symbol.GetAnnotatedType();
return VariableDeclaration.Create(cx, symbol, type, null, cx.CreateLocation(syntax.GetLocation()), true, parent, child);
return VariableDeclaration.Create(cx, symbol, type, null, cx.CreateLocation(syntax.GetLocation()), true, parent, child, isPattern: true);
}

throw new InternalError(varPattern, "Unable to get the declared symbol of the var pattern designation.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public RecursivePattern(Context cx, RecursivePatternSyntax syntax, IExpressionPa
{
var type = symbol.GetAnnotatedType();

VariableDeclaration.Create(cx, symbol, type, null, cx.CreateLocation(syntax.GetLocation()), false, this, 0);
VariableDeclaration.Create(cx, symbol, type, null, cx.CreateLocation(syntax.GetLocation()), false, this, 0, isPattern: true);
}

if (syntax.PositionalPatternClause is PositionalPatternClauseSyntax posPc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ internal class VariableDeclaration : Expression
{
private VariableDeclaration(IExpressionInfo info) : base(info) { }

public static VariableDeclaration Create(Context cx, ISymbol symbol, AnnotatedTypeSymbol? type, TypeSyntax? optionalSyntax, Location exprLocation, bool isVar, IExpressionParentEntity parent, int child)
public static VariableDeclaration Create(Context cx, ISymbol symbol, AnnotatedTypeSymbol? type, TypeSyntax? optionalSyntax, Location exprLocation, bool isVar, IExpressionParentEntity parent, int child, bool isPattern = false)
{
var ret = new VariableDeclaration(new ExpressionInfo(cx, type, exprLocation, ExprKind.LOCAL_VAR_DECL, parent, child, isCompilerGenerated: false, null));
cx.Try(null, null, () =>
{
var l = LocalVariable.Create(cx, symbol);
l.PopulateManual(ret, isVar);
if (isPattern)
ret.CreateDefiningAccess(l, type);
if (optionalSyntax is not null)
TypeMention.Create(cx, optionalSyntax, ret, type);
});
Expand Down Expand Up @@ -86,10 +88,7 @@ public static Expression CreateParenthesized(Context cx, VarPatternSyntax varPat
case SingleVariableDesignationSyntax single:
if (cx.GetModel(variable).GetDeclaredSymbol(single) is ILocalSymbol local)
{
var decl = Create(cx, variable, local.GetAnnotatedType(), tuple, child0++);
var l = LocalVariable.Create(cx, local);
l.PopulateManual(decl, true);
sub = decl;
sub = Create(cx, local, local.GetAnnotatedType(), null, cx.CreateLocation(single.FixedLocation()), true, tuple, child0++, isPattern: true);
}
else
{
Expand Down Expand Up @@ -176,10 +175,7 @@ public static VariableDeclaration CreateDeclarator(Context cx, VariableDeclarato

if (d.Initializer is not null)
{
// Create an access
var access = new Expression(new ExpressionInfo(cx, type, localVar.Location, ExprKind.LOCAL_VARIABLE_ACCESS, ret, 0, isCompilerGenerated: false, null));
cx.TrapWriter.Writer.expr_access(access, localVar);

ret.CreateDefiningAccess(localVar, type);
Create(cx, d.Initializer.Value, ret, 1);
}

Expand All @@ -188,6 +184,12 @@ public static VariableDeclaration CreateDeclarator(Context cx, VariableDeclarato
});
return ret;
}

private void CreateDefiningAccess(LocalVariable localVar, AnnotatedTypeSymbol? type)
{
var access = new Expression(new ExpressionInfo(Context, type, localVar.Location, ExprKind.LOCAL_VARIABLE_ACCESS, this, 0, isCompilerGenerated: false, null));
Context.TrapWriter.Writer.expr_access(access, localVar);
}
}

internal static class VariableDeclarations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: fix
---
* The C# extractor now records defining accesses for local variables declared in patterns. These accesses are available through `LocalVariableDeclExpr.getAccess()` and are classified as writes.
6 changes: 4 additions & 2 deletions csharp/ql/lib/semmle/code/csharp/Assignable.qll
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ module AssignableInternal {
or
def = TAddressOfDefinition(any(AddressOfExpr aoe | aoe.getOperand() = result))
or
def = TPatternDefinition(any(LocalVariablePatternDecl decl | decl.getAccess() = result))
or
def = TAssignOperationDefinition(any(AssignOperation ao | ao.getLeftOperand() = result))
}

Expand Down Expand Up @@ -474,8 +476,8 @@ class AssignableDefinition extends TAssignableDefinition {

/**
* Gets the access used in the definition of the underlying assignable,
* if any. Local variable declarations and implicit parameter definitions
* are the only definitions without associated accesses.
* if any. For example, an uninitialized local variable declaration or an
* implicit parameter definition has no associated access.
*/
final AssignableAccess getTargetAccess() { result = getTargetAccess(this) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ private module Input implements InputSig1, InputSig2 {

predicate matchAll(Ast::Case c) { c.(SwitchCaseExpr).matchesAll() }

predicate patternPerformsTest(Ast::Pattern pattern) {
not pattern instanceof RecursivePatternExpr
}

predicate patternIsExhaustive(Ast::Pattern pattern) { pattern instanceof DiscardPatternExpr }

private newtype TLabel =
TLblGoto(string label) { any(GotoLabelStmt goto).getLabel() = label } or
TLblSwitchCase(string value) { any(GotoCaseStmt goto).getLabel() = value } or
Expand Down
1 change: 1 addition & 0 deletions csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
ct.getComparisonKind().isInequality() and polarity = false
)
or
// todo: handled in shared lib?
exists(IsExpr ie, PatternExpr pat |
ie = eqtest and
ie.getExpr() = left and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module Ast implements AstSig<Location> {

additional predicate skipControlFlow(AstNode e) {
e instanceof TypeAccess and
not e instanceof TypeAccessPatternExpr and
// not e instanceof TypeAccessPatternExpr and
not any(CS::SpecificCatchClause sc).getTypeAccess() = e
or
not e.getFile().fromSource()
Expand Down Expand Up @@ -295,6 +295,24 @@ module Ast implements AstSig<Location> {
boolean getValue() { result = this.getBoolValue() }
}

final class Pattern = PatternImpl;

abstract private class PatternImpl extends AstNode { }

private class PatternExprPattern extends PatternImpl, CS::PatternExpr { }

class AndPattern extends Pattern instanceof CS::AndPatternExpr { }

class OrPattern extends Pattern instanceof CS::OrPatternExpr { }

class NotPattern extends Pattern instanceof CS::NotPatternExpr {
Pattern getNegatedPattern() { result = super.getPattern() }
}

class BindingPattern extends PatternImpl {
BindingPattern() { this = any(CS::VariablePatternExpr vp).getAccess() }
}

final private class FinalIsExpr = CS::IsExpr;

class PatternMatchExpr extends FinalIsExpr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ private module Impl {
* true.
*/
predicate isEquality(ExprNode e1, ExprNode e2, boolean polarity) {
exists(Expr e1_, Expr e2_ |
e1 = unique(ExprNode cfn | hasChild(this, e1_, _, cfn) | cfn) and
e2 = unique(ExprNode cfn | hasChild(this, e2_, _, cfn) | cfn) and
super.isEquality(e1_, e2_, any(G::GuardValue gv | gv.asBooleanValue() = polarity))
)
super
.isEquality(e1.getExpr(), e2.getExpr(),
any(G::GuardValue gv | gv.asBooleanValue() = polarity))
}
}

Expand Down
2 changes: 1 addition & 1 deletion csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class LocalVariableDeclExpr extends Expr, @local_var_decl_expr {
result = "_"
}

/** Gets the variable access used in this declaration, if any. */
/** Gets the defining variable access used in this declaration, if any. */
LocalVariableAccess getAccess() {
result = this.getChild(0) or
result = this // `out` argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,19 @@
| Finally.cs:17:16:17:16 | access to local variable i | Finally.cs:7:13:7:13 | i | read |
| Patterns.cs:7:16:7:16 | access to local variable o | Patterns.cs:7:16:7:16 | o | write |
| Patterns.cs:8:13:8:13 | access to local variable o | Patterns.cs:7:16:7:16 | o | read |
| Patterns.cs:8:22:8:23 | access to local variable i1 | Patterns.cs:8:22:8:23 | i1 | write |
| Patterns.cs:10:38:10:39 | access to local variable i1 | Patterns.cs:8:22:8:23 | i1 | read |
| Patterns.cs:12:18:12:18 | access to local variable o | Patterns.cs:7:16:7:16 | o | read |
| Patterns.cs:12:30:12:31 | access to local variable s1 | Patterns.cs:12:30:12:31 | s1 | write |
| Patterns.cs:14:41:14:42 | access to local variable s1 | Patterns.cs:12:30:12:31 | s1 | read |
| Patterns.cs:16:18:16:18 | access to local variable o | Patterns.cs:7:16:7:16 | o | read |
| Patterns.cs:16:27:16:28 | access to local variable v1 | Patterns.cs:16:27:16:28 | v1 | write |
| Patterns.cs:20:17:20:17 | access to local variable o | Patterns.cs:7:16:7:16 | o | read |
| Patterns.cs:24:22:24:23 | access to local variable i2 | Patterns.cs:24:22:24:23 | i2 | write |
| Patterns.cs:24:30:24:31 | access to local variable i2 | Patterns.cs:24:22:24:23 | i2 | read |
| Patterns.cs:25:47:25:48 | access to local variable i2 | Patterns.cs:24:22:24:23 | i2 | read |
| Patterns.cs:27:22:27:23 | access to local variable i3 | Patterns.cs:27:22:27:23 | i3 | write |
| Patterns.cs:28:42:28:43 | access to local variable i3 | Patterns.cs:27:22:27:23 | i3 | read |
| Patterns.cs:30:25:30:26 | access to local variable s2 | Patterns.cs:30:25:30:26 | s2 | write |
| Patterns.cs:31:45:31:46 | access to local variable s2 | Patterns.cs:30:25:30:26 | s2 | read |
| Patterns.cs:33:22:33:23 | access to local variable v2 | Patterns.cs:33:22:33:23 | v2 | write |
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@
| Finally.cs:7:13:7:13 | i | Finally.cs:7:13:7:17 | Int32 i = ... | Finally.cs:7:13:7:13 | access to local variable i | Finally.cs:7:17:7:17 | 0 | certain |
| Finally.cs:7:13:7:13 | i | Finally.cs:15:13:15:17 | ... = ... | Finally.cs:15:13:15:13 | access to local variable i | Finally.cs:15:17:15:17 | 1 | certain |
| Patterns.cs:7:16:7:16 | o | Patterns.cs:7:16:7:23 | Object o = ... | Patterns.cs:7:16:7:16 | access to local variable o | Patterns.cs:7:20:7:23 | null | certain |
| Patterns.cs:8:22:8:23 | i1 | Patterns.cs:8:18:8:23 | Int32 i1 | Patterns.cs:8:18:8:23 | <none> | Patterns.cs:8:13:8:13 | access to local variable o | certain |
| Patterns.cs:12:30:12:31 | s1 | Patterns.cs:12:23:12:31 | String s1 | Patterns.cs:12:23:12:31 | <none> | Patterns.cs:12:18:12:18 | access to local variable o | certain |
| Patterns.cs:16:27:16:28 | v1 | Patterns.cs:16:23:16:28 | Object v1 | Patterns.cs:16:23:16:28 | <none> | Patterns.cs:16:18:16:18 | access to local variable o | certain |
| Patterns.cs:24:22:24:23 | i2 | Patterns.cs:24:18:24:23 | Int32 i2 | Patterns.cs:24:18:24:23 | <none> | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:27:22:27:23 | i3 | Patterns.cs:27:18:27:23 | Int32 i3 | Patterns.cs:27:18:27:23 | <none> | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:30:25:30:26 | s2 | Patterns.cs:30:18:30:26 | String s2 | Patterns.cs:30:18:30:26 | <none> | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:33:22:33:23 | v2 | Patterns.cs:33:18:33:23 | Object v2 | Patterns.cs:33:18:33:23 | <none> | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:8:22:8:23 | i1 | Patterns.cs:8:18:8:23 | Int32 i1 | Patterns.cs:8:22:8:23 | access to local variable i1 | Patterns.cs:8:13:8:13 | access to local variable o | certain |
| Patterns.cs:12:30:12:31 | s1 | Patterns.cs:12:23:12:31 | String s1 | Patterns.cs:12:30:12:31 | access to local variable s1 | Patterns.cs:12:18:12:18 | access to local variable o | certain |
| Patterns.cs:16:27:16:28 | v1 | Patterns.cs:16:23:16:28 | Object v1 | Patterns.cs:16:27:16:28 | access to local variable v1 | Patterns.cs:16:18:16:18 | access to local variable o | certain |
| Patterns.cs:24:22:24:23 | i2 | Patterns.cs:24:18:24:23 | Int32 i2 | Patterns.cs:24:22:24:23 | access to local variable i2 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:27:22:27:23 | i3 | Patterns.cs:27:18:27:23 | Int32 i3 | Patterns.cs:27:22:27:23 | access to local variable i3 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:30:25:30:26 | s2 | Patterns.cs:30:18:30:26 | String s2 | Patterns.cs:30:25:30:26 | access to local variable s2 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:33:22:33:23 | v2 | Patterns.cs:33:18:33:23 | Object v2 | Patterns.cs:33:22:33:23 | access to local variable v2 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
Loading
Loading