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

Add core dump support to the runtime #6513

Merged
merged 11 commits into from
Aug 1, 2023
Merged

Add core dump support to the runtime #6513

merged 11 commits into from
Aug 1, 2023

Conversation

itsrainy
Copy link
Contributor

@itsrainy itsrainy commented Jun 2, 2023

This adds a configuration option and machinery to capture a core dump when
a trap occurs and attach it to the resulting anyhow::Error that gets bubbled up to
the caller. I've created a CoreDumpStack structure in the runtime, which is
currently just a backtrace until we design a way to recover the locals and
stack values when a trap occurs. When that CoreDumpStack gets converted to
a wasmtime::WasmCoreDump, we add additional information from the Store such
as globals, memories, and instance information.

A lot of this is mechanistically similar to how backtraces
are captured and attached to errors. Given that they both are attached as
context to anyhow::Errors, setting coredump_on_trap to true will supersede
any setting for wasm_backtrace.

@itsrainy itsrainy requested review from a team as code owners June 2, 2023 22:37
@itsrainy itsrainy requested review from pchickey and removed request for a team June 2, 2023 22:37
@github-actions github-actions bot added wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:config Issues related to the configuration of Wasmtime labels Jun 2, 2023
@github-actions
Copy link

github-actions bot commented Jun 2, 2023

Subscribe to Label Action

cc @peterhuene

This issue or pull request has been labeled: "wasmtime:api", "wasmtime:config"

Thus the following users have been cc'd because of the following labels:

  • peterhuene: wasmtime:api

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@github-actions
Copy link

github-actions bot commented Jun 2, 2023

Label Messager: wasmtime:config

It looks like you are changing Wasmtime's configuration options. Make sure to
complete this check list:

  • If you added a new Config method, you wrote extensive documentation for
    it.

    Our documentation should be of the following form:

    Short, simple summary sentence.
    
    More details. These details can be multiple paragraphs. There should be
    information about not just the method, but its parameters and results as
    well.
    
    Is this method fallible? If so, when can it return an error?
    
    Can this method panic? If so, when does it panic?
    
    # Example
    
    Optional example here.
    
  • If you added a new Config method, or modified an existing one, you
    ensured that this configuration is exercised by the fuzz targets.

    For example, if you expose a new strategy for allocating the next instance
    slot inside the pooling allocator, you should ensure that at least one of our
    fuzz targets exercises that new strategy.

    Often, all that is required of you is to ensure that there is a knob for this
    configuration option in wasmtime_fuzzing::Config (or one
    of its nested structs).

    Rarely, this may require authoring a new fuzz target to specifically test this
    configuration. See our docs on fuzzing for more details.

  • If you are enabling a configuration option by default, make sure that it
    has been fuzzed for at least two weeks before turning it on by default.


To modify this label's message, edit the .github/label-messager/wasmtime-config.md file.

To add new label messages or remove existing label messages, edit the
.github/label-messager.json configuration file.

Learn more.

@itsrainy itsrainy marked this pull request as draft June 6, 2023 21:10
Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good!

Before this can land, we still need some smoke tests under tests/all/coredump.rs though. Should test that we get the expected stack, modules, instances, globals, and memories in the resulting coredumps.

To be explicit, mostly for folks following along at home, I'm okay with waiting for follow up PRs to implement

  • serialization into the Wasm coredump format for wastime::WasmCoreDump
  • implementing wasmtime --coredump-on-trap=... in the CLI on top of wasmtime::WasmCoreDump.

A few nitpicks inline below.

Thanks!

crates/wasmtime/src/config.rs Outdated Show resolved Hide resolved
crates/wasmtime/src/coredump.rs Outdated Show resolved Hide resolved
crates/wasmtime/src/coredump.rs Outdated Show resolved Hide resolved
crates/wasmtime/src/coredump.rs Outdated Show resolved Hide resolved
crates/wasmtime/src/coredump.rs Show resolved Hide resolved
crates/wasmtime/src/trap.rs Outdated Show resolved Hide resolved
@itsrainy itsrainy force-pushed the rainy/coredumps branch 2 times, most recently from aa80bd1 to 98aea80 Compare June 29, 2023 23:04
@itsrainy
Copy link
Contributor Author

@fitzgen I added some smoke tests though I am still trying to figure out why exported Globals and Memories aren't getting included in the CoreDump.

Also in order to downcast the anyhow error to a WasmCoreDump, it needs to #[derive(Debug)]. To make that work I switched the Vec<Module> on the WasmCoreDump to be a vec of just the module names for now. Otherwise it seems like I would have to add #[derive(Debug)] to a whole bunch of things (Module, ModuleInner, Engine, EngineInner, Config, etc) which seemed a bit wide-sweeping.

@itsrainy itsrainy marked this pull request as ready for review June 29, 2023 23:22
@pchickey pchickey removed their request for review June 29, 2023 23:50
@fitzgen
Copy link
Member

fitzgen commented Jul 6, 2023

Also in order to downcast the anyhow error to a WasmCoreDump, it needs to #[derive(Debug)]. To make that work I switched the Vec<Module> on the WasmCoreDump to be a vec of just the module names for now. Otherwise it seems like I would have to add #[derive(Debug)] to a whole bunch of things (Module, ModuleInner, Engine, EngineInner, Config, etc) which seemed a bit wide-sweeping.

Ah, I think it would actually be better to make a custom Debug impl for WasmCoreDump than to derive(Debug) in this case. We can start with something like the following, and update it with useful diagnostic information over time as needed.

impl std::fmt::Debug for WasmCoreDump {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "<wasm core dump>")
    }
}

This avoids needing to do derive(Debug) on Module or anything like that and lets us continue to hold onto the modules inside WasmCoreDump.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Feel free to enqueue to merge when this is rebased and conflicts are resolved.

itsrainy added 9 commits July 27, 2023 11:04
This adds the machinery to capture a core dump when a trap occurs
and attach it to the resulting anyhow::Error that gets bubbled up to
the caller. I've created a CoreDumpStack structure in the runtime, which is
currently just a backtrace until we design a way to recover the locals
stack values when a trap occurs. When that CoreDumpStack gets converted to
a wasmtime::WasmCoreDump, we add additional information from the Store such
as globals, memories, and instance information.

A lot of this is mechanistically similar to how backtraces
are captured and attached to errors. Given that they both are attached as
context to anyhow::Errors, setting coredump_on_trap to true will supercede
any setting for wasm_backtrace.
@itsrainy itsrainy added this pull request to the merge queue Jul 27, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 27, 2023
@itsrainy itsrainy added this pull request to the merge queue Jul 28, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 28, 2023
@itsrainy itsrainy added this pull request to the merge queue Aug 1, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 1, 2023
@itsrainy itsrainy added this pull request to the merge queue Aug 1, 2023
Merged via the queue into main with commit 72b8718 Aug 1, 2023
@itsrainy itsrainy deleted the rainy/coredumps branch August 1, 2023 16:39
geekbeast pushed a commit to geekbeast/wasmtime that referenced this pull request Aug 1, 2023
* main: (47 commits)
  Add core dump support to the runtime (bytecodealliance#6513)
  Resource table tracks child relationships (bytecodealliance#6779)
  Wasmtime: Move `OnDemandInstanceAllocator` to its own module (bytecodealliance#6790)
  wasi: Test the stdio streams implementation (bytecodealliance#6764)
  Don't generate same-named imports in fact modules (bytecodealliance#6783)
  Wasmtime: Add support for Wasm tail calls (bytecodealliance#6774)
  Cranelift: Fix `ABIMachineSpec::gen_add_imm` for riscv64 (bytecodealliance#6780)
  Update the wasm-tools family of crates, disallow empty component types (bytecodealliance#6777)
  Fix broken link to WASI API documentation (bytecodealliance#6775)
  A bunch of cleanups for cranelift-codegen-meta (bytecodealliance#6772)
  Implement component-to-component calls with resources (bytecodealliance#6769)
  Ignore async_stack_size if async_support is disabled (bytecodealliance#6771)
  A bunch of minor cleanups (bytecodealliance#6767)
  Fix flaky tests in preview2 streams (bytecodealliance#6763)
  Refactor and simplify component trampolines (bytecodealliance#6751)
  Cranelift: Implement tail calls on riscv64 (bytecodealliance#6749)
  WASI Preview 2: rewrite streams and pollable implementation (bytecodealliance#6556)
  cranelift-wasm: Add support for translating Wasm tail calls to CLIF (bytecodealliance#6760)
  Cranelift: Get tail calls working on aarch64 (bytecodealliance#6723)
  Implement component model resources in Wasmtime (bytecodealliance#6691)
  ...
eduardomourar pushed a commit to eduardomourar/wasmtime that referenced this pull request Aug 18, 2023
* Add config method for whether or not to capture coredumps on trap

* Add core dump support to the runtime

This adds the machinery to capture a core dump when a trap occurs
and attach it to the resulting anyhow::Error that gets bubbled up to
the caller. I've created a CoreDumpStack structure in the runtime, which is
currently just a backtrace until we design a way to recover the locals
stack values when a trap occurs. When that CoreDumpStack gets converted to
a wasmtime::WasmCoreDump, we add additional information from the Store such
as globals, memories, and instance information.

A lot of this is mechanistically similar to how backtraces
are captured and attached to errors. Given that they both are attached as
context to anyhow::Errors, setting coredump_on_trap to true will supercede
any setting for wasm_backtrace.

* Address some PR feedback

* Fix docs

* Switch WasmCoreDump to have vec of module names rather than Modules

* Add smoketests for WasmCoreDump

* clean up tests

* Update memories and globals field to be store_

* add debug impl for wasmcoredump

* Remove duplicate imports

* ignore miri for wasm tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:config Issues related to the configuration of Wasmtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants