Skip to content
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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ private[spark] class JavaDeserializationStream(in: InputStream, loader: ClassLoa
case e: ClassNotFoundException =>
JavaDeserializationStream.primitiveMappings.getOrElse(desc.getName, throw e)
}

override def resolveProxyClass(ifaces: Array[String]): Class[_] = {
// scalastyle:off classforname
val resolved = ifaces.map(iface => Class.forName(iface, false, loader))
Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utils#classForName uses either getContextOrSparkClassLoader or thread.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).

// scalastyle:on classforname
java.lang.reflect.Proxy.getProxyClass(loader, resolved: _*)
}
}

def readObject[T: ClassTag](): T = objIn.readObject().asInstanceOf[T]
Expand Down
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(
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Up @@ -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")))
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 sun.misc.VM.latestUserDefinedLoader() not be able to resolve MyInterface, but I couldn't figure out how to do it.

}
}

private class ContainsPrimitiveClass extends Serializable {
Expand Down