|
16 | 16 | import csharp |
17 | 17 | import semmle.code.csharp.commons.Assertions |
18 | 18 | import semmle.code.csharp.commons.Constants |
| 19 | +import semmle.code.csharp.controlflow.BasicBlocks |
| 20 | +import semmle.code.csharp.controlflow.Guards as Guards |
| 21 | +import codeql.controlflow.queries.ConstantCondition as ConstCond |
| 22 | + |
| 23 | +module ConstCondInput implements ConstCond::InputSig<ControlFlow::BasicBlock> { |
| 24 | + class SsaDefinition = Ssa::Definition; |
| 25 | + |
| 26 | + class GuardValue = Guards::GuardValue; |
| 27 | + |
| 28 | + class Guard = Guards::Guards::Guard; |
| 29 | + |
| 30 | + predicate ssaControlsBranchEdge(SsaDefinition def, BasicBlock bb1, BasicBlock bb2, GuardValue v) { |
| 31 | + Guards::Guards::ssaControlsBranchEdge(def, bb1, bb2, v) |
| 32 | + } |
| 33 | + |
| 34 | + import Guards::Guards::InternalUtil |
| 35 | +} |
| 36 | + |
| 37 | +module ConstCondImpl = ConstCond::Make<Location, Cfg, ConstCondInput>; |
| 38 | + |
| 39 | +predicate nullCheck(Expr e, boolean direct) { |
| 40 | + exists(QualifiableExpr qe | qe.isConditional() and qe.getQualifier() = e and direct = true) |
| 41 | + or |
| 42 | + exists(NullCoalescingExpr nce | nce.getLeftOperand() = e and direct = true) |
| 43 | + or |
| 44 | + exists(ConditionalExpr ce | ce.getThen() = e or ce.getElse() = e | |
| 45 | + nullCheck(ce, _) and direct = false |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +predicate constantGuard( |
| 50 | + Guards::Guards::Guard g, string msg, Guards::Guards::Guard reason, string reasonMsg |
| 51 | +) { |
| 52 | + ConstCondImpl::problems(g, msg, reason, reasonMsg) and |
| 53 | + // conditional qualified expressions sit at an akward place in the CFG, which |
| 54 | + // leads to FPs |
| 55 | + not g.(QualifiableExpr).getQualifier() = reason and |
| 56 | + // if a logical connective is constant, one of its operands is constant, so |
| 57 | + // we report that instead |
| 58 | + not g instanceof LogicalNotExpr and |
| 59 | + not g instanceof LogicalAndExpr and |
| 60 | + not g instanceof LogicalOrExpr and |
| 61 | + // if a logical connective is a reason for another condition to be constant, |
| 62 | + // then one of its operands is a more precise reason |
| 63 | + not reason instanceof LogicalNotExpr and |
| 64 | + not reason instanceof LogicalAndExpr and |
| 65 | + not reason instanceof LogicalOrExpr and |
| 66 | + // don't report double-checked locking |
| 67 | + not exists(LockStmt ls, BasicBlock bb | |
| 68 | + bb = ls.getBasicBlock() and |
| 69 | + reason.getBasicBlock().strictlyDominates(bb) and |
| 70 | + bb.dominates(g.getBasicBlock()) |
| 71 | + ) and |
| 72 | + // exclude indirect null checks like `x` in `(b ? x : null)?.Foo()` |
| 73 | + not nullCheck(g, false) |
| 74 | +} |
19 | 75 |
|
20 | 76 | /** A constant condition. */ |
21 | | -abstract class ConstantCondition extends Expr { |
| 77 | +abstract class ConstantCondition extends Guards::Guards::Guard { |
22 | 78 | /** Gets the alert message for this constant condition. */ |
23 | 79 | abstract string getMessage(); |
24 | 80 |
|
| 81 | + predicate hasReason(Guards::Guards::Guard reason, string reasonMsg) { |
| 82 | + // dummy value, overridden when message has a placeholder |
| 83 | + reason = this and reasonMsg = "dummy" |
| 84 | + } |
| 85 | + |
25 | 86 | /** Holds if this constant condition is white-listed. */ |
26 | 87 | predicate isWhiteListed() { none() } |
27 | 88 | } |
28 | 89 |
|
| 90 | +/** A constant guard. */ |
| 91 | +class ConstantGuard extends ConstantCondition { |
| 92 | + ConstantGuard() { constantGuard(this, _, _, _) } |
| 93 | + |
| 94 | + override string getMessage() { constantGuard(this, result, _, _) } |
| 95 | + |
| 96 | + override predicate hasReason(Guards::Guards::Guard reason, string reasonMsg) { |
| 97 | + constantGuard(this, _, reason, reasonMsg) |
| 98 | + } |
| 99 | +} |
| 100 | + |
29 | 101 | /** A constant Boolean condition. */ |
30 | 102 | class ConstantBooleanCondition extends ConstantCondition { |
31 | 103 | boolean b; |
@@ -111,6 +183,7 @@ class ConstantMatchingCondition extends ConstantCondition { |
111 | 183 | boolean b; |
112 | 184 |
|
113 | 185 | ConstantMatchingCondition() { |
| 186 | + this instanceof Expr and |
114 | 187 | forex(ControlFlow::Node cfn | cfn = this.getAControlFlowNode() | |
115 | 188 | exists(ControlFlow::MatchingSuccessor t | exists(cfn.getASuccessorByType(t)) | |
116 | 189 | b = t.getValue() |
@@ -138,9 +211,10 @@ class ConstantMatchingCondition extends ConstantCondition { |
138 | 211 | } |
139 | 212 | } |
140 | 213 |
|
141 | | -from ConstantCondition c, string msg |
| 214 | +from ConstantCondition c, string msg, Guards::Guards::Guard reason, string reasonMsg |
142 | 215 | where |
143 | 216 | msg = c.getMessage() and |
| 217 | + c.hasReason(reason, reasonMsg) and |
144 | 218 | not c.isWhiteListed() and |
145 | 219 | not isExprInAssertion(c) |
146 | | -select c, msg |
| 220 | +select c, msg, reason, reasonMsg |
0 commit comments