2020-05-06 11:48:49 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2019-12-13 17:22:16 +01:00
From: Aikar <aikar@aikar.co>
Date: Fri, 15 Feb 2019 01:08:19 -0500
Subject: [PATCH] Allow Saving of Oversized Chunks
The Minecraft World Region File format has a hard cap of 1MB per chunk.
This is due to the fact that the header of the file format only allocates
a single byte for sector count, meaning a maximum of 256 sectors, at 4k per sector.
This limit can be reached fairly easily with books, resulting in the chunk being unable
to save to the world. Worse off, is that nothing printed when this occured, and silently
performed a chunk rollback on next load.
This leads to security risk with duplication and is being actively exploited.
This patch catches the too large scenario, falls back and moves any large Entity
or Tile Entity into a new compound, and this compound is saved into a different file.
On Chunk Load, we check for oversized status, and if so, we load the extra file and
merge the Entities and Tile Entities from the oversized chunk back into the level to
then be loaded as normal.
Once a chunk is returned back to normal size, the oversized flag will clear, and no
extra data file will exist.
This fix maintains compatability with all existing Anvil Region Format tools as it
does not alter the save format. They will just not know about the extra entities.
This fix also maintains compatability if someone switches server jars to one without
this fix, as the data will remain in the oversized file. Once the server returns
to a jar with this fix, the data will be restored.
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/nbt/NBTCompressedStreamTools.java b/src/main/java/net/minecraft/nbt/NBTCompressedStreamTools.java
index f792ac1639e16522695082caed754b1c2cb333b9..9da608c9dde183ad813fa5b7643314ce05c85aa5 100644
--- a/src/main/java/net/minecraft/nbt/NBTCompressedStreamTools.java
+++ b/src/main/java/net/minecraft/nbt/NBTCompressedStreamTools.java
@@ -131,6 +131,7 @@ public class NBTCompressedStreamTools {
2019-12-13 17:22:16 +01:00
}
2020-08-25 04:22:08 +02:00
+ public static NBTTagCompound readNBT(DataInput datainput) throws IOException { return a(datainput); } // Paper - OBFHELPER
public static NBTTagCompound a(DataInput datainput) throws IOException {
return a(datainput, NBTReadLimiter.a);
2019-12-13 17:22:16 +01:00
}
2021-03-16 08:19:45 +01:00
@@ -151,6 +152,7 @@ public class NBTCompressedStreamTools {
2019-12-13 17:22:16 +01:00
}
}
+ public static void writeNBT(NBTTagCompound nbttagcompound, DataOutput dataoutput) throws IOException { a(nbttagcompound, dataoutput); } // Paper - OBFHELPER
public static void a(NBTTagCompound nbttagcompound, DataOutput dataoutput) throws IOException {
a((NBTBase) nbttagcompound, dataoutput);
}
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
2021-03-16 14:04:28 +01:00
index 6efbb8a3502f86e105d4dfb9cef114a790966e95..a9cbe17f6ccf0ce4ace97ba4b951b3fd7415d71b 100644
2021-03-16 08:19:45 +01:00
--- a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
2021-03-16 14:04:28 +01:00
@@ -19,8 +19,12 @@ import java.nio.file.LinkOption;
2021-03-16 08:19:45 +01:00
import java.nio.file.Path;
2019-12-13 17:22:16 +01:00
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
+import java.util.zip.InflaterInputStream; // Paper
+
import javax.annotation.Nullable;
2021-03-16 08:19:45 +01:00
import net.minecraft.SystemUtils;
2021-03-16 14:04:28 +01:00
+import net.minecraft.nbt.NBTCompressedStreamTools;
+import net.minecraft.nbt.NBTTagCompound;
2021-03-16 08:19:45 +01:00
import net.minecraft.world.level.ChunkCoordIntPair;
2021-03-16 14:04:28 +01:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -37,6 +41,7 @@ public class RegionFile implements AutoCloseable {
2020-08-25 04:22:08 +02:00
private final IntBuffer i;
@VisibleForTesting
protected final RegionFileBitSet freeSectors;
+ public final File file; // Paper
2019-12-13 17:22:16 +01:00
2020-06-25 16:09:55 +02:00
public RegionFile(File file, File file1, boolean flag) throws IOException {
this(file.toPath(), file1.toPath(), RegionFileCompression.b, flag);
2021-03-16 14:04:28 +01:00
@@ -44,6 +49,8 @@ public class RegionFile implements AutoCloseable {
2019-12-13 17:22:16 +01:00
2021-03-16 08:19:45 +01:00
public RegionFile(Path path, Path path1, RegionFileCompression regionfilecompression, boolean flag) throws IOException {
2020-08-25 04:22:08 +02:00
this.g = ByteBuffer.allocateDirect(8192);
2021-03-16 14:04:28 +01:00
+ this.file = path.toFile(); // Paper
2020-08-25 04:22:08 +02:00
+ initOversizedState(); // Paper
2019-12-13 17:22:16 +01:00
this.freeSectors = new RegionFileBitSet();
2020-08-25 04:22:08 +02:00
this.f = regionfilecompression;
2021-03-16 08:19:45 +01:00
if (!Files.isDirectory(path1, new LinkOption[0])) {
2021-03-16 14:04:28 +01:00
@@ -407,6 +414,74 @@ public class RegionFile implements AutoCloseable {
2019-12-13 17:22:16 +01:00
void run() throws IOException;
}
2020-08-25 04:22:08 +02:00
+ // Paper start
2019-12-13 17:22:16 +01:00
+ private final byte[] oversized = new byte[1024];
+ private int oversizedCount = 0;
+
+ private synchronized void initOversizedState() throws IOException {
+ File metaFile = getOversizedMetaFile();
+ if (metaFile.exists()) {
+ final byte[] read = java.nio.file.Files.readAllBytes(metaFile.toPath());
+ System.arraycopy(read, 0, oversized, 0, oversized.length);
+ for (byte temp : oversized) {
+ oversizedCount += temp;
+ }
+ }
+ }
+
+ private static int getChunkIndex(int x, int z) {
+ return (x & 31) + (z & 31) * 32;
+ }
+ synchronized boolean isOversized(int x, int z) {
+ return this.oversized[getChunkIndex(x, z)] == 1;
+ }
+ synchronized void setOversized(int x, int z, boolean oversized) throws IOException {
+ final int offset = getChunkIndex(x, z);
+ boolean previous = this.oversized[offset] == 1;
+ this.oversized[offset] = (byte) (oversized ? 1 : 0);
+ if (!previous && oversized) {
+ oversizedCount++;
+ } else if (!oversized && previous) {
+ oversizedCount--;
+ }
+ if (previous && !oversized) {
+ File oversizedFile = getOversizedFile(x, z);
+ if (oversizedFile.exists()) {
+ oversizedFile.delete();
+ }
+ }
+ if (oversizedCount > 0) {
+ if (previous != oversized) {
+ writeOversizedMeta();
+ }
+ } else if (previous) {
+ File oversizedMetaFile = getOversizedMetaFile();
+ if (oversizedMetaFile.exists()) {
+ oversizedMetaFile.delete();
+ }
+ }
+ }
+
+ private void writeOversizedMeta() throws IOException {
+ java.nio.file.Files.write(getOversizedMetaFile().toPath(), oversized);
+ }
+
+ private File getOversizedMetaFile() {
+ return new File(this.file.getParentFile(), this.file.getName().replaceAll("\\.mca$", "") + ".oversized.nbt");
+ }
+
+ private File getOversizedFile(int x, int z) {
+ return new File(this.file.getParentFile(), this.file.getName().replaceAll("\\.mca$", "") + "_oversized_" + x + "_" + z + ".nbt");
+ }
+
+ synchronized NBTTagCompound getOversizedData(int x, int z) throws IOException {
+ File file = getOversizedFile(x, z);
+ try (DataInputStream out = new DataInputStream(new BufferedInputStream(new InflaterInputStream(new java.io.FileInputStream(file))))) {
2020-08-25 04:22:08 +02:00
+ return NBTCompressedStreamTools.readNBT((java.io.DataInput) out);
2019-12-13 17:22:16 +01:00
+ }
+
+ }
+ // Paper end
class ChunkBuffer extends ByteArrayOutputStream {
private final ChunkCoordIntPair b;
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileCache.java b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileCache.java
2021-03-16 14:04:28 +01:00
index 75b10a3755392870d8f5b51239a09a0e7fd75a42..ab9f4d40fd1126a3d7ba5b16fdc6ab09de4a7fdb 100644
2021-03-16 08:19:45 +01:00
--- a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileCache.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileCache.java
2021-03-16 14:04:28 +01:00
@@ -9,8 +9,10 @@ import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import javax.annotation.Nullable;
+import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTCompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
+import net.minecraft.nbt.NBTTagList;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ExceptionSuppressor;
import net.minecraft.world.level.ChunkCoordIntPair;
@@ -50,6 +52,74 @@ public final class RegionFileCache implements AutoCloseable {
2019-12-13 17:22:16 +01:00
}
}
+ // Paper start
+ private static void printOversizedLog(String msg, File file, int x, int z) {
+ org.apache.logging.log4j.LogManager.getLogger().fatal(msg + " (" + file.toString().replaceAll(".+[\\\\/]", "") + " - " + x + "," + z + ") Go clean it up to remove this message. /minecraft:tp " + (x<<4)+" 128 "+(z<<4) + " - DO NOT REPORT THIS TO PAPER - You may ask for help on Discord, but do not file an issue. These error messages can not be removed.");
+ }
+
+ private static final int DEFAULT_SIZE_THRESHOLD = 1024 * 8;
+ private static final int OVERZEALOUS_TOTAL_THRESHOLD = 1024 * 64;
+ private static final int OVERZEALOUS_THRESHOLD = 1024;
+ private static int SIZE_THRESHOLD = DEFAULT_SIZE_THRESHOLD;
+ private static void resetFilterThresholds() {
+ SIZE_THRESHOLD = Math.max(1024 * 4, Integer.getInteger("Paper.FilterThreshhold", DEFAULT_SIZE_THRESHOLD));
+ }
+ static {
+ resetFilterThresholds();
+ }
+
+ static boolean isOverzealous() {
+ return SIZE_THRESHOLD == OVERZEALOUS_THRESHOLD;
+ }
+
+
+ private static NBTTagCompound readOversizedChunk(RegionFile regionfile, ChunkCoordIntPair chunkCoordinate) throws IOException {
+ synchronized (regionfile) {
+ try (DataInputStream datainputstream = regionfile.getReadStream(chunkCoordinate)) {
+ NBTTagCompound oversizedData = regionfile.getOversizedData(chunkCoordinate.x, chunkCoordinate.z);
2020-08-25 04:22:08 +02:00
+ NBTTagCompound chunk = NBTCompressedStreamTools.readNBT((DataInput) datainputstream);
2019-12-13 17:22:16 +01:00
+ if (oversizedData == null) {
+ return chunk;
+ }
+ NBTTagCompound oversizedLevel = oversizedData.getCompound("Level");
+ NBTTagCompound level = chunk.getCompound("Level");
+
+ mergeChunkList(level, oversizedLevel, "Entities");
+ mergeChunkList(level, oversizedLevel, "TileEntities");
+
+ chunk.set("Level", level);
+
+ return chunk;
+ } catch (Throwable throwable) {
+ throwable.printStackTrace();
+ throw throwable;
+ }
+ }
+ }
+
+ private static void mergeChunkList(NBTTagCompound level, NBTTagCompound oversizedLevel, String key) {
+ NBTTagList levelList = level.getList(key, 10);
+ NBTTagList oversizedList = oversizedLevel.getList(key, 10);
+
+ if (!oversizedList.isEmpty()) {
+ levelList.addAll(oversizedList);
+ level.set(key, levelList);
+ }
+ }
+
+ private static int getNBTSize(NBTBase nbtBase) {
+ DataOutputStream test = new DataOutputStream(new org.apache.commons.io.output.NullOutputStream());
+ try {
+ nbtBase.write(test);
+ return test.size();
+ } catch (IOException e) {
+ e.printStackTrace();
+ return 0;
+ }
+ }
+
+ // Paper End
+
@Nullable
public NBTTagCompound read(ChunkCoordIntPair chunkcoordintpair) throws IOException {
2020-04-27 09:34:45 +02:00
// CraftBukkit start - SPIGOT-5680: There's no good reason to preemptively create files on read, save that for writing
2021-03-16 14:04:28 +01:00
@@ -59,6 +129,12 @@ public final class RegionFileCache implements AutoCloseable {
2020-04-27 09:34:45 +02:00
}
// CraftBukkit end
2019-12-13 17:22:16 +01:00
DataInputStream datainputstream = regionfile.a(chunkcoordintpair);
+ // Paper start
+ if (regionfile.isOversized(chunkcoordintpair.x, chunkcoordintpair.z)) {
+ printOversizedLog("Loading Oversized Chunk!", regionfile.file, chunkcoordintpair.x, chunkcoordintpair.z);
+ return readOversizedChunk(regionfile, chunkcoordintpair);
+ }
+ // Paper end
Throwable throwable = null;
NBTTagCompound nbttagcompound;
2021-03-16 14:04:28 +01:00
@@ -99,6 +175,7 @@ public final class RegionFileCache implements AutoCloseable {
2019-12-13 17:22:16 +01:00
try {
NBTCompressedStreamTools.a(nbttagcompound, (DataOutput) dataoutputstream);
2020-08-25 04:22:08 +02:00
+ regionfile.setOversized(chunkcoordintpair.x, chunkcoordintpair.z, false); // Paper - We don't do this anymore, mojang stores differently, but clear old meta flag if it exists to get rid of our own meta file once last oversized is gone
2019-12-13 17:22:16 +01:00
} catch (Throwable throwable1) {
throwable = throwable1;
throw throwable1;