Skip to content

Commit 4302f74

Browse files
xermicusgithub-actions[bot]athei
authored
[pallet-revive] pack exceeding syscall arguments into registers (#7319)
This PR changes how we call runtime API methods with more than 6 arguments: They are no longer spilled to the stack but packed into registers instead. Pointers are 32 bit wide so we can pack two of them into a single 64 bit register. Since we mostly pass pointers, this technique effectively increases the number of arguments we can pass using the available registers. To make this work for `instantiate` too we now pass the code hash and the call data in the same buffer, akin to how the `create` family opcodes work in the EVM. The code hash is fixed in size, implying the start of the constructor call data. --------- Signed-off-by: xermicus <cyrill@parity.io> Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com> Co-authored-by: command-bot <> Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Alexander Theißen <alex.theissen@me.com>
1 parent e6aad5b commit 4302f74

File tree

18 files changed

+715
-745
lines changed

18 files changed

+715
-745
lines changed

prdoc/pr_7319.prdoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
title: '[pallet-revive] pack exceeding syscall arguments into registers'
2+
doc:
3+
- audience: Runtime Dev
4+
description: |-
5+
This PR changes how we call runtime API methods with more than 6 arguments: They are no longer spilled to the stack but packed into registers instead. Pointers are 32 bit wide so we can pack two of them into a single 64 bit register. Since we mostly pass pointers, this technique effectively increases the number of arguments we can pass using the available registers.
6+
7+
To make this work for `instantiate` too we now pass the code hash and the call data in the same buffer, akin to how the `create` family opcodes work in the EVM. The code hash is fixed in size, implying the start of the constructor call data.
8+
crates:
9+
- name: pallet-revive-fixtures
10+
bump: major
11+
- name: pallet-revive-proc-macro
12+
bump: major
13+
- name: pallet-revive
14+
bump: major
15+
- name: pallet-revive-uapi
16+
bump: major

substrate/frame/revive/fixtures/contracts/call_diverging_out_len.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,11 @@ fn assert_instantiate<const N: usize>(expected_output: [u8; BUF_SIZE]) {
6666
let output_buf_capped = &mut &mut output_buf[..N];
6767

6868
api::instantiate(
69-
&code_hash,
7069
u64::MAX,
7170
u64::MAX,
7271
&[u8::MAX; 32],
7372
&[0; 32],
74-
&[0; 32],
73+
&code_hash,
7574
None,
7675
Some(output_buf_capped),
7776
None,

substrate/frame/revive/fixtures/contracts/caller_contract.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use common::{input, u256_bytes};
2222
use uapi::{HostFn, HostFnImpl as api, ReturnErrorCode};
2323

24+
const INPUT: [u8; 8] = [0u8, 1, 34, 51, 68, 85, 102, 119];
25+
const REVERTED_INPUT: [u8; 7] = [1u8, 34, 51, 68, 85, 102, 119];
26+
2427
#[no_mangle]
2528
#[polkavm_derive::polkavm_export]
2629
pub extern "C" fn deploy() {}
@@ -36,17 +39,21 @@ pub extern "C" fn call() {
3639
let salt = [0u8; 32];
3740

3841
// Callee will use the first 4 bytes of the input to return an exit status.
39-
let input = [0u8, 1, 34, 51, 68, 85, 102, 119];
40-
let reverted_input = [1u8, 34, 51, 68, 85, 102, 119];
42+
let mut input_deploy = [0; 32 + INPUT.len()];
43+
input_deploy[..32].copy_from_slice(code_hash);
44+
input_deploy[32..].copy_from_slice(&INPUT);
45+
46+
let mut reverted_input_deploy = [0; 32 + REVERTED_INPUT.len()];
47+
reverted_input_deploy[..32].copy_from_slice(code_hash);
48+
reverted_input_deploy[32..].copy_from_slice(&REVERTED_INPUT);
4149

4250
// Fail to deploy the contract since it returns a non-zero exit status.
4351
let res = api::instantiate(
44-
code_hash,
4552
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
4653
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
4754
&[u8::MAX; 32], // No deposit limit.
4855
&value,
49-
&reverted_input,
56+
&reverted_input_deploy,
5057
None,
5158
None,
5259
Some(&salt),
@@ -55,12 +62,11 @@ pub extern "C" fn call() {
5562

5663
// Fail to deploy the contract due to insufficient ref_time weight.
5764
let res = api::instantiate(
58-
code_hash,
5965
1u64, // too little ref_time weight
6066
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
6167
&[u8::MAX; 32], // No deposit limit.
6268
&value,
63-
&input,
69+
&input_deploy,
6470
None,
6571
None,
6672
Some(&salt),
@@ -69,12 +75,11 @@ pub extern "C" fn call() {
6975

7076
// Fail to deploy the contract due to insufficient proof_size weight.
7177
let res = api::instantiate(
72-
code_hash,
7378
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
7479
1u64, // Too little proof_size weight
7580
&[u8::MAX; 32], // No deposit limit.
7681
&value,
77-
&input,
82+
&input_deploy,
7883
None,
7984
None,
8085
Some(&salt),
@@ -85,12 +90,11 @@ pub extern "C" fn call() {
8590
let mut callee = [0u8; 20];
8691

8792
api::instantiate(
88-
code_hash,
8993
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
9094
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
9195
&[u8::MAX; 32], // No deposit limit.
9296
&value,
93-
&input,
97+
&input_deploy,
9498
Some(&mut callee),
9599
None,
96100
Some(&salt),
@@ -101,11 +105,11 @@ pub extern "C" fn call() {
101105
let res = api::call(
102106
uapi::CallFlags::empty(),
103107
&callee,
104-
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
105-
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
108+
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
109+
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
106110
&[u8::MAX; 32], // No deposit limit.
107111
&value,
108-
&reverted_input,
112+
&REVERTED_INPUT,
109113
None,
110114
);
111115
assert!(matches!(res, Err(ReturnErrorCode::CalleeReverted)));
@@ -118,7 +122,7 @@ pub extern "C" fn call() {
118122
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
119123
&[u8::MAX; 32], // No deposit limit.
120124
&value,
121-
&input,
125+
&INPUT,
122126
None,
123127
);
124128
assert!(matches!(res, Err(ReturnErrorCode::OutOfResources)));
@@ -127,11 +131,11 @@ pub extern "C" fn call() {
127131
let res = api::call(
128132
uapi::CallFlags::empty(),
129133
&callee,
130-
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
131-
1u64, // too little proof_size weight
134+
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
135+
1u64, // too little proof_size weight
132136
&[u8::MAX; 32], // No deposit limit.
133137
&value,
134-
&input,
138+
&INPUT,
135139
None,
136140
);
137141
assert!(matches!(res, Err(ReturnErrorCode::OutOfResources)));
@@ -141,13 +145,13 @@ pub extern "C" fn call() {
141145
api::call(
142146
uapi::CallFlags::empty(),
143147
&callee,
144-
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
145-
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
148+
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
149+
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
146150
&[u8::MAX; 32], // No deposit limit.
147151
&value,
148-
&input,
152+
&INPUT,
149153
Some(&mut &mut output[..]),
150154
)
151155
.unwrap();
152-
assert_eq!(&output, &input[4..])
156+
assert_eq!(&output, &INPUT[4..])
153157
}

substrate/frame/revive/fixtures/contracts/create1_with_value.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ pub extern "C" fn call() {
3434
api::value_transferred(&mut value);
3535

3636
// Deploy the contract with no salt (equivalent to create1).
37-
let ret = api::instantiate(
38-
code_hash,
39-
u64::MAX,
40-
u64::MAX,
41-
&[u8::MAX; 32],
42-
&value,
43-
&[],
44-
None,
45-
None,
46-
None
47-
);
48-
assert!(ret.is_ok());
37+
api::instantiate(u64::MAX, u64::MAX, &[u8::MAX; 32], &value, code_hash, None, None, None)
38+
.unwrap();
4939
}

substrate/frame/revive/fixtures/contracts/create_storage_and_instantiate.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,24 @@ pub extern "C" fn deploy() {}
3030
#[polkavm_derive::polkavm_export]
3131
pub extern "C" fn call() {
3232
input!(
33-
input: [u8; 4],
3433
code_hash: &[u8; 32],
34+
input: [u8; 4],
3535
deposit_limit: &[u8; 32],
3636
);
3737

3838
let value = u256_bytes(10_000u64);
3939
let salt = [0u8; 32];
4040
let mut address = [0u8; 20];
41+
let mut deploy_input = [0; 32 + 4];
42+
deploy_input[..32].copy_from_slice(code_hash);
43+
deploy_input[32..].copy_from_slice(&input);
4144

4245
let ret = api::instantiate(
43-
code_hash,
4446
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
4547
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
4648
deposit_limit,
4749
&value,
48-
input,
50+
&deploy_input,
4951
Some(&mut address),
5052
None,
5153
Some(&salt),

substrate/frame/revive/fixtures/contracts/destroy_and_transfer.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ const VALUE: [u8; 32] = u256_bytes(65536);
2929
pub extern "C" fn deploy() {
3030
input!(code_hash: &[u8; 32],);
3131

32-
let input = [0u8; 0];
3332
let mut address = [0u8; 20];
3433
let salt = [47u8; 32];
3534

3635
api::instantiate(
37-
code_hash,
3836
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
3937
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
4038
&[u8::MAX; 32], // No deposit limit.
4139
&VALUE,
42-
&input,
40+
code_hash,
4341
Some(&mut address),
4442
None,
4543
Some(&salt),

substrate/frame/revive/fixtures/contracts/instantiate_return_code.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ pub extern "C" fn deploy() {}
2828
#[no_mangle]
2929
#[polkavm_derive::polkavm_export]
3030
pub extern "C" fn call() {
31-
input!(buffer, 36, code_hash: &[u8; 32],);
32-
let input = &buffer[32..];
31+
input!(buffer: &[u8; 36],);
3332

3433
let err_code = match api::instantiate(
35-
code_hash,
36-
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
37-
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
38-
&[u8::MAX; 32], // No deposit limit.
34+
u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all.
35+
u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all.
36+
&[u8::MAX; 32], // No deposit limit.
3937
&u256_bytes(10_000u64), // Value to transfer.
40-
input,
38+
buffer,
4139
None,
4240
None,
4341
Some(&[0u8; 32]), // Salt.

substrate/frame/revive/fixtures/contracts/return_data_api.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ fn assert_balance_transfer_does_reset() {
8888
&[u8::MAX; 32],
8989
&u256_bytes(128),
9090
&[],
91-
None
92-
).unwrap();
91+
None,
92+
)
93+
.unwrap();
9394
assert_return_data_size_of(0);
9495
}
9596

@@ -117,13 +118,16 @@ pub extern "C" fn call() {
117118
input
118119
};
119120
let mut instantiate = |exit_flag| {
121+
let input = construct_input(exit_flag);
122+
let mut deploy_input = [0; 32 + INPUT_BUF_SIZE];
123+
deploy_input[..32].copy_from_slice(code_hash);
124+
deploy_input[32..].copy_from_slice(&input);
120125
api::instantiate(
121-
code_hash,
122126
u64::MAX,
123127
u64::MAX,
124128
&[u8::MAX; 32],
125129
&[0; 32],
126-
&construct_input(exit_flag),
130+
&deploy_input,
127131
Some(&mut address_buf),
128132
None,
129133
None,

substrate/frame/revive/proc-macro/src/lib.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,11 @@ where
355355
{
356356
const ALLOWED_REGISTERS: usize = 6;
357357

358+
// too many arguments
359+
if param_names.clone().count() > ALLOWED_REGISTERS {
360+
panic!("Syscalls take a maximum of {ALLOWED_REGISTERS} arguments");
361+
}
362+
358363
// all of them take one register but we truncate them before passing into the function
359364
// it is important to not allow any type which has illegal bit patterns like 'bool'
360365
if !param_types.clone().all(|ty| {
@@ -369,39 +374,7 @@ where
369374
panic!("Only primitive unsigned integers are allowed as arguments to syscalls");
370375
}
371376

372-
// too many arguments: pass as pointer to a struct in memory
373-
if param_names.clone().count() > ALLOWED_REGISTERS {
374-
let fields = param_names.clone().zip(param_types.clone()).map(|(name, ty)| {
375-
quote! {
376-
#name: #ty,
377-
}
378-
});
379-
return quote! {
380-
#[derive(Default)]
381-
#[repr(C)]
382-
struct Args {
383-
#(#fields)*
384-
}
385-
let Args { #(#param_names,)* } = {
386-
let len = ::core::mem::size_of::<Args>();
387-
let mut args = Args::default();
388-
let ptr = &mut args as *mut Args as *mut u8;
389-
// Safety
390-
// 1. The struct is initialized at all times.
391-
// 2. We only allow primitive integers (no bools) as arguments so every bit pattern is safe.
392-
// 3. The reference doesn't outlive the args field.
393-
// 4. There is only the single reference to the args field.
394-
// 5. The length of the generated slice is the same as the struct.
395-
let reference = unsafe {
396-
::core::slice::from_raw_parts_mut(ptr, len)
397-
};
398-
memory.read_into_buf(__a0__ as _, reference)?;
399-
args
400-
};
401-
}
402-
}
403-
404-
// otherwise: one argument per register
377+
// one argument per register
405378
let bindings = param_names.zip(param_types).enumerate().map(|(idx, (name, ty))| {
406379
let reg = quote::format_ident!("__a{}__", idx);
407380
quote! {
Binary file not shown.

0 commit comments

Comments
 (0)