From 452f02e102092fa04d79a7690e7fa16bb999adab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 26 Sep 2020 11:29:57 +0200 Subject: [PATCH] Removed alignment asserts (broke tests). Fixed bounds checks in memory classes. --- .../cil/circuity/vm/device/memory/ByteBufferMemory.java | 4 ++-- .../li/cil/circuity/vm/device/memory/UnsafeMemory.java | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/li/cil/circuity/vm/device/memory/ByteBufferMemory.java b/src/main/java/li/cil/circuity/vm/device/memory/ByteBufferMemory.java index ff1f672b..b02ad38a 100644 --- a/src/main/java/li/cil/circuity/vm/device/memory/ByteBufferMemory.java +++ b/src/main/java/li/cil/circuity/vm/device/memory/ByteBufferMemory.java @@ -29,7 +29,7 @@ public class ByteBufferMemory implements PhysicalMemory { @Override public int load(final int offset, final int sizeLog2) throws MemoryAccessException { - if (offset < 0 || offset >= data.limit()) { + if (offset < 0 || offset >= data.limit() - (1 << sizeLog2)) { throw new LoadFaultException(offset); } switch (sizeLog2) { @@ -46,7 +46,7 @@ public class ByteBufferMemory implements PhysicalMemory { @Override public void store(final int offset, final int value, final int sizeLog2) throws MemoryAccessException { - if (offset < 0 || offset >= data.limit()) { + if (offset < 0 || offset >= data.limit() - (1 << sizeLog2)) { throw new StoreFaultException(offset); } switch (sizeLog2) { diff --git a/src/main/java/li/cil/circuity/vm/device/memory/UnsafeMemory.java b/src/main/java/li/cil/circuity/vm/device/memory/UnsafeMemory.java index 6ab0cedc..7274099a 100644 --- a/src/main/java/li/cil/circuity/vm/device/memory/UnsafeMemory.java +++ b/src/main/java/li/cil/circuity/vm/device/memory/UnsafeMemory.java @@ -52,17 +52,15 @@ public final class UnsafeMemory implements PhysicalMemory { @Override public int load(final int offset, final int sizeLog2) throws MemoryAccessException { - if (offset < 0 || offset >= size) { + if (offset < 0 || offset >= size - (1 << sizeLog2)) { throw new LoadFaultException(offset); } switch (sizeLog2) { case Sizes.SIZE_8_LOG2: return UNSAFE.getByte(address + offset); case Sizes.SIZE_16_LOG2: - assert (offset & 0b1) == 0; return UNSAFE.getShort(address + offset); case Sizes.SIZE_32_LOG2: - assert (offset & 0b11) == 0; return UNSAFE.getInt(address + offset); default: throw new IllegalArgumentException(); @@ -71,7 +69,7 @@ public final class UnsafeMemory implements PhysicalMemory { @Override public void store(final int offset, final int value, final int sizeLog2) throws MemoryAccessException { - if (offset < 0 || offset >= size) { + if (offset < 0 || offset >= size - (1 << sizeLog2)) { throw new LoadFaultException(offset); } switch (sizeLog2) { @@ -79,11 +77,9 @@ public final class UnsafeMemory implements PhysicalMemory { UNSAFE.putByte(address + offset, (byte) value); break; case Sizes.SIZE_16_LOG2: - assert (offset & 0b1) == 0; UNSAFE.putShort(address + offset, (short) value); break; case Sizes.SIZE_32_LOG2: - assert (offset & 0b11) == 0; UNSAFE.putInt(address + offset, value); break; default: