diff --git a/cmd/land/process_test.go b/cmd/land/process_test.go index bbc939a..29d4c18 100644 --- a/cmd/land/process_test.go +++ b/cmd/land/process_test.go @@ -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) + } +} diff --git a/cmd/land/testdata/Tupfile b/cmd/land/testdata/Tupfile new file mode 100644 index 0000000..cb970cd --- /dev/null +++ b/cmd/land/testdata/Tupfile @@ -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 diff --git a/cmd/land/testdata/arbint.c b/cmd/land/testdata/arbint.c new file mode 100644 index 0000000..ac1cf21 --- /dev/null +++ b/cmd/land/testdata/arbint.c @@ -0,0 +1,7 @@ +void _start() {} + +__attribute__ ((visibility ("default"))) +int main() { + *(int*)0x200 = 255; + return 0; +} diff --git a/cmd/land/testdata/arbint.wasm b/cmd/land/testdata/arbint.wasm new file mode 100755 index 0000000..696d679 Binary files /dev/null and b/cmd/land/testdata/arbint.wasm differ diff --git a/cmd/land/testdata/struct.c b/cmd/land/testdata/struct.c new file mode 100644 index 0000000..ce2c5d9 --- /dev/null +++ b/cmd/land/testdata/struct.c @@ -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; +} diff --git a/cmd/land/testdata/struct.wasm b/cmd/land/testdata/struct.wasm new file mode 100755 index 0000000..543b3d1 Binary files /dev/null and b/cmd/land/testdata/struct.wasm differ