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
28 changes: 28 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,34 @@
)
}

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

Pattern getASubPattern() { none() }

Check warning on line 197 in cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll

View workflow job for this annotation

GitHub Actions / qldoc

Missing QLdoc for member-predicate IRGuards::GuardsInput::Pattern::getASubPattern/0
}

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,19 @@ 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
or
pattern = any(RecursivePatternExpr rpe).getVariableDeclExpr()
or
// `var x` always matches, also when the value is `null`
pattern.(VariablePatternExpr).isImplicitlyTyped()
}
Comment thread
hvitved marked this conversation as resolved.
Resolved

private newtype TLabel =
TLblGoto(string label) { any(GotoLabelStmt goto).getLabel() = label } or
TLblSwitchCase(string value) { any(GotoCaseStmt goto).getLabel() = value } or
Expand Down
119 changes: 48 additions & 71 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 @@ -884,6 +851,16 @@ module Internal {
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
)
}

/** Holds if expression `e2` is a non-`null` value whenever `e1` is. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,24 @@ module Ast implements AstSig<Location> {
private AstNode getExprChild0(Expr e, int i) {
not e instanceof NameOfExpr and
not e instanceof AnonymousFunctionExpr and
not e instanceof RecursivePatternExpr and
not skipControlFlow(result) and
result = e.getChild(i)
or
e =
any(RecursivePatternExpr rpe |
i = 0 and
result = rpe.getTypeAccess()
or
i = 1 and
result = rpe.getPositionalPatterns()
or
i = 2 and
result = rpe.getPropertyPatterns()
or
i = 3 and
result = rpe.getVariableDeclExpr()
)
}

private AstNode getStmtChild0(Stmt s, int i) {
Expand Down Expand Up @@ -295,6 +311,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
10 changes: 5 additions & 5 deletions csharp/ql/lib/semmle/code/csharp/dataflow/ModulusAnalysis.qll
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private predicate valueFlowStepSsa(SsaVariable v, SsaReadPosition pos, Expr e, i
exists(Guard guard, boolean testIsTrue |
pos.hasReadOfVar(v) and
guard = eqFlowCond(v, e, delta, true, testIsTrue) and
guardControlsSsaRead(guard, pos, testIsTrue)
guardControlsSsaRead(guard, pos, any(GuardValue gv | gv.asBooleanValue() = testIsTrue))
)
}

Expand Down Expand Up @@ -69,14 +69,14 @@ private Expr modExpr(Expr arg, int mod) {
* its `testIsTrue` branch.
*/
private Guard moduloCheck(SsaVariable v, int val, int mod, boolean testIsTrue) {
exists(Expr rem, ConstantIntegerExpr c, int r, boolean polarity |
exists(Expr rem, ConstantIntegerExpr c, int r, GuardValue polarity |
result.isEquality(rem, c, polarity) and
c.getIntValue() = r and
rem = modExpr(v.getAUse(), mod) and
(
testIsTrue = polarity and val = r
testIsTrue = polarity.asBooleanValue() and val = r
or
testIsTrue = polarity.booleanNot() and
testIsTrue = polarity.asBooleanValue().booleanNot() and
Comment thread
hvitved marked this conversation as resolved.
Resolved
mod = 2 and
val = 1 - r and
(r = 0 or r = 1)
Expand All @@ -91,7 +91,7 @@ private predicate moduloGuardedRead(SsaVariable v, SsaReadPosition pos, int val,
exists(Guard guard, boolean testIsTrue |
pos.hasReadOfVar(v) and
guard = moduloCheck(v, val, mod, testIsTrue) and
guardControlsSsaRead(guard, pos, testIsTrue)
guardControlsSsaRead(guard, pos, any(GuardValue gv | gv.asBooleanValue() = testIsTrue))
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Private {

class Guard = RU::Guard;

class GuardValue = RU::GuardValue;

class ConstantIntegerExpr = CU::ConstantIntegerExpr;

class ConditionalExpr = RU::ExprNode::ConditionalExpr;
Expand Down
Loading
Loading