From 90105224be600acf2f890ec4ce6e5d3d32362c3b Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Tue, 7 Nov 2023 13:30:26 +0100 Subject: [PATCH 1/3] [GR-49849] Revert "Create an internal resource TruffleFile for FileLoader" * This reverts commit 53f7afc5f8563cda517a8bee4d9a795160f519b7. * No longer necessary, now getInternalTruffleFile() handles InternalResource. --- .../language/loader/FileLoader.java | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/truffleruby/language/loader/FileLoader.java b/src/main/java/org/truffleruby/language/loader/FileLoader.java index 318ec477b2e2..a236c569d867 100644 --- a/src/main/java/org/truffleruby/language/loader/FileLoader.java +++ b/src/main/java/org/truffleruby/language/loader/FileLoader.java @@ -11,6 +11,7 @@ import java.io.File; import java.io.IOException; +import java.util.Locale; import com.oracle.truffle.api.nodes.Node; import org.graalvm.collections.Pair; @@ -56,6 +57,7 @@ public static void ensureReadable(RubyContext context, TruffleFile file, Node cu } } + public Pair loadFile(String path) throws IOException { if (context.getOptions().LOG_LOAD) { RubyLanguage.LOGGER.info("loading " + path); @@ -77,10 +79,6 @@ public Pair loadFile(String path) throws IOExceptio public static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext context, String path) { final Env env = context.getEnv(); - if (env.isFileIOAllowed()) { - return env.getPublicTruffleFile(path); - } - final TruffleFile file; try { file = env.getInternalTruffleFile(path).getCanonicalFile(); @@ -90,29 +88,36 @@ public static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext context.getCoreExceptions().loadError("Failed to canonicalize -- " + path, path, null)); } - String homeLibDir = language.getRubyHome() + "/lib/"; - if (file.getPath().startsWith(homeLibDir)) { - String homeRelativePath = file.getPath().substring(language.getRubyHome().length() + 1); - TruffleFile internalResourceFile = language.getRubyHomeTruffleFile().resolve(homeRelativePath); - if (isStdLibRubyOrCExtFile(internalResourceFile.getPath())) { - return internalResourceFile; + final TruffleFile home = language.getRubyHomeTruffleFile(); + if (file.startsWith(home) && isStdLibRubyOrCExtFile(home.relativize(file))) { + return file; + } else { + try { + return env.getPublicTruffleFile(path); + } catch (SecurityException e) { + throw new RaiseException( + context, + context.getCoreExceptions().loadError( + "Permission denied (" + e.getMessage() + ") -- " + path, + path, + null)); } } + } - try { - return env.getPublicTruffleFile(path); - } catch (SecurityException e) { - throw new RaiseException( - context, - context.getCoreExceptions().loadError( - "Permission denied (" + e.getMessage() + ") -- " + path, - path, - null)); + private static boolean isStdLibRubyOrCExtFile(TruffleFile relativePathFromHome) { + final String fileName = relativePathFromHome.getName(); + if (fileName == null) { + return false; + } + + final String lowerCaseFileName = fileName.toLowerCase(Locale.ROOT); + if (!lowerCaseFileName.endsWith(TruffleRuby.EXTENSION) && + !lowerCaseFileName.endsWith(RubyLanguage.CEXT_EXTENSION)) { + return false; } - } - private static boolean isStdLibRubyOrCExtFile(String path) { - return path.endsWith(TruffleRuby.EXTENSION) || path.endsWith(RubyLanguage.CEXT_EXTENSION); + return relativePathFromHome.startsWith("lib"); } Source buildSource(TruffleFile file, String path, TStringWithEncoding sourceTStringWithEncoding, boolean internal, From a40992f404143e778518ec8bc56243353a2d8a64 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Tue, 7 Nov 2023 13:33:56 +0100 Subject: [PATCH 2/3] Add fast path when access too all files is allowed --- src/main/java/org/truffleruby/language/loader/FileLoader.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/truffleruby/language/loader/FileLoader.java b/src/main/java/org/truffleruby/language/loader/FileLoader.java index a236c569d867..5ed5347a31ed 100644 --- a/src/main/java/org/truffleruby/language/loader/FileLoader.java +++ b/src/main/java/org/truffleruby/language/loader/FileLoader.java @@ -79,6 +79,10 @@ public Pair loadFile(String path) throws IOExceptio public static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext context, String path) { final Env env = context.getEnv(); + if (env.isFileIOAllowed()) { + return env.getPublicTruffleFile(path); + } + final TruffleFile file; try { file = env.getInternalTruffleFile(path).getCanonicalFile(); From 239acdeb83e13442db1a1cf96c890a710ef5c032 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Tue, 7 Nov 2023 13:39:33 +0100 Subject: [PATCH 3/3] Optimize isStdLibRubyOrCExtFile() check --- .../language/loader/FileLoader.java | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/truffleruby/language/loader/FileLoader.java b/src/main/java/org/truffleruby/language/loader/FileLoader.java index 5ed5347a31ed..6c72394f2487 100644 --- a/src/main/java/org/truffleruby/language/loader/FileLoader.java +++ b/src/main/java/org/truffleruby/language/loader/FileLoader.java @@ -11,7 +11,6 @@ import java.io.File; import java.io.IOException; -import java.util.Locale; import com.oracle.truffle.api.nodes.Node; import org.graalvm.collections.Pair; @@ -57,7 +56,6 @@ public static void ensureReadable(RubyContext context, TruffleFile file, Node cu } } - public Pair loadFile(String path) throws IOException { if (context.getOptions().LOG_LOAD) { RubyLanguage.LOGGER.info("loading " + path); @@ -93,7 +91,7 @@ public static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext } final TruffleFile home = language.getRubyHomeTruffleFile(); - if (file.startsWith(home) && isStdLibRubyOrCExtFile(home.relativize(file))) { + if (file.startsWith(home.resolve("lib")) && isStdLibRubyOrCExtFile(file.getPath())) { return file; } else { try { @@ -109,19 +107,8 @@ public static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext } } - private static boolean isStdLibRubyOrCExtFile(TruffleFile relativePathFromHome) { - final String fileName = relativePathFromHome.getName(); - if (fileName == null) { - return false; - } - - final String lowerCaseFileName = fileName.toLowerCase(Locale.ROOT); - if (!lowerCaseFileName.endsWith(TruffleRuby.EXTENSION) && - !lowerCaseFileName.endsWith(RubyLanguage.CEXT_EXTENSION)) { - return false; - } - - return relativePathFromHome.startsWith("lib"); + private static boolean isStdLibRubyOrCExtFile(String path) { + return path.endsWith(TruffleRuby.EXTENSION) || path.endsWith(RubyLanguage.CEXT_EXTENSION); } Source buildSource(TruffleFile file, String path, TStringWithEncoding sourceTStringWithEncoding, boolean internal,