From 3f0094113af46697b2e122b79854bb5c193d27f1 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 6 Nov 2018 19:21:38 +0800 Subject: [PATCH 1/5] Fix json tracer overflow --- evmbin/src/display/json.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/evmbin/src/display/json.rs b/evmbin/src/display/json.rs index 8159b07e9c3..af517571151 100644 --- a/evmbin/src/display/json.rs +++ b/evmbin/src/display/json.rs @@ -125,7 +125,6 @@ impl trace::VMTracer for Informant { fn trace_executed(&mut self, gas_used: U256, stack_push: &[U256], mem: &[u8]) { let subdepth = self.subdepth; Self::with_informant_in_depth(self, subdepth, |informant: &mut Informant| { - let mem_diff = informant.mem_written.clone().map(|(o, s)| (o, &(mem[o..o+s]))); let store_diff = informant.store_written.clone(); let info = ::evm::Instruction::from_u8(informant.instruction).map(|i| i.info()); @@ -151,11 +150,11 @@ impl trace::VMTracer for Informant { informant.stack.extend_from_slice(stack_push); // TODO [ToDr] Align memory? - if let Some((pos, data)) = mem_diff { - if informant.memory.len() < (pos + data.len()) { - informant.memory.resize(pos + data.len(), 0); + if let Some((pos, size)) = informant.mem_written.clone() { + if informant.memory.len() < (pos + size) { + informant.memory.resize(pos + size, 0); } - informant.memory[pos..pos + data.len()].copy_from_slice(data); + informant.memory[pos..(pos + size)].copy_from_slice(&mem[pos..(pos + size)]); } if let Some((pos, val)) = store_diff { From cad06ec4edaea9488214815cdf564c6371db571b Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 7 Nov 2018 21:15:45 +0800 Subject: [PATCH 2/5] Replace trace_executed with a direct trace push --- evmbin/src/display/json.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/evmbin/src/display/json.rs b/evmbin/src/display/json.rs index af517571151..52d8255f50f 100644 --- a/evmbin/src/display/json.rs +++ b/evmbin/src/display/json.rs @@ -194,7 +194,25 @@ impl trace::VMTracer for Informant { // print last line with final state: self.gas_cost = 0.into(); let gas_used = self.gas_used; - self.trace_executed(gas_used, &[], &[]); + let subdepth = self.subdepth; + + Self::with_informant_in_depth(&mut self, subdepth, |informant: &mut Informant| { + let store_diff = informant.store_written.clone(); + let info = ::evm::Instruction::from_u8(informant.instruction).map(|i| i.info()); + + let trace = json!({ + "pc": informant.pc, + "op": informant.instruction, + "opName": info.map(|i| i.name).unwrap_or(""), + "gas": format!("{:#x}", gas_used.saturating_add(informant.gas_cost)), + "gasCost": format!("{:#x}", informant.gas_cost), + "memory": format!("0x{}", informant.memory.to_hex()), + "stack": informant.stack, + "storage": informant.storage, + "depth": informant.depth, + }); + informant.traces.push(trace.to_string()); + }); } else if !self.subtraces.is_empty() { self.traces.extend(mem::replace(&mut self.subtraces, vec![])); } From 78841de8397356dfd4cb6ce94563435a7448576e Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 7 Nov 2018 21:18:07 +0800 Subject: [PATCH 3/5] Remove unused variable --- evmbin/src/display/json.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/evmbin/src/display/json.rs b/evmbin/src/display/json.rs index 52d8255f50f..36abc844bb8 100644 --- a/evmbin/src/display/json.rs +++ b/evmbin/src/display/json.rs @@ -197,7 +197,6 @@ impl trace::VMTracer for Informant { let subdepth = self.subdepth; Self::with_informant_in_depth(&mut self, subdepth, |informant: &mut Informant| { - let store_diff = informant.store_written.clone(); let info = ::evm::Instruction::from_u8(informant.instruction).map(|i| i.info()); let trace = json!({ From 514d0f1b23d696be569e5d181e9663e851bab379 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 7 Nov 2018 22:32:16 +0800 Subject: [PATCH 4/5] Add test for 5a51 --- evmbin/src/display/json.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/evmbin/src/display/json.rs b/evmbin/src/display/json.rs index 36abc844bb8..cdcd8509529 100644 --- a/evmbin/src/display/json.rs +++ b/evmbin/src/display/json.rs @@ -296,6 +296,17 @@ mod tests { {"pc":0,"op":248,"opName":"","gas":"0xffff","gasCost":"0x0","memory":"0x","stack":[],"storage":{},"depth":1} "#, ); + + run_test( + Informant::default(), + &compare_json, + "5A51", + 0xfffff, + r#" +{"depth":1,"gas":"0xfffff","gasCost":"0x2","memory":"0x","op":90,"opName":"GAS","pc":0,"stack":[],"storage":{}} +{"depth":1,"gas":"0xffffd","gasCost":"0x0","memory":"0x","op":81,"opName":"MLOAD","pc":1,"stack":["0xffffd"],"storage":{}} + "#, + ); } #[test] From d11b3a86c1acbd6bafef114a4e0f042dfd40137d Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 7 Nov 2018 22:59:43 +0800 Subject: [PATCH 5/5] Remove duplicate json! --- evmbin/src/display/json.rs | 46 +++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/evmbin/src/display/json.rs b/evmbin/src/display/json.rs index cdcd8509529..9b1b7b10e37 100644 --- a/evmbin/src/display/json.rs +++ b/evmbin/src/display/json.rs @@ -55,6 +55,22 @@ impl Informant { Self::with_informant_in_depth(informant.subinfos.last_mut().expect("prepare/done_trace are not balanced"), depth - 1, f); } } + + fn informant_trace(informant: &Informant, gas_used: U256) -> String { + let info = ::evm::Instruction::from_u8(informant.instruction).map(|i| i.info()); + + json!({ + "pc": informant.pc, + "op": informant.instruction, + "opName": info.map(|i| i.name).unwrap_or(""), + "gas": format!("{:#x}", gas_used.saturating_add(informant.gas_cost)), + "gasCost": format!("{:#x}", informant.gas_cost), + "memory": format!("0x{}", informant.memory.to_hex()), + "stack": informant.stack, + "storage": informant.storage, + "depth": informant.depth, + }).to_string() + } } impl vm::Informant for Informant { @@ -128,18 +144,8 @@ impl trace::VMTracer for Informant { let store_diff = informant.store_written.clone(); let info = ::evm::Instruction::from_u8(informant.instruction).map(|i| i.info()); - let trace = json!({ - "pc": informant.pc, - "op": informant.instruction, - "opName": info.map(|i| i.name).unwrap_or(""), - "gas": format!("{:#x}", gas_used.saturating_add(informant.gas_cost)), - "gasCost": format!("{:#x}", informant.gas_cost), - "memory": format!("0x{}", informant.memory.to_hex()), - "stack": informant.stack, - "storage": informant.storage, - "depth": informant.depth, - }); - informant.traces.push(trace.to_string()); + let trace = Self::informant_trace(informant, gas_used); + informant.traces.push(trace); informant.unmatched = false; informant.gas_used = gas_used; @@ -197,20 +203,8 @@ impl trace::VMTracer for Informant { let subdepth = self.subdepth; Self::with_informant_in_depth(&mut self, subdepth, |informant: &mut Informant| { - let info = ::evm::Instruction::from_u8(informant.instruction).map(|i| i.info()); - - let trace = json!({ - "pc": informant.pc, - "op": informant.instruction, - "opName": info.map(|i| i.name).unwrap_or(""), - "gas": format!("{:#x}", gas_used.saturating_add(informant.gas_cost)), - "gasCost": format!("{:#x}", informant.gas_cost), - "memory": format!("0x{}", informant.memory.to_hex()), - "stack": informant.stack, - "storage": informant.storage, - "depth": informant.depth, - }); - informant.traces.push(trace.to_string()); + let trace = Self::informant_trace(informant, gas_used); + informant.traces.push(trace); }); } else if !self.subtraces.is_empty() { self.traces.extend(mem::replace(&mut self.subtraces, vec![]));