-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Update minijinja-cabi to avoid depending on usize bit length #601
Conversation
Hi! While trying to build minijinja-cabi for Wasm targets, I encountered an error due to a size mismatch on transmute operations, due to `usize` being 32 bits on those targets which breaks existing size assumptions, this attempts to fix this by doing the following: * Add `const VALUE_SIZE: usize = size_of::<Value>();` * Change `_opaque: [usize; 3],` to `_opaque: [u8; VALUE_SIZE],` in the `mj_value` struct. * Change `transmute::<Value, [usize; 3]>(value)` to `transmute::<Value, [u8; VALUE_SIZE]>(value)` in the `impl From<Value> for mj_value` block. Does this look like the correct approach?
The problem I see is that |
@mitsuhiko From what I understand using #[repr(C, align(8))]
pub struct mj_value {
_opaque: [u8; VALUE_SIZE],
} Would probably offer the proper alignement guarantees as long as |
EDIT: I see another option in this answer to a Stack Overflow question which suggests using a #[repr(C)]
pub struct mj_value {
_align: [Value; 0],
_opaque: [u8; VALUE_SIZE]
} The post does note that even then there are subtleties when dealing with FFI, however I wonder if this is actually an issue as long as the type is treated as an opaque value by the C code? Am I correct that as long as all these conditions are met:
Then we can safely transmute between |
This also adds tests to check for matching size and alignement
What does this even generate in C? Can you run bindgen to see what it spits out? I'm assuming it is not generating valid C. #[repr(C)]
pub struct mj_value {
_align: [Value; 0],
_opaque: [u8; VALUE_SIZE]
} |
I cannot actually get See also mozilla/cbindgen#593 |
Hi!
While trying to build minijinja-cabi for Wasm targets, I encountered an error due to a size mismatch on transmute operations, the reason is that
usize
is 32 bits on those targets which breaks existing assumption for usize to be 64 bits, this attempts to fix this by doing the following:const VALUE_SIZE: usize = size_of::<Value>();
_opaque: [usize; 3],
to_opaque: [u8; VALUE_SIZE],
in themj_value
struct.transmute::<Value, [usize; 3]>(value)
totransmute::<Value, [u8; VALUE_SIZE]>(value)
in theimpl From<Value> for mj_value
block.Does this look like the correct approach?
For reference here is the build error without those changes when running
cargo build -p minijinja-cabi --target wasm32-wasi