Skip to content

Commit

Permalink
Update CI nightly to the latest nightly (#9355)
Browse files Browse the repository at this point in the history
* Update CI nightly to the latest nightly

Brings in a new warning which fires on generating a safe (either `&` or
`&mut`) reference to a `static mut`. This then additionally refactors
code to use `addr_of_mut!` appropriately and/or switch to other
alternatives.

* Fix macos build
  • Loading branch information
alexcrichton authored Oct 2, 2024
1 parent bdb834b commit 1b4ea3a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .github/actions/install-rust/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ runs:
elif [ "${{ inputs.toolchain }}" = "msrv" ]; then
echo "version=1.$msrv.0" >> "$GITHUB_OUTPUT"
elif [ "${{ inputs.toolchain }}" = "wasmtime-ci-pinned-nightly" ]; then
echo "version=nightly-2024-09-05" >> "$GITHUB_OUTPUT"
echo "version=nightly-2024-10-01" >> "$GITHUB_OUTPUT"
else
echo "version=${{ inputs.toolchain }}" >> "$GITHUB_OUTPUT"
fi
Expand Down
7 changes: 1 addition & 6 deletions crates/component-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl FlagsSize {
} else if count <= 16 {
FlagsSize::Size2
} else {
let amt = ceiling_divide(count, 32);
let amt = count.div_ceil(32);
if amt > (u8::MAX as usize) {
panic!("too many flags");
}
Expand All @@ -84,11 +84,6 @@ impl FlagsSize {
}
}

/// Divide `n` by `d`, rounding up in the case of a non-zero remainder.
const fn ceiling_divide(n: usize, d: usize) -> usize {
(n + d - 1) / d
}

/// A simple bump allocator which can be used with modules
pub const REALLOC_AND_FREE: &str = r#"
(global $last (mut i32) (i32.const 8))
Expand Down
66 changes: 35 additions & 31 deletions crates/test-programs/src/bin/api_reactor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::{Mutex, MutexGuard};

wit_bindgen::generate!({
world: "test-reactor",
path: "../wasi/wit",
Expand All @@ -8,57 +10,59 @@ struct T;

export!(T);

static mut STATE: Vec<String> = Vec::new();
fn state() -> MutexGuard<'static, Vec<String>> {
static STATE: Mutex<Vec<String>> = Mutex::new(Vec::new());
STATE.lock().unwrap()
}

impl Guest for T {
fn add_strings(ss: Vec<String>) -> u32 {
let mut state = state();
for s in ss {
match s.split_once("$") {
Some((prefix, var)) if prefix.is_empty() => match std::env::var(var) {
Ok(val) => unsafe { STATE.push(val) },
Err(_) => unsafe { STATE.push("undefined".to_owned()) },
Ok(val) => state.push(val),
Err(_) => state.push("undefined".to_owned()),
},
_ => unsafe { STATE.push(s) },
_ => state.push(s),
}
}
unsafe { STATE.len() as u32 }
state.len() as u32
}
fn get_strings() -> Vec<String> {
unsafe { STATE.clone() }
state().clone()
}

fn write_strings_to(o: OutputStream) -> Result<(), ()> {
let pollable = o.subscribe();
unsafe {
for s in STATE.iter() {
let mut out = s.as_bytes();
while !out.is_empty() {
pollable.block();
let n = match o.check_write() {
Ok(n) => n,
Err(_) => return Err(()),
};
for s in state().iter() {
let mut out = s.as_bytes();
while !out.is_empty() {
pollable.block();
let n = match o.check_write() {
Ok(n) => n,
Err(_) => return Err(()),
};

let len = (n as usize).min(out.len());
match o.write(&out[..len]) {
Ok(_) => out = &out[len..],
Err(_) => return Err(()),
}
let len = (n as usize).min(out.len());
match o.write(&out[..len]) {
Ok(_) => out = &out[len..],
Err(_) => return Err(()),
}
}
}

match o.flush() {
Ok(_) => {}
Err(_) => return Err(()),
}
pollable.block();
match o.check_write() {
Ok(_) => {}
Err(_) => return Err(()),
}

Ok(())
match o.flush() {
Ok(_) => {}
Err(_) => return Err(()),
}
pollable.block();
match o.check_write() {
Ok(_) => {}
Err(_) => return Err(()),
}

Ok(())
}
fn pass_an_imported_record(stat: wasi::filesystem::types::DescriptorStat) -> String {
format!("{stat:?}")
Expand Down
8 changes: 4 additions & 4 deletions crates/wasmtime/src/runtime/vm/sys/unix/machports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use mach2::thread_act::*;
use mach2::thread_status::*;
use mach2::traps::*;
use std::io;
use std::mem::{self, MaybeUninit};
use std::mem;
use std::ptr::addr_of_mut;
use std::thread;
use wasmtime_environ::Trap;
Expand All @@ -69,7 +69,7 @@ pub struct TrapHandler {
thread: Option<thread::JoinHandle<()>>,
}

static mut PREV_SIGBUS: MaybeUninit<libc::sigaction> = MaybeUninit::uninit();
static mut PREV_SIGBUS: libc::sigaction = unsafe { mem::zeroed() };

impl TrapHandler {
pub unsafe fn new() -> TrapHandler {
Expand Down Expand Up @@ -103,7 +103,7 @@ impl TrapHandler {
handler.sa_flags = libc::SA_SIGINFO | libc::SA_ONSTACK;
handler.sa_sigaction = sigbus_handler as usize;
libc::sigemptyset(&mut handler.sa_mask);
if libc::sigaction(libc::SIGBUS, &handler, PREV_SIGBUS.as_mut_ptr()) != 0 {
if libc::sigaction(libc::SIGBUS, &handler, addr_of_mut!(PREV_SIGBUS)) != 0 {
panic!(
"unable to install signal handler: {}",
io::Error::last_os_error(),
Expand Down Expand Up @@ -149,7 +149,7 @@ unsafe extern "C" fn sigbus_handler(
});

super::signals::delegate_signal_to_previous_handler(
PREV_SIGBUS.as_ptr(),
addr_of_mut!(PREV_SIGBUS),
signum,
siginfo,
context,
Expand Down
29 changes: 15 additions & 14 deletions crates/wasmtime/src/runtime/vm/sys/unix/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::runtime::vm::traphandlers::{tls, TrapRegisters, TrapTest};
use crate::runtime::vm::VMContext;
use std::cell::RefCell;
use std::io;
use std::mem::{self, MaybeUninit};
use std::ptr::{self, null_mut};
use std::mem;
use std::ptr::{self, addr_of, addr_of_mut, null_mut};

#[link(name = "wasmtime-helpers")]
extern "C" {
Expand All @@ -26,10 +26,11 @@ extern "C" {
pub type SignalHandler<'a> =
dyn Fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) -> bool + Send + Sync + 'a;

static mut PREV_SIGSEGV: MaybeUninit<libc::sigaction> = MaybeUninit::uninit();
static mut PREV_SIGBUS: MaybeUninit<libc::sigaction> = MaybeUninit::uninit();
static mut PREV_SIGILL: MaybeUninit<libc::sigaction> = MaybeUninit::uninit();
static mut PREV_SIGFPE: MaybeUninit<libc::sigaction> = MaybeUninit::uninit();
const UNINIT_SIGACTION: libc::sigaction = unsafe { mem::zeroed() };
static mut PREV_SIGSEGV: libc::sigaction = UNINIT_SIGACTION;
static mut PREV_SIGBUS: libc::sigaction = UNINIT_SIGACTION;
static mut PREV_SIGILL: libc::sigaction = UNINIT_SIGACTION;
static mut PREV_SIGFPE: libc::sigaction = UNINIT_SIGACTION;

pub struct TrapHandler;

Expand Down Expand Up @@ -82,20 +83,20 @@ impl TrapHandler {

unsafe fn foreach_handler(mut f: impl FnMut(*mut libc::sigaction, i32)) {
// Allow handling OOB with signals on all architectures
f(PREV_SIGSEGV.as_mut_ptr(), libc::SIGSEGV);
f(addr_of_mut!(PREV_SIGSEGV), libc::SIGSEGV);

// Handle `unreachable` instructions which execute `ud2` right now
f(PREV_SIGILL.as_mut_ptr(), libc::SIGILL);
f(addr_of_mut!(PREV_SIGILL), libc::SIGILL);

// x86 and s390x use SIGFPE to report division by zero
if cfg!(target_arch = "x86_64") || cfg!(target_arch = "s390x") {
f(PREV_SIGFPE.as_mut_ptr(), libc::SIGFPE);
f(addr_of_mut!(PREV_SIGFPE), libc::SIGFPE);
}

// Sometimes we need to handle SIGBUS too:
// - On Darwin, guard page accesses are raised as SIGBUS.
if cfg!(target_os = "macos") || cfg!(target_os = "freebsd") {
f(PREV_SIGBUS.as_mut_ptr(), libc::SIGBUS);
f(addr_of_mut!(PREV_SIGBUS), libc::SIGBUS);
}

// TODO(#1980): x86-32, if we support it, will also need a SIGFPE handler.
Expand Down Expand Up @@ -145,10 +146,10 @@ unsafe extern "C" fn trap_handler(
context: *mut libc::c_void,
) {
let previous = match signum {
libc::SIGSEGV => PREV_SIGSEGV.as_ptr(),
libc::SIGBUS => PREV_SIGBUS.as_ptr(),
libc::SIGFPE => PREV_SIGFPE.as_ptr(),
libc::SIGILL => PREV_SIGILL.as_ptr(),
libc::SIGSEGV => addr_of!(PREV_SIGSEGV),
libc::SIGBUS => addr_of!(PREV_SIGBUS),
libc::SIGFPE => addr_of!(PREV_SIGFPE),
libc::SIGILL => addr_of!(PREV_SIGILL),
_ => panic!("unknown signal: {signum}"),
};
let handled = tls::with(|info| {
Expand Down
21 changes: 11 additions & 10 deletions fuzz/fuzz_targets/differential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use libfuzzer_sys::arbitrary::{Result, Unstructured};
use libfuzzer_sys::fuzz_target;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::Once;
use std::sync::{Mutex, Once};
use wasmtime_fuzzing::generators::{Config, DiffValue, DiffValueType, SingleInstModule};
use wasmtime_fuzzing::oracles::diff_wasmtime::WasmtimeInstance;
use wasmtime_fuzzing::oracles::engine::{build_allowed_env_list, parse_env_list};
Expand All @@ -22,8 +22,8 @@ static SETUP: Once = Once::new();
// - ALLOWED_ENGINES=wasmi,spec cargo +nightly fuzz run ...
// - ALLOWED_ENGINES=-v8 cargo +nightly fuzz run ...
// - ALLOWED_MODULES=single-inst cargo +nightly fuzz run ...
static mut ALLOWED_ENGINES: Vec<&str> = vec![];
static mut ALLOWED_MODULES: Vec<&str> = vec![];
static ALLOWED_ENGINES: Mutex<Vec<&str>> = Mutex::new(vec![]);
static ALLOWED_MODULES: Mutex<Vec<&str>> = Mutex::new(vec![]);

// Statistics about what's actually getting executed during fuzzing
static STATS: RuntimeStats = RuntimeStats::new();
Expand All @@ -45,10 +45,8 @@ fuzz_target!(|data: &[u8]| {
&["wasm-smith", "single-inst"],
);

unsafe {
ALLOWED_ENGINES = allowed_engines;
ALLOWED_MODULES = allowed_modules;
}
*ALLOWED_ENGINES.lock().unwrap() = allowed_engines;
*ALLOWED_MODULES.lock().unwrap() = allowed_modules;
});

// Errors in `run` have to do with not enough input in `data`, which we
Expand All @@ -69,10 +67,13 @@ fn execute_one(data: &[u8]) -> Result<()> {
let mut config: Config = u.arbitrary()?;
config.set_differential_config();

let allowed_engines = ALLOWED_ENGINES.lock().unwrap();
let allowed_modules = ALLOWED_MODULES.lock().unwrap();

// Choose an engine that Wasmtime will be differentially executed against.
// The chosen engine is then created, which might update `config`, and
// returned as a trait object.
let lhs = u.choose(unsafe { &*std::ptr::addr_of!(ALLOWED_ENGINES) })?;
let lhs = u.choose(&allowed_engines)?;
let mut lhs = match engine::build(&mut u, lhs, &mut config)? {
Some(engine) => engine,
// The chosen engine does not have support compiled into the fuzzer,
Expand All @@ -95,10 +96,10 @@ fn execute_one(data: &[u8]) -> Result<()> {
let module = SingleInstModule::new(u, &config.module_config)?;
Ok(module.to_bytes())
};
if unsafe { ALLOWED_MODULES.is_empty() } {
if allowed_modules.is_empty() {
panic!("unable to generate a module to fuzz against; check `ALLOWED_MODULES`")
}
let wasm = match *u.choose(unsafe { ALLOWED_MODULES.as_slice() })? {
let wasm = match *u.choose(&allowed_modules)? {
"wasm-smith" => build_wasm_smith_module(&mut u, &config)?,
"single-inst" => build_single_inst_module(&mut u, &config)?,
_ => unreachable!(),
Expand Down

0 comments on commit 1b4ea3a

Please sign in to comment.