wasm: add support for the crypto/rand package

This is done via wasi-libc and the WASI interface, for ease of
maintenance (only one implementation for both WASI and JS/browsers).
Этот коммит содержится в:
Ayke van Laethem 2021-07-31 15:10:18 +02:00 коммит произвёл Ron Evans
родитель ab47cea055
коммит 478c592b13
7 изменённых файлов: 37 добавлений и 26 удалений

Просмотреть файл

@ -162,7 +162,7 @@ func runPlatTests(target string, tests []string, t *testing.T) {
runTest(name, target, t, nil, nil) runTest(name, target, t, nil, nil)
}) })
} }
if target == "wasi" || target == "" { if target == "" || target == "wasi" {
t.Run("filesystem.go", func(t *testing.T) { t.Run("filesystem.go", func(t *testing.T) {
t.Parallel() t.Parallel()
runTest("filesystem.go", target, t, nil, nil) runTest("filesystem.go", target, t, nil, nil)
@ -172,6 +172,12 @@ func runPlatTests(target string, tests []string, t *testing.T) {
runTest("env.go", target, t, []string{"first", "second"}, []string{"ENV1=VALUE1", "ENV2=VALUE2"}) runTest("env.go", target, t, []string{"first", "second"}, []string{"ENV1=VALUE1", "ENV2=VALUE2"})
}) })
} }
if target == "" || target == "wasi" || target == "wasm" {
t.Run("rand.go", func(t *testing.T) {
t.Parallel()
runTest("rand.go", target, t, nil, nil)
})
}
} }
// Due to some problems with LLD, we cannot run links in parallel, or in parallel with compiles. // Due to some problems with LLD, we cannot run links in parallel, or in parallel with compiles.

Просмотреть файл

@ -1,4 +1,4 @@
// +build darwin freebsd wasi // +build darwin freebsd tinygo.wasm
// This implementation of crypto/rand uses the getentropy system call (available // This implementation of crypto/rand uses the getentropy system call (available
// on both MacOS and WASI) to generate random numbers. // on both MacOS and WASI) to generate random numbers.

Просмотреть файл

@ -285,6 +285,10 @@
throw 'trying to exit with code ' + code; throw 'trying to exit with code ' + code;
} }
}, },
random_get: (bufPtr, bufLen) => {
crypto.getRandomValues(loadSlice(bufPtr, bufLen));
return 0;
},
}, },
env: { env: {
// func ticks() float64 // func ticks() float64

23
testdata/env.go предоставленный
Просмотреть файл

@ -1,7 +1,6 @@
package main package main
import ( import (
"crypto/rand"
"os" "os"
) )
@ -21,26 +20,4 @@ func main() {
for _, arg := range os.Args[1:] { for _, arg := range os.Args[1:] {
println("arg:", arg) println("arg:", arg)
} }
// Check for crypto/rand support.
checkRand()
}
func checkRand() {
buf := make([]byte, 500)
n, err := rand.Read(buf)
if n != len(buf) || err != nil {
println("could not read random numbers:", err)
}
// Very simple test that random numbers are at least somewhat random.
sum := 0
for _, b := range buf {
sum += int(b)
}
if sum < 95*len(buf) || sum > 159*len(buf) {
println("random numbers don't seem that random, the average byte is", sum/len(buf))
} else {
println("random number check was successful")
}
} }

1
testdata/env.txt предоставленный
Просмотреть файл

@ -3,4 +3,3 @@ ENV2: VALUE2
arg: first arg: first
arg: second arg: second
random number check was successful

24
testdata/rand.go предоставленный Обычный файл
Просмотреть файл

@ -0,0 +1,24 @@
package main
import "crypto/rand"
// TODO: make this a test in the crypto/rand package.
func main() {
buf := make([]byte, 500)
n, err := rand.Read(buf)
if n != len(buf) || err != nil {
println("could not read random numbers:", err)
}
// Very simple test that random numbers are at least somewhat random.
sum := 0
for _, b := range buf {
sum += int(b)
}
if sum < 95*len(buf) || sum > 159*len(buf) {
println("random numbers don't seem that random, the average byte is", sum/len(buf))
} else {
println("random number check was successful")
}
}

1
testdata/rand.txt предоставленный Обычный файл
Просмотреть файл

@ -0,0 +1 @@
random number check was successful