Skip to content

Commit f28aefa

Browse files
committed
Ruby: Reduce string pool preasure by late-inlining locationRelativePathToString
``` [2023-06-23 13:48:23] Evaluated non-recursive predicate Sinatra#e09174a3::Sinatra::locationRelativePathToString#1#ff@683a25ce in 34682ms (size: 8048121). Evaluated relational algebra for predicate Sinatra#e09174a3::Sinatra::locationRelativePathToString#1#ff@683a25ce with tuple counts: 8048122 ~0% {6} r1 = SCAN locations_default OUTPUT In.1, In.0, toString(In.5), toString(In.2), toString(In.3), toString(In.4) 8048121 ~0% {2} r2 = JOIN r1 WITH FileSystem#df18ed9a::Make#FileSystem#e91ad87f::Input#::Container::getRelativePath#0#dispred#ff ON FIRST 1 OUTPUT Lhs.1, (Rhs.1 ++ "@" ++ Lhs.3 ++ ":" ++ Lhs.4 ++ ":" ++ Lhs.5 ++ ":" ++ Lhs.2) return r2 ```
1 parent b615e98 commit f28aefa

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

ruby/ql/lib/codeql/ruby/frameworks/Sinatra.qll

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,34 +105,45 @@ module Sinatra {
105105
* Gets the template file referred to by `erbCall`.
106106
* This works on the AST level to avoid non-monotonic reecursion in `ErbLocalsHashSyntheticGlobal`.
107107
*/
108+
pragma[nomagic]
108109
private ErbFile getTemplateFile(MethodCall erbCall) {
109110
erbCall.getMethodName() = "erb" and
110111
result.getTemplateName() = erbCall.getArgument(0).getConstantValue().getStringlikeValue() and
111112
result.getRelativePath().matches("%views/%")
112113
}
113114

115+
pragma[nomagic]
116+
private predicate erbCallAtLocation(MethodCall erbCall, ErbFile erbFile, Location l) {
117+
erbCall.getMethodName() = "erb" and
118+
erbFile = getTemplateFile(erbCall) and
119+
l = erbCall.getLocation()
120+
}
121+
114122
/**
115123
* Like `Location.toString`, but displays the relative path rather than the full path.
116124
*/
125+
bindingset[loc]
126+
pragma[inline_late]
117127
private string locationRelativePathToString(Location loc) {
118128
result =
119129
loc.getFile().getRelativePath() + "@" + loc.getStartLine() + ":" + loc.getStartColumn() + ":" +
120130
loc.getEndLine() + ":" + loc.getEndColumn()
121131
}
122132

123133
/**
124-
* A synthetic global representing the hash of local variables passed to an ERB template.
134+
* A synthetic global representing the hash of local variables passed to an ERB template.
125135
*/
126136
class ErbLocalsHashSyntheticGlobal extends SummaryComponent::SyntheticGlobal {
127137
private string id;
128138
private MethodCall erbCall;
129139
private ErbFile erbFile;
130140

131141
ErbLocalsHashSyntheticGlobal() {
132-
this = "SinatraErbLocalsHash(" + id + ")" and
133-
id = erbFile.getRelativePath() + "," + locationRelativePathToString(erbCall.getLocation()) and
134-
erbCall.getMethodName() = "erb" and
135-
erbFile = getTemplateFile(erbCall)
142+
exists(Location l |
143+
erbCallAtLocation(erbCall, erbFile, l) and
144+
id = erbFile.getRelativePath() + "," + locationRelativePathToString(l) and
145+
this = "SinatraErbLocalsHash(" + id + ")"
146+
)
136147
}
137148

138149
/**

0 commit comments

Comments
 (0)