Skip to content

Commit 0373034

Browse files
Address comments.
1 parent 1577145 commit 0373034

File tree

4 files changed

+70
-17
lines changed

4 files changed

+70
-17
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ This reposistory contains a collection of example grate implementations that can
44

55
Grates provide custom syscall wrappers for Lind cages. Each example grate here demonstrates how to override one or more syscalls with a custom implementation.
66

7-
For more details on Lind and grates, refer to the official [documentation.](https://lind-project.github.io/lind-wasm/)
7+
For more details, refer to the documentation here:
8+
9+
- [Lind-Wasm documentation](https://lind-project.github.io/lind-wasm/)
10+
- [3i](https://github.com/Lind-Project/lind-wasm/blob/main/src/threei/README.md)
811

912
## Repository Structure
1013

@@ -98,6 +101,8 @@ This also allows multiple grates to be interposed. For example:
98101
99102
Grates are compiled similarly to standard Lind programs, with the additional requirement that the WASM module exports the `pass_fptr_to_wt` function.
100103
104+
[`lind_compile`](https://github.com/Lind-Project/lind-wasm/blob/main/scripts/lind_compile) script compiles `.c` programs to `.wasm` binaries for lind.
105+
101106
Example of a compile script: [`examples/geteuid-grate/compile_grate.sh`](./examples/geteuid-grate/compile_grate.sh)
102107
103108
## Running a Grate

examples/geteuid-grate/build.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ENTRY=geteuid_grate.c
Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,58 @@
1-
#!/bin/bash
2-
if [ $# -lt 1 ]; then
3-
echo "Usage: $0 <source.c>"
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
if [[ $# -ne 1 ]]; then
5+
echo "usage: $0 <example-dir>"
6+
exit 1
7+
fi
8+
9+
TARGET="$1"
10+
11+
# Enter the example directory
12+
pushd "$TARGET" >/dev/null
13+
14+
# Now everything is relative to the example dir
15+
echo "[cwd] $(pwd)"
16+
17+
# Load per-example config
18+
if [[ ! -f build.conf ]]; then
19+
echo "missing build.conf"
420
exit 1
521
fi
22+
source build.conf
23+
24+
CLANG="${CLANG:-/home/lind/lind-wasm/clang+llvm-18.1.8-x86_64-linux-gnu-ubuntu-18.04/bin/clang}"
25+
SYSROOT="${SYSROOT:-/home/lind/lind-wasm/src/glibc/sysroot}"
26+
WASM_OPT="${WASM_OPT:-/home/lind/lind-wasm/tools/binaryen/bin/wasm-opt}"
27+
WASMTIME="${WASMTIME:-/home/lind/lind-wasm/src/wasmtime/target/release/wasmtime}"
628

7-
SRC="$1"
8-
BASE="${SRC%.c}"
29+
SRC_DIR="src"
30+
mkdir -p output
31+
OUT="output/${ENTRY%.c}"
932

10-
CLANG="/home/lind/lind-wasm/clang+llvm-18.1.8-x86_64-linux-gnu-ubuntu-18.04/bin/clang"
11-
SYSROOT="/home/lind/lind-wasm/src/glibc/sysroot"
12-
WASM_OPT="/home/lind/lind-wasm/tools/binaryen/bin/wasm-opt"
13-
WASMTIME="/home/lind/lind-wasm/src/wasmtime/target/release/wasmtime"
33+
MAX_MEMORY="${MAX_MEMORY:-268435456}"
34+
EXTRA_CFLAGS="${EXTRA_CFLAGS:-}"
35+
EXTRA_WASM_OPT="${EXTRA_WASM_OPT:-}"
36+
37+
echo "[build] $OUT (max-mem=$MAX_MEMORY)"
1438

1539
"$CLANG" -pthread \
16-
--target=wasm32-unknown-wasi \
17-
--sysroot "$SYSROOT" \
18-
-Wl,--import-memory,--export-memory,--max-memory=67108864,\
40+
--target=wasm32-unknown-wasi \
41+
--sysroot "$SYSROOT" \
42+
-Wl,--import-memory,--export-memory,--max-memory="$MAX_MEMORY",\
1943
--export=__stack_pointer,--export=__stack_low,--export=pass_fptr_to_wt \
20-
"$SRC" \
21-
-g -O0 -o "${BASE}.wasm"
44+
$EXTRA_CFLAGS \
45+
"$SRC_DIR"/*.c \
46+
-g -O0 -o "$OUT.wasm"
47+
48+
"$WASM_OPT" \
49+
--asyncify \
50+
--epoch-injection \
51+
--debuginfo \
52+
$EXTRA_WASM_OPT \
53+
"$OUT.wasm" -o "$OUT.wasm"
2254

23-
"$WASM_OPT" --asyncify --epoch-injection --debuginfo "${BASE}.wasm" -o "${BASE}.wasm"
55+
"$WASMTIME" compile "$OUT.wasm" -o "$OUT.cwasm"
2456

25-
"$WASMTIME" compile "${BASE}.wasm" -o "${BASE}.cwasm"
57+
# Return to original directory
58+
popd >/dev/null

examples/geteuid-grate/src/geteuid_grate.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <errno.h>
2+
// lind_syscall.h provides functions needed for cage interactions: register_handler, copy_data_between_cage, and make_threei_call
23
#include <lind_syscall.h>
34
#include <stdio.h>
45
#include <stdlib.h>
@@ -7,6 +8,19 @@
78
#include <unistd.h>
89

910
// Dispatcher function
11+
//
12+
// Entry point into a grate when a child cage invokes a registered
13+
// syscall. This function is used to invoke the appropriate handler,
14+
// and value returned is the passed down to the calling cage.
15+
//
16+
// Args:
17+
// fn_ptr_uint Address of the registered syscall handler within the
18+
// grate's address space.
19+
// cageid Identifier of the calling cage.
20+
// arg[1-6] Syscall arguments. Numeric types are passed by value, pointers
21+
// are passed as addresses in the originating cage's address space.
22+
// arg[1-6]cage Cage IDs corresponding to each argument, indicating which cage's address
23+
// space the argument belongs to.
1024
int pass_fptr_to_wt(uint64_t fn_ptr_uint, uint64_t cageid, uint64_t arg1,
1125
uint64_t arg1cage, uint64_t arg2, uint64_t arg2cage,
1226
uint64_t arg3, uint64_t arg3cage, uint64_t arg4,

0 commit comments

Comments
 (0)