From 878558174888118f5b11199e8767cc62a691da2e Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 20 Jun 2018 07:58:19 -0700 Subject: [PATCH] cmd/land: add struct test --- cmd/land/process_test.go | 34 ++++++++++++++++++++++++++++++++++ cmd/land/testdata/Tupfile | 7 +++++++ cmd/land/testdata/arbint.c | 7 +++++++ cmd/land/testdata/arbint.wasm | Bin 0 -> 200 bytes cmd/land/testdata/struct.c | 16 ++++++++++++++++ cmd/land/testdata/struct.wasm | Bin 0 -> 235 bytes 6 files changed, 64 insertions(+) create mode 100644 cmd/land/testdata/Tupfile create mode 100644 cmd/land/testdata/arbint.c create mode 100755 cmd/land/testdata/arbint.wasm create mode 100644 cmd/land/testdata/struct.c create mode 100755 cmd/land/testdata/struct.wasm 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 0000000000000000000000000000000000000000..696d679cf1150e5af2f3dba5d064215c39bb5b47 GIT binary patch literal 200 zcmX|*v2MaZ5JYEo&j#U0Ns2U7ccP_slj^=?xxh(8_zc2Gh{`Vc>%>ZtXk&r)x(1W7#aWt}eNpuMa_EcFgHwUunq7mDwTPlI8xO z;!67N!PTeCTFZLeO!E*B-vT{6PeCGr#>oX2e;6t+UR!zb&R4C#>uZDH`cyL4f_Jm~ ReA)J?>ui5JUkCmbegR;6C{F+Y literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..543b3d10e944872e5d36bda5594ab98ea0d6bc4f GIT binary patch literal 235 zcmX|+yG{c!6hvpPy$@xPfP#j~78MfpQu{M#xG{+(5b-9;MnW`rOMaD}9xMu`In61~ zAkLWp*vlGNgT)vDZNme}#t_n8nZmGq+M(bd{m|XnHmAHlz6%D`=M)e66z62PQr*T= z)HH34*c@LbaP=rvdoW$$2g}V;2aj;!>EQV4CBPde-i9CgCVYL