This adds two clock interfaces: sys.Walltime and sys.Nanotime to allow implementations to override readings for purposes of security or determinism. The default values of both are a fake timestamp, to avoid the sandbox break we formerly had by returning the real time. This is similar to how we don't inherit OS Env values.
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package platform
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/tetratelabs/wazero/internal/testing/require"
|
|
)
|
|
|
|
var testCode, _ = io.ReadAll(io.LimitReader(rand.Reader, 8*1024))
|
|
|
|
func Test_MmapCodeSegment(t *testing.T) {
|
|
if !CompilerSupported() {
|
|
t.Skip()
|
|
}
|
|
|
|
newCode, err := MmapCodeSegment(testCode)
|
|
require.NoError(t, err)
|
|
// Verify that the mmap is the same as the original.
|
|
require.Equal(t, testCode, newCode)
|
|
// TODO: test newCode can executed.
|
|
|
|
t.Run("panic on zero length", func(t *testing.T) {
|
|
captured := require.CapturePanic(func() {
|
|
_, _ = MmapCodeSegment(make([]byte, 0))
|
|
})
|
|
require.EqualError(t, captured, "BUG: MmapCodeSegment with zero length")
|
|
})
|
|
}
|
|
|
|
func Test_MunmapCodeSegment(t *testing.T) {
|
|
if !CompilerSupported() {
|
|
t.Skip()
|
|
}
|
|
|
|
// Errors if never mapped
|
|
require.Error(t, MunmapCodeSegment(testCode))
|
|
|
|
newCode, err := MmapCodeSegment(testCode)
|
|
require.NoError(t, err)
|
|
// First munmap should succeed.
|
|
require.NoError(t, MunmapCodeSegment(newCode))
|
|
// Double munmap should fail.
|
|
require.Error(t, MunmapCodeSegment(newCode))
|
|
|
|
t.Run("panic on zero length", func(t *testing.T) {
|
|
captured := require.CapturePanic(func() {
|
|
_ = MunmapCodeSegment(make([]byte, 0))
|
|
})
|
|
require.EqualError(t, captured, "BUG: MunmapCodeSegment with zero length")
|
|
})
|
|
}
|