Skip to content

Commit 0d1b4b9

Browse files
committed
unified: Be more precise about writebacks
1 parent 3769c46 commit 0d1b4b9

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

‎unified/ql/lib/codeql/unified/internal/dataflow/DataFlowPlugin.qll‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
private import unified
66
private import AllDataFlow
77
private import codeql.util.Unit
8+
private import codeql.unified.internal.ExprPositions
9+
private import codeql.unified.internal.typeinference.TypeInference as T
810

911
private module Plugins {
1012
private import DataFlowPluginSwift
@@ -34,7 +36,37 @@ class DataFlowPlugin extends Unit {
3436
* `expr` is an expression in getter position, such as a member access.
3537
*/
3638
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+
)
3870
}
3971

4072
/**

0 commit comments

Comments
 (0)