-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 basic wasm32: C types and malloc/realloc/free. #1092
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Thanks for the PR! This crate, however, is a small shim around the underlying system's exported functionality and doesn't itself implement polyfills or shims like this. A polyfill like this is left to higher-level crates which wrap The typedefs also don't have a clear definition in C as there isn't a widely-agreed-upon definition of the C ABI yet (aka what sizes each of the types in C are). There's an early effort to define a hopefully standard sysroot implementation for wasm (with reference types and such), and we'll be sure to want to follow that closely for type definitions! |
The problem with wrapping libc is that I'm trying to use a crate that
assumes libc::malloc is available, so I have two options: patch that crate
[1] or patch libc.
But the situation with the types is not so clear. C usually closely follows
the ABI of the platform, so wasm32-unknown-unknown should have the same
type as wasm32-unknown-emscripten. And these would be much more complicated
to strip from all the crates. I guess that is why there is a switch.rs [2]
(Nintendo?) with just types.
Would you accept a PR with just C types? Then I could try another one to
the miniz_oxide crate removing the malloc usage...
[1]: https://github.com/Frommi/miniz_oxide/blob/8a498b2679449a992714228c4a10c8b05d61c81c/src/lib_oxide.rs#L131
[2]: https://github.com/rust-lang/libc/blob/master/src/switch.rs
|
Yes in this case you'll need to patch the crate for wasm to not use libc types/methods. We don't heavily review all platforms added to libc, so they're not all held to as strict of a standard as the main tier 1 targets. |
Unfortunately, So until |
It's true that this is unfortunate! Note, however, that flate2 has an issue for wasm support which was fixed in Frommi/miniz_oxide#35 and is waiting for an new version of the crate to be published. That's doing the "proper" fix here which is to wrap |
Ah, that's great, thanks for the link. I've just tried that and now What would be the proper solution for this? Making Or maybe creating a new crate
IMHO, we would better having at least the C types in |
@rodrigorc it's possible yes for a |
@alexcrichton I understand About the other types, however, I've noticed that And curiously many of the other C types are defined in Then, should we wait for clang-wasm to arrive in order to add the C types to this crate? |
Yeah let's wait for clang-wasm to ship before adding the types to this crate I think, and |
Ok, I'll close this PR and keep using my clib fork until clang-wasm is deployed (cargo makes it so easy). By any chance, do you have any link about the clang-wasm development? Thanks for you attention! |
Ah unfortunately I'm not sure how to stay in tune with wasm development in clang, but I do know that it's merged as a mainstream target so the next release (Clang 8) will have wasm support by default |
This adds a wasm32 implementation for `free`, `malloc` and `realloc`. Originally written by @rodrigorc in rust-lang/libc#1092 and adapted.
This adds a wasm32 implementation for `free`, `malloc` and `realloc`. Originally written by @rodrigorc in rust-lang/libc#1092 and adapted. NOTE: This is only enabled for `wasm32-unknown-unknown` ([`target_arch = "wasm32"`](https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch), [`target_os = "unknown"`](https://doc.rust-lang.org/reference/conditional-compilation.html#target_os)), because other wasm targets (like `wasm32-unknown-emscripten`) may provide a `libc` implementation.
This adds a wasm32 implementation for `free`, `malloc` and `realloc`. Originally written by @rodrigorc in rust-lang/libc#1092 and adapted. NOTE: This is only enabled for `wasm32-unknown-unknown` ([`target_arch = "wasm32"`](https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch), [`target_os = "unknown"`](https://doc.rust-lang.org/reference/conditional-compilation.html#target_os)), because other wasm targets (like `wasm32-unknown-emscripten`) may provide a `libc` implementation.
This adds a wasm32 implementation for `free`, `malloc` and `realloc`. Originally written by @rodrigorc in rust-lang/libc#1092 and adapted. NOTE: This is only enabled for `wasm32-unknown-unknown` ([`target_arch = "wasm32"`](https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch), [`target_os = "unknown"`](https://doc.rust-lang.org/reference/conditional-compilation.html#target_os)), because other wasm targets (like `wasm32-unknown-emscripten`) may provide a `libc` implementation.
This adds a wasm32 implementation for `free`, `malloc` and `realloc`. Originally written by @rodrigorc in rust-lang/libc#1092 and adapted. NOTE: This is only enabled for `wasm32-unknown-unknown` ([`target_arch = "wasm32"`](https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch), [`target_os = "unknown"`](https://doc.rust-lang.org/reference/conditional-compilation.html#target_os)), because other wasm targets (like `wasm32-unknown-emscripten`) may provide a `libc` implementation.
Currently trying to compile "zip" modules from crates.io (without bzip2) for wasm32-unknown-unknown fails because of libc usage, even though all the code is native rust.
It fails because even it is full Rust code, it uses types from libc, such as c_int and c_void, for compatibility with other C bindings.
I see no reason why wasm32 should not have the C types defined, just like the Switch.
Another issue is that the deflate library uses malloc/free from libc. These can be easily emulated with the global allocator, so we can emulate them here.