Added memory verification option

This commit is contained in:
mcrcortex
2024-09-15 19:54:00 +10:00
parent b9a3d18b56
commit 1c0e0cc666
8 changed files with 32 additions and 17 deletions

View File

@@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx1G
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.21
yarn_mappings=1.21+build.2
loader_version=0.15.11
loader_version=0.16.5
# Mod Properties
mod_version = 0.1.6-alpha

View File

@@ -72,6 +72,7 @@ public class RenderService<T extends AbstractSectionRenderer<J, ?>, J extends Vi
Arrays.stream(world.getMapper().getBiomeEntries()).forEach(this.modelService::addBiome);
world.getMapper().setBiomeCallback(this.modelService::addBiome);
/*
final int H_WIDTH = 1;
for (int x = -H_WIDTH; x <= H_WIDTH; x++) {
@@ -80,8 +81,8 @@ public class RenderService<T extends AbstractSectionRenderer<J, ?>, J extends Vi
this.nodeManager.insertTopLevelNode(WorldEngine.getWorldSectionId(4, x, y, z));
}
}
}
*/
}*/
router.watch(WorldEngine.getWorldSectionId(4, 0,0,0), 3);
}
public void setup(Camera camera) {

View File

@@ -1,12 +1,13 @@
package me.cortex.voxy.client.core.rendering.building;
import me.cortex.voxy.common.util.MemoryBuffer;
import me.cortex.voxy.commonImpl.VoxyCommon;
import java.util.Arrays;
//TODO: also have an AABB size stored
public final class BuiltSection {
public static final boolean VERIFY_BUILT_SECTION_OFFSETS = System.getProperty("voxy.verifyBuiltSectionOffsets", "true").equals("true");
public static final boolean VERIFY_BUILT_SECTION_OFFSETS = VoxyCommon.isVerificationFlagOn("verifyBuiltSectionOffsets");
public final long position;
public final int aabb;
public final MemoryBuffer geometryBuffer;

View File

@@ -1,11 +1,13 @@
package me.cortex.voxy.common.util;
import me.cortex.voxy.commonImpl.VoxyCommon;
import java.lang.ref.Cleaner;
public abstract class TrackedObject {
//TODO: maybe make this false? for performance overhead?
public static final boolean TRACK_OBJECT_ALLOCATIONS = System.getProperty("voxy.ensureTrackedObjectsAreFreed", "true").equals("true");
public static final boolean TRACK_OBJECT_ALLOCATION_STACKS = System.getProperty("voxy.trackObjectAllocationStacks", "true").equals("true");
public static final boolean TRACK_OBJECT_ALLOCATIONS = VoxyCommon.isVerificationFlagOn("ensureTrackedObjectsAreFreed");
public static final boolean TRACK_OBJECT_ALLOCATION_STACKS = VoxyCommon.isVerificationFlagOn("trackObjectAllocationStacks");
private final Ref ref;
protected TrackedObject() {

View File

@@ -6,6 +6,7 @@ import it.unimi.dsi.fastutil.longs.LongArrayList;
import me.cortex.voxy.common.util.MemoryBuffer;
import me.cortex.voxy.common.util.UnsafeUtil;
import me.cortex.voxy.common.world.other.Mapper;
import me.cortex.voxy.commonImpl.VoxyCommon;
import org.lwjgl.system.MemoryUtil;
import java.nio.ByteBuffer;
@@ -13,7 +14,8 @@ import java.nio.ByteBuffer;
import static org.lwjgl.util.zstd.Zstd.*;
public class SaveLoadSystem {
public static final boolean VERIFY_HASH_ON_LOAD = System.getProperty("voxy.verifySectionOnLoad", "true").equals("true");
public static final boolean VERIFY_HASH_ON_LOAD = VoxyCommon.isVerificationFlagOn("verifySectionHash");
public static final boolean VERIFY_MEMORY_ACCESS = VoxyCommon.isVerificationFlagOn("verifyMemoryAccess");
public static final int BIGGEST_SERIALIZED_SECTION_SIZE = 32 * 32 * 32 * 8 * 2 + 8;
public static int lin2z(int i) {//y,z,x
@@ -86,12 +88,15 @@ public class SaveLoadSystem {
public static boolean deserialize(WorldSection section, MemoryBuffer data) {
long ptr = data.address;
long key = MemoryUtil.memGetLong(ptr); ptr += 8;
long key = MemoryUtil.memGetLong(ptr); ptr += 8; if (VERIFY_MEMORY_ACCESS && data.size<=(ptr-data.address)) throw new IllegalStateException("Memory access OOB");
long metadata = MemoryUtil.memGetLong(ptr); ptr += 8;
long metadata = MemoryUtil.memGetLong(ptr); ptr += 8; if (VERIFY_MEMORY_ACCESS && data.size<=(ptr-data.address)) throw new IllegalStateException("Memory access OOB");
section.nonEmptyChildren = (byte) (metadata&0xFF);
int lutLen = MemoryUtil.memGetInt(ptr); ptr += 4;
int lutLen = MemoryUtil.memGetInt(ptr); ptr += 4; if (VERIFY_MEMORY_ACCESS && data.size<=(ptr-data.address)) throw new IllegalStateException("Memory access OOB");
if (lutLen > 32*32*32) {
throw new IllegalStateException("lutLen impossibly large, max size should be 32768 but got size " + lutLen);
}
long[] lut = new long[lutLen];
long hash = 0;
if (VERIFY_HASH_ON_LOAD) {
@@ -99,7 +104,7 @@ public class SaveLoadSystem {
hash ^= metadata; hash *= 1242629872171L;
}
for (int i = 0; i < lutLen; i++) {
lut[i] = MemoryUtil.memGetLong(ptr); ptr += 8;
lut[i] = MemoryUtil.memGetLong(ptr); ptr += 8; if (VERIFY_MEMORY_ACCESS && data.size<=(ptr-data.address)) throw new IllegalStateException("Memory access OOB");
if (VERIFY_HASH_ON_LOAD) {
hash *= 1230987149811L;
hash += 12831;
@@ -115,7 +120,7 @@ public class SaveLoadSystem {
int nonEmptyBlockCount = 0;
for (int i = 0; i < section.data.length; i++) {
long state = lut[MemoryUtil.memGetShort(ptr)]; ptr += 2;
long state = lut[MemoryUtil.memGetShort(ptr)]; ptr += 2; if (VERIFY_MEMORY_ACCESS && data.size<=(ptr-data.address)) throw new IllegalStateException("Memory access OOB");
nonEmptyBlockCount += Mapper.isAir(state)?0:1;
section.data[z2lin(i)] = state;
}
@@ -131,7 +136,7 @@ public class SaveLoadSystem {
}
hash ^= pHash;
long expectedHash = MemoryUtil.memGetLong(ptr); ptr += 8;
long expectedHash = MemoryUtil.memGetLong(ptr); ptr += 8; if (VERIFY_MEMORY_ACCESS && data.size<(ptr-data.address)) throw new IllegalStateException("Memory access OOB");
if (expectedHash != hash) {
//throw new IllegalStateException("Hash mismatch got: " + hash + " expected: " + expectedHash);
System.err.println("Hash mismatch got: " + hash + " expected: " + expectedHash + " removing region");

View File

@@ -2,6 +2,7 @@ package me.cortex.voxy.common.world;
import me.cortex.voxy.client.Voxy;
import me.cortex.voxy.commonImpl.VoxyCommon;
import net.minecraft.util.Pair;
import java.lang.invoke.MethodHandles;
@@ -14,7 +15,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
//Represents a loaded world section at a specific detail level
// holds a 32x32x32 region of detail
public final class WorldSection {
public static final boolean VERIFY_WORLD_SECTION_EXECUTION = System.getProperty("voxy.verifyWorldSectionExecution", "true").equals("true");
public static final boolean VERIFY_WORLD_SECTION_EXECUTION = VoxyCommon.isVerificationFlagOn("verifyWorldSectionExecution");
private static final VarHandle ATOMIC_STATE_HANDLE;

View File

@@ -1,10 +1,8 @@
package me.cortex.voxy.commonImpl;
import me.cortex.voxy.common.config.Serialization;
import me.cortex.voxy.common.thread.ServiceThreadPool;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
@@ -34,4 +32,11 @@ public class VoxyCommon implements ModInitializer {
public static void breakpoint() {
int breakpoint = 0;
}
//This is hardcoded like this because people do not understand what they are doing
private static final boolean GlobalVerificationDisableOverride = false;//System.getProperty("voxy.verificationDisableOverride", "false").equals("true");
public static boolean isVerificationFlagOn(String name) {
return (!GlobalVerificationDisableOverride) && System.getProperty("voxy."+name, "true").equals("true");
}
}

View File

@@ -8,7 +8,7 @@ layout(local_size_x=LOCAL_SIZE) in;//, local_size_y=1
void main() {
uint node = getCurrentNode();
if (node != SENTINAL_OUT_OF_BOUNDS) {
if (node != SENTINAL_OUT_OF_BOUNDS && queueIdx != 4) {
printf("GID:%d, NODE %d, %d, AA, %d, %d, %d, %d", gl_GlobalInvocationID.x, node, queueIdx, nodeQueueMetadata[queueIdx].x, nodeQueueMetadata[queueIdx].y, nodeQueueMetadata[queueIdx].z, nodeQueueMetadata[queueIdx].w);
pushNodesInit(1);
pushNode(node);