Skip to content

Commit

Permalink
Merge pull request rust-lang#3877 from tgross35/backport-butternut
Browse files Browse the repository at this point in the history
[0.2] Backport of two pull requests
  • Loading branch information
tgross35 committed Aug 29, 2024
2 parents fa7bb69 + 4d990a5 commit 567c854
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 70 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/full_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ jobs:
powerpc64le-unknown-linux-gnu,
s390x-unknown-linux-gnu,
riscv64gc-unknown-linux-gnu,
# FIXME: A recent nightly causes a linker failure:
# https://github.com/rust-lang/rust/issues/76679
# See this comment for more details:
# https://github.com/rust-lang/libc/pull/2225#issuecomment-880696737
#wasm32-wasi,
wasm32-wasip1,
wasm32-wasip2,
sparc64-unknown-linux-gnu,
wasm32-unknown-emscripten,
x86_64-linux-android,
Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const ALLOWED_CFGS: &'static [&'static str] = &[
"libc_thread_local",
"libc_underscore_const_names",
"libc_union",
"libc_ctest",
];

// Extra values to allow for check-cfg.
Expand Down
40 changes: 0 additions & 40 deletions ci/docker/wasm32-wasi/Dockerfile

This file was deleted.

2 changes: 0 additions & 2 deletions ci/docker/wasm32-wasi/clang.sh

This file was deleted.

16 changes: 16 additions & 0 deletions ci/docker/wasm32-wasip1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM ubuntu:24.04

COPY wasi.sh /
RUN bash /wasi.sh

# Note that `-D_WASI_EMULATED_PROCESS_CLOCKS` is used to enable access to
# clock-related defines even though they're emulated. Also note that the usage
# of `-Ctarget-feature=-crt-static` here forces usage of the external wasi-libc
# installed via `wasi-sdk` instead of the version that comes with the standard
# library.
ENV CARGO_TARGET_WASM32_WASIP1_RUNNER=wasmtime \
CARGO_TARGET_WASM32_WASIP1_LINKER=/opt/wasi-sdk/bin/clang \
CARGO_TARGET_WASM32_WASIP1_RUSTFLAGS="-lwasi-emulated-process-clocks -Ctarget-feature=-crt-static" \
CC_wasm32_wasip1=/opt/wasi-sdk/bin/clang \
CFLAGS_wasm32_wasip1=-D_WASI_EMULATED_PROCESS_CLOCKS \
PATH=$PATH:/rust/bin:/wasmtime
15 changes: 15 additions & 0 deletions ci/docker/wasm32-wasip2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM ubuntu:24.04

COPY wasi.sh /
RUN bash /wasi.sh

# Note that most of these are copied from `wasm32-wasip1/Dockerfile`
#
# FIXME: the `-Clink-arg` to export `cabi_realloc` is a bug in the target
# itself, this should be fixed upstream.
ENV CARGO_TARGET_WASM32_WASIP2_RUNNER=wasmtime \
CARGO_TARGET_WASM32_WASIP2_LINKER=/opt/wasi-sdk/bin/clang \
CARGO_TARGET_WASM32_WASIP2_RUSTFLAGS="-lwasi-emulated-process-clocks -Ctarget-feature=-crt-static -Clink-arg=-Wl,--export,cabi_realloc" \
CC_wasm32_wasip2=/opt/wasi-sdk/bin/clang \
CFLAGS_wasm32_wasip2=-D_WASI_EMULATED_PROCESS_CLOCKS \
PATH=$PATH:/rust/bin:/wasmtime
27 changes: 27 additions & 0 deletions ci/wasi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

set -ex

apt-get update
apt-get install -y --no-install-recommends \
ca-certificates \
curl \
clang \
xz-utils

# Wasmtime is used to execute tests and wasi-sdk is used to compile tests.
# Download appropriate versions here and configure various flags below.
#
# At the time of this writing wasmtime 24.0.0 is the latest release and
# wasi-sdk-24 is the latest release, that these numbers match is just
# coincidence.
wasmtime=24.0.0
wasi_sdk=24

curl -L https://github.com/bytecodealliance/wasmtime/releases/download/v$wasmtime/wasmtime-v$wasmtime-x86_64-linux.tar.xz | \
tar xJf -
mv wasmtime-v$wasmtime-x86_64-linux wasmtime

# The pre-built `*.deb` files for wasi-sdk install to `/opt/wasi-sdk`
curl -LO https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-$wasi_sdk/wasi-sdk-$wasi_sdk.0-x86_64-linux.deb
dpkg -i ./wasi-sdk-*.deb
35 changes: 24 additions & 11 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,7 @@ fn test_wasi(target: &str) {
"dirent.h",
"errno.h",
"fcntl.h",
"langinfo.h",
"limits.h",
"locale.h",
"malloc.h",
Expand All @@ -1541,6 +1542,7 @@ fn test_wasi(target: &str) {
"stdio.h",
"stdlib.h",
"string.h",
"sys/ioctl.h",
"sys/resource.h",
"sys/select.h",
"sys/socket.h",
Expand All @@ -1549,16 +1551,20 @@ fn test_wasi(target: &str) {
"sys/types.h",
"sys/uio.h",
"sys/utsname.h",
"sys/ioctl.h",
"time.h",
"unistd.h",
"wasi/api.h",
"wasi/libc.h",
"wasi/libc-find-relpath.h",
"wasi/libc-nocwd.h",
"wasi/libc.h",
"wchar.h",
}

// Currently `ctest2` doesn't support macros-in-static-expressions and will
// panic on them. That affects `CLOCK_*` defines in wasi to set this here
// to omit them.
cfg.cfg("libc_ctest", None);

cfg.type_name(move |ty, is_struct, is_union| match ty {
"FILE" | "fd_set" | "DIR" => ty.to_string(),
t if is_union => format!("union {}", t),
Expand All @@ -1577,20 +1583,27 @@ fn test_wasi(target: &str) {
}
});

// Looks like LLD doesn't merge duplicate imports, so if the Rust
// code imports from a module and the C code also imports from a
// module we end up with two imports of function pointers which
// import the same thing but have different function pointers
cfg.skip_fn_ptrcheck(|f| f.starts_with("__wasi"));
// These have a different and internal type in header files and are only
// used here to generate a pointer to them in bindings so skip these tests.
cfg.skip_static(|c| c.starts_with("_CLOCK_"));

cfg.skip_fn(|f| match f {
// This function doesn't actually exist in libc's header files
"__errno_location" => true,

// The `timeout` argument to this function is `*const` in Rust but
// mutable in C which causes a mismatch. Avoiding breakage by changing
// this in wasi-libc and instead accepting that this is slightly
// different.
"select" => true,

_ => false,
});

// 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");

// Currently Rust/clang disagree on function argument ABI, so skip these
// tests. For more info see WebAssembly/tool-conventions#88
cfg.skip_roundtrip(|_| true);

cfg.generate("../src/lib.rs", "main.rs");
}

Expand Down
34 changes: 22 additions & 12 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ pub const AT_SYMLINK_FOLLOW: c_int = 0x2;
pub const AT_REMOVEDIR: c_int = 0x4;
pub const UTIME_OMIT: c_long = 0xfffffffe;
pub const UTIME_NOW: c_long = 0xffffffff;
pub const S_IFIFO: mode_t = 49152;
pub const S_IFIFO: mode_t = 0o1_0000;
pub const S_IFCHR: mode_t = 8192;
pub const S_IFBLK: mode_t = 24576;
pub const S_IFDIR: mode_t = 16384;
pub const S_IFREG: mode_t = 32768;
pub const S_IFLNK: mode_t = 40960;
pub const S_IFSOCK: mode_t = 49152;
pub const S_IFMT: mode_t = 57344;
pub const S_IFMT: mode_t = 0o17_0000;
pub const S_IRWXO: mode_t = 0x7;
pub const S_IXOTH: mode_t = 0x1;
pub const S_IWOTH: mode_t = 0x2;
Expand Down Expand Up @@ -372,16 +372,26 @@ pub const _SC_PAGE_SIZE: ::c_int = _SC_PAGESIZE;
pub const _SC_IOV_MAX: c_int = 60;
pub const _SC_SYMLOOP_MAX: c_int = 173;

#[allow(unused_unsafe)] // `addr_of!(EXTERN_STATIC)` is now safe; remove `unsafe` when MSRV >= 1.82
pub static CLOCK_MONOTONIC: clockid_t = unsafe { clockid_t(ptr_addr_of!(_CLOCK_MONOTONIC)) };
#[allow(unused_unsafe)]
pub static CLOCK_PROCESS_CPUTIME_ID: clockid_t =
unsafe { clockid_t(ptr_addr_of!(_CLOCK_PROCESS_CPUTIME_ID)) };
#[allow(unused_unsafe)]
pub static CLOCK_REALTIME: clockid_t = unsafe { clockid_t(ptr_addr_of!(_CLOCK_REALTIME)) };
#[allow(unused_unsafe)]
pub static CLOCK_THREAD_CPUTIME_ID: clockid_t =
unsafe { clockid_t(ptr_addr_of!(_CLOCK_THREAD_CPUTIME_ID)) };
cfg_if! {
if #[cfg(libc_ctest)] {
// skip these constants when this is active because `ctest` currently
// panics on parsing the constants below
} else {
// `addr_of!(EXTERN_STATIC)` is now safe; remove `unsafe` when MSRV >= 1.82
#[allow(unused_unsafe)]
pub static CLOCK_MONOTONIC: clockid_t =
unsafe { clockid_t(ptr_addr_of!(_CLOCK_MONOTONIC)) };
#[allow(unused_unsafe)]
pub static CLOCK_PROCESS_CPUTIME_ID: clockid_t =
unsafe { clockid_t(ptr_addr_of!(_CLOCK_PROCESS_CPUTIME_ID)) };
#[allow(unused_unsafe)]
pub static CLOCK_REALTIME: clockid_t =
unsafe { clockid_t(ptr_addr_of!(_CLOCK_REALTIME)) };
#[allow(unused_unsafe)]
pub static CLOCK_THREAD_CPUTIME_ID: clockid_t =
unsafe { clockid_t(ptr_addr_of!(_CLOCK_THREAD_CPUTIME_ID)) };
}
}

pub const ABDAY_1: ::nl_item = 0x20000;
pub const ABDAY_2: ::nl_item = 0x20001;
Expand Down

0 comments on commit 567c854

Please sign in to comment.