Skip to content

Commit 446eb13

Browse files
hvitvedhmac
authored andcommitted
Minor adjustments to SSA library for self variables
1 parent 0d39a15 commit 446eb13

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

ruby/ql/lib/codeql/ruby/ast/Variable.qll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ class ClassVariable extends Variable instanceof ClassVariableImpl {
7171
final override ClassVariableAccess getAnAccess() { result.getVariable() = this }
7272
}
7373

74-
/** The `self` variable */
75-
class SelfVariable extends Variable instanceof SelfVariableImpl { }
74+
/** A `self` variable. */
75+
class SelfVariable extends LocalVariable instanceof SelfVariableImpl {
76+
/** Gets the method that this `self` variable belongs to. */
77+
MethodBase getMethod() { result = this.getDeclaringScope() }
78+
}
7679

7780
/** An access to a variable. */
7881
class VariableAccess extends Expr instanceof VariableAccessImpl {

ruby/ql/lib/codeql/ruby/dataflow/SSA.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,19 @@ module Ssa {
220220
final override Location getLocation() { result = this.getControlFlowNode().getLocation() }
221221
}
222222

223+
/**
224+
* An SSA definition that corresponds to the value of `self` upon method entry.
225+
*/
226+
class SelfDefinition extends Definition, SsaImplCommon::WriteDefinition {
227+
private SelfVariable v;
228+
229+
SelfDefinition() { this.definesAt(v, _, _) }
230+
231+
final override string toString() { result = "self (" + v.getMethod() + ")" }
232+
233+
final override Location getLocation() { result = this.getControlFlowNode().getLocation() }
234+
}
235+
223236
/**
224237
* An SSA definition inserted at the beginning of a scope to represent an
225238
* uninitialized local variable. For example, in

ruby/ql/lib/codeql/ruby/dataflow/internal/SsaImpl.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ private import CfgNodes::ExprNodes
77
/** Holds if `v` is uninitialized at index `i` in entry block `bb`. */
88
predicate uninitializedWrite(EntryBasicBlock bb, int i, LocalVariable v) {
99
v.getDeclaringScope() = bb.getScope() and
10-
i = -1
10+
i = -1 and
11+
not v instanceof SelfVariable
1112
}
1213

1314
/** Holds if `bb` contains a caputured read of variable `v`. */

ruby/ql/lib/codeql/ruby/dataflow/internal/SsaImplSpecific.qll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ class SourceVariable = LocalVariable;
2424
predicate variableWrite(BasicBlock bb, int i, SourceVariable v, boolean certain) {
2525
(
2626
// We consider the `self` variable to have a single write at the entry to a method block.
27-
exists(SelfVariableAccess access |
28-
access.getCfgScope() = bb.getScope() and
29-
access.getVariable() = v and
30-
i = 0
31-
)
27+
v.(SelfVariable).getDeclaringScope() = bb.(BasicBlocks::EntryBasicBlock).getScope() and
28+
i = 0
3229
or
3330
SsaImpl::uninitializedWrite(bb, i, v)
3431
or

0 commit comments

Comments
 (0)