Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make wasm_global_set a safe API. #133

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions example/global.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
check(results[0], type, expected); \
}

#define check_no_trap(maybe_trap) \
if ((maybe_trap)) { \
printf("> Trap occurred\n"); \
exit(1); \
}

#define check_trap(maybe_trap) \
if (!(maybe_trap)) { \
printf("> Expected trap did not occur\n"); \
exit(1); \
}


int main(int argc, const char* argv[]) {
// Initialize.
Expand Down Expand Up @@ -174,15 +186,31 @@ int main(int argc, const char* argv[]) {
check_call(get_var_f32_export, f32, 7);
check_call(get_var_i64_export, i64, 8);

// Attempt to mutate immutable globals.
wasm_val_t val33 = {.kind = WASM_F32, .of = {.f32 = 33}};
check_trap(wasm_global_set(const_f32_import, &val33));
check_global(const_f32_import, f32, 1);
wasm_val_t val34 = {.kind = WASM_I64, .of = {.i64 = 34}};
check_trap(wasm_global_set(const_i64_import, &val34));
check_global(const_i64_import, i64, 2);

// Attempt to set globals with values of incorrect types.
wasm_val_t val37 = {.kind = WASM_F64, .of = {.f64 = 37}};
check_trap(wasm_global_set(var_f32_export, &val37));
check_global(var_f32_import, f32, 3);
wasm_val_t val38 = {.kind = WASM_I32, .of = {.i32 = 38}};
check_trap(wasm_global_set(var_i64_export, &val38));
check_global(var_i64_import, i64, 4);

// Modify variables through API and check again.
wasm_val_t val33 = {.kind = WASM_F32, .of = {.f32 = 33}};
wasm_global_set(var_f32_import, &val33);
check_no_trap(wasm_global_set(var_f32_import, &val33));
wasm_val_t val34 = {.kind = WASM_I64, .of = {.i64 = 34}};
wasm_global_set(var_i64_import, &val34);
check_no_trap(wasm_global_set(var_i64_import, &val34));
wasm_val_t val37 = {.kind = WASM_F32, .of = {.f32 = 37}};
wasm_global_set(var_f32_export, &val37);
check_no_trap(wasm_global_set(var_f32_export, &val37));
wasm_val_t val38 = {.kind = WASM_I64, .of = {.i64 = 38}};
wasm_global_set(var_i64_export, &val38);
check_no_trap(wasm_global_set(var_i64_export, &val38));

check_global(var_f32_import, f32, 33);
check_global(var_i64_import, i64, 34);
Expand All @@ -194,6 +222,26 @@ int main(int argc, const char* argv[]) {
check_call(get_var_f32_export, f32, 37);
check_call(get_var_i64_export, i64, 38);

// Modify variables through unsafe API and check again.
wasm_val_t val33 = {.kind = WASM_F32, .of = {.f32 = 53}};
wasm_global_set_unsafe(var_f32_import, &val33);
wasm_val_t val34 = {.kind = WASM_I64, .of = {.i64 = 54}};
wasm_global_set_unsafe(var_i64_import, &val34);
wasm_val_t val37 = {.kind = WASM_F32, .of = {.f32 = 57}};
wasm_global_set_unsafe(var_f32_export, &val37);
wasm_val_t val38 = {.kind = WASM_I64, .of = {.i64 = 58}};
wasm_global_set_unsafe(var_i64_export, &val38);

check_global(var_f32_import, f32, 53);
check_global(var_i64_import, i64, 54);
check_global(var_f32_export, f32, 57);
check_global(var_i64_export, i64, 58);

check_call(get_var_f32_import, f32, 53);
check_call(get_var_i64_import, i64, 54);
check_call(get_var_f32_export, f32, 57);
check_call(get_var_i64_export, i64, 58);

// Modify variables through calls and check again.
wasm_val_t args73[] = { {.kind = WASM_F32, .of = {.f32 = 73}} };
wasm_func_call(set_var_f32_import, args73, NULL);
Expand Down
3 changes: 2 additions & 1 deletion example/hostref.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ int main(int argc, const char* argv[]) {
assert(val.kind == WASM_ANYREF);
check(val.of.ref, NULL);
val.of.ref = host2;
wasm_global_set(global, &val);
own wasm_trap_t *global_set_trap = wasm_global_set(global, &val);
assert(global_set_trap == NULL);
check(call_v_r(global_get), host2);
wasm_global_get(global, &val);
assert(val.kind == WASM_ANYREF);
Expand Down
9 changes: 8 additions & 1 deletion include/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,14 @@ WASM_API_EXTERN own wasm_global_t* wasm_global_new(
WASM_API_EXTERN own wasm_globaltype_t* wasm_global_type(const wasm_global_t*);

WASM_API_EXTERN void wasm_global_get(const wasm_global_t*, own wasm_val_t* out);
WASM_API_EXTERN void wasm_global_set(wasm_global_t*, const wasm_val_t*);

/// Perform a `global.set` operation. If the global is immutable or if the new
/// value has the wrong type, return an error.
WASM_API_EXTERN own wasm_trap_t* wasm_global_set(wasm_global_t*, const wasm_val_t*);

/// Similar to `wasm_global_set`, except with undefined behavior if the global
/// is immutable or if the new value has the wrong type.
WASM_API_EXTERN void wasm_global_set_unsafe(wasm_global_t*, const wasm_val_t*);


// Table Instances
Expand Down