mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-22 08:57:35 +01:00
2641c02193
Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: 69fa4695 Add some missing deprecation annotations f850da2e Update Maven plugins/versions 8d8400db Use regular compiler seeing as ECJ doesn't support Java 21 JRE c29e1688 Revert "BUILDTOOLS-676: Downgrade Maven compiler version" 07bce714 SPIGOT-7355: More field renames and fixes 6a8ea764 Fix bad merge in penultimate commit 50a7920c Fix imports in previous commit 83640dd1 PR-995: Add required feature to MinecraftExperimental for easy lookups fc1f96cf BUILDTOOLS-676: Downgrade Maven compiler version CraftBukkit Changes: 90f1059ba Fix item placement 661afb43c SPIGOT-7633: Clearer error message for missing particle data 807b465b3 SPIGOT-7634: Armadillo updates infrequently 590cf09a8 Fix unit tests always seeing Mojang server as unavailable 7c7ac5eb2 SPIGOT-7636: Fix clearing ItemMeta 4a72905cf SPIGOT-7635: Fix Player#transfer and cookie methods ebb50e136 Fix incorrect Vault implementation b33fed8b7 Update Maven plugins/versions 6f00f0608 SPIGOT-7632: Control middle clicking chest does not copy contents db821f405 Use regular compiler seeing as ECJ doesn't support Java 21 JRE 8a2976737 Revert "BUILDTOOLS-676: Downgrade Maven compiler version" 0297f87bb SPIGOT-7355: More field renames and fixes 2d03bdf6a SPIGOT-7629: Fix loading banner patterns e77951fac Fix equality of deserialized display names c66f3e4fd SPIGOT-7631: Fix deserialisation of BlockStateMeta 9c2c7be8d SPIGOT-7630: Fix crash saving unticked leashed entities 8c1e7c841 PR-1384: Disable certain PlayerProfile tests, if Mojang's services or internet are not available ced93d572 SPIGOT-7626: sendSignChange() has no effect c77362cae SPIGOT-7625: ItemStack with lore cannot be serialized in 1.20.5 ff2004387 SPIGOT-7620: Fix server crash when hoppers transfer items to double chests 8b4abeb03 BUILDTOOLS-676: Downgrade Maven compiler version
368 lines
16 KiB
Diff
368 lines
16 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Techcable <Techcable@outlook.com>
|
|
Date: Thu, 3 Mar 2016 13:20:33 -0700
|
|
Subject: [PATCH] Use ASM for event executors.
|
|
|
|
Uses method handles for private or static methods.
|
|
|
|
diff --git a/build.gradle.kts b/build.gradle.kts
|
|
index c23ee26662657baa496d6048b9a8ba39b4998e72..759294705e0f53c18ce2825b5f6c715a65f23d90 100644
|
|
--- a/build.gradle.kts
|
|
+++ b/build.gradle.kts
|
|
@@ -47,6 +47,9 @@ dependencies {
|
|
apiAndDocs("net.kyori:adventure-text-serializer-legacy")
|
|
apiAndDocs("net.kyori:adventure-text-serializer-plain")
|
|
apiAndDocs("net.kyori:adventure-text-logger-slf4j")
|
|
+
|
|
+ implementation("org.ow2.asm:asm:9.7")
|
|
+ implementation("org.ow2.asm:asm-commons:9.7")
|
|
// Paper end
|
|
|
|
compileOnly("org.apache.maven:maven-resolver-provider:3.9.6")
|
|
diff --git a/src/main/java/com/destroystokyo/paper/event/executor/MethodHandleEventExecutor.java b/src/main/java/com/destroystokyo/paper/event/executor/MethodHandleEventExecutor.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..5b28e9b1daba7834af67dbc193dd656bedd9a994
|
|
--- /dev/null
|
|
+++ b/src/main/java/com/destroystokyo/paper/event/executor/MethodHandleEventExecutor.java
|
|
@@ -0,0 +1,42 @@
|
|
+package com.destroystokyo.paper.event.executor;
|
|
+
|
|
+import java.lang.invoke.MethodHandle;
|
|
+import java.lang.invoke.MethodHandles;
|
|
+import java.lang.reflect.Method;
|
|
+
|
|
+import com.destroystokyo.paper.util.SneakyThrow;
|
|
+import org.bukkit.event.Event;
|
|
+import org.bukkit.event.EventException;
|
|
+import org.bukkit.event.Listener;
|
|
+import org.bukkit.plugin.EventExecutor;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public class MethodHandleEventExecutor implements EventExecutor {
|
|
+ private final Class<? extends Event> eventClass;
|
|
+ private final MethodHandle handle;
|
|
+
|
|
+ public MethodHandleEventExecutor(@NotNull Class<? extends Event> eventClass, @NotNull MethodHandle handle) {
|
|
+ this.eventClass = eventClass;
|
|
+ this.handle = handle;
|
|
+ }
|
|
+
|
|
+ public MethodHandleEventExecutor(@NotNull Class<? extends Event> eventClass, @NotNull Method m) {
|
|
+ this.eventClass = eventClass;
|
|
+ try {
|
|
+ m.setAccessible(true);
|
|
+ this.handle = MethodHandles.lookup().unreflect(m);
|
|
+ } catch (IllegalAccessException e) {
|
|
+ throw new AssertionError("Unable to set accessible", e);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void execute(@NotNull Listener listener, @NotNull Event event) throws EventException {
|
|
+ if (!eventClass.isInstance(event)) return;
|
|
+ try {
|
|
+ handle.invoke(listener, event);
|
|
+ } catch (Throwable t) {
|
|
+ SneakyThrow.sneaky(t);
|
|
+ }
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/com/destroystokyo/paper/event/executor/StaticMethodHandleEventExecutor.java b/src/main/java/com/destroystokyo/paper/event/executor/StaticMethodHandleEventExecutor.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..827f2b27f70a7ec0bc11d039305c3e58c02a4ef4
|
|
--- /dev/null
|
|
+++ b/src/main/java/com/destroystokyo/paper/event/executor/StaticMethodHandleEventExecutor.java
|
|
@@ -0,0 +1,42 @@
|
|
+package com.destroystokyo.paper.event.executor;
|
|
+
|
|
+import java.lang.invoke.MethodHandle;
|
|
+import java.lang.invoke.MethodHandles;
|
|
+import java.lang.reflect.Method;
|
|
+import java.lang.reflect.Modifier;
|
|
+
|
|
+import com.destroystokyo.paper.util.SneakyThrow;
|
|
+import com.google.common.base.Preconditions;
|
|
+
|
|
+import org.bukkit.event.Event;
|
|
+import org.bukkit.event.EventException;
|
|
+import org.bukkit.event.Listener;
|
|
+import org.bukkit.plugin.EventExecutor;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public class StaticMethodHandleEventExecutor implements EventExecutor {
|
|
+ private final Class<? extends Event> eventClass;
|
|
+ private final MethodHandle handle;
|
|
+
|
|
+ public StaticMethodHandleEventExecutor(@NotNull Class<? extends Event> eventClass, @NotNull Method m) {
|
|
+ Preconditions.checkArgument(Modifier.isStatic(m.getModifiers()), "Not a static method: %s", m);
|
|
+ Preconditions.checkArgument(eventClass != null, "eventClass is null");
|
|
+ this.eventClass = eventClass;
|
|
+ try {
|
|
+ m.setAccessible(true);
|
|
+ this.handle = MethodHandles.lookup().unreflect(m);
|
|
+ } catch (IllegalAccessException e) {
|
|
+ throw new AssertionError("Unable to set accessible", e);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void execute(@NotNull Listener listener, @NotNull Event event) throws EventException {
|
|
+ if (!eventClass.isInstance(event)) return;
|
|
+ try {
|
|
+ handle.invoke(event);
|
|
+ } catch (Throwable throwable) {
|
|
+ SneakyThrow.sneaky(throwable);
|
|
+ }
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/com/destroystokyo/paper/event/executor/asm/ASMEventExecutorGenerator.java b/src/main/java/com/destroystokyo/paper/event/executor/asm/ASMEventExecutorGenerator.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..084c31af1a7ba32bb4c3dc8f16f67fd09ce0b6a4
|
|
--- /dev/null
|
|
+++ b/src/main/java/com/destroystokyo/paper/event/executor/asm/ASMEventExecutorGenerator.java
|
|
@@ -0,0 +1,54 @@
|
|
+package com.destroystokyo.paper.event.executor.asm;
|
|
+
|
|
+import java.lang.reflect.Method;
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
+
|
|
+import org.bukkit.plugin.EventExecutor;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+import org.objectweb.asm.ClassWriter;
|
|
+import org.objectweb.asm.Type;
|
|
+import org.objectweb.asm.commons.GeneratorAdapter;
|
|
+
|
|
+import static org.objectweb.asm.Opcodes.*;
|
|
+
|
|
+public class ASMEventExecutorGenerator {
|
|
+
|
|
+ private static final String EXECUTE_DESCRIPTOR = "(Lorg/bukkit/event/Listener;Lorg/bukkit/event/Event;)V";
|
|
+
|
|
+ @NotNull
|
|
+ public static byte[] generateEventExecutor(@NotNull Method m, @NotNull String name) {
|
|
+ ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
|
+ writer.visit(V1_8, ACC_PUBLIC, name.replace('.', '/'), null, Type.getInternalName(Object.class), new String[] {Type.getInternalName(EventExecutor.class)});
|
|
+ // Generate constructor
|
|
+ GeneratorAdapter methodGenerator = new GeneratorAdapter(writer.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null), ACC_PUBLIC, "<init>", "()V");
|
|
+ methodGenerator.loadThis();
|
|
+ methodGenerator.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); // Invoke the super class (Object) constructor
|
|
+ methodGenerator.returnValue();
|
|
+ methodGenerator.endMethod();
|
|
+ // Generate the execute method
|
|
+ methodGenerator = new GeneratorAdapter(writer.visitMethod(ACC_PUBLIC, "execute", EXECUTE_DESCRIPTOR, null, null), ACC_PUBLIC, "execute", EXECUTE_DESCRIPTOR);
|
|
+ methodGenerator.loadArg(0);
|
|
+ methodGenerator.checkCast(Type.getType(m.getDeclaringClass()));
|
|
+ methodGenerator.loadArg(1);
|
|
+ methodGenerator.checkCast(Type.getType(m.getParameterTypes()[0]));
|
|
+ methodGenerator.visitMethodInsn(m.getDeclaringClass().isInterface() ? INVOKEINTERFACE : INVOKEVIRTUAL, Type.getInternalName(m.getDeclaringClass()), m.getName(), Type.getMethodDescriptor(m), m.getDeclaringClass().isInterface());
|
|
+ // The only purpose of this switch statement is to generate the correct pop instruction, should the event handler method return something other than void.
|
|
+ // Non-void event handlers will be unsupported in a future release.
|
|
+ switch (Type.getType(m.getReturnType()).getSize()) {
|
|
+ // case 0 is omitted because the only type that has size 0 is void - no pop instruction needed.
|
|
+ case 1 -> methodGenerator.pop(); // handles reference types and most primitives
|
|
+ case 2 -> methodGenerator.pop2(); // handles long and double
|
|
+ }
|
|
+ methodGenerator.returnValue();
|
|
+ methodGenerator.endMethod();
|
|
+ writer.visitEnd();
|
|
+ return writer.toByteArray();
|
|
+ }
|
|
+
|
|
+ public static AtomicInteger NEXT_ID = new AtomicInteger(1);
|
|
+ @NotNull
|
|
+ public static String generateName() {
|
|
+ int id = NEXT_ID.getAndIncrement();
|
|
+ return "com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor" + id;
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/com/destroystokyo/paper/event/executor/asm/ClassDefiner.java b/src/main/java/com/destroystokyo/paper/event/executor/asm/ClassDefiner.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..f79685b48bb581277a6891927988b6f7a4389dc4
|
|
--- /dev/null
|
|
+++ b/src/main/java/com/destroystokyo/paper/event/executor/asm/ClassDefiner.java
|
|
@@ -0,0 +1,34 @@
|
|
+package com.destroystokyo.paper.event.executor.asm;
|
|
+
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public interface ClassDefiner {
|
|
+
|
|
+ /**
|
|
+ * Returns if the defined classes can bypass access checks
|
|
+ *
|
|
+ * @return if classes bypass access checks
|
|
+ */
|
|
+ public default boolean isBypassAccessChecks() {
|
|
+ return false;
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Define a class
|
|
+ *
|
|
+ * @param parentLoader the parent classloader
|
|
+ * @param name the name of the class
|
|
+ * @param data the class data to load
|
|
+ * @return the defined class
|
|
+ * @throws ClassFormatError if the class data is invalid
|
|
+ * @throws NullPointerException if any of the arguments are null
|
|
+ */
|
|
+ @NotNull
|
|
+ public Class<?> defineClass(@NotNull ClassLoader parentLoader, @NotNull String name, @NotNull byte[] data);
|
|
+
|
|
+ @NotNull
|
|
+ public static ClassDefiner getInstance() {
|
|
+ return SafeClassDefiner.INSTANCE;
|
|
+ }
|
|
+
|
|
+}
|
|
diff --git a/src/main/java/com/destroystokyo/paper/event/executor/asm/SafeClassDefiner.java b/src/main/java/com/destroystokyo/paper/event/executor/asm/SafeClassDefiner.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..abcc966d8ee01d73c1d1480237ab46fa0ab55fdc
|
|
--- /dev/null
|
|
+++ b/src/main/java/com/destroystokyo/paper/event/executor/asm/SafeClassDefiner.java
|
|
@@ -0,0 +1,64 @@
|
|
+package com.destroystokyo.paper.event.executor.asm;
|
|
+
|
|
+import java.util.concurrent.ConcurrentMap;
|
|
+
|
|
+import com.google.common.base.Preconditions;
|
|
+
|
|
+import com.google.common.collect.MapMaker;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public class SafeClassDefiner implements ClassDefiner {
|
|
+ /* default */ static final SafeClassDefiner INSTANCE = new SafeClassDefiner();
|
|
+
|
|
+ private SafeClassDefiner() {}
|
|
+
|
|
+ private final ConcurrentMap<ClassLoader, GeneratedClassLoader> loaders = new MapMaker().weakKeys().makeMap();
|
|
+
|
|
+ @NotNull
|
|
+ @Override
|
|
+ public Class<?> defineClass(@NotNull ClassLoader parentLoader, @NotNull String name, @NotNull byte[] data) {
|
|
+ GeneratedClassLoader loader = loaders.computeIfAbsent(parentLoader, GeneratedClassLoader::new);
|
|
+ synchronized (loader.getClassLoadingLock(name)) {
|
|
+ Preconditions.checkState(!loader.hasClass(name), "%s already defined", name);
|
|
+ Class<?> c = loader.define(name, data);
|
|
+ assert c.getName().equals(name);
|
|
+ return c;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private static class GeneratedClassLoader extends ClassLoader {
|
|
+ static {
|
|
+ ClassLoader.registerAsParallelCapable();
|
|
+ }
|
|
+
|
|
+ protected GeneratedClassLoader(@NotNull ClassLoader parent) {
|
|
+ super(parent);
|
|
+ }
|
|
+
|
|
+ private Class<?> define(@NotNull String name, byte[] data) {
|
|
+ synchronized (getClassLoadingLock(name)) {
|
|
+ assert !hasClass(name);
|
|
+ Class<?> c = defineClass(name, data, 0, data.length);
|
|
+ resolveClass(c);
|
|
+ return c;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ @NotNull
|
|
+ public Object getClassLoadingLock(@NotNull String name) {
|
|
+ return super.getClassLoadingLock(name);
|
|
+ }
|
|
+
|
|
+ public boolean hasClass(@NotNull String name) {
|
|
+ synchronized (getClassLoadingLock(name)) {
|
|
+ try {
|
|
+ Class.forName(name);
|
|
+ return true;
|
|
+ } catch (ClassNotFoundException e) {
|
|
+ return false;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/org/bukkit/plugin/EventExecutor.java b/src/main/java/org/bukkit/plugin/EventExecutor.java
|
|
index a850f0780de05463fc0d3f9e15ff7f19d88b2aed..9026e108ccd3a88aee1267ee275137befa646455 100644
|
|
--- a/src/main/java/org/bukkit/plugin/EventExecutor.java
|
|
+++ b/src/main/java/org/bukkit/plugin/EventExecutor.java
|
|
@@ -5,9 +5,75 @@ import org.bukkit.event.EventException;
|
|
import org.bukkit.event.Listener;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
+// Paper start
|
|
+import java.lang.reflect.Method;
|
|
+import java.lang.reflect.Modifier;
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
+import java.util.concurrent.ConcurrentMap;
|
|
+import java.util.function.Function;
|
|
+
|
|
+import com.destroystokyo.paper.event.executor.MethodHandleEventExecutor;
|
|
+import com.destroystokyo.paper.event.executor.StaticMethodHandleEventExecutor;
|
|
+import com.destroystokyo.paper.event.executor.asm.ASMEventExecutorGenerator;
|
|
+import com.destroystokyo.paper.event.executor.asm.ClassDefiner;
|
|
+import com.google.common.base.Preconditions;
|
|
+// Paper end
|
|
+
|
|
/**
|
|
* Interface which defines the class for event call backs to plugins
|
|
*/
|
|
public interface EventExecutor {
|
|
public void execute(@NotNull Listener listener, @NotNull Event event) throws EventException;
|
|
+
|
|
+ // Paper start
|
|
+ ConcurrentMap<Method, Class<? extends EventExecutor>> eventExecutorMap = new ConcurrentHashMap<Method, Class<? extends EventExecutor>>() {
|
|
+ @NotNull
|
|
+ @Override
|
|
+ public Class<? extends EventExecutor> computeIfAbsent(@NotNull Method key, @NotNull Function<? super Method, ? extends Class<? extends EventExecutor>> mappingFunction) {
|
|
+ Class<? extends EventExecutor> executorClass = get(key);
|
|
+ if (executorClass != null)
|
|
+ return executorClass;
|
|
+
|
|
+ //noinspection SynchronizationOnLocalVariableOrMethodParameter
|
|
+ synchronized (key) {
|
|
+ executorClass = get(key);
|
|
+ if (executorClass != null)
|
|
+ return executorClass;
|
|
+
|
|
+ return super.computeIfAbsent(key, mappingFunction);
|
|
+ }
|
|
+ }
|
|
+ };
|
|
+
|
|
+ @NotNull
|
|
+ public static EventExecutor create(@NotNull Method m, @NotNull Class<? extends Event> eventClass) {
|
|
+ Preconditions.checkNotNull(m, "Null method");
|
|
+ Preconditions.checkArgument(m.getParameterCount() != 0, "Incorrect number of arguments %s", m.getParameterCount());
|
|
+ Preconditions.checkArgument(m.getParameterTypes()[0] == eventClass, "First parameter %s doesn't match event class %s", m.getParameterTypes()[0], eventClass);
|
|
+ ClassDefiner definer = ClassDefiner.getInstance();
|
|
+ if (Modifier.isStatic(m.getModifiers())) {
|
|
+ return new StaticMethodHandleEventExecutor(eventClass, m);
|
|
+ } else if (definer.isBypassAccessChecks() || Modifier.isPublic(m.getDeclaringClass().getModifiers()) && Modifier.isPublic(m.getModifiers())) {
|
|
+ // get the existing generated EventExecutor class for the Method or generate one
|
|
+ Class<? extends EventExecutor> executorClass = eventExecutorMap.computeIfAbsent(m, (__) -> {
|
|
+ String name = ASMEventExecutorGenerator.generateName();
|
|
+ byte[] classData = ASMEventExecutorGenerator.generateEventExecutor(m, name);
|
|
+ return definer.defineClass(m.getDeclaringClass().getClassLoader(), name, classData).asSubclass(EventExecutor.class);
|
|
+ });
|
|
+
|
|
+ try {
|
|
+ EventExecutor asmExecutor = executorClass.newInstance();
|
|
+ // Define a wrapper to conform to bukkit stupidity (passing in events that don't match and wrapper exception)
|
|
+ return (listener, event) -> {
|
|
+ if (!eventClass.isInstance(event)) return;
|
|
+ asmExecutor.execute(listener, event);
|
|
+ };
|
|
+ } catch (InstantiationException | IllegalAccessException e) {
|
|
+ throw new AssertionError("Unable to initialize generated event executor", e);
|
|
+ }
|
|
+ } else {
|
|
+ return new MethodHandleEventExecutor(eventClass, m);
|
|
+ }
|
|
+ }
|
|
+ // Paper end
|
|
}
|