Skip to content

Commit e1f2433

Browse files
committed
Ruby: make resolveConstant overlay[global]
1 parent b51940d commit e1f2433

File tree

11 files changed

+69
-5
lines changed

11 files changed

+69
-5
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ private import internal.TreeSitter
1515
*/
1616
class Expr extends Stmt, TExpr {
1717
/** Gets the constant value of this expression, if any. */
18+
overlay[global]
1819
ConstantValue getConstantValue() { result = getConstantValueExpr(this) }
1920
}
2021

@@ -428,6 +429,7 @@ class StringConcatenation extends Expr, TStringConcatenation {
428429
* "foo" "bar#{ n }"
429430
* ```
430431
*/
432+
overlay[global]
431433
final string getConcatenatedValueText() {
432434
forall(StringLiteral c | c = this.getString(_) |
433435
exists(c.getConstantValue().getStringlikeValue())

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ class HashPattern extends CasePattern, THashPattern {
206206
}
207207

208208
/** Gets the value for a given key name. */
209+
overlay[global]
209210
CasePattern getValueByKey(string key) {
210211
exists(int i |
211212
this.getKey(i).getConstantValue().isStringlikeValue(key) and result = this.getValue(i)

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
overlay[local]
2-
module;
3-
41
private import codeql.ruby.AST
52
private import codeql.ruby.ast.internal.AST
63
private import codeql.ruby.ast.internal.Literal
@@ -41,6 +38,7 @@ private import ExprNodes
4138
* constant value in some cases.
4239
*/
4340
private module Propagation {
41+
overlay[local]
4442
ExprCfgNode getSource(VariableReadAccessCfgNode read) {
4543
exists(Ssa::WriteDefinition def |
4644
def.assigns(result) and
@@ -202,6 +200,7 @@ private module Propagation {
202200
forex(ExprCfgNode n | n = e.getAControlFlowNode() | isComplex(n, real, imaginary))
203201
}
204202

203+
overlay[local]
205204
private class StringlikeLiteralWithInterpolationCfgNode extends StringlikeLiteralCfgNode {
206205
StringlikeLiteralWithInterpolationCfgNode() {
207206
this.getAComponent() =
@@ -211,6 +210,7 @@ private module Propagation {
211210
)
212211
}
213212

213+
overlay[global]
214214
pragma[nomagic]
215215
private string getComponentValue(int i) {
216216
this.getComponent(i) =
@@ -222,24 +222,28 @@ private module Propagation {
222222
}
223223

224224
language[monotonicAggregates]
225+
overlay[global]
225226
private string getValue() {
226227
result =
227228
strictconcat(int i | exists(this.getComponent(i)) | this.getComponentValue(i) order by i)
228229
}
229230

231+
overlay[global]
230232
pragma[nomagic]
231233
string getSymbolValue() {
232234
result = this.getValue() and
233235
this.getExpr() instanceof SymbolLiteral
234236
}
235237

238+
overlay[global]
236239
pragma[nomagic]
237240
string getStringValue() {
238241
result = this.getValue() and
239242
not this.getExpr() instanceof SymbolLiteral and
240243
not this.getExpr() instanceof RegExpLiteral
241244
}
242245

246+
overlay[global]
243247
pragma[nomagic]
244248
string getRegExpValue(string flags) {
245249
result = this.getValue() and
@@ -569,6 +573,7 @@ private predicate isArrayExpr(Expr e, ArrayLiteralCfgNode arr) {
569573
isArrayExpr(e.(MethodCall).getReceiver(), arr)
570574
}
571575

576+
overlay[local]
572577
private class TokenConstantAccess extends ConstantAccess, TTokenConstantAccess {
573578
private Ruby::Constant g;
574579

@@ -580,6 +585,7 @@ private class TokenConstantAccess extends ConstantAccess, TTokenConstantAccess {
580585
/**
581586
* A constant access that has a scope resolution qualifier.
582587
*/
588+
overlay[local]
583589
class ScopeResolutionConstantAccess extends ConstantAccess, TScopeResolutionConstantAccess {
584590
private Ruby::ScopeResolution g;
585591
private Ruby::Constant constant;
@@ -598,6 +604,7 @@ class ScopeResolutionConstantAccess extends ConstantAccess, TScopeResolutionCons
598604
final override predicate hasGlobalScope() { not exists(g.getScope()) }
599605
}
600606

607+
overlay[local]
601608
private class ConstantReadAccessSynth extends ConstantAccess, TConstantReadAccessSynth {
602609
private string value;
603610

@@ -612,6 +619,7 @@ private class ConstantReadAccessSynth extends ConstantAccess, TConstantReadAcces
612619
final override predicate hasGlobalScope() { value.matches("::%") }
613620
}
614621

622+
overlay[local]
615623
private class ConstantWriteAccessSynth extends ConstantAccess, TConstantWriteAccessSynth {
616624
private string value;
617625

0 commit comments

Comments
 (0)