|
| 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: 2024-10-01 |
| 10 | +feature: TBD |
| 11 | +extends: SIMD-0184, SIMD-0185 |
| 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 | +and the length of the scratchpad. |
| 61 | + |
| 62 | +- The number of transaction accounts: `u64` |
| 63 | +- Key of the program which wrote to the scratchpad most recently: `[u8; 32]` |
| 64 | +- The scratchpad data: `&[u8]` which is composed of: |
| 65 | + - Pointer to scratchpad data: `u64` |
| 66 | + - Length of scratchpad data: `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 | + - // TODO: Account payload capacity: `u64` |
| 75 | + |
| 76 | +This memory region starts at `0x500000000` and is readonly. It must be |
| 77 | +updated as instructions through out the transaction call TODO. |
| 78 | + |
| 79 | +### Per Instruction Serialization |
| 80 | + |
| 81 | +For each instruction the program runtime must prepare the following. |
| 82 | +This memory region starts at `0x600000000` and is readonly. It does not require |
| 83 | +any updates once serialized. |
| 84 | + |
| 85 | +- Magic: `u32`: `0x76494241` ("ABIv" encoded in ASCII) |
| 86 | +- ABI version `u32`: `0x00000002` |
| 87 | +- // TODO: Instruction trace to replace get sibling instructions syscall? |
| 88 | +- Program key: `[u8; 32]` |
| 89 | +- The instruction data: `&[u8]` which is composed of: |
| 90 | + - Pointer to instruction data: `u64` |
| 91 | + - Length of instruction data: `u64` |
| 92 | +- Number of instruction accounts: `u16` |
| 93 | +- For each instruction account (non interleaved): |
| 94 | + - Index to transaction account: `u16` |
| 95 | + - Index of first occurence (for aliasing): `u16` |
| 96 | + - Flags bitfield: `u8` (bit 8 is signer, bit 16 is writable) |
| 97 | + |
| 98 | +### Per Instruction Mappings |
| 99 | + |
| 100 | +A readonly memory region starting at `0x700000000` must be mapped |
| 101 | +in for the instruction data. It too does not require any updates. |
| 102 | + |
| 103 | +For each unique (meaning deduplicated) instruction account the payload must |
| 104 | +be mapped in at `0x800000000` plus `0x100000000` times the index of the |
| 105 | +**transaction** account (not the index of the instruction account). Only if the |
| 106 | +instruction account has the writable flag set and is owned by the current |
| 107 | +program it is mapped in as a writable region. The writability of a region must |
| 108 | +be updated as programs through out the transaction modify the account metadata. |
| 109 | + |
| 110 | +### Lazy deserialization on the dApp side (inside the SDK) |
| 111 | + |
| 112 | +With this design a program SDK can (but no longer needs to) eagerly deserialize |
| 113 | +all account metadata at the entrypoint. Because this layout is strictly aligned |
| 114 | +and uses proper arrays, it is possible to directly calculate the offset of a |
| 115 | +single accounts metadata with only one indirect lookup and no need to scan all |
| 116 | +preceeding metadata. This allows a program SDK to offer a lazy interface which |
| 117 | +only interacts with the account metadata fields which are needed, only of the |
| 118 | +accounts which are of interest and only when necessary. |
| 119 | + |
| 120 | +### Changes to syscalls |
| 121 | + |
| 122 | +The `AccountInfo` parameter of the CPI syscalls will be ignored if ABI v2 is in |
| 123 | +use. Instead the changes to account metadata will be communicated explicitly |
| 124 | +through separate syscalls `set_account_owner`, `set_account_lamports` and |
| 125 | +`set_account_length`. Each of these must take a guest pointer to the structure |
| 126 | +of the transaction account (see per transaction serialization) to be updated and |
| 127 | +the new value as second parameter. In case of the pubkey parameter the guest |
| 128 | +pointer to a 32 byte slice is taken instead. |
| 129 | + |
| 130 | +### Changes to CU metering |
| 131 | + |
| 132 | +CPI will no longer charge CUs for the length of instruction data or account |
| 133 | +payloads. TBD CUs will be charged for every instruction account. |
| 134 | + |
| 135 | +## Impact |
| 136 | + |
| 137 | +This change is expected to drastically reduce the CU costs as the cost will no |
| 138 | +longer depend on the length of the instruction account payloads or instruction |
| 139 | +data. |
| 140 | + |
| 141 | +From the dApp devs perspective almost all changes are hidden inside the SDK. |
| 142 | + |
| 143 | +## Security Considerations |
| 144 | + |
| 145 | +What security implications/considerations come with implementing this feature? |
| 146 | +Are there any implementation-specific guidance or pitfalls? |
| 147 | + |
| 148 | +## Drawbacks |
| 149 | + |
| 150 | +This will require parallel code paths for serialization, deserialization, CPI |
| 151 | +call edges and CPI return edges. All of these will coexist with the exisiting |
| 152 | +ABI v0 and v1 for the forseeable future, until we decide to deprecate them. |
| 153 | + |
| 154 | +## Backwards Compatibility |
| 155 | + |
| 156 | +The magic field (`u32`) and version field (`u32`) of ABI v2 are placed at the |
| 157 | +beginning, where ABI v0 and v1 would otherwise indicate the number of |
| 158 | +instruction accounts as an `u64`. Because the older ABIs will never serialize |
| 159 | +more than a few hundred accounts, it is possible to differentiate the ABI |
| 160 | +that way without breaking the older layouts. |
0 commit comments