aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2021-07-31 15:10:18 +0200
committerRon Evans <[email protected]>2021-08-05 19:01:14 +0200
commit478c592b131b21506e50ed1793b09689f0da456f (patch)
tree4afd68b4b0bf3cc83e84a0ba0b76d4ae4e63c7d4
parentab47cea055d24e06135ba4d7d896dc6e8837f8d7 (diff)
downloadtinygo-478c592b131b21506e50ed1793b09689f0da456f.tar.gz
tinygo-478c592b131b21506e50ed1793b09689f0da456f.zip
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).
-rw-r--r--main_test.go8
-rw-r--r--src/crypto/rand/rand_getentropy.go2
-rw-r--r--targets/wasm_exec.js4
-rw-r--r--testdata/env.go23
-rw-r--r--testdata/env.txt1
-rw-r--r--testdata/rand.go24
-rw-r--r--testdata/rand.txt1
7 files changed, 37 insertions, 26 deletions
diff --git a/main_test.go b/main_test.go
index 9ed0c6c54..2c1b2f864 100644
--- a/main_test.go
+++ b/main_test.go
@@ -162,7 +162,7 @@ func runPlatTests(target string, tests []string, t *testing.T) {
runTest(name, target, t, nil, nil)
})
}
- if target == "wasi" || target == "" {
+ if target == "" || target == "wasi" {
t.Run("filesystem.go", func(t *testing.T) {
t.Parallel()
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"})
})
}
+ 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.
diff --git a/src/crypto/rand/rand_getentropy.go b/src/crypto/rand/rand_getentropy.go
index 661132fb7..4cd037956 100644
--- a/src/crypto/rand/rand_getentropy.go
+++ b/src/crypto/rand/rand_getentropy.go
@@ -1,4 +1,4 @@
-// +build darwin freebsd wasi
+// +build darwin freebsd tinygo.wasm
// This implementation of crypto/rand uses the getentropy system call (available
// on both MacOS and WASI) to generate random numbers.
diff --git a/targets/wasm_exec.js b/targets/wasm_exec.js
index 21bfcda18..b0545abdb 100644
--- a/targets/wasm_exec.js
+++ b/targets/wasm_exec.js
@@ -285,6 +285,10 @@
throw 'trying to exit with code ' + code;
}
},
+ random_get: (bufPtr, bufLen) => {
+ crypto.getRandomValues(loadSlice(bufPtr, bufLen));
+ return 0;
+ },
},
env: {
// func ticks() float64
diff --git a/testdata/env.go b/testdata/env.go
index 9c5bb5a89..115da6d3d 100644
--- a/testdata/env.go
+++ b/testdata/env.go
@@ -1,7 +1,6 @@
package main
import (
- "crypto/rand"
"os"
)
@@ -21,26 +20,4 @@ func main() {
for _, arg := range os.Args[1:] {
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")
- }
}
diff --git a/testdata/env.txt b/testdata/env.txt
index e392cd5e9..8ba50a7fd 100644
--- a/testdata/env.txt
+++ b/testdata/env.txt
@@ -3,4 +3,3 @@ ENV2: VALUE2
arg: first
arg: second
-random number check was successful
diff --git a/testdata/rand.go b/testdata/rand.go
new file mode 100644
index 000000000..958b95c22
--- /dev/null
+++ b/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")
+ }
+}
diff --git a/testdata/rand.txt b/testdata/rand.txt
new file mode 100644
index 000000000..d6b8162f7
--- /dev/null
+++ b/testdata/rand.txt
@@ -0,0 +1 @@
+random number check was successful