Skip to content

Commit

Permalink
Fix dirent to match WASI libc's definition.
Browse files Browse the repository at this point in the history
dirent contains a flexible array member, so don't test its sizeof, don't
allow it to be copied, and don't represent it with an artificial size.
  • Loading branch information
sunfishcode committed Apr 23, 2019
1 parent a625c69 commit ef7ae73
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 4 additions & 0 deletions libc-test/build.rs
Expand Up @@ -1931,5 +1931,9 @@ fn test_wasi(target: &str) {
// import the same thing but have different function pointers
cfg.skip_fn_ptrcheck(|f| f.starts_with("__wasi"));

// d_name is declared as a flexible array in WASI libc, so it
// doesn't support sizeof.
cfg.skip_field(|s, field| s == "dirent" && field == "d_name");

cfg.generate("../src/lib.rs", "main.rs");
}
20 changes: 14 additions & 6 deletions src/wasi.rs
Expand Up @@ -141,12 +141,6 @@ s! {
fds_bits: [c_ulong; FD_SETSIZE / ULONG_SIZE],
}

pub struct dirent {
pub d_ino: ino_t,
pub d_type: c_uchar,
pub d_name: [c_char; 1024],
}

pub struct lconv {
pub decimal_point: *mut c_char,
pub thousands_sep: *mut c_char,
Expand Down Expand Up @@ -303,6 +297,20 @@ s_no_extra_traits! {

}

// Declare dirent outside of s! so that it doesn't implement Copy, Eq, Hash,
// etc., since it contains a flexible array member with a dynamic size.
#[repr(C)]
#[allow(missing_copy_implementations)]
#[cfg_attr(feature = "extra_traits", derive(Debug))]
pub struct dirent {
pub d_ino: ino_t,
pub d_type: c_uchar,
/// d_name is declared in WASI libc as a flexible array member, which
/// can't be directly expressed in Rust. As an imperfect workaround,
/// declare it as a zero-length array instead.
pub d_name: [c_char; 0],
}

// intentionally not public, only used for fd_set
cfg_if! {
if #[cfg(target_pointer_width = "32")] {
Expand Down

0 comments on commit ef7ae73

Please sign in to comment.