Skip to content

Commit

Permalink
Merge pull request #35 from dylibso/fix-memory-grow-and-size
Browse files Browse the repository at this point in the history
Fix memory.grow and memory.size
  • Loading branch information
bhelx authored Oct 13, 2023
2 parents 5d9cc17 + a5e75ac commit 17245b4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ void eval(List<Instruction> code) throws ChicoryException {
}
case MEMORY_GROW:
{
instance.getMemory().grow();
var size = stack.pop().asInt();
var nPages = instance.getMemory().grow(size);
// TODO add -1 to stack if memory is exhausted
stack.push(Value.i32(nPages));
break;
}
case I32_STORE8:
Expand All @@ -364,7 +367,7 @@ void eval(List<Instruction> code) throws ChicoryException {
}
case MEMORY_SIZE:
{
var sz = instance.getMemory().getInitialSize();
var sz = instance.getMemory().getSize();
this.stack.push(Value.i32(sz));
break;
}
Expand Down
25 changes: 17 additions & 8 deletions runtime/src/main/java/com/dylibso/chicory/runtime/Memory.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,27 @@ public Memory(MemoryLimits limits, DataSegment[] dataSegments) {
this.reinstantiate();
}

public void grow() {
/**
* Gets the size of the memory in number of pages
*/
public int getSize() {
return nPages;
}

public int grow(int size) {
int numPages = this.nPages + size;
// TODO if max is null then we just let it grow as much as it wants?
if (limits.getMaximum() != null && this.nPages > limits.getMaximum())
if (limits.getMaximum() != null && numPages >= limits.getMaximum())
throw new RuntimeException("Program exceeded max pages: " + limits.getMaximum());
var capacity = this.buffer.capacity() + PAGE_SIZE;
var capacity = buffer.capacity() + (PAGE_SIZE * size);
var result = ByteBuffer.allocate(capacity).order(ByteOrder.LITTLE_ENDIAN);
var position = this.buffer.position();
this.buffer.rewind();
result.put(this.buffer);
var position = buffer.position();
buffer.rewind();
result.put(buffer);
result.position(position);
this.buffer = result;
this.nPages++;
buffer = result;
nPages = numPages;
return nPages;
}

public int getInitialSize() {
Expand Down
2 changes: 1 addition & 1 deletion wasm/scripts/instructions.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ i32.store16 <varuint> <varuint> $3B
i64.store8 <varuint> <varuint> $3C
i64.store16 <varuint> <varuint> $3D
i64.store32 <varuint> <varuint> $3E
memory.size $3F
memory.size <varuint> $3F
memory.grow <varuint> $40
i32.const <varsint32> $41
i64.const <varsint64> $42
Expand Down
4 changes: 2 additions & 2 deletions wasm/src/main/java/com/dylibso/chicory/wasm/types/OpCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public enum OpCode {
I64_STORE8(0x3C), // i64.store8 <varuint> <varuint>
I64_STORE16(0x3D), // i64.store16 <varuint> <varuint>
I64_STORE32(0x3E), // i64.store32 <varuint> <varuint>
MEMORY_SIZE(0x3F), // memory.size
MEMORY_SIZE(0x3F), // memory.size <varuint>
MEMORY_GROW(0x40), // memory.grow <varuint>
I32_CONST(0x41), // i32.const <varsint32>
I64_CONST(0x42), // i64.const <varsint64>
Expand Down Expand Up @@ -262,7 +262,7 @@ public static WasmEncoding[] getSignature(OpCode o) {
signature.put(I64_STORE8, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT});
signature.put(I64_STORE16, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT});
signature.put(I64_STORE32, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT});
signature.put(MEMORY_SIZE, new WasmEncoding[] {});
signature.put(MEMORY_SIZE, new WasmEncoding[] {WasmEncoding.VARUINT});
signature.put(MEMORY_GROW, new WasmEncoding[] {WasmEncoding.VARUINT});
signature.put(I32_CONST, new WasmEncoding[] {WasmEncoding.VARSINT32});
signature.put(I64_CONST, new WasmEncoding[] {WasmEncoding.VARSINT64});
Expand Down

0 comments on commit 17245b4

Please sign in to comment.