cmd/land: add struct test
This commit is contained in:
parent
a0805258e3
commit
8785581748
|
@ -1,8 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kr/pretty"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testWasmFile(t *testing.T, fname string) *Process {
|
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))
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
|
@ -0,0 +1,7 @@
|
||||||
|
void _start() {}
|
||||||
|
|
||||||
|
__attribute__ ((visibility ("default")))
|
||||||
|
int main() {
|
||||||
|
*(int*)0x200 = 255;
|
||||||
|
return 0;
|
||||||
|
}
|
Binary file not shown.
|
@ -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;
|
||||||
|
}
|
Binary file not shown.
Loading…
Reference in New Issue