Skip to content

Commit 5d77e3b

Browse files
elichaiTheBlueMatt
authored andcommitted
Add sanity checks for wasm32 for size and alignment of types
1 parent 3796194 commit 5d77e3b

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

secp256k1-sys/src/types.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub type c_char = i8;
1212

1313
/// This is an exact copy of https://doc.rust-lang.org/core/ffi/enum.c_void.html
1414
/// It should be Equivalent to C's void type when used as a pointer.
15-
///
15+
///
1616
/// We can replace this with `core::ffi::c_void` once we update the rustc version to >=1.30.0.
1717
#[repr(u8)]
1818
pub enum c_void {
@@ -40,3 +40,42 @@ mod tests {
4040
assert_eq!(TypeId::of::<types::c_char>(), TypeId::of::<raw::c_char>());
4141
}
4242
}
43+
44+
45+
#[doc(hidden)]
46+
#[cfg(target_arch = "wasm32")]
47+
pub fn sanity_checks_for_wasm() {
48+
use std::mem::{size_of, align_of};
49+
extern "C" {
50+
pub static WASM32_INT_SIZE: c_uchar;
51+
pub static WASM32_INT_ALIGN: c_uchar;
52+
53+
pub static WASM32_UNSIGNED_INT_SIZE: c_uchar;
54+
pub static WASM32_UNSIGNED_INT_ALIGN: c_uchar;
55+
56+
pub static WASM32_SIZE_T_SIZE: c_uchar;
57+
pub static WASM32_SIZE_T_ALIGN: c_uchar;
58+
59+
pub static WASM32_UNSIGNED_CHAR_SIZE: c_uchar;
60+
pub static WASM32_UNSIGNED_CHAR_ALIGN: c_uchar;
61+
62+
pub static WASM32_PTR_SIZE: c_uchar;
63+
pub static WASM32_PTR_ALIGN: c_uchar;
64+
}
65+
unsafe {
66+
assert_eq!(size_of::<c_int>(), WASM32_INT_SIZE as usize);
67+
assert_eq!(align_of::<c_int>(), WASM32_INT_ALIGN as usize);
68+
69+
assert_eq!(size_of::<c_uint>(), WASM32_UNSIGNED_INT_SIZE as usize);
70+
assert_eq!(align_of::<c_uint>(), WASM32_UNSIGNED_INT_ALIGN as usize);
71+
72+
assert_eq!(size_of::<size_t>(), WASM32_SIZE_T_SIZE as usize);
73+
assert_eq!(align_of::<size_t>(), WASM32_SIZE_T_ALIGN as usize);
74+
75+
assert_eq!(size_of::<c_uchar>(), WASM32_UNSIGNED_CHAR_SIZE as usize);
76+
assert_eq!(align_of::<c_uchar>(), WASM32_UNSIGNED_CHAR_ALIGN as usize);
77+
78+
assert_eq!(size_of::<*const ()>(), WASM32_PTR_SIZE as usize);
79+
assert_eq!(align_of::<*const ()>(), WASM32_PTR_ALIGN as usize);
80+
}
81+
}

secp256k1-sys/wasm-sysroot/stdio.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stddef.h>
2+
#define alignof(type) offsetof (struct { char c; type member; }, member)
3+
4+
extern const unsigned char WASM32_INT_SIZE = sizeof(int);
5+
extern const unsigned char WASM32_INT_ALIGN = alignof(int);
6+
7+
extern const unsigned char WASM32_UNSIGNED_INT_SIZE = sizeof(unsigned int);
8+
extern const unsigned char WASM32_UNSIGNED_INT_ALIGN = alignof(unsigned int);
9+
10+
extern const unsigned char WASM32_SIZE_T_SIZE = sizeof(size_t);
11+
extern const unsigned char WASM32_SIZE_T_ALIGN = alignof(size_t);
12+
13+
extern const unsigned char WASM32_UNSIGNED_CHAR_SIZE = sizeof(unsigned char);
14+
extern const unsigned char WASM32_UNSIGNED_CHAR_ALIGN = alignof(unsigned char);
15+
16+
extern const unsigned char WASM32_PTR_SIZE = sizeof(void*);
17+
extern const unsigned char WASM32_PTR_ALIGN = alignof(void*);

src/context.rs

+6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ mod std_only {
106106
impl<C: Context> Secp256k1<C> {
107107
/// Lets you create a context in a generic manner(sign/verify/all)
108108
pub fn gen_new() -> Secp256k1<C> {
109+
#[cfg(target_arch = "wasm32")]
110+
ffi::types::sanity_checks_for_wasm();
111+
109112
let buf = vec![0u8; Self::preallocate_size_gen()].into_boxed_slice();
110113
let ptr = Box::into_raw(buf);
111114
Secp256k1 {
@@ -192,6 +195,9 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
192195
impl<'buf, C: Context + 'buf> Secp256k1<C> {
193196
/// Lets you create a context with preallocated buffer in a generic manner(sign/verify/all)
194197
pub fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result<Secp256k1<C>, Error> {
198+
#[cfg(target_arch = "wasm32")]
199+
ffi::types::sanity_checks_for_wasm();
200+
195201
if buf.len() < Self::preallocate_size_gen() {
196202
return Err(Error::NotEnoughMemory);
197203
}

0 commit comments

Comments
 (0)