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