cmd/land: add struct test

This commit is contained in:
Cadey Ratio 2018-06-20 07:58:19 -07:00
parent a0805258e3
commit 8785581748
6 changed files with 64 additions and 0 deletions

View File

@ -1,8 +1,12 @@
package main
import (
"bytes"
"encoding/binary"
"os"
"testing"
"github.com/kr/pretty"
)
func testWasmFile(t *testing.T, fname string) *Process {
@ -77,3 +81,33 @@ func TestFileOps(t *testing.T) {
t.Fatalf("wanted \"Hello, world\", got: %q", string(data))
}
}
func TestArbint(t *testing.T) {
p := testWasmFile(t, "./testdata/arbint.wasm")
pretty.Println(p.vm.Memory()[0x200:0x204])
}
type structFromC struct {
Bar int32
Baz int32
}
func TestStruct(t *testing.T) {
p := testWasmFile(t, "./testdata/struct.wasm")
pretty.Println(p.vm.Memory()[0x200:0x208])
foo := structFromC{}
if err := binary.Read(bytes.NewReader(p.vm.Memory()[0x200:0x208]), binary.LittleEndian, &foo); err != nil {
t.Fatalf("can't read memory as the struct: %v", err)
}
t.Logf("%#v", foo)
if foo.Bar != 31337 {
t.Fatalf("wanted foo.bar to be 31337, got: %d", foo.Bar)
}
if foo.Baz != 255 {
t.Fatalf("wanted foo.baz to be 255, got: %d", foo.Baz)
}
}

7
cmd/land/testdata/Tupfile vendored Normal file
View File

@ -0,0 +1,7 @@
include_rules
CFLAGS += --target=wasm32-unknown-unknown-wasm
CFLAGS += --sysroot=~/code/wasmception/sysroot/
CFLAGS += -O0 -nostartfiles -Wl,--no-entry -nostdlib
: foreach *.c |> ~/code/wasmception/dist/bin/clang $(CFLAGS) %f -o %o |> %B.wasm

7
cmd/land/testdata/arbint.c vendored Normal file
View File

@ -0,0 +1,7 @@
void _start() {}
__attribute__ ((visibility ("default")))
int main() {
*(int*)0x200 = 255;
return 0;
}

BIN
cmd/land/testdata/arbint.wasm vendored Executable file

Binary file not shown.

16
cmd/land/testdata/struct.c vendored Normal file
View File

@ -0,0 +1,16 @@
struct Foo {
int bar;
int baz;
};
__attribute__ ((visibility ("default")))
int main() {
struct Foo foo;
foo.bar = 31337;
foo.baz = 255;
*(struct Foo*)0x200 = foo;
return 0;
}

BIN
cmd/land/testdata/struct.wasm vendored Executable file

Binary file not shown.