Ruby: Cache ConstantReadAccess::getValue

This commit is contained in:
Tom Hvitved
2022-03-22 14:15:07 +01:00
parent fe50c2879e
commit 15ef8c1d8f
2 changed files with 19 additions and 15 deletions

View File

@@ -224,21 +224,7 @@ class ConstantReadAccess extends ConstantAccess {
*
* the value being read at `M::CONST` is `"const"`.
*/
Expr getValue() {
not exists(this.getScopeExpr()) and
result = lookupConst(this.getEnclosingModule+().getModule(), this.getName()) and
// For now, we restrict the scope of top-level declarations to their file.
// This may remove some plausible targets, but also removes a lot of
// implausible targets
if result.getEnclosingModule() instanceof Toplevel
then result.getFile() = this.getFile()
else any()
or
this.hasGlobalScope() and
result = lookupConst(TResolved("Object"), this.getName())
or
result = lookupConst(resolveConstantReadAccess(this.getScopeExpr()), this.getName())
}
Expr getValue() { result = getConstantReadAccessValue(this) }
final override string getAPrimaryQlClass() { result = "ConstantReadAccess" }
}

View File

@@ -1,5 +1,6 @@
private import codeql.ruby.AST
private import codeql.ruby.ast.internal.Literal
private import codeql.ruby.ast.internal.Module
private import codeql.ruby.controlflow.CfgNodes
private import codeql.ruby.dataflow.SSA
private import ExprNodes
@@ -441,6 +442,23 @@ private module Cached {
result.isNil() and
isNilExpr(e)
}
cached
Expr getConstantReadAccessValue(ConstantReadAccess read) {
not exists(read.getScopeExpr()) and
result = lookupConst(read.getEnclosingModule+().getModule(), read.getName()) and
// For now, we restrict the scope of top-level declarations to their file.
// This may remove some plausible targets, but also removes a lot of
// implausible targets
if result.getEnclosingModule() instanceof Toplevel
then result.getFile() = read.getFile()
else any()
or
read.hasGlobalScope() and
result = lookupConst(TResolved("Object"), read.getName())
or
result = lookupConst(resolveConstantReadAccess(read.getScopeExpr()), read.getName())
}
}
import Cached