From bfa9f39a288f207f6f73e9153e3e4ebb2b862bdf Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Tue, 25 Jul 2023 14:14:35 +0800 Subject: [PATCH] Use `tracing` unconditionally in the `wasmer` crate --- lib/api/Cargo.toml | 2 +- lib/api/src/js/externals/memory.rs | 5 +---- lib/api/src/js/externals/memory_view.rs | 21 ++++++------------ lib/api/src/js/module.rs | 6 ++--- lib/api/src/js/vm.rs | 20 ++++++++--------- lib/api/src/jsc/externals/memory.rs | 5 +---- lib/api/src/jsc/mem_access.rs | 2 -- lib/api/src/jsc/module.rs | 2 -- lib/api/src/jsc/vm.rs | 3 --- lib/api/src/sys/externals/memory.rs | 29 ++++++++++++------------- lib/api/src/sys/mem_access.rs | 9 ++++---- lib/api/src/sys/module.rs | 17 +++++++++------ 12 files changed, 50 insertions(+), 71 deletions(-) diff --git a/lib/api/Cargo.toml b/lib/api/Cargo.toml index e7754d8b6ea..92996a1d2ee 100644 --- a/lib/api/Cargo.toml +++ b/lib/api/Cargo.toml @@ -32,7 +32,7 @@ derivative = { version = "^2" } bytes = "1" # - Optional shared dependencies. wat = { version = "1.0", optional = true } -tracing = { version = "0.1", optional = true } +tracing = { version = "0.1" } rustc-demangle = "0.1" # Dependencies and Development Dependencies for `sys`. diff --git a/lib/api/src/js/externals/memory.rs b/lib/api/src/js/externals/memory.rs index 30c95ddcb6a..4f36fe62834 100644 --- a/lib/api/src/js/externals/memory.rs +++ b/lib/api/src/js/externals/memory.rs @@ -5,7 +5,7 @@ use crate::MemoryType; use std::marker::PhantomData; use std::mem::MaybeUninit; use std::slice; -#[cfg(feature = "tracing")] + use tracing::warn; use wasm_bindgen::prelude::*; @@ -178,7 +178,6 @@ impl<'a> MemoryBuffer<'a> { .ok_or(MemoryAccessError::Overflow)?; let view = unsafe { &*(self.base) }; if end > view.length().into() { - #[cfg(feature = "tracing")] warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), @@ -202,7 +201,6 @@ impl<'a> MemoryBuffer<'a> { .ok_or(MemoryAccessError::Overflow)?; let view = unsafe { &*(self.base) }; if end > view.length().into() { - #[cfg(feature = "tracing")] warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), @@ -224,7 +222,6 @@ impl<'a> MemoryBuffer<'a> { .ok_or(MemoryAccessError::Overflow)?; let view = unsafe { &mut *(self.base) }; if end > view.length().into() { - #[cfg(feature = "tracing")] warn!( "attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", data.len(), diff --git a/lib/api/src/js/externals/memory_view.rs b/lib/api/src/js/externals/memory_view.rs index fccd385ba88..f882a026a39 100644 --- a/lib/api/src/js/externals/memory_view.rs +++ b/lib/api/src/js/externals/memory_view.rs @@ -1,16 +1,14 @@ -use crate::mem_access::MemoryAccessError; -use crate::store::AsStoreRef; -use std::convert::TryInto; -use std::marker::PhantomData; -use std::mem::MaybeUninit; -use std::slice; -#[cfg(feature = "tracing")] +use std::{convert::TryInto, marker::PhantomData, mem::MaybeUninit, slice}; + use tracing::warn; use wasm_bindgen::JsCast; - use wasmer_types::{Bytes, Pages}; -use super::memory::{Memory, MemoryBuffer}; +use crate::{ + js::externals::memory::{Memory, MemoryBuffer}, + mem_access::MemoryAccessError, + store::AsStoreRef, +}; /// A WebAssembly `memory` view. /// @@ -128,7 +126,6 @@ impl<'a> MemoryView<'a> { .map_err(|_| MemoryAccessError::Overflow)?; let end = offset.checked_add(len).ok_or(MemoryAccessError::Overflow)?; if end > view.length() { - #[cfg(feature = "tracing")] warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", len, @@ -149,7 +146,6 @@ impl<'a> MemoryView<'a> { let view = &self.view; let offset: u32 = offset.try_into().map_err(|_| MemoryAccessError::Overflow)?; if offset >= view.length() { - #[cfg(feature = "tracing")] warn!( "attempted to read beyond the bounds of the memory view ({} >= {})", offset, @@ -183,7 +179,6 @@ impl<'a> MemoryView<'a> { .map_err(|_| MemoryAccessError::Overflow)?; let end = offset.checked_add(len).ok_or(MemoryAccessError::Overflow)?; if end > view.length() { - #[cfg(feature = "tracing")] warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", len, @@ -220,7 +215,6 @@ impl<'a> MemoryView<'a> { let view = &self.view; let end = offset.checked_add(len).ok_or(MemoryAccessError::Overflow)?; if end > view.length() { - #[cfg(feature = "tracing")] warn!( "attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", len, @@ -241,7 +235,6 @@ impl<'a> MemoryView<'a> { let view = &self.view; let offset: u32 = offset.try_into().map_err(|_| MemoryAccessError::Overflow)?; if offset >= view.length() { - #[cfg(feature = "tracing")] warn!( "attempted to write beyond the bounds of the memory view ({} >= {})", offset, diff --git a/lib/api/src/js/module.rs b/lib/api/src/js/module.rs index 1a202def78f..dc4dd102a55 100644 --- a/lib/api/src/js/module.rs +++ b/lib/api/src/js/module.rs @@ -10,7 +10,6 @@ use crate::{AsEngineRef, ExportType, ImportType}; use bytes::Bytes; use js_sys::{Reflect, Uint8Array, WebAssembly}; use std::path::Path; -#[cfg(feature = "tracing")] use tracing::{debug, warn}; use wasm_bindgen::JsValue; use wasmer_types::{ @@ -155,10 +154,8 @@ impl Module { #[allow(unused_variables)] if let wasmer_types::ExternType::Memory(mem_ty) = import_type.ty() { if resolved_import.is_some() { - #[cfg(feature = "tracing")] debug!("imported shared memory {:?}", &mem_ty); } else { - #[cfg(feature = "tracing")] warn!( "Error while importing {0:?}.{1:?}: memory. Expected {2:?}", import_type.module(), @@ -195,7 +192,6 @@ impl Module { } import_externs.push(import); } else { - #[cfg(feature = "tracing")] warn!( "import not found {}:{}", import_type.module(), @@ -227,6 +223,7 @@ impl Module { )); } + #[tracing::instrument(level = "debug", skip_all)] pub unsafe fn deserialize_unchecked( _engine: &impl AsEngineRef, _bytes: impl IntoBytes, @@ -239,6 +236,7 @@ impl Module { return Err(DeserializeError::Generic("You need to enable the `js-serializable-module` feature flag to deserialize a `Module`".to_string())); } + #[tracing::instrument(level = "debug", skip_all)] pub unsafe fn deserialize( _engine: &impl AsEngineRef, _bytes: impl IntoBytes, diff --git a/lib/api/src/js/vm.rs b/lib/api/src/js/vm.rs index b7cb94093d2..ff553c27469 100644 --- a/lib/api/src/js/vm.rs +++ b/lib/api/src/js/vm.rs @@ -4,21 +4,21 @@ /// This module should not be needed any longer (with the exception of the memory) /// once the type reflection is added to the WebAssembly JS API. /// https://github.com/WebAssembly/js-types/ -use crate::js::wasm_bindgen_polyfill::Global as JsGlobal; -use js_sys::Function as JsFunction; -use js_sys::WebAssembly; -use js_sys::WebAssembly::{Memory as JsMemory, Table as JsTable}; +use std::{any::Any, fmt}; + +use js_sys::{ + Function as JsFunction, + WebAssembly::{self, Memory as JsMemory, Table as JsTable}, +}; use serde::{Deserialize, Serialize}; -use std::any::Any; -use std::fmt; -#[cfg(feature = "tracing")] use tracing::trace; use wasm_bindgen::{JsCast, JsValue}; -use wasmer_types::RawValue; use wasmer_types::{ - FunctionType, GlobalType, MemoryError, MemoryType, Pages, TableType, WASM_PAGE_SIZE, + FunctionType, GlobalType, MemoryError, MemoryType, Pages, RawValue, TableType, WASM_PAGE_SIZE, }; +use crate::js::wasm_bindgen_polyfill::Global as JsGlobal; + /// Represents linear memory that is managed by the javascript runtime #[derive(Clone, Debug, PartialEq)] pub struct VMMemory { @@ -71,7 +71,6 @@ impl VMMemory { let src = crate::js::externals::memory_view::MemoryView::new_raw(&self.memory); let amount = src.data_size() as usize; - #[cfg(feature = "tracing")] trace!(%amount, "memory copy started"); let mut dst = crate::js::externals::memory_view::MemoryView::new_raw(&new_memory); @@ -102,7 +101,6 @@ impl VMMemory { wasmer_types::MemoryError::Generic(format!("failed to copy the memory - {}", err)) })?; - #[cfg(feature = "tracing")] trace!("memory copy finished (size={})", dst.size().bytes().0); Ok(Self { diff --git a/lib/api/src/jsc/externals/memory.rs b/lib/api/src/jsc/externals/memory.rs index 38fcc952e3d..b334826b6bc 100644 --- a/lib/api/src/jsc/externals/memory.rs +++ b/lib/api/src/jsc/externals/memory.rs @@ -7,7 +7,7 @@ use std::convert::TryInto; use std::marker::PhantomData; use std::mem::{self, MaybeUninit}; use std::slice; -#[cfg(feature = "tracing")] + use tracing::warn; use wasmer_types::{Pages, WASM_PAGE_SIZE}; @@ -215,7 +215,6 @@ impl<'a> MemoryBuffer<'a> { .checked_add(buf.len() as u64) .ok_or(MemoryAccessError::Overflow)?; if end > self.len.try_into().unwrap() { - #[cfg(feature = "tracing")] warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), @@ -239,7 +238,6 @@ impl<'a> MemoryBuffer<'a> { .checked_add(buf.len() as u64) .ok_or(MemoryAccessError::Overflow)?; if end > self.len.try_into().unwrap() { - #[cfg(feature = "tracing")] warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), @@ -261,7 +259,6 @@ impl<'a> MemoryBuffer<'a> { .checked_add(data.len() as u64) .ok_or(MemoryAccessError::Overflow)?; if end > self.len.try_into().unwrap() { - #[cfg(feature = "tracing")] warn!( "attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", data.len(), diff --git a/lib/api/src/jsc/mem_access.rs b/lib/api/src/jsc/mem_access.rs index ad045eedc5d..eb88625ecad 100644 --- a/lib/api/src/jsc/mem_access.rs +++ b/lib/api/src/jsc/mem_access.rs @@ -16,7 +16,6 @@ where .checked_add(total_len) .ok_or(MemoryAccessError::Overflow)?; if end > slice.buffer.0.len as u64 { - #[cfg(feature = "tracing")] warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", total_len, end, slice.buffer.0.len @@ -46,7 +45,6 @@ where .checked_add(total_len) .ok_or(MemoryAccessError::Overflow)?; if end > ptr.buffer.0.len as u64 { - #[cfg(feature = "tracing")] warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", total_len, end, ptr.buffer.0.len diff --git a/lib/api/src/jsc/module.rs b/lib/api/src/jsc/module.rs index 529b3de3ee4..d08cceb0e59 100644 --- a/lib/api/src/jsc/module.rs +++ b/lib/api/src/jsc/module.rs @@ -12,7 +12,6 @@ use crate::{AsEngineRef, ExportType, ImportType}; use bytes::Bytes; use rusty_jsc::{JSObject, JSString, JSValue}; use std::path::Path; -#[cfg(feature = "tracing")] use tracing::{debug, warn}; use wasmer_types::{ CompileError, DeserializeError, ExportsIterator, ExternType, FunctionType, GlobalType, @@ -154,7 +153,6 @@ impl Module { .unwrap(); } } else { - #[cfg(feature = "tracing")] warn!( "import not found {}:{}", import_type.module(), diff --git a/lib/api/src/jsc/vm.rs b/lib/api/src/jsc/vm.rs index ae84b88d3dc..23bcf683aef 100644 --- a/lib/api/src/jsc/vm.rs +++ b/lib/api/src/jsc/vm.rs @@ -8,7 +8,6 @@ use crate::store::AsStoreRef; use rusty_jsc::{JSObject, JSObjectCallAsFunctionCallback, JSValue}; use std::any::Any; use std::fmt; -#[cfg(feature = "tracing")] use tracing::trace; use wasmer_types::RawValue; use wasmer_types::{ @@ -63,7 +62,6 @@ impl VMMemory { let new_memory = crate::jsc::externals::memory::Memory::js_memory_from_type(&store, &self.ty)?; - #[cfg(feature = "tracing")] trace!("memory copy started"); let src = crate::jsc::externals::memory_view::MemoryView::new_raw(&self.memory, store); @@ -96,7 +94,6 @@ impl VMMemory { wasmer_types::MemoryError::Generic(format!("failed to copy the memory - {}", err)) })?; - #[cfg(feature = "tracing")] trace!("memory copy finished (size={})", dst.size().bytes().0); Ok(Self { diff --git a/lib/api/src/sys/externals/memory.rs b/lib/api/src/sys/externals/memory.rs index 90c02ac3051..171f20049ad 100644 --- a/lib/api/src/sys/externals/memory.rs +++ b/lib/api/src/sys/externals/memory.rs @@ -1,19 +1,21 @@ -use super::memory_view::MemoryView; -use crate::store::{AsStoreMut, AsStoreRef}; -use crate::sys::engine::NativeEngineExt; -use crate::vm::VMExternMemory; -use crate::MemoryAccessError; -use crate::MemoryType; -use std::convert::TryInto; -use std::marker::PhantomData; -use std::mem; -use std::mem::MaybeUninit; -use std::slice; -#[cfg(feature = "tracing")] +use std::{ + convert::TryInto, + marker::PhantomData, + mem::{self, MaybeUninit}, + slice, +}; + use tracing::warn; use wasmer_types::Pages; use wasmer_vm::{LinearMemory, MemoryError, StoreHandle, VMExtern, VMMemory}; +use crate::{ + store::{AsStoreMut, AsStoreRef}, + sys::{engine::NativeEngineExt, externals::memory_view::MemoryView}, + vm::VMExternMemory, + MemoryAccessError, MemoryType, +}; + #[derive(Debug, Clone)] pub struct Memory { pub(crate) handle: StoreHandle, @@ -110,7 +112,6 @@ impl<'a> MemoryBuffer<'a> { .checked_add(buf.len() as u64) .ok_or(MemoryAccessError::Overflow)?; if end > self.len.try_into().unwrap() { - #[cfg(feature = "tracing")] warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), @@ -134,7 +135,6 @@ impl<'a> MemoryBuffer<'a> { .checked_add(buf.len() as u64) .ok_or(MemoryAccessError::Overflow)?; if end > self.len.try_into().unwrap() { - #[cfg(feature = "tracing")] warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), @@ -156,7 +156,6 @@ impl<'a> MemoryBuffer<'a> { .checked_add(data.len() as u64) .ok_or(MemoryAccessError::Overflow)?; if end > self.len.try_into().unwrap() { - #[cfg(feature = "tracing")] warn!( "attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", data.len(), diff --git a/lib/api/src/sys/mem_access.rs b/lib/api/src/sys/mem_access.rs index 502a556dded..376c7245446 100644 --- a/lib/api/src/sys/mem_access.rs +++ b/lib/api/src/sys/mem_access.rs @@ -1,7 +1,10 @@ -use crate::access::{RefCow, SliceCow, WasmRefAccess, WasmSliceAccess}; -use crate::{MemoryAccessError, WasmRef, WasmSlice}; use std::mem; +use crate::{ + access::{RefCow, SliceCow, WasmRefAccess, WasmSliceAccess}, + MemoryAccessError, WasmRef, WasmSlice, +}; + impl<'a, T> WasmSliceAccess<'a, T> where T: wasmer_types::ValueType, @@ -16,7 +19,6 @@ where .checked_add(total_len) .ok_or(MemoryAccessError::Overflow)?; if end > slice.buffer.0.len as u64 { - #[cfg(feature = "tracing")] tracing::warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", total_len, @@ -48,7 +50,6 @@ where .checked_add(total_len) .ok_or(MemoryAccessError::Overflow)?; if end > ptr.buffer.0.len as u64 { - #[cfg(feature = "tracing")] tracing::warn!( "attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", total_len, diff --git a/lib/api/src/sys/module.rs b/lib/api/src/sys/module.rs index e4eaf6eba5a..e7db743b632 100644 --- a/lib/api/src/sys/module.rs +++ b/lib/api/src/sys/module.rs @@ -1,17 +1,17 @@ -use crate::engine::AsEngineRef; -use bytes::Bytes; use std::path::Path; use std::sync::Arc; -use wasmer_compiler::Artifact; -use wasmer_compiler::ArtifactCreate; + +use bytes::Bytes; +use wasmer_compiler::{Artifact, ArtifactCreate}; use wasmer_types::{ CompileError, DeserializeError, ExportsIterator, ImportsIterator, ModuleInfo, SerializeError, }; use wasmer_types::{ExportType, ImportType}; -use crate::sys::engine::NativeEngineExt; -use crate::vm::VMInstance; -use crate::{AsStoreMut, AsStoreRef, InstantiationError, IntoBytes}; +use crate::{ + engine::AsEngineRef, sys::engine::NativeEngineExt, vm::VMInstance, AsStoreMut, AsStoreRef, + InstantiationError, IntoBytes, +}; #[derive(Clone, PartialEq, Eq)] pub struct Module { @@ -49,6 +49,7 @@ impl Module { Ok(module) } + #[tracing::instrument(level = "debug", skip_all)] pub(crate) fn validate(engine: &impl AsEngineRef, binary: &[u8]) -> Result<(), CompileError> { engine.as_engine_ref().engine().0.validate(binary) } @@ -70,6 +71,7 @@ impl Module { self.artifact.serialize().map(|bytes| bytes.into()) } + #[tracing::instrument(level = "debug", skip_all)] pub unsafe fn deserialize_unchecked( engine: &impl AsEngineRef, bytes: impl IntoBytes, @@ -83,6 +85,7 @@ impl Module { Ok(Self::from_artifact(artifact)) } + #[tracing::instrument(level = "debug", skip_all)] pub unsafe fn deserialize( engine: &impl AsEngineRef, bytes: impl IntoBytes,