Skip to content

Commit

Permalink
Defensively copy chunk data in upgradeChunkTag
Browse files Browse the repository at this point in the history
The data provided is always from the regionfile thread, which
does not copy the data out. So if two separate calls need
the data, then there's going to be a problem.
  • Loading branch information
Spottedleaf committed Dec 27, 2021
1 parent 692283d commit 22be4d5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
14 changes: 11 additions & 3 deletions patches/server/0258-Asynchronous-chunk-IO-and-loading.patch
Original file line number Diff line number Diff line change
Expand Up @@ -3170,7 +3170,7 @@ index 1729af83b979e35a585c8d049b14dc23ddf23803..89de1589833dcce8028fd402aea8a3e5
CompoundTag nbttagcompound3 = new CompoundTag();
Iterator iterator1 = chunk.getHeightmaps().iterator();
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
index 2d1c8d5cf8ea6739d5e9df91cc16cde72917feff..d7fab2fb61dc3de14e382bac6127db956605e7ad 100644
index 2d1c8d5cf8ea6739d5e9df91cc16cde72917feff..ba677f282f2c8a05d1bad88226655549a81679bb 100644
--- a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
@@ -25,27 +25,38 @@ import net.minecraft.world.level.storage.DimensionDataStorage;
Expand Down Expand Up @@ -3218,7 +3218,15 @@ index 2d1c8d5cf8ea6739d5e9df91cc16cde72917feff..d7fab2fb61dc3de14e382bac6127db95
if (nbt != null) {
CompoundTag level = nbt.getCompound("Level");
if (level.getBoolean("TerrainPopulated")) {
@@ -80,11 +91,13 @@ public class ChunkStorage implements AutoCloseable {
@@ -63,6 +74,7 @@ public class ChunkStorage implements AutoCloseable {

public CompoundTag upgradeChunkTag(ResourceKey<LevelStem> resourcekey, Supplier<DimensionDataStorage> supplier, CompoundTag nbttagcompound, Optional<ResourceKey<Codec<? extends ChunkGenerator>>> optional, ChunkPos pos, @Nullable LevelAccessor generatoraccess) throws IOException {
// CraftBukkit end
+ nbttagcompound = nbttagcompound.copy(); // Paper - defensive copy, another thread might modify this
int i = ChunkStorage.getVersion(nbttagcompound);

// CraftBukkit start
@@ -80,11 +92,13 @@ public class ChunkStorage implements AutoCloseable {
if (i < 1493) {
nbttagcompound = NbtUtils.update(this.fixerUpper, DataFixTypes.CHUNK, nbttagcompound, i, 1493);
if (nbttagcompound.getCompound("Level").getBoolean("hasLegacyStructureData")) {
Expand All @@ -3232,7 +3240,7 @@ index 2d1c8d5cf8ea6739d5e9df91cc16cde72917feff..d7fab2fb61dc3de14e382bac6127db95
}
}

@@ -129,26 +142,39 @@ public class ChunkStorage implements AutoCloseable {
@@ -129,26 +143,39 @@ public class ChunkStorage implements AutoCloseable {

@Nullable
public CompoundTag read(ChunkPos chunkPos) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ index 3e631d55d30831a4063e23f9dbc7a315d11a7b68..cf86755050632b158576849b78607978
if (!Objects.equals(chunkPos, chunkcoordintpair1)) {
ChunkSerializer.LOGGER.error("Chunk file at {} is in the wrong location; relocating. (Expected {}, got {})", chunkPos, chunkPos, chunkcoordintpair1);
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
index d7fab2fb61dc3de14e382bac6127db956605e7ad..b1b1fa19cfd533d5625a462af399c5fd055629b0 100644
index ba677f282f2c8a05d1bad88226655549a81679bb..392fd2f5919d6a8f37c9f2b0421831d6ce381e0c 100644
--- a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
@@ -147,6 +147,13 @@ public class ChunkStorage implements AutoCloseable {
@@ -148,6 +148,13 @@ public class ChunkStorage implements AutoCloseable {

// Paper start - async chunk io
public void write(ChunkPos chunkPos, CompoundTag nbt) throws IOException {
Expand Down
8 changes: 4 additions & 4 deletions patches/server/0778-Rewrite-dataconverter-system.patch
Original file line number Diff line number Diff line change
Expand Up @@ -21609,10 +21609,10 @@ index 0000000000000000000000000000000000000000..967ad1186cbc81a76a4958ea99d4eff3
+ }
+}
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
index d44154ba43e06934d7889f2f20d1a27765504574..f8167882a0f11c6fff86e494800864ecf59bb8b5 100644
index a5a45118644fc1bc8caeefa59b5f897ed590e7ae..8dfac195e7f03f8fe18ba6ee79e195efade46bea 100644
--- a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
@@ -78,7 +78,7 @@ public class ChunkStorage implements AutoCloseable {
@@ -79,7 +79,7 @@ public class ChunkStorage implements AutoCloseable {
int i = ChunkStorage.getVersion(nbttagcompound);

// CraftBukkit start
Expand All @@ -21621,7 +21621,7 @@ index d44154ba43e06934d7889f2f20d1a27765504574..f8167882a0f11c6fff86e494800864ec
CompoundTag level = nbttagcompound.getCompound("Level");
if (level.getBoolean("TerrainPopulated") && !level.getBoolean("LightPopulated")) {
ServerChunkCache cps = (generatoraccess == null) ? null : ((ServerLevel) generatoraccess).getChunkSource();
@@ -90,7 +90,7 @@ public class ChunkStorage implements AutoCloseable {
@@ -91,7 +91,7 @@ public class ChunkStorage implements AutoCloseable {
// CraftBukkit end

if (i < 1493) {
Expand All @@ -21630,7 +21630,7 @@ index d44154ba43e06934d7889f2f20d1a27765504574..f8167882a0f11c6fff86e494800864ec
if (nbttagcompound.getCompound("Level").getBoolean("hasLegacyStructureData")) {
synchronized (this.persistentDataLock) { // Paper - Async chunk loading
if (this.legacyStructureHandler == null) {
@@ -112,7 +112,7 @@ public class ChunkStorage implements AutoCloseable {
@@ -113,7 +113,7 @@ public class ChunkStorage implements AutoCloseable {
// Spigot end

ChunkStorage.injectDatafixingContext(nbttagcompound, resourcekey, optional);
Expand Down

0 comments on commit 22be4d5

Please sign in to comment.