Skip to content

Commit

Permalink
Runtime ABI decoding checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
g-r-a-n-t committed Jul 13, 2021
1 parent c0e8d47 commit c1415e2
Show file tree
Hide file tree
Showing 29 changed files with 820 additions and 219 deletions.
29 changes: 29 additions & 0 deletions crates/analyzer/src/namespace/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ impl AbiType {
_ => todo!("recursive encoding"),
}
}

/// `true` if the encoded value is stored in the data section, `false` if it is not.
pub fn has_data(&self) -> bool {
match self {
AbiType::Uint { .. } => false,
AbiType::StaticArray { .. } => false,
AbiType::Tuple { .. } => false,
AbiType::Int { .. } => false,
AbiType::Bool => false,
AbiType::Address => false,
AbiType::String { .. } => true,
AbiType::Bytes { .. } => true,
}
}
}

/// Data can be decoded from memory or calldata.
Expand Down Expand Up @@ -381,6 +395,21 @@ impl FixedSize {
FixedSize::Base(Base::Bool)
}

/// Creates an instance of address.
pub fn address() -> Self {
FixedSize::Base(Base::Address)
}

/// Creates an instance of u256.
pub fn u256() -> Self {
FixedSize::Base(Base::Numeric(Integer::U256))
}

/// Creates an instance of u8.
pub fn u8() -> Self {
FixedSize::Base(Base::Numeric(Integer::U8))
}

/// Creates an instance of `()`.
pub fn unit() -> Self {
FixedSize::Base(Base::Unit)
Expand Down
22 changes: 22 additions & 0 deletions crates/test-files/fixtures/features/abi_decode_checks.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
contract Foo:
pub def decode_u256(a: u256):
pass

pub def decode_u128_bool(a: u128, b: bool):
pass

pub def decode_u256_bytes_tuple_array(
a: u256,
b: u8[100],
c: (address, u8),
d: i16[26]
):
pass

pub def decode_string_address_bytes_bool(
a: String<80>,
b: address,
c: u8[1000],
d: bool
):
pass
32 changes: 27 additions & 5 deletions crates/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,28 @@ impl ContractHarness {
name: &str,
input: &[ethabi::Token],
) -> evm::Capture<(evm::ExitReason, Vec<u8>), std::convert::Infallible> {
let input = self.build_calldata(name, input);
self.capture_call_raw_bytes(executor, input)
}

pub fn build_calldata(&self, name: &str, input: &[ethabi::Token]) -> Vec<u8> {
let function = &self.abi.functions[name][0];
function
.encode_input(input)
.unwrap_or_else(|_| panic!("Unable to encode input for {}", name))
}

pub fn capture_call_raw_bytes(
&self,
executor: &mut Executor,
input: Vec<u8>,
) -> evm::Capture<(evm::ExitReason, Vec<u8>), std::convert::Infallible> {
let context = evm::Context {
address: self.address,
caller: self.caller,
apparent_value: self.value,
};

let input = function
.encode_input(input)
.unwrap_or_else(|_| panic!("Unable to encode input for {}", name));

executor.call(self.address, None, input, None, false, context)
}

Expand Down Expand Up @@ -114,6 +124,13 @@ impl ContractHarness {
validate_revert(self.capture_call(executor, name, input), revert_data)
}

pub fn test_call_reverts(&self, executor: &mut Executor, input: Vec<u8>) {
match self.capture_call_raw_bytes(executor, input) {
evm::Capture::Exit((ExitReason::Revert(_), _)) => {}
_ => panic!("function did not revert"),
}
}

// Executor must be passed by value to get emitted events.
pub fn events_emitted(&self, executor: Executor, events: &[(&str, &[ethabi::Token])]) {
let raw_logs = executor
Expand Down Expand Up @@ -616,10 +633,15 @@ pub fn bytes_token(s: &str) -> ethabi::Token {
}

#[allow(dead_code)]
pub fn u256_array_token(v: &[u64]) -> ethabi::Token {
pub fn uint_array_token(v: &[u64]) -> ethabi::Token {
ethabi::Token::FixedArray(v.iter().map(|n| uint_token(*n)).collect())
}

#[allow(dead_code)]
pub fn int_array_token(v: &[i64]) -> ethabi::Token {
ethabi::Token::FixedArray(v.iter().map(|n| int_token(*n)).collect())
}

#[allow(dead_code)]
pub fn address_array_token(v: &[&str]) -> ethabi::Token {
ethabi::Token::FixedArray(v.iter().map(|s| address_token(s)).collect())
Expand Down
Loading

0 comments on commit c1415e2

Please sign in to comment.