Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasm): override random_get #831

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions pkg/workflows/wasm/host/test/rand/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build wasip1

package main

import (
"bytes"
"crypto/rand"
"errors"

"github.com/smartcontractkit/chainlink-common/pkg/capabilities/cli/cmd/testdata/fixtures/capabilities/basictrigger"
"github.com/smartcontractkit/chainlink-common/pkg/workflows/sdk"
"github.com/smartcontractkit/chainlink-common/pkg/workflows/wasm"
)

func BuildWorkflow(config []byte) *sdk.WorkflowSpecFactory {
workflow := sdk.NewWorkflowSpecFactory(
sdk.NewWorkflowParams{
Name: "tester",
Owner: "ryan",
},
)

triggerCfg := basictrigger.TriggerConfig{Name: "trigger", Number: 100}
trigger := triggerCfg.New(workflow)

sdk.Compute1[basictrigger.TriggerOutputs, bool](
workflow,
"transform",
sdk.Compute1Inputs[basictrigger.TriggerOutputs]{Arg0: trigger},
func(sdk sdk.Runtime, outputs basictrigger.TriggerOutputs) (bool, error) {
b := make([]byte, 5)
_, err := rand.Read(b)
if err != nil {
return false, err
}

deterministic := bytes.Compare(b, []byte{0x53, 0x8c, 0x7f, 0x96, 0xb1}) == 0

if !deterministic {
return false, errors.New("expected deterministic output")
}

return deterministic, nil
})

return workflow
}

func main() {

runner := wasm.NewRunner()
workflow := BuildWorkflow(runner.Config())
runner.Run(workflow)

}
28 changes: 28 additions & 0 deletions pkg/workflows/wasm/host/wasip1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package host

import (
"encoding/binary"
"io"
"math/rand"
"time"
"unsafe"

"github.com/bytecodealliance/wasmtime-go/v23"
"github.com/jonboulle/clockwork"
Expand Down Expand Up @@ -41,9 +44,34 @@ func newWasiLinker(engine *wasmtime.Engine) (*wasmtime.Linker, error) {
return nil, err
}

err = linker.FuncWrap(
"wasi_snapshot_preview1",
"random_get",
randomGet,
)
if err != nil {
return nil, err
}

return linker, nil
}

func randomGet(caller *wasmtime.Caller, buf, bufLen int32) int32 {
// Get slice of memory for the buffer
bufPtr := &buf
bufFromPtr := (*[4]byte)(unsafe.Pointer(bufPtr))[:]

// Fix the random source with a hardcoded seed
randSource := rand.New(rand.NewSource(42)) //nolint:gosec

// Fill the buffer with random bytes
if _, err := io.ReadAtLeast(randSource, bufFromPtr, int(bufLen)); err != nil {
return ErrnoFault
}

return ErrnoSuccess
}

const (
clockIDRealtime = iota
clockIDMonotonic
Expand Down
29 changes: 29 additions & 0 deletions pkg/workflows/wasm/host/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const (
envBinaryCmd = "test/env/cmd"
logBinaryLocation = "test/log/cmd/testmodule.wasm"
logBinaryCmd = "test/log/cmd"
randBinaryLocation = "test/rand/cmd/testmodule.wasm"
randBinaryCmd = "test/rand/cmd"
)

func createTestBinary(outputPath, path string, compress bool, t *testing.T) []byte {
Expand Down Expand Up @@ -375,6 +377,33 @@ func TestModule_Sandbox_ReadEnv(t *testing.T) {
assert.Nil(t, err)
}

func TestModule_Sandbox_RandRead(t *testing.T) {
binary := createTestBinary(randBinaryCmd, randBinaryLocation, true, t)

m, err := NewModule(&ModuleConfig{Logger: logger.Test(t)}, binary)
require.NoError(t, err)

m.Start()

req := &wasmpb.Request{
Id: uuid.New().String(),
Message: &wasmpb.Request_ComputeRequest{
ComputeRequest: &wasmpb.ComputeRequest{
Request: &capabilitiespb.CapabilityRequest{
Inputs: &valuespb.Map{},
Config: &valuespb.Map{},
Metadata: &capabilitiespb.RequestMetadata{
ReferenceId: "transform",
},
},
},
},
}

_, err = m.Run(req)
assert.Nil(t, err)
}

type Entry struct {
Log zapcore.Entry
Fields []zapcore.Field
Expand Down
Loading