Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/wasi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ apt-get install -y --no-install-recommends \
# Wasmtime is used to execute tests and wasi-sdk is used to compile tests.
# Download appropriate versions here and configure various flags below.
wasmtime=35.0.0
wasi_sdk=25
wasi_sdk=27

curl -L https://github.com/bytecodealliance/wasmtime/releases/download/v$wasmtime/wasmtime-v$wasmtime-x86_64-linux.tar.xz |
tar xJf -
Expand Down
1 change: 1 addition & 0 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,7 @@ fn test_wasi(target: &str) {
[p2]: "netinet/in.h",
[p2]: "netinet/tcp.h",
"poll.h",
"pthread.h",
"sched.h",
"stdbool.h",
"stddef.h",
Expand Down
116 changes: 116 additions & 0 deletions src/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub type wchar_t = i32;
pub type nl_item = c_int;
pub type __wasi_rights_t = u64;
pub type locale_t = *mut __locale_struct;
pub type pthread_t = *mut c_void;
pub type pthread_once_t = c_int;
pub type pthread_key_t = c_uint;
pub type pthread_spinlock_t = c_int;

s_no_extra_traits! {
#[repr(align(16))]
Expand Down Expand Up @@ -170,6 +174,47 @@ s! {
__nfds: usize,
__fds: [c_int; FD_SETSIZE as usize],
}

#[repr(align(4))]
pub struct pthread_attr_t {
size: [u8; 36],
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is 64-bit wasi something we'll need to support? If so, maybe [c_ulong; if size_of::<c_long>() == 8 { 7 } else { 9 }] to account for that, and it's a bit easier to match up to the source anyway (assuming this is the right definition https://github.com/WebAssembly/wasi-libc/blob/6b45da5b05bc0edda355a6de46101d4b21631985/libc-top-half/musl/include/alltypes.h.in#L108). Same kind of thing for the others from that block.

Copy link
Member Author

Choose a reason for hiding this comment

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

Eventually, yes. For now though I don't think it's worth it. Currently there is no 64-bit WASI target (WebAssembly/component-model#22) so there's nothing to test against. I'm happy, however, to come around later and fix this once 64-bit targets do exist though!

Copy link
Member Author

Choose a reason for hiding this comment

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

I like the idea of using a more representative type internally, though, so I've switched to that.


pub struct pthread_mutexattr_t {
__attr: c_uint,
}

pub struct pthread_condattr_t {
__attr: c_uint,
}

pub struct pthread_barrierattr_t {
__attr: c_uint,
}

pub struct pthread_rwlockattr_t {
__attr: [c_uint; 2],
}

#[repr(align(4))]
pub struct pthread_cond_t {
size: [u8; 48],
}

#[repr(align(4))]
pub struct pthread_mutex_t {
size: [u8; 24],
}

#[repr(align(4))]
pub struct pthread_rwlock_t {
size: [u8; 32],
}

#[repr(align(4))]
pub struct pthread_barrier_t {
size: [u8; 20],
}
}

// Declare dirent outside of s! so that it doesn't implement Copy, Eq, Hash,
Expand Down Expand Up @@ -262,7 +307,9 @@ pub const DT_BLK: u8 = 1;
pub const DT_CHR: u8 = 2;
pub const DT_DIR: u8 = 3;
pub const DT_REG: u8 = 4;
pub const DT_FIFO: u8 = 6;
pub const DT_LNK: u8 = 7;
pub const DT_SOCK: u8 = 20;
pub const FIONREAD: c_int = 1;
pub const FIONBIO: c_int = 2;
pub const F_OK: c_int = 0;
Expand Down Expand Up @@ -438,6 +485,9 @@ pub const NOEXPR: crate::nl_item = 0x50001;
pub const YESSTR: crate::nl_item = 0x50002;
pub const NOSTR: crate::nl_item = 0x50003;

pub const PTHREAD_STACK_MIN: usize = 2048;
pub const TIMER_ABSTIME: c_int = 1;

f! {
pub fn FD_ISSET(fd: c_int, set: *const fd_set) -> bool {
let set = &*set;
Expand Down Expand Up @@ -843,6 +893,72 @@ extern "C" {
pub fn arc4random_uniform(a: u32) -> u32;

pub fn __errno_location() -> *mut c_int;

pub fn chmod(path: *const c_char, mode: mode_t) -> c_int;
pub fn fchmod(fd: c_int, mode: mode_t) -> c_int;
pub fn realpath(pathname: *const c_char, resolved: *mut c_char) -> *mut c_char;

pub fn pthread_self() -> pthread_t;
pub fn pthread_create(
native: *mut pthread_t,
attr: *const pthread_attr_t,
f: extern "C" fn(*mut c_void) -> *mut c_void,
value: *mut c_void,
) -> c_int;
pub fn pthread_equal(t1: pthread_t, t2: pthread_t) -> c_int;
pub fn pthread_join(native: pthread_t, value: *mut *mut c_void) -> c_int;
pub fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int;
pub fn pthread_attr_destroy(attr: *mut pthread_attr_t) -> c_int;
pub fn pthread_attr_getstacksize(attr: *const pthread_attr_t, stacksize: *mut size_t) -> c_int;
pub fn pthread_attr_setstacksize(attr: *mut pthread_attr_t, stack_size: size_t) -> c_int;
pub fn pthread_attr_setdetachstate(attr: *mut pthread_attr_t, state: c_int) -> c_int;
pub fn pthread_detach(thread: pthread_t) -> c_int;

pub fn pthread_key_create(
key: *mut pthread_key_t,
dtor: Option<unsafe extern "C" fn(*mut c_void)>,
) -> c_int;
pub fn pthread_key_delete(key: pthread_key_t) -> c_int;
pub fn pthread_getspecific(key: pthread_key_t) -> *mut c_void;
pub fn pthread_setspecific(key: pthread_key_t, value: *const c_void) -> c_int;
pub fn pthread_mutex_init(
lock: *mut pthread_mutex_t,
attr: *const pthread_mutexattr_t,
) -> c_int;
pub fn pthread_mutex_destroy(lock: *mut pthread_mutex_t) -> c_int;
pub fn pthread_mutex_lock(lock: *mut pthread_mutex_t) -> c_int;
pub fn pthread_mutex_trylock(lock: *mut pthread_mutex_t) -> c_int;
pub fn pthread_mutex_unlock(lock: *mut pthread_mutex_t) -> c_int;

pub fn pthread_mutexattr_init(attr: *mut pthread_mutexattr_t) -> c_int;
pub fn pthread_mutexattr_destroy(attr: *mut pthread_mutexattr_t) -> c_int;
pub fn pthread_mutexattr_settype(attr: *mut pthread_mutexattr_t, _type: c_int) -> c_int;

pub fn pthread_cond_init(cond: *mut pthread_cond_t, attr: *const pthread_condattr_t) -> c_int;
pub fn pthread_cond_wait(cond: *mut pthread_cond_t, lock: *mut pthread_mutex_t) -> c_int;
pub fn pthread_cond_timedwait(
cond: *mut pthread_cond_t,
lock: *mut pthread_mutex_t,
abstime: *const timespec,
) -> c_int;
pub fn pthread_cond_signal(cond: *mut pthread_cond_t) -> c_int;
pub fn pthread_cond_broadcast(cond: *mut pthread_cond_t) -> c_int;
pub fn pthread_cond_destroy(cond: *mut pthread_cond_t) -> c_int;
pub fn pthread_condattr_init(attr: *mut pthread_condattr_t) -> c_int;
pub fn pthread_condattr_destroy(attr: *mut pthread_condattr_t) -> c_int;

pub fn pthread_rwlock_init(
lock: *mut pthread_rwlock_t,
attr: *const pthread_rwlockattr_t,
) -> c_int;
pub fn pthread_rwlock_destroy(lock: *mut pthread_rwlock_t) -> c_int;
pub fn pthread_rwlock_rdlock(lock: *mut pthread_rwlock_t) -> c_int;
pub fn pthread_rwlock_tryrdlock(lock: *mut pthread_rwlock_t) -> c_int;
pub fn pthread_rwlock_wrlock(lock: *mut pthread_rwlock_t) -> c_int;
pub fn pthread_rwlock_trywrlock(lock: *mut pthread_rwlock_t) -> c_int;
pub fn pthread_rwlock_unlock(lock: *mut pthread_rwlock_t) -> c_int;
pub fn pthread_rwlockattr_init(attr: *mut pthread_rwlockattr_t) -> c_int;
pub fn pthread_rwlockattr_destroy(attr: *mut pthread_rwlockattr_t) -> c_int;
}

cfg_if! {
Expand Down
Loading