diff --git a/Cargo-recent.lock b/Cargo-recent.lock index c56d0661..c1933b9e 100644 --- a/Cargo-recent.lock +++ b/Cargo-recent.lock @@ -503,7 +503,7 @@ dependencies = [ "miniscript", "santiago", "serde", - "simplicity-sys 0.6.1", + "simplicity-sys 0.6.2", ] [[package]] @@ -518,7 +518,7 @@ dependencies = [ [[package]] name = "simplicity-sys" -version = "0.6.1" +version = "0.6.2" dependencies = [ "bitcoin_hashes", "cc", diff --git a/Cargo.toml b/Cargo.toml index 076b1f9a..f8e4e15c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,14 +30,14 @@ ghost-cell = { version = "0.2.6", default-features = false } hashes = { package = "bitcoin_hashes", version = "0.14" } hex = { package = "hex-conservative", version = "0.2.1" } santiago = "1.3" -simplicity-sys = { version = "0.6.1", path = "./simplicity-sys" } +simplicity-sys = { version = "0.6.2", path = "./simplicity-sys" } serde = { version = "1.0.103", features = ["derive"], optional = true } [target.wasm32-unknown-unknown.dependencies] getrandom = { version = "0.2", features = ["js"] } [dev-dependencies] -simplicity-sys = { version = "0.6.1", path = "./simplicity-sys", features = [ +simplicity-sys = { version = "0.6.2", path = "./simplicity-sys", features = [ "test-utils", ] } diff --git a/simplicity-sys/Cargo.toml b/simplicity-sys/Cargo.toml index 622949e0..35a01ed6 100644 --- a/simplicity-sys/Cargo.toml +++ b/simplicity-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "simplicity-sys" -version = "0.6.1" +version = "0.6.2" license = "CC0-1.0" homepage = "https://github.com/BlockstreamResearch/rust-simplicity/" repository = "https://github.com/BlockstreamResearch/rust-simplicity/" diff --git a/simplicity-sys/src/c_jets/mod.rs b/simplicity-sys/src/c_jets/mod.rs index 2cc7d864..ae2fc852 100644 --- a/simplicity-sys/src/c_jets/mod.rs +++ b/simplicity-sys/src/c_jets/mod.rs @@ -28,36 +28,83 @@ pub mod exec_ffi; /// /// This will also us detect whenever there is a change in the underlying C representation pub fn sanity_checks() -> bool { - unsafe { - if std::mem::size_of::() != crate::ffi::c_sizeof_UWORD - || std::mem::align_of::() != crate::ffi::c_alignof_UWORD - { - return false; - } - - if std::mem::size_of::() != frame_ffi::c_sizeof_frameItem - || std::mem::size_of::() != c_env::elements::c_sizeof_rawBuffer - || std::mem::size_of::() != c_env::elements::c_sizeof_rawInput - || std::mem::size_of::() != c_env::elements::c_sizeof_rawOutput - || std::mem::size_of::() - != c_env::elements::c_sizeof_rawTransaction - || std::mem::size_of::() != c_env::elements::c_sizeof_txEnv - || std::mem::size_of::() != c_env::elements::c_sizeof_rawTapEnv - { - return false; - } - - if std::mem::align_of::() != frame_ffi::c_alignof_frameItem - || std::mem::align_of::() != c_env::elements::c_alignof_rawBuffer - || std::mem::align_of::() != c_env::elements::c_alignof_rawInput - || std::mem::align_of::() != c_env::elements::c_alignof_rawOutput - || std::mem::align_of::() - != c_env::elements::c_alignof_rawTransaction - || std::mem::align_of::() != c_env::elements::c_alignof_txEnv - || std::mem::align_of::() != c_env::elements::c_alignof_rawTapEnv - { - return false; - } + sanity_check_report().is_empty() +} + +#[must_use] +pub fn sanity_check_report() -> Vec { + let mut issues = Vec::new(); + + macro_rules! check_layout { + ($ty:ty, $c_size:path, $c_align:path, $name:expr) => {{ + let rust_size = std::mem::size_of::<$ty>(); + let rust_align = std::mem::align_of::<$ty>(); + let (c_size, c_align) = unsafe { ($c_size, $c_align) }; + + if rust_size != c_size { + issues.push(format!( + "{name}: size mismatch (rust={rust_size}, c={c_size})", + name = $name + )); + } + + if rust_align != c_align { + issues.push(format!( + "{name}: align mismatch (rust={rust_align}, c={c_align})", + name = $name + )); + } + }}; } - true + + check_layout!( + crate::ffi::UWORD, + crate::ffi::c_sizeof_UWORD, + crate::ffi::c_alignof_UWORD, + "UWORD" + ); + check_layout!( + CFrameItem, + frame_ffi::c_sizeof_frameItem, + frame_ffi::c_alignof_frameItem, + "CFrameItem" + ); + check_layout!( + elements::CRawBuffer, + elements::c_sizeof_rawBuffer, + elements::c_alignof_rawBuffer, + "CRawBuffer" + ); + check_layout!( + elements::CRawInput, + elements::c_sizeof_rawInput, + elements::c_alignof_rawInput, + "CRawInput" + ); + check_layout!( + elements::CRawOutput, + elements::c_sizeof_rawOutput, + elements::c_alignof_rawOutput, + "CRawOutput" + ); + check_layout!( + elements::CRawTransaction, + elements::c_sizeof_rawTransaction, + elements::c_alignof_rawTransaction, + "CRawTransaction" + ); + check_layout!( + elements::CTxEnv, + elements::c_sizeof_txEnv, + elements::c_alignof_txEnv, + "CTxEnv" + ); + check_layout!( + elements::CRawTapEnv, + elements::c_sizeof_rawTapEnv, + elements::c_alignof_rawTapEnv, + "CRawTapEnv" + ); + + issues } diff --git a/simplicity-sys/src/ffi.rs b/simplicity-sys/src/ffi.rs index d2e901d5..1cdbcf03 100644 --- a/simplicity-sys/src/ffi.rs +++ b/simplicity-sys/src/ffi.rs @@ -21,7 +21,7 @@ pub type c_size_t = usize; pub type c_uint_fast8_t = u8; #[cfg(any(target_os = "macos", target_os = "ios"))] pub type c_uint_fast16_t = u16; -#[cfg(any(target_os = "windows", target_os = "android"))] +#[cfg(target_os = "windows")] pub type c_uint_fast16_t = u32; #[cfg(target_arch = "wasm32")] pub type c_uint_fast16_t = u16; @@ -29,7 +29,6 @@ pub type c_uint_fast16_t = u16; target_os = "macos", target_os = "ios", target_os = "windows", - target_os = "android", target_arch = "wasm32" )))] pub type c_uint_fast16_t = usize; @@ -38,7 +37,6 @@ pub type c_uint_fast16_t = usize; target_os = "macos", target_os = "ios", target_os = "windows", - target_os = "android", target_arch = "wasm32" ))] pub type c_uint_fast32_t = u32; @@ -46,14 +44,13 @@ pub type c_uint_fast32_t = u32; target_os = "macos", target_os = "ios", target_os = "windows", - target_os = "android", target_arch = "wasm32" )))] pub type c_uint_fast32_t = usize; -#[cfg(target_arch = "wasm32")] -pub type c_uint_fast64_t = u64; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(target_pointer_width = "64")] pub type c_uint_fast64_t = usize; +#[cfg(not(target_pointer_width = "64"))] +pub type c_uint_fast64_t = u64; pub type c_uint_least32_t = u32; extern "C" {