Add MH.defineClass into redirect handler

This commit is contained in:
IzzelAliz 2021-06-27 14:29:12 +08:00
parent 9617cffe89
commit f2b24ca9f0
2 changed files with 11 additions and 0 deletions

View File

@ -91,6 +91,7 @@ public class ArclightRedirectAdapter implements PluginTransformer {
modify(classOf("sun.misc.Unsafe"), "defineClass", "unsafeDefineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class); modify(classOf("sun.misc.Unsafe"), "defineClass", "unsafeDefineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class);
modify(classOf("jdk.internal.misc.Unsafe"), "defineClass", "unsafeDefineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class); modify(classOf("jdk.internal.misc.Unsafe"), "defineClass", "unsafeDefineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class);
modify(classOf("jdk.internal.misc.Unsafe"), "defineClass0", "unsafeDefineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class); modify(classOf("jdk.internal.misc.Unsafe"), "defineClass0", "unsafeDefineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class);
modify(MethodHandles.Lookup.class, "defineClass", "lookupDefineClass", byte[].class);
} }
public static Object[] runHandle(ClassLoaderRemapper remapper, Method method, Object src, Object[] param) { public static Object[] runHandle(ClassLoaderRemapper remapper, Method method, Object src, Object[] param) {

View File

@ -461,6 +461,16 @@ public class ArclightReflectionHandler extends ClassLoader {
return redirectDefineClass(loader, name, bytes, off, len, pd); return redirectDefineClass(loader, name, bytes, off, len, pd);
} }
public static Object[] handleLookupDefineClass(MethodHandles.Lookup lookup, byte[] bytes) {
return new Object[]{lookup, transformOrAdd(lookup.lookupClass().getClassLoader(), bytes)};
}
public static Class<?> redirectLookupDefineClass(MethodHandles.Lookup lookup, byte[] bytes) throws Throwable {
byte[] transform = transformOrAdd(lookup.lookupClass().getClassLoader(), bytes);
MethodHandle mh = Unsafe.lookup().findVirtual(MethodHandles.Lookup.class, "defineClass", MethodType.methodType(Class.class, byte[].class));
return (Class<?>) mh.invokeExact(lookup, transform);
}
public static byte[] transformOrAdd(ClassLoader loader, byte[] bytes) { public static byte[] transformOrAdd(ClassLoader loader, byte[] bytes) {
RemappingClassLoader rcl = null; RemappingClassLoader rcl = null;
while (loader != null) { while (loader != null) {