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

wasmtime: failed to set thread exception port on macOS #6788

Closed
casimiro opened this issue Jul 31, 2023 · 1 comment
Closed

wasmtime: failed to set thread exception port on macOS #6788

casimiro opened this issue Jul 31, 2023 · 1 comment
Labels
bug Incorrect behavior in the current implementation that needs fixing

Comments

@casimiro
Copy link
Contributor

Test Case

Although the behaviour observed and here described does not seem dependent on the Wasm file itself at all, this is the one considered for tests:

(module
  (func $hello (import "" "hello"))
  (func (export "run") (call $hello))
)

Steps to Reproduce

This issue can be reproduced in a C program that invokes wasm_engine_new,
forks itself and then proceed to invoke the remaining C API calls in the child
process.

The engine created in the main process doesn't even need to be used
in the child process to trigger the segmentation fault.

The following C code, adapted from ./examples/hello.c, triggers the behaviour:

#include <stdio.h>
#include <stdlib.h>
#include <wasm.h>
#include <wasmtime.h>
#include <unistd.h>

static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);

static wasm_trap_t* hello_callback(
    void *env,
    wasmtime_caller_t *caller,
    const wasmtime_val_t *args,
    size_t nargs,
    wasmtime_val_t *results,
    size_t nresults
) {
  printf("Calling back...\n");
  printf("> Hello World!\n");
  return NULL;
}

int main() {
  wasm_engine_t* e = wasm_engine_new();
  wasm_engine_delete(e);

  printf("Initializing...\n");

  if (fork() == 0) {
    wasm_engine_t* engine = wasm_engine_new();
    wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
    wasmtime_context_t *context = wasmtime_store_context(store);

    FILE* file = fopen("examples/hello.wat", "r");
    fseek(file, 0L, SEEK_END);
    size_t file_size = ftell(file);
    fseek(file, 0L, SEEK_SET);

    wasm_byte_vec_t wat;
    wasm_byte_vec_new_uninitialized(&wat, file_size);
    fread(wat.data, file_size, 1, file);
    fclose(file);

    wasm_byte_vec_t wasm;
    wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
    if (error != NULL)
      exit_with_error("failed to parse wat", error, NULL);
    wasm_byte_vec_delete(&wat);

    printf("Compiling module...\n");
    wasmtime_module_t *module = NULL;
    error = wasmtime_module_new(engine, (uint8_t*) wasm.data, wasm.size, &module);
    wasm_byte_vec_delete(&wasm);
    if (error != NULL)
      exit_with_error("failed to compile module", error, NULL);

    printf("Creating callback...\n");
    wasm_functype_t *hello_ty = wasm_functype_new_0_0();
    wasmtime_func_t hello;
    wasmtime_func_new(context, hello_ty, hello_callback, NULL, NULL, &hello);

    printf("Instantiating module...\n");
    wasm_trap_t *trap = NULL;
    wasmtime_instance_t instance;
    wasmtime_extern_t import;
    import.kind = WASMTIME_EXTERN_FUNC;
    import.of.func = hello;
    error = wasmtime_instance_new(context, module, &import, 1, &instance, &trap);
    if (error != NULL || trap != NULL)
      exit_with_error("failed to instantiate", error, trap);

    printf("Extracting export...\n");
    wasmtime_extern_t run;
    wasmtime_instance_export_get(context, &instance, "run", 3, &run);

    printf("Calling export...\n");
    error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
    if (error != NULL || trap != NULL)
      exit_with_error("failed to call function", error, trap);

    printf("All finished!\n");

    wasmtime_module_delete(module);
    wasmtime_store_delete(store);
    wasm_engine_delete(engine);
  }

  return 0;
}

static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
  fprintf(stderr, "error: %s\n", message);
  wasm_byte_vec_t error_message;
  if (error != NULL) {
    wasmtime_error_message(error, &error_message);
    wasmtime_error_delete(error);
  } else {
    wasm_trap_message(trap, &error_message);
    wasm_trap_delete(trap);
  }
  fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
  wasm_byte_vec_delete(&error_message);
  exit(1);
}

Expected Results

I expect to have something like this from the standard output:

$ ./wasmtime-hello
Initializing...
Compiling module...
Creating callback...
Instantiating module...
Extracting export...
Calling export...
Calling back...
> Hello World!
All finished!

Actual Results

./wasmtime-hello
Initializing...
ec2-user@ip-10-0-1-73 build % Compiling module...
Creating callback...
Instantiating module...
Extracting export...
Calling export...
thread '<unnamed>' panicked at 'assertion failed: `(left == right)`
  left: `268435466`,
 right: `0`: failed to set thread exception port', crates/runtime/src/traphandlers/macos.rs:474:9
stack backtrace:
   0:        0x102185d28 - std::backtrace_rs::backtrace::libunwind::trace::h63c1c2c6ab21e780
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:        0x102185d28 - std::backtrace_rs::backtrace::trace_unsynchronized::hf38aadaccf972d0c
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:        0x102185d28 - std::sys_common::backtrace::_print_fmt::hbc7fc8ab62c7587a
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:65:5
   3:        0x102185d28 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h3fbf720fbb1cb41b
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:44:22
   4:        0x1021d0e38 - core::fmt::rt::Argument::fmt::h6f942517dda9fa22
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/fmt/rt.rs:138:9
   5:        0x1021d0e38 - core::fmt::write::h8ab6230d6f2a4204
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/fmt/mod.rs:1094:21
   6:        0x10217be04 - std::io::Write::write_fmt::h15ecdca1916a179e
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/io/mod.rs:1713:15
   7:        0x102185b7c - std::sys_common::backtrace::_print::hb9cd8d0f949f9219
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:47:5
   8:        0x102185b7c - std::sys_common::backtrace::print::hb6e94bab886a9ccf
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:34:9
   9:        0x1021885e0 - std::panicking::default_hook::{{closure}}::h4a0e30e90cb1c7bb
  10:        0x1021883e8 - std::panicking::default_hook::h0bb5bbf65ac7c64d
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:288:9
  11:        0x102188b68 - std::panicking::rust_panic_with_hook::h17facd9c53870157
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:705:13
  12:        0x102188a74 - std::panicking::begin_panic_handler::{{closure}}::h9eab8195c369d860
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:597:13
  13:        0x102186108 - std::sys_common::backtrace::__rust_end_short_backtrace::hce5f67454da3493d
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:151:18
  14:        0x1021887e8 - rust_begin_unwind
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:593:5
  15:        0x102257afc - core::panicking::panic_fmt::hc7e96873bfc1c7ba
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/panicking.rs:67:14
  16:        0x102257df4 - core::panicking::assert_failed_inner::h9c0c629e544785f4
  17:        0x102244f1c - core::panicking::assert_failed::hfe838f74eaaf86df
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/panicking.rs:228:5
  18:        0x102244a54 - wasmtime_runtime::traphandlers::macos::lazy_per_thread_init::h3f4bc6e0342ace6f
                               at /Users/ec2-user/wasmtime/crates/runtime/src/traphandlers/macos.rs:474:9
  19:        0x101dd2880 - wasmtime_runtime::traphandlers::tls::raw::replace::{{closure}}::h7b1f85cce42a81fb
                               at /Users/ec2-user/wasmtime/crates/runtime/src/traphandlers.rs:577:21
  20:        0x101dcb1f8 - std::thread::local::LocalKey<T>::try_with::hbcc1f52fa2ccce66
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/thread/local.rs:270:16
  21:        0x101dcb0a4 - std::thread::local::LocalKey<T>::with::hfb6aa9a623e59b91
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/thread/local.rs:246:9
  22:        0x100cd7eec - wasmtime_runtime::traphandlers::tls::raw::replace::h7f17824cf711ea1d
                               at /Users/ec2-user/wasmtime/crates/runtime/src/traphandlers.rs:571:13
  23:        0x100cdd318 - wasmtime_runtime::traphandlers::tls::set::hdca844b261078194
                               at /Users/ec2-user/wasmtime/crates/runtime/src/traphandlers.rs:678:20
  24:        0x100e4d320 - wasmtime_runtime::traphandlers::<impl wasmtime_runtime::traphandlers::call_thread_state::CallThreadState>::with::h4ba7e7d0ac56b8e0
                               at /Users/ec2-user/wasmtime/crates/runtime/src/traphandlers.rs:409:19
  25:        0x100e2bce0 - wasmtime_runtime::traphandlers::catch_traps::h60659c6a959930da
                               at /Users/ec2-user/wasmtime/crates/runtime/src/traphandlers.rs:224:18
  26:        0x100d44e00 - wasmtime::func::invoke_wasm_and_catch_traps::h708e91479a89ea5b
                               at /Users/ec2-user/wasmtime/crates/wasmtime/src/func.rs:1306:22
  27:        0x100d463e0 - wasmtime::func::Func::call_unchecked_raw::hf945319dac3e3b38
                               at /Users/ec2-user/wasmtime/crates/wasmtime/src/func.rs:898:9
  28:        0x100d46240 - wasmtime::func::Func::call_unchecked::h476a636c11f30715
                               at /Users/ec2-user/wasmtime/crates/wasmtime/src/func.rs:889:9
  29:        0x100d49740 - wasmtime::func::Func::call_impl::h1f5328d2bcad75b9
                               at /Users/ec2-user/wasmtime/crates/wasmtime/src/func.rs:1053:13
  30:        0x100d46db8 - wasmtime::func::Func::call::h29b107347c6df6cb
                               at /Users/ec2-user/wasmtime/crates/wasmtime/src/func.rs:842:9
  31:        0x100de21ec - wasmtime::func::wasmtime_func_call::{{closure}}::hf36e13d36f2d5fed
                               at /Users/ec2-user/wasmtime/crates/c-api/src/func.rs:342:9
  32:        0x100e50828 - core::ops::function::FnOnce::call_once::h0cbc2d0828395d84
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/ops/function.rs:250:5
  33:        0x100e4f59c - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h9d66afbeb5ef0fe9
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/panic/unwind_safe.rs:271:9
  34:        0x100cc57d0 - std::panicking::try::do_call::he9414f52b0a3effd
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:500:40
  35:        0x100cc9940 - ___rust_try
  36:        0x100cbedbc - std::panicking::try::h334b18aeaada54bb
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:464:19
  37:        0x100e04304 - std::panic::catch_unwind::h51526ebcdfa5ce5c
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panic.rs:142:14
  38:        0x100db90bc - wasmtime_func_call
                               at /Users/ec2-user/wasmtime/crates/c-api/src/func.rs:341:18
  39:        0x100c5e7c4 - _main
error: failed to call function
assertion failed: `(left == right)`
  left: `268435466`,
 right: `0`: failed to set thread exception port

Versions and Environment

Wasmtime version or commit: v8.0.1

Operating system: macOS

Architecture: x86, arm64

Extra Info

Although this behaviour can be consistently observed, it doesn't always happen.
It also seems to be more easily observed in arm64 than in x86.

@casimiro casimiro added the bug Incorrect behavior in the current implementation that needs fixing label Jul 31, 2023
@casimiro
Copy link
Contributor Author

Closing this issue as this behaviour has already been reported at: #6785

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect behavior in the current implementation that needs fixing
Projects
None yet
Development

No branches or pull requests

1 participant