|
5 | 5 | private import unified |
6 | 6 | private import AllDataFlow |
7 | 7 | private import codeql.util.Unit |
| 8 | +private import codeql.unified.internal.ExprPositions |
| 9 | +private import codeql.unified.internal.typeinference.TypeInference as T |
8 | 10 |
|
9 | 11 | private module Plugins { |
10 | 12 | private import DataFlowPluginSwift |
@@ -34,7 +36,37 @@ class DataFlowPlugin extends Unit { |
34 | 36 | * `expr` is an expression in getter position, such as a member access. |
35 | 37 | */ |
36 | 38 | bindingset[expr] |
37 | | - predicate mayWriteBackViaSetterCall(Expr expr) { none() } |
| 39 | + predicate mayWriteBackViaSetterCall(Expr expr) { |
| 40 | + isInWriteBackPosition(expr) |
| 41 | + // TODO: Check that 'expr' has a value type. Reference types don't cause writebacks. |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Holds if `expr` is target of an assignment or 'inout' argument, |
| 47 | + * or the base of such an expression, e.g. `x` in `x.f1.f2 = foo()` |
| 48 | + */ |
| 49 | +private predicate isInWriteBackPosition(Expr expr) { |
| 50 | + isInAssignmentContext(expr, _) |
| 51 | + or |
| 52 | + // Arguments to an 'inout' parameter must be prefixed with '&' |
| 53 | + exists(UnaryExpr unary | |
| 54 | + unary.getOperator().(PrefixOperator).getValue() = "&" and |
| 55 | + unary.getOperand() = expr |
| 56 | + // TODO: support custom operators with an 'inout' parameter, which lack the '&' indicator |
| 57 | + ) |
| 58 | + or |
| 59 | + // The receiver of a call to a 'mutating' function counts as an 'inout' argument |
| 60 | + exists(CallExpr call, Callable target | |
| 61 | + target = T::resolveCallTarget(call) and |
| 62 | + target.hasModifier("mutating") and |
| 63 | + expr = call.getCallee().(MemberAccessExpr).getBase() |
| 64 | + ) |
| 65 | + or |
| 66 | + exists(MemberAccessExpr member | |
| 67 | + isInWriteBackPosition(member) and |
| 68 | + expr = member.getBase() |
| 69 | + ) |
38 | 70 | } |
39 | 71 |
|
40 | 72 | /** |
|
0 commit comments