Skip to content

Commit e6134c7

Browse files
authored
Upgrade libm and make no-std config testable (#241)
* Make everything build and test without 'std' feature Signed-off-by: Brian Anderson <[email protected]> * Use faster constructor in vec_bytebuf. `vec![0; len]` uses a specialized intrinsic codepath, which, at least in debug mode, is extremely faster than `resize`. This makes the `alloc` test pass instantaneously instead of taking minutes. Signed-off-by: Brian Anderson <[email protected]> * Fix a comment about core+mmap Signed-off-by: Brian Anderson <[email protected]> * Don't use traits to call libm functions These no longer exist in the latest revision of libm. Signed-off-by: Brian Anderson <[email protected]> * Upgrade libm to 0.2.1 Fixes #240 Signed-off-by: Brian Anderson <[email protected]> * Remove warning from readme about debug-mode panics from libm Signed-off-by: Brian Anderson <[email protected]> * Test no-std configuration under travis Signed-off-by: Brian Anderson <[email protected]> * rustfmt Signed-off-by: Brian Anderson <[email protected]>
1 parent edce5aa commit e6134c7

File tree

12 files changed

+127
-39
lines changed

12 files changed

+127
-39
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ script:
3030
# Check that `vec_memory` feature works.
3131
- cargo check --features vec_memory
3232
- travis_wait 60 ./test.sh
33+
- TEST_NO_STD=1 travis_wait 60 ./test.sh
3334
- ./doc.sh
3435

3536
after_success: |

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exclude = [ "/res/*", "/tests/*", "/fuzz/*", "/benches/*" ]
1515
validation = { package = "wasmi-validation", version = "0.3", path = "validation", default-features = false }
1616
parity-wasm = { version = "0.41.0", default-features = false }
1717
memory_units = "0.3.0"
18-
libm = { version = "0.1.2", optional = true }
18+
libm = { version = "0.2.1", optional = true }
1919
num-rational = { version = "0.2.2", default-features = false }
2020
num-traits = { version = "0.2.8", default-features = false }
2121
libc = { version = "0.2.58", optional = true}
@@ -41,7 +41,7 @@ std = [
4141
]
4242
# Enable for no_std support
4343
core = [
44-
# `core` doesn't support vec_memory
44+
# `core` doesn't support mmaped memory
4545
"vec_memory",
4646
"validation/core",
4747
"libm"

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ wasmi = {
3535

3636
When the `core` feature is enabled, code related to `std::error` is disabled.
3737

38-
Floating point operations in `no_std` use [`libm`](https://crates.io/crates/libm), which sometimes panics in debug mode (https://github.com/japaric/libm/issues/4).
39-
So make sure to either use release builds or avoid WASM with floating point operations, for example by using [`deny_floating_point`](https://docs.rs/wasmi/0.4.0/wasmi/struct.Module.html#method.deny_floating_point).
40-
4138
# License
4239

4340
`wasmi` is primarily distributed under the terms of both the MIT

examples/invoke.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate wasmi;
33

44
use std::env::args;
55

6-
use parity_wasm::elements::{External, FunctionType, Internal, Type, ValueType};
6+
use parity_wasm::elements::{External, FunctionType, Internal, Module, Type, ValueType};
77
use wasmi::{ImportsBuilder, ModuleInstance, NopExternals, RuntimeValue};
88

99
fn main() {
@@ -15,7 +15,7 @@ fn main() {
1515
let func_name = &args[2];
1616
let (_, program_args) = args.split_at(3);
1717

18-
let module = parity_wasm::deserialize_file(&args[1]).expect("File to be deserialized");
18+
let module = load_module(&args[1]);
1919

2020
// Extracts call arguments from command-line arguments
2121
let args = {
@@ -118,3 +118,14 @@ fn main() {
118118
.expect("")
119119
);
120120
}
121+
122+
#[cfg(feature = "std")]
123+
fn load_module(file: &str) -> Module {
124+
parity_wasm::deserialize_file(file).expect("File to be deserialized")
125+
}
126+
127+
#[cfg(not(feature = "std"))]
128+
fn load_module(file: &str) -> Module {
129+
let mut buf = std::fs::read(file).expect("Read file");
130+
parity_wasm::deserialize_buffer(&mut buf).expect("Deserialize module")
131+
}

src/memory/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ mod tests {
542542
use super::{MemoryInstance, MemoryRef, LINEAR_MEMORY_PAGE_SIZE};
543543
use crate::memory_units::Pages;
544544
use crate::Error;
545-
use std::rc::Rc;
545+
use alloc::rc::Rc;
546546

547547
#[test]
548548
fn alloc() {

src/memory/vec_bytebuf.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ pub struct ByteBuf {
88

99
impl ByteBuf {
1010
pub fn new(len: usize) -> Result<Self, String> {
11-
let mut buf = Vec::new();
12-
buf.resize(len, 0u8);
11+
let buf = vec![0; len];
1312
Ok(Self { buf })
1413
}
1514

src/prepare/tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Test-only code importing std for no-std testing
2+
extern crate std;
3+
4+
use alloc::vec::Vec;
5+
use std::println;
6+
17
use super::{compile_module, CompiledModule};
28
use crate::isa;
39
use parity_wasm::{deserialize_buffer, elements::Module};

src/tests/host.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Test-only code importing std for no-std testing
2+
extern crate std;
3+
14
use super::parse_wat;
25
use crate::memory_units::Pages;
36
use crate::types::ValueType;
@@ -6,6 +9,8 @@ use crate::{
69
MemoryInstance, MemoryRef, ModuleImportResolver, ModuleInstance, ModuleRef, ResumableError,
710
RuntimeArgs, RuntimeValue, Signature, TableDescriptor, TableInstance, TableRef, Trap, TrapKind,
811
};
12+
use alloc::boxed::Box;
13+
use std::println;
914

1015
#[derive(Debug, Clone, PartialEq)]
1116
struct HostErrorWithCode {

src/tests/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use super::Error;
88

99
fn assert_send<T: Send>() {}
1010
fn assert_sync<T: Sync>() {}
11+
#[cfg(feature = "std")]
1112
fn assert_std_err_impl<T: ::std::error::Error>() {}
13+
#[cfg(not(feature = "std"))]
14+
fn assert_std_err_impl<T>() {}
1215

1316
#[test]
1417
fn assert_error_properties() {

src/tests/wasm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
// Test-only code importing std for no-std testing
2+
extern crate std;
3+
14
use crate::memory_units::Pages;
25
use crate::{
36
Error, FuncRef, GlobalDescriptor, GlobalInstance, GlobalRef, ImportsBuilder, MemoryDescriptor,
47
MemoryInstance, MemoryRef, Module, ModuleImportResolver, ModuleInstance, NopExternals,
58
RuntimeValue, Signature, TableDescriptor, TableInstance, TableRef,
69
};
10+
use alloc::vec::Vec;
711
use std::fs::File;
812

913
struct Env {

0 commit comments

Comments
 (0)