From 50b0e0f262b2594c2d47bc4a1bf0d06742ee5365 Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Thu, 26 Nov 2020 14:43:36 +0100 Subject: [PATCH] Disable ForkClassLoader when --thin is passed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ForkClassLoader injects resources from another ClassLoader. When --thin is passed, it could have injected resources that should have been blacklisted. Alternatively, maybe we could have adde a whitelisting layer on top of it too… --- .../ammonite/interp/script/SingleScriptCompiler.scala | 2 +- amm/repl/src/main/scala/ammonite/repl/Repl.scala | 4 +++- .../src/main/scala/ammonite/runtime/ClassLoaders.scala | 10 ++++++++-- amm/src/main/scala/ammonite/Main.scala | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/amm/interp/src/main/scala/ammonite/interp/script/SingleScriptCompiler.scala b/amm/interp/src/main/scala/ammonite/interp/script/SingleScriptCompiler.scala index 1486f225f..48d5c415a 100644 --- a/amm/interp/src/main/scala/ammonite/interp/script/SingleScriptCompiler.scala +++ b/amm/interp/src/main/scala/ammonite/interp/script/SingleScriptCompiler.scala @@ -43,7 +43,7 @@ class SingleScriptCompiler( } private val frame = { - val f = Frame.createInitial(initialClassLoader) + val f = Frame.createInitial(initialClassLoader, forking = false) f.addClasspath(dependencies.jars.map(_.toNIO.toUri.toURL)) f.addPluginClasspath(dependencies.pluginJars.map(_.toNIO.toUri.toURL)) for ((clsName, byteCode) <- dependencies.byteCode) diff --git a/amm/repl/src/main/scala/ammonite/repl/Repl.scala b/amm/repl/src/main/scala/ammonite/repl/Repl.scala index 418cc9078..e5b892bf3 100644 --- a/amm/repl/src/main/scala/ammonite/repl/Repl.scala +++ b/amm/repl/src/main/scala/ammonite/repl/Repl.scala @@ -55,7 +55,9 @@ class Repl(input: InputStream, """ }.mkString(newLine) - val frames = Ref(List(Frame.createInitial(initialClassLoader))) + val frames = Ref(List( + Frame.createInitial(initialClassLoader, forking = classPathWhitelist.isEmpty) + )) /** * The current line number of the REPL, used to make sure every snippet diff --git a/amm/runtime/src/main/scala/ammonite/runtime/ClassLoaders.scala b/amm/runtime/src/main/scala/ammonite/runtime/ClassLoaders.scala index da2bb5fb6..3137df68b 100644 --- a/amm/runtime/src/main/scala/ammonite/runtime/ClassLoaders.scala +++ b/amm/runtime/src/main/scala/ammonite/runtime/ClassLoaders.scala @@ -62,7 +62,10 @@ class Frame(val classloader: SpecialClassLoader, usedEarlierDefinitions0 = usedEarlierDefinitions } object Frame{ - def createInitial(baseClassLoader: ClassLoader = Thread.currentThread().getContextClassLoader) = { + def createInitial( + baseClassLoader: ClassLoader = Thread.currentThread().getContextClassLoader, + forking: Boolean = true + ) = { // *Try* to load the JVM source files and make them available as resources, // so that the `source` helper can navigate to the sources within the @@ -70,8 +73,11 @@ object Frame{ val likelyJdkSourceLocation = os.Path(System.getProperty("java.home"))/os.up/"src.zip" val hash = SpecialClassLoader.initialClasspathSignature(baseClassLoader) + val parent = + if (forking) new ForkClassLoader(baseClassLoader, getClass.getClassLoader) + else baseClassLoader def special = new SpecialClassLoader( - new ForkClassLoader(baseClassLoader, getClass.getClassLoader), + parent, hash, Set.empty, likelyJdkSourceLocation.wrapped.toUri.toURL diff --git a/amm/src/main/scala/ammonite/Main.scala b/amm/src/main/scala/ammonite/Main.scala index 83f5a895b..f901a3b95 100644 --- a/amm/src/main/scala/ammonite/Main.scala +++ b/amm/src/main/scala/ammonite/Main.scala @@ -154,7 +154,7 @@ case class Main(predefCode: String = "", errorStream, verboseOutput ) - val frame = Frame.createInitial(initialClassLoader) + val frame = Frame.createInitial(initialClassLoader, forking = classPathWhitelist.isEmpty) val customPredefs = predefFileInfoOpt.toSeq ++ Seq( PredefInfo(Name("CodePredef"), predefCode, false, None)