Skip to content

Commit

Permalink
Merge pull request #6 from WAFoundation/fix-warnings
Browse files Browse the repository at this point in the history
Fixed all Rust code warnings
  • Loading branch information
syrusakbary authored Nov 6, 2018
2 parents 18ece12 + e7b4d06 commit e13ab80
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 96 deletions.
90 changes: 47 additions & 43 deletions src/build_spectests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::{self, Read};
use std::path::PathBuf;
use std::time::SystemTime;
use wabt::script::{Action, Command, CommandKind, ModuleBinary, ScriptParser, Value};
use wabt::wasm2wat;

Expand Down Expand Up @@ -74,10 +72,10 @@ const TESTS: [&str; 54] = [

fn wabt2rust_type(v: &Value) -> String {
match v {
Value::I32(v) => format!("i32"),
Value::I64(v) => format!("i64"),
Value::F32(v) => format!("f32"),
Value::F64(v) => format!("f64"),
Value::I32(_v) => format!("i32"),
Value::I64(_v) => format!("i64"),
Value::F32(_v) => format!("f32"),
Value::F64(_v) => format!("f64"),
}
}

Expand All @@ -95,32 +93,30 @@ fn wabt2rust_value(v: &Value) -> String {
Value::I32(v) => format!("{:?} as i32", v),
Value::I64(v) => format!("{:?} as i64", v),
Value::F32(v) => {
match *v {
std::f32::INFINITY => "f32::INFINITY".to_string(),
std::f32::NEG_INFINITY => "f32::NEG_INFINITY".to_string(),
// std::f32::NAN => "f32::NAN".to_string(),
_ => {
if v.is_nan() {
// Support for non-canonical NaNs
format!("f32::from_bits({:?})", v.to_bits())
} else {
format!("{:?} as f32", v)
}
if v.is_infinite() {
if v.is_sign_negative() {
"f32::NEG_INFINITY".to_string()
} else {
"f32::INFINITY".to_string()
}
} else if v.is_nan() {
// Support for non-canonical NaNs
format!("f32::from_bits({:?})", v.to_bits())
} else {
format!("{:?} as f32", v)
}
}
Value::F64(v) => {
match *v {
std::f64::INFINITY => "f64::INFINITY".to_string(),
std::f64::NEG_INFINITY => "f64::NEG_INFINITY".to_string(),
// std::f64::NAN => "f64::NAN".to_string(),
_ => {
if v.is_nan() {
format!("f64::from_bits({:?})", v.to_bits())
} else {
format!("{:?} as f64", v)
}
if v.is_infinite() {
if v.is_sign_negative() {
"f64::NEG_INFINITY".to_string()
} else {
"f64::INFINITY".to_string()
}
} else if v.is_nan() {
format!("f64::from_bits({:?})", v.to_bits())
} else {
format!("{:?} as f64", v)
}
}
}
Expand All @@ -140,9 +136,8 @@ impl WastTestGenerator {
fn new(path: &PathBuf) -> Self {
let filename = path.file_name().unwrap().to_str().unwrap();
let source = fs::read(&path).unwrap();
let mut script: ScriptParser =
ScriptParser::from_source_and_name(&source, filename).unwrap();
let mut buffer = String::new();
let script: ScriptParser = ScriptParser::from_source_and_name(&source, filename).unwrap();
let buffer = String::new();
WastTestGenerator {
last_module: 0,
last_line: 0,
Expand Down Expand Up @@ -216,7 +211,7 @@ fn test_module_{}() {{
self.module_calls.remove(&module);
}

fn visit_module(&mut self, module: &ModuleBinary, name: &Option<String>) {
fn visit_module(&mut self, module: &ModuleBinary, _name: &Option<String>) {
let wasm_binary: Vec<u8> = module.clone().into_vec();
let wast_string = wasm2wat(wasm_binary).expect("Can't convert back to wasm");
self.flush_module_calls(self.last_module);
Expand Down Expand Up @@ -280,12 +275,12 @@ fn {}_assert_invalid() {{
fn visit_assert_return_arithmetic_nan(&mut self, action: &Action) {
match action {
Action::Invoke {
module,
module: _,
field,
args,
} => {
let mut return_type = wabt2rust_type(&args[0]);
let mut func_return = format!(" -> {}", return_type);
let return_type = wabt2rust_type(&args[0]);
let func_return = format!(" -> {}", return_type);
let assertion = String::from("assert!(result.is_quiet_nan())");

// We map the arguments provided into the raw Arguments provided
Expand Down Expand Up @@ -333,16 +328,16 @@ fn {}_assert_invalid() {{
fn visit_assert_return_canonical_nan(&mut self, action: &Action) {
match action {
Action::Invoke {
module,
module: _,
field,
args,
} => {
let mut return_type = match &field.as_str() {
let return_type = match &field.as_str() {
&"f64.promote_f32" => String::from("f64"),
&"f32.promote_f64" => String::from("f32"),
_ => wabt2rust_type(&args[0]),
};
let mut func_return = format!(" -> {}", return_type);
let func_return = format!(" -> {}", return_type);
let assertion = String::from("assert!(result.is_quiet_nan())");

// We map the arguments provided into the raw Arguments provided
Expand Down Expand Up @@ -409,7 +404,7 @@ fn {}_assert_malformed() {{
fn visit_action(&mut self, action: &Action, expected: Option<&Vec<Value>>) -> Option<String> {
match action {
Action::Invoke {
module,
module: _,
field,
args,
} => {
Expand Down Expand Up @@ -554,16 +549,25 @@ fn {}() {{
CommandKind::AssertMalformed { module, message: _ } => {
self.visit_assert_malformed(module);
}
CommandKind::AssertUninstantiable { module, message: _ } => {
CommandKind::AssertUninstantiable {
module: _,
message: _,
} => {
// Do nothing for now
}
CommandKind::AssertExhaustion { action } => {
CommandKind::AssertExhaustion { action: _ } => {
// Do nothing for now
}
CommandKind::AssertUnlinkable { module, message: _ } => {
CommandKind::AssertUnlinkable {
module: _,
message: _,
} => {
// Do nothing for now
}
CommandKind::Register { name, as_name } => {
CommandKind::Register {
name: _,
as_name: _,
} => {
// Do nothing for now
}
CommandKind::PerformAction(action) => {
Expand Down Expand Up @@ -592,7 +596,7 @@ fn wast_to_rust(wast_filepath: &str) -> (String, i32) {
.expect("Can't get wast file metadata")
.modified()
.expect("Can't get wast file modified date");
let should_modify = match fs::metadata(&rust_test_filepath) {
let _should_modify = match fs::metadata(&rust_test_filepath) {
Ok(m) => {
m.modified()
.expect("Can't get rust test file modified date")
Expand Down
11 changes: 3 additions & 8 deletions src/integrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::webassembly::{ImportObject, VmCtx};
use libc::{printf, putchar};
use std::io;
use std::io::Write;

extern "C" fn _printf(memory_offset: i32, extra: i32, vm_context: &mut VmCtx) -> i32 {
// println!("PRINTF");
Expand All @@ -23,18 +21,15 @@ pub fn generate_libc_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
#[cfg(test)]
mod tests {
use super::generate_libc_env;
use crate::webassembly::{
instantiate, ErrorKind, Export, ImportObject, Instance, Module, ResultObject, VmCtx,
};
use libc::putchar;
use crate::webassembly::{instantiate, Export, VmCtx};

#[test]
fn test_putchar() {
let wasm_bytes = include_wast2wasm_bytes!("tests/putchar.wast");
let import_object = generate_libc_env();
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
let module = result_object.module;
let mut instance = result_object.instance;
let instance = result_object.instance;
let func_index = match module.info.exports.get("main") {
Some(&Export::Function(index)) => index,
_ => panic!("Function not found"),
Expand All @@ -50,7 +45,7 @@ mod tests {
let import_object = generate_libc_env();
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
let module = result_object.module;
let mut instance = result_object.instance;
let instance = result_object.instance;
let func_index = match module.info.exports.get("main") {
Some(&Export::Function(index)) => index,
_ => panic!("Function not found"),
Expand Down
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ macro_rules! get_instance_function {
macro_rules! include_wast2wasm_bytes {
($x:expr) => {{
use wabt::wat2wasm;
const wast_bytes: &[u8] = include_bytes!($x);
wat2wasm(wast_bytes.to_vec()).expect(&format!("Can't convert {} file to wasm", $x))
const WAST_BYTES: &[u8] = include_bytes!($x);
wat2wasm(WAST_BYTES.to_vec()).expect(&format!("Can't convert {} file to wasm", $x))
}};
}

Expand Down
15 changes: 6 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
extern crate test;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate structopt;
extern crate cranelift_codegen;
extern crate cranelift_entity;
extern crate cranelift_native;
extern crate cranelift_wasm;
extern crate structopt;
extern crate wabt;
#[macro_use]
extern crate target_lexicon;
extern crate nix;
extern crate spin;

// use std::alloc::System;
use std::time::{Duration, Instant};
// use std::time::{Duration, Instant};

// #[global_allocator]
// static A: System = System;

// #[macro_use] extern crate log;

use libc;
// use libc;
use std::error::Error;
use std::fs::File;
use std::io;
Expand Down Expand Up @@ -78,11 +77,9 @@ fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
}

let import_object = integrations::generate_libc_env();
let webassembly::ResultObject {
module,
mut instance,
} = webassembly::instantiate(wasm_binary, import_object)
.map_err(|err| String::from(err.description()))?;
let webassembly::ResultObject { module, instance } =
webassembly::instantiate(wasm_binary, import_object)
.map_err(|err| String::from(err.description()))?;
let func_index = instance
.start_func
.unwrap_or_else(|| match module.info.exports.get("main") {
Expand Down
6 changes: 3 additions & 3 deletions src/sighandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ pub unsafe fn install_sighandler() {
sigaction(SIGILL, &sa).unwrap();
sigaction(SIGSEGV, &sa).unwrap();
sigaction(SIGBUS, &sa).unwrap();
let result = setjmp((&mut setjmp_buffer[..]).as_mut_ptr() as *mut ::nix::libc::c_void);
let result = setjmp((&mut SETJMP_BUFFER[..]).as_mut_ptr() as *mut ::nix::libc::c_void);
if result != 0 {
panic!("Signal Error: {}", result);
}
}

static mut setjmp_buffer: [::nix::libc::c_int; 27] = [0; 27];
static mut SETJMP_BUFFER: [::nix::libc::c_int; 27] = [0; 27];
extern "C" {
fn setjmp(env: *mut ::nix::libc::c_void) -> ::nix::libc::c_int;
fn longjmp(env: *mut ::nix::libc::c_void, val: ::nix::libc::c_int);
}
extern "C" fn signal_trap_handler(_: ::nix::libc::c_int) {
unsafe {
longjmp(
(&mut setjmp_buffer).as_mut_ptr() as *mut ::nix::libc::c_void,
(&mut SETJMP_BUFFER).as_mut_ptr() as *mut ::nix::libc::c_void,
3,
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/spectests/_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub trait NaNCheck {
impl NaNCheck for f32 {
/// The MSB of the mantissa must be set for a NaN to be a quiet NaN.
fn is_quiet_nan(&self) -> bool {
let bit_mask = (0b1 << 22); // Used to check if 23rd bit is set, which is MSB of the mantissa
let bit_mask = 0b1 << 22; // Used to check if 23rd bit is set, which is MSB of the mantissa
self.is_nan() && (self.to_bits() & bit_mask) == bit_mask
}

Expand All @@ -53,7 +53,7 @@ impl NaNCheck for f32 {
impl NaNCheck for f64 {
/// The MSB of the mantissa must be set for a NaN to be a quiet NaN.
fn is_quiet_nan(&self) -> bool {
let bit_mask = (0b1 << 51); // Used to check if 51st bit is set, which is MSB of the mantissa
let bit_mask = 0b1 << 51; // Used to check if 51st bit is set, which is MSB of the mantissa
self.is_nan() && (self.to_bits() & bit_mask) == bit_mask
}

Expand All @@ -65,4 +65,3 @@ impl NaNCheck for f64 {
(self.to_bits() & bit_mask) == bit_mask
}
}

16 changes: 8 additions & 8 deletions src/webassembly/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use region;
use std::iter::Iterator;
use std::marker::PhantomData;
use std::ptr::write_unaligned;
use std::slice;
use std::sync::Arc;
use std::{mem, slice};

use super::super::common::slice::{BoundedSlice, UncheckedSlice};
use super::errors::ErrorKind;
Expand Down Expand Up @@ -59,7 +59,7 @@ fn get_function_addr(
}

// #[derive(Debug)]
#[repr(C, packed)]
#[repr(C)]
pub struct VmCtx<'phantom> {
pub user_data: UserData,
globals: UncheckedSlice<u8>,
Expand All @@ -69,7 +69,7 @@ pub struct VmCtx<'phantom> {
}

// #[derive(Debug)]
#[repr(C, packed)]
#[repr(C)]
pub struct UserData {
// pub process: Dispatch<Process>,
pub instance: Instance,
Expand Down Expand Up @@ -126,7 +126,7 @@ impl Instance {
// We walk through the imported functions and set the relocations
// for each of this functions to be an empty vector (as is defined outside of wasm)
for (module, field) in module.info.imported_funcs.iter() {
let mut function = import_object
let function = import_object
.get(&module.as_str(), &field.as_str())
.ok_or_else(|| {
ErrorKind::LinkError(format!(
Expand Down Expand Up @@ -163,7 +163,7 @@ impl Instance {
.compile_and_emit(&*isa, &mut code_buf, &mut reloc_sink, &mut trap_sink)
.map_err(|e| ErrorKind::CompileError(e.to_string()))?;
// We set this code_buf to be readable & executable
protect_codebuf(&code_buf);
protect_codebuf(&code_buf).unwrap();

let func_offset = code_buf;
functions.push(func_offset);
Expand Down Expand Up @@ -334,7 +334,7 @@ impl Instance {
}
for init in &module.info.data_initializers {
debug_assert!(init.base.is_none(), "globalvar base not supported yet");
let mut offset = init.offset;
let offset = init.offset;
let mem_mut = memories[init.memory_index].as_mut();
let to_init = &mut mem_mut[offset..offset + init.data.len()];
to_init.copy_from_slice(&init.data);
Expand All @@ -359,7 +359,7 @@ impl Instance {
GlobalInit::I64Const(n) => n,
GlobalInit::F32Const(f) => f as _, // unsafe { mem::transmute(f as f64) },
GlobalInit::F64Const(f) => f as _, // unsafe { mem::transmute(f) },
GlobalInit::GlobalRef(global_index) => {
GlobalInit::GlobalRef(_global_index) => {
unimplemented!("GlobalInit::GlobalRef is not yet supported")
}
GlobalInit::Import() => {
Expand Down Expand Up @@ -484,7 +484,7 @@ impl Clone for Instance {
}

extern "C" fn grow_memory(size: u32, memory_index: u32, vmctx: &mut VmCtx) -> i32 {
let mut instance = &mut vmctx.user_data.instance;
let instance = &mut vmctx.user_data.instance;
instance
.memory_mut(memory_index as usize)
.grow(size)
Expand Down
Loading

0 comments on commit e13ab80

Please sign in to comment.