Skip to content

Commit

Permalink
Fix memory (#45)
Browse files Browse the repository at this point in the history
* better observability

* load is the culprit

* little-endian
  • Loading branch information
ahuoguo authored Oct 6, 2024
1 parent a8781d4 commit ea20ef7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 2,653 deletions.
2,635 changes: 0 additions & 2,635 deletions benchmarks/wasm/btree/2o1u-no-label.wat

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@
i32.store offset=4
end)
(func (;3;) (type 2) (param i32 i32)
(local i32)
(local i32 i32)
local.get 0
i32.load offset=4
i32.const 1
Expand Down Expand Up @@ -2631,5 +2631,5 @@
i32.const 1
call 6)
(memory (;0;) 2)
(export "main" (func 6))
(export "main" (func 7))
(start 7))
10 changes: 5 additions & 5 deletions benchmarks/wasm/btree/2o1u.wat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(module
;; (import "env" "log" (func $log (param i32)))
(memory $0 2)
(func $createBtree (param i32) (result i32) ;; createBtree(t), where t: degree of the btree
(i32.const 0)
Expand Down Expand Up @@ -519,7 +520,7 @@
)
)
;; btreeInsertNonFull(x, k), where x: addr of a non full internal node; k: the key to insert
(func $btreeInsertNonFull (param i32) (param i32) (local i32)
(func $btreeInsertNonFull (param i32) (param i32) (local i32) (local $tmp i32)
(local.get 0) ;; x
(i32.load offset=4) ;; x.n
(i32.const 1)
Expand Down Expand Up @@ -2809,13 +2810,12 @@
(i32.and)
(drop)
)
(export "main" (func $main))
(export "main" (func $real_main))
(func $real_main
i32.const 1
i32.const 3
i32.const 2
i32.const 1
call $main
)
(start $real_main)
(start $real_main)
)
)
19 changes: 19 additions & 0 deletions benchmarks/wasm/btree/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require("fs");

async function main() {
const bytes = fs.readFileSync("2o1u.wasm");
const env = { log: val => console.log(`logged ${val}`), };
const { instance } = await WebAssembly.instantiate(
bytes,
{
env: {
log(val) {
console.log(`log saw ${val}`);
}
}
}
);
instance.exports.real_main();
console.log(`finished`);
}
main();
19 changes: 19 additions & 0 deletions benchmarks/wasm/load.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(module
(type (;0;) (func (result i32)))
(type (;1;) (func))
(func (;0;) (type 0) (result i32)
i32.const 0
i32.const 1
i32.store
i32.const 0
i32.load
)
(func (;1;) (type 1)
call 0
;; should be 65536
;; drop
)
(start 1)
(memory (;0;) 2)
(export "main" (func 1))
)
4 changes: 2 additions & 2 deletions src/main/scala/wasm/Memory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ case class RTMemory(var memType: RTMemoryType, data: ArrayBuffer[Byte]) {
def loadn(a: Long, o: Int, n: Int): Long = {
assert(n > 0 && n <= 8)
var result: Long = 0
for (i <- 0 until n) {
for (i <- (n - 1) to 0 by -1) { // Little-endian: start from least significant byte
result = (result << 8) | (loadByte(a + i) & 0xff)
}
result
Expand All @@ -43,7 +43,7 @@ case class RTMemory(var memType: RTMemoryType, data: ArrayBuffer[Byte]) {
assert(n > 0 && n <= 8)
var temp = x
for (i <- 0 until n) {
storeByte(a + i, (temp & 0xff).toByte)
storeByte(a + i, (temp & 0xff).toByte) // Little-endian: store least significant byte first
temp = temp >> 8
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/wasm/MiniWasm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ object Evaluator {
val inst = insts.head
val rest = insts.tail

//println(f"stack size: ${stack.size}")
//println(s"eval: $inst")
// println(s"inst: ${inst} \t | ${frame.locals} | ${stack.reverse}" )

inst match {
case Drop => eval(rest, stack.tail, frame, kont, trail, ret)
case Select(_) =>
Expand Down
13 changes: 6 additions & 7 deletions src/test/scala/genwasym/TestEval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import org.scalatest.FunSuite
class TestEval extends FunSuite {

// Mostly testing the files generated form `benchmarks/wasm/test.rs`
def testFile(filename: String, main: Option[String] = None, expected: Option[Int] = None) = {
def testFile(filename: String,
main: Option[String] = None,
expected: Option[Int] = None) = {
val module = Parser.parseFile(filename)
//println(module)
val haltK: Evaluator.Cont[Unit] = stack => {
Expand All @@ -39,12 +41,9 @@ class TestEval extends FunSuite {
test("loop") { testFile("./benchmarks/wasm/loop.wat", None, Some(10)) }
test("even-odd") { testFile("./benchmarks/wasm/even_odd.wat", None, Some(1)) }
test("return") { testFile("./benchmarks/wasm/return.wat", None, None) }

test("load") { testFile("./benchmarks/wasm/load.wat", None, Some(1)) }
test("btree") { testFile("./benchmarks/wasm/btree/2o1u-unlabeled.wat") }
test("fib") { testFile("./benchmarks/wasm/fib.wat", None, Some(144)) }

// Parser works, but the memory issue remains
// test("btree") { testFile("./benchmarks/wasm/btree/2o1u-no-label-for-real.wat") }

// TODO: add more wasm spec tests?
// test("memory") { test_btree("./benchmarks/wasm/spectest/test.wat", "$real_main") }
// TODO: add wasm spec tests? How to utilize wast files?
}

0 comments on commit ea20ef7

Please sign in to comment.