From b74c4d4587720c74d647b429d4c9b2c83c52edae Mon Sep 17 00:00:00 2001 From: Owen <23108066+Owen1212055@users.noreply.github.com> Date: Fri, 29 Jul 2022 19:16:26 -0400 Subject: [PATCH] Warn on plugins accessing faraway chunks (#8208) --- patches/server/0007-MC-Utils.patch | 30 ++++++ ...-on-plugins-accessing-faraway-chunks.patch | 96 +++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 patches/server/0926-Warn-on-plugins-accessing-faraway-chunks.patch diff --git a/patches/server/0007-MC-Utils.patch b/patches/server/0007-MC-Utils.patch index 8f59a33c8..271f065ad 100644 --- a/patches/server/0007-MC-Utils.patch +++ b/patches/server/0007-MC-Utils.patch @@ -3009,6 +3009,36 @@ index 0000000000000000000000000000000000000000..cea9c098ade00ee87b8efc8164ab72f5 + return this.sum; + } +} +diff --git a/src/main/java/io/papermc/paper/util/StackWalkerUtil.java b/src/main/java/io/papermc/paper/util/StackWalkerUtil.java +new file mode 100644 +index 0000000000000000000000000000000000000000..f7114d5b8f2f93f62883e24da29afaf9f74ee1a6 +--- /dev/null ++++ b/src/main/java/io/papermc/paper/util/StackWalkerUtil.java +@@ -0,0 +1,24 @@ ++package io.papermc.paper.util; ++ ++import org.bukkit.plugin.java.JavaPlugin; ++import org.bukkit.plugin.java.PluginClassLoader; ++import org.jetbrains.annotations.Nullable; ++ ++import java.util.Optional; ++ ++public class StackWalkerUtil { ++ ++ @Nullable ++ public static JavaPlugin getFirstPluginCaller() { ++ Optional foundFrame = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE) ++ .walk(stream -> stream ++ .filter(frame -> frame.getDeclaringClass().getClassLoader() instanceof PluginClassLoader) ++ .map((frame) -> { ++ PluginClassLoader classLoader = (PluginClassLoader) frame.getDeclaringClass().getClassLoader(); ++ return classLoader.getPlugin(); ++ }) ++ .findFirst()); ++ ++ return foundFrame.orElse(null); ++ } ++} diff --git a/src/main/java/io/papermc/paper/util/WorldUtil.java b/src/main/java/io/papermc/paper/util/WorldUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..67bb91fcfb532a919954cd9d7733d09a6c3fec35 diff --git a/patches/server/0926-Warn-on-plugins-accessing-faraway-chunks.patch b/patches/server/0926-Warn-on-plugins-accessing-faraway-chunks.patch new file mode 100644 index 000000000..069ec6a90 --- /dev/null +++ b/patches/server/0926-Warn-on-plugins-accessing-faraway-chunks.patch @@ -0,0 +1,96 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Owen1212055 <23108066+Owen1212055@users.noreply.github.com> +Date: Fri, 29 Jul 2022 12:35:19 -0400 +Subject: [PATCH] Warn on plugins accessing faraway chunks + + +diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java +index 9467ccaa1d73e1913495a46919aee530e749977d..5a2a1d394852d39ea576624586f7fa736dec807c 100644 +--- a/src/main/java/net/minecraft/world/level/Level.java ++++ b/src/main/java/net/minecraft/world/level/Level.java +@@ -418,7 +418,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable { + } + + private static boolean isInWorldBoundsHorizontal(BlockPos pos) { +- return pos.getX() >= -30000000 && pos.getZ() >= -30000000 && pos.getX() < 30000000 && pos.getZ() < 30000000; ++ return pos.getX() >= -30000000 && pos.getZ() >= -30000000 && pos.getX() < 30000000 && pos.getZ() < 30000000; // Dif on change + } + + private static boolean isOutsideSpawnableHeight(int y) { +diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +index a5d8dfc77475845be7c6d37eed04fb19eeef1c0c..f0b14914438840bd819fa7da8b76f4fcc13704d0 100644 +--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java ++++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +@@ -314,9 +314,24 @@ public class CraftWorld extends CraftRegionAccessor implements World { + public boolean setSpawnLocation(int x, int y, int z) { + return this.setSpawnLocation(x, y, z, 0.0F); + } ++ // Paper start ++ private static void warnUnsafeChunk(String reason, int x, int z) { ++ // if any chunk coord is outside of 30 million blocks ++ if (x > 1875000 || z > 1875000 || x < -1875000 || z < -1875000) { ++ Plugin plugin = io.papermc.paper.util.StackWalkerUtil.getFirstPluginCaller(); ++ if (plugin != null) { ++ plugin.getLogger().warning("Plugin is %s at (%s, %s), this might cause issues.".formatted(reason, x, z)); ++ } ++ if (net.minecraft.server.MinecraftServer.getServer().isDebugging()) { ++ io.papermc.paper.util.TraceUtil.dumpTraceForThread("Dangerous chunk retrieval"); ++ } ++ } ++ } ++ // Paper end + + @Override + public Chunk getChunkAt(int x, int z) { ++ warnUnsafeChunk("getting a faraway chunk", x, z); // Paper + // Paper start - add ticket to hold chunk for a little while longer if plugin accesses it + net.minecraft.world.level.chunk.LevelChunk chunk = world.getChunkSource().getChunkAtIfLoadedImmediately(x, z); + if (chunk == null) { +@@ -432,6 +447,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { + @Override + public boolean regenerateChunk(int x, int z) { + org.spigotmc.AsyncCatcher.catchOp("chunk regenerate"); // Spigot ++ warnUnsafeChunk("regenerating a faraway chunk", x, z); // Paper + // Paper start - implement regenerateChunk method + final ServerLevel serverLevel = this.world; + final net.minecraft.server.level.ServerChunkCache serverChunkCache = serverLevel.getChunkSource(); +@@ -525,6 +541,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { + @Override + public boolean loadChunk(int x, int z, boolean generate) { + org.spigotmc.AsyncCatcher.catchOp("chunk load"); // Spigot ++ warnUnsafeChunk("loading a faraway chunk", x, z); // Paper + // Paper start - Optimize this method + ChunkPos chunkPos = new ChunkPos(x, z); + ChunkAccess immediate = world.getChunkSource().getChunkAtIfLoadedImmediately(x, z); // Paper +@@ -589,6 +606,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { + + @Override + public boolean addPluginChunkTicket(int x, int z, Plugin plugin) { ++ warnUnsafeChunk("adding a faraway chunk ticket", x, z); // Paper + Preconditions.checkArgument(plugin != null, "null plugin"); + Preconditions.checkArgument(plugin.isEnabled(), "plugin is not enabled"); + +@@ -670,6 +688,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { + + @Override + public void setChunkForceLoaded(int x, int z, boolean forced) { ++ warnUnsafeChunk("forceloading a faraway chunk", x, z); // Paper + this.getHandle().setChunkForced(x, z, forced); + } + +@@ -981,6 +1000,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { + + @Override + public int getHighestBlockYAt(int x, int z, org.bukkit.HeightMap heightMap) { ++ warnUnsafeChunk("getting a faraway chunk", x >> 4, z >> 4); // Paper + // Transient load for this tick + return this.world.getChunk(x >> 4, z >> 4).getHeight(CraftHeightMap.toNMS(heightMap), x, z); + } +@@ -2238,6 +2258,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { + // Paper start + @Override + public java.util.concurrent.CompletableFuture getChunkAtAsync(int x, int z, boolean gen, boolean urgent) { ++ warnUnsafeChunk("getting a faraway chunk async", x, z); // Paper + if (Bukkit.isPrimaryThread()) { + net.minecraft.world.level.chunk.LevelChunk immediate = this.world.getChunkSource().getChunkAtIfLoadedImmediately(x, z); + if (immediate != null) {