-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement delegate_call #80
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8; | ||
|
||
/* runner.json | ||
{ | ||
"differential": true, | ||
"actions": [ | ||
{ | ||
"Upload": { | ||
"code": { | ||
"Solidity": { | ||
"contract": "Logic" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"Instantiate": { | ||
"code": { | ||
"Solidity": { | ||
"contract": "Tester" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"Call": { | ||
"dest": { | ||
"Instantiated": 0 | ||
}, | ||
"value": 123, | ||
"data": "6466414b0000000000000000000000000000000000000000000000000000000000000020" | ||
} | ||
} | ||
] | ||
} | ||
*/ | ||
|
||
contract Logic { | ||
// NOTE: storage layout must be the same as contract Tester | ||
uint256 public num; | ||
address public sender; | ||
uint256 public value; | ||
|
||
event DidSetVars(); | ||
|
||
function setVars(uint256 _num) public payable returns (uint256) { | ||
num = _num * 2; | ||
sender = msg.sender; | ||
value = msg.value; | ||
emit DidSetVars(); | ||
return _num; | ||
} | ||
} | ||
|
||
contract Tester { | ||
uint256 public num; | ||
address public sender; | ||
uint256 public value; | ||
|
||
function setVars(uint256 _num) public payable returns (bool, bytes memory) { | ||
Logic impl = new Logic(); | ||
|
||
// Tester's storage is set, Logic is not modified. | ||
(bool success, bytes memory data) = address(impl).delegatecall( | ||
abi.encodeWithSignature("setVars(uint256)", _num) | ||
); | ||
|
||
assert(success); | ||
assert(impl.num() == 0); | ||
assert(impl.sender() == address(0)); | ||
assert(impl.value() == 0); | ||
assert(this.num() == _num * 2); | ||
assert(this.sender() == msg.sender); | ||
assert(this.value() == msg.value); | ||
|
||
return (success, data); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ test_spec!(call, "Caller", "Call.sol"); | |
test_spec!(transfer, "Transfer", "Transfer.sol"); | ||
test_spec!(return_data_oob, "ReturnDataOob", "ReturnDataOob.sol"); | ||
test_spec!(immutables, "Immutables", "Immutables.sol"); | ||
test_spec!(delegate, "Delegate", "Delegate.sol"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could use some better test coverage, i.e. calling into non-existent accounts, plain accounts, recursively into self (from runtime code and the constructor). |
||
|
||
fn instantiate(path: &str, contract: &str) -> Vec<SpecsAction> { | ||
vec![Instantiate { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ const REENTRANT_CALL_FLAG: u32 = 0b0000_1000; | |
#[allow(clippy::too_many_arguments)] | ||
pub fn call<'ctx, D>( | ||
context: &mut Context<'ctx, D>, | ||
_gas: inkwell::values::IntValue<'ctx>, | ||
gas: inkwell::values::IntValue<'ctx>, | ||
address: inkwell::values::IntValue<'ctx>, | ||
value: Option<inkwell::values::IntValue<'ctx>>, | ||
input_offset: inkwell::values::IntValue<'ctx>, | ||
|
@@ -38,10 +38,9 @@ where | |
let output_offset = context.safe_truncate_int_to_xlen(output_offset)?; | ||
let output_length = context.safe_truncate_int_to_xlen(output_length)?; | ||
|
||
// TODO: What to supply here? Is there a weight to gas? | ||
let _gas = context | ||
let gas = context | ||
.builder() | ||
.build_int_truncate(_gas, context.integer_type(64), "gas")?; | ||
.build_int_truncate(gas, context.integer_type(64), "gas")?; | ||
|
||
let input_pointer = context.build_heap_gep(input_offset, input_length)?; | ||
let output_pointer = context.build_heap_gep(output_offset, output_length)?; | ||
|
@@ -61,7 +60,7 @@ where | |
let arguments = &[ | ||
flags.as_basic_value_enum(), | ||
address_pointer.value.as_basic_value_enum(), | ||
context.integer_const(64, 0).as_basic_value_enum(), | ||
gas.as_basic_value_enum(), | ||
context.integer_const(64, 0).as_basic_value_enum(), | ||
context.sentinel_pointer().value.as_basic_value_enum(), | ||
value_pointer.value.as_basic_value_enum(), | ||
|
@@ -103,20 +102,79 @@ where | |
|
||
#[allow(clippy::too_many_arguments)] | ||
pub fn delegate_call<'ctx, D>( | ||
_context: &mut Context<'ctx, D>, | ||
_gas: inkwell::values::IntValue<'ctx>, | ||
_address: inkwell::values::IntValue<'ctx>, | ||
context: &mut Context<'ctx, D>, | ||
gas: inkwell::values::IntValue<'ctx>, | ||
address: inkwell::values::IntValue<'ctx>, | ||
_value: Option<inkwell::values::IntValue<'ctx>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be removed. |
||
_input_offset: inkwell::values::IntValue<'ctx>, | ||
_input_length: inkwell::values::IntValue<'ctx>, | ||
_output_offset: inkwell::values::IntValue<'ctx>, | ||
_output_length: inkwell::values::IntValue<'ctx>, | ||
input_offset: inkwell::values::IntValue<'ctx>, | ||
input_length: inkwell::values::IntValue<'ctx>, | ||
output_offset: inkwell::values::IntValue<'ctx>, | ||
output_length: inkwell::values::IntValue<'ctx>, | ||
_constants: Vec<Option<num::BigUint>>, | ||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> | ||
where | ||
D: Dependency + Clone, | ||
{ | ||
todo!() | ||
let address_pointer = context.build_address_argument_store(address)?; | ||
|
||
let input_offset = context.safe_truncate_int_to_xlen(input_offset)?; | ||
let input_length = context.safe_truncate_int_to_xlen(input_length)?; | ||
let output_offset = context.safe_truncate_int_to_xlen(output_offset)?; | ||
let output_length = context.safe_truncate_int_to_xlen(output_length)?; | ||
|
||
let gas = context | ||
.builder() | ||
.build_int_truncate(gas, context.integer_type(64), "gas")?; | ||
|
||
let input_pointer = context.build_heap_gep(input_offset, input_length)?; | ||
let output_pointer = context.build_heap_gep(output_offset, output_length)?; | ||
|
||
let output_length_pointer = context.build_alloca_at_entry(context.xlen_type(), "output_length"); | ||
context.build_store(output_length_pointer, output_length)?; | ||
|
||
let flags = context.xlen_type().const_int(0u64, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delegate calls can be reentrant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pallet-revive doesn't allow reentrant for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah okay. Would be good to know the reason for this and document it :) I mean ideally this would be changed in the pallet to better match the EVM semantics. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At first I thought pallet-revive was updated to fully support revive but now it seems more just a copy of pallet-contracts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, pallet-revive was started as a fork of pallet-contracts :) Since the contracts pallet is already close to what we need and heavily audited, we decided to fork it instead of starting from scratch. |
||
|
||
let argument_type = revive_runtime_api::calling_convention::delegate_call(context.llvm()); | ||
let argument_pointer = context.build_alloca_at_entry(argument_type, "delegate_call_arguments"); | ||
let arguments = &[ | ||
flags.as_basic_value_enum(), | ||
address_pointer.value.as_basic_value_enum(), | ||
gas.as_basic_value_enum(), | ||
context.integer_const(64, 0).as_basic_value_enum(), | ||
input_pointer.value.as_basic_value_enum(), | ||
input_length.as_basic_value_enum(), | ||
output_pointer.value.as_basic_value_enum(), | ||
output_length_pointer.value.as_basic_value_enum(), | ||
]; | ||
revive_runtime_api::calling_convention::spill( | ||
context.builder(), | ||
argument_pointer.value, | ||
argument_type, | ||
arguments, | ||
)?; | ||
|
||
let name = runtime_api::imports::DELEGATE_CALL; | ||
let argument_pointer = context.builder().build_ptr_to_int( | ||
argument_pointer.value, | ||
context.xlen_type(), | ||
"delegate_call_argument_pointer", | ||
)?; | ||
let success = context | ||
.build_runtime_call(name, &[argument_pointer.into()]) | ||
.unwrap_or_else(|| panic!("{name} should return a value")) | ||
.into_int_value(); | ||
|
||
let is_success = context.builder().build_int_compare( | ||
inkwell::IntPredicate::EQ, | ||
success, | ||
context.xlen_type().const_zero(), | ||
"is_success", | ||
)?; | ||
|
||
Ok(context | ||
.builder() | ||
.build_int_z_extend(is_success, context.word_type(), "success")? | ||
.as_basic_value_enum()) | ||
} | ||
|
||
/// Translates the Yul `linkersymbol` instruction. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing this is irrelevant here? Keeping the fixtures as concise as possible eases debugging them :)