-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wasi: backfills constants and TODOs for remaining functions (#222)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
- Loading branch information
1 parent
9f2ba95
commit e6d841c
Showing
13 changed files
with
963 additions
and
664 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package wasi | ||
|
||
import ( | ||
"fmt" | ||
"unicode/utf8" | ||
) | ||
|
||
// nullTerminatedStrings holds null-terminated strings. It ensures that | ||
// its length and total buffer size don't exceed the max of uint32. | ||
// nullTerminatedStrings are convenience struct for args_get and environ_get. (environ_get is not implemented yet) | ||
// | ||
// A Null-terminated string is a byte string with a NULL suffix ("\x00"). | ||
// See https://en.wikipedia.org/wiki/Null-terminated_string | ||
type nullTerminatedStrings struct { | ||
// nullTerminatedValues are null-terminated values with a NULL suffix. | ||
nullTerminatedValues [][]byte | ||
totalBufSize uint32 | ||
} | ||
|
||
// newNullTerminatedStrings creates a nullTerminatedStrings from the given string slice. It returns an error | ||
// if the length or the total buffer size of the result WASIStringArray exceeds the maxBufSize | ||
func newNullTerminatedStrings(maxBufSize uint32, args ...string) (*nullTerminatedStrings, error) { | ||
if len(args) == 0 { | ||
return &nullTerminatedStrings{nullTerminatedValues: [][]byte{}}, nil | ||
} | ||
var strings [][]byte // don't pre-allocate as this function is size bound | ||
totalBufSize := uint32(0) | ||
for i, arg := range args { | ||
if !utf8.ValidString(arg) { | ||
return nil, fmt.Errorf("arg[%d] is not a valid UTF-8 string", i) | ||
} | ||
argLen := uint64(len(arg)) + 1 // + 1 for "\x00"; uint64 in case this one arg is huge | ||
nextSize := uint64(totalBufSize) + argLen | ||
if nextSize > uint64(maxBufSize) { | ||
return nil, fmt.Errorf("arg[%d] will exceed max buffer size %d", i, maxBufSize) | ||
} | ||
totalBufSize = uint32(nextSize) | ||
strings = append(strings, append([]byte(arg), 0)) | ||
} | ||
return &nullTerminatedStrings{nullTerminatedValues: strings, totalBufSize: totalBufSize}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package wasi | ||
|
||
import ( | ||
_ "embed" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewNullTerminatedStrings(t *testing.T) { | ||
emptyWASIStringArray := &nullTerminatedStrings{nullTerminatedValues: [][]byte{}} | ||
tests := []struct { | ||
name string | ||
input []string | ||
expected *nullTerminatedStrings | ||
}{ | ||
{ | ||
name: "nil", | ||
expected: emptyWASIStringArray, | ||
}, | ||
{ | ||
name: "none", | ||
input: []string{}, | ||
expected: emptyWASIStringArray, | ||
}, | ||
{ | ||
name: "two", | ||
input: []string{"a", "bc"}, | ||
expected: &nullTerminatedStrings{ | ||
nullTerminatedValues: [][]byte{ | ||
{'a', 0}, | ||
{'b', 'c', 0}, | ||
}, | ||
totalBufSize: 5, | ||
}, | ||
}, | ||
{ | ||
name: "two and empty string", | ||
input: []string{"a", "", "bc"}, | ||
expected: &nullTerminatedStrings{ | ||
nullTerminatedValues: [][]byte{ | ||
{'a', 0}, | ||
{0}, | ||
{'b', 'c', 0}, | ||
}, | ||
totalBufSize: 6, | ||
}, | ||
}, | ||
{ | ||
name: "utf-8", | ||
// "😨", "🤣", and "️🏃♀️" have 4, 4, and 13 bytes respectively | ||
input: []string{"😨🤣🏃\u200d♀️", "foo", "bar"}, | ||
expected: &nullTerminatedStrings{ | ||
nullTerminatedValues: [][]byte{ | ||
[]byte("😨🤣🏃\u200d♀️\x00"), | ||
{'f', 'o', 'o', 0}, | ||
{'b', 'a', 'r', 0}, | ||
}, | ||
totalBufSize: 30, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tc := tt | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
s, err := newNullTerminatedStrings(100, tc.input...) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, s) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewNullTerminatedStrings_Errors(t *testing.T) { | ||
t.Run("invalid utf-8", func(t *testing.T) { | ||
_, err := newNullTerminatedStrings(100, "\xff\xfe\xfd", "foo", "bar") | ||
require.EqualError(t, err, "arg[0] is not a valid UTF-8 string") | ||
}) | ||
t.Run("arg[0] too large", func(t *testing.T) { | ||
_, err := newNullTerminatedStrings(1, "a", "bc") | ||
require.EqualError(t, err, "arg[0] will exceed max buffer size 1") | ||
}) | ||
t.Run("empty arg too large due to null terminator", func(t *testing.T) { | ||
_, err := newNullTerminatedStrings(2, "a", "", "bc") | ||
require.EqualError(t, err, "arg[1] will exceed max buffer size 2") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.