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
29 changes: 29 additions & 0 deletions cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,35 @@ module GuardsInput implements SharedGuards::InputSig<Cpp::Location, Instruction,
)
}

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

/** Gets a subpattern of this pattern. */
Pattern getASubPattern() { none() }
}

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

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

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

Pattern getNegatedPattern() { none() }
}

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

Expr getExpr() { none() }

AstNode getPattern() { none() }
}

abstract private class BinExpr extends Expr instanceof BinaryInstruction {
Expr getAnOperand() { result = super.getAnInput() }
}
Expand Down
2 changes: 1 addition & 1 deletion csharp/ql/lib/semmle/code/csharp/Assignable.qll
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ module AssignableInternal {
or
def = TAddressOfDefinition(result)
or
def = TPatternDefinition(result)
def = TPatternDefinition(any(LocalVariablePatternDecl decl | decl.getAccess() = result))
or
def = TAssignOperationDefinition(result)
or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ private module Input implements InputSig1, InputSig2 {

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

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

predicate patternIsExhaustive(Ast::Pattern pattern) {
pattern instanceof DiscardPatternExpr
or
pattern = any(SlicePatternExpr slice | not exists(slice.getPattern()))
or
pattern = any(RecursivePatternExpr rpe).getVariableDeclExpr()
or
// `var x` always matches, also when the value is `null`
pattern.(VariablePatternExpr).isImplicitlyTyped()
}

private newtype TLabel =
TLblGoto(string label) { any(GotoLabelStmt goto).getLabel() = label } or
TLblSwitchCase(string value) { any(GotoCaseStmt goto).getLabel() = value } or
Expand Down
197 changes: 125 additions & 72 deletions csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
AstNode getAPattern() { super.getPattern() = result }
}

final private class FinalIsExpr = CS::IsExpr;

class Pattern extends CS::PatternExpr {
Pattern getASubPattern() { result = this.getAChild() }
}

class AndPattern extends Pattern instanceof CS::AndPatternExpr { }

class OrPattern extends Pattern instanceof CS::OrPatternExpr { }

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

class PatternMatchExpr extends FinalIsExpr {
AstNode getPattern() { result = super.getPattern() }
}

predicate nonDeconstructingSubPattern(AstNode pattern, AstNode subPattern) {
pattern =
any(RecursivePatternExpr rpe |
subPattern =
[
rpe.getTypeAccess().(AstNode), //
rpe.getVariableDeclExpr(), //
rpe.getPositionalPatterns(), //
rpe.getPropertyPatterns()
]
)
}

abstract private class BinExpr extends BinaryOperation { }

class AndExpr extends BinExpr {
Expand Down Expand Up @@ -125,18 +156,6 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
or
ct.getComparisonKind().isInequality() and polarity = false
)
or
exists(IsExpr ie, PatternExpr pat |
ie = eqtest and
ie.getExpr() = left and
ie.getPattern() = pat
|
right = pat.(ConstantPatternExpr) and
polarity = true
or
right = pat.(NotPatternExpr).getPattern().(ConstantPatternExpr) and
polarity = false
)
}

class ConditionalExpr = CS::ConditionalExpr;
Expand Down Expand Up @@ -209,38 +228,15 @@ private module LogicInput implements GuardsImpl::LogicInputSig {
isNull = false
)
or
// TODO: Remove this superfluous case once CFG changes for IsExpr lands.
guard =
any(PatternMatch pm |
e instanceof DereferenceableExpr and
e = pm.getExpr() and
(
val.asBooleanValue().booleanNot() = patternMatchesNull(pm.getPattern()) and
isNull = false
or
exists(TypePatternExpr tpe |
// E.g. `x is string` where `x` has type `string`
typePattern(guard, tpe, tpe.getCheckedType()) and
val.asBooleanValue() = false and
isNull = true
)
)
)
or
// E.g. `x is string` where `x` has type `string`
exists(PatternMatch pm |
guard = pm.getPattern() and
e instanceof DereferenceableExpr and
e = pm.getExpr()
|
val.isMatch(patternMatchesNull(pm.getPattern()).booleanNot()) and
isNull = false
or
exists(TypePatternExpr tpe |
// E.g. `x is string` where `x` has type `string`
typePattern(pm, tpe, tpe.getCheckedType()) and
val.isMatch(false) and
isNull = true
)
e = pm.getExpr() and
GuardsInput::nonDeconstructingSubPattern*(pm.getPattern(), guard) and
Comment thread
hvitved marked this conversation as resolved.
Resolved
guard.(TypePatternExpr).getCheckedType() = e.getType() and
not guard.(VariablePatternExpr).isImplicitlyTyped() and
val.isMatch(false) and
isNull = true
)
or
e.(DereferenceableExpr).hasNullableType() and
Expand Down Expand Up @@ -355,35 +351,6 @@ deprecated module AbstractValues {
}
}

/** Gets the value resulting from matching `null` against `pat`. */
private boolean patternMatchesNull(PatternExpr pat) {
pat instanceof NullLiteral and result = true
or
not pat instanceof NullLiteral and
not pat instanceof NotPatternExpr and
not pat instanceof OrPatternExpr and
not pat instanceof AndPatternExpr and
result = false
or
result = patternMatchesNull(pat.(NotPatternExpr).getPattern()).booleanNot()
or
exists(OrPatternExpr ope | pat = ope |
result =
patternMatchesNull(ope.getLeftOperand()).booleanOr(patternMatchesNull(ope.getRightOperand()))
)
or
exists(AndPatternExpr ape | pat = ape |
result =
patternMatchesNull(ape.getLeftOperand()).booleanAnd(patternMatchesNull(ape.getRightOperand()))
)
}

pragma[nomagic]
private predicate typePattern(PatternMatch pm, TypePatternExpr tpe, Type t) {
tpe = pm.getPattern() and
t = pm.getExpr().getType()
}

pragma[nomagic]
private predicate dereferenceableExpr(Expr e, boolean isNullableType) {
exists(Type t | t = e.getType() |
Expand Down Expand Up @@ -854,6 +821,71 @@ module Internal {
NonNullCallable() { this = any(SystemObjectClass c).getGetTypeMethod() }
}

private Expr constantPatternInitializer(PatternExpr pat) {
pat instanceof ConstantPatternExpr and
exists(Variable variable |
variable = pat.(VariableAccess).getTarget() and
(variable instanceof LocalConstant or variable.(Field).isConst()) and
result = variable.getInitializer().stripCasts()
)
}

/** Gets the result of matching `null` against `pat`, when known. */
private boolean patternMatchesNull(PatternExpr pat) {
result = true and
(
nullValue(pat)
or
pat instanceof DiscardPatternExpr
or
pat.(VariablePatternExpr).isImplicitlyTyped()
or
pat.(DefaultValueExpr).getType() instanceof NullableType
or
nullValue(constantPatternInitializer(pat))
or
constantPatternInitializer(pat).(DefaultValueExpr).getType() instanceof NullableType
)
or
result = false and
(
pat instanceof Literal and not pat instanceof NullLiteral
or
pat instanceof TypePatternExpr and not pat.(VariablePatternExpr).isImplicitlyTyped()
or
pat instanceof RelationalPatternExpr
or
pat instanceof RecursivePatternExpr
or
pat instanceof ListPatternExpr
or
// The extracted value "null" may denote either null or the string "null".
pat.(ConstantPatternExpr).getValue() != "null"
or
constantPatternInitializer(pat) instanceof StringLiteral
)
or
result = patternMatchesNull(pat.(NotPatternExpr).getPattern()).booleanNot()
or
pat =
any(OrPatternExpr orPat |
result = true and patternMatchesNull(orPat.getAnOperand()) = true
or
result = false and
patternMatchesNull(orPat.getLeftOperand()) = false and
patternMatchesNull(orPat.getRightOperand()) = false
)
or
pat =
any(AndPatternExpr andPat |
result = false and patternMatchesNull(andPat.getAnOperand()) = false
or
result = true and
patternMatchesNull(andPat.getLeftOperand()) = true and
patternMatchesNull(andPat.getRightOperand()) = true
)
}

/** Holds if expression `e` is a non-`null` value. */
predicate nonNullValue(Expr e) {
e instanceof ObjectCreation
Expand All @@ -864,6 +896,11 @@ module Internal {
not nullValueImplied(e)
or
e.hasValue() and
(
not e instanceof PatternExpr or
e instanceof StringLiteral or
e.getValue() != "null"
) and
exists(Expr stripped | stripped = e.stripCasts() |
not stripped instanceof NullLiteral and
not stripped instanceof DefaultValueExpr
Expand All @@ -876,14 +913,30 @@ module Internal {
e instanceof AddOperation and
e.getType() instanceof StringType
or
e.(DefaultValueExpr).getType().isValueType()
e.(DefaultValueExpr).getType().isValueType() and
not e.getType() instanceof NullableType
or
e.(Call).getTarget().getUnboundDeclaration() instanceof NonNullCallable and
not e.(QualifiableExpr).isConditional()
or
e instanceof SuppressNullableWarningExpr
or
e.stripCasts().getType() = any(ValueType t | not t instanceof NullableType)
or
e =
any(PatternExpr pat |
not pat instanceof NullLiteral and
not pat instanceof DiscardPatternExpr and
not pat.(VariablePatternExpr).isImplicitlyTyped() and
not pat instanceof NotPatternExpr and
not pat instanceof OrPatternExpr and
not pat instanceof AndPatternExpr and
not pat instanceof DefaultValueExpr and
not pat.(ConstantPatternExpr).getValue() = "null"
)
or
patternMatchesNull(e) = false and
(e instanceof OrPatternExpr or e instanceof ConstantPatternExpr)
}

/** Holds if expression `e2` is a non-`null` value whenever `e1` is. */
Expand Down
Loading
Loading