Skip to content

Commit 21dc396

Browse files
committed
Ruby: make resolveConstant overlay[global]
1 parent 6b79e24 commit 21dc396

File tree

11 files changed

+73
-8
lines changed

11 files changed

+73
-8
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module;
44
private import codeql.ruby.AST
55
private import internal.AST
66
private import internal.Call
7+
private import internal.Literal
78
private import internal.TreeSitter
89
private import codeql.ruby.dataflow.internal.DataFlowDispatch
910
private import codeql.ruby.dataflow.internal.DataFlowImplCommon
@@ -44,7 +45,7 @@ class Call extends Expr instanceof CallImpl {
4445
final Expr getKeywordArgument(string keyword) {
4546
exists(Pair p |
4647
p = this.getAnArgument() and
47-
p.getKey().getConstantValue().isSymbol(keyword) and
48+
keyword = p.getKey().(SymbolLiteral).(StringlikeLiteralImpl).getStringValue() and
4849
result = p.getValue()
4950
)
5051
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private import internal.Variable
99
private import internal.TreeSitter
1010

1111
/** A constant value. */
12+
overlay[global]
1213
class ConstantValue extends TConstantValue {
1314
/** Gets a textual representation of this constant value. */
1415
final string toString() { this.hasValueWithType(result, _) }
@@ -137,6 +138,7 @@ class ConstantValue extends TConstantValue {
137138
}
138139

139140
/** Provides different sub classes of `ConstantValue`. */
141+
overlay[global]
140142
module ConstantValue {
141143
/** A constant integer value. */
142144
class ConstantIntegerValue extends ConstantValue, TInt { }
@@ -271,15 +273,18 @@ class ConstantReadAccess extends ConstantAccess {
271273
*
272274
* the value being read at `M::CONST` is `"const"`.
273275
*/
276+
overlay[global]
274277
Expr getValue() { result = getConstantReadAccessValue(this) }
275278

276279
/**
277280
* Gets a fully qualified name for this constant read, based on the context in
278281
* which it occurs.
279282
*/
283+
overlay[global]
280284
string getAQualifiedName() { result = resolveConstant(this) }
281285

282286
/** Gets the module that this read access resolves to, if any. */
287+
overlay[global]
283288
Module getModule() { result = resolveConstantReadAccess(this) }
284289

285290
final override string getAPrimaryQlClass() { result = "ConstantReadAccess" }
@@ -345,6 +350,7 @@ class ConstantWriteAccess extends ConstantAccess {
345350
* constants up the namespace chain, the fully qualified name of a nested
346351
* constant can be ambiguous from just statically looking at the AST.
347352
*/
353+
overlay[global]
348354
string getAQualifiedName() { result = resolveConstantWrite(this) }
349355
}
350356

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ private import codeql.ruby.CFG
66
private import internal.AST
77
private import internal.Constant
88
private import internal.Expr
9+
private import internal.Literal
910
private import internal.TreeSitter
1011

1112
/**
@@ -15,6 +16,7 @@ private import internal.TreeSitter
1516
*/
1617
class Expr extends Stmt, TExpr {
1718
/** Gets the constant value of this expression, if any. */
19+
overlay[global]
1820
ConstantValue getConstantValue() { result = getConstantValueExpr(this) }
1921
}
2022

@@ -428,13 +430,14 @@ class StringConcatenation extends Expr, TStringConcatenation {
428430
* "foo" "bar#{ n }"
429431
* ```
430432
*/
433+
overlay[global]
431434
final string getConcatenatedValueText() {
432435
forall(StringLiteral c | c = this.getString(_) |
433-
exists(c.getConstantValue().getStringlikeValue())
436+
exists(c.(StringlikeLiteralImpl).getStringValue())
434437
) and
435438
result =
436439
concat(string valueText, int i |
437-
valueText = this.getString(i).getConstantValue().getStringlikeValue()
440+
valueText = this.getString(i).(StringlikeLiteralImpl).getStringValue()
438441
|
439442
valueText order by i
440443
)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class IntegerLiteral extends NumericLiteral instanceof IntegerLiteralImpl {
4444
/** Gets the numerical value of this integer literal. */
4545
final int getValue() { result = super.getValue() }
4646

47+
overlay[global]
4748
final override ConstantValue::ConstantIntegerValue getConstantValue() {
4849
result = NumericLiteral.super.getConstantValue()
4950
}
@@ -60,6 +61,7 @@ class IntegerLiteral extends NumericLiteral instanceof IntegerLiteralImpl {
6061
* ```
6162
*/
6263
class FloatLiteral extends NumericLiteral instanceof FloatLiteralImpl {
64+
overlay[global]
6365
final override ConstantValue::ConstantFloatValue getConstantValue() {
6466
result = NumericLiteral.super.getConstantValue()
6567
}
@@ -75,6 +77,7 @@ class FloatLiteral extends NumericLiteral instanceof FloatLiteralImpl {
7577
* ```
7678
*/
7779
class RationalLiteral extends NumericLiteral instanceof RationalLiteralImpl {
80+
overlay[global]
7881
final override ConstantValue::ConstantRationalValue getConstantValue() {
7982
result = NumericLiteral.super.getConstantValue()
8083
}
@@ -90,6 +93,7 @@ class RationalLiteral extends NumericLiteral instanceof RationalLiteralImpl {
9093
* ```
9194
*/
9295
class ComplexLiteral extends NumericLiteral instanceof ComplexLiteralImpl {
96+
overlay[global]
9397
final override ConstantValue::ConstantComplexValue getConstantValue() {
9498
result = NumericLiteral.super.getConstantValue()
9599
}
@@ -99,6 +103,7 @@ class ComplexLiteral extends NumericLiteral instanceof ComplexLiteralImpl {
99103

100104
/** A `nil` literal. */
101105
class NilLiteral extends Literal instanceof NilLiteralImpl {
106+
overlay[global]
102107
final override ConstantValue::ConstantNilValue getConstantValue() { result = TNil() }
103108

104109
final override string getAPrimaryQlClass() { result = "NilLiteral" }
@@ -125,6 +130,7 @@ class BooleanLiteral extends Literal instanceof BooleanLiteralImpl {
125130
/** Gets the value of this Boolean literal. */
126131
boolean getValue() { result = super.getValue() }
127132

133+
overlay[global]
128134
final override ConstantValue::ConstantBooleanValue getConstantValue() {
129135
result = Literal.super.getConstantValue()
130136
}
@@ -136,6 +142,7 @@ class BooleanLiteral extends Literal instanceof BooleanLiteralImpl {
136142
class EncodingLiteral extends Literal instanceof EncodingLiteralImpl {
137143
final override string getAPrimaryQlClass() { result = "EncodingLiteral" }
138144

145+
overlay[global]
139146
final override ConstantValue::ConstantStringValue getConstantValue() {
140147
result = Literal.super.getConstantValue()
141148
}
@@ -147,6 +154,7 @@ class EncodingLiteral extends Literal instanceof EncodingLiteralImpl {
147154
class LineLiteral extends Literal instanceof LineLiteralImpl {
148155
final override string getAPrimaryQlClass() { result = "LineLiteral" }
149156

157+
overlay[global]
150158
final override ConstantValue::ConstantIntegerValue getConstantValue() {
151159
result = Literal.super.getConstantValue()
152160
}
@@ -158,6 +166,7 @@ class LineLiteral extends Literal instanceof LineLiteralImpl {
158166
class FileLiteral extends Literal instanceof FileLiteralImpl {
159167
final override string getAPrimaryQlClass() { result = "FileLiteral" }
160168

169+
overlay[global]
161170
final override ConstantValue::ConstantStringValue getConstantValue() {
162171
result = Literal.super.getConstantValue()
163172
}
@@ -169,6 +178,7 @@ class FileLiteral extends Literal instanceof FileLiteralImpl {
169178
*/
170179
class StringComponent extends AstNode instanceof StringComponentImpl {
171180
/** Gets the constant value of this string component, if any. */
181+
overlay[global]
172182
ConstantValue::ConstantStringValue getConstantValue() { result = TString(super.getValue()) }
173183
}
174184

@@ -213,6 +223,7 @@ class StringInterpolationComponent extends StringComponent, StmtSequence instanc
213223

214224
final override Stmt getStmt(int n) { toGenerated(result) = g.getChild(n) }
215225

226+
overlay[global]
216227
final override ConstantValue::ConstantStringValue getConstantValue() {
217228
result = StmtSequence.super.getConstantValue()
218229
}
@@ -260,6 +271,7 @@ class RegExpInterpolationComponent extends RegExpComponent, StmtSequence instanc
260271

261272
final override Stmt getStmt(int n) { toGenerated(result) = g.getChild(n) }
262273

274+
overlay[global]
263275
final override ConstantValue::ConstantStringValue getConstantValue() {
264276
result = StmtSequence.super.getConstantValue()
265277
}
@@ -408,6 +420,7 @@ class SymbolLiteral extends StringlikeLiteral instanceof SymbolLiteralImpl {
408420
not this instanceof MethodName and result = "SymbolLiteral"
409421
}
410422

423+
overlay[global]
411424
final override ConstantValue::ConstantSymbolValue getConstantValue() {
412425
result = StringlikeLiteral.super.getConstantValue()
413426
}
@@ -440,6 +453,7 @@ class SubshellLiteral extends StringlikeLiteral instanceof SubshellLiteralImpl {
440453
class CharacterLiteral extends Literal instanceof CharacterLiteralImpl {
441454
final override string getAPrimaryQlClass() { result = "CharacterLiteral" }
442455

456+
overlay[global]
443457
final override ConstantValue::ConstantStringValue getConstantValue() {
444458
result = Literal.super.getConstantValue()
445459
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,22 @@ class MethodBase extends Callable, BodyStmt, Scope, TMethodBase {
4343
* Holds if this method is public.
4444
* Methods are public by default.
4545
*/
46+
overlay[global]
4647
predicate isPublic() { this.getVisibility() = "public" }
4748

4849
/** Holds if this method is private. */
50+
overlay[global]
4951
predicate isPrivate() { this.getVisibility() = "private" }
5052

5153
/** Holds if this method is protected. */
54+
overlay[global]
5255
predicate isProtected() { this.getVisibility() = "protected" }
5356

5457
/**
5558
* Gets a string describing the visibility of this method.
5659
* This is either 'public', 'private' or 'protected'.
5760
*/
61+
overlay[global]
5862
string getVisibility() {
5963
result = getVisibilityModifier(this).getVisibility()
6064
or
@@ -76,6 +80,7 @@ class MethodBase extends Callable, BodyStmt, Scope, TMethodBase {
7680
* end
7781
* ```
7882
*/
83+
overlay[global]
7984
private VisibilityModifier getExplicitVisibilityModifier(Method m) {
8085
result.getMethodArgument() = m
8186
or
@@ -89,6 +94,7 @@ private VisibilityModifier getExplicitVisibilityModifier(Method m) {
8994
* Gets the visibility modifier that defines the visibility of method `m`, if
9095
* any.
9196
*/
97+
overlay[global]
9298
private VisibilityModifier getVisibilityModifier(MethodBase mb) {
9399
mb =
94100
any(Method m |
@@ -205,6 +211,7 @@ class Method extends MethodBase, TMethod {
205211
* end
206212
* ```
207213
*/
214+
overlay[global]
208215
override predicate isPrivate() { super.isPrivate() }
209216

210217
final override Parameter getParameter(int n) {
@@ -213,6 +220,7 @@ class Method extends MethodBase, TMethod {
213220

214221
final override string toString() { result = this.getName() }
215222

223+
overlay[global]
216224
override string getVisibility() {
217225
result = getVisibilityModifier(this).getVisibility()
218226
or
@@ -226,6 +234,7 @@ class Method extends MethodBase, TMethod {
226234
}
227235
}
228236

237+
overlay[global]
229238
pragma[nomagic]
230239
private predicate modifiesIn(VisibilityModifier vm, ModuleBase n, string name) {
231240
n = vm.getEnclosingModule() and
@@ -302,6 +311,7 @@ class SingletonMethod extends MethodBase, TSingletonMethod {
302311
* end
303312
* ```
304313
*/
314+
overlay[global]
305315
override predicate isPrivate() { super.isPrivate() }
306316
}
307317

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private import internal.Scope
1111
/**
1212
* A representation of a run-time `module` or `class` value.
1313
*/
14+
overlay[global]
1415
class Module extends TModule {
1516
/** Gets a declaration of this module, if any. */
1617
ModuleBase getADeclaration() { result.getModule() = this }
@@ -258,6 +259,7 @@ class ModuleBase extends BodyStmt, Scope, TModuleBase {
258259
}
259260

260261
/** Gets the representation of the run-time value of this module or class. */
262+
overlay[global]
261263
Module getModule() { none() }
262264

263265
/**
@@ -336,6 +338,7 @@ class Toplevel extends ModuleBase, TToplevel {
336338
pred = "getBeginBlock" and result = this.getBeginBlock(_)
337339
}
338340

341+
overlay[global]
339342
final override Module getModule() { result = TResolved("Object") }
340343

341344
final override string toString() { result = g.getLocation().getFile().getBaseName() }
@@ -408,6 +411,7 @@ class Namespace extends ModuleBase, ConstantWriteAccess, TNamespace {
408411
*/
409412
override predicate hasGlobalScope() { none() }
410413

414+
overlay[global]
411415
final override Module getModule() {
412416
result = any(string qName | qName = namespaceDeclaration(this) | TResolved(qName))
413417
or

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module;
33

44
private import codeql.ruby.AST
55
private import internal.AST
6+
private import internal.Literal
67
private import internal.Pattern
78
private import internal.TreeSitter
89
private import internal.Variable
@@ -208,7 +209,7 @@ class HashPattern extends CasePattern, THashPattern {
208209
/** Gets the value for a given key name. */
209210
CasePattern getValueByKey(string key) {
210211
exists(int i |
211-
this.getKey(i).getConstantValue().isStringlikeValue(key) and result = this.getValue(i)
212+
key = this.getKey(i).(StringlikeLiteralImpl).getStringValue() and result = this.getValue(i)
212213
)
213214
}
214215

0 commit comments

Comments
 (0)