-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #4440: Do not serialize the content of static objects #5775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d89e6be
Fix #4440: Do not serialize the content of static objects
smarter 9b3cccd
Allow `classOf[Foo.type]` if `Foo` is an object
smarter 84ba712
Do not include writeReplace when decompiling
nicolasstucki 72ee855
Add regression test for classOf on an object.
nicolasstucki 140f7e1
Update method name
nicolasstucki 191442c
Do not decompile synthetic writeReplace in objects
nicolasstucki 95900de
Remove unnecessary check
nicolasstucki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copied from https://github.com/scala/scala/blob/2.13.x/src/library/scala/runtime/ModuleSerializationProxy.java | ||
// TODO: Remove this file once we switch to the Scala 2.13 stdlib since it already contains it. | ||
|
||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.runtime; | ||
|
||
import java.io.Serializable; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedActionException; | ||
import java.security.PrivilegedExceptionAction; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** A serialization proxy for singleton objects */ | ||
public final class ModuleSerializationProxy implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
private final Class<?> moduleClass; | ||
private static final ClassValue<Object> instances = new ClassValue<Object>() { | ||
@Override | ||
protected Object computeValue(Class<?> type) { | ||
try { | ||
return AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> type.getField("MODULE$").get(null)); | ||
} catch (PrivilegedActionException e) { | ||
return rethrowRuntime(e.getCause()); | ||
} | ||
} | ||
}; | ||
|
||
private static Object rethrowRuntime(Throwable e) { | ||
Throwable cause = e.getCause(); | ||
if (cause instanceof RuntimeException) throw (RuntimeException) cause; | ||
else throw new RuntimeException(cause); | ||
} | ||
|
||
public ModuleSerializationProxy(Class<?> moduleClass) { | ||
this.moduleClass = moduleClass; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private Object readResolve() { | ||
return instances.get(moduleClass); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** Decompiled from out/runTestFromTasty/run/classof-object/Test.tasty */ | ||
object Test { | ||
def main(args: scala.Array[scala.Predef.String]): scala.Unit = if (scala.Predef.classOf[Test.type].==(Test.getClass()).unary_!) dotty.DottyPredef.assertFail() else () | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
assert(classOf[Test.type] == Test.getClass) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import java.io.File | ||
|
||
object Module { | ||
val data = new Array[Byte](32 * 1024 * 1024) | ||
} | ||
|
||
object Test { | ||
private val readResolve = classOf[scala.runtime.ModuleSerializationProxy].getDeclaredMethod("readResolve") | ||
readResolve.setAccessible(true) | ||
|
||
val testClassesDir = new File(Module.getClass.getClassLoader.getResource("Module.class").toURI).getParentFile | ||
def main(args: Array[String]): Unit = { | ||
for (i <- 1 to 256) { | ||
// This would "java.lang.OutOfMemoryError: Java heap space" if ModuleSerializationProxy | ||
// prevented class unloading. | ||
deserializeDynamicLoadedClass() | ||
} | ||
} | ||
|
||
def deserializeDynamicLoadedClass(): Unit = { | ||
val loader = new java.net.URLClassLoader(Array(testClassesDir.toURI.toURL), ClassLoader.getSystemClassLoader) | ||
val moduleClass = loader.loadClass("Module$") | ||
assert(moduleClass ne Module.getClass) | ||
val result = readResolve.invoke(new scala.runtime.ModuleSerializationProxy(moduleClass)) | ||
assert(result.getClass == moduleClass) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.