Skip to content

Commit 2b04f1e

Browse files
committed
Unified: Add Guards instantiation.
1 parent 3aa54d6 commit 2b04f1e

3 files changed

Lines changed: 231 additions & 10 deletions

File tree

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/**
2+
* Provides classes and predicates for reasoning about guards and the control
3+
* flow elements controlled by those guards.
4+
*/
5+
overlay[local?]
6+
module;
7+
8+
private import unified
9+
private import unified as U
10+
private import codeql.controlflow.Guards as SharedGuards
11+
private import codeql.controlflow.SuccessorType
12+
private import codeql.unified.internal.typeinference.TypeInference as T
13+
private import codeql.unified.internal.typeinference.TypeMention as TM
14+
private import codeql.unified.internal.ExprPositions as EP
15+
16+
private module GuardsInput implements SharedGuards::InputSig<Location, ControlFlowNode, BasicBlock> {
17+
class NormalExitNode = ControlFlow::NormalExitNode;
18+
19+
class AstNode = U::AstNode;
20+
21+
class Expr = U::Expr;
22+
23+
predicate nodeIsAfterValue(ControlFlowNode node, Expr e, ConditionalSuccessor s) {
24+
node.isAfterValue(e, s)
25+
}
26+
27+
private newtype TConstantValue =
28+
// TODO: More cases. Chars? Enums?
29+
TStringValue(string value) { any(Expr e).getStringValue() = value }
30+
31+
class ConstantValue extends TConstantValue {
32+
string toString() { this = TStringValue(result) }
33+
}
34+
35+
abstract class ConstantExpr extends Expr {
36+
predicate isNull() { none() }
37+
38+
boolean asBooleanValue() { none() }
39+
40+
int asIntegerValue() { none() }
41+
42+
ConstantValue asConstantValue() { none() }
43+
}
44+
45+
private class NullConstant extends ConstantExpr instanceof U::BuiltinExpr {
46+
NullConstant() {
47+
// TODO: This might be fine as a general purpose condition for
48+
// identifying null literals, but if so it should moved to a more
49+
// general location
50+
this.getValue() = ["null", "nil"]
51+
}
52+
53+
override predicate isNull() { any() }
54+
}
55+
56+
private class BooleanConstant extends ConstantExpr instanceof U::BooleanLiteral {
57+
override boolean asBooleanValue() { result.toString() = super.getValue() }
58+
}
59+
60+
private class IntegerConstant extends ConstantExpr instanceof U::IntLiteral {
61+
override int asIntegerValue() { result = super.getValue().toInt() }
62+
}
63+
64+
private class StringConstant extends ConstantExpr {
65+
StringConstant() { exists(this.getStringValue()) }
66+
67+
override ConstantValue asConstantValue() { result = TStringValue(this.getStringValue()) }
68+
}
69+
70+
class NonNullExpr extends Expr {
71+
NonNullExpr() {
72+
// TODO: Placeholder implementation. There are more cases of non-null
73+
// expressions that should be handled. Also, (parts of) this is likely
74+
// best handled in language-specific plugins.
75+
exists(CallExpr call, ConstructorDeclaration constructor, ClassLikeDeclaration cls |
76+
// Explicit or inherited Swift initializers may be failable, so this just covers the generated case.
77+
call = this and
78+
constructor = unique(Callable c | c = T::resolveCallTarget(call)) and
79+
constructor.hasModifier("generated") and
80+
cls.getAMember() = constructor and
81+
cls.hasModifier("class") and
82+
not exists(cls.getABaseType())
83+
)
84+
}
85+
}
86+
87+
class Case extends AstNode instanceof U::SwitchCase {
88+
Expr getSwitchExpr() { exists(U::SwitchExpr s | s.getValue() = result and s.getACase() = this) }
89+
90+
AstNode getAPattern() { result = super.getPattern() }
91+
}
92+
93+
predicate nonDeconstructingSubPattern(AstNode pattern, AstNode subPattern) {
94+
pattern.(U::ConditionalPattern).getPattern() = subPattern or
95+
pattern.(U::ExprPattern).getExpr() = subPattern or
96+
pattern.(U::NamedPattern).getSubPattern() = subPattern or
97+
pattern.(U::OrPattern).getAPattern() = subPattern
98+
}
99+
100+
class AndExpr extends BinaryExpr {
101+
AndExpr() {
102+
this instanceof BitwiseAndExpr or
103+
this instanceof LogicalAndExpr or
104+
this instanceof AssignBitwiseAndExpr or
105+
this instanceof AssignLogicalAndExpr
106+
}
107+
}
108+
109+
class OrExpr extends BinaryExpr {
110+
OrExpr() {
111+
this instanceof BitwiseOrExpr or
112+
this instanceof LogicalOrExpr or
113+
this instanceof AssignBitwiseOrExpr or
114+
this instanceof AssignLogicalOrExpr
115+
}
116+
}
117+
118+
class NotExpr = U::LogicalNotExpr;
119+
120+
class IdExpr extends Expr {
121+
IdExpr() {
122+
this instanceof AssignExpr
123+
or
124+
this instanceof TypeCastExpr and
125+
// TODO: This is Swift-specific and needs to go through a plugin
126+
not this.(TypeCastExpr).getOperator().getValue() = "as?"
127+
}
128+
129+
Expr getEqualChildExpr() {
130+
result = this.(AssignExpr).getValue()
131+
or
132+
result = this.(TypeCastExpr).getExpr()
133+
}
134+
}
135+
136+
predicate equalityTest(Expr eqtest, Expr left, Expr right, boolean polarity) {
137+
exists(U::EqualityTest eq | eq = eqtest |
138+
eq.getLeft() = left and eq.getRight() = right and eq.getPolarity() = polarity
139+
)
140+
}
141+
142+
class ConditionalExpr = U::IfExpr;
143+
144+
class Parameter = U::Parameter;
145+
146+
import codeql.unified.internal.dataflow.ParameterPositions
147+
148+
class NonOverridableMethod extends Callable {
149+
NonOverridableMethod() {
150+
// TODO: The concept of non-overridability likely belongs in a language plugin
151+
// TODO: This logic isn't quite right, but just a placeholder for now
152+
// Swift `dynamic` functions can be replaced at runtime.
153+
not this.hasModifier("dynamic") and
154+
(
155+
this instanceof ConstructorDeclaration or
156+
this.(FunctionDeclaration).hasModifier("static") or
157+
this.(FunctionDeclaration).hasModifier("private") or
158+
this.(FunctionDeclaration).hasModifier("final") or
159+
this instanceof LocalFunctionDeclaration or
160+
// Top-level functions are classified as static members, not local functions.
161+
this.(FunctionDeclaration) instanceof TopLevelStmt
162+
)
163+
}
164+
165+
Parameter getParameter(ParameterPosition ppos) {
166+
result.getDeclaringCallable() = this and
167+
ppos = getParameterPosition(result)
168+
}
169+
170+
Expr getAReturnExpr() { EP::isReturnedExpr(this, result) }
171+
}
172+
173+
class NonOverridableMethodCall extends Expr instanceof CallExpr {
174+
NonOverridableMethodCall() {
175+
unique(Callable c | c = T::resolveCallTarget(this)) instanceof NonOverridableMethod
176+
}
177+
178+
NonOverridableMethod getMethod() { result = T::resolveCallTarget(this) }
179+
180+
Expr getArgument(ArgumentPosition apos) {
181+
exists(Argument arg |
182+
super.getAnArgument() = arg and
183+
arg.getValue() = result and
184+
apos = getArgumentPosition(arg)
185+
)
186+
}
187+
}
188+
}
189+
190+
private module GuardsImpl = SharedGuards::Make<Location, Cfg, GuardsInput>;
191+
192+
private module LogicInput_v1 implements GuardsImpl::LogicInputSig {
193+
import Ssa
194+
195+
predicate additionalNullCheck(
196+
GuardsImpl::PreGuard guard, GuardsImpl::GuardValue val, Expr e, boolean isNull
197+
) {
198+
exists(TypeTestExpr test, T::Type testedType |
199+
guard = test and
200+
e = test.getExpr() and
201+
testedType = test.getType().(TM::TypeMention).getType() and
202+
// In Swift, `nil is Any` and `nil is T?` can both be true.
203+
testedType.asClass().hasModifier(["class", "actor", "struct", "enum"]) and
204+
not testedType.asClass().getName() = "Optional" and
205+
val.asBooleanValue() = true and
206+
isNull = false
207+
)
208+
}
209+
}
210+
211+
class GuardValue = GuardsImpl::GuardValue;
212+
213+
private module Guards_v1 = GuardsImpl::Logic<LogicInput_v1>;
214+
215+
class Guard = Guards_v1::Guard;

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,8 @@ module DataFlowInput implements InputSig<Location> {
8282
|
8383
exists(Parameter param |
8484
param = p.asParameter() and
85-
callable = param.getDeclaringCallable()
86-
|
87-
pos.asPositional() = param.getPositionalIndex()
88-
or
89-
pos.asNamed() = param.getExternalName()
85+
callable = param.getDeclaringCallable() and
86+
pos = getParameterPosition(param)
9087
)
9188
or
9289
p.isImplicitReceiverParameter(callable) and
@@ -114,11 +111,8 @@ module DataFlowInput implements InputSig<Location> {
114111
private predicate isArgumentNodeImpl(Node n, DataFlowCall call, ArgumentPosition pos) {
115112
exists(Argument arg |
116113
n.asExpr() = arg.getValue() and
117-
call.getCallExpr().getAnArgument() = arg
118-
|
119-
pos.asPositional() = arg.getPositionalIndex()
120-
or
121-
pos.asNamed() = arg.getName()
114+
call.getCallExpr().getAnArgument() = arg and
115+
pos = getArgumentPosition(arg)
122116
)
123117
or
124118
n.(BuilderNode).isImplicitArgument(call, pos, false)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,15 @@ class ImplicitParameterPosition extends ParameterPosition {
5050
class ImplicitArgumentPosition = ImplicitParameterPosition;
5151

5252
predicate parameterMatch(ParameterPosition ppos, ArgumentPosition apos) { apos = ppos }
53+
54+
ParameterPosition getParameterPosition(Parameter p) {
55+
result.asPositional() = p.getPositionalIndex()
56+
or
57+
result.asNamed() = p.getExternalName()
58+
}
59+
60+
ArgumentPosition getArgumentPosition(Argument arg) {
61+
result.asPositional() = arg.getPositionalIndex()
62+
or
63+
result.asNamed() = arg.getName()
64+
}

0 commit comments

Comments
 (0)