Skip to content
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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions crates/integration/contracts/Delegate.sol
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();
Copy link
Member

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 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just making sure emitted event belongs to caller and not callee


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);
}
}
1 change: 1 addition & 0 deletions crates/integration/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Member

Choose a reason for hiding this comment

The 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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will add more cases


fn instantiate(path: &str, contract: &str) -> Vec<SpecsAction> {
vec![Instantiate {
Expand Down
8 changes: 7 additions & 1 deletion crates/llvm-context/src/polkavm/const/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ pub mod imports {

pub static CALL: &str = "call";

pub static DELEGATE_CALL: &str = "delegate_call";

pub static CALLER: &str = "caller";

pub static CODE_SIZE: &str = "code_size";

pub static CODE_HASH: &str = "code_hash";

pub static DEPOSIT_EVENT: &str = "deposit_event";

pub static GET_IMMUTABLE_DATA: &str = "get_immutable_data";
Expand Down Expand Up @@ -57,15 +61,17 @@ pub mod imports {

/// All imported runtime API symbols.
/// Useful for configuring common attributes and linkage.
pub static IMPORTS: [&str; 21] = [
pub static IMPORTS: [&str; 23] = [
ADDRESS,
BALANCE,
BALANCE_OF,
BLOCK_NUMBER,
CALL,
DELEGATE_CALL,
CALLER,
CHAIN_ID,
CODE_SIZE,
CODE_HASH,
DEPOSIT_EVENT,
GET_IMMUTABLE_DATA,
GET_STORAGE,
Expand Down
73 changes: 66 additions & 7 deletions crates/llvm-context/src/polkavm/evm/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,79 @@ where

#[allow(clippy::too_many_arguments)]
pub fn delegate_call<'ctx, D>(
_context: &mut Context<'ctx, D>,
context: &mut Context<'ctx, D>,
_gas: inkwell::values::IntValue<'ctx>,
_address: inkwell::values::IntValue<'ctx>,
address: inkwell::values::IntValue<'ctx>,
_value: Option<inkwell::values::IntValue<'ctx>>,
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved
_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 extcodehash_pointer =
context.build_alloca_at_entry(context.word_type(), "extcodehash_pointer");

context.build_runtime_call(
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved
runtime_api::imports::CODE_HASH,
&[
address_pointer.to_int(context).into(),
extcodehash_pointer.to_int(context).into(),
],
);

context.build_byte_swap(context.build_load(extcodehash_pointer, "extcodehash_value")?)?;
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved

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)?;

// TODO: What to supply here? Is there a weight to gas?
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved
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);
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved

let name = runtime_api::imports::DELEGATE_CALL;
let success = context
.build_runtime_call(
name,
&[
flags.into(),
extcodehash_pointer.to_int(context).into(),
input_pointer.to_int(context).into(),
input_length.into(),
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).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.
Expand Down
2 changes: 1 addition & 1 deletion crates/runtime-api/src/polkavm_imports.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ POLKAVM_IMPORT(void, caller, uint32_t)

POLKAVM_IMPORT(uint32_t, is_contract, uint32_t)

POLKAVM_IMPORT(uint32_t, code_hash, uint32_t, uint32_t, uint32_t)
POLKAVM_IMPORT(uint32_t, code_hash, uint32_t, uint32_t)

POLKAVM_IMPORT(uint32_t, code_size, uint32_t)

Expand Down