-
Notifications
You must be signed in to change notification settings - Fork 28.1k
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
[SPARK-36627][CORE] Fix java deserialization of proxy classes #33879
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.apache.spark.serializer; | ||
srowen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
|
||
class ContainsProxyClass implements Serializable { | ||
final MyInterface proxy = (MyInterface) Proxy.newProxyInstance( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise 2-space |
||
MyInterface.class.getClassLoader(), | ||
new Class[]{MyInterface.class}, | ||
new MyInvocationHandler()); | ||
|
||
// Interface needs to be public as classloaders will mismatch - see ObjectInputStream#resolveProxyClass for details. | ||
public interface MyInterface { | ||
void myMethod(); | ||
} | ||
|
||
static class MyClass implements MyInterface, Serializable { | ||
@Override | ||
public void myMethod() { | ||
} | ||
} | ||
|
||
class MyInvocationHandler implements InvocationHandler, Serializable { | ||
private final MyClass real = new MyClass(); | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
return method.invoke(real, args); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,11 +31,33 @@ class JavaSerializerSuite extends SparkFunSuite { | |
test("Deserialize object containing a primitive Class as attribute") { | ||
val serializer = new JavaSerializer(new SparkConf()) | ||
val instance = serializer.newInstance() | ||
val obj = instance.deserialize[ContainsPrimitiveClass](instance.serialize( | ||
new ContainsPrimitiveClass())) | ||
val obj = instance.deserialize[ContainsPrimitiveClass]( | ||
instance.serialize(new ContainsPrimitiveClass())) | ||
// enforce class cast | ||
obj.getClass | ||
} | ||
|
||
test("SPARK-36627: Deserialize object containing a proxy Class as attribute") { | ||
var classesLoaded = Set[String]() | ||
val outer = Thread.currentThread.getContextClassLoader | ||
val inner = new ClassLoader() { | ||
override def loadClass(name: String): Class[_] = { | ||
classesLoaded = classesLoaded + name | ||
outer.loadClass(name) | ||
} | ||
} | ||
Thread.currentThread.setContextClassLoader(inner) | ||
|
||
val serializer = new JavaSerializer(new SparkConf()) | ||
val instance = serializer.newInstance() | ||
val obj = | ||
instance.deserialize[ContainsProxyClass](instance.serialize(new ContainsProxyClass())) | ||
// enforce class cast | ||
obj.getClass | ||
|
||
// check that serializer's loader is used to resolve proxied interface. | ||
assert(classesLoaded.exists(klass => klass.contains("MyInterface"))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very roundabout way of testing this, but I couldn't think of another way of testing it - the way we hit this issue was running spark in YARN, and I don't think we can trivially reproduce it with spark local. In order to repro exactly what happened in YARN we'd need to make |
||
} | ||
} | ||
|
||
private class ContainsPrimitiveClass extends Serializable { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We definitely want better exceptions if this fails, but not sure what exactly we'd want here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2-space indent, and maybe you can use the Utils method in Spark for loading the class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Utils#classForName
uses eithergetContextOrSparkClassLoader
orthread.currentThread().getContextClassLoader
which might not be the ones we want depending on the context we're deserializing -- I'd rather use the same classloader we're using above (when deserializing non-proxy java objects).