summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwayne warren <wayne.warren.s@gmail.com>2023-05-09 23:55:36 -0600
committerwayne warren <wayne.warren.s@gmail.com>2023-05-24 11:10:55 -0600
commitc176d6d58f11fe95aee22c820443da1b368cfbab (patch)
treee22fb134eeb3ada1314cd44afb2b8da1752f234c
parentb72f485d18f243287d03d157a3e371bef00cc4f1 (diff)
examples: add getrandom.c example
-rw-r--r--examples/Makefile11
-rw-r--r--examples/getrandom.c32
2 files changed, 38 insertions, 5 deletions
diff --git a/examples/Makefile b/examples/Makefile
index dfbf48f..322b5d8 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -1,9 +1,10 @@
-CC=clang
+CC ?= "clang"
+SYSROOT ?= "../sysroot32"
LLD_PATH=/prog/rust/build/x86_64-unknown-linux-gnu/lld/bin
CFLAGS = --target=wasm32-wasmer-wasi \
-O2 \
- --sysroot ../../wasix-libc/sysroot32 \
+ --sysroot ${SYSROOT} \
-matomics \
-mbulk-memory \
-mmutable-globals \
@@ -54,7 +55,7 @@ SRC=$(wildcard *.c)
export PATH := $(LLD_PATH):$(PATH)
-all: longjmp file-copy signal fork vfork fork-longjmp execve spawn pipe epoll
+all: longjmp file-copy signal fork vfork fork-longjmp execve spawn pipe epoll getrandom
cp -f ../dist/wasix/longjmp.wasm ../../wasmer/tests/integration/cli/tests/wasm/example-longjmp.wasm
cp -f ../dist/wasix/fork.wasm ../../wasmer/tests/integration/cli/tests/wasm/example-fork.wasm
cp -f ../dist/wasix/fork.wasm ../../packages/fork-test/test.wasm
@@ -69,9 +70,9 @@ all: longjmp file-copy signal fork vfork fork-longjmp execve spawn pipe epoll
%: %.c
mkdir -p ../dist/host
- clang $@.c -o ../dist/host/$@
+ ${CC} $(CFLAGS) $@.c -o ../dist/host/$@
mkdir -p ../dist/wasix
- clang $(CFLAGS) $(CLFLAGS) \
+ ${CC} $(CFLAGS) $(CLFLAGS) \
$@.c \
-o ../dist/wasix/$@.rustc.wasm
wasm-opt -O2 --asyncify ../dist/wasix/$@.rustc.wasm -o ../dist/wasix/$@.wasm
diff --git a/examples/getrandom.c b/examples/getrandom.c
new file mode 100644
index 0000000..7da78a5
--- /dev/null
+++ b/examples/getrandom.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <sys/random.h>
+#include <unistd.h>
+
+int print_random_int_array() {
+ ssize_t retval;
+ int buf[100];
+ retval = getrandom(buf, sizeof(int) * 100, 0);
+ if (retval == -1) {
+ return retval;
+ }
+ for (int i = 0; i < 100; ++i) {
+ printf("%d ", buf[i]);
+ }
+ printf("\n");
+ return 0;
+}
+
+int main() {
+ int i = 0, retval = 0;
+ while (i < 5) {
+ printf("trial %d:\n ", i);
+ retval = print_random_int_array();
+ printf("\n");
+ if (retval == -1) {
+ printf("failed!\n");
+ return retval;
+ }
+ i++;
+ }
+ return 0;
+}