|
| 1 | +/** |
| 2 | + * SSA for captured variables. |
| 3 | + */ |
| 4 | + |
| 5 | +private import unified |
| 6 | +private import unified as U |
| 7 | +private import AllDataFlow |
| 8 | +private import codeql.dataflow.VariableCapture |
| 9 | +private import codeql.unified.internal.ControlFlowGraph |
| 10 | +private import codeql.unified.internal.ExprPositions |
| 11 | + |
| 12 | +module CaptureSsaInput implements InputSig<Location, BasicBlock> { |
| 13 | + final private class FinalCallable = U::Callable; |
| 14 | + |
| 15 | + class Callable extends FinalCallable { |
| 16 | + Callable() { this.fromSource() } |
| 17 | + |
| 18 | + predicate isConstructor() { none() } // TODO |
| 19 | + } |
| 20 | + |
| 21 | + Callable basicBlockGetEnclosingCallable(BasicBlock bb) { result = bb.getEnclosingCallable() } |
| 22 | + |
| 23 | + class CapturedVariable extends LocalVariable { |
| 24 | + CapturedVariable() { this.isCaptured() } |
| 25 | + |
| 26 | + Callable getCallable() { result = super.getDeclaringCallable() } |
| 27 | + } |
| 28 | + |
| 29 | + class CapturedParameter extends CapturedVariable { |
| 30 | + // This class should be empty, as the capture library never gets to work with a node that corresponds to a `DataFlowInput::ParameterNode`. |
| 31 | + // |
| 32 | + // When a variable is mentioned in a pattern in the parameter list, the pattern itself (TValueNode) becomes the ParameterNode |
| 33 | + // and the assignment to the variable is modeled as an ordinary variable-assignment at the point where the variable mention appears in the CFG. |
| 34 | + // Likewise, the receiver is represented by TImplicitParameterNode and its assignment to 'self/this' is modeled as an ordinary assignment. |
| 35 | + CapturedParameter() { none() } |
| 36 | + } |
| 37 | + |
| 38 | + private class TExpr = TCallableNode or TLocalVariableRefNode; |
| 39 | + |
| 40 | + private predicate isTopLevelCallable(BuilderNode callable) { |
| 41 | + callable.isCallable(any(TopLevel t)) |
| 42 | + } |
| 43 | + |
| 44 | + class Expr extends TExpr { |
| 45 | + Expr() { not isTopLevelCallable(this) } |
| 46 | + |
| 47 | + string toString() { result = this.(BuilderNode).toString() } |
| 48 | + |
| 49 | + Location getLocation() { result = this.(BuilderNode).getLocation() } |
| 50 | + |
| 51 | + predicate hasCfgNode(BasicBlock bb, int i) { |
| 52 | + this.(LocalVariableRefNode).hasCfgNode(bb, i) |
| 53 | + or |
| 54 | + exists(DataFlowCallable callable | |
| 55 | + this = TCallableNode(callable) and |
| 56 | + bb.getNode(i).injects(callable.asSourceCallable()) |
| 57 | + ) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + final private class FinalLocalVariableRefNode = LocalVariableRefNode; |
| 62 | + |
| 63 | + class VariableWrite extends FinalLocalVariableRefNode { |
| 64 | + VariableWrite() { |
| 65 | + this.getRefKind().isWrite() and super.getVariable() instanceof CapturedVariable |
| 66 | + } |
| 67 | + |
| 68 | + CapturedVariable getVariable() { result = super.getVariable() } |
| 69 | + } |
| 70 | + |
| 71 | + class VariableRead extends Expr instanceof LocalVariableRefNode { |
| 72 | + VariableRead() { |
| 73 | + super.getVariable() instanceof CapturedVariable and |
| 74 | + super.getRefKind().isRead() |
| 75 | + } |
| 76 | + |
| 77 | + CapturedVariable getVariable() { result = super.getVariable() } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Holds if `node` is a possible alias for `callable`. |
| 82 | + */ |
| 83 | + private predicate callableHasLocalAlias(Callable callable, Stage2Node node) { |
| 84 | + node.(BuilderNode).isCallable(callable) |
| 85 | + or |
| 86 | + exists(Stage2Node prev | callableHasLocalAlias(callable, prev) | |
| 87 | + step(prev, any(Step s | s.value()), node) |
| 88 | + or |
| 89 | + localSsaStep(prev, node, _) |
| 90 | + ) |
| 91 | + or |
| 92 | + exists(CapturedVariable var | |
| 93 | + callableHasLocalAliasVar(callable, var) and |
| 94 | + node.(BuilderNode).isLocalVariableRead(_, var) |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + pragma[nomagic] |
| 99 | + private predicate callableHasLocalAliasVar(Callable callable, CapturedVariable var) { |
| 100 | + exists(BuilderNode ref | |
| 101 | + callableHasLocalAlias(callable, ref) and |
| 102 | + ref.isLocalVariableWrite(_, var) |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + class ClosureExpr extends Expr instanceof TCallableNode { |
| 107 | + // TODO: In order to support classes with captures, this needs to cover constructor calls to such classes as well. |
| 108 | + private Callable callable; |
| 109 | + |
| 110 | + ClosureExpr() { this.(BuilderNode).isCallable(callable) } |
| 111 | + |
| 112 | + predicate hasBody(Callable body) { callable = body } |
| 113 | + |
| 114 | + predicate hasAliasedAccess(Expr f) { |
| 115 | + // TODO: Restrict to places where the function is called or escapes into a call where it might get called. |
| 116 | + callableHasLocalAlias(callable, f) and not f.(LocalVariableRefNode).getRefKind().isWrite() |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +module CaptureSsaOutput = Flow<Location, Cfg, CaptureSsaInput>; |
| 122 | + |
| 123 | +Node getNodeFromCaptureSsaNode(CaptureSsaOutput::ClosureNode n) { |
| 124 | + result = TCaptureSsaNode(n) |
| 125 | + or |
| 126 | + result = n.(CaptureSsaOutput::ExprNode).getExpr() |
| 127 | + or |
| 128 | + result = n.(CaptureSsaOutput::ExprPostUpdateNode).getExpr().(BuilderNode).getPostUpdateNode() |
| 129 | + or |
| 130 | + result = n.(CaptureSsaOutput::VariableWriteSourceNode).getVariableWrite() |
| 131 | + or |
| 132 | + // NOTE: This only supports lambdas at the moment. Local classes in Swift cannot capture variables. |
| 133 | + result = n.(CaptureSsaOutput::MallocNode).getClosureExpr() |
| 134 | + or |
| 135 | + exists(CaptureSsaOutput::ThisParameterNode thisParam, Callable callable | |
| 136 | + n = thisParam and |
| 137 | + callable = thisParam.getCallable() and |
| 138 | + result |
| 139 | + .(BuilderNode) |
| 140 | + .isImplicitParameter(toDataFlowCallable(callable), any(ParameterPosition p | p.isCallee())) |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +CaptureSsaOutput::ClosureNode getCaptureSsaNodeFromNode(Node n) { |
| 145 | + n = getNodeFromCaptureSsaNode(result) |
| 146 | +} |
| 147 | + |
| 148 | +predicate captureSsaLocalFlowStep(Node node1, Node node2) { |
| 149 | + CaptureSsaOutput::localFlowStep(getCaptureSsaNodeFromNode(node1), getCaptureSsaNodeFromNode(node2)) |
| 150 | +} |
| 151 | + |
| 152 | +predicate captureSsaStoreStep(Node node1, ContentSet contents, Node node2) { |
| 153 | + CaptureSsaOutput::storeStep(getCaptureSsaNodeFromNode(node1), |
| 154 | + contents.asSingleton().asCapturedVariable(), getCaptureSsaNodeFromNode(node2)) |
| 155 | +} |
| 156 | + |
| 157 | +predicate captureSsaReadStep(Node node1, ContentSet contents, Node node2) { |
| 158 | + CaptureSsaOutput::readStep(getCaptureSsaNodeFromNode(node1), |
| 159 | + contents.asSingleton().asCapturedVariable(), getCaptureSsaNodeFromNode(node2)) |
| 160 | +} |
| 161 | + |
| 162 | +predicate captureSsaClearsContent(Node node, ContentSet contents) { |
| 163 | + CaptureSsaOutput::clearsContent(getCaptureSsaNodeFromNode(node), |
| 164 | + contents.asSingleton().asCapturedVariable()) |
| 165 | +} |
| 166 | + |
| 167 | +Node getCaptureSsaPostUpdate(Node pre) { |
| 168 | + CaptureSsaOutput::capturePostUpdateNode(getCaptureSsaNodeFromNode(result), |
| 169 | + getCaptureSsaNodeFromNode(pre)) |
| 170 | +} |
| 171 | + |
| 172 | +predicate captureSsaAllowParameterReturnInSelf(Node param) { |
| 173 | + exists(Callable callable | |
| 174 | + CaptureSsaOutput::heuristicAllowInstanceParameterReturnInSelf(callable) and |
| 175 | + param = |
| 176 | + getNodeFromCaptureSsaNode(any(CaptureSsaOutput::ThisParameterNode n | |
| 177 | + n.getCallable() = callable |
| 178 | + )) |
| 179 | + ) |
| 180 | +} |
0 commit comments