Skip to content

Commit

Permalink
Disable ForkClassLoader when --thin is passed
Browse files Browse the repository at this point in the history
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…
  • Loading branch information
alexarchambault committed Dec 3, 2020
1 parent 9113fe5 commit 50b0e0f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion amm/repl/src/main/scala/ammonite/repl/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions amm/runtime/src/main/scala/ammonite/runtime/ClassLoaders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,22 @@ 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
// Java standard library

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
Expand Down
2 changes: 1 addition & 1 deletion amm/src/main/scala/ammonite/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 50b0e0f

Please sign in to comment.