aboutsummaryrefslogtreecommitdiff
path: root/src/wasmrt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/wasmrt.c')
-rw-r--r--src/wasmrt.c20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/wasmrt.c b/src/wasmrt.c
index 3074a75..2283d0b 100644
--- a/src/wasmrt.c
+++ b/src/wasmrt.c
@@ -72,12 +72,17 @@ void wasm_rt_trap_unregister(void) {
wasm_rt_trap_handler = NULL;
}
-void wasm_rt_memory_check(struct wasm_rt_memory_t const *mem, uint32_t addr,
- uint32_t len) {
+static inline bool memory_oob(struct wasm_rt_memory_t const *mem, uint32_t addr,
+ uint32_t len) {
// While memory address 0 is _technically_ allowed in Wasm, we reject it in
// our memory bounds checking, as nothing should ask us to write to it.
uint32_t end = addr + len;
- if (len && UNLIKELY(!addr || end < addr || end > mem->size)) {
+ return len && (!addr || end < addr || end > mem->size);
+}
+
+void wasm_rt_memory_check(struct wasm_rt_memory_t const *mem, uint32_t addr,
+ uint32_t len) {
+ if (UNLIKELY(memory_oob(mem, addr, len))) {
wasm_rt_trap(WASM_RT_TRAP_OOB);
}
}
@@ -123,6 +128,15 @@ uint32_t wasm_rt_memory_load32_unchecked(struct wasm_rt_memory_t const *mem,
return value;
}
+bool wasm_rt_memory_load32_checked(struct wasm_rt_memory_t const *mem,
+ uint32_t addr, uint32_t *value) {
+ if (UNLIKELY(memory_oob(mem, addr, sizeof(*value)))) {
+ return false;
+ }
+ *value = wasm_rt_memory_load32_unchecked(mem, addr);
+ return true;
+}
+
size_t wasm_rt_memory_loada(struct wasm_rt_memory_t const *mem, uint32_t addr,
char *buf, size_t sz) {
// This is overly pessimistic in case we try to load a string at the