Line data Source code
1 : #include "fd_system_program.h"
2 : #include "../fd_executor.h"
3 : #include "../fd_borrowed_account.h"
4 : #include "../fd_system_ids.h"
5 : #include "../fd_pubkey_utils.h"
6 : #include "../../log_collector/fd_log_collector.h"
7 :
8 : /* The dynamically sized portion of the system program instruction only
9 : comes from the seed. This means in the worst case assuming that the
10 : seed takes up the entire transaction MTU, the worst case footprint
11 : is the sum of the size of the instruction and the transaction MTU.
12 : This is not the tightest bound, but it's a reasonable bound. */
13 :
14 : #define FD_SYSTEM_PROGRAM_INSTR_FOOTPRINT (FD_TXN_MTU + sizeof(fd_system_program_instruction_t))
15 :
16 : #define FD_FMT_ADDRESS(account_b58, base, out_fmt) \
17 3 : char out_fmt[ 128UL ]; \
18 3 : if( base ) { \
19 0 : FD_BASE58_ENCODE_32_BYTES( base->key, base_b58 ); \
20 0 : snprintf( out_fmt, 128UL, "Address { address: %s, base: Some(%s) }", account_b58, base_b58 ); \
21 3 : } else { \
22 3 : snprintf( out_fmt, 128UL, "Address { address: %s, base: None }", account_b58 ); \
23 3 : }
24 :
25 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L42-L68
26 :
27 : Partial port of system_processor::Address::create, only covering the
28 : case of the "seed" input actually existing. Note that this doesn't
29 : "create" an address, but rather re-derives from PDA inputs and checks
30 : that the result matches some expected value. */
31 :
32 : static int
33 : verify_seed_address( fd_exec_instr_ctx_t * ctx,
34 : fd_pubkey_t const * expected,
35 : fd_pubkey_t const * base,
36 : char const * seed,
37 : ulong seed_sz,
38 0 : fd_pubkey_t const * owner ) {
39 :
40 0 : fd_pubkey_t actual[1];
41 0 : do {
42 0 : int err = fd_pubkey_create_with_seed(
43 0 : ctx,
44 0 : base->uc,
45 0 : seed,
46 0 : seed_sz,
47 0 : owner->uc,
48 0 : actual->uc );
49 0 : if( FD_UNLIKELY( err ) ) return err;
50 0 : } while(0);
51 :
52 0 : if( FD_UNLIKELY( 0!=memcmp( actual->uc, expected->uc, sizeof(fd_pubkey_t) ) ) ) {
53 : /* Log msg_sz can be more or less than 127 bytes */
54 0 : FD_BASE58_ENCODE_32_BYTES( expected->key, expected_b58 );
55 0 : FD_BASE58_ENCODE_32_BYTES( actual->key, actual_b58 );
56 0 : fd_log_collector_printf_inefficient_max_512( ctx,
57 0 : "Create: address %s does not match derived address %s",
58 0 : expected_b58, actual_b58 );
59 0 : ctx->txn_out->err.custom_err = FD_SYSTEM_PROGRAM_ERR_ADDR_WITH_SEED_MISMATCH;
60 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
61 0 : }
62 :
63 0 : return 0;
64 0 : }
65 :
66 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L183
67 : https://github.com/anza-xyz/agave/blob/v2.0.9/programs/system/src/system_processor.rs#L182
68 :
69 : Matches Solana Labs system_processor::transfer_verified */
70 :
71 : static int
72 : fd_system_program_transfer_verified( fd_exec_instr_ctx_t * ctx,
73 : ulong transfer_amount,
74 : ushort from_acct_idx,
75 42 : ushort to_acct_idx ) {
76 42 : int err;
77 :
78 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L191-L192 */
79 :
80 42 : fd_guarded_borrowed_account_t from = {0};
81 42 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, from_acct_idx, &from );
82 :
83 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L193-L196 */
84 :
85 42 : if( fd_borrowed_account_get_data_len( &from ) != 0UL ) {
86 3 : fd_log_collector_msg_literal( ctx, "Transfer: `from` must not carry data" );
87 3 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
88 3 : }
89 :
90 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L197-L205 */
91 :
92 39 : if( transfer_amount > fd_borrowed_account_get_lamports( &from ) ) {
93 : /* Max msg_sz: 45 - 6 + 20 + 20 = 79 < 127 => we can use printf */
94 3 : fd_log_collector_printf_dangerous_max_127( ctx, "Transfer: insufficient lamports %lu, need %lu", fd_borrowed_account_get_lamports( &from ), transfer_amount );
95 3 : ctx->txn_out->err.custom_err = FD_SYSTEM_PROGRAM_ERR_RESULT_WITH_NEGATIVE_LAMPORTS;
96 3 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
97 3 : }
98 :
99 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L207 */
100 :
101 36 : err = fd_borrowed_account_checked_sub_lamports( &from, transfer_amount );
102 : /* Note: this err can never happen because of the check above */
103 36 : if( FD_UNLIKELY( err ) ) return err;
104 :
105 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L208 */
106 :
107 36 : fd_borrowed_account_drop( &from );
108 :
109 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L209-L210 */
110 :
111 36 : fd_guarded_borrowed_account_t to = {0};
112 36 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, to_acct_idx, &to );
113 :
114 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L211 */
115 :
116 36 : err = fd_borrowed_account_checked_add_lamports( &to, transfer_amount );
117 36 : if( FD_UNLIKELY( err ) ) return err;
118 :
119 36 : return 0;
120 36 : }
121 :
122 : /* https://github.com/anza-xyz/agave/blob/v2.0.9/programs/system/src/system_processor.rs#L214
123 :
124 : Matches system_processor::transfer */
125 :
126 : static int
127 : fd_system_program_transfer( fd_exec_instr_ctx_t * ctx,
128 : ulong transfer_amount,
129 : ushort from_acct_idx,
130 45 : ushort to_acct_idx ) {
131 :
132 : /* https://github.com/anza-xyz/agave/blob/v2.0.9/programs/system/src/system_processor.rs#L222-L232 */
133 :
134 45 : int instr_err_code = 0;
135 45 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( ctx->instr, from_acct_idx, &instr_err_code ) ) ) {
136 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
137 3 : if( FD_UNLIKELY( !!instr_err_code ) ) return instr_err_code;
138 : /* Max msg_sz: 37 - 2 + 45 = 80 < 127 => we can use printf */
139 3 : ushort idx_in_txn = ctx->instr->accounts[ from_acct_idx ].index_in_transaction;
140 3 : FD_BASE58_ENCODE_32_BYTES( ctx->txn_out->accounts.keys[ idx_in_txn ].key, key_b58 );
141 3 : fd_log_collector_printf_dangerous_max_127( ctx,
142 3 : "Transfer: `from` account %s must sign", key_b58 );
143 3 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
144 3 : }
145 :
146 : /* https://github.com/anza-xyz/agave/blob/v2.0.9/programs/system/src/system_processor.rs#L234-L241 */
147 :
148 42 : return fd_system_program_transfer_verified( ctx, transfer_amount, from_acct_idx, to_acct_idx );
149 45 : }
150 :
151 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L71-L111
152 : https://github.com/anza-xyz/agave/blob/v2.0.9/programs/system/src/system_processor.rs#L70
153 :
154 : Based on Solana Labs system_processor::allocate() */
155 :
156 : static int
157 : fd_system_program_allocate( fd_exec_instr_ctx_t * ctx,
158 : fd_borrowed_account_t * account,
159 : ulong space,
160 : fd_pubkey_t const * authority,
161 57 : fd_pubkey_t const * base ) {
162 57 : int err;
163 :
164 : /* Assumes that acct_idx was bounds checked */
165 :
166 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L78-L85 */
167 :
168 57 : if( FD_UNLIKELY( !fd_exec_instr_ctx_any_signed( ctx, authority ) ) ) {
169 : /* Max msg_sz: 35 - 2 + 125 = 158 */
170 3 : FD_BASE58_ENCODE_32_BYTES( account->pubkey->key, account_b58 );
171 3 : FD_FMT_ADDRESS( account_b58, base, address_fmt );
172 3 : fd_log_collector_printf_inefficient_max_512( ctx,
173 3 : "Allocate: 'to' account %s must sign",
174 3 : address_fmt );
175 3 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
176 3 : }
177 :
178 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L87-L96 */
179 :
180 54 : if( FD_UNLIKELY( ( fd_borrowed_account_get_data_len( account ) != 0UL ) ||
181 54 : ( 0!=memcmp( fd_borrowed_account_get_owner( account ), fd_solana_system_program_id.uc, 32UL ) ) ) ) {
182 : /* Max msg_sz: 35 - 2 + 125 = 158 */
183 0 : FD_BASE58_ENCODE_32_BYTES( account->pubkey->key, account_b58 );
184 0 : FD_FMT_ADDRESS( account_b58, base, address_fmt );
185 0 : fd_log_collector_printf_inefficient_max_512( ctx,
186 0 : "Allocate: account %s already in use",
187 0 : address_fmt );
188 0 : ctx->txn_out->err.custom_err = FD_SYSTEM_PROGRAM_ERR_ACCT_ALREADY_IN_USE;
189 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
190 0 : }
191 :
192 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L98-L106 */
193 :
194 54 : if( FD_UNLIKELY( space > FD_RUNTIME_ACC_SZ_MAX ) ) {
195 : /* Max msg_sz: 48 - 6 + 2*20 = 82 < 127 => we can use printf */
196 6 : fd_log_collector_printf_dangerous_max_127( ctx,
197 6 : "Allocate: requested %lu, max allowed %lu", space, FD_RUNTIME_ACC_SZ_MAX );
198 6 : ctx->txn_out->err.custom_err = FD_SYSTEM_PROGRAM_ERR_INVALID_ACCT_DATA_LEN;
199 6 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
200 6 : }
201 :
202 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L108 */
203 :
204 48 : err = fd_borrowed_account_set_data_length( account, space );
205 48 : if( FD_UNLIKELY( err ) ) {
206 0 : return err;
207 0 : }
208 :
209 48 : return FD_EXECUTOR_INSTR_SUCCESS;
210 48 : }
211 :
212 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L113-L131
213 : https://github.com/anza-xyz/agave/blob/v2.0.9/programs/system/src/system_processor.rs#L112
214 :
215 : Based on Solana Labs system_processor::assign() */
216 :
217 : static int
218 : fd_system_program_assign( fd_exec_instr_ctx_t * ctx,
219 : fd_borrowed_account_t * account,
220 : fd_pubkey_t const * owner,
221 : fd_pubkey_t const * authority,
222 27 : fd_pubkey_t const * base ) {
223 : /* Assumes addr_idx was bounds checked */
224 :
225 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L121-L123 */
226 :
227 27 : if( 0==memcmp( fd_borrowed_account_get_owner( account ), owner->uc, sizeof(fd_pubkey_t) ) )
228 3 : return 0;
229 :
230 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L125-L128 */
231 :
232 24 : if( FD_UNLIKELY( !fd_exec_instr_ctx_any_signed( ctx, authority ) ) ) {
233 : /* Max msg_sz: 28 - 2 + 125 = 151 */
234 0 : FD_BASE58_ENCODE_32_BYTES( account->pubkey->key, account_b58 );
235 0 : FD_FMT_ADDRESS( account_b58, base, address_fmt );
236 0 : fd_log_collector_printf_inefficient_max_512( ctx,
237 0 : "Assign: account %s must sign",
238 0 : address_fmt );
239 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
240 0 : }
241 :
242 24 : return fd_borrowed_account_set_owner( account, owner );
243 24 : }
244 :
245 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L133-L143
246 :
247 : Based on Solana Labs system_processor::allocate_and_assign() */
248 :
249 : static int
250 : fd_system_program_allocate_and_assign( fd_exec_instr_ctx_t * ctx,
251 : fd_borrowed_account_t * account,
252 : ulong space,
253 : fd_pubkey_t const * owner,
254 : fd_pubkey_t const * authority,
255 33 : fd_pubkey_t const * base ) {
256 :
257 33 : do {
258 33 : int err = fd_system_program_allocate( ctx, account, space, authority, base );
259 33 : if( FD_UNLIKELY( err ) ) return err;
260 33 : } while(0);
261 27 : return fd_system_program_assign( ctx, account, owner, authority, base );
262 :
263 33 : }
264 :
265 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L146-L181
266 : https://github.com/anza-xyz/agave/blob/v2.0.9/programs/system/src/system_processor.rs#L145
267 :
268 : Matches Solana Labs system_processor::create_account() */
269 :
270 : static int
271 : fd_system_program_create_account( fd_exec_instr_ctx_t * ctx,
272 : ushort from_acct_idx,
273 : ushort to_acct_idx,
274 : ulong lamports,
275 : ulong space,
276 : fd_pubkey_t const * owner,
277 : fd_pubkey_t const * authority,
278 0 : fd_pubkey_t const * base ) {
279 0 : int err;
280 :
281 : /* if it looks like the to account is already in use, bail
282 : https://github.com/anza-xyz/agave/blob/v2.1.14/programs/system/src/system_processor.rs#L159-L172 */
283 :
284 0 : do {
285 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L160-L161 */
286 :
287 0 : fd_guarded_borrowed_account_t to = {0};
288 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, to_acct_idx, &to );
289 :
290 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L162-L169 */
291 :
292 0 : if( FD_UNLIKELY( fd_borrowed_account_get_lamports( &to ) ) ) {
293 : /* Max msg_sz: 41 - 2 + 125 = 164 */
294 0 : FD_BASE58_ENCODE_32_BYTES( to.pubkey->key, to_b58 );
295 0 : FD_FMT_ADDRESS( to_b58, base, address_fmt );
296 0 : fd_log_collector_printf_inefficient_max_512( ctx,
297 0 : "Create Account: account %s already in use",
298 0 : address_fmt );
299 0 : ctx->txn_out->err.custom_err = FD_SYSTEM_PROGRAM_ERR_ACCT_ALREADY_IN_USE;
300 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
301 0 : }
302 :
303 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L171 */
304 :
305 0 : err = fd_system_program_allocate_and_assign( ctx, &to, space, owner, authority, base );
306 0 : if( FD_UNLIKELY( err ) ) return err;
307 :
308 : /* Implicit drop
309 : https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L172 */
310 0 : } while (0);
311 :
312 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L173-L180 */
313 :
314 0 : return fd_system_program_transfer( ctx, lamports, from_acct_idx, to_acct_idx );
315 0 : }
316 :
317 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.2/programs/system/src/system_processor.rs#L184-L214 */
318 :
319 : static int
320 : fd_system_program_create_account_allow_prefund( fd_exec_instr_ctx_t * ctx,
321 : ushort to_acct_idx,
322 : ulong lamports,
323 : ulong space,
324 : fd_pubkey_t const * owner,
325 : fd_pubkey_t const * to_address,
326 33 : ushort from_acct_idx ) {
327 33 : int err;
328 :
329 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.2/programs/system/src/system_processor.rs#L198-L201 */
330 33 : do {
331 33 : fd_guarded_borrowed_account_t to = {0};
332 33 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, to_acct_idx, &to );
333 33 : err = fd_system_program_allocate_and_assign( ctx, &to, space, owner, to_address, NULL );
334 33 : if( FD_UNLIKELY( err ) ) return err;
335 33 : } while(0);
336 :
337 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.2/programs/system/src/system_processor.rs#L202-L212 */
338 27 : if( FD_LIKELY( lamports > 0 ) ) {
339 21 : return fd_system_program_transfer( ctx, lamports, from_acct_idx, to_acct_idx );
340 21 : }
341 :
342 6 : return FD_EXECUTOR_INSTR_SUCCESS;
343 27 : }
344 :
345 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L327-L352
346 :
347 : Matches Solana Labs system_processor SystemInstruction::CreateAccount { ... } => { ... } */
348 :
349 : int
350 : fd_system_program_exec_create_account( fd_exec_instr_ctx_t * ctx,
351 0 : fd_system_program_instruction_create_account_t const * create_acc ) {
352 :
353 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L332 */
354 :
355 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 2 ) )
356 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
357 :
358 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L333-L339
359 : Authorization check is lifted out from 'allocate' to here. */
360 :
361 0 : ushort const from_acct_idx = 0UL;
362 0 : ushort const to_acct_idx = 1UL;
363 :
364 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/system/src/system_processor.rs#L317-L320 */
365 0 : fd_pubkey_t const * authority = NULL;
366 0 : int err = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, to_acct_idx, &authority );
367 0 : if( FD_UNLIKELY( err ) ) return err;
368 :
369 0 : return fd_system_program_create_account(
370 0 : ctx,
371 0 : from_acct_idx,
372 0 : to_acct_idx,
373 0 : create_acc->lamports,
374 0 : create_acc->space,
375 0 : &create_acc->owner,
376 0 : authority,
377 0 : NULL );
378 0 : }
379 :
380 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.2/programs/system/src/system_processor.rs#L530-L563 */
381 :
382 : int
383 : fd_system_program_exec_create_account_allow_prefund( fd_exec_instr_ctx_t * ctx,
384 39 : fd_system_program_instruction_create_account_t const * args ) {
385 :
386 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.2/programs/system/src/system_processor.rs#L535-L540 */
387 39 : if( FD_UNLIKELY( !FD_FEATURE_ACTIVE_BANK( ctx->bank, create_account_allow_prefund ) ) ) {
388 3 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
389 3 : }
390 :
391 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.2/programs/system/src/system_processor.rs#L541-L547 */
392 36 : ushort from_acct_idx = 0;
393 36 : if( args->lamports > 0 ) {
394 30 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( ctx, 2U ) ) ) {
395 3 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
396 3 : }
397 27 : from_acct_idx = 1;
398 27 : } else {
399 6 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( ctx, 1U ) ) ) {
400 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
401 0 : }
402 6 : }
403 :
404 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.2/programs/system/src/system_processor.rs#L548-L552 */
405 33 : ushort const to_acct_idx = 0;
406 33 : fd_pubkey_t const * to_address = NULL;
407 33 : int err = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, to_acct_idx, &to_address );
408 33 : if( FD_UNLIKELY( err ) ) return err;
409 :
410 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.2/programs/system/src/system_processor.rs#L553-L562 */
411 33 : return fd_system_program_create_account_allow_prefund(
412 33 : ctx, to_acct_idx, args->lamports, args->space, &args->owner, to_address, from_acct_idx );
413 33 : }
414 :
415 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L381-L393
416 :
417 : Matches Solana Labs system_processor SystemInstruction::Assign { ... } => { ... } */
418 :
419 : int
420 : fd_system_program_exec_assign( fd_exec_instr_ctx_t * ctx,
421 0 : fd_pubkey_t const * owner ) {
422 0 : int err;
423 :
424 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L382 */
425 :
426 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 1 ) )
427 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
428 :
429 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L383-L384 */
430 :
431 0 : fd_guarded_borrowed_account_t account = {0};
432 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, 0, &account );
433 :
434 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L385-L391
435 : system_processor::Address::create eliminated (dead code) */
436 :
437 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L392 */
438 :
439 0 : err = fd_system_program_assign( ctx, &account, owner, account.pubkey, NULL );
440 0 : if( FD_UNLIKELY( err ) ) return err;
441 :
442 : /* Implicit drop */
443 :
444 0 : return 0;
445 0 : }
446 :
447 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L394-L404
448 :
449 : Matches Solana Labs system_processor SystemInstruction::Transfer { ... } => { ... } */
450 :
451 : int
452 : fd_system_program_exec_transfer( fd_exec_instr_ctx_t * ctx,
453 24 : ulong transfer_amount ) {
454 :
455 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L395 */
456 :
457 24 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 2 ) )
458 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
459 :
460 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L396-L402 */
461 :
462 24 : return fd_system_program_transfer( ctx, transfer_amount, 0UL, 1UL );
463 24 : }
464 :
465 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L353
466 :
467 : Matches Solana Labs system_processor SystemInstruction::CreateAccountWithSeed { ... } => { ... } */
468 :
469 : int
470 : fd_system_program_exec_create_account_with_seed( fd_exec_instr_ctx_t * ctx,
471 0 : fd_system_program_instruction_create_account_with_seed_t const * args ) {
472 :
473 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L360 */
474 :
475 0 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( ctx, 2UL) ) )
476 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
477 :
478 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L361-L367 */
479 :
480 0 : fd_pubkey_t const * to_address = NULL;
481 0 : int err = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, 1UL, &to_address );
482 0 : if( FD_UNLIKELY( err ) ) return err;
483 :
484 0 : do {
485 0 : int err = verify_seed_address(
486 0 : ctx,
487 0 : to_address,
488 0 : &args->base,
489 0 : (char const *)args->seed,
490 0 : args->seed_len,
491 0 : &args->owner );
492 0 : if( FD_UNLIKELY( err ) ) return err;
493 0 : } while(0);
494 :
495 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L368-L379 */
496 :
497 0 : ushort const from_acct_idx = 0UL;
498 0 : ushort const to_acct_idx = 1UL;
499 0 : return fd_system_program_create_account(
500 0 : ctx,
501 0 : from_acct_idx,
502 0 : to_acct_idx,
503 0 : args->lamports,
504 0 : args->space,
505 0 : &args->owner,
506 0 : &args->base,
507 0 : &args->base );
508 0 : }
509 :
510 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L504-L516
511 :
512 : Matches Solana Labs system_processor SystemInstruction::Allocate { ... } => { ... } */
513 :
514 : int
515 : fd_system_program_exec_allocate( fd_exec_instr_ctx_t * ctx,
516 24 : ulong space ) {
517 24 : int err;
518 :
519 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L505 */
520 :
521 24 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 1 ) )
522 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
523 :
524 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L506-L507 */
525 24 : fd_guarded_borrowed_account_t account = {0};
526 24 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, 0, &account );
527 :
528 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L508-L514
529 : system_processor::Address::create eliminated (dead code) */
530 :
531 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L515
532 : Authorization check is lifted out from 'allocate' to here. */
533 :
534 24 : err = fd_system_program_allocate( ctx, &account, space, account.pubkey, NULL );
535 24 : if( FD_UNLIKELY( err ) ) return err;
536 :
537 : /* Implicit drop */
538 :
539 21 : return 0;
540 24 : }
541 :
542 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L517-L541
543 :
544 : Matches Solana Labs system_processor SystemInstruction::AllocateWithSeed { ... } => { ... } */
545 :
546 : int
547 : fd_system_program_exec_allocate_with_seed( fd_exec_instr_ctx_t * ctx,
548 0 : fd_system_program_instruction_allocate_with_seed_t const * args ) {
549 0 : int err;
550 :
551 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L523 */
552 :
553 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 1 ) )
554 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
555 :
556 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#524-525 */
557 :
558 0 : fd_guarded_borrowed_account_t account = {0};
559 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, 0, &account );
560 :
561 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L526-L532 */
562 :
563 0 : err = verify_seed_address(
564 0 : ctx,
565 0 : account.pubkey,
566 0 : &args->base,
567 0 : (char const *)args->seed,
568 0 : args->seed_len,
569 0 : &args->owner );
570 0 : if( FD_UNLIKELY( err ) ) return err;
571 :
572 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L533-L540
573 : Authorization check is lifted out from 'allocate' to here. */
574 :
575 0 : err = fd_system_program_allocate_and_assign(
576 0 : ctx,
577 0 : &account,
578 0 : args->space,
579 0 : &args->owner,
580 0 : &args->base,
581 0 : &args->base );
582 0 : if( FD_UNLIKELY( err ) ) return err;
583 :
584 : /* Implicit drop */
585 :
586 0 : return 0;
587 0 : }
588 :
589 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L542-L554
590 :
591 : Matches Solana Labs system_processor SystemInstruction::AssignWithSeed { ... } => { ... } */
592 :
593 : int
594 : fd_system_program_exec_assign_with_seed( fd_exec_instr_ctx_t * ctx,
595 0 : fd_system_program_instruction_assign_with_seed_t const * args ) {
596 0 : int err;
597 :
598 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#543 */
599 :
600 0 : if( FD_UNLIKELY( ctx->instr->acct_cnt < 1 ) )
601 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
602 :
603 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L544-L545 */
604 :
605 0 : fd_guarded_borrowed_account_t account = {0};
606 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( ctx, 0, &account );
607 :
608 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L546-L552 */
609 :
610 0 : err = verify_seed_address(
611 0 : ctx,
612 0 : account.pubkey,
613 0 : &args->base,
614 0 : (char const *)args->seed,
615 0 : args->seed_len,
616 0 : &args->owner );
617 0 : if( FD_UNLIKELY( err ) ) return err;
618 :
619 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L553
620 : Authorization check is lifted out from 'assign' to here. */
621 :
622 0 : err = fd_system_program_assign( ctx, &account, &args->owner, &args->base, &args->base );
623 0 : if( FD_UNLIKELY( err ) ) return err;
624 :
625 : /* Implicit drop */
626 :
627 0 : return 0;
628 0 : }
629 :
630 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L405-L422
631 :
632 : Matches Solana Labs system_processor SystemInstruction::TransferWithSeed { ... } => { ... } */
633 :
634 : int
635 : fd_system_program_exec_transfer_with_seed( fd_exec_instr_ctx_t * ctx,
636 0 : fd_system_program_instruction_transfer_with_seed_t const * args ) {
637 :
638 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L410 */
639 :
640 0 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( ctx, 3UL ) ) )
641 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
642 :
643 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L411-L421
644 : Inlined call to system_processor::transfer_with_seed */
645 :
646 0 : ushort const from_idx = 0UL;
647 0 : ushort const from_base_idx = 1UL;
648 0 : ushort const to_idx = 2UL;
649 :
650 0 : int instr_err_code = 0;
651 0 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( ctx->instr, from_base_idx, &instr_err_code ) ) ) {
652 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
653 0 : if( FD_UNLIKELY( !!instr_err_code ) ) return instr_err_code;
654 : /* Max msg_sz: 37 - 2 + 45 = 80 < 127 => we can use printf */
655 0 : ushort idx_in_txn = ctx->instr->accounts[ from_base_idx ].index_in_transaction;
656 0 : FD_BASE58_ENCODE_32_BYTES( ctx->txn_out->accounts.keys[ idx_in_txn ].key, key_b58 );
657 0 : fd_log_collector_printf_dangerous_max_127( ctx,
658 0 : "Transfer: 'from' account %s must sign", key_b58 );
659 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
660 0 : }
661 :
662 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/programs/system/src/system_processor.rs#L267-L274 */
663 :
664 0 : fd_pubkey_t const * base = NULL;
665 0 : int err = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, from_base_idx, &base );
666 0 : if( FD_UNLIKELY( err ) ) return err;
667 :
668 0 : fd_pubkey_t address_from_seed[1];
669 0 : do {
670 0 : int err = fd_pubkey_create_with_seed(
671 0 : ctx,
672 0 : base->uc,
673 0 : (char const *)args->from_seed,
674 0 : args->from_seed_len,
675 0 : args->from_owner.uc,
676 0 : address_from_seed->uc );
677 0 : if( FD_UNLIKELY( err ) ) return err;
678 0 : } while(0);
679 :
680 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/programs/system/src/system_processor.rs#L276-L287 */
681 0 : fd_pubkey_t const * from_key = NULL;
682 0 : err = fd_exec_instr_ctx_get_key_of_account_at_index( ctx, from_idx, &from_key );
683 0 : if( FD_UNLIKELY( err ) ) return err;
684 :
685 0 : if( FD_UNLIKELY( 0!=memcmp( address_from_seed->uc,
686 0 : from_key->uc,
687 0 : sizeof(fd_pubkey_t) ) ) ) {
688 : /* Log msg_sz can be more or less than 127 bytes */
689 0 : FD_BASE58_ENCODE_32_BYTES( from_key->key, from_key_b58 );
690 0 : FD_BASE58_ENCODE_32_BYTES( address_from_seed->key, address_from_seed_b58 );
691 0 : fd_log_collector_printf_inefficient_max_512( ctx,
692 0 : "Transfer: 'from' address %s does not match derived address %s",
693 0 : from_key_b58,
694 0 : address_from_seed_b58 );
695 0 : ctx->txn_out->err.custom_err = FD_SYSTEM_PROGRAM_ERR_ADDR_WITH_SEED_MISMATCH;
696 0 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
697 0 : }
698 :
699 : /* https://github.com/solana-labs/solana/blob/v1.17.22/programs/system/src/system_processor.rs#L305-L312 */
700 0 : return fd_system_program_transfer_verified( ctx, args->lamports, from_idx, to_idx );
701 0 : }
702 :
703 : int
704 87 : fd_system_program_execute( fd_exec_instr_ctx_t * ctx ) {
705 87 : FD_EXEC_CU_UPDATE( ctx, 150UL );
706 87 : uchar instr_mem[ FD_SYSTEM_PROGRAM_INSTR_FOOTPRINT ] __attribute__((aligned(alignof(fd_system_program_instruction_t))));
707 :
708 87 : fd_system_program_instruction_t * instruction = fd_bincode_decode_static_limited_deserialize(
709 87 : system_program_instruction,
710 87 : instr_mem,
711 87 : ctx->instr->data,
712 87 : ctx->instr->data_sz,
713 87 : FD_TXN_MTU
714 87 : );
715 87 : if( FD_UNLIKELY( !instruction ) ) {
716 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
717 0 : }
718 :
719 87 : int result = FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
720 :
721 87 : switch( instruction->discriminant ) {
722 0 : case fd_system_program_instruction_enum_create_account: {
723 0 : result = fd_system_program_exec_create_account(
724 0 : ctx, &instruction->inner.create_account );
725 0 : break;
726 0 : }
727 0 : case fd_system_program_instruction_enum_assign: {
728 0 : result = fd_system_program_exec_assign(
729 0 : ctx, &instruction->inner.assign );
730 0 : break;
731 0 : }
732 24 : case fd_system_program_instruction_enum_transfer: {
733 24 : result = fd_system_program_exec_transfer(
734 24 : ctx, instruction->inner.transfer );
735 24 : break;
736 0 : }
737 0 : case fd_system_program_instruction_enum_create_account_with_seed: {
738 0 : result = fd_system_program_exec_create_account_with_seed(
739 0 : ctx, &instruction->inner.create_account_with_seed );
740 0 : break;
741 0 : }
742 0 : case fd_system_program_instruction_enum_advance_nonce_account: {
743 0 : result = fd_system_program_exec_advance_nonce_account( ctx );
744 0 : break;
745 0 : }
746 0 : case fd_system_program_instruction_enum_withdraw_nonce_account: {
747 0 : result = fd_system_program_exec_withdraw_nonce_account(
748 0 : ctx, instruction->inner.withdraw_nonce_account );
749 0 : break;
750 0 : }
751 0 : case fd_system_program_instruction_enum_initialize_nonce_account: {
752 0 : result = fd_system_program_exec_initialize_nonce_account(
753 0 : ctx, &instruction->inner.initialize_nonce_account );
754 0 : break;
755 0 : }
756 0 : case fd_system_program_instruction_enum_authorize_nonce_account: {
757 0 : result = fd_system_program_exec_authorize_nonce_account(
758 0 : ctx, &instruction->inner.authorize_nonce_account );
759 0 : break;
760 0 : }
761 24 : case fd_system_program_instruction_enum_allocate: {
762 24 : result = fd_system_program_exec_allocate( ctx, instruction->inner.allocate );
763 24 : break;
764 0 : }
765 0 : case fd_system_program_instruction_enum_allocate_with_seed: {
766 : // https://github.com/solana-labs/solana/blob/b00d18cec4011bb452e3fe87a3412a3f0146942e/runtime/src/system_instruction_processor.rs#L525
767 0 : result = fd_system_program_exec_allocate_with_seed(
768 0 : ctx, &instruction->inner.allocate_with_seed );
769 0 : break;
770 0 : }
771 0 : case fd_system_program_instruction_enum_assign_with_seed: {
772 : // https://github.com/solana-labs/solana/blob/b00d18cec4011bb452e3fe87a3412a3f0146942e/runtime/src/system_instruction_processor.rs#L545
773 0 : result = fd_system_program_exec_assign_with_seed(
774 0 : ctx, &instruction->inner.assign_with_seed );
775 0 : break;
776 0 : }
777 0 : case fd_system_program_instruction_enum_transfer_with_seed: {
778 : // https://github.com/solana-labs/solana/blob/b00d18cec4011bb452e3fe87a3412a3f0146942e/runtime/src/system_instruction_processor.rs#L412
779 0 : result = fd_system_program_exec_transfer_with_seed(
780 0 : ctx, &instruction->inner.transfer_with_seed );
781 0 : break;
782 0 : }
783 0 : case fd_system_program_instruction_enum_upgrade_nonce_account: {
784 : // https://github.com/solana-labs/solana/blob/b00d18cec4011bb452e3fe87a3412a3f0146942e/runtime/src/system_instruction_processor.rs#L491
785 0 : result = fd_system_program_exec_upgrade_nonce_account( ctx );
786 0 : break;
787 0 : }
788 39 : case fd_system_program_instruction_enum_create_account_allow_prefund: {
789 : // https://github.com/anza-xyz/agave/blob/v4.0.0-beta.2/programs/system/src/system_processor.rs#L530-L563
790 39 : result = fd_system_program_exec_create_account_allow_prefund(
791 39 : ctx, &instruction->inner.create_account_allow_prefund );
792 39 : break;
793 0 : }
794 87 : }
795 :
796 87 : return result;
797 87 : }
798 :
799 : /**********************************************************************/
800 : /* Public API */
801 : /**********************************************************************/
802 :
803 : int
804 141 : fd_get_system_account_kind( fd_account_meta_t const * meta ) {
805 : /* https://github.com/anza-xyz/solana-sdk/blob/nonce-account%40v2.2.1/nonce-account/src/lib.rs#L56 */
806 141 : if( FD_UNLIKELY( memcmp( meta->owner, fd_solana_system_program_id.uc, sizeof(fd_pubkey_t) ) ) ) {
807 6 : return FD_SYSTEM_PROGRAM_NONCE_ACCOUNT_KIND_UNKNOWN;
808 6 : }
809 :
810 : /* https://github.com/anza-xyz/solana-sdk/blob/nonce-account%40v2.2.1/nonce-account/src/lib.rs#L57-L58 */
811 135 : if( FD_LIKELY( !meta->dlen ) ) {
812 129 : return FD_SYSTEM_PROGRAM_NONCE_ACCOUNT_KIND_SYSTEM;
813 129 : }
814 :
815 : /* https://github.com/anza-xyz/solana-sdk/blob/nonce-account%40v2.2.1/nonce-account/src/lib.rs#L59 */
816 6 : if( FD_UNLIKELY( meta->dlen!=FD_SYSTEM_PROGRAM_NONCE_DLEN ) ) {
817 6 : return FD_SYSTEM_PROGRAM_NONCE_ACCOUNT_KIND_UNKNOWN;
818 6 : }
819 :
820 : /* https://github.com/anza-xyz/solana-sdk/blob/nonce-account%40v2.2.1/nonce-account/src/lib.rs#L60-L64 */
821 0 : fd_nonce_state_versions_t versions[1];
822 0 : if( FD_UNLIKELY( !fd_bincode_decode_static(
823 0 : nonce_state_versions, versions,
824 0 : fd_account_data( meta ),
825 0 : meta->dlen ) ) ) {
826 0 : return FD_SYSTEM_PROGRAM_NONCE_ACCOUNT_KIND_UNKNOWN;
827 0 : }
828 :
829 0 : fd_nonce_state_t * state = NULL;
830 0 : if( fd_nonce_state_versions_is_current( versions ) ) {
831 0 : state = &versions->inner.current;
832 0 : } else {
833 0 : state = &versions->inner.legacy;
834 0 : }
835 :
836 0 : if( FD_LIKELY( fd_nonce_state_is_initialized( state ) ) ) {
837 0 : return FD_SYSTEM_PROGRAM_NONCE_ACCOUNT_KIND_NONCE;
838 0 : }
839 :
840 0 : return FD_SYSTEM_PROGRAM_NONCE_ACCOUNT_KIND_UNKNOWN;
841 0 : }
|