From 967827f4b518aa17cebf997d9e90e2fc2e9693fc Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 20 Apr 2020 10:04:44 -0500 Subject: [PATCH] Remove wasi-common's dependency on `num` (#1557) This dependency was unfortunately causing rebuilds switching between `cargo test` and `cargo build` since the `num` crate had different features activated in testbuild mode. Instead of fixing this I went ahead and just removed the small dependency on the `num` crate in the `wasi-common` crate, opting for simple local definitions or using the standard library's endian-switching methods. --- Cargo.lock | 56 -------- crates/wasi-common/Cargo.toml | 1 - .../wasi-common/src/old/snapshot_0/memory.rs | 132 ++++++++++-------- 3 files changed, 74 insertions(+), 115 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 46c1b2a243..b6e06ebb81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1149,61 +1149,6 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "816df386e5557ac1843a96f1ba8a7cbf4ab175d05ccc15c87a3cda27b4fbdece" -[[package]] -name = "num" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36" -dependencies = [ - "num-complex", - "num-integer", - "num-iter", - "num-rational", - "num-traits", -] - -[[package]] -name = "num-complex" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95" -dependencies = [ - "autocfg 1.0.0", - "num-traits", -] - -[[package]] -name = "num-integer" -version = "0.1.42" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba" -dependencies = [ - "autocfg 1.0.0", - "num-traits", -] - -[[package]] -name = "num-iter" -version = "0.1.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfb0800a0291891dd9f4fe7bd9c19384f98f7fbe0cd0f39a2c6b88b9868bbc00" -dependencies = [ - "autocfg 1.0.0", - "num-integer", - "num-traits", -] - -[[package]] -name = "num-rational" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef" -dependencies = [ - "autocfg 1.0.0", - "num-integer", - "num-traits", -] - [[package]] name = "num-traits" version = "0.2.11" @@ -2073,7 +2018,6 @@ dependencies = [ "lazy_static", "libc", "log", - "num", "thiserror", "wig", "wiggle", diff --git a/crates/wasi-common/Cargo.toml b/crates/wasi-common/Cargo.toml index 38a3697d3f..6ba6f92269 100644 --- a/crates/wasi-common/Cargo.toml +++ b/crates/wasi-common/Cargo.toml @@ -25,7 +25,6 @@ cfg-if = "0.1.9" log = "0.4" filetime = "0.2.7" lazy_static = "1.4.0" -num = { version = "0.2.0", default-features = false } wig = { path = "wig", version = "0.15.0" } wiggle = { path = "../wiggle", default-features = false, version = "0.15.0" } diff --git a/crates/wasi-common/src/old/snapshot_0/memory.rs b/crates/wasi-common/src/old/snapshot_0/memory.rs index 7f46db6c1e..324d8712cc 100644 --- a/crates/wasi-common/src/old/snapshot_0/memory.rs +++ b/crates/wasi-common/src/old/snapshot_0/memory.rs @@ -10,11 +10,32 @@ #![allow(unused)] use crate::old::snapshot_0::wasi::{self, WasiError, WasiResult}; use crate::old::snapshot_0::{host, wasi32}; -use num::PrimInt; use std::convert::TryFrom; use std::mem::{align_of, size_of}; use std::{ptr, slice}; +pub(crate) trait FromLe { + fn from_le(bits: Self) -> Self; +} + +fn from_le(bits: T) -> T { + FromLe::from_le(bits) +} + +macro_rules! impl_from_le { + ($($i:ident)*) => ($( + impl FromLe for $i { + fn from_le(bits: Self) -> Self { + <$i>::from_le(bits) + } + } + )*) +} +impl_from_le! { + i8 i16 i32 i64 isize + u8 u16 u32 u64 usize +} + fn dec_ptr(memory: &[u8], ptr: wasi32::uintptr_t, len: usize) -> WasiResult<*const u8> { // check for overflow let checked_len = (ptr as usize).checked_add(len).ok_or(WasiError::EFAULT)?; @@ -70,16 +91,13 @@ fn enc_raw_byref(memory: &mut [u8], ptr: wasi32::uintptr_t, t: T) -> WasiResu pub(crate) fn dec_int_byref(memory: &[u8], ptr: wasi32::uintptr_t) -> WasiResult where - T: PrimInt, + T: FromLe, { - dec_raw_byref::(memory, ptr).map(|i| PrimInt::from_le(i)) + dec_raw_byref::(memory, ptr).map(|i| FromLe::from_le(i)) } -pub(crate) fn enc_int_byref(memory: &mut [u8], ptr: wasi32::uintptr_t, t: T) -> WasiResult<()> -where - T: PrimInt, -{ - enc_raw_byref::(memory, ptr, PrimInt::to_le(t)) +pub(crate) fn enc_int_byref(memory: &mut [u8], ptr: wasi32::uintptr_t, t: T) -> WasiResult<()> { + enc_raw_byref::(memory, ptr, t) } fn check_slice_of(ptr: wasi32::uintptr_t, len: wasi32::size_t) -> WasiResult<(usize, usize)> { @@ -175,7 +193,7 @@ pub(crate) fn enc_slice_of_wasi32_uintptr( let mut output_iter = raw_slice_for_enc::(memory, slice, ptr)?.into_iter(); for p in slice { - *output_iter.next().unwrap() = PrimInt::to_le(*p); + *output_iter.next().unwrap() = p.to_le(); } Ok(()) @@ -195,7 +213,7 @@ macro_rules! dec_enc_scalar { ptr: wasi32::uintptr_t, x: wasi::$ty, ) -> WasiResult<()> { - enc_int_byref::(memory, ptr, x) + enc_int_byref::(memory, ptr, x.to_le()) } }; } @@ -210,8 +228,8 @@ pub(crate) fn dec_ciovec_slice( raw_slice .iter() .map(|raw_iov| { - let len = dec_usize(PrimInt::from_le(raw_iov.buf_len)); - let buf = PrimInt::from_le(raw_iov.buf); + let len = dec_usize(from_le(raw_iov.buf_len)); + let buf = from_le(raw_iov.buf); Ok(host::__wasi_ciovec_t { buf: dec_ptr(memory, buf, len)? as *const u8, buf_len: len, @@ -230,8 +248,8 @@ pub(crate) fn dec_iovec_slice( raw_slice .iter() .map(|raw_iov| { - let len = dec_usize(PrimInt::from_le(raw_iov.buf_len)); - let buf = PrimInt::from_le(raw_iov.buf); + let len = dec_usize(from_le(raw_iov.buf_len)); + let buf = from_le(raw_iov.buf); Ok(host::__wasi_iovec_t { buf: dec_ptr(memory, buf, len)? as *mut u8, buf_len: len, @@ -256,14 +274,14 @@ pub(crate) fn dec_filestat_byref( let raw = dec_raw_byref::(memory, filestat_ptr)?; Ok(wasi::__wasi_filestat_t { - dev: PrimInt::from_le(raw.dev), - ino: PrimInt::from_le(raw.ino), - filetype: PrimInt::from_le(raw.filetype), - nlink: PrimInt::from_le(raw.nlink), - size: PrimInt::from_le(raw.size), - atim: PrimInt::from_le(raw.atim), - mtim: PrimInt::from_le(raw.mtim), - ctim: PrimInt::from_le(raw.ctim), + dev: from_le(raw.dev), + ino: from_le(raw.ino), + filetype: from_le(raw.filetype), + nlink: from_le(raw.nlink), + size: from_le(raw.size), + atim: from_le(raw.atim), + mtim: from_le(raw.mtim), + ctim: from_le(raw.ctim), }) } @@ -273,14 +291,14 @@ pub(crate) fn enc_filestat_byref( filestat: wasi::__wasi_filestat_t, ) -> WasiResult<()> { let raw = wasi::__wasi_filestat_t { - dev: PrimInt::to_le(filestat.dev), - ino: PrimInt::to_le(filestat.ino), - filetype: PrimInt::to_le(filestat.filetype), - nlink: PrimInt::to_le(filestat.nlink), - size: PrimInt::to_le(filestat.size), - atim: PrimInt::to_le(filestat.atim), - mtim: PrimInt::to_le(filestat.mtim), - ctim: PrimInt::to_le(filestat.ctim), + dev: filestat.dev.to_le(), + ino: filestat.ino.to_le(), + filetype: filestat.filetype.to_le(), + nlink: filestat.nlink.to_le(), + size: filestat.size.to_le(), + atim: filestat.atim.to_le(), + mtim: filestat.mtim.to_le(), + ctim: filestat.ctim.to_le(), }; enc_raw_byref::(memory, filestat_ptr, raw) @@ -293,10 +311,10 @@ pub(crate) fn dec_fdstat_byref( let raw = dec_raw_byref::(memory, fdstat_ptr)?; Ok(wasi::__wasi_fdstat_t { - fs_filetype: PrimInt::from_le(raw.fs_filetype), - fs_flags: PrimInt::from_le(raw.fs_flags), - fs_rights_base: PrimInt::from_le(raw.fs_rights_base), - fs_rights_inheriting: PrimInt::from_le(raw.fs_rights_inheriting), + fs_filetype: from_le(raw.fs_filetype), + fs_flags: from_le(raw.fs_flags), + fs_rights_base: from_le(raw.fs_rights_base), + fs_rights_inheriting: from_le(raw.fs_rights_inheriting), }) } @@ -306,10 +324,10 @@ pub(crate) fn enc_fdstat_byref( fdstat: wasi::__wasi_fdstat_t, ) -> WasiResult<()> { let raw = wasi::__wasi_fdstat_t { - fs_filetype: PrimInt::to_le(fdstat.fs_filetype), - fs_flags: PrimInt::to_le(fdstat.fs_flags), - fs_rights_base: PrimInt::to_le(fdstat.fs_rights_base), - fs_rights_inheriting: PrimInt::to_le(fdstat.fs_rights_inheriting), + fs_filetype: fdstat.fs_filetype.to_le(), + fs_flags: fdstat.fs_flags.to_le(), + fs_rights_base: fdstat.fs_rights_base.to_le(), + fs_rights_inheriting: fdstat.fs_rights_inheriting.to_le(), }; enc_raw_byref::(memory, fdstat_ptr, raw) @@ -333,12 +351,12 @@ pub(crate) fn dec_prestat_byref( ) -> WasiResult { let raw = dec_raw_byref::(memory, prestat_ptr)?; - match PrimInt::from_le(raw.tag) { + match from_le(raw.tag) { wasi::__WASI_PREOPENTYPE_DIR => Ok(host::__wasi_prestat_t { tag: wasi::__WASI_PREOPENTYPE_DIR, u: host::__wasi_prestat_u_t { dir: host::__wasi_prestat_dir_t { - pr_name_len: dec_usize(PrimInt::from_le(unsafe { raw.u.dir.pr_name_len })), + pr_name_len: dec_usize(from_le(unsafe { raw.u.dir.pr_name_len })), }, }, }), @@ -353,7 +371,7 @@ pub(crate) fn enc_prestat_byref( ) -> WasiResult<()> { let raw = match prestat.tag { wasi::__WASI_PREOPENTYPE_DIR => Ok(wasi32::__wasi_prestat_t { - tag: PrimInt::to_le(wasi::__WASI_PREOPENTYPE_DIR), + tag: wasi::__WASI_PREOPENTYPE_DIR.to_le(), u: wasi32::__wasi_prestat_u_t { dir: wasi32::__wasi_prestat_dir_t { pr_name_len: enc_usize(unsafe { prestat.u.dir.pr_name_len }), @@ -382,7 +400,7 @@ pub(crate) fn enc_usize_byref( usize_ptr: wasi32::uintptr_t, host_usize: usize, ) -> WasiResult<()> { - enc_int_byref::(memory, usize_ptr, enc_usize(host_usize)) + enc_int_byref::(memory, usize_ptr, enc_usize(host_usize).to_le()) } dec_enc_scalar!(__wasi_whence_t, dec_whence_byref, enc_whence_byref); @@ -413,31 +431,29 @@ pub(crate) fn dec_subscriptions( raw_input_slice .into_iter() .map(|raw_subscription| { - let userdata = PrimInt::from_le(raw_subscription.userdata); - let tag = PrimInt::from_le(raw_subscription.u.tag); + let userdata = from_le(raw_subscription.userdata); + let tag = from_le(raw_subscription.u.tag); let raw_u = raw_subscription.u.u; let u = match tag { wasi::__WASI_EVENTTYPE_CLOCK => wasi::__wasi_subscription_u_u_t { clock: unsafe { wasi::__wasi_subscription_clock_t { - identifier: PrimInt::from_le(raw_u.clock.identifier), - id: PrimInt::from_le(raw_u.clock.id), - timeout: PrimInt::from_le(raw_u.clock.timeout), - precision: PrimInt::from_le(raw_u.clock.precision), - flags: PrimInt::from_le(raw_u.clock.flags), + identifier: from_le(raw_u.clock.identifier), + id: from_le(raw_u.clock.id), + timeout: from_le(raw_u.clock.timeout), + precision: from_le(raw_u.clock.precision), + flags: from_le(raw_u.clock.flags), } }, }, wasi::__WASI_EVENTTYPE_FD_READ => wasi::__wasi_subscription_u_u_t { fd_read: wasi::__wasi_subscription_fd_readwrite_t { - file_descriptor: PrimInt::from_le(unsafe { raw_u.fd_read.file_descriptor }), + file_descriptor: from_le(unsafe { raw_u.fd_read.file_descriptor }), }, }, wasi::__WASI_EVENTTYPE_FD_WRITE => wasi::__wasi_subscription_u_u_t { fd_write: wasi::__wasi_subscription_fd_readwrite_t { - file_descriptor: PrimInt::from_le(unsafe { - raw_u.fd_write.file_descriptor - }), + file_descriptor: from_le(unsafe { raw_u.fd_write.file_descriptor }), }, }, _ => return Err(WasiError::EINVAL), @@ -463,11 +479,11 @@ pub(crate) fn enc_events( *raw_output_iter .next() .expect("the number of events cannot exceed the number of subscriptions") = { - let userdata = PrimInt::to_le(event.userdata); - let error = PrimInt::to_le(event.error); - let r#type = PrimInt::to_le(event.r#type); - let flags = PrimInt::to_le(event.fd_readwrite.flags); - let nbytes = PrimInt::to_le(event.fd_readwrite.nbytes); + let userdata = event.userdata.to_le(); + let error = event.error.to_le(); + let r#type = event.r#type.to_le(); + let flags = event.fd_readwrite.flags.to_le(); + let nbytes = event.fd_readwrite.nbytes.to_le(); wasi::__wasi_event_t { userdata, error,