Skip to content

Commit e3e3e19

Browse files
committed
First draft
1 parent 4b1eaaf commit e3e3e19

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
simd: '0177'
3+
title: Program Runtime ABI v2
4+
authors:
5+
- Alexander Meißner
6+
category: Standard
7+
type: Core
8+
status: Idea
9+
created: 2025-02-23
10+
feature: TBD
11+
extends: SIMD-0219
12+
---
13+
14+
## Summary
15+
16+
Align the layout of the virtual address space to large pages in order to
17+
simplify the address translation logic and allow for easy direct mapping.
18+
19+
## Motivation
20+
21+
At the moment all validator implementations have to copy (and compare) data in
22+
and out of the virtual memory of the virtual machine. There are four possible
23+
account data copy paths:
24+
25+
- Serialization: Copy from program runtime (host) to virtual machine (guest)
26+
- CPI call edge: Copy from virtual machine (guest) to program runtime (host)
27+
- CPI return edge: Copy from program runtime (host) to virtual machine (guest)
28+
- Deserialization: Copy from virtual machine (guest) to program runtime (host)
29+
30+
To avoid this a feature named "direct mapping" was designed which uses the
31+
address translation logic of the virtual machine to emulate the serialization
32+
and deserialization without actually performing copies.
33+
34+
Implementing direct mapping in the current ABI v0 and v1 is very complex
35+
because of unaligned virtual memory regions and memory accesses overlapping
36+
multiple virtual memory regions. Instead the layout of the virtual address
37+
space should be adjusted so that all virtual memory regions are aligned to
38+
4 GiB.
39+
40+
## Alternatives Considered
41+
42+
None.
43+
44+
## New Terminology
45+
46+
None.
47+
48+
## Detailed Design
49+
50+
Programs signal their support through their SBPF version field being v4 or
51+
above while the program runtime signals which ABI is chosen through the
52+
serialized magic field.
53+
54+
### Per Transaction Serialization
55+
56+
At the beginning of a transaction the program runtime must prepare the
57+
following which is shared by all instructions running programs suporting the
58+
new ABI. This memory region starts at `0x400000000` and is readonly. It must be
59+
updated as instructions through out the transaction modify the account metadata
60+
or the scratchpad via `sol_set_return_data`.
61+
62+
- Key of the program which wrote to the scratchpad most recently: `[u8; 32]`
63+
- The scratchpad data: `&[u8]` which is composed of:
64+
- Pointer to scratchpad data: `u64`
65+
- Length of scratchpad data: `u64`
66+
- The number of transaction accounts: `u64`
67+
- For each transaction account:
68+
- Key: `[u8; 32]`
69+
- Owner: `[u8; 32]`
70+
- Lamports: `u64`
71+
- Account payload: `&[u8]` which is composed of:
72+
- Pointer to account payload: `u64`
73+
- Account payload length: `u64`
74+
75+
A readonly memory region starting at `0x500000000` must be mapped in for the
76+
scratchpad data. It must be updated when `sol_set_return_data` is called.
77+
78+
### Per Instruction Serialization
79+
80+
For each instruction the program runtime must prepare the following.
81+
This memory region starts at `0x600000000` and is readonly. It does not require
82+
any updates once serialized.
83+
84+
- The instruction data: `&[u8]` which is composed of:
85+
- Pointer to instruction data: `u64`
86+
- Length of instruction data: `u64`
87+
- Programm account index in transaction: `u16`
88+
- Number of instruction accounts: `u16`
89+
- For each instruction account:
90+
- Index to transaction account: `u16`
91+
- Flags bitfield: `u16` (bit 0 is signer, bit 1 is writable)
92+
93+
### Per Instruction Mappings
94+
95+
A readonly memory region starting at `0x700000000` must be mapped
96+
in for the instruction data. It too does not require any updates.
97+
98+
For each unique (meaning deduplicated) instruction account the payload must
99+
be mapped in at `0x800000000` plus `0x100000000` times the index of the
100+
**transaction** account (not the index of the instruction account). Only if the
101+
instruction account has the writable flag set and is owned by the current
102+
program it is mapped in as a writable region. The writability of a region must
103+
be updated as programs through out the transaction modify the account metadata.
104+
105+
### Lazy deserialization on the dApp side (inside the SDK)
106+
107+
With this design a program SDK can (but no longer needs to) eagerly deserialize
108+
all account metadata at the entrypoint. Because this layout is strictly aligned
109+
and uses proper arrays, it is possible to directly calculate the offset of a
110+
single accounts metadata with only one indirect lookup and no need to scan all
111+
preceeding metadata. This allows a program SDK to offer a lazy interface which
112+
only interacts with the account metadata fields which are needed, only of the
113+
accounts which are of interest and only when necessary.
114+
115+
### Changes to syscalls
116+
117+
The `AccountInfo` parameter of the CPI syscalls (`sol_invoke_signed_c` and
118+
`sol_invoke_signed_rust`) will be ignored if ABI v2 is in use. Instead the
119+
changes to account metadata will be communicated explicitly through separate
120+
syscalls `sol_set_account_owner`, `sol_set_account_lamports` and
121+
`sol_set_account_length`. Each of these must take a guest pointer to the
122+
structure of the transaction account (see per transaction serialization) to be
123+
updated and the new value as second parameter. In case of the pubkey parameter
124+
the guest pointer to a 32 byte slice is taken instead.
125+
126+
### Changes to CU metering
127+
128+
CPI will no longer charge CUs for the length of account payloads. Instead TBD
129+
CUs will be charged for every instruction account.
130+
131+
## Impact
132+
133+
This change is expected to drastically reduce the CU costs as the cost will no
134+
longer depend on the length of the instruction account payloads or instruction
135+
data.
136+
137+
From the dApp devs perspective almost all changes are hidden inside the SDK.
138+
139+
## Security Considerations
140+
141+
What security implications/considerations come with implementing this feature?
142+
Are there any implementation-specific guidance or pitfalls?
143+
144+
## Drawbacks
145+
146+
This will require parallel code paths for serialization, deserialization, CPI
147+
call edges and CPI return edges. All of these will coexist with the exisiting
148+
ABI v0 and v1 for the forseeable future, until we decide to deprecate them.
149+
150+
## Backwards Compatibility
151+
152+
The magic field (`u32`) and version field (`u32`) of ABI v2 are placed at the
153+
beginning, where ABI v0 and v1 would otherwise indicate the number of
154+
instruction accounts as an `u64`. Because the older ABIs will never serialize
155+
more than a few hundred accounts, it is possible to differentiate the ABI
156+
that way without breaking the older layouts.

0 commit comments

Comments
 (0)