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

[CRE-43] fix slicing of events #992

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
18 changes: 16 additions & 2 deletions pkg/workflows/wasm/host/wasip1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package host

import (
"encoding/binary"
"fmt"
"io"
"math/rand"
"time"
Expand Down Expand Up @@ -122,9 +123,11 @@ func pollOneoff(caller *wasmtime.Caller, subscriptionptr int32, eventsptr int32,
eventType := subs[inOffset+8]
argBuf := subs[inOffset+8+8:]

outOffset := events[i*eventsLen]
slot, err := getSlot(events, i)
if err != nil {
return ErrnoFault
}

slot := events[outOffset:]
switch eventType {
case eventTypeClock:
// We want to stub out clock events,
Expand Down Expand Up @@ -228,3 +231,14 @@ func createRandomGet(cfg *ModuleConfig) func(caller *wasmtime.Caller, buf, bufLe
return ErrnoSuccess
}
}

func getSlot(events []byte, i int32) ([]byte, error) {
outOffset := i * eventsLen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the prefix out? Offset is simpler to understand


if outOffset+eventsLen > int32(len(events)) {
return nil, fmt.Errorf("slot %d out of bounds", i)
}

slot := events[outOffset : outOffset+eventsLen]
return slot, nil
}
25 changes: 25 additions & 0 deletions pkg/workflows/wasm/host/wasip1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package host

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetSlot(t *testing.T) {
events := []byte{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
250, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
}

expectedSlots := [][]byte{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
{250, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63},
}

for i := int32(0); i < 2; i++ {
slot, err := getSlot(events, i)
assert.NoError(t, err)
assert.Equal(t, expectedSlots[i], slot)
}
}
Loading