-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Provide cabi_realloc on wasm32-wasip2 by default #122411
Merged
bors
merged 2 commits into
rust-lang:master
from
alexcrichton:wasm32-wasip2-cabi-realloc
Apr 3, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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,65 @@ | ||
//! This module contains a canonical definition of the `cabi_realloc` function | ||
//! for the component model. | ||
//! | ||
//! The component model's canonical ABI for representing datatypes in memory | ||
//! makes use of this function when transferring lists and strings, for example. | ||
//! This function behaves like C's `realloc` but also takes alignment into | ||
//! account. | ||
//! | ||
//! Components are notably not required to export this function, but nearly | ||
//! all components end up doing so currently. This definition in the standard | ||
//! library removes the need for all compilations to define this themselves. | ||
//! | ||
//! More information about the canonical ABI can be found at | ||
//! <https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md> | ||
//! | ||
//! Note that the name of this function is not standardized in the canonical ABI | ||
//! at this time. Instead it's a convention of the "componentization process" | ||
//! where a core wasm module is converted to a component to use this name. | ||
//! Additionally this is not the only possible definition of this function, so | ||
//! this is defined as a "weak" symbol. This means that other definitions are | ||
//! allowed to overwrite it if they are present in a compilation. | ||
|
||
use crate::alloc::{self, Layout}; | ||
use crate::ptr; | ||
|
||
#[used] | ||
static FORCE_CODEGEN_OF_CABI_REALLOC: unsafe extern "C" fn( | ||
*mut u8, | ||
usize, | ||
usize, | ||
usize, | ||
) -> *mut u8 = cabi_realloc; | ||
|
||
#[linkage = "weak"] | ||
#[no_mangle] | ||
pub unsafe extern "C" fn cabi_realloc( | ||
sunfishcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
old_ptr: *mut u8, | ||
old_len: usize, | ||
align: usize, | ||
new_len: usize, | ||
) -> *mut u8 { | ||
let layout; | ||
let ptr = if old_len == 0 { | ||
if new_len == 0 { | ||
return ptr::without_provenance_mut(align); | ||
} | ||
layout = Layout::from_size_align_unchecked(new_len, align); | ||
alloc::alloc(layout) | ||
} else { | ||
debug_assert_ne!(new_len, 0, "non-zero old_len requires non-zero new_len!"); | ||
layout = Layout::from_size_align_unchecked(old_len, align); | ||
alloc::realloc(old_ptr, layout, new_len) | ||
}; | ||
if ptr.is_null() { | ||
// Print a nice message in debug mode, but in release mode don't | ||
// pull in so many dependencies related to printing so just emit an | ||
// `unreachable` instruction. | ||
if cfg!(debug_assertions) { | ||
alloc::handle_alloc_error(layout); | ||
} else { | ||
super::abort_internal(); | ||
} | ||
} | ||
return ptr; | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to rename this module to
wasip1
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! I'll do that as a follow-up