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

WASM support #100

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

WASM support #100

wants to merge 7 commits into from

Conversation

wolfv
Copy link

@wolfv wolfv commented Aug 23, 2023

This compiles and works quite nicely with --target wasm32-unknown-unknown.

#[no_mangle]
pub extern "C" fn rust_bzip2_wasm_shim_malloc(size: usize) -> *mut c_void {
unsafe {
let layout = Layout::from_size_align_unchecked(size, 1);
Copy link
Owner

Choose a reason for hiding this comment

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

I think this will need to change, notably the alignment of 1. The default malloc alignment is at least the alignment of usize and I think it's actually twice that, although it'd be good to check what malloc guarantees.

Comment on lines 22 to 23
// layout is not actually used
let layout = Layout::from_size_align_unchecked(1, 1);
Copy link
Owner

Choose a reason for hiding this comment

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

This unfortunately is not something I'm personally willing to rely on. The comment that this isn't used is only applicable with the dlmalloc system allocator which can be overridden and may not be true in the future.

I think that the malloc/free/calloc shims here will need to have a header/footer with the size to reconstruct the Layout here.

Copy link
Author

Choose a reason for hiding this comment

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

Do you think that plays a role for WASM though? Can you even use dlmalloc with WASM? I kinda copied this part - would have to dig deeper into the dealloc implementation.

pub extern "C" fn rust_bzip2_wasm_shim_calloc(nmemb: usize, size: usize) -> *mut c_void {
unsafe {
let layout = Layout::from_size_align_unchecked(size * nmemb, 1);
alloc(layout).cast()
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think this is correct because I believe the memory needs to be zeroed to respect the contract of the calloc function.

// wasm32-unknown-emscripten should not be included here.
// See: https://github.com/gyscos/zstd-rs/pull/209
let need_wasm_shim = env::var("TARGET").map_or(false, |target| {
target == "wasm32-unknown-unknown" || target == "wasm32-wasi"
Copy link
Owner

Choose a reason for hiding this comment

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

WASI shouldn't need a shim I believe since that should use the wasi-sdk C compiler which has all the support necessary.

I believe this PR should only affect wasm32-unknown-unknown

Copy link
Owner

Choose a reason for hiding this comment

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

Out of curiosity, where did this file come from?

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

I removed this file since I don't think we need it. This came from the other open PR.

bzip2-sys/lib.rs Outdated
Comment on lines 5 to 6
#[cfg(target_arch = "wasm32")]
mod wasm_shim;
Copy link
Owner

Choose a reason for hiding this comment

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

Could this be behind something like cfg(wasm_shim) which is printed out by the build script?

@@ -28,3 +28,5 @@ cc = "1.0"
[features]
# Enable this feature if you want to have a statically linked bzip2
static = []
std = [] # Use std types instead of libc in bindgen
Copy link
Owner

Choose a reason for hiding this comment

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

I think std and libc are the same, so this may not be necessary?

Copy link
Owner

Choose a reason for hiding this comment

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

Could the wasm-shim directory have a README.md explaining what it is, how it's used, and why it's necessary?

@wolfv
Copy link
Author

wolfv commented Mar 1, 2024

Would love a re-review & potential release. We're trying to test this in CI and it's a bit tricky to test against a branch :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants