Skip to content

Commit 2e2b4ed

Browse files
committed
[GR-20371] Do not save image generator paths in the image heap.
PullRequest: truffleruby/1222
2 parents dcc6c5e + 87c650d commit 2e2b4ed

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/main/java/org/truffleruby/RubyContext.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public class RubyContext {
9494
@CompilationFinal private Options options;
9595
@CompilationFinal private String rubyHome;
9696
@CompilationFinal private TruffleFile rubyHomeTruffleFile;
97+
@CompilationFinal private boolean hadHome;
9798

9899
private final PrimitiveManager primitiveManager = new PrimitiveManager();
99100
private final SafepointManager safepointManager = new SafepointManager(this);
@@ -122,7 +123,7 @@ public class RubyContext {
122123

123124
@CompilationFinal private SecureRandom random;
124125
private final Hashing hashing;
125-
@CompilationFinal BacktraceFormatter defaultBacktraceFormatter;
126+
@CompilationFinal private BacktraceFormatter defaultBacktraceFormatter;
126127
private final BacktraceFormatter userBacktraceFormatter;
127128
private final RopeCache ropeCache;
128129
private final PathToRopeCache pathToRopeCache = new PathToRopeCache(this);
@@ -258,6 +259,11 @@ public void initialize() {
258259
random = null;
259260
// Cannot save the root Java Thread instance in the image
260261
threadManager.resetMainThread();
262+
// Do not save image generator paths in the image heap
263+
hadHome = rubyHome != null;
264+
rubyHome = null;
265+
rubyHomeTruffleFile = null;
266+
featureLoader.setWorkingDirectory(null);
261267
} else {
262268
initialized = true;
263269
}
@@ -275,9 +281,8 @@ protected boolean patch(Env newEnv) {
275281

276282
final Options oldOptions = this.options;
277283
final Options newOptions = createOptions(newEnv);
278-
final String oldHome = this.rubyHome;
279284
final String newHome = findRubyHome(newOptions);
280-
if (!compatibleOptions(oldOptions, newOptions, oldHome, newHome)) {
285+
if (!compatibleOptions(oldOptions, newOptions, this.hadHome, newHome != null)) {
281286
return false;
282287
}
283288
this.options = newOptions;
@@ -337,7 +342,7 @@ protected boolean patchContext(Env newEnv) {
337342
}
338343
}
339344

340-
private boolean compatibleOptions(Options oldOptions, Options newOptions, String oldHome, String newHome) {
345+
private boolean compatibleOptions(Options oldOptions, Options newOptions, boolean hadHome, boolean hasHome) {
341346
final String notReusingContext = "not reusing pre-initialized context: ";
342347

343348
if (!newOptions.PREINITIALIZATION) {
@@ -350,9 +355,8 @@ private boolean compatibleOptions(Options oldOptions, Options newOptions, String
350355
return false; // Should load the specified core files
351356
}
352357

353-
if ((oldHome != null) != (newHome != null)) {
354-
RubyLanguage.LOGGER
355-
.fine(notReusingContext + "Ruby home is " + (newHome != null ? "set (" + newHome + ")" : "unset"));
358+
if (hadHome != hasHome) {
359+
RubyLanguage.LOGGER.fine(notReusingContext + "Ruby home is " + (hasHome ? "set" : "unset"));
356360
return false;
357361
}
358362

src/main/java/org/truffleruby/language/loader/MainLoader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package org.truffleruby.language.loader;
1111

1212
import java.io.ByteArrayOutputStream;
13-
import java.io.File;
1413
import java.io.IOException;
1514

1615
import org.jcodings.specific.UTF8Encoding;
@@ -97,7 +96,7 @@ public RubySource loadFromFile(Env env, RubyNode currentNode, String path) throw
9796
final Rope sourceRope = transformScript(currentNode, path, sourceBytes);
9897

9998
final Source source = fileLoader.buildSource(file, path, sourceRope, false);
100-
context.setMainSource(source, new File(path).getAbsolutePath());
99+
context.setMainSource(source, file.getAbsoluteFile().getPath());
101100

102101
return new RubySource(source, sourceRope);
103102
}

src/main/ruby/truffleruby/post-boot/post-boot.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,21 @@
7171
if old_home
7272
# We need to fix all paths which capture the image build-time home to point
7373
# to the runtime home.
74+
75+
paths_starting_with_home = []
76+
[$LOAD_PATH, $LOADED_FEATURES].each do |array|
77+
array.each do |path|
78+
if path.start_with?(old_home)
79+
paths_starting_with_home << path[old_home.size..-1]
80+
end
81+
end
82+
end
83+
old_home = nil
84+
7485
Truffle::Boot.delay do
7586
new_home = Truffle::Boot.ruby_home
76-
[$LOAD_PATH, $LOADED_FEATURES].each do |array|
77-
array.each do |path|
78-
if path.start_with?(old_home)
79-
path.replace(new_home + path[old_home.size..-1])
80-
end
81-
end
87+
paths_starting_with_home.each do |path|
88+
path.replace(new_home + path)
8289
end
8390
end
8491
end

0 commit comments

Comments
 (0)