Skip to content

Commit

Permalink
Update WASM example
Browse files Browse the repository at this point in the history
  • Loading branch information
wrv committed Sep 30, 2023
1 parent 7f913cf commit 117400a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
9 changes: 6 additions & 3 deletions src/chapters/examples/wasm-hello-example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ WASI_SDK_ROOT=$(RLBOX_ROOT)/build/_deps/wasiclang-src/

#location of our wasi/wasm runtime
WASM2C_RUNTIME_PATH=$(RLBOX_ROOT)/build/_deps/mod_wasm2c-src/wasm2c
WASI_RUNTIME_FILES=$(addprefix $(WASM2C_RUNTIME_PATH), /wasm-rt-impl.c /wasm-rt-os-win.c /wasm-rt-os-unix.c /wasm-rt-wasi.c)
WASI_RUNTIME_FILES=$(addprefix $(WASM2C_RUNTIME_PATH), /wasm-rt-impl.c)

#Some more wasi/wasm runtime files
WASM2C_RUNTIME_FILES=$(addprefix $(RLBOX_ROOT)/src, /wasm2c_rt_minwasi.c /wasm2c_rt_mem.c)

WASI_CLANG=$(WASI_SDK_ROOT)/bin/clang
WASI_SYSROOT=$(WASI_SDK_ROOT)/share/wasi-sysroot
WASM2C=$(RLBOX_ROOT)/build/_deps/mod_wasm2c-src/bin/wasm2c

#CFLAGS for compiling files to place nice with wasm2c
WASM_CFLAGS=-Wl,--export-all -Wl,--no-entry -Wl,--growable-table -Wl,--stack-first -Wl,-z,stack-size=1048576
WASM_CFLAGS=-Wl,--export-all -Wl,--stack-first -Wl,-z,stack-size=262144 -Wl,--no-entry -Wl,--growable-table -Wl,--import-memory -Wl,--import-table

all: mylib.wasm mylib.wasm.c myapp

Expand All @@ -33,5 +36,5 @@ mylib.wasm.c: mylib.wasm

#Step 3: compiling and linking our application with our library
myapp: mylib.wasm.c
$(CC) -c $(WASI_RUNTIME_FILES) -I$(RLBOX_INCLUDE) -I$(RLBOX_ROOT)/include -I$(WASM2C_RUNTIME_PATH) mylib.wasm.c
$(CC) -c $(WASI_RUNTIME_FILES) $(WASM2C_RUNTIME_FILES) -I$(RLBOX_INCLUDE) -I$(RLBOX_ROOT)/include -I$(WASM2C_RUNTIME_PATH) mylib.wasm.c
$(CXX) -std=c++17 main.cpp -o myapp -I$(RLBOX_INCLUDE) -I$(RLBOX_ROOT)/include -I$(WASM2C_RUNTIME_PATH) *.o -lpthread
41 changes: 22 additions & 19 deletions src/chapters/examples/wasm-hello-example/main-diff.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
1 $diff ../noop-hello-example/main.cpp main.cpp
2 3a4
3 >
4 5c6
5 < #define RLBOX_USE_STATIC_CALLS() rlbox_noop_sandbox_lookup_symbol
6 ---
7 > #define RLBOX_USE_STATIC_CALLS() rlbox_wasm2c_sandbox_lookup_symbol
8 9,11d9
9 < #include <rlbox/rlbox.hpp>
10 < #include <rlbox/rlbox_noop_sandbox.hpp>
11 <
12 12a11,14
13 > #include "mylib.wasm.h"
14 > #include "rlbox.hpp"
15 > #include "rlbox_wasm2c_sandbox.hpp"
16 >
17 18c20
18 < RLBOX_DEFINE_BASE_TYPES_FOR(mylib, noop);
19 ---
20 > RLBOX_DEFINE_BASE_TYPES_FOR(mylib, wasm2c);
2 3a4,7
3 >
4 > // Provide the sandbox a name
5 > #define RLBOX_WASM2C_MODULE_NAME mylib
6 >
7 5c9
8 < #define RLBOX_USE_STATIC_CALLS() rlbox_noop_sandbox_lookup_symbol
9 ---
10 > #define RLBOX_USE_STATIC_CALLS() rlbox_wasm2c_sandbox_lookup_symbol
11 9,11d12
12 < #include <rlbox/rlbox.hpp>
13 < #include <rlbox/rlbox_noop_sandbox.hpp>
14 <
15 12a14,17
16 > #include "mylib.wasm.h"
17 > #include "rlbox.hpp"
18 > #include "rlbox_wasm2c_sandbox.hpp"
19 >
20 18c23
21 < RLBOX_DEFINE_BASE_TYPES_FOR(mylib, noop);
22 ---
23 > RLBOX_DEFINE_BASE_TYPES_FOR(mylib, wasm2c);
3 changes: 3 additions & 0 deletions src/chapters/examples/wasm-hello-example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// All calls into the sandbox are resolved statically.
#define RLBOX_USE_STATIC_CALLS() rlbox_wasm2c_sandbox_lookup_symbol

// Provide the wasm2c module a name
#define RLBOX_WASM2C_MODULE_NAME mylib

#include <stdio.h>
#include <cassert>
#include "mylib.h"
Expand Down
18 changes: 10 additions & 8 deletions src/chapters/wasm-sandbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ sandbox backend) and in our current example (using the `wasm2c` backend).
```

As you can see, most of what has changed is renaming a few key instances of
`noop` to `wasm2c`, most notably to change our backend type on line 10.
`noop` to `wasm2c`, most notably to change the backend type that we include.

The only other changes are ```#include "mylib.wasm.h"``` on line 13, which
brings in the new header for our sandboxed library generated by `wasm2c`.
brings in the new header for our sandboxed library generated by `wasm2c`, and
```#define RLBOX_WASM2C_MODULE_NAME mylib``` which will provide a unique name
for this sandbox.

These are essentially all the changes you will need to make to your application
to switch wasm backends.

The changes to how rlbox is included on lines 14 and 15 are just an artifact of
The changes to how rlbox is included on lines 17 and 18 are just an artifact of
differences how our examples are built.


Expand Down Expand Up @@ -65,7 +67,7 @@ e.g. `wasm2c`, wasi-sdk (which CMake downloads), RLBox, etc.
### Step 1: Compiling our library to Wasm

```Makefile
{{#include examples/wasm-hello-example/Makefile:27:28}}
{{#include examples/wasm-hello-example/Makefile:30:31}}
```

Here we are building our library to wasm. Typically you will just want to update
Expand All @@ -79,8 +81,8 @@ our output plays nicely with the rest of the toolchain.

<!-- XXX explain flags -->

Notice the `dummy_main.c` file to keep wasi-clang happy, you can find a copy
`rlbox_wasm2c_sandbox/_src/wasm2c_sandbox_wrapper.c`
Notice the `dummy_main.c` file to keep wasi-clang happy. You can
find a copy of this file at `rlbox_wasm2c_sandbox/c_src/wasm2c_sandbox_wrapper.c`.

wasi-libc at the moment has a variety of limitations such as lack of pthread
support (though this should be fixed soon!). Anything platform specific such as
Expand All @@ -91,7 +93,7 @@ platform specific code e.g. inline assembly will also fail at this stage.
### Step 2: Using `wasm2c` to generate our sandboxed library

```Makefile
{{#include examples/wasm-hello-example/Makefile:31:32}}
{{#include examples/wasm-hello-example/Makefile:34:35}}
```


Expand All @@ -110,7 +112,7 @@ system and network.
### Step 3: Compiling and linking our application with our library

```Makefile
{{#include examples/wasm-hello-example/Makefile:35:37}}
{{#include examples/wasm-hello-example/Makefile:38:40}}
```

<!-- XXX not our .c and .h files -->
Expand Down

0 comments on commit 117400a

Please sign in to comment.