-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Introduce an OOM-handling Error type for Wasmtime
#12163
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
Merged
+3,977
−8
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5d81e2c
Introduce an OOM-handling `Error` type for Wasmtime
fitzgen 547efba
Fill out more cargo.toml info
fitzgen 9060bc3
Add cargo vet entries for new crate
fitzgen 3907f7d
Use single quotes for string in Cargo.toml to work around publish scr…
fitzgen 2b871b0
Add the `wasmtime_error::Ok` function
fitzgen f8326d0
fix some stuff that was broken in the move to a module
fitzgen fbac398
maybe fix cargo vet?
fitzgen 9396fb4
Add comments about backtraces and OOM
fitzgen 6a9cfa6
Debug assert `ConcreteError<E>` and `DynError` layouts are compatible…
fitzgen 2f32711
Add reference to layout test and assertions to docs about compatible …
fitzgen 58a6e15
Use `#[track_caller]` to hide internal frame from error's backtrace
fitzgen 4ed675a
Fix typo
fitzgen 20b06e3
Pull various internal `ErrorExt` implementations out to top level
fitzgen 0add4dc
Switch to `0x1` as the representation of OOM in `OomOrDynError` packing
fitzgen 7422d1b
add audit-as-crates-io for wasmtime-internal-error
fitzgen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| [package] | ||
| authors.workspace = true | ||
| description = "INTERNAL: Wasmtime's universal error type's implementation" | ||
| edition.workspace = true | ||
| license = "Apache-2.0 WITH LLVM-exception" | ||
| name = "wasmtime-internal-error" | ||
| rust-version.workspace = true | ||
| version.workspace = true | ||
|
|
||
| [dependencies] | ||
| anyhow = { workspace = true, optional = true } | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [features] | ||
| # Enable the use of the `std` crate. | ||
| std = [] | ||
| # Enable backtraces. | ||
| backtrace = ["std"] | ||
| # Enable the `From<Error> for anyhow::Error` implementation and | ||
| # `Error::from_anyhow` constructor. | ||
| anyhow = ["dep:anyhow"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| use std::backtrace::Backtrace; | ||
| use std::sync::atomic::{AtomicBool, Ordering}; | ||
|
|
||
| static ENABLED: AtomicBool = AtomicBool::new(true); | ||
|
|
||
| fn enabled() -> bool { | ||
| ENABLED.load(Ordering::Acquire) | ||
| } | ||
|
|
||
| /// Forcibly disable capturing backtraces dynamically. | ||
| /// | ||
| /// XXX: This is only exposed for internal testing, to work around cargo | ||
| /// workspaces and feature resolution. This method may disappear or change | ||
| /// at any time. Instead of using this method, you should disable the | ||
| /// `backtrace` cargo feature. | ||
| #[doc(hidden)] | ||
| pub fn disable_backtrace() { | ||
| ENABLED.store(false, Ordering::Release) | ||
| } | ||
fitzgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #[track_caller] | ||
| pub fn capture() -> Backtrace { | ||
| if enabled() { | ||
| Backtrace::capture() | ||
| } else { | ||
| Backtrace::disabled() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| use super::{OutOfMemory, Result}; | ||
| use alloc::boxed::Box; | ||
| use core::alloc::Layout; | ||
| use core::ptr::NonNull; | ||
|
|
||
| /// Try to allocate a block of memory that fits the given layout, or return an | ||
| /// `OutOfMemory` error. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// Same as `alloc::alloc::alloc`: layout must have non-zero size. | ||
| #[inline] | ||
| pub(crate) unsafe fn try_alloc(layout: Layout) -> Result<NonNull<u8>, OutOfMemory> { | ||
| // Safety: same as our safety conditions. | ||
| debug_assert!(layout.size() > 0); | ||
| let ptr = unsafe { alloc::alloc::alloc(layout) }; | ||
|
|
||
| if let Some(ptr) = NonNull::new(ptr) { | ||
| Ok(ptr) | ||
| } else { | ||
| out_of_line_slow_path!(Err(OutOfMemory::new())) | ||
| } | ||
| } | ||
|
|
||
| /// Create a `Box<T>`, or return an `OutOfMemory` error. | ||
| #[inline] | ||
| pub(crate) fn try_box<T>(value: T) -> Result<Box<T>, OutOfMemory> { | ||
| let layout = alloc::alloc::Layout::new::<T>(); | ||
|
|
||
| if layout.size() == 0 { | ||
| // Safety: `Box` explicitly allows construction from dangling pointers | ||
| // (which are guaranteed non-null and aligned) for zero-sized types. | ||
| return Ok(unsafe { Box::from_raw(core::ptr::dangling::<T>().cast_mut()) }); | ||
| } | ||
|
|
||
| // Safety: layout size is non-zero. | ||
| let ptr = unsafe { try_alloc(layout)? }; | ||
|
|
||
| let ptr = ptr.cast::<T>(); | ||
|
|
||
| // Safety: The allocation succeeded, and it has `T`'s layout, so the pointer | ||
| // is valid for writing a `T`. | ||
| unsafe { | ||
| ptr.write(value); | ||
| } | ||
|
|
||
| // Safety: The pointer's memory block was allocated by the global allocator, | ||
| // is valid for `T`, and is initialized. | ||
| Ok(unsafe { Box::from_raw(ptr.as_ptr()) }) | ||
fitzgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.