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