land/cmd/land/process.go

344 lines
6.4 KiB
Go
Raw Permalink Normal View History

2018-06-07 22:31:01 +00:00
package main
import (
gcontext "context"
2018-06-08 00:08:00 +00:00
"errors"
"io"
2018-06-18 01:48:10 +00:00
"log"
2018-06-07 22:31:01 +00:00
"os"
"reflect"
2018-06-18 01:16:24 +00:00
"strings"
2018-06-07 22:31:01 +00:00
"github.com/Xe/ln"
"github.com/go-interpreter/wagon/exec"
"github.com/go-interpreter/wagon/validate"
"github.com/go-interpreter/wagon/wasm"
2018-06-18 00:38:05 +00:00
"github.com/spf13/afero"
2018-06-07 22:31:01 +00:00
)
2018-06-08 00:08:00 +00:00
// Process is a larger level wrapper around a webassembly VM that gives it
// system call access.
2018-06-07 22:31:01 +00:00
type Process struct {
2018-06-18 01:16:24 +00:00
id int32
vm *exec.VM
mod *wasm.Module
fs afero.Fs
2018-06-18 00:38:05 +00:00
files []afero.File
2018-06-18 01:48:10 +00:00
name string
2018-08-07 01:04:11 +00:00
// go runtime cruft
goRuntimeValues []interface{}
2018-06-07 22:31:01 +00:00
}
2018-06-08 00:08:00 +00:00
// NewProcess constructs a new webassembly process based on the input webassembly module as a reader.
2018-06-18 01:48:10 +00:00
func NewProcess(fin io.Reader, name string) (*Process, error) {
2018-06-08 00:08:00 +00:00
p := &Process{}
mod, err := wasm.ReadModule(fin, p.importer)
if err != nil {
return nil, err
}
if mod.Memory == nil {
return nil, errors.New("must declare a memory, sorry :(")
}
vm, err := exec.NewVM(mod)
if err != nil {
return nil, err
2018-06-07 22:31:01 +00:00
}
2018-06-08 00:08:00 +00:00
p.mod = mod
p.vm = vm
2018-06-18 01:16:24 +00:00
p.fs = afero.NewMemMapFs()
2018-06-08 00:08:00 +00:00
return p, nil
2018-06-07 22:31:01 +00:00
}
2018-06-08 00:08:00 +00:00
func (p *Process) importer(name string) (*wasm.Module, error) {
2018-06-07 22:31:01 +00:00
switch name {
case "env":
m := wasm.NewModule()
m.Types = &wasm.SectionTypes{
Entries: []wasm.FunctionSig{
{
Form: 0,
ParamTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32},
},
2018-06-18 01:16:24 +00:00
{
Form: 0,
ParamTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32},
ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32},
},
2018-06-18 01:48:10 +00:00
{
Form: 0,
ParamTypes: []wasm.ValueType{wasm.ValueTypeI32},
ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32},
},
2018-06-07 22:31:01 +00:00
},
}
m.FunctionIndexSpace = []wasm.Function{
{
Sig: &m.Types.Entries[0],
Host: reflect.ValueOf(p.log),
Body: &wasm.FunctionBody{},
},
2018-06-18 01:16:24 +00:00
{
Sig: &m.Types.Entries[0],
Host: reflect.ValueOf(p.open),
Body: &wasm.FunctionBody{},
},
{
Sig: &m.Types.Entries[1],
Host: reflect.ValueOf(p.write),
Body: &wasm.FunctionBody{},
},
2018-06-18 01:48:10 +00:00
{
Sig: &m.Types.Entries[1],
Host: reflect.ValueOf(p.read),
Body: &wasm.FunctionBody{},
},
{
Sig: &m.Types.Entries[2],
Host: reflect.ValueOf(p.close),
Body: &wasm.FunctionBody{},
},
2018-06-18 02:35:10 +00:00
{
Sig: &m.Types.Entries[2],
Host: reflect.ValueOf(isatty),
Body: &wasm.FunctionBody{},
},
{
Sig: &m.Types.Entries[2],
Host: reflect.ValueOf(p.unlink),
Body: &wasm.FunctionBody{},
},
2018-06-07 22:31:01 +00:00
}
m.Export = &wasm.SectionExports{
Entries: map[string]wasm.ExportEntry{
"log": {
FieldStr: "log",
Kind: wasm.ExternalFunction,
Index: 0,
},
2018-06-18 01:16:24 +00:00
"open": {
FieldStr: "open",
Kind: wasm.ExternalFunction,
Index: 1,
},
"write": {
FieldStr: "write",
Kind: wasm.ExternalFunction,
Index: 2,
},
2018-06-18 01:48:10 +00:00
"read": {
FieldStr: "read",
Kind: wasm.ExternalFunction,
Index: 3,
},
"close": {
FieldStr: "close",
Kind: wasm.ExternalFunction,
Index: 4,
},
2018-06-18 02:35:10 +00:00
"isatty": {
FieldStr: "isatty",
Kind: wasm.ExternalFunction,
Index: 5,
},
"unlink": {
FieldStr: "unlink",
Kind: wasm.ExternalFunction,
Index: 6,
},
2018-06-07 22:31:01 +00:00
},
}
return m, nil
default:
f, err := os.Open(name + ".wasm")
if err != nil {
return nil, err
}
defer f.Close()
m, err := wasm.ReadModule(f, nil)
if err != nil {
return nil, err
}
err = validate.VerifyModule(m)
if err != nil {
return nil, err
}
return m, nil
}
}
2018-06-08 00:08:00 +00:00
// ID returns the process ID.
func (p Process) ID() int32 { return p.id }
2018-06-07 22:31:01 +00:00
// VM returns the webassembly VM used to execute code. This isn't thread-safe.
func (p Process) VM() *exec.VM { return p.vm }
func (p *Process) log(ptr int32, len int32) int32 {
2018-06-08 00:08:00 +00:00
mem := p.vm.Memory()
if mem == nil {
panic("memory is nil, wtf")
}
data := mem[ptr : ptr+len]
2018-06-07 22:31:01 +00:00
ln.Log(gcontext.Background(), ln.F{"data": data, "data_string": string(data), "pid": p.id})
return 0
}
2018-06-08 02:02:12 +00:00
2018-06-18 00:38:05 +00:00
func (p *Process) writeMem(ptr int32, data []byte) (int, error) {
mem := p.vm.Memory()
if mem == nil {
return 0, errors.New("wtf")
}
for i, d := range data {
mem[ptr+int32(i)] = d
}
return len(data), nil
}
func (p *Process) readMem(ptr int32) []byte {
var result []byte
mem := p.vm.Memory()[ptr:]
for _, bt := range mem {
if bt == 0 {
return result
}
result = append(result, bt)
}
return result
}
2018-06-18 01:16:24 +00:00
func (p *Process) open(fnamesP int32, flags int32) int32 {
str := string(p.readMem(fnamesP))
var fi afero.File
var err error
switch str {
2018-06-18 01:48:10 +00:00
case "stdout":
2018-06-22 01:43:03 +00:00
fi = stdFD{
ReadWriteCloser: os.Stdout,
}
case "log":
2018-06-18 01:48:10 +00:00
fi = stdFD{
ReadWriteCloser: loggerFile{
2018-06-21 03:06:35 +00:00
Logger: log.New(os.Stdout, p.name+" ", log.LstdFlags),
2018-06-18 01:48:10 +00:00
},
}
2018-06-18 01:16:24 +00:00
default:
fi, err = p.fs.OpenFile(string(str), int(flags), 0666)
if err != nil {
if strings.Contains(err.Error(), afero.ErrFileNotFound.Error()) {
fi, err = p.fs.Create(str)
}
}
}
if err != nil {
panic(err)
}
fd := len(p.files)
p.files = append(p.files, fi)
return int32(fd)
}
func (p *Process) write(fd int32, ptr int32, len int32) int32 {
data := p.vm.Memory()[ptr : ptr+len]
n, err := p.files[fd].Write(data)
if err != nil {
panic(err)
}
return int32(n)
}
2018-06-18 01:48:10 +00:00
func (p *Process) read(fd int32, ptr int32, len int32) int32 {
data := make([]byte, len)
na, err := p.files[fd].Read(data)
if err != nil {
panic(err)
}
nb, err := p.writeMem(ptr, data)
if err != nil {
panic(err)
}
if na != nb {
panic("did not copy the same number of bytes???")
}
return int32(na)
}
func (p *Process) close(fd int32) int32 {
f := p.files[fd]
err := f.Close()
if err != nil {
panic(err)
}
if len(p.files) == 1 {
p.files = []afero.File{}
} else {
p.files = append(p.files[:fd], p.files[fd+1])
}
return 0
}
2018-06-18 02:35:10 +00:00
func (p *Process) unlink(fnameP int32) int32 {
fname := string(p.readMem(fnameP))
err := p.fs.RemoveAll(fname)
if err != nil {
panic(err)
}
return 0
}
func isatty(fd int32) int32 {
return 0
}
2018-06-08 02:02:12 +00:00
func (p *Process) Main() (uint32, error) {
foundMain := false
mainID := uint32(0)
for name, entry := range p.mod.Export.Entries {
if name == "main" && entry.FieldStr == "main" {
mainID = entry.Index
foundMain = true
break
}
}
if !foundMain {
return 1, errors.New("no main function found")
}
returnCode, err := p.vm.ExecCode(int64(mainID))
if err != nil {
return 1, err
}
return returnCode.(uint32), nil
}