Skip to content

Commit

Permalink
arguments: better string type
Browse files Browse the repository at this point in the history
  • Loading branch information
cdump committed Jun 7, 2024
1 parent fab5fa8 commit d6fe511
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
10 changes: 8 additions & 2 deletions evmole/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,21 @@ def function_arguments(code: bytes | str, selector: bytes | str, gas_limit: int
vm.stack.peek().label = ArgDynamic(offset=arg.offset)

case (Op.SHL, _, Element() as ot, Element(ArgDynamicLength() as arg)):
if int.from_bytes(ot.data, 'big') == 5:
v = int.from_bytes(ot.data, 'big')
if v == 5:
args.set(arg.offset, 'uint256[]')
elif v == 1:
args.set(arg.offset, 'string')

case (
(Op.MUL, _, Element(ArgDynamicLength() as arg), Element() as ot)
| (Op.MUL, _, Element() as ot, Element(ArgDynamicLength() as arg))
):
if int.from_bytes(ot.data, 'big') == 32:
v = int.from_bytes(ot.data, 'big')
if v == 32:
args.set(arg.offset, 'uint256[]')
elif v == 2:
args.set(arg.offset, 'string')

case (Op.AND, _, Element(Arg() as arg), Element() as ot) | (Op.AND, _, Element() as ot, Element(Arg() as arg)):
v = int.from_bytes(ot.data, 'big')
Expand Down
26 changes: 20 additions & 6 deletions js/src/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,32 @@ export function functionArguments(code, selector, gas_limit = 1e4) {
case Op.SHL:
{
const [r2, arg] = [uint8ArrayToBigInt(ret[2].data), ret[3].label]
if (r2 == 5n && arg instanceof ArgDynamicLength) {
args.set(arg.offset, 'uint256[]')
if (arg instanceof ArgDynamicLength) {
if (r2 === 5n) {
args.set(arg.offset, 'uint256[]')
} else if (r2 === 1n) {
args.set(arg.offset, 'string')
}
}
}
break

case Op.MUL:
{
if (ret[2].label instanceof ArgDynamicLength && uint8ArrayToBigInt(ret[3].data) == 32n) {
args.set(ret[2].label.offset, 'uint256[]')
} else if (ret[3].label instanceof ArgDynamicLength && uint8ArrayToBigInt(ret[2].data) == 32n) {
args.set(ret[3].label.offset, 'uint256[]')
if (ret[2].label instanceof ArgDynamicLength) {
const n = uint8ArrayToBigInt(ret[3].data);
if (n === 32n) {
args.set(ret[2].label.offset, 'uint256[]')
} else if (n === 2n) {
args.set(ret[2].label.offset, 'string')
}
} else if (ret[3].label instanceof ArgDynamicLength) {
const n = uint8ArrayToBigInt(ret[2].data);
if (n === 32n) {
args.set(ret[3].label.offset, 'uint256[]')
} else if (n === 2n) {
args.set(ret[3].label.offset, 'string')
}
} else if (ret[2].label instanceof Arg) {
args.setIf(ret[2].label.offset, 'bool', '')
} else if (ret[3].label instanceof Arg) {
Expand Down
15 changes: 10 additions & 5 deletions rust/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
};
use std::collections::BTreeMap;

const VAL_2_B: [u8; 32] = ruint::uint!(2_U256).to_be_bytes();
const VAL_4_B: [u8; 32] = ruint::uint!(4_U256).to_be_bytes();
const VAL_5_B: [u8; 32] = ruint::uint!(5_U256).to_be_bytes();
const VAL_131072_B: [u8; 32] = ruint::uint!(131072_U256).to_be_bytes();
Expand Down Expand Up @@ -120,16 +121,20 @@ fn analyze(

StepResult{op: op::SHL, fa: Some(ot), sa: Some(Element{label: Some(Label::ArgDynamicLength(off)), ..}), ..} =>
{
if ot.data == VAL_5_B {
args.set(off, "uint256[]");
}
if ot.data == VAL_5_B {
args.set(off, "uint256[]");
} else if ot.data == VAL_1_B {
args.set(off, "string");
}
}

StepResult{op: op::MUL, fa: Some(Element{label: Some(Label::ArgDynamicLength(off)), ..}), sa: Some(ot), ..}
| StepResult{op: op::MUL, sa: Some(Element{label: Some(Label::ArgDynamicLength(off)), ..}), fa: Some(ot), ..} =>
{
if ot.data == VAL_32_B {
args.set(off, "uint256[]");
args.set(off, "uint256[]");
} else if ot.data == VAL_2_B {
args.set(off, "string");
}
}

Expand Down Expand Up @@ -252,7 +257,7 @@ pub fn function_arguments(code: &[u8], selector: &Selector, gas_limit: u32) -> S
let ret = match vm.step() {
Ok(v) => v,
Err(_e) => {
// eprintln!("{}", _e);
// println!("{}", _e);
break;
}
};
Expand Down

0 comments on commit d6fe511

Please sign in to comment.