A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
bench-api: breaking ABI updates to enable future extensibility and add a `cache` option (#14705)
Nick Fitzgerald239274b0b5e9
diff --git a/crates/bench-api/src/lib.rs b/crates/bench-api/src/lib.rsindex 2d3ee208b5..97086b1566 100644--- a/crates/bench-api/src/lib.rs+++ b/crates/bench-api/src/lib.rs@@ -73,6 +73,8 @@//! }//!//! let config = WasmBenchConfig {+//! version: WASM_BENCH_CONFIG_VERSION,+//! size: std::mem::size_of::<WasmBenchConfig>(),//! working_dir_ptr: working_dir.as_ptr(),//! working_dir_len: working_dir.len(),//! stdout_path_ptr: stdout_path.as_ptr(),@@ -92,11 +94,12 @@//! execution_end,//! execution_flags_ptr: ptr::null(),//! execution_flags_len: 0,+//! cache: false,//! };//!//! let mut bench_api = ptr::null_mut();//! unsafe {-//! let code = wasm_bench_create(config, &mut bench_api);+//! let code = wasm_bench_create(&config, &mut bench_api);//! assert_eq!(code, OK);//! assert!(!bench_api.is_null());//! };@@ -151,6 +154,13 @@ pub type ExitCode = c_int;pub const OK: ExitCode = 0;pub const ERR: ExitCode = -1;+/// The current version of `WasmBenchConfig`.+///+/// Bumped once per release that appends a field, and never otherwise, so a+/// field added in version `N` is present exactly when the caller's `version` is+/// at least `N`.+pub const WASM_BENCH_CONFIG_VERSION: u32 = 1;+// Randomize the location of heap objects to avoid accidental locality being an// uncontrolled variable that obscures performance evaluation in our// experiments.@@ -159,54 +169,236 @@ pub const ERR: ExitCode = -1;static ALLOC: shuffling_allocator::ShufflingAllocator<std::alloc::System> =shuffling_allocator::wrap!(&std::alloc::System);-/// Configuration options for the benchmark.-#[repr(C)]-pub struct WasmBenchConfig {- /// The working directory where benchmarks should be executed.- pub working_dir_ptr: *const u8,- pub working_dir_len: usize,-- /// The file path that should be created and used as `stdout`.- pub stdout_path_ptr: *const u8,- pub stdout_path_len: usize,-- /// The file path that should be created and used as `stderr`.- pub stderr_path_ptr: *const u8,- pub stderr_path_len: usize,-- /// The (optional) file path that should be opened and used as `stdin`. If- /// not provided, then the WASI context will not have a `stdin` initialized.- pub stdin_path_ptr: *const u8,- pub stdin_path_len: usize,-- /// The functions to start and stop performance timers/counters during Wasm- /// compilation.- pub compilation_timer: *mut u8,- pub compilation_start: extern "C" fn(*mut u8),- pub compilation_end: extern "C" fn(*mut u8),-- /// The functions to start and stop performance timers/counters during Wasm- /// instantiation.- pub instantiation_timer: *mut u8,- pub instantiation_start: extern "C" fn(*mut u8),- pub instantiation_end: extern "C" fn(*mut u8),-- /// The functions to start and stop performance timers/counters during Wasm- /// execution.- pub execution_timer: *mut u8,- pub execution_start: extern "C" fn(*mut u8),- pub execution_end: extern "C" fn(*mut u8),-- /// The (optional) flags to use when running Wasmtime. These correspond to- /// the flags used when running Wasmtime from the command line.- pub execution_flags_ptr: *const u8,- pub execution_flags_len: usize,+/// Generate `WasmBenchConfig` and a `Config` accessor for each of its fields.+///+/// Tag each field with the `#[since_version = N]` that introduced it. Version 0+/// fields are mandatory, so their accessors return the field directly; later+/// fields may be missing from an older caller, so theirs return `Option`.+macro_rules! define_config {+ (+ $(#[doc = $struct_doc:expr])*+ pub struct $name:ident {+ $(+ $(#[doc = $field_doc:expr])*+ #[since_version = $version:tt]+ pub $field:ident : $ty:ty,+ )*+ }+ ) => {+ $(#[doc = $struct_doc])*+ #[repr(C)]+ pub struct $name {+ $(+ $(#[doc = $field_doc])*+ pub $field : $ty,+ )*+ }++ impl Config<'_> {+ $(+ define_config!(@accessor $name, $field, $ty, $version);+ )*+ }+ };++ // Version 0 fields are mandatory. `Config::new` rejects any caller too+ // small to hold them, so they are always in bounds.+ (@accessor $name:ident, $field:ident, $ty:ty, 0) => {+ fn $field(&self) -> $ty {+ unsafe { self.read(std::ptr::addr_of!((*self.ptr).$field)) }+ }+ };++ // Fields added later may lie outside an older caller's allocation, so they+ // are only read once `has_field` confirms this caller provided them.+ (@accessor $name:ident, $field:ident, $ty:ty, $version:tt) => {+ fn $field(&self) -> Option<$ty> {+ let offset = std::mem::offset_of!($name, $field);+ if self.has_field($version, offset, size_of::<$ty>()) {+ Some(unsafe { self.read(std::ptr::addr_of!((*self.ptr).$field)) })+ } else {+ None+ }+ }+ };+}++define_config! {+ /// Configuration options for the benchmark.+ ///+ /// This structure is always passed across the bench API by pointer, and new+ /// fields are only ever appended to the end, so that a caller built against+ /// an older version of this structure can still drive a newer bench API+ /// library.+ ///+ /// We only ever read one through [`Config`], never as a reference.+ pub struct WasmBenchConfig {+ /// The version of this structure that the caller was built against.+ ///+ /// Each field's documentation names the version that added it; a field+ /// is only present when `version` is at least that.+ #[since_version = 0]+ pub version: u32,++ /// The size of this structure, in bytes, as the caller knows it.+ ///+ /// Fields beyond these first `size` bytes were not allocated by the+ /// caller and must not be read.+ ///+ /// This acts as a secondary backstop to `version`.+ #[since_version = 0]+ pub size: usize,++ /// The working directory where benchmarks should be executed.+ #[since_version = 0]+ pub working_dir_ptr: *const u8,+ #[since_version = 0]+ pub working_dir_len: usize,++ /// The file path that should be created and used as `stdout`.+ #[since_version = 0]+ pub stdout_path_ptr: *const u8,+ #[since_version = 0]+ pub stdout_path_len: usize,++ /// The file path that should be created and used as `stderr`.+ #[since_version = 0]+ pub stderr_path_ptr: *const u8,+ #[since_version = 0]+ pub stderr_path_len: usize,++ /// The (optional) file path that should be opened and used as `stdin`.+ /// If not provided, then the WASI context will not have a `stdin`+ /// initialized.+ #[since_version = 0]+ pub stdin_path_ptr: *const u8,+ #[since_version = 0]+ pub stdin_path_len: usize,++ /// The functions to start and stop performance timers/counters during+ /// Wasm compilation.+ #[since_version = 0]+ pub compilation_timer: *mut u8,+ #[since_version = 0]+ pub compilation_start: extern "C" fn(*mut u8),+ #[since_version = 0]+ pub compilation_end: extern "C" fn(*mut u8),++ /// The functions to start and stop performance timers/counters during+ /// Wasm instantiation.+ #[since_version = 0]+ pub instantiation_timer: *mut u8,+ #[since_version = 0]+ pub instantiation_start: extern "C" fn(*mut u8),+ #[since_version = 0]+ pub instantiation_end: extern "C" fn(*mut u8),++ /// The functions to start and stop performance timers/counters during+ /// Wasm execution.+ #[since_version = 0]+ pub execution_timer: *mut u8,+ #[since_version = 0]+ pub execution_start: extern "C" fn(*mut u8),+ #[since_version = 0]+ pub execution_end: extern "C" fn(*mut u8),++ /// The (optional) flags to use when running Wasmtime. These correspond+ /// to the flags used when running Wasmtime from the command line.+ #[since_version = 0]+ pub execution_flags_ptr: *const u8,+ #[since_version = 0]+ pub execution_flags_len: usize,++ /// Whether to enable Wasmtime's compilation cache.+ ///+ /// Benchmark runs that measure compilation want this off, so that they+ /// measure compilation rather than a cache lookup. Benchmark runs that+ /// measure only instantiation or execution want this on, to avoid+ /// spending time recompiling the Wasm.+ #[since_version = 1]+ pub cache: bool,+ }+}++/// A caller-provided [`WasmBenchConfig`], read through its pointer.+///+/// Holding the pointer rather than a reference is what keeps this sound. A+/// `&WasmBenchConfig` would assert that the whole struct is initialized, which+/// is not true of its padding bytes, nor of the fields belonging to versions+/// newer than the caller's.+pub struct Config<'a> {+ ptr: *const WasmBenchConfig,+ _lifetime: std::marker::PhantomData<&'a WasmBenchConfig>,}-impl WasmBenchConfig {+impl<'a> Config<'a> {+ /// The fields every caller must provide, regardless of how old it is.+ const MIN_SIZE: usize =+ std::mem::offset_of!(WasmBenchConfig, execution_flags_len) + size_of::<usize>();++ /// Wrap a caller-provided config pointer, after checking it is large enough+ /// to hold every mandatory field.+ ///+ /// # Safety+ ///+ /// `ptr` must be non-null, aligned, and point to a `version` and `size`+ /// that accurately describe the fields that follow, all of which must+ /// remain valid for `'a`.+ unsafe fn new(ptr: *const WasmBenchConfig) -> Result<Config<'a>> {+ assert!(!ptr.is_null());+ let config = Config {+ ptr,+ _lifetime: std::marker::PhantomData,+ };++ // Reads `size`, which every caller provides, so this is in bounds even+ // for the oldest one.+ let size = config.size();+ if size < Self::MIN_SIZE {+ return Err(format_err!(+ "given `WasmBenchConfig` is {} bytes, but at least {} are required",+ size,+ Self::MIN_SIZE,+ ));+ }++ Ok(config)+ }++ /// Read one field out of the caller's config.+ ///+ /// # Safety+ ///+ /// `field` must be a pointer to a field this caller actually provided,+ /// derived from `self.ptr`.+ unsafe fn read<T>(&self, field: *const T) -> T {+ assert!(!field.is_null());+ assert!(field.is_aligned());+ unsafe { field.read() }+ }++ /// Did the caller provide the field that version `version` added, which+ /// occupies `field_size` bytes at `offset`?+ ///+ /// Both checks must pass. `version` alone is the field's real name, but+ /// trusting it would let a caller that bumped its version without growing+ /// the struct send us past its allocation; `size` alone cannot tell a field+ /// apart from the padding that may follow the one before it.+ fn has_field(&self, version: u32, offset: usize, field_size: usize) -> bool {+ self.version() >= version && self.size() >= offset + field_size+ }++ /// Whether to enable the compilation cache.+ ///+ /// Conservatively defaults to disabled for callers that predate the `cache`+ /// field, so we don't break compilation measurements.+ fn cache_enabled(&self) -> bool {+ self.cache().unwrap_or(false)+ }+fn working_dir(&self) -> Result<PathBuf> {let working_dir =- unsafe { std::slice::from_raw_parts(self.working_dir_ptr, self.working_dir_len) };+ unsafe { std::slice::from_raw_parts(self.working_dir_ptr(), self.working_dir_len()) };let working_dir = std::str::from_utf8(working_dir).context("given working directory is not valid UTF-8")?;Ok(working_dir.into())@@ -214,7 +406,7 @@ impl WasmBenchConfig {fn stdout_path(&self) -> Result<PathBuf> {let stdout_path =- unsafe { std::slice::from_raw_parts(self.stdout_path_ptr, self.stdout_path_len) };+ unsafe { std::slice::from_raw_parts(self.stdout_path_ptr(), self.stdout_path_len()) };let stdout_path =std::str::from_utf8(stdout_path).context("given stdout path is not valid UTF-8")?;Ok(stdout_path.into())@@ -222,30 +414,32 @@ impl WasmBenchConfig {fn stderr_path(&self) -> Result<PathBuf> {let stderr_path =- unsafe { std::slice::from_raw_parts(self.stderr_path_ptr, self.stderr_path_len) };+ unsafe { std::slice::from_raw_parts(self.stderr_path_ptr(), self.stderr_path_len()) };let stderr_path =std::str::from_utf8(stderr_path).context("given stderr path is not valid UTF-8")?;Ok(stderr_path.into())}fn stdin_path(&self) -> Result<Option<PathBuf>> {- if self.stdin_path_ptr.is_null() {+ let stdin_path_ptr = self.stdin_path_ptr();+ if stdin_path_ptr.is_null() {return Ok(None);}let stdin_path =- unsafe { std::slice::from_raw_parts(self.stdin_path_ptr, self.stdin_path_len) };+ unsafe { std::slice::from_raw_parts(stdin_path_ptr, self.stdin_path_len()) };let stdin_path =std::str::from_utf8(stdin_path).context("given stdin path is not valid UTF-8")?;Ok(Some(stdin_path.into()))}fn execution_flags(&self) -> Result<CommonOptions> {- let flags = if self.execution_flags_ptr.is_null() {+ let execution_flags_ptr = self.execution_flags_ptr();+ let flags = if execution_flags_ptr.is_null() {""} else {let execution_flags = unsafe {- std::slice::from_raw_parts(self.execution_flags_ptr, self.execution_flags_len)+ std::slice::from_raw_parts(execution_flags_ptr, self.execution_flags_len())};std::str::from_utf8(execution_flags).context("given execution flags string is not valid UTF-8")?@@ -267,12 +461,17 @@ impl WasmBenchConfig {/// that contains the engine's initialized state, and `0` is returned. On/// failure, a non-zero status code is returned and `out_bench_ptr` is left/// untouched.+///+/// The `config` pointer only needs to remain valid for the duration of this+/// call, as does everything it points to.#[unsafe(no_mangle)]pub extern "C" fn wasm_bench_create(- config: WasmBenchConfig,+ config: *const WasmBenchConfig,out_bench_ptr: *mut *mut c_void,) -> ExitCode {let result = (|| -> Result<_> {+ let config = unsafe { Config::new(config)? };+// Clone paths for the core Wasm WASI closure.let working_dir = config.working_dir()?;let stdout_path = config.stdout_path()?;@@ -282,15 +481,16 @@ pub extern "C" fn wasm_bench_create(let state = Box::new(BenchState::new(options,- config.compilation_timer,- config.compilation_start,- config.compilation_end,- config.instantiation_timer,- config.instantiation_start,- config.instantiation_end,- config.execution_timer,- config.execution_start,- config.execution_end,+ config.cache_enabled(),+ config.compilation_timer(),+ config.compilation_start(),+ config.compilation_end(),+ config.instantiation_timer(),+ config.instantiation_start(),+ config.instantiation_end(),+ config.execution_timer(),+ config.execution_start(),+ config.execution_end(),move || {let mut cx = WasiCtx::builder();@@ -424,6 +624,7 @@ impl WasiView for HostState {impl BenchState {fn new(mut options: CommonOptions,+ cache: bool,compilation_timer: *mut u8,compilation_start: extern "C" fn(*mut u8),compilation_end: extern "C" fn(*mut u8),@@ -436,8 +637,15 @@ impl BenchState {make_wasi_cx: impl FnMut() -> Result<WasiP1Ctx> + 'static,) -> Result<Self> {let mut config = options.config(None)?;- // NB: always disable the compilation cache.- config.cache(None);++ // NB: set this either way, since `CommonOptions` enables the cache by+ // default and we must not break compilation benchmarks.+ config.cache(if cache {+ Some(wasmtime::Cache::from_file(None)?)+ } else {+ None+ });+let engine = Engine::new(&config)?;let mut linker = Linker::<HostState>::new(&engine);let mut component_linker = wasmtime::component::Linker::<HostState>::new(&engine);