Split rewriting flag into paper.disableOldApiSupport and paper.disablePluginRemapping (#11108)

This commit is contained in:
Nassim Jahnke 2024-07-19 13:08:08 +02:00
parent 4c38b7496b
commit 203314faf8
3 changed files with 39 additions and 31 deletions

View file

@ -915,8 +915,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
- visitor = new LimitedClassRemapper(cw, new SimpleRemapper(Commodore.ENUM_RENAMES));
- }
cr.accept(new ClassRemapper(new ClassVisitor(Opcodes.ASM9, io.papermc.paper.pluginremap.reflect.ReflectionRemapper.visitor(visitor)) { // Paper
final Set<RerouteMethodData> rerouteMethodData = new HashSet<>();
visitor = io.papermc.paper.pluginremap.reflect.ReflectionRemapper.visitor(visitor); // Paper
cr.accept(new ClassRemapper(new ClassVisitor(Opcodes.ASM9, visitor) {
@@ -0,0 +0,0 @@ public class Commodore {
@Override

View file

@ -92,7 +92,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
PluginInitializerManager(final Path pluginDirectory, final Path updateDirectory) {
this.pluginDirectory = pluginDirectory;
this.updateDirectory = updateDirectory;
+ this.pluginRemapper = Boolean.getBoolean("paper.disable-plugin-rewriting")
+ this.pluginRemapper = Boolean.getBoolean("paper.disablePluginRemapping")
+ ? null
+ : PluginRemapper.create(pluginDirectory);
+ LibraryLoader.REMAPPER = this.pluginRemapper == null ? Function.identity() : this.pluginRemapper::remapLibraries;

View file

@ -240,23 +240,12 @@ diff --git a/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperC
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperClassloaderBytecodeModifier.java
+++ b/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperClassloaderBytecodeModifier.java
@@ -0,0 +0,0 @@
package io.papermc.paper.plugin.entrypoint.classloader;
import io.papermc.paper.plugin.configuration.PluginMeta;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassWriter;
// Stub, implement in future.
public class PaperClassloaderBytecodeModifier implements ClassloaderBytecodeModifier {
@@ -0,0 +0,0 @@ public class PaperClassloaderBytecodeModifier implements ClassloaderBytecodeModi
@Override
public byte[] modify(PluginMeta configuration, byte[] bytecode) {
- return bytecode;
+ ClassReader classReader = new ClassReader(bytecode);
+ ClassWriter classWriter = new ClassWriter(classReader, 0);
+ classReader.accept(io.papermc.paper.pluginremap.reflect.ReflectionRemapper.visitor(classWriter), 0);
+ return classWriter.toByteArray();
+ return io.papermc.paper.pluginremap.reflect.ReflectionRemapper.processClass(bytecode);
}
}
diff --git a/src/main/java/io/papermc/paper/plugin/loader/PaperClasspathBuilder.java b/src/main/java/io/papermc/paper/plugin/loader/PaperClasspathBuilder.java
@ -292,7 +281,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
try {
- return new PaperPluginClassLoader(logger, source, jarFile, configuration, this.getClass().getClassLoader(), new URLClassLoader(urls, getClass().getClassLoader()));
+ final URLClassLoader libraryLoader = MappingEnvironment.DISABLE_PLUGIN_REWRITING
+ final URLClassLoader libraryLoader = MappingEnvironment.DISABLE_PLUGIN_REMAPPING
+ ? new URLClassLoader(urls, this.getClass().getClassLoader())
+ : new BytecodeModifyingURLClassLoader(urls, this.getClass().getClassLoader());
+ return new PaperPluginClassLoader(logger, source, jarFile, configuration, this.getClass().getClassLoader(), libraryLoader);
@ -321,7 +310,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
class SpigotPluginProviderFactory implements PluginTypeFactory<SpigotPluginProvider, PluginDescriptionFile> {
+ static {
+ if (!MappingEnvironment.DISABLE_PLUGIN_REWRITING) {
+ if (!MappingEnvironment.DISABLE_PLUGIN_REMAPPING) {
+ LibraryLoader.LIBRARY_LOADER_FACTORY = BytecodeModifyingURLClassLoader::new;
+ }
+ }
@ -352,8 +341,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.checkerframework.framework.qual.DefaultQualifier;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassWriter;
+import org.slf4j.Logger;
+
+// todo proper inheritance handling
@ -541,10 +528,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ // todo apply bytecode remap here as well
+ private static byte[] processClass(final byte[] bytes) {
+ try {
+ final ClassReader reader = new ClassReader(bytes);
+ final ClassWriter writer = new ClassWriter(reader, 0);
+ reader.accept(ReflectionRemapper.visitor(writer), 0);
+ return writer.toByteArray();
+ return ReflectionRemapper.processClass(bytes);
+ } catch (final Exception ex) {
+ LOGGER.warn("Failed to process class bytes", ex);
+ return bytes;
@ -569,7 +553,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import java.lang.reflect.Method;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.framework.qual.DefaultQualifier;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.Opcodes;
+
+@DefaultQualifier(NonNull.class)
@ -593,12 +579,22 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ public static ClassVisitor visitor(final ClassVisitor parent) {
+ if (MappingEnvironment.reobf()) {
+ if (MappingEnvironment.reobf() || MappingEnvironment.DISABLE_PLUGIN_REMAPPING) {
+ return parent;
+ }
+ return VISITOR_FACTORY.createVisitor(parent);
+ }
+
+ public static byte[] processClass(final byte[] bytes) {
+ if (MappingEnvironment.DISABLE_PLUGIN_REMAPPING) {
+ return bytes;
+ }
+ final ClassReader classReader = new ClassReader(bytes);
+ final ClassWriter classWriter = new ClassWriter(classReader, 0);
+ classReader.accept(ReflectionRemapper.visitor(classWriter), 0);
+ return classWriter.toByteArray();
+ }
+
+ private static void setupProxy() {
+ try {
+ final byte[] bytes = ProxyGenerator.generateProxy(PaperReflection.class, PAPER_REFLECTION_HOLDER_DESC);
@ -619,7 +615,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@DefaultQualifier(NonNull.class)
public final class MappingEnvironment {
+ public static final boolean DISABLE_PLUGIN_REWRITING = Boolean.getBoolean("paper.disable-plugin-rewriting");
+ public static final boolean DISABLE_PLUGIN_REMAPPING = Boolean.getBoolean("paper.disablePluginRemapping");
+ public static final String LEGACY_CB_VERSION = "v1_21_R1";
private static final @Nullable String MAPPINGS_HASH = readMappingsHash();
private static final boolean REOBF = checkReobf();
@ -704,21 +700,28 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
visitor = new LimitedClassRemapper(cw, new SimpleRemapper(Commodore.ENUM_RENAMES));
}
- cr.accept(new ClassRemapper(new ClassVisitor(Opcodes.ASM9, visitor) {
+ cr.accept(new ClassRemapper(new ClassVisitor(Opcodes.ASM9, io.papermc.paper.pluginremap.reflect.ReflectionRemapper.visitor(visitor)) { // Paper
+ visitor = io.papermc.paper.pluginremap.reflect.ReflectionRemapper.visitor(visitor); // Paper
cr.accept(new ClassRemapper(new ClassVisitor(Opcodes.ASM9, visitor) {
final Set<RerouteMethodData> rerouteMethodData = new HashSet<>();
String className;
boolean isInterface;
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -0,0 +0,0 @@ import org.bukkit.potion.PotionType;
@SuppressWarnings("deprecation")
public final class CraftMagicNumbers implements UnsafeValues {
public static final UnsafeValues INSTANCE = new CraftMagicNumbers();
+ public static final boolean DISABLE_OLD_API_SUPPORT = Boolean.getBoolean("paper.disableOldApiSupport"); // Paper
private CraftMagicNumbers() {}
@@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
throw new InvalidPluginException("Plugin API version " + pdf.getAPIVersion() + " is lower than the minimum allowed version. Please update or replace it.");
}
- if (toCheck.isOlderThan(ApiVersion.FLATTENING)) {
+ if (!io.papermc.paper.util.MappingEnvironment.DISABLE_PLUGIN_REWRITING && toCheck.isOlderThan(ApiVersion.FLATTENING)) { // Paper
+ if (!DISABLE_OLD_API_SUPPORT && toCheck.isOlderThan(ApiVersion.FLATTENING)) { // Paper
CraftLegacy.init();
}
@ -726,7 +729,12 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@Override
public byte[] processClass(PluginDescriptionFile pdf, String path, byte[] clazz) {
+ if (io.papermc.paper.util.MappingEnvironment.DISABLE_PLUGIN_REWRITING) return clazz; // Paper
+ // Paper start
+ if (DISABLE_OLD_API_SUPPORT) {
+ // Make sure we still go through our reflection rewriting if needed
+ return io.papermc.paper.pluginremap.reflect.ReflectionRemapper.processClass(clazz);
+ }
+ // Paper end
try {
clazz = Commodore.convert(clazz, pdf.getName(), ApiVersion.getOrCreateVersion(pdf.getAPIVersion()), ((CraftServer) Bukkit.getServer()).activeCompatibilities);
} catch (Exception ex) {