Skip to content

Commit

Permalink
Fix: move test failed (#408)
Browse files Browse the repository at this point in the history
* fix: move test failed

* fix: better move solution output
  • Loading branch information
jacob-chia authored Dec 24, 2023
1 parent 773e8b6 commit 00a4d49
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 16 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ default = [
"print_txn_corpus",
"full_trace",
"force_cache",
"real_balance"
"real_balance",
]
evm = []
cmp = []
Expand Down Expand Up @@ -127,3 +127,6 @@ alloy-primitives = "0.4.1"
tracing = "0.1"
tracing-subscriber = "0.3"
colored = "2.0"

# error handling
anyhow = "1.0"
16 changes: 8 additions & 8 deletions src/move/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use move_vm_types::{
};
use serde::{Deserialize, Serialize};

use super::input_printer::print_value;
use crate::{
evm::{abi::BoxedABI, types::EVMU256},
generic_vm::vm_executor::ExecutionResult,
Expand Down Expand Up @@ -144,14 +145,13 @@ impl ConciseSerde for ConciseMoveInput {
}

fn serialize_string(&self) -> String {
format!(
"{:?} => {}::{}<{}>({})",
self.caller,
self.module,
self.function,
self.ty_args.iter().map(|ty| format!("{:?}", ty)).join(","),
self.args.iter().map(|arg| format!("{:?}", arg.value)).join(", ")
)
let mut res = format!("{:?} => {}::{}", self.caller, self.module, self.function);
if !self.ty_args.is_empty() {
res.push_str(format!("<{}>", self.ty_args.iter().map(|ty| format!("{:?}", ty)).join(",")).as_str())
}
res.push_str(format!("({})", self.args.iter().map(|arg| print_value(&arg.value)).join(", ")).as_str());

res
}
}

Expand Down
157 changes: 157 additions & 0 deletions src/move/input_printer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
use std::fmt::Write;

use anyhow::{anyhow, Result};
use move_core_types::{account_address::AccountAddress, u256};
use move_vm_types::values::{Container, ContainerRef, IndexedRef, Value, ValueImpl};

pub fn print_value(val: &Value) -> String {
let mut buf = String::new();
print_value_impl(&mut buf, &val.0).unwrap();
buf
}

fn print_value_impl<B: Write>(buf: &mut B, val: &ValueImpl) -> Result<()> {
match val {
ValueImpl::Invalid => print_invalid(buf),

ValueImpl::U8(x) => print_u8(buf, x),
ValueImpl::U16(x) => print_u16(buf, x),
ValueImpl::U32(x) => print_u32(buf, x),
ValueImpl::U64(x) => print_u64(buf, x),
ValueImpl::U128(x) => print_u128(buf, x),
ValueImpl::U256(x) => print_u256(buf, x),
ValueImpl::Bool(x) => print_bool(buf, x),
ValueImpl::Address(x) => print_address(buf, x),

ValueImpl::Container(c) => print_container(buf, c),

ValueImpl::ContainerRef(r) => print_container_ref(buf, r),
ValueImpl::IndexedRef(r) => print_indexed_ref(buf, r),
}
}

fn print_invalid<B: Write>(buf: &mut B) -> Result<()> {
Ok(write!(buf, "-")?)
}

fn print_u8<B: Write>(buf: &mut B, x: &u8) -> Result<()> {
Ok(write!(buf, "{}", x)?)
}

fn print_u16<B: Write>(buf: &mut B, x: &u16) -> Result<()> {
Ok(write!(buf, "{}", x)?)
}

fn print_u32<B: Write>(buf: &mut B, x: &u32) -> Result<()> {
Ok(write!(buf, "{}", x)?)
}

fn print_u64<B: Write>(buf: &mut B, x: &u64) -> Result<()> {
Ok(write!(buf, "{}", x)?)
}

fn print_u128<B: Write>(buf: &mut B, x: &u128) -> Result<()> {
Ok(write!(buf, "{}", x)?)
}

fn print_u256<B: Write>(buf: &mut B, x: &u256::U256) -> Result<()> {
Ok(write!(buf, "{}", x)?)
}

fn print_bool<B: Write>(buf: &mut B, x: &bool) -> Result<()> {
Ok(write!(buf, "{}", x)?)
}

fn print_address<B: Write>(buf: &mut B, x: &AccountAddress) -> Result<()> {
Ok(write!(buf, "{}", x)?)
}

fn print_list<'a, B, I, X, F>(buf: &mut B, begin: &str, items: I, print: F, end: &str) -> Result<()>
where
B: Write,
X: 'a,
I: IntoIterator<Item = &'a X>,
F: Fn(&mut B, &X) -> Result<()>,
{
write!(buf, "{}", begin)?;
let mut it = items.into_iter();
if let Some(x) = it.next() {
print(buf, x)?;
for x in it {
write!(buf, ", ")?;
print(buf, x)?;
}
}
write!(buf, "{}", end)?;
Ok(())
}

fn print_bytes<B: Write>(buf: &mut B, bytes: &[u8]) -> Result<()> {
if bytes.is_empty() {
write!(buf, "\"\"")?;
} else {
write!(buf, "0x{}", hex::encode(bytes))?;
}

Ok(())
}

fn print_container<B: Write>(buf: &mut B, c: &Container) -> Result<()> {
match c {
Container::Vec(r) => print_list(buf, "[", r.borrow().iter(), print_value_impl, "]"),

Container::Struct(r) => print_list(buf, "{ ", r.borrow().iter(), print_value_impl, " }"),

Container::VecU8(r) => print_bytes(buf, r.borrow().as_ref()),
Container::VecU16(r) => print_list(buf, "[", r.borrow().iter(), print_u16, "]"),
Container::VecU32(r) => print_list(buf, "[", r.borrow().iter(), print_u32, "]"),
Container::VecU64(r) => print_list(buf, "[", r.borrow().iter(), print_u64, "]"),
Container::VecU128(r) => print_list(buf, "[", r.borrow().iter(), print_u128, "]"),
Container::VecU256(r) => print_list(buf, "[", r.borrow().iter(), print_u256, "]"),
Container::VecBool(r) => print_list(buf, "[", r.borrow().iter(), print_bool, "]"),
Container::VecAddress(r) => print_list(buf, "[", r.borrow().iter(), print_address, "]"),

Container::Locals(_) => Err(anyhow!("debug print - invalid container: Locals")),
}
}

fn print_container_ref<B: Write>(buf: &mut B, r: &ContainerRef) -> Result<()> {
let c = match r {
ContainerRef::Local(container) | ContainerRef::Global { container, .. } => container,
};

print_container(buf, c)
}

fn print_slice_elem<B, X, F>(buf: &mut B, v: &[X], idx: usize, print: F) -> Result<()>
where
B: Write,
F: FnOnce(&mut B, &X) -> Result<()>,
{
match v.get(idx) {
Some(x) => print(buf, x),
None => Err(anyhow!("ref index out of bounds")),
}
}

fn print_indexed_ref<B: Write>(buf: &mut B, r: &IndexedRef) -> Result<()> {
let idx = r.idx;
let c = match &r.container_ref {
ContainerRef::Local(container) | ContainerRef::Global { container, .. } => container,
};

match c {
Container::Locals(r) | Container::Vec(r) | Container::Struct(r) => {
print_slice_elem(buf, &r.borrow(), idx, print_value_impl)
}

Container::VecU8(r) => print_slice_elem(buf, &r.borrow(), idx, print_u8),
Container::VecU16(r) => print_slice_elem(buf, &r.borrow(), idx, print_u16),
Container::VecU32(r) => print_slice_elem(buf, &r.borrow(), idx, print_u32),
Container::VecU64(r) => print_slice_elem(buf, &r.borrow(), idx, print_u64),
Container::VecU128(r) => print_slice_elem(buf, &r.borrow(), idx, print_u128),
Container::VecU256(r) => print_slice_elem(buf, &r.borrow(), idx, print_u256),
Container::VecBool(r) => print_slice_elem(buf, &r.borrow(), idx, print_bool),
Container::VecAddress(r) => print_slice_elem(buf, &r.borrow(), idx, print_address),
}
}
2 changes: 2 additions & 0 deletions src/move/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub mod scheduler;
pub mod types;
pub mod vm_state;

mod input_printer;

use clap::Parser;

use crate::fuzzers::move_fuzzer::{move_fuzzer, MoveFuzzConfig};
Expand Down
7 changes: 4 additions & 3 deletions src/move/oracles/typed_bug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ impl
.map(|bug_id| {
let mut hasher = DefaultHasher::new();
bug_id.hash(&mut hasher);
let real_bug_idx = (hasher.finish() << 8) + TYPED_BUG_BUG_IDX;
let msg = json!({
"bug_type": ctx.post_state.typed_bug,
"bug_type": "Bug".to_string(),
"bug_info": format!("{:?} violated", bug_id),
"module": ctx.input.module,
"bug_idx": real_bug_idx,
});
unsafe {
ORACLE_OUTPUT.push(msg);
}

hasher.finish() << (8 + TYPED_BUG_BUG_IDX)
real_bug_idx
})
.collect_vec()
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/move/generic_struct/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "generic_struct"
version = "0.0.1"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/mainnet" }

[addresses]
generic_struct = "0x0"
2 changes: 1 addition & 1 deletion tests/move/helloworld/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "hello_world"
version = "0.0.1"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/mainnet" }

[addresses]
hello_world = "0x0"
2 changes: 1 addition & 1 deletion tests/move/share_object/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "share_object"
version = "0.0.1"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/mainnet" }

[addresses]
share_object = "0x0"
2 changes: 1 addition & 1 deletion tests/move/struct_tests/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "struct_tests"
version = "0.0.1"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/mainnet" }

[addresses]
struct_tests = "0x0"

0 comments on commit 00a4d49

Please sign in to comment.