perlin-network / life

A secure WebAssembly VM catered for decentralized applications.
MIT License
1.7k stars 119 forks source link

too many arguments in call to disasm.Disassemble #82

Closed kleash closed 5 years ago

kleash commented 5 years ago

Hi,

I am facing this error while trying to call a wasm file from go code:

connecto/input/src/github.com/connecto/lifecontractone/vendor/github.com/perlin-network/life/compiler/module.go:147:31: too many arguments in call to disasm.Disassemble
    have (wasm.Function, *wasm.Module)
    want ([]byte)
connecto/input/src/github.com/connecto/lifecontractone/vendor/github.com/perlin-network/life/compiler/module.go:151:39: cannot use d (type []disasm.Instr) as type *disasm.Disassembly in argument to NewSSAFunctionCompiler
connecto/input/src/github.com/connecto/lifecontractone/vendor/github.com/perlin-network/life/compiler/module.go:223:31: too many arguments in call to disasm.Disassemble
    have (wasm.Function, *wasm.Module)
    want ([]byte)
connecto/input/src/github.com/connecto/lifecontractone/vendor/github.com/perlin-network/life/compiler/module.go:227:39: cannot use d (type []disasm.Instr) as type *disasm.Disassembly in argument to NewSSAFunctionCompiler

Snippet of go code:


// Resolver defines imports for WebAssembly modules ran in Life.
type Resolver struct {
    tempRet0 int64
}

// ResolveFunc defines a set of import functions that may be called within a WebAssembly module.
func (r *Resolver) ResolveFunc(module, field string) exec.FunctionImport {
    fmt.Printf("Resolve func: %s %s\n", module, field)
    switch module {
    case "env":
        switch field {
        case "__life_log":
            return func(vm *exec.VirtualMachine) int64 {
                ptr := int(uint32(vm.GetCurrentFrame().Locals[0]))
                msgLen := int(uint32(vm.GetCurrentFrame().Locals[1]))
                msg := vm.Memory[ptr : ptr+msgLen]
                fmt.Printf("[app] %s\n", string(msg))
                return 0
            }
        default:
            panic(fmt.Errorf("unknown field: %s", field))
        }
    default:
        panic(fmt.Errorf("unknown module: %s", module))
    }
}

// ResolveGlobal defines a set of global variables for use within a WebAssembly module.
func (r *Resolver) ResolveGlobal(module, field string) int64 {
    fmt.Printf("Resolve global: %s %s\n", module, field)
    switch module {
    case "env":
        switch field {
        case "__life_magic":
            return 424
        default:
            panic(fmt.Errorf("unknown field: %s", field))
        }
    default:
        panic(fmt.Errorf("unknown module: %s", module))
    }
}

// query callback representing the query of a chaincode
func main()  {

    wasmbytes, _ := getWasm(A)
    entryFunctionFlag := flag.String("entry", "funcToInvoke", "entry function name")
    noFloatingPointFlag := flag.Bool("no-fp", false, "disable floating point")
    flag.Parse()

    validator, err := wasm_validation.NewValidator()
    if err != nil {
        panic(err)
    }
    err = validator.ValidateWasm(Chaincodebytes)
    if err != nil {
        panic(err)
    }

    // Instantiate a new WebAssembly VM with a few resolved imports.
    vm, err := exec.NewVirtualMachine(Chaincodebytes, exec.VMConfig{
        DefaultMemoryPages:   128,
        DefaultTableSize:     65536,
        DisableFloatingPoint: *noFloatingPointFlag,
    }, new(Resolver), nil)

    if err != nil {
        panic(err)
    }

    // Get the function ID of the entry function to be executed.
    entryID, ok := vm.GetFunctionExport(*entryFunctionFlag)
    if !ok {
        fmt.Printf("Entry function %s not found; starting from 0.\n", *entryFunctionFlag)
        entryID = 0
    }

    start := time.Now()

    // Run the WebAssembly module's entry function.
    result, err := vm.Run(entryID)
    if err != nil {
        vm.PrintStackTrace()
        panic(err)
    }
    end := time.Now()

    fmt.Printf("return value = %d, duration = %v\n", result, end.Sub(start))

    fmt.Printf("Query Response:%s\n", result)
}

Any pointers on what might have gone wrong would be appreciated.

Thanks

justinclift commented 5 years ago

Guessing here, but maybe the version of Wagon (used by Life for disassembling wasm) you have isn't matching what Life uses?

Life uses an older fork of Wagon, and some stuff has changed in the more recent versions.

If that's the case, you can adjust the version of Wagon you're using to match what Life needs. Then it would work. :smile:

kleash commented 5 years ago

It worked. Thank you :)

justinclift commented 5 years ago

Awesome. :smile: