Stream: git-wasmtime

Topic: wasmtime / issue #6788 wasmtime: failed to set thread exc...


view this post on Zulip Wasmtime GitHub notifications bot (Jul 31 2023 at 11:45):

casimiro opened issue #6788:

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::invok
[message truncated]

view this post on Zulip Wasmtime GitHub notifications bot (Jul 31 2023 at 11:45):

casimiro added the bug label to Issue #6788.

view this post on Zulip Wasmtime GitHub notifications bot (Jul 31 2023 at 14:39):

casimiro commented on issue #6788:

Closing this issue as this behaviour has already been reported at: https://github.com/bytecodealliance/wasmtime/issues/6785

view this post on Zulip Wasmtime GitHub notifications bot (Jul 31 2023 at 14:39):

casimiro closed issue #6788:

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::invok
[message truncated]


Last updated: Dec 23 2024 at 12:05 UTC