Line data Source code
1 : #include "fd_bpf_loader_program.h"
2 : #include "fd_system_program.h"
3 :
4 : /* For additional context see https://solana.com/docs/programs/deploying#state-accounts */
5 :
6 : #include "../../progcache/fd_progcache_user.h"
7 : #include "../fd_runtime_helpers.h"
8 : #include "../fd_executor.h"
9 : #include "../tests/fd_dump_pb.h"
10 : #include "../sysvar/fd_sysvar.h"
11 : #include "../fd_pubkey_utils.h"
12 : #include "../fd_borrowed_account.h"
13 : #include "../fd_system_ids.h"
14 : #include "fd_bpf_loader_serialization.h"
15 : #include "fd_builtin_programs.h"
16 : #include "fd_native_cpi.h"
17 :
18 : /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/sdk/program/src/program_error.rs#L290-L335 */
19 : static inline int
20 : program_error_to_instr_error( ulong err,
21 47 : uint * custom_err ) {
22 47 : switch( err ) {
23 2 : case CUSTOM_ZERO:
24 2 : *custom_err = 0;
25 2 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
26 0 : case INVALID_ARGUMENT:
27 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
28 2 : case INVALID_INSTRUCTION_DATA:
29 2 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
30 0 : case INVALID_ACCOUNT_DATA:
31 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
32 0 : case ACCOUNT_DATA_TOO_SMALL:
33 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
34 0 : case INSUFFICIENT_FUNDS:
35 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
36 0 : case INCORRECT_PROGRAM_ID:
37 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
38 0 : case MISSING_REQUIRED_SIGNATURES:
39 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
40 0 : case ACCOUNT_ALREADY_INITIALIZED:
41 0 : return FD_EXECUTOR_INSTR_ERR_ACC_ALREADY_INITIALIZED;
42 0 : case UNINITIALIZED_ACCOUNT:
43 0 : return FD_EXECUTOR_INSTR_ERR_UNINITIALIZED_ACCOUNT;
44 6 : case NOT_ENOUGH_ACCOUNT_KEYS:
45 6 : return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
46 0 : case ACCOUNT_BORROW_FAILED:
47 0 : return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED;
48 0 : case MAX_SEED_LENGTH_EXCEEDED:
49 0 : return FD_EXECUTOR_INSTR_ERR_MAX_SEED_LENGTH_EXCEEDED;
50 0 : case INVALID_SEEDS:
51 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_SEEDS;
52 0 : case BORSH_IO_ERROR:
53 0 : return FD_EXECUTOR_INSTR_ERR_BORSH_IO_ERROR;
54 0 : case ACCOUNT_NOT_RENT_EXEMPT:
55 0 : return FD_EXECUTOR_INSTR_ERR_ACC_NOT_RENT_EXEMPT;
56 0 : case UNSUPPORTED_SYSVAR:
57 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
58 0 : case ILLEGAL_OWNER:
59 0 : return FD_EXECUTOR_INSTR_ERR_ILLEGAL_OWNER;
60 0 : case MAX_ACCOUNTS_DATA_ALLOCATIONS_EXCEEDED:
61 0 : return FD_EXECUTOR_INSTR_ERR_MAX_ACCS_DATA_ALLOCS_EXCEEDED;
62 0 : case INVALID_ACCOUNT_DATA_REALLOC:
63 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
64 0 : case MAX_INSTRUCTION_TRACE_LENGTH_EXCEEDED:
65 0 : return FD_EXECUTOR_INSTR_ERR_MAX_INSN_TRACE_LENS_EXCEEDED;
66 0 : case BUILTIN_PROGRAMS_MUST_CONSUME_COMPUTE_UNITS:
67 0 : return FD_EXECUTOR_INSTR_ERR_BUILTINS_MUST_CONSUME_CUS;
68 1 : case INVALID_ACCOUNT_OWNER:
69 1 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER;
70 0 : case ARITHMETIC_OVERFLOW:
71 0 : return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW;
72 0 : case IMMUTABLE:
73 0 : return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
74 0 : case INCORRECT_AUTHORITY:
75 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
76 36 : default:
77 36 : if( err>>BUILTIN_BIT_SHIFT == 0 ) {
78 36 : *custom_err = (uint)err;
79 36 : return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
80 36 : }
81 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ERR;
82 47 : }
83 47 : }
84 :
85 : /* https://github.com/anza-xyz/agave/blob/9b22f28104ec5fd606e4bb39442a7600b38bb671/programs/bpf_loader/src/lib.rs#L216-L229 */
86 : static ulong
87 3954 : calculate_heap_cost( ulong heap_size, ulong heap_cost ) {
88 7908 : #define KIBIBYTE_MUL_PAGES (1024UL * 32UL)
89 3954 : #define KIBIBYTE_MUL_PAGES_SUB_1 (KIBIBYTE_MUL_PAGES - 1UL)
90 :
91 3954 : heap_size = fd_ulong_sat_add( heap_size, KIBIBYTE_MUL_PAGES_SUB_1 );
92 :
93 3954 : heap_size = fd_ulong_sat_mul( fd_ulong_sat_sub( heap_size / KIBIBYTE_MUL_PAGES, 1UL ), heap_cost );
94 3954 : return heap_size;
95 :
96 3954 : #undef KIBIBYTE_MUL_PAGES
97 3954 : #undef KIBIBYTE_MUL_PAGES_SUB_1
98 3954 : }
99 :
100 : static int
101 : deploy_program( fd_exec_instr_ctx_t * instr_ctx,
102 : uchar const * programdata,
103 : ulong programdata_size,
104 : int disable_sbpf_v0_v1_v2_deployment,
105 250 : fd_bpf_deploy_scratch_t * scratch ) {
106 250 : int deploy_mode = 1;
107 :
108 250 : uchar syscalls_mem[ FD_SBPF_SYSCALLS_FOOTPRINT ] __attribute__((aligned(FD_SBPF_SYSCALLS_ALIGN)));
109 250 : fd_sbpf_syscalls_t * syscalls = fd_sbpf_syscalls_join( fd_sbpf_syscalls_new( syscalls_mem ) );
110 250 : if( FD_UNLIKELY( !syscalls ) ) {
111 : //TODO: full log including err
112 0 : fd_log_collector_msg_literal( instr_ctx, "Failed to register syscalls" );
113 0 : return FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE;
114 0 : }
115 :
116 : /* https://github.com/anza-xyz/agave/blob/v5.0.0-alpha.1/runtime/src/bank.rs#L2066-L2076 */
117 250 : int single_env = FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, single_program_runtime_environment );
118 250 : ulong deploy_env_slot = instr_ctx->bank->f.slot + (single_env ? 0UL : 1UL);
119 :
120 250 : fd_vm_syscall_register_slot( syscalls,
121 250 : deploy_env_slot,
122 250 : &instr_ctx->bank->f.features,
123 250 : 1 );
124 :
125 : /* Load executable */
126 250 : fd_sbpf_elf_info_t elf_info[ 1UL ];
127 250 : fd_prog_versions_t versions = fd_prog_versions( &instr_ctx->bank->f.features, deploy_env_slot );
128 :
129 : /* SIMD-0500: when active, restrict program deployment to SBPF v3+.
130 : Older SBPF versions remain executable.
131 : TODO: fix link when 4.1 is released
132 : https://github.com/anza-xyz/agave/blob/v4.1.0-alpha.0/program-runtime/src/deploy.rs#L30-L32 */
133 250 : if( disable_sbpf_v0_v1_v2_deployment ) {
134 0 : versions.min_sbpf_version = FD_SBPF_V3;
135 0 : }
136 :
137 250 : fd_sbpf_loader_config_t config = { 0 };
138 250 : config.elf_deploy_checks = deploy_mode;
139 250 : config.sbpf_min_version = versions.min_sbpf_version;
140 250 : config.sbpf_max_version = versions.max_sbpf_version;
141 :
142 250 : if( FD_UNLIKELY( fd_sbpf_elf_peek( elf_info, programdata, programdata_size, &config )<0 ) ) {
143 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
144 0 : }
145 :
146 250 : fd_sbpf_program_t * prog = fd_sbpf_program_new( scratch->sbpf_footprint, elf_info, scratch->rodata );
147 250 : if( FD_UNLIKELY( !prog ) ) {
148 0 : FD_LOG_ERR(( "fd_sbpf_program_new() failed" ));
149 0 : }
150 :
151 : /* Load program */
152 250 : int err = fd_sbpf_program_load( prog, programdata, programdata_size, syscalls, &config, scratch->programdata, programdata_size );
153 250 : if( FD_UNLIKELY( err ) ) {
154 71 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
155 71 : }
156 :
157 : /* Validate the program */
158 179 : fd_vm_t _vm[ 1UL ];
159 179 : fd_vm_t * vm = fd_vm_join( fd_vm_new( _vm ) );
160 :
161 179 : vm = fd_vm_init(
162 179 : /* vm */ vm,
163 179 : /* instr_ctx */ instr_ctx,
164 179 : /* heap_max */ instr_ctx->txn_out->details.compute_budget.heap_size,
165 179 : /* entry_cu */ instr_ctx->txn_out->details.compute_budget.compute_meter,
166 179 : /* rodata */ prog->rodata,
167 179 : /* rodata_sz */ prog->rodata_sz,
168 179 : /* text */ prog->text,
169 179 : /* text_cnt */ prog->info.text_cnt,
170 179 : /* text_off */ prog->info.text_off, /* FIXME: What if text_off is not multiple of 8 */
171 179 : /* text_sz */ prog->info.text_sz,
172 179 : /* entry_pc */ prog->entry_pc,
173 179 : /* calldests */ prog->calldests,
174 179 : /* sbpf_version */ elf_info->sbpf_version,
175 179 : /* syscalls */ syscalls,
176 : /* trace */ NULL,
177 : /* sha */ NULL,
178 : /* mem_regions */ NULL,
179 179 : /* mem_regions_cnt */ 0,
180 : /* mem_region_accs */ NULL,
181 179 : /* is_deprecated */ 0,
182 179 : /* dump_syscall_to_pb */ 0,
183 179 : /* reg */ (ulong[ FD_VM_REG_CNT ]){ 0 } );
184 179 : if ( FD_UNLIKELY( vm == NULL ) ) {
185 0 : FD_LOG_WARNING(( "NULL vm" ));
186 0 : return FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE;
187 0 : }
188 :
189 179 : int validate_result = fd_vm_validate( vm );
190 179 : if( FD_UNLIKELY( validate_result!=FD_VM_SUCCESS ) ) {
191 1 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
192 1 : }
193 :
194 178 : return FD_EXECUTOR_INSTR_SUCCESS;
195 179 : }
196 :
197 : int
198 : fd_deploy_program( fd_exec_instr_ctx_t * instr_ctx,
199 : uchar const * programdata,
200 : ulong programdata_size,
201 250 : int disable_sbpf_v0_v1_v2_deployment ) {
202 250 : fd_bpf_deploy_scratch_t * scratch = fd_bpf_frames_deploy_scratch( instr_ctx->runtime->bpf_frames );
203 250 : return deploy_program( instr_ctx, programdata, programdata_size, disable_sbpf_v0_v1_v2_deployment, scratch );
204 250 : }
205 :
206 : /* SIMD-0500 finalize gate: when the feature is active and the ELF
207 : embedded in `programdata` parses as SBPFv0/v1/v2, reject;
208 : otherwise accept (including the short-programdata case, which
209 : mirrors the let-chain short-circuit in agave). See the call site
210 : in `process_loader_upgradeable_instruction` for the SetAuthority
211 : handler.
212 : https://github.com/anza-xyz/agave/blob/v4.1.0-alpha.0/programs/bpf_loader/src/lib.rs#L580-L591 */
213 : int
214 : fd_bpf_loader_finalize_v3_check( int feature_active,
215 : uchar const * programdata,
216 12 : ulong programdata_len ) {
217 12 : ulong const off = PROGRAMDATA_METADATA_SIZE + 48UL; /* ELF64 e_flags */
218 12 : if( !feature_active ) return FD_EXECUTOR_INSTR_SUCCESS;
219 0 : if( programdata_len < off + sizeof(uint) ) return FD_EXECUTOR_INSTR_SUCCESS;
220 0 : if( FD_LOAD( uint, programdata+off )<FD_SBPF_V3 ) return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
221 0 : return FD_EXECUTOR_INSTR_SUCCESS;
222 0 : }
223 :
224 : /* SIMD-0433 ProgramData sizing for the Loader V3 Upgrade instruction.
225 : https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.6/programs/bpf_loader/src/lib.rs#L443-L468 */
226 : int
227 : fd_bpf_loader_v3_upgrade_sizing( int set_programdata_to_elf_len,
228 : ulong programdata_current_len,
229 : ulong buffer_data_len,
230 : fd_rent_t const * rent,
231 : ulong * out_programdata_len,
232 249 : ulong * out_balance_required ) {
233 249 : ulong programdata_len;
234 249 : if( set_programdata_to_elf_len ) {
235 0 : programdata_len = fd_ulong_sat_add( PROGRAMDATA_METADATA_SIZE, buffer_data_len );
236 0 : *out_programdata_len = programdata_len;
237 0 : if( FD_UNLIKELY( programdata_len>MAX_PERMITTED_DATA_LENGTH ) ) {
238 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
239 0 : }
240 249 : } else {
241 249 : programdata_len = programdata_current_len;
242 249 : *out_programdata_len = programdata_len;
243 249 : if( FD_UNLIKELY( programdata_len<fd_ulong_sat_add( PROGRAMDATA_METADATA_SIZE, buffer_data_len ) ) ) {
244 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
245 0 : }
246 249 : }
247 249 : *out_balance_required = fd_ulong_max( 1UL, fd_rent_exempt_minimum_balance( rent, programdata_len ) );
248 249 : return FD_EXECUTOR_INSTR_SUCCESS;
249 249 : }
250 :
251 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L195-L218 */
252 : static int
253 : write_program_data( fd_exec_instr_ctx_t * instr_ctx,
254 : ushort instr_acc_idx,
255 : ulong program_data_offset,
256 : uchar const * bytes,
257 259 : ulong bytes_len ) {
258 259 : int err;
259 :
260 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L202 */
261 259 : fd_guarded_borrowed_account_t program = {0};
262 259 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, instr_acc_idx, &program );
263 :
264 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L203 */
265 259 : uchar * data = NULL;
266 259 : ulong dlen = 0UL;
267 259 : err = fd_borrowed_account_get_data_mut( &program, &data, &dlen );
268 259 : if( FD_UNLIKELY( err ) ) {
269 1 : return err;
270 1 : }
271 :
272 258 : ulong write_offset = fd_ulong_sat_add( program_data_offset, bytes_len );
273 258 : if( FD_UNLIKELY( fd_borrowed_account_get_data_len( &program )<write_offset ) ) {
274 : /* Max msg_sz: 24 - 6 + 2*20 = 58 < 127 => we can use printf */
275 0 : fd_log_collector_printf_dangerous_max_127( instr_ctx,
276 0 : "Write overflow: %lu < %lu", fd_borrowed_account_get_data_len( &program ), write_offset );
277 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
278 0 : }
279 :
280 258 : if( FD_UNLIKELY( program_data_offset>dlen ) ) {
281 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
282 0 : }
283 :
284 258 : if( FD_LIKELY( bytes_len ) ) {
285 258 : fd_memcpy( data+program_data_offset, bytes, bytes_len );
286 258 : }
287 :
288 258 : return FD_EXECUTOR_INSTR_SUCCESS;
289 258 : }
290 :
291 : int
292 : fd_bpf_loader_program_get_state( fd_acc_t const * acc,
293 23674 : fd_bpf_state_t * state ) {
294 23674 : if( FD_UNLIKELY( fd_bpf_state_decode( state, acc->data, acc->data_len ) ) ) {
295 632 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
296 632 : }
297 23042 : return FD_EXECUTOR_INSTR_SUCCESS;
298 23674 : }
299 :
300 : int
301 : fd_bpf_loader_program_get_state2( uchar const * data,
302 : ulong data_len,
303 28291 : fd_bpf_state_t * state ) {
304 28291 : if( FD_UNLIKELY( fd_bpf_state_decode( state, data, data_len ) ) ) {
305 4035 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
306 4035 : }
307 24256 : return FD_EXECUTOR_INSTR_SUCCESS;
308 28291 : }
309 :
310 : /* Mirrors solana_sdk::transaction_context::BorrowedAccount::set_state()
311 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L973 */
312 : int
313 : fd_bpf_loader_v3_program_set_state( fd_borrowed_account_t * borrowed_acct,
314 1353 : fd_bpf_state_t * state ) {
315 1353 : ulong state_size = fd_bpf_state_size( state );
316 :
317 1353 : uchar * data = NULL;
318 1353 : ulong dlen = 0UL;
319 :
320 1353 : int err = fd_borrowed_account_get_data_mut( borrowed_acct, &data, &dlen );
321 1353 : if( FD_UNLIKELY( err ) ) {
322 0 : return err;
323 0 : }
324 :
325 1353 : if( FD_UNLIKELY( state_size>dlen ) ) {
326 298 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
327 298 : }
328 :
329 1055 : ulong out_sz = 0UL;
330 1055 : if( FD_UNLIKELY( fd_bpf_state_encode( state, data, state_size, &out_sz ) ) ) {
331 0 : return FD_EXECUTOR_INSTR_ERR_GENERIC_ERR;
332 0 : }
333 :
334 1055 : return FD_EXECUTOR_INSTR_SUCCESS;
335 1055 : }
336 :
337 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1299-L1331 */
338 : static int
339 : common_close_account( fd_pubkey_t * authority_address,
340 : fd_exec_instr_ctx_t * instr_ctx,
341 161 : fd_bpf_state_t * state ) {
342 161 : int err;
343 :
344 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L1307 */
345 161 : if( FD_UNLIKELY( !authority_address ) ) {
346 0 : fd_log_collector_msg_literal( instr_ctx, "Account is immutable" );
347 0 : return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
348 0 : }
349 :
350 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L1312-L1313 */
351 161 : fd_pubkey_t const * acc_key = NULL;
352 161 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 2UL, &acc_key );
353 161 : if( FD_UNLIKELY( err ) ) {
354 0 : return err;
355 0 : }
356 :
357 161 : if( FD_UNLIKELY( memcmp( authority_address, acc_key, sizeof(fd_pubkey_t) ) ) ) {
358 15 : fd_log_collector_msg_literal( instr_ctx, "Incorrect authority provided" );
359 15 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
360 15 : }
361 :
362 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L1319-L1322 */
363 146 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 2UL, &err ) ) ) {
364 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
365 5 : if( FD_UNLIKELY( !!err ) ) return err;
366 5 : fd_log_collector_msg_literal( instr_ctx, "Authority did not sign" );
367 5 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
368 5 : }
369 :
370 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1324 */
371 141 : fd_guarded_borrowed_account_t close_account = {0};
372 141 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &close_account );
373 :
374 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1326 */
375 141 : fd_guarded_borrowed_account_t recipient_account = {0};
376 141 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 1UL, &recipient_account );
377 :
378 141 : err = fd_borrowed_account_checked_add_lamports( &recipient_account,
379 141 : fd_borrowed_account_get_lamports( &close_account ) );
380 141 : if( FD_UNLIKELY( err ) ) {
381 0 : return err;
382 0 : }
383 :
384 141 : err = fd_borrowed_account_set_lamports( &close_account, 0UL );
385 141 : if( FD_UNLIKELY( err ) ) {
386 0 : return err;
387 0 : }
388 :
389 : /* https://github.com/anza-xyz/agave/blob/v4.4.0-beta.0/programs/bpf_loader/src/lib.rs#L1043 */
390 141 : err = fd_borrowed_account_set_data_length( &close_account, SIZE_OF_UNINITIALIZED );
391 141 : if( FD_UNLIKELY( err ) ) {
392 0 : return err;
393 0 : }
394 :
395 141 : state->discriminant = FD_BPF_STATE_UNINITIALIZED;
396 141 : err = fd_bpf_loader_v3_program_set_state( &close_account, state );
397 141 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
398 0 : return err;
399 0 : }
400 :
401 141 : return FD_EXECUTOR_INSTR_SUCCESS;
402 141 : }
403 :
404 :
405 : /* Every loader-owned BPF program goes through this function, which goes into the VM.
406 :
407 : https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1332-L1501 */
408 : static int
409 : bpf_execute( fd_exec_instr_ctx_t * instr_ctx,
410 : fd_progcache_rec_t const * cache_entry,
411 3951 : uchar is_deprecated ) {
412 3951 : long const regime0 = fd_tickcount();
413 :
414 3951 : int err = FD_EXECUTOR_INSTR_SUCCESS;
415 :
416 3951 : uchar syscalls_mem[ FD_SBPF_SYSCALLS_FOOTPRINT ] __attribute__((aligned(FD_SBPF_SYSCALLS_ALIGN)));
417 3951 : fd_sbpf_syscalls_t * syscalls = fd_sbpf_syscalls_join( fd_sbpf_syscalls_new( syscalls_mem ) );
418 3951 : if( FD_UNLIKELY( !syscalls ) ) {
419 0 : FD_LOG_CRIT(( "Unable to allocate syscalls" ));
420 0 : }
421 :
422 : /* TODO do we really need to re-do this on every instruction? */
423 3951 : fd_vm_syscall_register_slot( syscalls,
424 3951 : instr_ctx->bank->f.slot,
425 3951 : &instr_ctx->bank->f.features,
426 3951 : 0 );
427 :
428 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1362-L1368 */
429 3951 : ulong input_sz = 0UL;
430 3951 : ulong pre_lens[256];
431 3951 : fd_vm_input_region_t input_mem_regions[1000]; /* We can have a max of (3 * num accounts + 1) regions */
432 3951 : fd_vm_acc_region_meta_t acc_region_metas[256]; /* instr acc idx to idx */
433 3951 : uint input_mem_regions_cnt = 0U;
434 3951 : int provide_instruction_data_offset_in_vm_r2 = FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, provide_instruction_data_offset_in_vm_r2 );
435 3951 : int direct_account_pointers_in_program_input = FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, direct_account_pointers_in_program_input );
436 3951 : int pass_input_metadata_in_vm_r3_r5 = FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, pass_input_metadata_in_vm_r3_r5 );
437 :
438 3951 : ulong instruction_data_offset = 0UL;
439 3951 : ulong account_pointers_offset = 0UL;
440 : /* 16-byte aligned buffer:
441 : https://github.com/anza-xyz/agave/blob/v3.0.0/program-runtime/src/serialization.rs#L60 */
442 3951 : uchar * input = instr_ctx->runtime->bpf_loader_serialization.frame[ instr_ctx->runtime->instr.stack_sz-1UL ];
443 3951 : err = fd_bpf_loader_input_serialize_parameters( instr_ctx, pre_lens,
444 3951 : input_mem_regions, &input_mem_regions_cnt,
445 3951 : acc_region_metas,
446 3951 : direct_account_pointers_in_program_input, is_deprecated,
447 3951 : &instruction_data_offset,
448 3951 : &account_pointers_offset, &input_sz );
449 3951 : if( FD_UNLIKELY( err ) ) {
450 0 : return err;
451 0 : }
452 :
453 3951 : fd_sha256_t _sha[1];
454 3951 : fd_sha256_t * sha = fd_sha256_join( fd_sha256_new( _sha ) );
455 :
456 3951 : fd_vm_t _vm[1];
457 3951 : fd_vm_t * vm = fd_vm_join( fd_vm_new( _vm ) );
458 :
459 3951 : ulong pre_insn_cus = instr_ctx->txn_out->details.compute_budget.compute_meter;
460 3951 : ulong heap_size = instr_ctx->txn_out->details.compute_budget.heap_size;
461 :
462 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L275-L278 */
463 3951 : ulong heap_cost = calculate_heap_cost( heap_size, FD_VM_HEAP_COST );
464 3951 : int heap_cost_result = fd_executor_consume_cus( instr_ctx->txn_out, heap_cost );
465 3951 : if( FD_UNLIKELY( heap_cost_result ) ) {
466 0 : return FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE;
467 0 : }
468 :
469 : /* For dumping syscalls for seed corpora */
470 3951 : int dump_syscall_to_pb = instr_ctx->runtime->log.dump_proto_ctx &&
471 3951 : instr_ctx->bank->f.slot>=instr_ctx->runtime->log.dump_proto_ctx->dump_proto_start_slot &&
472 3951 : instr_ctx->runtime->log.dump_proto_ctx->dump_syscall_to_pb;
473 :
474 : /* Set up initial register values */
475 3951 : ulong reg[ FD_VM_REG_CNT ] = { 0 };
476 : /* https://github.com/anza-xyz/agave/blob/v4.5/program-runtime/src/vm.rs#L323 */
477 3951 : reg[1] = FD_VM_MEM_MAP_INPUT_REGION_START;
478 : /* https://github.com/anza-xyz/agave/blob/v3.1.1/programs/bpf_loader/src/lib.rs#L1525-L1528 */
479 >1844*10^16 : reg[2] = provide_instruction_data_offset_in_vm_r2 ? instruction_data_offset : 0UL;
480 : /* https://github.com/anza-xyz/agave/blob/v4.5/program-runtime/src/vm.rs#L326-L343 */
481 3951 : if( !is_deprecated && pass_input_metadata_in_vm_r3_r5 ) {
482 0 : if( FD_UNLIKELY( account_pointers_offset==ULONG_MAX ) ) {
483 0 : fd_log_collector_msg_literal( instr_ctx, "Failed to create SBF VM: cannot determine account pointers offset" );
484 0 : return FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE;
485 0 : }
486 0 : reg[3] = instr_ctx->instr->data_sz;
487 0 : reg[4] = account_pointers_offset;
488 0 : reg[5] = instr_ctx->instr->acct_cnt;
489 0 : }
490 :
491 : /* TODO: (topointon): correctly set check_size in vm setup */
492 3951 : fd_wksp_t * progcache_wksp = instr_ctx->runtime->progcache->join->data_base;
493 3951 : void const * rodata = fd_progcache_rec_rodata( cache_entry, progcache_wksp );
494 3951 : vm = fd_vm_init(
495 3951 : /* vm */ vm,
496 3951 : /* instr_ctx */ instr_ctx,
497 3951 : /* heap_max */ heap_size,
498 3951 : /* entry_cu */ instr_ctx->txn_out->details.compute_budget.compute_meter,
499 3951 : /* rodata */ rodata,
500 3951 : /* rodata_sz */ cache_entry->rodata_sz,
501 3951 : /* text (note: text_off is byte offset) */ (ulong *)( (ulong)rodata + cache_entry->text_off ),
502 3951 : /* text_cnt */ cache_entry->text_cnt,
503 3951 : /* text_off */ cache_entry->text_off,
504 3951 : /* text_sz */ cache_entry->text_sz,
505 3951 : /* entry_pc */ cache_entry->entry_pc,
506 3951 : /* calldests */ fd_progcache_rec_calldests( cache_entry, progcache_wksp ),
507 3951 : /* sbpf_version */ cache_entry->sbpf_version,
508 3951 : /* syscalls */ syscalls,
509 : /* trace */ NULL,
510 3951 : /* sha */ sha,
511 3951 : /* input_mem_regions */ input_mem_regions,
512 3951 : /* input_mem_regions_cnt */ input_mem_regions_cnt,
513 3951 : /* acc_region_metas */ acc_region_metas,
514 3951 : /* is_deprecated */ is_deprecated,
515 3951 : /* dump_syscall_to_pb */ dump_syscall_to_pb,
516 3951 : /* reg */ reg );
517 3951 : if( FD_UNLIKELY( !vm ) ) {
518 : /* We throw an error here because it could be the case that the given heap_size > HEAP_MAX.
519 : In this case, Agave fails the transaction but does not error out.
520 :
521 : https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1396 */
522 0 : return FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE;
523 0 : }
524 :
525 3951 : if( FD_UNLIKELY( instr_ctx->runtime->log.enable_vm_tracing && instr_ctx->runtime->log.tracing_mem ) ) {
526 0 : vm->trace = fd_vm_trace_join( fd_vm_trace_new( instr_ctx->runtime->log.tracing_mem + ((instr_ctx->runtime->instr.stack_sz-1UL) * FD_RUNTIME_VM_TRACE_STATIC_FOOTPRINT), FD_RUNTIME_VM_TRACE_EVENT_MAX ));
527 0 : if( FD_UNLIKELY( !vm->trace ) ) FD_LOG_ERR(( "unable to create trace; make sure you've compiled with sufficient spad size " ));
528 0 : }
529 :
530 3951 : long const regime1 = fd_tickcount();
531 :
532 3951 : int exec_err = fd_vm_exec( vm );
533 3951 : instr_ctx->txn_out->details.compute_budget.compute_meter = vm->cu;
534 :
535 3951 : long const regime2 = fd_tickcount();
536 :
537 3951 : if( instr_ctx->instr->stack_height==1 ) {
538 3600 : instr_ctx->runtime->metrics.vm_setup_cum_ticks += (ulong)( regime1-regime0 );
539 3600 : instr_ctx->runtime->metrics.vm_exec_cum_ticks += (ulong)( regime2-regime1 );
540 3600 : } else {
541 351 : instr_ctx->runtime->metrics.cpi_setup_cum_ticks += (ulong)( regime1-regime0 );
542 351 : }
543 :
544 3951 : if( FD_UNLIKELY( vm->trace ) ) {
545 0 : err = fd_vm_trace_printf( vm->trace );
546 0 : if( FD_UNLIKELY( err ) ) {
547 0 : FD_LOG_WARNING(( "fd_vm_trace_printf failed (%i-%s)", err, fd_vm_strerror( err ) ));
548 0 : }
549 0 : }
550 :
551 : /* Log consumed compute units and return data.
552 : https://github.com/anza-xyz/agave/blob/v4.3.0/program-runtime/src/vm.rs#L347-L353 */
553 3951 : fd_log_collector_program_consumed( instr_ctx, pre_insn_cus-heap_cost-vm->cu, pre_insn_cus );
554 3951 : if( FD_UNLIKELY( instr_ctx->txn_out->details.return_data.len ) ) {
555 149 : fd_log_collector_program_return( instr_ctx );
556 149 : }
557 :
558 : /* We have a big error-matching arm here
559 : https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1674-L1744 */
560 :
561 : /* Handle non-zero return status with successful VM execution. This is
562 : the Ok(status) case, hence exec_err must be 0 for this case to be hit.
563 : https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1675-L1678 */
564 3951 : if( FD_LIKELY( !exec_err ) ) {
565 1553 : ulong status = vm->reg[0];
566 1553 : if( FD_UNLIKELY( status ) ) {
567 47 : err = program_error_to_instr_error( status, &instr_ctx->txn_out->err.custom_err );
568 47 : FD_VM_PREPARE_ERR_OVERWRITE( vm );
569 47 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
570 47 : return err;
571 47 : }
572 2398 : } else {
573 : /* https://github.com/anza-xyz/agave/blob/v2.1.13/programs/bpf_loader/src/lib.rs#L1434-L1439 */
574 : /* (SIMD-182) Consume ALL requested CUs on non-Syscall errors */
575 2398 : if( FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, deplete_cu_meter_on_vm_failure ) &&
576 2398 : exec_err!=FD_VM_ERR_EBPF_SYSCALL_ERROR ) {
577 0 : instr_ctx->txn_out->details.compute_budget.compute_meter = 0UL;
578 0 : }
579 :
580 : /* Remap load and store access violations to more specific account
581 : errors.
582 : https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1556-L1618 */
583 2398 : if( FD_UNLIKELY( ( exec_err==FD_VM_ERR_EBPF_ACCESS_VIOLATION || instr_ctx->txn_out->err.exec_err==FD_VM_ERR_EBPF_ACCESS_VIOLATION ) &&
584 2398 : vm->segv_vaddr!=ULONG_MAX ) ) {
585 :
586 : /* If the vaddr of the access violation falls within the bounds of a
587 : serialized account vaddr range, then try to retrieve a more specific
588 : vm error based on the account's access permissions. */
589 3119 : for( ushort i=0UL; i<instr_ctx->instr->acct_cnt; i++ ) {
590 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1455 */
591 :
592 : /* Find the input memory region that corresponds to the access
593 : https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1566-L1617 */
594 3002 : ulong idx = acc_region_metas[i].region_idx;
595 3002 : fd_vm_input_region_t const * input_mem_region = &input_mem_regions[idx];
596 3002 : fd_vm_acc_region_meta_t const * acc_region_meta = &acc_region_metas[i];
597 :
598 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1484-L1492 */
599 3002 : ulong region_data_vaddr_start = FD_VM_MEM_MAP_INPUT_REGION_START + input_mem_region->vaddr_offset + input_mem_region->region_sz;
600 3002 : ulong region_data_vaddr_end = fd_ulong_sat_add( region_data_vaddr_start, acc_region_meta->original_data_len );
601 3002 : if( FD_LIKELY( !is_deprecated ) ) {
602 1404 : region_data_vaddr_end = fd_ulong_sat_add( region_data_vaddr_end, MAX_PERMITTED_DATA_INCREASE );
603 1404 : }
604 :
605 3002 : if( vm->segv_vaddr >= region_data_vaddr_start && vm->segv_vaddr <= region_data_vaddr_end ) {
606 :
607 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1575-L1616 */
608 22 : fd_guarded_borrowed_account_t instr_acc = {0};
609 22 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, i, &instr_acc );
610 :
611 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1581-L1616 */
612 22 : if( fd_ulong_sat_add( vm->segv_vaddr, vm->segv_access_len ) <= region_data_vaddr_end ) {
613 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1592-L1601 */
614 20 : if( vm->segv_access_type==FD_VM_ACCESS_TYPE_ST ) {
615 6 : int borrow_err = FD_EXECUTOR_INSTR_SUCCESS;
616 6 : if( !fd_borrowed_account_can_data_be_changed( &instr_acc, &borrow_err ) || borrow_err != FD_EXECUTOR_INSTR_SUCCESS ) {
617 6 : err = borrow_err;
618 6 : } else {
619 0 : err = FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
620 0 : }
621 14 : } else if( vm->segv_access_type==FD_VM_ACCESS_TYPE_LD ) {
622 14 : int borrow_err = FD_EXECUTOR_INSTR_SUCCESS;
623 14 : if( !fd_borrowed_account_can_data_be_changed( &instr_acc, &borrow_err ) || borrow_err != FD_EXECUTOR_INSTR_SUCCESS ) {
624 14 : err = FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
625 14 : } else {
626 0 : err = FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
627 0 : }
628 14 : } else {
629 0 : FD_LOG_CRIT(( "invariant violation: unsupported access type: %i", vm->segv_access_type ));
630 0 : }
631 :
632 20 : FD_VM_PREPARE_ERR_OVERWRITE( vm );
633 20 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
634 20 : return err;
635 20 : }
636 22 : }
637 3002 : }
638 137 : }
639 :
640 : /* The error kind should have been set in the VM. Match it and set
641 : the error code accordingly. There are no direct permalinks here -
642 : this is all a result of Agave's complex nested error-code handling
643 : and our design decisions for making our error codes match. */
644 :
645 : /* Instr error case. Set the error kind and return the instruction error */
646 2378 : if( instr_ctx->txn_out->err.exec_err_kind==FD_EXECUTOR_ERR_KIND_INSTR ) {
647 550 : err = instr_ctx->txn_out->err.exec_err;
648 550 : FD_VM_PREPARE_ERR_OVERWRITE( vm );
649 550 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
650 550 : return err;
651 550 : }
652 :
653 : /* Syscall error case. The VM would have also set the syscall error
654 : code in the txn_ctx exec_err. */
655 1828 : if( instr_ctx->txn_out->err.exec_err_kind==FD_EXECUTOR_ERR_KIND_SYSCALL ) {
656 301 : err = instr_ctx->txn_out->err.exec_err;
657 301 : FD_VM_PREPARE_ERR_OVERWRITE( vm );
658 301 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, err );
659 301 : return FD_EXECUTOR_INSTR_ERR_PROGRAM_FAILED_TO_COMPLETE;
660 301 : }
661 :
662 : /* An access violation that takes place inside a syscall will
663 : cause `exec_res` to be set to EbpfError::SyscallError,
664 : 'but the `txn_ctx->err.exec_err_kind` will be set to EBPF and
665 : `txn_ctx->err.exec_err` will be set to the EBPF error. In this
666 : specific case, there is nothing to do since the error and error
667 : kind area already set correctly. Otherwise, we need to log the
668 : EBPF error. */
669 1527 : if( exec_err!=FD_VM_ERR_EBPF_SYSCALL_ERROR ) {
670 1480 : FD_VM_PREPARE_ERR_OVERWRITE( vm );
671 1480 : FD_VM_ERR_FOR_LOG_EBPF( vm, exec_err );
672 1480 : }
673 :
674 1527 : return FD_EXECUTOR_INSTR_ERR_PROGRAM_FAILED_TO_COMPLETE;
675 1828 : }
676 :
677 1506 : err = fd_bpf_loader_input_deserialize_parameters(
678 1506 : instr_ctx, pre_lens, input, input_sz, is_deprecated );
679 :
680 1506 : long const regime3 = fd_tickcount();
681 1506 : if( instr_ctx->instr->stack_height==1 ) {
682 1210 : instr_ctx->runtime->metrics.vm_commit_cum_ticks += (ulong)( regime3-regime2 );
683 1210 : } else {
684 296 : instr_ctx->runtime->metrics.cpi_commit_cum_ticks += (ulong)( regime3-regime2 );
685 296 : }
686 :
687 1506 : return err;
688 3951 : }
689 :
690 : int
691 : fd_bpf_execute( fd_exec_instr_ctx_t * instr_ctx,
692 : fd_progcache_rec_t const * cache_entry,
693 3953 : uchar is_deprecated ) {
694 : /* Tiles holding frames can wait on the program cache spill lock, so
695 : its holder must never wait for a frame. */
696 3953 : fd_runtime_t * runtime = instr_ctx->runtime;
697 3953 : uchar * frame = fd_bpf_frames_acquire( runtime->bpf_frames, fd_progcache_spill_held( runtime->progcache ) );
698 3953 : runtime->bpf_loader_serialization.frame[ runtime->instr.stack_sz-1UL ] = frame;
699 3953 : int err = bpf_execute( instr_ctx, cache_entry, is_deprecated );
700 3953 : fd_bpf_frames_release( runtime->bpf_frames, frame );
701 3953 : return err;
702 3953 : }
703 :
704 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1358-L1539 */
705 : static int
706 : common_extend_program( fd_exec_instr_ctx_t * instr_ctx,
707 : uint additional_bytes,
708 381 : uchar check_authority ) {
709 381 : int err;
710 :
711 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1366 */
712 381 : fd_pubkey_t const * program_id = NULL;
713 381 : err = fd_exec_instr_ctx_get_last_program_key( instr_ctx, &program_id );
714 381 : if( FD_UNLIKELY( err ) ) {
715 0 : return err;
716 0 : }
717 :
718 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1368-L1370 */
719 381 : #define PROGRAM_DATA_ACCOUNT_INDEX (0)
720 381 : #define PROGRAM_ACCOUNT_INDEX (1)
721 381 : #define AUTHORITY_ACCOUNT_INDEX (2)
722 :
723 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1371-L1372 */
724 381 : uchar optional_payer_account_index = check_authority ? 4 : 3;
725 :
726 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1374-L1377 */
727 381 : if( FD_UNLIKELY( additional_bytes==0U ) ) {
728 0 : fd_log_collector_msg_literal( instr_ctx, "Additional bytes must be greater than 0" );
729 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
730 0 : }
731 :
732 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1379-L1381 */
733 381 : fd_guarded_borrowed_account_t programdata_account = {0};
734 381 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, PROGRAM_DATA_ACCOUNT_INDEX, &programdata_account );
735 381 : fd_pubkey_t const * programdata_key = (fd_pubkey_t*)programdata_account.acc->pubkey;
736 :
737 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1383-L1386 */
738 381 : if( FD_UNLIKELY( memcmp( program_id, fd_borrowed_account_get_owner( &programdata_account ), sizeof(fd_pubkey_t) ) ) ) {
739 7 : fd_log_collector_msg_literal( instr_ctx, "ProgramData owner is invalid" );
740 7 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER;
741 7 : }
742 :
743 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1387-L1390 */
744 374 : if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &programdata_account ) ) ) {
745 0 : fd_log_collector_msg_literal( instr_ctx, "ProgramData is not writable" );
746 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
747 0 : }
748 :
749 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1392-L1393 */
750 374 : fd_guarded_borrowed_account_t program_account = {0};
751 374 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, PROGRAM_ACCOUNT_INDEX, &program_account );
752 :
753 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1394-L1397 */
754 374 : if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &program_account ) ) ) {
755 0 : fd_log_collector_msg_literal( instr_ctx, "Program account is not writable" );
756 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
757 0 : }
758 :
759 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1398-L1401 */
760 374 : if( FD_UNLIKELY( memcmp( program_id, fd_borrowed_account_get_owner( &program_account ), sizeof(fd_pubkey_t) ) ) ) {
761 1 : fd_log_collector_msg_literal( instr_ctx, "Program account not owned by loader" );
762 1 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER;
763 1 : }
764 :
765 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1403-L1419 */
766 373 : fd_bpf_state_t program_state[1];
767 373 : err = fd_bpf_loader_program_get_state( program_account.acc, program_state );
768 373 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
769 0 : return err;
770 0 : }
771 :
772 373 : if( program_state->discriminant==FD_BPF_STATE_PROGRAM ) {
773 373 : if( FD_UNLIKELY( memcmp( &program_state->inner.program.programdata_address, programdata_key, sizeof(fd_pubkey_t) ) ) ) {
774 83 : fd_log_collector_msg_literal( instr_ctx, "Program account does not match ProgramData account" );
775 83 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
776 83 : }
777 373 : } else {
778 0 : fd_log_collector_msg_literal( instr_ctx, "Invalid Program account" );
779 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
780 0 : }
781 :
782 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1420 */
783 290 : fd_borrowed_account_drop( &program_account );
784 :
785 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1422-L1432 */
786 290 : ulong old_len = fd_borrowed_account_get_data_len( &programdata_account );
787 290 : ulong new_len = fd_ulong_sat_add( old_len, additional_bytes );
788 290 : if( FD_UNLIKELY( new_len>MAX_PERMITTED_DATA_LENGTH ) ) {
789 : /* Max msg_sz: 85 - 6 + 2*20 = 119 < 127 => we can use printf */
790 0 : fd_log_collector_printf_dangerous_max_127( instr_ctx,
791 0 : "Extended ProgramData length of %lu bytes exceeds max account data length of %lu bytes", new_len, MAX_PERMITTED_DATA_LENGTH );
792 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
793 0 : }
794 :
795 : /* https://github.com/anza-xyz/agave/blob/v4.1.0-beta.1/programs/bpf_loader/src/lib.rs#L861-L883 */
796 290 : if( FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, loader_v3_minimum_extend_program_size ) ) {
797 290 : ulong headroom = fd_ulong_sat_sub( MAX_PERMITTED_DATA_LENGTH, old_len );
798 290 : if( FD_UNLIKELY( additional_bytes<MINIMUM_EXTEND_PROGRAM_BYTES && additional_bytes!=headroom ) ) {
799 : /* Max msg_sz: 113 - (3+2) + (5+10) = 123 < 127 => we can use printf */
800 288 : fd_log_collector_printf_dangerous_max_127(
801 288 : instr_ctx,
802 288 : "ExtendProgram requires a minimum of %lu additional bytes or to extend to maximum size, but only %u were requested",
803 288 : MINIMUM_EXTEND_PROGRAM_BYTES,
804 288 : additional_bytes
805 288 : );
806 288 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
807 288 : }
808 290 : }
809 :
810 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1434-L1437 */
811 2 : fd_sol_sysvar_clock_t clock[1];
812 2 : if( FD_UNLIKELY( !fd_sysvar_cache_clock_read( instr_ctx->sysvar_cache, clock ) ) ) {
813 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
814 0 : }
815 2 : ulong clock_slot = clock->slot;
816 :
817 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1439-L1478 */
818 2 : fd_pubkey_t * upgrade_authority_address = NULL;
819 2 : fd_bpf_state_t programdata_state[1];
820 2 : err = fd_bpf_loader_program_get_state( programdata_account.acc, programdata_state );
821 2 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
822 0 : return err;
823 0 : }
824 2 : if( programdata_state->discriminant==FD_BPF_STATE_PROGRAM_DATA ) {
825 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1444-L1447 */
826 2 : if( FD_UNLIKELY( clock_slot==programdata_state->inner.program_data.slot ) ) {
827 0 : fd_log_collector_msg_literal( instr_ctx, "Program was extended in this block already" );
828 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
829 0 : }
830 :
831 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1449-L1455 */
832 2 : if( FD_UNLIKELY( !programdata_state->inner.program_data.has_upgrade_authority_address ) ) {
833 0 : fd_log_collector_msg_literal( instr_ctx, "Cannot extend ProgramData accounts that are not upgradeable" );
834 0 : return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
835 0 : }
836 :
837 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1457-L1472 */
838 2 : if( check_authority ) {
839 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1458-L1463 */
840 0 : fd_pubkey_t const * authority_key = NULL;
841 0 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, AUTHORITY_ACCOUNT_INDEX, &authority_key );
842 0 : if( FD_UNLIKELY( err ) ) {
843 0 : return err;
844 0 : }
845 :
846 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1464-L1467 */
847 0 : if( FD_UNLIKELY( !fd_pubkey_eq( &programdata_state->inner.program_data.upgrade_authority_address, authority_key ) ) ) {
848 0 : fd_log_collector_msg_literal( instr_ctx, "Incorrect upgrade authority provided" );
849 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
850 0 : }
851 :
852 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1468-L1471 */
853 0 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, AUTHORITY_ACCOUNT_INDEX, &err ) ) ) {
854 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
855 0 : if( FD_UNLIKELY( !!err ) ) return err;
856 0 : fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
857 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
858 0 : }
859 0 : }
860 :
861 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1474 */
862 2 : fd_bpf_state_program_data_t * pd = &programdata_state->inner.program_data;
863 2 : upgrade_authority_address = pd->has_upgrade_authority_address ? &pd->upgrade_authority_address : NULL;
864 2 : } else {
865 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1476-L1477 */
866 0 : fd_log_collector_msg_literal( instr_ctx, "ProgramData state is invalid" );
867 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
868 0 : }
869 :
870 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1480-L1485 */
871 2 : fd_rent_t rent_;
872 2 : fd_rent_t const * rent = fd_sysvar_cache_rent_read( instr_ctx->sysvar_cache, &rent_ );
873 2 : if( FD_UNLIKELY( !rent ) ) {
874 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
875 0 : }
876 :
877 2 : ulong balance = fd_borrowed_account_get_lamports( &programdata_account );
878 2 : ulong min_balance = fd_ulong_max( fd_rent_exempt_minimum_balance( rent, new_len ), 1UL );
879 2 : ulong required_payment = fd_ulong_sat_sub( min_balance, balance );
880 :
881 : /* Borrowed accounts need to be dropped before native invocations. Note:
882 : the programdata account is manually released and acquired within the
883 : extend instruction to preserve the local variable scoping to maintain
884 : readability. The scoped macro still successfully handles the case of
885 : freeing a write lock in case of an early termination. */
886 :
887 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1488 */
888 2 : fd_borrowed_account_drop( &programdata_account );
889 :
890 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1492-L1502 */
891 2 : if( FD_UNLIKELY( required_payment>0UL ) ) {
892 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1493-L1496 */
893 2 : fd_pubkey_t const * payer_key = NULL;
894 2 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, optional_payer_account_index, &payer_key );
895 2 : if( FD_UNLIKELY( err ) ) {
896 0 : return err;
897 0 : }
898 :
899 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1498-L1501 */
900 2 : uchar instr_data[FD_TXN_MTU];
901 2 : fd_system_program_instruction_t instr = {
902 2 : .discriminant = FD_SYSTEM_PROGRAM_INSTR_TRANSFER,
903 2 : .inner = {
904 2 : .transfer = required_payment
905 2 : }
906 2 : };
907 :
908 2 : ulong instr_data_sz;
909 2 : int err = fd_system_program_instruction_encode( &instr, instr_data, FD_TXN_MTU, &instr_data_sz );
910 2 : if( FD_UNLIKELY( err ) ) {
911 0 : FD_LOG_CRIT(( "Failed to encode SystemInstruction with error %d", err ));
912 0 : }
913 :
914 2 : fd_vm_rust_account_meta_t acct_metas[ 2UL ];
915 2 : fd_native_cpi_create_account_meta( payer_key, 1UL, 1UL, &acct_metas[ 0UL ] );
916 2 : fd_native_cpi_create_account_meta( programdata_key, 0UL, 1UL, &acct_metas[ 1UL ] );
917 :
918 2 : err = fd_native_cpi_native_invoke( instr_ctx,
919 2 : &fd_solana_system_program_id,
920 2 : instr_data,
921 2 : instr_data_sz,
922 2 : acct_metas,
923 2 : 2UL,
924 2 : NULL,
925 2 : 0UL );
926 2 : if( FD_UNLIKELY( err ) ) {
927 2 : return err;
928 2 : }
929 2 : }
930 :
931 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1506-L1507 */
932 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, PROGRAM_DATA_ACCOUNT_INDEX, &programdata_account );
933 :
934 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1508 */
935 0 : err = fd_borrowed_account_set_data_length( &programdata_account, new_len );
936 0 : if( FD_UNLIKELY( err ) ) {
937 0 : return err;
938 0 : }
939 :
940 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1510 */
941 0 : ulong programdata_data_offset = PROGRAMDATA_METADATA_SIZE;
942 :
943 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1517-L1520 */
944 0 : if( FD_UNLIKELY( programdata_data_offset>fd_borrowed_account_get_data_len( &programdata_account ) ) ) {
945 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
946 0 : }
947 0 : uchar const * programdata_data = fd_borrowed_account_get_data( &programdata_account ) + programdata_data_offset;
948 0 : ulong programdata_size = new_len - PROGRAMDATA_METADATA_SIZE;
949 :
950 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1512-L1522 */
951 : /* SIMD-0500: extend_program is exempt from the deployment-version gate. */
952 0 : err = fd_deploy_program( instr_ctx, programdata_data, programdata_size, /* disable_sbpf_v0_v1_v2_deployment */ 0 );
953 0 : if( FD_UNLIKELY( err ) ) {
954 0 : return err;
955 0 : }
956 :
957 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1523 */
958 0 : fd_borrowed_account_drop( &programdata_account );
959 :
960 : /* Setting the discriminant and upgrade authority address here can likely
961 : be a no-op because these values shouldn't change. These can probably be
962 : removed, but can help to mirror against Agave client's implementation.
963 : The set_state function also contains an ownership check. */
964 :
965 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1525-L1526 */
966 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &programdata_account );
967 :
968 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1527-L1530 */
969 0 : programdata_state->discriminant = FD_BPF_STATE_PROGRAM_DATA;
970 0 : programdata_state->inner.program_data.slot = clock_slot;
971 0 : programdata_state->inner.program_data.has_upgrade_authority_address = !!upgrade_authority_address;
972 0 : if( upgrade_authority_address ) programdata_state->inner.program_data.upgrade_authority_address = *upgrade_authority_address;
973 :
974 0 : err = fd_bpf_loader_v3_program_set_state( &programdata_account, programdata_state );
975 0 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
976 0 : return err;
977 0 : }
978 0 : programdata_account.acc->pd_write = 1;
979 :
980 : /* Max msg_sz: 41 - 2 + 20 = 57 < 127 => we can use printf
981 : https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1532-L1536 */
982 0 : fd_log_collector_printf_dangerous_max_127( instr_ctx,
983 0 : "Extended ProgramData account by %u bytes", additional_bytes );
984 :
985 : /* programdata account is dropped when it goes out of scope */
986 :
987 0 : return FD_EXECUTOR_INSTR_SUCCESS;
988 :
989 0 : #undef PROGRAM_DATA_ACCOUNT_INDEX
990 0 : #undef PROGRAM_ACCOUNT_INDEX
991 0 : #undef AUTHORITY_ACCOUNT_INDEX
992 0 : }
993 :
994 : /* https://github.com/anza-xyz/agave/blob/77daab497df191ef485a7ad36ed291c1874596e5/programs/bpf_loader/src/lib.rs#L566-L1444 */
995 : static int
996 3710 : process_loader_upgradeable_instruction( fd_exec_instr_ctx_t * instr_ctx ) {
997 3710 : fd_bpf_instruction_t instruction[1] = {0};
998 3710 : if( FD_UNLIKELY( fd_bpf_instruction_decode(
999 3710 : instruction,
1000 3710 : instr_ctx->instr->data,
1001 3710 : instr_ctx->instr->data_sz,
1002 3710 : FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, unbound_loader_v3_instruction_data ) ) ) ) {
1003 51 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
1004 51 : }
1005 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/programs/bpf_loader/src/lib.rs#L510 */
1006 3659 : fd_pubkey_t const * program_id = NULL;
1007 3659 : int err = fd_exec_instr_ctx_get_last_program_key( instr_ctx, &program_id );
1008 3659 : if( FD_UNLIKELY( err ) ) {
1009 0 : return err;
1010 0 : }
1011 :
1012 3659 : switch( instruction->discriminant ) {
1013 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L476-L493 */
1014 311 : case FD_BPF_INSTR_INITIALIZE_BUFFER: {
1015 311 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 2U ) ) ) {
1016 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
1017 0 : }
1018 :
1019 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L479 */
1020 311 : fd_guarded_borrowed_account_t buffer = {0};
1021 311 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &buffer );
1022 :
1023 311 : fd_bpf_state_t buffer_state[1];
1024 311 : err = fd_bpf_loader_program_get_state( buffer.acc, buffer_state );
1025 311 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1026 0 : return err;
1027 0 : }
1028 :
1029 311 : if( FD_UNLIKELY( buffer_state->discriminant!=FD_BPF_STATE_UNINITIALIZED ) ) {
1030 0 : fd_log_collector_msg_literal( instr_ctx, "Buffer account already initialized" );
1031 0 : return FD_EXECUTOR_INSTR_ERR_ACC_ALREADY_INITIALIZED;
1032 0 : }
1033 :
1034 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L487-L489 */
1035 311 : fd_pubkey_t const * authority_key = NULL;
1036 311 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 1UL, &authority_key );
1037 311 : if( FD_UNLIKELY( err ) ) {
1038 0 : return err;
1039 0 : }
1040 :
1041 311 : buffer_state->discriminant = FD_BPF_STATE_BUFFER;
1042 311 : buffer_state->inner.buffer.has_authority_address = 1;
1043 311 : buffer_state->inner.buffer.authority_address = *authority_key;
1044 :
1045 311 : err = fd_bpf_loader_v3_program_set_state( &buffer, buffer_state );
1046 311 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1047 298 : return err;
1048 298 : }
1049 :
1050 : /* implicit drop of buffer account */
1051 :
1052 13 : break;
1053 311 : }
1054 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L494-L525 */
1055 278 : case FD_BPF_INSTR_WRITE: {
1056 278 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 2U ) ) ) {
1057 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
1058 0 : }
1059 :
1060 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L497 */
1061 278 : fd_guarded_borrowed_account_t buffer = {0};
1062 278 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &buffer );
1063 :
1064 278 : fd_bpf_state_t loader_state[1];
1065 278 : err = fd_bpf_loader_program_get_state( buffer.acc, loader_state );
1066 278 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1067 16 : return err;
1068 16 : }
1069 :
1070 262 : if( loader_state->discriminant==FD_BPF_STATE_BUFFER ) {
1071 261 : if( FD_UNLIKELY( !loader_state->inner.buffer.has_authority_address ) ) {
1072 0 : fd_log_collector_msg_literal( instr_ctx, "Buffer is immutable" );
1073 0 : return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
1074 0 : }
1075 :
1076 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L505-L507 */
1077 261 : fd_pubkey_t const * authority_key = NULL;
1078 261 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 1UL, &authority_key );
1079 261 : if( FD_UNLIKELY( err ) ) {
1080 0 : return err;
1081 0 : }
1082 :
1083 261 : if( FD_UNLIKELY( !fd_pubkey_eq( &loader_state->inner.buffer.authority_address, authority_key ) ) ) {
1084 1 : fd_log_collector_msg_literal( instr_ctx, "Incorrect buffer authority provided" );
1085 1 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
1086 1 : }
1087 260 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 1UL, &err ) ) ) {
1088 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
1089 1 : if( FD_UNLIKELY( !!err ) ) return err;
1090 1 : fd_log_collector_msg_literal( instr_ctx, "Buffer authority did not sign" );
1091 1 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
1092 1 : }
1093 260 : } else {
1094 1 : fd_log_collector_msg_literal( instr_ctx, "Invalid Buffer account" );
1095 1 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1096 1 : }
1097 :
1098 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L520 */
1099 259 : fd_borrowed_account_drop( &buffer );
1100 :
1101 259 : ulong program_data_offset = fd_ulong_sat_add( BUFFER_METADATA_SIZE, instruction->inner.write.offset );
1102 259 : err = write_program_data( instr_ctx,
1103 259 : 0UL,
1104 259 : program_data_offset,
1105 259 : instruction->inner.write.bytes,
1106 259 : instruction->inner.write.bytes_len );
1107 259 : if( FD_UNLIKELY( err ) ) {
1108 1 : return err;
1109 1 : }
1110 :
1111 258 : break;
1112 259 : }
1113 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L526-L702 */
1114 360 : case FD_BPF_INSTR_DEPLOY_WITH_MAX_DATA_LEN: {
1115 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L527-L541 */
1116 360 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 4U ) ) ) {
1117 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
1118 0 : }
1119 :
1120 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L529-L534 */
1121 360 : fd_pubkey_t const * payer_key = NULL;
1122 360 : fd_pubkey_t const * programdata_key = NULL;
1123 :
1124 360 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 0UL, &payer_key );
1125 360 : if( FD_UNLIKELY( err ) ) {
1126 0 : return err;
1127 0 : }
1128 :
1129 360 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 1UL, &programdata_key );
1130 360 : if( FD_UNLIKELY( err ) ) {
1131 0 : return err;
1132 0 : }
1133 :
1134 360 : err = fd_sysvar_instr_acct_check( instr_ctx, 4UL, &fd_sysvar_rent_id );
1135 360 : if( FD_UNLIKELY( err ) ) {
1136 2 : return err;
1137 2 : }
1138 :
1139 358 : fd_rent_t rent_;
1140 358 : fd_rent_t const * rent = fd_sysvar_cache_rent_read( instr_ctx->sysvar_cache, &rent_ );
1141 358 : if( FD_UNLIKELY( !rent ) ) {
1142 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
1143 0 : }
1144 :
1145 358 : err = fd_sysvar_instr_acct_check( instr_ctx, 5UL, &fd_sysvar_clock_id );
1146 358 : if( FD_UNLIKELY( err ) ) {
1147 22 : return err;
1148 22 : }
1149 :
1150 336 : fd_sol_sysvar_clock_t clock_;
1151 336 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( instr_ctx->sysvar_cache, &clock_ );
1152 336 : if( FD_UNLIKELY( !clock ) ) {
1153 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
1154 0 : }
1155 :
1156 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L538 */
1157 336 : if( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 8U ) ) {
1158 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
1159 0 : }
1160 :
1161 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L539-L541 */
1162 336 : fd_pubkey_t const * authority_key = NULL;
1163 336 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 7UL, &authority_key );
1164 336 : if( FD_UNLIKELY( err ) ) return err;
1165 :
1166 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L542-L560 */
1167 : /* Verify Program account */
1168 336 : fd_pubkey_t const * new_program_id = NULL;
1169 :
1170 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L545 */
1171 336 : fd_guarded_borrowed_account_t program = {0};
1172 336 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &program );
1173 :
1174 336 : fd_bpf_state_t loader_state[1];
1175 336 : int err = fd_bpf_loader_program_get_state( program.acc, loader_state );
1176 336 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1177 0 : return err;
1178 0 : }
1179 336 : if( FD_UNLIKELY( loader_state->discriminant!=FD_BPF_STATE_UNINITIALIZED ) ) {
1180 0 : fd_log_collector_msg_literal( instr_ctx, "Program account already initialized" );
1181 0 : return FD_EXECUTOR_INSTR_ERR_ACC_ALREADY_INITIALIZED;
1182 0 : }
1183 336 : if( FD_UNLIKELY( fd_borrowed_account_get_data_len( &program )<SIZE_OF_PROGRAM ) ) {
1184 0 : fd_log_collector_msg_literal( instr_ctx, "Program account too small" );
1185 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
1186 0 : }
1187 336 : if( FD_UNLIKELY( fd_borrowed_account_get_lamports( &program )<
1188 336 : fd_rent_exempt_minimum_balance( rent, fd_borrowed_account_get_data_len( &program ) ) ) ) {
1189 0 : fd_log_collector_msg_literal( instr_ctx, "Program account not rent-exempt" );
1190 0 : return FD_EXECUTOR_INSTR_ERR_EXECUTABLE_ACCOUNT_NOT_RENT_EXEMPT;
1191 0 : }
1192 336 : new_program_id = (fd_pubkey_t*)program.acc->pubkey;
1193 :
1194 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L560 */
1195 336 : fd_borrowed_account_drop( &program );
1196 :
1197 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L561-L600 */
1198 : /* Verify Buffer account */
1199 :
1200 336 : fd_pubkey_t const* buffer_key = NULL;
1201 336 : ulong buffer_data_offset = 0UL;
1202 336 : ulong buffer_data_len = 0UL;
1203 336 : ulong programdata_len = 0UL;
1204 :
1205 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L564-L565 */
1206 336 : fd_guarded_borrowed_account_t buffer = {0};
1207 336 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 3UL, &buffer );
1208 :
1209 : /* https://github.com/anza-xyz/agave/blob/645f638832e19269d7d1c8614bed3b3b9badb158/programs/bpf_loader/src/lib.rs#L234-L241 */
1210 336 : if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &buffer ) ) ) {
1211 1 : fd_log_collector_msg_literal( instr_ctx, "Buffer account not writeable" );
1212 1 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1213 1 : }
1214 :
1215 335 : if( FD_UNLIKELY( memcmp( program_id, fd_borrowed_account_get_owner( &buffer ), sizeof(fd_pubkey_t) ) ) ) {
1216 20 : fd_log_collector_msg_literal( instr_ctx, "Buffer account not owned by loader" );
1217 20 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
1218 20 : }
1219 :
1220 315 : fd_bpf_state_t buffer_state[1];
1221 315 : err = fd_bpf_loader_program_get_state( buffer.acc, buffer_state );
1222 315 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1223 54 : return err;
1224 54 : }
1225 :
1226 261 : if( buffer_state->discriminant==FD_BPF_STATE_BUFFER ) {
1227 261 : if( FD_UNLIKELY( (authority_key==NULL) != (!buffer_state->inner.buffer.has_authority_address) ||
1228 261 : (authority_key!=NULL && !fd_pubkey_eq( &buffer_state->inner.buffer.authority_address, authority_key ) ) ) ) {
1229 13 : fd_log_collector_msg_literal( instr_ctx, "Buffer and upgrade authority don't match" );
1230 13 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
1231 13 : }
1232 248 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 7UL, &err ) ) ) {
1233 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
1234 4 : if( FD_UNLIKELY( !!err ) ) return err;
1235 4 : fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
1236 4 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
1237 4 : }
1238 248 : } else {
1239 0 : fd_log_collector_msg_literal( instr_ctx, "Invalid Buffer account" );
1240 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1241 0 : }
1242 244 : buffer_key = (fd_pubkey_t*)buffer.acc->pubkey;
1243 244 : buffer_data_offset = BUFFER_METADATA_SIZE;
1244 244 : buffer_data_len = fd_ulong_sat_sub( fd_borrowed_account_get_data_len( &buffer ), buffer_data_offset );
1245 : /* UpgradeableLoaderState::size_of_program_data( max_data_len ) */
1246 244 : programdata_len = fd_ulong_sat_add( PROGRAMDATA_METADATA_SIZE,
1247 244 : instruction->inner.deploy_with_max_data_len.max_data_len );
1248 :
1249 244 : if( FD_UNLIKELY( fd_borrowed_account_get_data_len( &buffer )<BUFFER_METADATA_SIZE || buffer_data_len==0UL ) ) {
1250 0 : fd_log_collector_msg_literal( instr_ctx, "Buffer account too small" );
1251 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1252 0 : }
1253 :
1254 244 : if( FD_UNLIKELY( instruction->inner.deploy_with_max_data_len.max_data_len<buffer_data_len ) ) {
1255 0 : fd_log_collector_msg_literal( instr_ctx, "Max data length is too small to hold Buffer data" );
1256 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
1257 0 : }
1258 :
1259 244 : if( FD_UNLIKELY( programdata_len>MAX_PERMITTED_DATA_LENGTH ) ) {
1260 0 : fd_log_collector_msg_literal( instr_ctx, "Max data length is too large" );
1261 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1262 0 : }
1263 :
1264 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L590 */
1265 244 : fd_borrowed_account_drop( &buffer );
1266 :
1267 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L602-L608 */
1268 : /* Create ProgramData account */
1269 :
1270 244 : fd_pubkey_t derived_address[ 1UL ];
1271 244 : uchar const * seeds[ 1UL ];
1272 244 : seeds[ 0UL ] = (uchar const *)new_program_id;
1273 244 : ulong seed_sz = sizeof(fd_pubkey_t);
1274 244 : uchar bump_seed = 0;
1275 244 : err = fd_pubkey_find_program_address( program_id, 1UL, seeds, &seed_sz, derived_address,
1276 244 : &bump_seed, &instr_ctx->txn_out->err.custom_err );
1277 244 : if( FD_UNLIKELY( err ) ) {
1278 : /* TODO: We should handle these errors more gracefully instead of just killing the client (e.g. excluding the transaction
1279 : from the block). */
1280 0 : FD_LOG_ERR(( "Unable to find a viable program address bump seed" )); // Solana panics, error code is undefined
1281 0 : return err;
1282 0 : }
1283 244 : if( FD_UNLIKELY( memcmp( derived_address, programdata_key, sizeof(fd_pubkey_t) ) ) ) {
1284 1 : fd_log_collector_msg_literal( instr_ctx, "ProgramData address is not derived" );
1285 1 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1286 1 : }
1287 :
1288 : /* Drain the Buffer account to payer before paying for programdata account in a local scope
1289 : https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L612-L628 */
1290 :
1291 243 : do {
1292 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L615 */
1293 243 : fd_guarded_borrowed_account_t payer = {0};
1294 243 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &payer );
1295 :
1296 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L613 */
1297 243 : fd_guarded_borrowed_account_t buffer = {0};
1298 243 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 3UL, &buffer );
1299 :
1300 243 : err = fd_borrowed_account_checked_add_lamports( &payer, fd_borrowed_account_get_lamports( &buffer ) );
1301 243 : if( FD_UNLIKELY( err ) ) {
1302 0 : return err;
1303 0 : }
1304 243 : err = fd_borrowed_account_set_lamports( &buffer, 0UL );
1305 243 : if( FD_UNLIKELY( err ) ) {
1306 0 : return err;
1307 0 : }
1308 243 : } while (0);
1309 :
1310 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L628-L642 */
1311 : /* Pass an extra account to avoid the overly strict unbalanced instruction error */
1312 : /* Invoke the system program to create the new account */
1313 243 : uchar instr_data[FD_TXN_MTU];
1314 243 : fd_system_program_instruction_t instr;
1315 243 : fd_vm_rust_account_meta_t acct_metas[ 3UL ];
1316 243 : ulong acct_metas_cnt = 0UL;
1317 : /* https://github.com/anza-xyz/agave/blob/v4.5/programs/bpf_loader/src/lib.rs#L348-L363 */
1318 243 : if( FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, loader_v3_allow_prefunded_programdata ) ) {
1319 0 : fd_guarded_borrowed_account_t programdata = {0};
1320 0 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 1UL, &programdata );
1321 0 : ulong top_up_lamports = fd_ulong_sat_sub( fd_rent_exempt_minimum_balance( rent, programdata_len ),
1322 0 : fd_borrowed_account_get_lamports( &programdata ) );
1323 0 : instr = (fd_system_program_instruction_t) {
1324 0 : .discriminant = FD_SYSTEM_PROGRAM_INSTR_CREATE_ACCOUNT_ALLOW_PREFUND,
1325 0 : .inner = {
1326 0 : .create_account_allow_prefund = {
1327 0 : .lamports = top_up_lamports,
1328 0 : .space = programdata_len,
1329 0 : .owner = *program_id,
1330 0 : },
1331 0 : }
1332 0 : };
1333 0 : fd_native_cpi_create_account_meta( programdata_key, 1U, 1U, &acct_metas[ acct_metas_cnt++ ] );
1334 0 : if( top_up_lamports>0UL ) {
1335 0 : fd_native_cpi_create_account_meta( payer_key, 1U, 1U, &acct_metas[ acct_metas_cnt++ ] );
1336 0 : }
1337 243 : } else {
1338 243 : create_account_t create_acct = {
1339 243 : .lamports = fd_rent_exempt_minimum_balance( rent, programdata_len ),
1340 243 : .space = programdata_len,
1341 243 : .owner = *program_id,
1342 243 : };
1343 243 : if( !create_acct.lamports ) {
1344 0 : create_acct.lamports = 1UL;
1345 0 : }
1346 :
1347 243 : instr = (fd_system_program_instruction_t) {
1348 243 : .discriminant = FD_SYSTEM_PROGRAM_INSTR_CREATE_ACCOUNT,
1349 243 : .inner = {
1350 243 : .create_account = create_acct,
1351 243 : }
1352 243 : };
1353 243 : fd_native_cpi_create_account_meta( payer_key, 1U, 1U, &acct_metas[ acct_metas_cnt++ ] );
1354 243 : fd_native_cpi_create_account_meta( programdata_key, 1U, 1U, &acct_metas[ acct_metas_cnt++ ] );
1355 243 : }
1356 243 : fd_native_cpi_create_account_meta( buffer_key, 0U, 1U, &acct_metas[ acct_metas_cnt++ ] );
1357 :
1358 243 : ulong instr_data_sz;
1359 243 : err = fd_system_program_instruction_encode( &instr, instr_data, FD_TXN_MTU, &instr_data_sz );
1360 243 : if( FD_UNLIKELY( err ) ) {
1361 0 : FD_LOG_CRIT(( "Failed to encode SystemInstruction with error %d", err ));
1362 0 : }
1363 :
1364 : /* caller_program_id == program_id */
1365 243 : fd_pubkey_t signers[ 1UL ];
1366 243 : err = fd_pubkey_derive_pda( program_id, 1UL, seeds, &seed_sz, &bump_seed, signers, &instr_ctx->txn_out->err.custom_err );
1367 243 : if( FD_UNLIKELY( err ) ) {
1368 0 : return err;
1369 0 : }
1370 243 : err = fd_native_cpi_native_invoke( instr_ctx,
1371 243 : &fd_solana_system_program_id,
1372 243 : instr_data,
1373 243 : instr_data_sz,
1374 243 : acct_metas,
1375 243 : acct_metas_cnt,
1376 243 : signers,
1377 243 : 1UL );
1378 243 : if( FD_UNLIKELY( err ) ) {
1379 0 : return err;
1380 0 : }
1381 :
1382 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L644-L665 */
1383 : /* Load and verify the program bits */
1384 :
1385 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L648-L649 */
1386 243 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 3UL, &buffer );
1387 :
1388 243 : if( FD_UNLIKELY( buffer_data_offset>fd_borrowed_account_get_data_len( &buffer ) ) ) {
1389 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
1390 0 : }
1391 :
1392 243 : const uchar * buffer_data = fd_borrowed_account_get_data( &buffer ) + buffer_data_offset;
1393 :
1394 243 : err = fd_deploy_program( instr_ctx, buffer_data, buffer_data_len,
1395 243 : FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, disable_sbpf_v0_v1_v2_deployment ) );
1396 243 : if( FD_UNLIKELY( err ) ) {
1397 69 : return err;
1398 69 : }
1399 :
1400 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L657 */
1401 174 : fd_borrowed_account_drop( &buffer );
1402 :
1403 : /* Update the ProgramData account and record the program bits in a local scope
1404 : https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L669-L691 */
1405 174 : do {
1406 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L670-L671 */
1407 174 : fd_guarded_borrowed_account_t programdata = {0};
1408 174 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 1UL, &programdata );
1409 :
1410 174 : fd_bpf_state_t programdata_loader_state = {
1411 174 : .discriminant = FD_BPF_STATE_PROGRAM_DATA,
1412 174 : .inner.program_data = {
1413 174 : .slot = clock->slot,
1414 174 : .has_upgrade_authority_address = !!authority_key,
1415 174 : .upgrade_authority_address = authority_key ? *authority_key : (fd_pubkey_t){{0}},
1416 174 : },
1417 174 : };
1418 174 : err = fd_bpf_loader_v3_program_set_state( &programdata, &programdata_loader_state );
1419 174 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1420 0 : return err;
1421 0 : }
1422 174 : programdata.acc->pd_write = 1;
1423 :
1424 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L675-L689 */
1425 174 : if( FD_UNLIKELY( PROGRAMDATA_METADATA_SIZE+buffer_data_len>fd_borrowed_account_get_data_len( &programdata ) ) ) {
1426 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
1427 0 : }
1428 :
1429 174 : uchar * programdata_data = NULL;
1430 174 : ulong programdata_dlen = 0UL;
1431 174 : err = fd_borrowed_account_get_data_mut( &programdata, &programdata_data, &programdata_dlen );
1432 174 : if( FD_UNLIKELY( err ) ) {
1433 0 : return err;
1434 0 : }
1435 :
1436 174 : uchar * dst_slice = programdata_data + PROGRAMDATA_METADATA_SIZE;
1437 174 : ulong dst_slice_len = buffer_data_len;
1438 :
1439 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L683-L684 */
1440 174 : fd_guarded_borrowed_account_t buffer = {0};
1441 174 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 3UL, &buffer );
1442 :
1443 174 : if( FD_UNLIKELY( buffer_data_offset>fd_borrowed_account_get_data_len( &buffer ) ) ) {
1444 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
1445 0 : }
1446 174 : const uchar * src_slice = fd_borrowed_account_get_data( &buffer ) + buffer_data_offset;
1447 174 : fd_memcpy( dst_slice, src_slice, dst_slice_len );
1448 : /* Update buffer data length.
1449 : BUFFER_METADATA_SIZE == UpgradeableLoaderState::size_of_buffer(0) */
1450 174 : err = fd_borrowed_account_set_data_length( &buffer, BUFFER_METADATA_SIZE );
1451 174 : if( FD_UNLIKELY( err ) ) {
1452 0 : return err;
1453 0 : }
1454 174 : } while(0);
1455 :
1456 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L692-L699 */
1457 :
1458 : /* Update the Program account
1459 : https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L694-L695 */
1460 174 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &program );
1461 :
1462 174 : loader_state->discriminant = FD_BPF_STATE_PROGRAM;
1463 174 : loader_state->inner.program.programdata_address = *programdata_key;
1464 174 : err = fd_bpf_loader_v3_program_set_state( &program, loader_state );
1465 174 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1466 0 : return err;
1467 0 : }
1468 174 : err = fd_borrowed_account_set_executable( &program, 1 );
1469 174 : if( FD_UNLIKELY( err ) ) {
1470 0 : return err;
1471 0 : }
1472 :
1473 174 : FD_BASE58_ENCODE_32_BYTES( program.acc->pubkey, program_b58 );
1474 174 : FD_LOG_INFO(( "Program deployed %s", program_b58 ));
1475 :
1476 : /* Max msg_sz: 19 - 2 + 45 = 62 < 127 => we can use printf */
1477 174 : fd_log_collector_printf_dangerous_max_127( instr_ctx, "Deployed program %s", program_b58 );
1478 :
1479 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L700 */
1480 174 : fd_borrowed_account_drop( &program );
1481 :
1482 174 : break;
1483 174 : }
1484 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L703-L891 */
1485 496 : case FD_BPF_INSTR_UPGRADE: {
1486 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L704-L714 */
1487 496 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 3U ) ) ) {
1488 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
1489 0 : }
1490 :
1491 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L706-L708 */
1492 496 : fd_pubkey_t const * programdata_key = NULL;
1493 496 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 0UL, &programdata_key );
1494 496 : if( FD_UNLIKELY( err ) ) {
1495 0 : return err;
1496 0 : }
1497 :
1498 : /* rent is accessed directly from the epoch bank and the clock from the
1499 : slot context. However, a check must be done to make sure that the
1500 : sysvars are correctly included in the set of transaction accounts. */
1501 496 : err = fd_sysvar_instr_acct_check( instr_ctx, 4UL, &fd_sysvar_rent_id );
1502 496 : if( FD_UNLIKELY( err ) ) {
1503 1 : return err;
1504 1 : }
1505 :
1506 495 : fd_rent_t rent_;
1507 495 : fd_rent_t const * rent = fd_sysvar_cache_rent_read( instr_ctx->sysvar_cache, &rent_ );
1508 495 : if( FD_UNLIKELY( !rent ) ) {
1509 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
1510 0 : }
1511 :
1512 495 : err = fd_sysvar_instr_acct_check( instr_ctx, 5UL, &fd_sysvar_clock_id );
1513 495 : if( FD_UNLIKELY( err ) ) {
1514 25 : return err;
1515 25 : }
1516 :
1517 470 : fd_sol_sysvar_clock_t clock_;
1518 470 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( instr_ctx->sysvar_cache, &clock_ );
1519 470 : if( FD_UNLIKELY( !clock ) ) {
1520 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
1521 0 : }
1522 :
1523 470 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 7U ) ) ) {
1524 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
1525 0 : }
1526 :
1527 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L713-L715 */
1528 470 : fd_pubkey_t const * authority_key = NULL;
1529 470 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 6UL, &authority_key );
1530 470 : if( FD_UNLIKELY( err ) ) return err;
1531 :
1532 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L716-L745 */
1533 : /* Verify Program account */
1534 :
1535 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L719-L720 */
1536 470 : fd_guarded_borrowed_account_t program = {0};
1537 470 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 1UL, &program );
1538 :
1539 470 : if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &program ) ) ) {
1540 0 : fd_log_collector_msg_literal( instr_ctx, "Program account not writeable" );
1541 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1542 0 : }
1543 470 : if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &program ), program_id, sizeof(fd_pubkey_t) ) ) ) {
1544 0 : fd_log_collector_msg_literal( instr_ctx, "Program account not owned by loader" );
1545 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
1546 0 : }
1547 470 : fd_bpf_state_t program_state[1];
1548 470 : err = fd_bpf_loader_program_get_state( program.acc, program_state );
1549 470 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1550 0 : return err;
1551 0 : }
1552 470 : if( FD_UNLIKELY( program_state->discriminant==FD_BPF_STATE_PROGRAM ) ) {
1553 470 : if( FD_UNLIKELY( memcmp( &program_state->inner.program.programdata_address, programdata_key, sizeof(fd_pubkey_t) ) ) ) {
1554 211 : fd_log_collector_msg_literal( instr_ctx, "Program and ProgramData account mismatch" );
1555 211 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1556 211 : }
1557 470 : } else {
1558 0 : fd_log_collector_msg_literal( instr_ctx, "Invalid Program account" );
1559 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1560 0 : }
1561 :
1562 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L746 */
1563 259 : fd_pubkey_t new_program_id = *(fd_pubkey_t*)program.acc->pubkey;
1564 259 : fd_borrowed_account_drop( &program );
1565 :
1566 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L747-L773 */
1567 : /* Verify Buffer account */
1568 :
1569 259 : ulong buffer_lamports = 0UL;
1570 259 : ulong buffer_data_offset = 0UL;
1571 259 : ulong buffer_data_len = 0UL;
1572 :
1573 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L750-L751 */
1574 259 : fd_guarded_borrowed_account_t buffer = {0};
1575 259 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &buffer );
1576 :
1577 : /* https://github.com/anza-xyz/agave/blob/645f638832e19269d7d1c8614bed3b3b9badb158/programs/bpf_loader/src/lib.rs#L407-L414 */
1578 259 : if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &buffer ) ) ) {
1579 0 : fd_log_collector_msg_literal( instr_ctx, "Buffer account not writeable" );
1580 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1581 0 : }
1582 :
1583 259 : if( FD_UNLIKELY( memcmp( program_id, fd_borrowed_account_get_owner( &buffer ), sizeof(fd_pubkey_t) ) ) ) {
1584 0 : fd_log_collector_msg_literal( instr_ctx, "Buffer account not owned by loader" );
1585 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
1586 0 : }
1587 :
1588 259 : fd_bpf_state_t buffer_state[1];
1589 259 : err = fd_bpf_loader_program_get_state( buffer.acc, buffer_state );
1590 259 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1591 0 : return err;
1592 0 : }
1593 259 : if( buffer_state->discriminant==FD_BPF_STATE_BUFFER ) {
1594 259 : if( FD_UNLIKELY( (authority_key==NULL) != (!buffer_state->inner.buffer.has_authority_address) ||
1595 259 : (authority_key!=NULL && !fd_pubkey_eq( &buffer_state->inner.buffer.authority_address, authority_key ) ) ) ) {
1596 5 : fd_log_collector_msg_literal( instr_ctx, "Buffer and upgrade authority don't match" );
1597 5 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
1598 5 : }
1599 254 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 6UL, &err ) ) ) {
1600 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
1601 5 : if( FD_UNLIKELY( !!err ) ) return err;
1602 5 : fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
1603 5 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
1604 5 : }
1605 254 : } else {
1606 0 : fd_log_collector_msg_literal( instr_ctx, "Invalid Buffer account" );
1607 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1608 0 : }
1609 249 : buffer_lamports = fd_borrowed_account_get_lamports( &buffer );
1610 249 : buffer_data_offset = BUFFER_METADATA_SIZE;
1611 249 : buffer_data_len = fd_ulong_sat_sub( fd_borrowed_account_get_data_len( &buffer ), buffer_data_offset );
1612 249 : if( FD_UNLIKELY( fd_borrowed_account_get_data_len( &buffer )<BUFFER_METADATA_SIZE || buffer_data_len==0UL ) ) {
1613 0 : fd_log_collector_msg_literal( instr_ctx, "Buffer account too small" );
1614 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1615 0 : }
1616 :
1617 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L774 */
1618 249 : fd_borrowed_account_drop( &buffer );
1619 :
1620 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L775-L823 */
1621 : /* Verify ProgramData account */
1622 :
1623 249 : ulong programdata_data_offset = PROGRAMDATA_METADATA_SIZE;
1624 249 : ulong programdata_len = 0UL;
1625 249 : ulong programdata_balance_required = 0UL;
1626 :
1627 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L778-L779 */
1628 249 : fd_guarded_borrowed_account_t programdata = {0};
1629 249 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &programdata );
1630 :
1631 : /* SIMD-0433: ProgramData account is resized to hold the new ELF */
1632 249 : err = fd_bpf_loader_v3_upgrade_sizing(
1633 249 : FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, loader_v3_set_program_data_to_elf_length ),
1634 249 : fd_borrowed_account_get_data_len( &programdata ),
1635 249 : buffer_data_len,
1636 249 : rent,
1637 249 : &programdata_len,
1638 249 : &programdata_balance_required );
1639 249 : if( FD_UNLIKELY( err==FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA ) ) {
1640 0 : fd_log_collector_printf_dangerous_max_127( instr_ctx,
1641 0 : "Resized ProgramData length of %lu bytes exceeds max account data length", programdata_len );
1642 0 : return err;
1643 0 : }
1644 249 : if( FD_UNLIKELY( err==FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL ) ) {
1645 0 : fd_log_collector_msg_literal( instr_ctx, "ProgramData account not large enough" );
1646 0 : return err;
1647 0 : }
1648 249 : if( FD_UNLIKELY( fd_ulong_sat_add( fd_borrowed_account_get_lamports( &programdata ), buffer_lamports )<programdata_balance_required ) ) {
1649 0 : fd_log_collector_msg_literal( instr_ctx, "Buffer account balance too low to fund upgrade" );
1650 0 : return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
1651 0 : }
1652 :
1653 249 : fd_bpf_state_t programdata_state[1];
1654 249 : err = fd_bpf_loader_program_get_state( programdata.acc, programdata_state );
1655 249 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1656 0 : return err;
1657 0 : }
1658 :
1659 249 : if( programdata_state->discriminant==FD_BPF_STATE_PROGRAM_DATA ) {
1660 248 : if( FD_UNLIKELY( clock->slot==programdata_state->inner.program_data.slot ) ) {
1661 241 : fd_log_collector_msg_literal( instr_ctx, "Program was deployed in this block already" );
1662 241 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1663 241 : }
1664 7 : if( FD_UNLIKELY( !programdata_state->inner.program_data.has_upgrade_authority_address ) ) {
1665 0 : fd_log_collector_msg_literal( instr_ctx, "Program not upgradeable" );
1666 0 : return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
1667 0 : }
1668 7 : if( FD_UNLIKELY( !fd_pubkey_eq( &programdata_state->inner.program_data.upgrade_authority_address, authority_key ) ) ) {
1669 0 : fd_log_collector_msg_literal( instr_ctx, "Incorrect upgrade authority provided" );
1670 0 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
1671 0 : }
1672 7 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 6UL, &err ) ) ) {
1673 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
1674 0 : if( FD_UNLIKELY( !!err ) ) return err;
1675 0 : fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
1676 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
1677 0 : }
1678 7 : } else {
1679 1 : fd_log_collector_msg_literal( instr_ctx, "Invalid ProgramData account" );
1680 1 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
1681 1 : }
1682 :
1683 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L824 */
1684 7 : fd_borrowed_account_drop( &programdata );
1685 :
1686 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L825-L845 */
1687 : /* Load and verify the program bits */
1688 :
1689 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L827-L828 */
1690 7 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &buffer );
1691 :
1692 7 : if( FD_UNLIKELY( buffer_data_offset>fd_borrowed_account_get_data_len( &buffer ) ) ) {
1693 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
1694 0 : }
1695 :
1696 7 : const uchar * buffer_data = fd_borrowed_account_get_data( &buffer ) + buffer_data_offset;
1697 7 : err = fd_deploy_program( instr_ctx, buffer_data, buffer_data_len,
1698 7 : FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, disable_sbpf_v0_v1_v2_deployment ) );
1699 7 : if( FD_UNLIKELY( err ) ) {
1700 3 : return err;
1701 3 : }
1702 :
1703 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L836 */
1704 4 : fd_borrowed_account_drop( &buffer );
1705 :
1706 : /* https://github.com/anza-xyz/agave/blob/v4.5.0-beta.0/programs/bpf_loader/src/lib.rs#L614 */
1707 4 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &programdata );
1708 :
1709 : /* https://github.com/anza-xyz/agave/blob/v4.5.0-beta.0/programs/bpf_loader/src/lib.rs#L613-L636 */
1710 : /* Update the ProgramData account, record the upgraded data, and zero the rest in a local scope */
1711 4 : do {
1712 : /* Replace the data with a zeroed header before growing it.
1713 : https://github.com/anza-xyz/agave/blob/v4.5.0-beta.0/programs/bpf_loader/src/lib.rs#L623-L625 */
1714 4 : uchar const zero_header[ PROGRAMDATA_METADATA_SIZE ] = {0};
1715 4 : err = fd_borrowed_account_set_data_from_slice( &programdata, zero_header, PROGRAMDATA_METADATA_SIZE );
1716 4 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1717 0 : return err;
1718 0 : }
1719 :
1720 : /* https://github.com/anza-xyz/agave/blob/v4.5.0-beta.0/programs/bpf_loader/src/lib.rs#L627-L631 */
1721 4 : err = fd_borrowed_account_set_data_length( &programdata, programdata_len );
1722 4 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1723 0 : return err;
1724 0 : }
1725 :
1726 4 : programdata_state->discriminant = FD_BPF_STATE_PROGRAM_DATA;
1727 4 : programdata_state->inner.program_data.slot = clock->slot;
1728 4 : programdata_state->inner.program_data.has_upgrade_authority_address = 1;
1729 4 : programdata_state->inner.program_data.upgrade_authority_address = *authority_key;
1730 4 : err = fd_bpf_loader_v3_program_set_state( &programdata, programdata_state );
1731 4 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1732 0 : return err;
1733 0 : }
1734 4 : programdata.acc->pd_write = 1;
1735 :
1736 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L846-L875 */
1737 : /* We want to copy over the data and zero out the rest */
1738 4 : if( FD_UNLIKELY( programdata_data_offset+buffer_data_len>fd_borrowed_account_get_data_len( &programdata ) ) ) {
1739 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
1740 0 : }
1741 :
1742 4 : uchar * programdata_data = NULL;
1743 4 : ulong programdata_dlen = 0UL;
1744 4 : err = fd_borrowed_account_get_data_mut( &programdata, &programdata_data, &programdata_dlen );
1745 4 : if( FD_UNLIKELY( err ) ) {
1746 0 : return err;
1747 0 : }
1748 4 : uchar * dst_slice = programdata_data + programdata_data_offset;
1749 4 : ulong dst_slice_len = buffer_data_len;
1750 :
1751 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L863-L864 */
1752 4 : fd_guarded_borrowed_account_t buffer = {0};
1753 4 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &buffer );
1754 :
1755 4 : if( FD_UNLIKELY( buffer_data_offset>fd_borrowed_account_get_data_len( &buffer ) ) ){
1756 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
1757 0 : }
1758 :
1759 4 : const uchar * src_slice = fd_borrowed_account_get_data( &buffer ) + buffer_data_offset;
1760 4 : fd_memcpy( dst_slice, src_slice, dst_slice_len );
1761 4 : fd_memset( dst_slice + dst_slice_len, 0, fd_borrowed_account_get_data_len( &programdata ) - programdata_data_offset - dst_slice_len );
1762 :
1763 : /* implicit drop of buffer */
1764 4 : } while (0);
1765 :
1766 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L876-L891 */
1767 : /* Fund ProgramData to rent-exemption, spill the rest */
1768 :
1769 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L878-L879 */
1770 4 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &buffer );
1771 :
1772 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L880-L881 */
1773 4 : fd_guarded_borrowed_account_t spill = {0};
1774 4 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 3UL, &spill );
1775 :
1776 4 : ulong spill_addend = fd_ulong_sat_sub( fd_ulong_sat_add( fd_borrowed_account_get_lamports( &programdata ), buffer_lamports ),
1777 4 : programdata_balance_required );
1778 4 : err = fd_borrowed_account_checked_add_lamports( &spill, spill_addend );
1779 4 : if( FD_UNLIKELY( err ) ) {
1780 0 : return err;
1781 0 : }
1782 4 : err = fd_borrowed_account_set_lamports( &buffer, 0UL );
1783 4 : if( FD_UNLIKELY( err ) ) {
1784 0 : return err;
1785 0 : }
1786 4 : err = fd_borrowed_account_set_lamports( &programdata, programdata_balance_required );
1787 4 : if( FD_UNLIKELY( err ) ) {
1788 0 : return err;
1789 0 : }
1790 :
1791 : /* Buffer account set_data_length */
1792 4 : err = fd_borrowed_account_set_data_length( &buffer, BUFFER_METADATA_SIZE );
1793 4 : if( FD_UNLIKELY( err ) ) {
1794 0 : return err;
1795 0 : }
1796 :
1797 : /* buffer is dropped when it goes out of scope */
1798 : /* spill is dropped when it goes out of scope */
1799 : /* programdata is dropped when it goes out of scope */
1800 :
1801 : /* Max msg_sz: 19 - 2 + 45 = 62 < 127 => we can use printf */
1802 4 : FD_BASE58_ENCODE_32_BYTES( new_program_id.uc, program_id_b58 );
1803 4 : fd_log_collector_printf_dangerous_max_127( instr_ctx, "Upgraded program %s", program_id_b58 );
1804 :
1805 4 : break;
1806 4 : }
1807 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L893-L957 */
1808 302 : case FD_BPF_INSTR_SET_AUTHORITY: {
1809 302 : int err;
1810 302 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 2U ) ) ) {
1811 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
1812 0 : }
1813 :
1814 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L896-L897 */
1815 302 : fd_guarded_borrowed_account_t account = {0};
1816 302 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &account );
1817 :
1818 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L898-L900 */
1819 302 : fd_pubkey_t const * present_authority_key = NULL;
1820 302 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 1UL, &present_authority_key );
1821 302 : if( FD_UNLIKELY( err ) ) {
1822 0 : return err;
1823 0 : }
1824 :
1825 : /* Don't check the error here because the new_authority key is allowed to be NULL until further checks.
1826 : https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L901-L906 */
1827 302 : fd_pubkey_t const * new_authority = NULL;
1828 302 : fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 2UL, &new_authority );
1829 :
1830 302 : fd_bpf_state_t account_state[1];
1831 302 : err = fd_bpf_loader_program_get_state( account.acc, account_state );
1832 302 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1833 2 : return err;
1834 2 : }
1835 :
1836 300 : if( account_state->discriminant==FD_BPF_STATE_BUFFER ) {
1837 152 : if( FD_UNLIKELY( !new_authority ) ) {
1838 13 : fd_log_collector_msg_literal( instr_ctx, "Buffer authority is not optional" );
1839 13 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
1840 13 : }
1841 139 : if( FD_UNLIKELY( !account_state->inner.buffer.has_authority_address ) ) {
1842 0 : fd_log_collector_msg_literal( instr_ctx, "Buffer is immutable" );
1843 0 : return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
1844 0 : }
1845 139 : if( FD_UNLIKELY( !fd_pubkey_eq( &account_state->inner.buffer.authority_address, present_authority_key ) ) ) {
1846 1 : fd_log_collector_msg_literal( instr_ctx, "Incorrect buffer authority provided" );
1847 1 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
1848 1 : }
1849 138 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 1UL, &err ) ) ) {
1850 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
1851 3 : if( FD_UNLIKELY( !!err ) ) return err;
1852 3 : fd_log_collector_msg_literal( instr_ctx, "Buffer authority did not sign" );
1853 3 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
1854 3 : }
1855 :
1856 : /* copy in the authority public key into the authority address.
1857 : https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L926-L928 */
1858 135 : account_state->inner.buffer.has_authority_address = !!new_authority;
1859 135 : if( new_authority ) {
1860 135 : account_state->inner.buffer.authority_address = *new_authority;
1861 135 : }
1862 :
1863 135 : err = fd_bpf_loader_v3_program_set_state( &account, account_state );
1864 135 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1865 0 : return err;
1866 0 : }
1867 148 : } else if( account_state->discriminant==FD_BPF_STATE_PROGRAM_DATA ) {
1868 148 : if( FD_UNLIKELY( !account_state->inner.program_data.has_upgrade_authority_address ) ) {
1869 0 : fd_log_collector_msg_literal( instr_ctx, "Program not upgradeable" );
1870 0 : return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
1871 0 : }
1872 148 : if( FD_UNLIKELY( !fd_pubkey_eq( &account_state->inner.program_data.upgrade_authority_address, present_authority_key ) ) ) {
1873 2 : fd_log_collector_msg_literal( instr_ctx, "Incorrect upgrade authority provided" );
1874 2 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
1875 2 : }
1876 146 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 1UL, &err ) ) ) {
1877 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
1878 8 : if( FD_UNLIKELY( !!err ) ) return err;
1879 8 : fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
1880 8 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
1881 8 : }
1882 :
1883 : /* SIMD-0500: forbid finalize (SetAuthority None) on programs
1884 : that are not at least SBPF v3. */
1885 138 : if( !new_authority ) {
1886 12 : int rc = fd_bpf_loader_finalize_v3_check(
1887 12 : FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, disable_sbpf_v0_v1_v2_deployment ),
1888 12 : fd_borrowed_account_get_data ( &account ),
1889 12 : fd_borrowed_account_get_data_len( &account ) );
1890 12 : if( FD_UNLIKELY( rc!=FD_EXECUTOR_INSTR_SUCCESS ) ) return rc;
1891 12 : }
1892 :
1893 : /* copy in the authority public key into the upgrade authority address.
1894 : https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L946-L949 */
1895 138 : account_state->inner.program_data.has_upgrade_authority_address = !!new_authority;
1896 138 : if( new_authority ) {
1897 126 : account_state->inner.program_data.upgrade_authority_address = *new_authority;
1898 126 : }
1899 :
1900 138 : err = fd_bpf_loader_v3_program_set_state( &account, account_state );
1901 138 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1902 0 : return err;
1903 0 : }
1904 138 : } else {
1905 0 : fd_log_collector_msg_literal( instr_ctx, "Account does not support authorities" );
1906 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
1907 0 : }
1908 :
1909 : /* Max msg_sz: 16 - 2 + 45 = 59 < 127 => we can use printf */
1910 273 : if( new_authority ) {
1911 261 : FD_BASE58_ENCODE_32_BYTES( new_authority->uc, new_authority_b58 );
1912 261 : fd_log_collector_printf_dangerous_max_127( instr_ctx, "New authority Some(%s)", new_authority_b58 );
1913 261 : } else {
1914 12 : fd_log_collector_printf_dangerous_max_127( instr_ctx, "New authority None" );
1915 12 : }
1916 :
1917 : /* implicit drop of account */
1918 :
1919 273 : break;
1920 300 : }
1921 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L958-L1030 */
1922 295 : case FD_BPF_INSTR_SET_AUTHORITY_CHECKED: {
1923 295 : int err;
1924 295 : if( !FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, enable_bpf_loader_set_authority_checked_ix ) ) {
1925 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
1926 0 : }
1927 :
1928 295 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 3U ) ) ) {
1929 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
1930 0 : }
1931 :
1932 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L968-L969 */
1933 295 : fd_guarded_borrowed_account_t account = {0};
1934 295 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &account );
1935 :
1936 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L970-L975 */
1937 295 : fd_pubkey_t const * present_authority_key = NULL;
1938 295 : fd_pubkey_t const * new_authority_key = NULL;
1939 :
1940 295 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 1UL, &present_authority_key );
1941 295 : if( FD_UNLIKELY( err ) ) return err;
1942 :
1943 295 : err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 2UL, &new_authority_key );
1944 295 : if( FD_UNLIKELY( err ) ) return err;
1945 :
1946 295 : fd_bpf_state_t account_state[1];
1947 295 : err = fd_bpf_loader_program_get_state( account.acc, account_state );
1948 295 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1949 7 : return err;
1950 7 : }
1951 :
1952 288 : if( account_state->discriminant==FD_BPF_STATE_BUFFER ) {
1953 135 : if( FD_UNLIKELY( !account_state->inner.buffer.has_authority_address ) ) {
1954 0 : fd_log_collector_msg_literal( instr_ctx, "Buffer is immutable" );
1955 0 : return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
1956 0 : }
1957 135 : if( FD_UNLIKELY( !fd_pubkey_eq( &account_state->inner.buffer.authority_address, present_authority_key ) ) ) {
1958 3 : fd_log_collector_msg_literal( instr_ctx, "Incorrect buffer authority provided" );
1959 3 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
1960 3 : }
1961 132 : int instr_err_code = 0;
1962 132 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 1UL, &instr_err_code ) ) ) {
1963 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
1964 3 : if( FD_UNLIKELY( !!instr_err_code ) ) return instr_err_code;
1965 3 : fd_log_collector_msg_literal( instr_ctx, "Buffer authority did not sign" );
1966 3 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
1967 3 : }
1968 129 : instr_err_code = 0;
1969 129 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 2UL, &instr_err_code ) ) ) {
1970 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
1971 0 : if( FD_UNLIKELY( !!instr_err_code ) ) return instr_err_code;
1972 0 : fd_log_collector_msg_literal( instr_ctx, "New authority did not sign" );
1973 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
1974 0 : }
1975 129 : account_state->inner.buffer.has_authority_address = 1;
1976 129 : account_state->inner.buffer.authority_address = *new_authority_key;
1977 129 : err = fd_bpf_loader_v3_program_set_state( &account, account_state );
1978 129 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1979 0 : return err;
1980 0 : }
1981 153 : } else if( account_state->discriminant==FD_BPF_STATE_PROGRAM_DATA ) {
1982 153 : if( FD_UNLIKELY( !account_state->inner.program_data.has_upgrade_authority_address ) ) {
1983 0 : fd_log_collector_msg_literal( instr_ctx, "Program not upgradeable" );
1984 0 : return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
1985 0 : }
1986 153 : if( FD_UNLIKELY( !fd_pubkey_eq( &account_state->inner.program_data.upgrade_authority_address, present_authority_key ) ) ) {
1987 1 : fd_log_collector_msg_literal( instr_ctx, "Incorrect upgrade authority provided" );
1988 1 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
1989 1 : }
1990 152 : int instr_err_code = 0;
1991 152 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 1UL, &instr_err_code ) ) ) {
1992 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
1993 3 : if( FD_UNLIKELY( !!instr_err_code ) ) return instr_err_code;
1994 3 : fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
1995 3 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
1996 3 : }
1997 149 : instr_err_code = 0;
1998 149 : if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 2UL, &instr_err_code ) ) ) {
1999 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
2000 1 : if( FD_UNLIKELY( !!instr_err_code ) ) return instr_err_code;
2001 1 : fd_log_collector_msg_literal( instr_ctx, "New authority did not sign" );
2002 1 : return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
2003 1 : }
2004 148 : account_state->inner.program_data.has_upgrade_authority_address = 1;
2005 148 : account_state->inner.program_data.upgrade_authority_address = *new_authority_key;
2006 148 : err = fd_bpf_loader_v3_program_set_state( &account, account_state );
2007 148 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
2008 0 : return err;
2009 0 : }
2010 148 : } else {
2011 0 : fd_log_collector_msg_literal( instr_ctx, "Account does not support authorities" );
2012 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2013 0 : }
2014 :
2015 : /* Max msg_sz: 16 - 2 + 45 = 59 < 127 => we can use printf */
2016 277 : FD_BASE58_ENCODE_32_BYTES( new_authority_key->uc, new_authority_b58 );
2017 277 : fd_log_collector_printf_dangerous_max_127( instr_ctx, "New authority %s", new_authority_b58 );
2018 :
2019 : /* implicit drop of account */
2020 :
2021 277 : break;
2022 288 : }
2023 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1031-L1134 */
2024 334 : case FD_BPF_INSTR_CLOSE: {
2025 334 : int err;
2026 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1032-L1046 */
2027 334 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 2U ) ) ) {
2028 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2029 0 : }
2030 :
2031 : /* It's safe to directly access the instruction accounts because we already checked for two
2032 : instruction accounts previously.
2033 : https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L1034-L1035 */
2034 334 : if( FD_UNLIKELY( instr_ctx->instr->accounts[ 0UL ].index_in_transaction ==
2035 334 : instr_ctx->instr->accounts[ 1UL ].index_in_transaction ) ) {
2036 0 : fd_log_collector_msg_literal( instr_ctx, "Recipient is the same as the account being closed" );
2037 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2038 0 : }
2039 :
2040 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1043-L1044 */
2041 334 : fd_guarded_borrowed_account_t close_account = {0};
2042 334 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &close_account );
2043 :
2044 334 : fd_pubkey_t const * close_key = (fd_pubkey_t*)close_account.acc->pubkey;
2045 334 : fd_bpf_state_t close_account_state[1];
2046 334 : err = fd_bpf_loader_program_get_state( close_account.acc, close_account_state );
2047 334 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
2048 2 : return err;
2049 2 : }
2050 :
2051 : /* https://github.com/anza-xyz/agave/blob/v4.4.0-beta.0/programs/bpf_loader/src/lib.rs#L719-L728 */
2052 332 : if( close_account_state->discriminant==FD_BPF_STATE_UNINITIALIZED ) {
2053 :
2054 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1050-L1051 */
2055 86 : fd_guarded_borrowed_account_t recipient_account = {0};
2056 86 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 1UL, &recipient_account );
2057 :
2058 86 : err = fd_borrowed_account_checked_add_lamports( &recipient_account, fd_borrowed_account_get_lamports( &close_account ) );
2059 86 : if( FD_UNLIKELY( err ) ) {
2060 1 : return err;
2061 1 : }
2062 85 : err = fd_borrowed_account_set_lamports( &close_account, 0UL );
2063 85 : if( FD_UNLIKELY( err ) ) {
2064 1 : return err;
2065 1 : }
2066 84 : err = fd_borrowed_account_set_data_length( &close_account, SIZE_OF_UNINITIALIZED );
2067 84 : if( FD_UNLIKELY( err ) ) {
2068 0 : return err;
2069 0 : }
2070 : /* Max msg_sz: 23 - 2 + 45 = 66 < 127 => we can use printf */
2071 84 : FD_BASE58_ENCODE_32_BYTES( close_key->uc, close_key_b58 );
2072 84 : fd_log_collector_printf_dangerous_max_127( instr_ctx, "Closed Uninitialized %s", close_key_b58 );
2073 :
2074 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1057-L1068 */
2075 246 : } else if( close_account_state->discriminant==FD_BPF_STATE_BUFFER ) {
2076 :
2077 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1059 */
2078 158 : fd_borrowed_account_drop( &close_account );
2079 :
2080 158 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 3U ) ) ) {
2081 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2082 0 : }
2083 :
2084 158 : fd_bpf_state_buffer_t * state_buf = &close_account_state->inner.buffer;
2085 158 : err = common_close_account(
2086 158 : state_buf->has_authority_address ? &state_buf->authority_address : NULL,
2087 158 : instr_ctx,
2088 158 : close_account_state );
2089 158 : if( FD_UNLIKELY( err ) ) return err;
2090 :
2091 : /* Max msg_sz: 16 - 2 + 45 = 63 < 127 => we can use printf */
2092 138 : FD_BASE58_ENCODE_32_BYTES( close_key->uc, close_key_b58 );
2093 138 : fd_log_collector_printf_dangerous_max_127( instr_ctx, "Closed Buffer %s", close_key_b58 );
2094 :
2095 : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1069-L1129 */
2096 138 : } else if( close_account_state->discriminant==FD_BPF_STATE_PROGRAM_DATA ) {
2097 88 : int err;
2098 88 : if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 4U ) ) ) {
2099 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
2100 0 : }
2101 :
2102 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1074 */
2103 88 : fd_borrowed_account_drop( &close_account );
2104 :
2105 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1075-L1076 */
2106 88 : fd_guarded_borrowed_account_t program_account = {0};
2107 88 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK(instr_ctx, 3UL, &program_account );
2108 :
2109 88 : if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &program_account ) ) ) {
2110 0 : fd_log_collector_msg_literal( instr_ctx, "Program account is not writable" );
2111 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2112 0 : }
2113 88 : if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &program_account ), program_id, sizeof(fd_pubkey_t) ) ) ) {
2114 1 : fd_log_collector_msg_literal( instr_ctx, "Program account not owned by loader" );
2115 1 : return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
2116 1 : }
2117 87 : fd_sol_sysvar_clock_t clock_;
2118 87 : fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( instr_ctx->sysvar_cache, &clock_ );
2119 87 : if( FD_UNLIKELY( !clock ) ) {
2120 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
2121 0 : }
2122 87 : if( FD_UNLIKELY( clock->slot==close_account_state->inner.program_data.slot ) ) {
2123 75 : fd_log_collector_msg_literal( instr_ctx,"Program was deployed in this block already" );
2124 75 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2125 75 : }
2126 :
2127 12 : fd_bpf_state_t program_state[1];
2128 12 : err = fd_bpf_loader_program_get_state( program_account.acc, program_state );
2129 12 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
2130 0 : return err;
2131 0 : }
2132 :
2133 12 : if( program_state->discriminant==FD_BPF_STATE_PROGRAM ) {
2134 12 : if( FD_UNLIKELY( memcmp( &program_state->inner.program.programdata_address, close_key, sizeof(fd_pubkey_t) ) ) ) {
2135 9 : fd_log_collector_msg_literal( instr_ctx,"ProgramData account does not match ProgramData account" );
2136 9 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2137 9 : }
2138 :
2139 : /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1105 */
2140 3 : fd_borrowed_account_drop( &program_account );
2141 :
2142 3 : fd_bpf_state_program_data_t * pd = &close_account_state->inner.program_data;
2143 3 : err = common_close_account(
2144 3 : pd->has_upgrade_authority_address ? &pd->upgrade_authority_address : NULL,
2145 3 : instr_ctx,
2146 3 : close_account_state );
2147 3 : if( FD_UNLIKELY( err ) ) return err;
2148 :
2149 : /* Mark the closed programdata pd_write via the txn account (no
2150 : borrow is live here, and buffer/uninitialized closes going
2151 : through common_close_account must not set the bit). */
2152 3 : ushort pd_idx_in_txn;
2153 3 : err = fd_exec_instr_ctx_get_index_of_instr_account_in_transaction( instr_ctx, 0U, &pd_idx_in_txn );
2154 3 : if( FD_UNLIKELY( err ) ) return err; /* unreachable */
2155 3 : instr_ctx->txn_out->accounts.account[ pd_idx_in_txn ]->pd_write = 1;
2156 :
2157 : /* Execution validates ProgramData and rejects a parent copy
2158 : if pd_write_this_slot is set, before consulting the program
2159 : cache. */
2160 :
2161 3 : } else {
2162 0 : fd_log_collector_msg_literal( instr_ctx, "Invalid Program account" );
2163 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2164 0 : }
2165 :
2166 : /* Max msg_sz: 17 - 2 + 45 = 60 < 127 => we can use printf */
2167 3 : FD_BASE58_ENCODE_32_BYTES( program_account.acc->pubkey, program_account_b58 );
2168 3 : fd_log_collector_printf_dangerous_max_127( instr_ctx, "Closed Program %s", program_account_b58 );
2169 :
2170 : /* program account is dropped when it goes out of scope */
2171 3 : } else {
2172 0 : fd_log_collector_msg_literal( instr_ctx, "Account does not support closing" );
2173 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
2174 0 : }
2175 :
2176 : /* implicit drop of close account */
2177 225 : break;
2178 332 : }
2179 : /* https://github.com/anza-xyz/agave/blob/v4.5/programs/bpf_loader/src/lib.rs#L878-L888 */
2180 381 : case FD_BPF_INSTR_EXTEND_PROGRAM: {
2181 : /* https://github.com/anza-xyz/agave/blob/v4.5/programs/bpf_loader/src/lib.rs#L879-L886 */
2182 381 : if( FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, loader_v3_remove_extend_program ) ) {
2183 0 : fd_log_collector_msg_literal( instr_ctx, "ExtendProgram has been disabled" );
2184 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2185 0 : }
2186 :
2187 381 : err = common_extend_program( instr_ctx, instruction->inner.extend_program.additional_bytes, 0 );
2188 381 : if( FD_UNLIKELY( err ) ) {
2189 381 : return err;
2190 381 : }
2191 :
2192 0 : break;
2193 381 : }
2194 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1171-L1179 */
2195 298 : case FD_BPF_INSTR_EXTEND_PROGRAM_CHECKED: {
2196 298 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2197 381 : }
2198 : /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1338-L1508 */
2199 613 : case FD_BPF_INSTR_MIGRATE: {
2200 : /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1339-L1344 */
2201 613 : if( FD_UNLIKELY( !FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, enable_loader_v4 ) ) ) {
2202 613 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2203 613 : }
2204 :
2205 : /* TODO: Implement me */
2206 0 : FD_LOG_WARNING(( "migrate instruction is not implemented" ));
2207 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
2208 613 : }
2209 0 : default: {
2210 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
2211 613 : }
2212 3659 : }
2213 1223 : return FD_EXECUTOR_INSTR_SUCCESS;
2214 3659 : }
2215 :
2216 : /* process_instruction_inner() */
2217 : /* https://github.com/anza-xyz/agave/blob/77daab497df191ef485a7ad36ed291c1874596e5/programs/bpf_loader/src/lib.rs#L394-L564 */
2218 : int
2219 9691 : fd_bpf_loader_program_execute( fd_exec_instr_ctx_t * ctx ) {
2220 : /* https://github.com/anza-xyz/agave/blob/77daab497df191ef485a7ad36ed291c1874596e5/programs/bpf_loader/src/lib.rs#L491-L529 */
2221 :
2222 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L403-L404 */
2223 9691 : fd_guarded_borrowed_account_t program_account = {0};
2224 9691 : int err = fd_exec_instr_ctx_try_borrow_last_program_account( ctx, &program_account );
2225 9691 : if( FD_UNLIKELY( err ) ) {
2226 0 : return err;
2227 0 : }
2228 :
2229 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/programs/bpf_loader/src/lib.rs#L409 */
2230 9691 : fd_pubkey_t const * program_id = NULL;
2231 9691 : err = fd_exec_instr_ctx_get_last_program_key( ctx, &program_id );
2232 9691 : if( FD_UNLIKELY( err ) ) {
2233 0 : return err;
2234 0 : }
2235 :
2236 : /* Program management instruction */
2237 9691 : if( FD_UNLIKELY( !memcmp( &fd_solana_native_loader_id, fd_borrowed_account_get_owner( &program_account ), sizeof(fd_pubkey_t) ) ) ) {
2238 : /* https://github.com/anza-xyz/agave/blob/v2.2.3/programs/bpf_loader/src/lib.rs#L416 */
2239 3739 : fd_borrowed_account_drop( &program_account );
2240 :
2241 3739 : if( FD_UNLIKELY( !memcmp( &fd_solana_bpf_loader_upgradeable_program_id, program_id, sizeof(fd_pubkey_t) ) ) ) {
2242 3714 : FD_EXEC_CU_UPDATE( ctx, UPGRADEABLE_LOADER_COMPUTE_UNITS );
2243 3708 : return process_loader_upgradeable_instruction( ctx );
2244 3714 : } else if( FD_UNLIKELY( !memcmp( &fd_solana_bpf_loader_program_id, program_id, sizeof(fd_pubkey_t) ) ) ) {
2245 26 : FD_EXEC_CU_UPDATE( ctx, DEFAULT_LOADER_COMPUTE_UNITS );
2246 26 : fd_log_collector_msg_literal( ctx, "BPF loader management instructions are no longer supported" );
2247 26 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2248 >1844*10^16 : } else if( FD_UNLIKELY( !memcmp( &fd_solana_bpf_loader_deprecated_program_id, program_id, sizeof(fd_pubkey_t) ) ) ) {
2249 2 : FD_EXEC_CU_UPDATE( ctx, DEPRECATED_LOADER_COMPUTE_UNITS );
2250 2 : fd_log_collector_msg_literal( ctx, "Deprecated loader is no longer supported" );
2251 2 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2252 >1844*10^16 : } else {
2253 >1844*10^16 : fd_log_collector_msg_literal( ctx, "Invalid BPF loader id" );
2254 >1844*10^16 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2255 >1844*10^16 : }
2256 3739 : }
2257 :
2258 : /* https://github.com/anza-xyz/agave/blob/77daab497df191ef485a7ad36ed291c1874596e5/programs/bpf_loader/src/lib.rs#L551-L563 */
2259 : /* The Agave client stores a loaded program type state in its implementation
2260 : of the loaded program cache. It checks to see if an account is able to be
2261 : executed. It is possible for a program to be in the DelayVisibility state or
2262 : Closed state but it won't be reflected in the Firedancer cache. Program
2263 : accounts that are in this state should exit with an unsupported program
2264 : id error. For programs that are recently deployed or upgraded, they should not
2265 : be allowed to be executed for the remainder of the slot. For closed
2266 : accounts, they're uninitialized and shouldn't be executed as well.
2267 :
2268 : For the former case the slot that the
2269 : program was last updated in is in the program data account.
2270 : This means that if the slot in the program data account is greater than or
2271 : equal to the current execution slot, then the account is in a
2272 : 'LoadedProgramType::DelayVisiblity' state.
2273 :
2274 : The latter case as described above is a tombstone account which is in a Closed
2275 : state. This occurs when a program data account is closed. However, our cache
2276 : does not track this. Instead, this can be checked for by seeing if the program
2277 : account's respective program data account is uninitialized. This should only
2278 : happen when the account is closed.
2279 :
2280 : The programdata account here is fetched via fd_runtime_get_executable_account,
2281 : which may return a copy read from the parent fork. A parent copy's
2282 : program_data.slot cannot reflect a current-slot deploy/upgrade/extend/close,
2283 : so the DelayVisibility gate below is provenance-aware: current-fork copies keep
2284 : the slot comparison, parent-fork copies gate on the accdb pd_write probe
2285 : result instead. */
2286 :
2287 5952 : uchar is_deprecated = !memcmp( program_account.acc->owner, &fd_solana_bpf_loader_deprecated_program_id, sizeof(fd_pubkey_t) );
2288 :
2289 5952 : fd_acc_t const * progdata_ro = program_account.acc;
2290 5952 : if( !memcmp( program_account.acc->owner, &fd_solana_bpf_loader_upgradeable_program_id, sizeof(fd_pubkey_t) ) ) {
2291 2325 : fd_bpf_state_t program_account_state[1];
2292 2325 : err = fd_bpf_loader_program_get_state( program_account.acc, program_account_state );
2293 2325 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
2294 1 : fd_log_collector_msg_literal( ctx, "Program is not deployed" );
2295 1 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2296 1 : }
2297 :
2298 : /* https://github.com/anza-xyz/agave/blob/v2.0.9/svm/src/program_loader.rs#L96-L98
2299 : Program account and program data account discriminants get checked when loading in program accounts
2300 : into the program cache. If the discriminants are incorrect, the program is marked as closed. */
2301 2324 : if( FD_UNLIKELY( program_account_state->discriminant!=FD_BPF_STATE_PROGRAM ) ) {
2302 : /* https://github.com/anza-xyz/agave/tree/v3.0.5/programs/bpf_loader/src/lib.rs#L424-L433
2303 : Agave's program cache will add any non-migrating built-ins as built-in
2304 : accounts, even though they might be owned by the BPF loader. In these
2305 : cases, Agave does not log this message. Meanwhile, non-migrating
2306 : built-in programs do not use the BPF loader, by definition. */
2307 204 : if( !fd_is_non_migrating_builtin_program( program_id ) ) {
2308 203 : fd_log_collector_msg_literal( ctx, "Program is not deployed" );
2309 203 : }
2310 204 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2311 204 : }
2312 :
2313 2120 : fd_pubkey_t * programdata_pubkey = &program_account_state->inner.program.programdata_address;
2314 2120 : int from_parent_copy = 0;
2315 2120 : int pd_write_this_slot = 0;
2316 2120 : progdata_ro = fd_runtime_get_executable_account( ctx->txn_out, programdata_pubkey, &from_parent_copy, &pd_write_this_slot );
2317 2120 : if( FD_UNLIKELY( !progdata_ro ) ) {
2318 1199 : fd_log_collector_msg_literal( ctx, "Program is not deployed" );
2319 1199 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2320 1199 : }
2321 :
2322 921 : if( FD_UNLIKELY( progdata_ro->data_len<PROGRAMDATA_METADATA_SIZE ) ) {
2323 19 : fd_log_collector_msg_literal( ctx, "Program is not deployed" );
2324 19 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2325 19 : }
2326 :
2327 902 : fd_bpf_state_t program_data_account_state[1];
2328 902 : err = fd_bpf_loader_program_get_state( progdata_ro, program_data_account_state );
2329 902 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
2330 6 : fd_log_collector_msg_literal( ctx, "Program is not deployed" );
2331 6 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2332 6 : }
2333 :
2334 : /* https://github.com/anza-xyz/agave/blob/v2.0.9/svm/src/program_loader.rs#L100-L104
2335 : Same as above comment. Program data discriminant must be set correctly. */
2336 896 : if( FD_UNLIKELY( program_data_account_state->discriminant!=FD_BPF_STATE_PROGRAM_DATA ) ) {
2337 : /* The account is closed. */
2338 0 : fd_log_collector_msg_literal( ctx, "Program is not deployed" );
2339 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2340 0 : }
2341 :
2342 896 : ulong program_data_slot = program_data_account_state->inner.program_data.slot;
2343 897 : if( FD_LIKELY( !from_parent_copy ) ) {
2344 897 : if( FD_UNLIKELY( program_data_slot>=ctx->bank->f.slot ) ) {
2345 : /* The account was likely just deployed or upgraded. Corresponds to
2346 : 'LoadedProgramType::DelayVisibility' */
2347 1 : fd_log_collector_msg_literal( ctx, "Program is not deployed" );
2348 1 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2349 1 : }
2350 >1844*10^16 : } else {
2351 >1844*10^16 : if( FD_UNLIKELY( pd_write_this_slot ) ) {
2352 0 : fd_log_collector_msg_literal( ctx, "Program is not deployed" );
2353 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2354 0 : }
2355 >1844*10^16 : }
2356 896 : }
2357 :
2358 4522 : fd_prog_load_env_t load_env[1]; fd_prog_load_env_from_bank( load_env, ctx->bank );
2359 4522 : fd_progcache_t * progcache = ctx->runtime->progcache;
2360 4522 : fd_progcache_rec_t * cache_entry =
2361 4522 : fd_progcache_pull( progcache, ctx->bank->progcache_fork_id, program_id, load_env, progdata_ro );
2362 4522 : if( FD_UNLIKELY( !cache_entry ) ) {
2363 0 : fd_log_collector_msg_literal( ctx, "Program is not deployed" );
2364 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2365 0 : }
2366 :
2367 : /* The program may be in the cache but could have failed verification in the current epoch. */
2368 4522 : if( FD_UNLIKELY( !cache_entry->data_gaddr ) ) {
2369 577 : fd_progcache_rec_close( progcache, cache_entry );
2370 577 : fd_log_collector_msg_literal( ctx, "Program is not deployed" );
2371 577 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
2372 577 : }
2373 :
2374 : /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L446 */
2375 3945 : fd_borrowed_account_drop( &program_account );
2376 :
2377 3945 : int exec_err = fd_bpf_execute( ctx, cache_entry, is_deprecated );
2378 3945 : fd_progcache_rec_close( progcache, cache_entry );
2379 3945 : return exec_err;
2380 4522 : }
|