|
| 1 | +/** |
| 2 | + * @name Var only used in one side of disjunct. |
| 3 | + * @description Only using a variable on one side of a disjunction can cause a cartesian product. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity warning |
| 6 | + * @id ql/var-unused-in-disjunct |
| 7 | + * @tags maintainability |
| 8 | + * performance |
| 9 | + * @precision high |
| 10 | + */ |
| 11 | + |
| 12 | +import ql |
| 13 | + |
| 14 | +/** |
| 15 | + * Holds if `node` bind `var` in a (transitive) child node. |
| 16 | + * Is a practical approximation that ignores `not` and many other features. |
| 17 | + */ |
| 18 | +pragma[noinline] |
| 19 | +predicate alwaysBindsVar(VarDef var, AstNode node) { |
| 20 | + // base case |
| 21 | + node.(VarAccess).getDeclaration() = var and |
| 22 | + not isSmallType(var.getType()) // <- early pruning |
| 23 | + or |
| 24 | + // recursive cases |
| 25 | + alwaysBindsVar(var, node.getAChild(_)) and // the recursive step, go one step up to the parent. |
| 26 | + not node.(FullAggregate).getAnArgument() = var and // except if the parent defines the variable, then we stop. |
| 27 | + not node.(Quantifier).getAnArgument() = var and |
| 28 | + not node instanceof EffectiveDisjunction // for disjunctions, we need to check both sides. |
| 29 | + or |
| 30 | + exists(EffectiveDisjunction disj | disj = node | |
| 31 | + alwaysBindsVar(var, disj.getLeft()) and |
| 32 | + alwaysBindsVar(var, disj.getRight()) |
| 33 | + ) |
| 34 | + or |
| 35 | + exists(EffectiveDisjunction disj | disj = node | |
| 36 | + alwaysBindsVar(var, disj.getAnOperand()) and |
| 37 | + disj.getAnOperand() instanceof NoneCall |
| 38 | + ) |
| 39 | + or |
| 40 | + exists(IfFormula ifForm | ifForm = node | alwaysBindsVar(var, ifForm.getCondition())) |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Holds if we assume `t` is a small type, and |
| 45 | + * variables of this type are therefore not an issue in cartesian products. |
| 46 | + */ |
| 47 | +predicate isSmallType(Type t) { |
| 48 | + t.getName() = "string" // DataFlow::Configuration and the like |
| 49 | + or |
| 50 | + exists(NewType newType | newType = t.getDeclaration() | |
| 51 | + forex(NewTypeBranch branch | branch = newType.getABranch() | branch.getArity() = 0) |
| 52 | + ) |
| 53 | + or |
| 54 | + t.getName() = "boolean" |
| 55 | + or |
| 56 | + exists(NewType newType | newType = t.getDeclaration() | |
| 57 | + forex(NewTypeBranch branch | branch = newType.getABranch() | |
| 58 | + isSmallType(branch.getReturnType()) |
| 59 | + ) |
| 60 | + ) |
| 61 | + or |
| 62 | + exists(NewTypeBranch branch | t = branch.getReturnType() | |
| 63 | + forall(Type param | param = branch.getParameterType(_) | isSmallType(param)) |
| 64 | + ) |
| 65 | + or |
| 66 | + isSmallType(t.getASuperType()) |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Holds if `pred` is inlined. |
| 71 | + */ |
| 72 | +predicate isInlined(Predicate pred) { |
| 73 | + exists(Annotation inline | |
| 74 | + inline = pred.getAnAnnotation() and |
| 75 | + inline.getName() = "pragma" and |
| 76 | + inline.getArgs(0).getValue() = "inline" |
| 77 | + ) |
| 78 | + or |
| 79 | + pred.getAnAnnotation().getName() = "bindingset" |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * An AstNode that acts like a disjunction. |
| 84 | + */ |
| 85 | +class EffectiveDisjunction extends AstNode { |
| 86 | + EffectiveDisjunction() { |
| 87 | + this instanceof IfFormula |
| 88 | + or |
| 89 | + this instanceof Disjunction |
| 90 | + } |
| 91 | + |
| 92 | + /** Gets the left operand of this disjunction. */ |
| 93 | + AstNode getLeft() { |
| 94 | + result = this.(IfFormula).getThenPart() |
| 95 | + or |
| 96 | + result = this.(Disjunction).getLeft() |
| 97 | + } |
| 98 | + |
| 99 | + /** Gets the right operand of this disjunction. */ |
| 100 | + AstNode getRight() { |
| 101 | + result = this.(IfFormula).getElsePart() |
| 102 | + or |
| 103 | + result = this.(Disjunction).getRight() |
| 104 | + } |
| 105 | + |
| 106 | + /** Gets any of the operands of this disjunction. */ |
| 107 | + AstNode getAnOperand() { result = [this.getLeft(), this.getRight()] } |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Holds if `disj` only uses `var` in one of its branches. |
| 112 | + */ |
| 113 | +pragma[noinline] |
| 114 | +predicate onlyUseInOneBranch(EffectiveDisjunction disj, VarDef var) { |
| 115 | + alwaysBindsVar(var, disj.getLeft()) and |
| 116 | + not alwaysBindsVar(var, disj.getRight()) |
| 117 | + or |
| 118 | + not alwaysBindsVar(var, disj.getLeft()) and |
| 119 | + alwaysBindsVar(var, disj.getRight()) |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Holds if `comp` is an equality comparison that has a low number of results. |
| 124 | + */ |
| 125 | +predicate isTinyAssignment(ComparisonFormula comp) { |
| 126 | + comp.getOperator() = "=" and |
| 127 | + ( |
| 128 | + isSmallType(comp.getAnOperand().getType()) |
| 129 | + or |
| 130 | + comp.getAnOperand() instanceof Literal |
| 131 | + ) |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * An AstNode that acts like a conjunction. |
| 136 | + */ |
| 137 | +class EffectiveConjunction extends AstNode { |
| 138 | + EffectiveConjunction() { |
| 139 | + this instanceof Conjunction |
| 140 | + or |
| 141 | + this instanceof Exists |
| 142 | + } |
| 143 | + |
| 144 | + /** Gets the left operand of this conjunction */ |
| 145 | + Formula getLeft() { |
| 146 | + result = this.(Conjunction).getLeft() |
| 147 | + or |
| 148 | + result = this.(Exists).getRange() |
| 149 | + } |
| 150 | + |
| 151 | + /** Gets the right operand of this conjunction */ |
| 152 | + Formula getRight() { |
| 153 | + result = this.(Conjunction).getRight() |
| 154 | + or |
| 155 | + result = this.(Exists).getFormula() |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Holds if `node` is a sub-node of a node that always binds `var`. |
| 161 | + */ |
| 162 | +predicate varIsAlwaysBound(VarDef var, AstNode node) { |
| 163 | + // base case |
| 164 | + alwaysBindsVar(var, node) and |
| 165 | + onlyUseInOneBranch(_, var) // <- manual magic |
| 166 | + or |
| 167 | + // recursive cases |
| 168 | + exists(AstNode parent | node.getParent() = parent | varIsAlwaysBound(var, parent)) |
| 169 | + or |
| 170 | + exists(EffectiveConjunction parent | |
| 171 | + varIsAlwaysBound(var, parent.getLeft()) and |
| 172 | + node = parent.getRight() |
| 173 | + or |
| 174 | + varIsAlwaysBound(var, parent.getRight()) and |
| 175 | + node = parent.getLeft() |
| 176 | + ) |
| 177 | + or |
| 178 | + exists(IfFormula ifForm | varIsAlwaysBound(var, ifForm.getCondition()) | |
| 179 | + node = [ifForm.getThenPart(), ifForm.getElsePart()] |
| 180 | + ) |
| 181 | + or |
| 182 | + exists(Forall for | varIsAlwaysBound(var, for.getRange()) | node = for.getFormula()) |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * Holds if `disj` only uses `var` in one of its branches. |
| 187 | + * And we should report it as being a bad thing. |
| 188 | + */ |
| 189 | +predicate badDisjunction(EffectiveDisjunction disj, VarDef var) { |
| 190 | + onlyUseInOneBranch(disj, var) and |
| 191 | + // it's fine if it's always bound further up |
| 192 | + not varIsAlwaysBound(var, disj) and |
| 193 | + // none() on one side makes everything fine. (this happens, it's a type-system hack) |
| 194 | + not disj.getAnOperand() instanceof NoneCall and |
| 195 | + // inlined predicates might bind unused variables in the context they are used in. |
| 196 | + not ( |
| 197 | + isInlined(disj.getEnclosingPredicate()) and |
| 198 | + var = disj.getEnclosingPredicate().getParameter(_) |
| 199 | + ) and |
| 200 | + // recursion prevention never runs, it's a compile-time check, so we remove those results here |
| 201 | + not disj.getEnclosingPredicate().getParent().(Class).getName().matches("%RecursionPrevention") and // these are by design |
| 202 | + // not a small type |
| 203 | + not isSmallType(var.getType()) and |
| 204 | + // one of the branches is a tiny assignment. These are usually intentional cartesian products (and not too big). |
| 205 | + not isTinyAssignment(disj.getAnOperand()) |
| 206 | +} |
| 207 | + |
| 208 | +from EffectiveDisjunction disj, VarDef var |
| 209 | +where |
| 210 | + badDisjunction(disj, var) and |
| 211 | + not badDisjunction(disj.getParent(), var) // avoid duplicate reporting of the same error |
| 212 | +select disj, "The variable " + var.getName() + " is only used in one side of disjunct." |
0 commit comments