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