Line data Source code
1 : #include "fd_dump_pb.h"
2 : #include "generated/block.pb.h"
3 : #include "generated/invoke.pb.h"
4 : #include "generated/txn.pb.h"
5 : #include "generated/vm.pb.h"
6 : #include "../fd_system_ids.h"
7 : #include "../fd_bank.h"
8 : #include "../fd_runtime.h"
9 : #include "../program/fd_address_lookup_table_program.h"
10 : #include "../../../ballet/nanopb/pb_encode.h"
11 : #include "../../accdb/fd_accdb_sync.h"
12 : #include "../../progcache/fd_prog_load.h"
13 :
14 : #include <stdio.h> /* fopen */
15 : #include <sys/mman.h> /* mmap */
16 : #include <unistd.h> /* ftruncate */
17 :
18 : #define SORT_NAME sort_uint64_t
19 0 : #define SORT_KEY_T uint64_t
20 0 : #define SORT_BEFORE(a,b) (a)<(b)
21 : #include "../../../util/tmpl/fd_sort.c"
22 :
23 : struct fd_dump_account_key_node {
24 : fd_pubkey_t key;
25 : ulong redblack_parent;
26 : ulong redblack_left;
27 : ulong redblack_right;
28 : int redblack_color;
29 : };
30 : typedef struct fd_dump_account_key_node fd_dump_account_key_node_t;
31 0 : #define REDBLK_T fd_dump_account_key_node_t
32 : #define REDBLK_NAME fd_dump_account_key_map
33 0 : long fd_dump_account_key_map_compare( fd_dump_account_key_node_t * left, fd_dump_account_key_node_t * right ) {
34 0 : return memcmp( left->key.uc, right->key.uc, sizeof(fd_pubkey_t) );
35 0 : }
36 : #include "../../../util/tmpl/fd_redblack.c"
37 :
38 : /***** CONSTANTS *****/
39 : static fd_pubkey_t const * fd_dump_sysvar_ids[] = {
40 : &fd_sysvar_recent_block_hashes_id,
41 : &fd_sysvar_clock_id,
42 : &fd_sysvar_slot_history_id,
43 : &fd_sysvar_slot_hashes_id,
44 : &fd_sysvar_epoch_schedule_id,
45 : &fd_sysvar_epoch_rewards_id,
46 : &fd_sysvar_fees_id,
47 : &fd_sysvar_rent_id,
48 : &fd_sysvar_stake_history_id,
49 : &fd_sysvar_last_restart_slot_id,
50 : &fd_sysvar_instructions_id,
51 : };
52 : static ulong const num_sysvar_entries = (sizeof(fd_dump_sysvar_ids) / sizeof(fd_pubkey_t *));
53 :
54 : static fd_pubkey_t const * fd_dump_builtin_ids[] = {
55 : &fd_solana_system_program_id,
56 : &fd_solana_vote_program_id,
57 : &fd_solana_stake_program_id,
58 : &fd_solana_bpf_loader_v4_program_id,
59 : &fd_solana_bpf_loader_deprecated_program_id,
60 : &fd_solana_bpf_loader_program_id,
61 : &fd_solana_bpf_loader_upgradeable_program_id,
62 : &fd_solana_compute_budget_program_id,
63 : &fd_solana_keccak_secp_256k_program_id,
64 : &fd_solana_secp256r1_program_id,
65 : &fd_solana_zk_elgamal_proof_program_id,
66 : &fd_solana_ed25519_sig_verify_program_id,
67 : };
68 : static ulong const num_loaded_builtins = (sizeof(fd_dump_builtin_ids) / sizeof(fd_pubkey_t *));
69 :
70 : /***** UTILITY FUNCTIONS *****/
71 :
72 : /** GENERAL UTILITY FUNCTIONS AND MACROS **/
73 :
74 : static inline int
75 0 : is_builtin_account( fd_pubkey_t const * account_key ) {
76 0 : for( ulong j=0UL; j<num_loaded_builtins; j++ ) {
77 0 : if( !memcmp( account_key, fd_dump_builtin_ids[j], sizeof(fd_pubkey_t) ) ) {
78 0 : return 1;
79 0 : }
80 0 : }
81 0 : return 0;
82 0 : }
83 :
84 : /** FEATURE DUMPING **/
85 : static void
86 : dump_sorted_features( fd_features_t const * features,
87 : fd_exec_test_feature_set_t * output_feature_set,
88 0 : fd_spad_t * spad ) {
89 : /* NOTE: Caller must have a spad frame prepared */
90 0 : uint64_t * unsorted_features = fd_spad_alloc( spad, alignof(uint64_t), FD_FEATURE_ID_CNT * sizeof(uint64_t) );
91 0 : ulong num_features = 0;
92 0 : for( const fd_feature_id_t * current_feature = fd_feature_iter_init(); !fd_feature_iter_done( current_feature ); current_feature = fd_feature_iter_next( current_feature ) ) {
93 0 : if (features->f[current_feature->index] != FD_FEATURE_DISABLED) {
94 0 : unsorted_features[num_features++] = (uint64_t) current_feature->id.ul[0];
95 0 : }
96 0 : }
97 : // Sort the features
98 0 : void * scratch = fd_spad_alloc( spad, sort_uint64_t_stable_scratch_align(), sort_uint64_t_stable_scratch_footprint(num_features) );
99 0 : uint64_t * sorted_features = sort_uint64_t_stable_fast( unsorted_features, num_features, scratch );
100 :
101 : // Set feature set in message
102 0 : output_feature_set->features_count = (pb_size_t) num_features;
103 0 : output_feature_set->features = sorted_features;
104 0 : }
105 :
106 : /** ACCOUNT DUMPING **/
107 : static void
108 : dump_account_state( fd_pubkey_t const * account_key,
109 : fd_account_meta_t const * account_meta,
110 : fd_exec_test_acct_state_t * output_account,
111 0 : fd_spad_t * spad ) {
112 : // Address
113 0 : fd_memcpy(output_account->address, account_key, sizeof(fd_pubkey_t));
114 :
115 : // Lamports
116 0 : output_account->lamports = (uint64_t)account_meta->lamports;
117 :
118 : // Data
119 0 : output_account->data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( account_meta->dlen ) );
120 0 : output_account->data->size = (pb_size_t) account_meta->dlen;
121 0 : fd_memcpy(output_account->data->bytes, fd_account_data( account_meta ), account_meta->dlen );
122 :
123 : // Executable
124 0 : output_account->executable = (bool)account_meta->executable;
125 :
126 : // Owner
127 0 : fd_memcpy(output_account->owner, account_meta->owner, sizeof(fd_pubkey_t));
128 0 : }
129 :
130 : static uchar
131 : account_already_dumped( fd_exec_test_acct_state_t const * dumped_accounts,
132 : ulong dumped_cnt,
133 0 : fd_pubkey_t const * account_key ) {
134 0 : for( ulong i=0UL; i<dumped_cnt; i++ ) {
135 0 : if( !memcmp( account_key, dumped_accounts[i].address, sizeof(fd_pubkey_t) ) ) {
136 0 : return 1;
137 0 : }
138 0 : }
139 0 : return 0;
140 0 : }
141 :
142 : /* Dumps a borrowed account if it exists and has not been dumped yet.
143 : Sets up the output borrowed account if it exists. Returns 0 if the
144 : account exists, 1 otherwise.
145 : TODO: This can be optimized by using a set. */
146 : static uchar
147 : dump_account_if_not_already_dumped( fd_accdb_user_t * accdb,
148 : fd_funk_txn_xid_t const * xid,
149 : fd_pubkey_t const * account_key,
150 : fd_spad_t * spad,
151 : fd_exec_test_acct_state_t * out_acct_states,
152 : pb_size_t * out_acct_states_cnt,
153 0 : fd_accdb_ro_t * out_ro ) {
154 0 : fd_accdb_ro_t ro[1];
155 0 : if( !fd_accdb_open_ro( accdb, ro, xid, account_key ) ) {
156 0 : return 1;
157 0 : }
158 :
159 0 : if( !account_already_dumped( out_acct_states, *out_acct_states_cnt, account_key ) ) {
160 0 : dump_account_state( account_key, ro->meta, &out_acct_states[*out_acct_states_cnt], spad );
161 0 : (*out_acct_states_cnt)++;
162 0 : }
163 :
164 0 : if( out_ro ) {
165 0 : *out_ro = *ro;
166 0 : } else {
167 0 : fd_accdb_close_ro( accdb, ro );
168 0 : }
169 0 : return 0;
170 0 : }
171 :
172 : static void
173 : dump_executable_account_if_exists( fd_accdb_user_t * accdb,
174 : fd_funk_txn_xid_t const * xid,
175 : fd_exec_test_acct_state_t const * program_account,
176 : fd_spad_t * spad,
177 : fd_exec_test_acct_state_t * out_account_states,
178 0 : pb_size_t * out_account_states_count ) {
179 0 : if( FD_LIKELY( memcmp( program_account->owner, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
180 0 : return;
181 0 : }
182 :
183 0 : int err;
184 0 : fd_bpf_upgradeable_loader_state_t * program_loader_state = fd_bincode_decode_spad(
185 0 : bpf_upgradeable_loader_state,
186 0 : spad,
187 0 : program_account->data->bytes,
188 0 : program_account->data->size,
189 0 : &err );
190 0 : if( FD_UNLIKELY( err ) ) return;
191 :
192 0 : if( !fd_bpf_upgradeable_loader_state_is_program( program_loader_state ) ) {
193 0 : return;
194 0 : }
195 :
196 0 : fd_pubkey_t * programdata_acc = &program_loader_state->inner.program.programdata_address;
197 0 : dump_account_if_not_already_dumped( accdb, xid, programdata_acc, spad, out_account_states, out_account_states_count, NULL );
198 0 : }
199 :
200 : static void
201 : dump_sanitized_transaction( fd_accdb_user_t * accdb,
202 : fd_funk_txn_xid_t const * xid,
203 : fd_txn_t const * txn_descriptor,
204 : uchar const * txn_payload,
205 : fd_spad_t * spad,
206 0 : fd_exec_test_sanitized_transaction_t * sanitized_transaction ) {
207 0 : fd_txn_acct_addr_lut_t const * address_lookup_tables = fd_txn_get_address_tables_const( txn_descriptor );
208 :
209 : /* Transaction Context -> tx -> message */
210 0 : sanitized_transaction->has_message = true;
211 0 : fd_exec_test_transaction_message_t * message = &sanitized_transaction->message;
212 :
213 : /* Transaction Context -> tx -> message -> is_legacy */
214 0 : message->is_legacy = txn_descriptor->transaction_version == FD_TXN_VLEGACY;
215 :
216 : /* Transaction Context -> tx -> message -> header */
217 0 : message->has_header = true;
218 0 : fd_exec_test_message_header_t * header = &message->header;
219 :
220 : /* Transaction Context -> tx -> message -> header -> num_required_signatures */
221 0 : header->num_required_signatures = txn_descriptor->signature_cnt;
222 :
223 : /* Transaction Context -> tx -> message -> header -> num_readonly_signed_accounts */
224 0 : header->num_readonly_signed_accounts = txn_descriptor->readonly_signed_cnt;
225 :
226 : /* Transaction Context -> tx -> message -> header -> num_readonly_unsigned_accounts */
227 0 : header->num_readonly_unsigned_accounts = txn_descriptor->readonly_unsigned_cnt;
228 :
229 : /* Transaction Context -> tx -> message -> account_keys */
230 0 : message->account_keys_count = txn_descriptor->acct_addr_cnt;
231 0 : message->account_keys = fd_spad_alloc( spad, alignof(pb_bytes_array_t *), PB_BYTES_ARRAY_T_ALLOCSIZE(txn_descriptor->acct_addr_cnt * sizeof(pb_bytes_array_t *)) );
232 0 : fd_acct_addr_t const * account_keys = fd_txn_get_acct_addrs( txn_descriptor, txn_payload );
233 0 : for( ulong i = 0; i < txn_descriptor->acct_addr_cnt; i++ ) {
234 0 : pb_bytes_array_t * account_key = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_pubkey_t)) );
235 0 : account_key->size = sizeof(fd_pubkey_t);
236 0 : memcpy( account_key->bytes, &account_keys[i], sizeof(fd_pubkey_t) );
237 0 : message->account_keys[i] = account_key;
238 0 : }
239 :
240 : /* Transaction Context -> tx -> message -> recent_blockhash */
241 0 : uchar const * recent_blockhash = fd_txn_get_recent_blockhash( txn_descriptor, txn_payload );
242 0 : message->recent_blockhash = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_hash_t)) );
243 0 : message->recent_blockhash->size = sizeof(fd_hash_t);
244 0 : memcpy( message->recent_blockhash->bytes, recent_blockhash, sizeof(fd_hash_t) );
245 :
246 : /* Transaction Context -> tx -> message -> instructions */
247 0 : message->instructions_count = txn_descriptor->instr_cnt;
248 0 : message->instructions = fd_spad_alloc( spad, alignof(fd_exec_test_compiled_instruction_t), txn_descriptor->instr_cnt * sizeof(fd_exec_test_compiled_instruction_t) );
249 0 : for( ulong i = 0; i < txn_descriptor->instr_cnt; ++i ) {
250 0 : fd_txn_instr_t instr = txn_descriptor->instr[i];
251 0 : fd_exec_test_compiled_instruction_t * compiled_instruction = &message->instructions[i];
252 :
253 : // compiled instruction -> program_id_index
254 0 : compiled_instruction->program_id_index = instr.program_id;
255 :
256 : // compiled instruction -> accounts
257 0 : compiled_instruction->accounts_count = instr.acct_cnt;
258 0 : compiled_instruction->accounts = fd_spad_alloc( spad, alignof(uint32_t), instr.acct_cnt * sizeof(uint32_t) );
259 0 : uchar const * instr_accounts = fd_txn_get_instr_accts( &instr, txn_payload );
260 0 : for( ulong j = 0; j < instr.acct_cnt; ++j ) {
261 0 : uchar instr_acct_index = instr_accounts[j];
262 0 : compiled_instruction->accounts[j] = instr_acct_index;
263 0 : }
264 :
265 : // compiled instruction -> data
266 0 : uchar const * instr_data = fd_txn_get_instr_data( &instr, txn_payload );
267 0 : compiled_instruction->data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(instr.data_sz) );
268 0 : compiled_instruction->data->size = instr.data_sz;
269 0 : memcpy( compiled_instruction->data->bytes, instr_data, instr.data_sz );
270 0 : }
271 :
272 : /* ALUT stuff (non-legacy) */
273 0 : message->address_table_lookups_count = 0;
274 0 : if( !message->is_legacy ) {
275 : /* Transaction Context -> tx -> message -> address_table_lookups */
276 0 : message->address_table_lookups_count = txn_descriptor->addr_table_lookup_cnt;
277 0 : message->address_table_lookups = fd_spad_alloc( spad,
278 0 : alignof(fd_exec_test_message_address_table_lookup_t),
279 0 : txn_descriptor->addr_table_lookup_cnt * sizeof(fd_exec_test_message_address_table_lookup_t) );
280 0 : for( ulong i = 0; i < txn_descriptor->addr_table_lookup_cnt; ++i ) {
281 : // alut -> account_key
282 0 : fd_pubkey_t * alut_key = (fd_pubkey_t *) (txn_payload + address_lookup_tables[i].addr_off);
283 0 : memcpy( message->address_table_lookups[i].account_key, alut_key, sizeof(fd_pubkey_t) );
284 :
285 : // Access ALUT account data to access its keys
286 0 : fd_accdb_ro_t addr_lut_ro[1];
287 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, addr_lut_ro, xid, alut_key ) ) ) {
288 0 : FD_LOG_ERR(( "addr lut not found" ));
289 0 : }
290 :
291 : // alut -> writable_indexes
292 0 : message->address_table_lookups[i].writable_indexes_count = address_lookup_tables[i].writable_cnt;
293 0 : message->address_table_lookups[i].writable_indexes = fd_spad_alloc( spad, alignof(uint32_t), address_lookup_tables[i].writable_cnt * sizeof(uint32_t) );
294 0 : uchar * writable_indexes = (uchar *) (txn_payload + address_lookup_tables[i].writable_off);
295 0 : for( ulong j = 0; j < address_lookup_tables[i].writable_cnt; ++j ) {
296 0 : message->address_table_lookups[i].writable_indexes[j] = writable_indexes[j];
297 0 : }
298 :
299 : // alut -> readonly_indexes
300 0 : message->address_table_lookups[i].readonly_indexes_count = address_lookup_tables[i].readonly_cnt;
301 0 : message->address_table_lookups[i].readonly_indexes = fd_spad_alloc( spad, alignof(uint32_t), address_lookup_tables[i].readonly_cnt * sizeof(uint32_t) );
302 0 : uchar * readonly_indexes = (uchar *) (txn_payload + address_lookup_tables[i].readonly_off);
303 0 : for( ulong j = 0; j < address_lookup_tables[i].readonly_cnt; ++j ) {
304 0 : message->address_table_lookups[i].readonly_indexes[j] = readonly_indexes[j];
305 0 : }
306 :
307 0 : fd_accdb_close_ro( accdb, addr_lut_ro );
308 0 : }
309 0 : }
310 :
311 : /* Transaction Context -> tx -> message_hash */
312 : // Skip because it does not matter what's in here
313 :
314 : /* Transaction Context -> tx -> signatures */
315 0 : sanitized_transaction->signatures_count = txn_descriptor->signature_cnt;
316 0 : sanitized_transaction->signatures = fd_spad_alloc( spad, alignof(pb_bytes_array_t *), PB_BYTES_ARRAY_T_ALLOCSIZE(txn_descriptor->signature_cnt * sizeof(pb_bytes_array_t *)) );
317 0 : fd_ed25519_sig_t const * signatures = fd_txn_get_signatures( txn_descriptor, txn_payload );
318 0 : for( uchar i = 0; i < txn_descriptor->signature_cnt; ++i ) {
319 0 : pb_bytes_array_t * signature = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_ed25519_sig_t)) );
320 0 : signature->size = sizeof(fd_ed25519_sig_t);
321 0 : memcpy( signature->bytes, &signatures[i], sizeof(fd_ed25519_sig_t) );
322 0 : sanitized_transaction->signatures[i] = signature;
323 0 : }
324 0 : }
325 :
326 : /** BLOCKHASH QUEUE DUMPING **/
327 :
328 : static void
329 : dump_blockhash_queue( fd_blockhashes_t const * queue,
330 : fd_spad_t * spad,
331 : pb_bytes_array_t ** output_blockhash_queue,
332 0 : pb_size_t * output_blockhash_queue_count ) {
333 0 : ulong bhq_size = fd_ulong_min( FD_BLOCKHASHES_MAX, fd_blockhash_deq_cnt( queue->d.deque ) );
334 :
335 : // Iterate over all block hashes in the queue and save them in the output
336 0 : pb_size_t cnt = 0U;
337 0 : for( fd_blockhash_deq_iter_t iter=fd_blockhash_deq_iter_init_rev( queue->d.deque );
338 0 : !fd_blockhash_deq_iter_done_rev( queue->d.deque, iter ) && cnt<FD_BLOCKHASHES_MAX;
339 0 : iter=fd_blockhash_deq_iter_prev( queue->d.deque, iter ), cnt++ ) {
340 0 : fd_blockhash_info_t const * ele = fd_blockhash_deq_iter_ele_const( queue->d.deque, iter );
341 0 : pb_bytes_array_t * output_blockhash = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_hash_t)) );
342 0 : output_blockhash->size = sizeof(fd_hash_t);
343 0 : fd_memcpy( output_blockhash->bytes, &ele->hash, sizeof(fd_hash_t) );
344 0 : output_blockhash_queue[ bhq_size-cnt-1UL ] = output_blockhash;
345 0 : }
346 :
347 0 : *output_blockhash_queue_count = cnt;
348 0 : }
349 :
350 : /** SECONDARY FUNCTIONS **/
351 :
352 : /* add_account_to_dumped_accounts adds an account to the dumped accounts
353 : set if it does not exist already. Returns 0 if the account already
354 : exists, and 1 if the account was added successfully.
355 :
356 : TODO: Txn dumping should be optimized to use these functions. */
357 : static uchar
358 : add_account_to_dumped_accounts( fd_dump_account_key_node_t * pool,
359 : fd_dump_account_key_node_t ** root,
360 0 : fd_pubkey_t const * pubkey ) {
361 : /* If the key already exists, return early. */
362 0 : fd_dump_account_key_node_t node = {
363 0 : .key = *pubkey,
364 0 : };
365 0 : if( fd_dump_account_key_map_find( pool, *root, &node ) ) {
366 0 : return 0;
367 0 : }
368 :
369 0 : fd_dump_account_key_node_t * new_node = fd_dump_account_key_map_acquire( pool );
370 0 : new_node->key = *pubkey;
371 0 : fd_dump_account_key_map_insert( pool, root, new_node );
372 0 : return 1;
373 0 : }
374 :
375 : /* add_account_and_programdata_to_dumped_accounts adds an account and
376 : its programdata account (if the account is a v3 program) to the
377 : dumped accounts set if they do not exist already. */
378 : static void
379 : add_account_and_programdata_to_dumped_accounts( fd_accdb_user_t * accdb,
380 : fd_funk_txn_xid_t const * xid,
381 : fd_dump_account_key_node_t * pool,
382 : fd_dump_account_key_node_t ** root,
383 0 : fd_pubkey_t const * pubkey ) {
384 : /* Add the current account to the dumped accounts set. We can save
385 : some time by enforcing an invariant that "if current account was
386 : dumped, then programdata account was also dumped," so we save
387 : ourselves a call to Funk. */
388 0 : uchar ret = add_account_to_dumped_accounts( pool, root, pubkey );
389 0 : if( ret==0 ) return;
390 :
391 : /* Read the account from Funk to see if its a program account and if
392 : it needs to be dumped. */
393 0 : fd_accdb_ro_t program_account[1];
394 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, program_account, xid, pubkey ) ) ) {
395 0 : return;
396 0 : }
397 :
398 : /* Return if its not owned by the v3 loader */
399 0 : if( FD_LIKELY( !fd_pubkey_eq( fd_accdb_ref_owner( program_account ), &fd_solana_bpf_loader_upgradeable_program_id ) ) ) {
400 0 : fd_accdb_close_ro( accdb, program_account );
401 0 : return;
402 0 : }
403 :
404 : /* Get the program account state */
405 0 : fd_bpf_upgradeable_loader_state_t program_account_state[1];
406 0 : if( FD_UNLIKELY( !fd_bincode_decode_static(
407 0 : bpf_upgradeable_loader_state,
408 0 : program_account_state,
409 0 : fd_accdb_ref_data_const( program_account ),
410 0 : fd_accdb_ref_data_sz ( program_account ),
411 0 : NULL ) ) ) {
412 0 : fd_accdb_close_ro( accdb, program_account );
413 0 : return;
414 0 : }
415 0 : if( !fd_bpf_upgradeable_loader_state_is_program( program_account_state ) ) {
416 0 : fd_accdb_close_ro( accdb, program_account );
417 0 : return;
418 0 : }
419 :
420 : /* Dump the programdata address */
421 0 : add_account_to_dumped_accounts( pool, root, &program_account_state->inner.program.programdata_address );
422 0 : fd_accdb_close_ro( accdb, program_account );
423 0 : }
424 :
425 : /* add_lut_account_to_dumped_accounts adds an address lookup table
426 : account AND all pubkeys in the lookup table to the dumped accounts
427 : set if they do not exist already. */
428 : static void
429 : add_lut_accounts_to_dumped_accounts( fd_accdb_user_t * accdb,
430 : fd_funk_txn_xid_t const * xid,
431 : fd_dump_account_key_node_t * pool,
432 : fd_dump_account_key_node_t ** root,
433 0 : fd_pubkey_t const * pubkey ) {
434 : /* Add the current account to the dumped accounts set. */
435 0 : add_account_to_dumped_accounts( pool, root, pubkey );
436 :
437 : /* Read the account and dump all pubkeys within the lookup table. */
438 0 : fd_accdb_ro_t lut_account[1];
439 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, lut_account, xid, pubkey ) ) ) {
440 0 : return;
441 0 : }
442 :
443 0 : uchar const * data = fd_accdb_ref_data_const( lut_account );
444 0 : ulong data_len = fd_accdb_ref_data_sz ( lut_account );
445 :
446 : /* Decode the ALUT account and dump all pubkeys within the lookup
447 : table. */
448 0 : if( data_len<FD_LOOKUP_TABLE_META_SIZE || (data_len&0x1fUL) ) {
449 0 : fd_accdb_close_ro( accdb, lut_account );
450 0 : return;
451 0 : }
452 0 : fd_pubkey_t const * lookup_addrs = fd_type_pun_const( data+FD_LOOKUP_TABLE_META_SIZE );
453 0 : ulong lookup_addrs_cnt = ( data_len-FD_LOOKUP_TABLE_META_SIZE)>>5UL; // = (dlen - 56) / 32
454 0 : for( ulong i=0UL; i<lookup_addrs_cnt; i++ ) {
455 0 : fd_pubkey_t const * referenced_pubkey = &lookup_addrs[i];
456 0 : add_account_and_programdata_to_dumped_accounts( accdb, xid, pool, root, referenced_pubkey );
457 0 : }
458 0 : fd_accdb_close_ro( accdb, lut_account );
459 0 : }
460 :
461 : /* create_synthetic_vote_account_from_vote_state creates a synthetic
462 : vote account from a vote state cache element. It fills in default
463 : values for unspecified fields and encodes the vote state into
464 : out_vote_account's data field. */
465 : static void
466 : create_synthetic_vote_account_from_vote_state( fd_vote_state_ele_t const * vote_state,
467 : fd_spad_t * spad,
468 : fd_exec_test_vote_account_t * out_vote_account,
469 0 : int is_t_1 ) {
470 0 : out_vote_account->has_vote_account = true;
471 0 : fd_memcpy( out_vote_account->vote_account.address, &vote_state->vote_account, sizeof(fd_pubkey_t) );
472 0 : out_vote_account->vote_account.executable = false;
473 0 : out_vote_account->vote_account.lamports = 100000UL;
474 0 : fd_memcpy( out_vote_account->vote_account.owner, fd_solana_vote_program_id.key, sizeof(fd_pubkey_t) );
475 0 : out_vote_account->stake = is_t_1 ? vote_state->stake_t_1 : vote_state->stake_t_2;
476 :
477 : /* Construct the vote account data. Fill in missing fields with
478 : arbitrary defaults (since they're not used anyways) */
479 0 : fd_vote_state_versioned_t vsv = {
480 0 : .discriminant = fd_vote_state_versioned_enum_v3,
481 0 : .inner = {
482 0 : .v3 = {
483 0 : .node_pubkey = vote_state->node_account,
484 0 : .authorized_withdrawer = vote_state->node_account,
485 0 : .commission = vote_state->commission,
486 0 : .root_slot = 0UL,
487 0 : .has_root_slot = 0,
488 0 : .last_timestamp = {
489 0 : .timestamp = vote_state->last_vote_timestamp,
490 0 : .slot = vote_state->last_vote_slot,
491 0 : },
492 0 : }
493 0 : }
494 0 : };
495 0 : fd_vote_state_v3_t * synthetic_vote_state = &vsv.inner.v3;
496 :
497 : /* Create synthetic landed votes */
498 0 : synthetic_vote_state->votes = deq_fd_landed_vote_t_join(
499 0 : deq_fd_landed_vote_t_new(
500 0 : fd_spad_alloc(
501 0 : spad,
502 0 : deq_fd_landed_vote_t_align(),
503 0 : deq_fd_landed_vote_t_footprint( 32UL ) ),
504 0 : 32UL ) );
505 0 : for( ulong i=0UL; i<32UL; i++ ) {
506 0 : fd_landed_vote_t elem = {0};
507 0 : deq_fd_landed_vote_t_push_tail( synthetic_vote_state->votes, elem );
508 0 : }
509 :
510 : /* Populate authoritzed voters */
511 0 : void * authorized_voters_pool_mem = fd_spad_alloc(
512 0 : spad,
513 0 : fd_vote_authorized_voters_pool_align(),
514 0 : fd_vote_authorized_voters_pool_footprint( 5UL ) );
515 0 : void * authorized_voters_treap_mem = fd_spad_alloc(
516 0 : spad,
517 0 : fd_vote_authorized_voters_treap_align(),
518 0 : fd_vote_authorized_voters_treap_footprint( 5UL ) );
519 0 : synthetic_vote_state->authorized_voters.pool = fd_vote_authorized_voters_pool_join( fd_vote_authorized_voters_pool_new( authorized_voters_pool_mem, 5UL ) );
520 0 : synthetic_vote_state->authorized_voters.treap = fd_vote_authorized_voters_treap_join( fd_vote_authorized_voters_treap_new( authorized_voters_treap_mem, 5UL ) );
521 :
522 : /* Encode the synthetic vote state */
523 0 : ulong encoded_sz = fd_vote_state_versioned_size( &vsv );
524 0 : out_vote_account->vote_account.data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( encoded_sz ) );
525 0 : out_vote_account->vote_account.data->size = (pb_size_t)encoded_sz;
526 :
527 0 : fd_bincode_encode_ctx_t encode_ctx = {
528 0 : .data = out_vote_account->vote_account.data->bytes,
529 0 : .dataend = out_vote_account->vote_account.data->bytes+encoded_sz,
530 0 : };
531 0 : fd_vote_state_versioned_encode( &vsv, &encode_ctx );
532 0 : }
533 :
534 : static void FD_FN_UNUSED
535 : dump_prior_vote_accounts( fd_vote_states_t const * vote_states,
536 : fd_dump_account_key_node_t * dumped_accounts_pool,
537 : fd_dump_account_key_node_t ** dumped_accounts_root,
538 : fd_exec_test_vote_account_t * out_vote_accounts,
539 : pb_size_t * out_vote_accounts_count,
540 : fd_spad_t * spad,
541 0 : int is_t_1 ) {
542 :
543 0 : fd_vote_states_iter_t iter_[1];
544 0 : for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, vote_states );
545 0 : !fd_vote_states_iter_done( iter );
546 0 : fd_vote_states_iter_next( iter ) ) {
547 0 : fd_vote_state_ele_t const * vote_state = fd_vote_states_iter_ele( iter );
548 0 : add_account_to_dumped_accounts( dumped_accounts_pool, dumped_accounts_root, &vote_state->vote_account );
549 :
550 0 : create_synthetic_vote_account_from_vote_state(
551 0 : vote_state,
552 0 : spad,
553 0 : &out_vote_accounts[(*out_vote_accounts_count)++],
554 0 : is_t_1 );
555 0 : }
556 0 : }
557 :
558 : static void
559 : create_block_context_protobuf_from_block( fd_block_dump_ctx_t * dump_ctx,
560 : fd_banks_t * banks,
561 : fd_bank_t * bank,
562 0 : fd_accdb_user_t * accdb ) {
563 : /* We should use the bank fields and funk txn from the parent slot in
564 : order to capture the block context from before the current block
565 : was executed, since dumping is happening in the block finalize
566 : step. */
567 0 : fd_bank_t parent_bank[1];
568 0 : fd_banks_get_parent( parent_bank, banks, bank );
569 0 : ulong current_slot = fd_bank_slot_get( bank );
570 0 : ulong parent_slot = fd_bank_slot_get( parent_bank );
571 0 : fd_funk_txn_xid_t parent_xid = { .ul = { parent_slot, parent_bank->data->idx } };
572 0 : fd_exec_test_block_context_t * block_context = &dump_ctx->block_context;
573 0 : ulong dump_txn_count = dump_ctx->txns_to_dump_cnt;
574 0 : fd_spad_t * spad = dump_ctx->spad;
575 :
576 : /* Get vote and stake delegation infos */
577 0 : fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query( parent_bank );
578 0 : ulong vote_account_t_cnt = fd_vote_states_cnt( vote_states );
579 0 : fd_bank_vote_states_end_locking_query( parent_bank );
580 :
581 0 : fd_stake_delegations_t const * stake_delegations = fd_bank_stake_delegations_frontier_query( banks, parent_bank );
582 0 : ulong stake_account_cnt = fd_stake_delegations_cnt( stake_delegations );
583 :
584 : /* Collect account states in a temporary set before iterating over
585 : them and dumping them out. */
586 0 : ulong total_num_accounts = num_sysvar_entries + /* Sysvars */
587 0 : num_loaded_builtins + /* Builtins */
588 0 : stake_account_cnt + /* Stake accounts */
589 0 : vote_account_t_cnt + /* Current vote accounts */
590 0 : dump_txn_count*128UL; /* Txn accounts upper bound */
591 0 : void * dumped_accounts_mem = fd_spad_alloc( spad, fd_dump_account_key_map_align(), fd_dump_account_key_map_footprint( total_num_accounts ) );
592 0 : fd_dump_account_key_node_t * dumped_accounts_pool = fd_dump_account_key_map_join( fd_dump_account_key_map_new( dumped_accounts_mem, total_num_accounts ) );
593 0 : fd_dump_account_key_node_t * dumped_accounts_root = NULL;
594 :
595 : /* BlockContext -> txns */
596 0 : block_context->txns_count = (pb_size_t)dump_txn_count;
597 0 : block_context->txns = fd_spad_alloc( spad, alignof(fd_exec_test_sanitized_transaction_t), dump_ctx->txns_to_dump_cnt * sizeof(fd_exec_test_sanitized_transaction_t) );
598 0 : fd_memset( block_context->txns, 0, dump_ctx->txns_to_dump_cnt * sizeof(fd_exec_test_sanitized_transaction_t) );
599 :
600 : /* Dump sanitized transactions from the transaction descriptors */
601 0 : for( ulong i=0UL; i<dump_ctx->txns_to_dump_cnt; i++ ) {
602 0 : fd_txn_p_t const * txn_ptr = &dump_ctx->txns_to_dump[i];
603 0 : fd_txn_t const * txn_descriptor = TXN( txn_ptr );
604 0 : dump_sanitized_transaction( accdb, &parent_xid, txn_descriptor, txn_ptr->payload, spad, &block_context->txns[i] );
605 :
606 : /* Dump account + alut + programdata accounts (if applicable).
607 : 1. Dump the raw txn account keys
608 : 2. Dump the ALUT accounts
609 : 3. Dump all referenced accounts in the ALUTs
610 : 4. Dump any executable accounts */
611 :
612 : // 1 + 4. Dump any account keys that are referenced by transactions
613 : // + any programdata accounts (if applicable).
614 0 : fd_acct_addr_t const * account_keys = fd_txn_get_acct_addrs( txn_descriptor, txn_ptr->payload );
615 0 : for( ushort l=0; l<txn_descriptor->acct_addr_cnt; l++ ) {
616 0 : fd_pubkey_t const * account_key = fd_type_pun_const( &account_keys[l] );
617 0 : add_account_and_programdata_to_dumped_accounts( accdb, &parent_xid, dumped_accounts_pool, &dumped_accounts_root, account_key );
618 0 : }
619 :
620 : // 2 + 3 + 4. Dump any ALUT accounts + any accounts referenced in
621 : // the ALUTs + any programdata accounts (if applicable).
622 0 : fd_txn_acct_addr_lut_t const * txn_lookup_tables = fd_txn_get_address_tables_const( txn_descriptor );
623 0 : for( ushort l=0; l<txn_descriptor->addr_table_lookup_cnt; l++ ) {
624 0 : fd_txn_acct_addr_lut_t const * lookup_table = &txn_lookup_tables[l];
625 0 : fd_pubkey_t const * lut_key = fd_type_pun_const( txn_ptr->payload+lookup_table->addr_off );
626 0 : add_lut_accounts_to_dumped_accounts( accdb, &parent_xid, dumped_accounts_pool, &dumped_accounts_root, lut_key );
627 0 : }
628 0 : }
629 :
630 : /* Dump sysvars */
631 0 : for( ulong i=0UL; i<num_sysvar_entries; i++ ) {
632 0 : add_account_to_dumped_accounts( dumped_accounts_pool, &dumped_accounts_root, fd_dump_sysvar_ids[i] );
633 0 : }
634 :
635 : /* Dump builtins */
636 0 : for( ulong i=0UL; i<num_loaded_builtins; i++ ) {
637 0 : add_account_to_dumped_accounts( dumped_accounts_pool, &dumped_accounts_root, fd_dump_builtin_ids[i] );
638 0 : }
639 :
640 : /* Dump stake accounts for this epoch */
641 0 : fd_stake_delegations_iter_t iter_[1];
642 0 : for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
643 0 : !fd_stake_delegations_iter_done( iter );
644 0 : fd_stake_delegations_iter_next( iter ) ) {
645 0 : fd_stake_delegation_t * stake_delegation = fd_stake_delegations_iter_ele( iter );
646 0 : add_account_to_dumped_accounts( dumped_accounts_pool, &dumped_accounts_root, &stake_delegation->stake_account );
647 0 : }
648 :
649 : /* Dump vote accounts for this epoch */
650 0 : vote_states = fd_bank_vote_states_locking_query( parent_bank );
651 0 : fd_vote_states_iter_t vote_iter_[1];
652 0 : for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( vote_iter_, vote_states ); !fd_vote_states_iter_done( iter ); fd_vote_states_iter_next( iter ) ) {
653 0 : fd_vote_state_ele_t const * vote_state = fd_vote_states_iter_ele( iter );
654 0 : add_account_to_dumped_accounts( dumped_accounts_pool, &dumped_accounts_root, &vote_state->vote_account );
655 0 : }
656 :
657 : // BlockContext -> EpochContext -> vote_accounts_t_1 (vote accounts at epoch T-1)
658 0 : block_context->epoch_ctx.vote_accounts_t_1 = fd_spad_alloc(
659 0 : spad,
660 0 : alignof(fd_exec_test_vote_account_t),
661 0 : sizeof(fd_exec_test_vote_account_t)*fd_vote_states_cnt( vote_states ) );
662 0 : block_context->epoch_ctx.vote_accounts_t_1_count = 0U;
663 0 : dump_prior_vote_accounts(
664 0 : vote_states,
665 0 : dumped_accounts_pool,
666 0 : &dumped_accounts_root,
667 0 : block_context->epoch_ctx.vote_accounts_t_1,
668 0 : &block_context->epoch_ctx.vote_accounts_t_1_count,
669 0 : spad,
670 0 : 1 );
671 :
672 : // // BlockContext -> EpochContext -> vote_accounts_t_2 (vote accounts at epoch T-2)
673 0 : block_context->epoch_ctx.vote_accounts_t_2 = fd_spad_alloc(
674 0 : spad,
675 0 : alignof(fd_exec_test_vote_account_t),
676 0 : sizeof(fd_exec_test_vote_account_t)*fd_vote_states_cnt( vote_states ) );
677 0 : block_context->epoch_ctx.vote_accounts_t_2_count = 0U;
678 0 : dump_prior_vote_accounts(
679 0 : vote_states,
680 0 : dumped_accounts_pool,
681 0 : &dumped_accounts_root,
682 0 : block_context->epoch_ctx.vote_accounts_t_2,
683 0 : &block_context->epoch_ctx.vote_accounts_t_2_count,
684 0 : spad,
685 0 : 0 );
686 :
687 0 : fd_bank_vote_states_end_locking_query( parent_bank );
688 :
689 : /* BlockContext -> acct_states
690 : Iterate over the set and dump all the account keys in one pass. */
691 0 : block_context->acct_states_count = 0U;
692 0 : block_context->acct_states = fd_spad_alloc(
693 0 : spad,
694 0 : alignof(fd_exec_test_acct_state_t),
695 0 : fd_dump_account_key_map_size( dumped_accounts_pool, dumped_accounts_root )*sizeof(fd_exec_test_acct_state_t) );
696 0 : for( fd_dump_account_key_node_t * node = fd_dump_account_key_map_minimum( dumped_accounts_pool, dumped_accounts_root );
697 0 : node;
698 0 : node = fd_dump_account_key_map_successor( dumped_accounts_pool, node ) ) {
699 0 : fd_accdb_ro_t ro[1];
700 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, ro, &parent_xid, &node->key ) ) ) {
701 0 : continue;
702 0 : }
703 0 : dump_account_state(
704 0 : fd_accdb_ref_address( ro ),
705 0 : ro->meta,
706 0 : &block_context->acct_states[block_context->acct_states_count++],
707 0 : spad );
708 0 : fd_accdb_close_ro( accdb, ro );
709 0 : }
710 :
711 : /* BlockContext -> blockhash_queue */
712 0 : fd_blockhashes_t const * bhq = fd_bank_block_hash_queue_query( parent_bank );
713 0 : block_context->blockhash_queue = fd_spad_alloc(
714 0 : spad,
715 0 : alignof(pb_bytes_array_t *),
716 0 : PB_BYTES_ARRAY_T_ALLOCSIZE((FD_BLOCKHASHES_MAX) * sizeof(pb_bytes_array_t *)) );
717 0 : block_context->blockhash_queue_count = 0U;
718 0 : dump_blockhash_queue( bhq, spad, block_context->blockhash_queue, &block_context->blockhash_queue_count );
719 :
720 : /* BlockContext -> SlotContext */
721 0 : block_context->has_slot_ctx = true;
722 0 : block_context->slot_ctx.slot = current_slot;
723 0 : block_context->slot_ctx.block_height = fd_bank_block_height_get( bank );
724 0 : block_context->slot_ctx.prev_slot = fd_bank_parent_slot_get( bank );
725 :
726 : // We need to store the POH hash for the current block since we don't
727 : // recalculate it in the harnesses.
728 0 : fd_memcpy( block_context->slot_ctx.poh, fd_bank_poh_query( bank ), sizeof(fd_pubkey_t) );
729 0 : fd_memcpy( block_context->slot_ctx.parent_bank_hash, fd_bank_bank_hash_query( parent_bank ), sizeof(fd_pubkey_t) );
730 :
731 0 : fd_lthash_value_t const * parent_lthash = fd_bank_lthash_locking_query( parent_bank );
732 0 : fd_memcpy( block_context->slot_ctx.parent_lthash, parent_lthash, sizeof(fd_lthash_value_t) );
733 0 : fd_bank_lthash_end_locking_query( parent_bank );
734 :
735 0 : block_context->slot_ctx.prev_epoch_capitalization = fd_bank_capitalization_get( parent_bank );
736 :
737 : /* BlockContext -> SlotContext -> fee_rate_governor */
738 0 : fd_fee_rate_governor_t const * fee_rate_governor = fd_bank_fee_rate_governor_query( parent_bank );
739 0 : block_context->slot_ctx.has_fee_rate_governor = true;
740 0 : block_context->slot_ctx.fee_rate_governor = (fd_exec_test_fee_rate_governor_t){
741 0 : .target_lamports_per_signature = fee_rate_governor->target_lamports_per_signature,
742 0 : .target_signatures_per_slot = fee_rate_governor->target_signatures_per_slot,
743 0 : .min_lamports_per_signature = fee_rate_governor->min_lamports_per_signature,
744 0 : .max_lamports_per_signature = fee_rate_governor->max_lamports_per_signature,
745 0 : .burn_percent = fee_rate_governor->burn_percent,
746 0 : };
747 :
748 : /* BlockContext -> EpochContext */
749 0 : block_context->has_epoch_ctx = true;
750 0 : block_context->epoch_ctx.has_features = true;
751 0 : dump_sorted_features( fd_bank_features_query( parent_bank ), &block_context->epoch_ctx.features, spad );
752 0 : block_context->epoch_ctx.hashes_per_tick = fd_bank_hashes_per_tick_get( parent_bank );
753 0 : block_context->epoch_ctx.ticks_per_slot = fd_bank_ticks_per_slot_get( parent_bank );
754 0 : block_context->epoch_ctx.has_inflation = true;
755 :
756 0 : fd_inflation_t const * inflation = fd_bank_inflation_query( parent_bank );
757 0 : block_context->epoch_ctx.inflation = (fd_exec_test_inflation_t) {
758 0 : .initial = inflation->initial,
759 0 : .terminal = inflation->terminal,
760 0 : .taper = inflation->taper,
761 0 : .foundation = inflation->foundation,
762 0 : .foundation_term = inflation->foundation_term,
763 0 : };
764 0 : block_context->epoch_ctx.genesis_creation_time = fd_bank_genesis_creation_time_get( parent_bank );
765 0 : }
766 :
767 : static void
768 : create_txn_context_protobuf_from_txn( fd_exec_test_txn_context_t * txn_context_msg,
769 : fd_runtime_t * runtime,
770 : fd_bank_t * bank,
771 : fd_txn_in_t const * txn_in,
772 : fd_txn_out_t * txn_out,
773 0 : fd_spad_t * spad ) {
774 0 : fd_txn_t const * txn_descriptor = TXN( txn_in->txn );
775 0 : uchar const * txn_payload = (uchar const *) txn_in->txn->payload;
776 :
777 : /* Transaction Context -> account_shared_data
778 : Contains:
779 : - Account data for regular accounts
780 : - Account data for LUT accounts
781 : - Account data for executable accounts
782 : - Account data for (almost) all sysvars
783 :
784 : We also don't want to store builtins in account shared data due to
785 : how Agave's bank handles them in the init phase. */
786 : // Dump regular accounts first
787 0 : txn_context_msg->account_shared_data_count = 0;
788 0 : txn_context_msg->account_shared_data = fd_spad_alloc( spad,
789 0 : alignof(fd_exec_test_acct_state_t),
790 0 : (256UL*2UL + txn_descriptor->addr_table_lookup_cnt + num_sysvar_entries) * sizeof(fd_exec_test_acct_state_t) );
791 0 : fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
792 0 : for( ulong i = 0; i < txn_out->accounts.cnt; ++i ) {
793 : // Make sure account is not a non-migrating builtin
794 0 : if( !is_builtin_account( &txn_out->accounts.keys[i] ) ) {
795 0 : dump_account_if_not_already_dumped(
796 0 : runtime->accdb,
797 0 : &xid,
798 0 : &txn_out->accounts.keys[i],
799 0 : spad,
800 0 : txn_context_msg->account_shared_data,
801 0 : &txn_context_msg->account_shared_data_count,
802 0 : NULL
803 0 : );
804 0 : }
805 0 : }
806 :
807 : // Dump LUT accounts
808 0 : fd_txn_acct_addr_lut_t const * address_lookup_tables = fd_txn_get_address_tables_const( txn_descriptor );
809 0 : for( ulong i = 0; i < txn_descriptor->addr_table_lookup_cnt; ++i ) {
810 0 : fd_txn_acct_addr_lut_t const * addr_lut = &address_lookup_tables[i];
811 0 : fd_pubkey_t * alut_key = (fd_pubkey_t *) (txn_payload + addr_lut->addr_off);
812 :
813 : // Dump the LUT account itself if not already dumped
814 0 : fd_accdb_ro_t ro[1];
815 0 : int ret = dump_account_if_not_already_dumped(
816 0 : runtime->accdb,
817 0 : &xid,
818 0 : alut_key,
819 0 : spad,
820 0 : txn_context_msg->account_shared_data,
821 0 : &txn_context_msg->account_shared_data_count,
822 0 : ro
823 0 : );
824 0 : if( FD_UNLIKELY( ret ) ) continue;
825 :
826 0 : uchar const * alut_data = fd_accdb_ref_data_const( ro );
827 0 : ulong alut_sz = fd_accdb_ref_data_sz ( ro );
828 :
829 0 : if( FD_UNLIKELY( alut_sz<FD_LOOKUP_TABLE_META_SIZE ) ) {
830 : /* Skip over invalid address lookup tables */
831 0 : fd_accdb_close_ro( runtime->accdb, ro );
832 0 : continue;
833 0 : }
834 :
835 0 : fd_pubkey_t const * lookup_addrs = fd_type_pun_const( alut_data+FD_LOOKUP_TABLE_META_SIZE );
836 0 : ulong lookup_addrs_cnt = (alut_sz - FD_LOOKUP_TABLE_META_SIZE) / sizeof(fd_pubkey_t);
837 :
838 : /* Dump any account state refererenced in ALUTs */
839 0 : uchar const * writable_lut_idxs = txn_payload + addr_lut->writable_off;
840 0 : for( ulong j=0; j<addr_lut->writable_cnt; j++ ) {
841 0 : if( writable_lut_idxs[j] >= lookup_addrs_cnt ) {
842 0 : continue;
843 0 : }
844 0 : fd_pubkey_t const * referenced_addr = lookup_addrs + writable_lut_idxs[j];
845 0 : if( is_builtin_account( referenced_addr ) ) continue;
846 :
847 0 : dump_account_if_not_already_dumped(
848 0 : runtime->accdb,
849 0 : &xid,
850 0 : referenced_addr,
851 0 : spad,
852 0 : txn_context_msg->account_shared_data,
853 0 : &txn_context_msg->account_shared_data_count,
854 0 : NULL
855 0 : );
856 0 : }
857 :
858 0 : uchar const * readonly_lut_idxs = txn_payload + addr_lut->readonly_off;
859 0 : for( ulong j = 0; j < addr_lut->readonly_cnt; j++ ) {
860 0 : if( readonly_lut_idxs[j] >= lookup_addrs_cnt ) {
861 0 : continue;
862 0 : }
863 0 : fd_pubkey_t const * referenced_addr = lookup_addrs + readonly_lut_idxs[j];
864 0 : if( is_builtin_account( referenced_addr ) ) continue;
865 :
866 0 : dump_account_if_not_already_dumped(
867 0 : runtime->accdb,
868 0 : &xid,
869 0 : referenced_addr,
870 0 : spad,
871 0 : txn_context_msg->account_shared_data,
872 0 : &txn_context_msg->account_shared_data_count,
873 0 : NULL
874 0 : );
875 0 : }
876 :
877 0 : fd_accdb_close_ro( runtime->accdb, ro );
878 0 : }
879 :
880 : /* Dump the programdata accounts for any potential v3-owned program accounts */
881 0 : uint accounts_dumped_so_far = txn_context_msg->account_shared_data_count;
882 0 : for( uint i=0U; i<accounts_dumped_so_far; i++ ) {
883 0 : fd_exec_test_acct_state_t const * maybe_program_account = &txn_context_msg->account_shared_data[i];
884 0 : dump_executable_account_if_exists( runtime->accdb, &xid, maybe_program_account, spad, txn_context_msg->account_shared_data, &txn_context_msg->account_shared_data_count );
885 0 : }
886 :
887 : /* Dump sysvars */
888 0 : for( ulong i = 0; i < num_sysvar_entries; i++ ) {
889 0 : dump_account_if_not_already_dumped(
890 0 : runtime->accdb,
891 0 : &xid,
892 0 : fd_dump_sysvar_ids[i],
893 0 : spad,
894 0 : txn_context_msg->account_shared_data,
895 0 : &txn_context_msg->account_shared_data_count,
896 0 : NULL
897 0 : );
898 0 : }
899 :
900 : /* Transaction Context -> tx */
901 0 : txn_context_msg->has_tx = true;
902 0 : fd_exec_test_sanitized_transaction_t * sanitized_transaction = &txn_context_msg->tx;
903 0 : dump_sanitized_transaction( runtime->accdb, &xid, txn_descriptor, txn_payload, spad, sanitized_transaction );
904 :
905 : /* Transaction Context -> blockhash_queue
906 : NOTE: Agave's implementation of register_hash incorrectly allows the blockhash queue to hold max_age + 1 (max 301)
907 : entries. We have this incorrect logic implemented in fd_sysvar_recent_hashes:register_blockhash and it's not a
908 : huge issue, but something to keep in mind. */
909 0 : pb_bytes_array_t ** output_blockhash_queue = fd_spad_alloc(
910 0 : spad,
911 0 : alignof(pb_bytes_array_t *),
912 0 : PB_BYTES_ARRAY_T_ALLOCSIZE((FD_BLOCKHASHES_MAX) * sizeof(pb_bytes_array_t *)) );
913 0 : txn_context_msg->blockhash_queue = output_blockhash_queue;
914 0 : fd_blockhashes_t const * block_hash_queue = fd_bank_block_hash_queue_query( bank );
915 0 : dump_blockhash_queue( block_hash_queue, spad, output_blockhash_queue, &txn_context_msg->blockhash_queue_count );
916 :
917 : /* Transaction Context -> epoch_ctx */
918 0 : txn_context_msg->has_epoch_ctx = true;
919 0 : txn_context_msg->epoch_ctx.has_features = true;
920 0 : dump_sorted_features( fd_bank_features_query( bank ), &txn_context_msg->epoch_ctx.features, spad );
921 :
922 : /* Transaction Context -> slot_ctx */
923 0 : txn_context_msg->has_slot_ctx = true;
924 0 : txn_context_msg->slot_ctx.slot = fd_bank_slot_get( bank );
925 0 : }
926 :
927 : static void
928 : create_instr_context_protobuf_from_instructions( fd_exec_test_instr_context_t * instr_context,
929 : fd_runtime_t * runtime,
930 : fd_bank_t * bank,
931 : fd_txn_out_t * txn_out,
932 : fd_instr_info_t const * instr,
933 0 : fd_spad_t * spad ) {
934 : /* Program ID */
935 0 : fd_memcpy( instr_context->program_id, txn_out->accounts.keys[ instr->program_id ].uc, sizeof(fd_pubkey_t) );
936 :
937 0 : fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
938 :
939 : /* Accounts */
940 0 : instr_context->accounts_count = (pb_size_t) txn_out->accounts.cnt;
941 0 : instr_context->accounts = fd_spad_alloc( spad, alignof(fd_exec_test_acct_state_t), (instr_context->accounts_count + num_sysvar_entries + runtime->accounts.executable_cnt) * sizeof(fd_exec_test_acct_state_t));
942 0 : for( ulong i = 0; i < txn_out->accounts.cnt; i++ ) {
943 : // Copy account information over
944 0 : fd_account_meta_t * account_meta = txn_out->accounts.account[i].meta;
945 0 : fd_exec_test_acct_state_t * output_account = &instr_context->accounts[i];
946 0 : dump_account_state( &txn_out->accounts.keys[i], account_meta, output_account, spad );
947 0 : }
948 :
949 : /* Add sysvar cache variables */
950 0 : for( ulong i = 0; i < num_sysvar_entries; i++ ) {
951 0 : fd_accdb_ro_t ro[1];
952 0 : if( !fd_accdb_open_ro( runtime->accdb, ro, &xid, fd_dump_sysvar_ids[i] ) ) {
953 0 : continue;
954 0 : }
955 : // Make sure the account doesn't exist in the output accounts yet
956 0 : int account_exists = 0;
957 0 : for( ulong j = 0; j < txn_out->accounts.cnt; j++ ) {
958 0 : if( fd_pubkey_eq( &txn_out->accounts.keys[j], fd_dump_sysvar_ids[i] ) ) {
959 0 : account_exists = true;
960 0 : break;
961 0 : }
962 0 : }
963 :
964 : // Copy it into output
965 0 : if( !account_exists ) {
966 0 : fd_exec_test_acct_state_t * output_account = &instr_context->accounts[instr_context->accounts_count++];
967 0 : dump_account_state( fd_accdb_ref_address( ro ), ro->meta, output_account, spad );
968 0 : }
969 0 : fd_accdb_close_ro( runtime->accdb, ro );
970 0 : }
971 :
972 : /* Add executable accounts */
973 0 : for( ulong i = 0; i < runtime->accounts.executable_cnt; i++ ) {
974 : // Make sure the account doesn't exist in the output accounts yet
975 0 : fd_accdb_ro_t const * ro = &runtime->accounts.executable[i];
976 0 : bool account_exists = false;
977 0 : for( ulong j = 0; j < instr_context->accounts_count; j++ ) {
978 0 : if( 0 == memcmp( instr_context->accounts[j].address, fd_accdb_ref_address( ro ), sizeof(fd_pubkey_t) ) ) {
979 0 : account_exists = true;
980 0 : break;
981 0 : }
982 0 : }
983 : // Copy it into output
984 0 : if( !account_exists ) {
985 0 : fd_exec_test_acct_state_t * output_account = &instr_context->accounts[instr_context->accounts_count++];
986 0 : dump_account_state( fd_accdb_ref_address( ro ), ro->meta, output_account, spad );
987 0 : }
988 0 : }
989 :
990 : /* Instruction Accounts */
991 0 : instr_context->instr_accounts_count = (pb_size_t) instr->acct_cnt;
992 0 : instr_context->instr_accounts = fd_spad_alloc( spad, alignof(fd_exec_test_instr_acct_t), instr_context->instr_accounts_count * sizeof(fd_exec_test_instr_acct_t) );
993 0 : for( ushort i = 0; i < instr->acct_cnt; i++ ) {
994 0 : fd_exec_test_instr_acct_t * output_instr_account = &instr_context->instr_accounts[i];
995 :
996 0 : output_instr_account->index = instr->accounts[i].index_in_transaction;
997 0 : output_instr_account->is_writable = instr->accounts[i].is_writable;
998 0 : output_instr_account->is_signer = instr->accounts[i].is_signer;
999 0 : }
1000 :
1001 : /* Data */
1002 0 : instr_context->data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( instr->data_sz ) );
1003 0 : instr_context->data->size = (pb_size_t) instr->data_sz;
1004 0 : fd_memcpy( instr_context->data->bytes, instr->data, instr->data_sz );
1005 :
1006 : /* Compute Units */
1007 0 : instr_context->cu_avail = txn_out->details.compute_budget.compute_meter;
1008 :
1009 : /* Slot Context */
1010 0 : instr_context->has_slot_context = true;
1011 :
1012 : /* Epoch Context */
1013 0 : instr_context->has_epoch_context = true;
1014 0 : instr_context->epoch_context.has_features = true;
1015 0 : dump_sorted_features( fd_bank_features_query( bank ), &instr_context->epoch_context.features, spad );
1016 0 : }
1017 :
1018 : /***** PUBLIC APIs *****/
1019 :
1020 : void
1021 : fd_dump_instr_to_protobuf( fd_runtime_t * runtime,
1022 : fd_bank_t * bank,
1023 : fd_txn_in_t const * txn_in,
1024 : fd_txn_out_t * txn_out,
1025 : fd_instr_info_t * instr,
1026 0 : ushort instruction_idx ) {
1027 : /* Check program ID filter, if it exists */
1028 0 : if( runtime->log.dump_proto_ctx->has_dump_instr_program_id_filter &&
1029 0 : memcmp( txn_out->accounts.keys[ instr->program_id ].uc, runtime->log.dump_proto_ctx->dump_instr_program_id_filter, sizeof(fd_pubkey_t) ) ) {
1030 0 : return;
1031 0 : }
1032 :
1033 0 : fd_spad_t * spad = fd_spad_join( fd_spad_new( runtime->log.dumping_mem, 1UL<<28UL ) );
1034 :
1035 0 : FD_SPAD_FRAME_BEGIN( spad ) {
1036 : // Get base58-encoded tx signature
1037 0 : const fd_ed25519_sig_t * signatures = fd_txn_get_signatures( TXN( txn_in->txn ), txn_in->txn->payload );
1038 0 : char encoded_signature[FD_BASE58_ENCODED_64_SZ];
1039 0 : fd_base58_encode_64( signatures[0], NULL, encoded_signature );
1040 :
1041 0 : fd_exec_test_instr_context_t instr_context = FD_EXEC_TEST_INSTR_CONTEXT_INIT_DEFAULT;
1042 0 : create_instr_context_protobuf_from_instructions( &instr_context, runtime, bank, txn_out, instr, spad );
1043 :
1044 : /* Output to file */
1045 0 : ulong out_buf_size = 100 * 1024 * 1024;
1046 0 : uint8_t * out = fd_spad_alloc( spad, alignof(uchar) , out_buf_size );
1047 0 : pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
1048 0 : if (pb_encode(&stream, FD_EXEC_TEST_INSTR_CONTEXT_FIELDS, &instr_context)) {
1049 0 : char output_filepath[ PATH_MAX ];
1050 0 : snprintf( output_filepath, PATH_MAX, "%s/instr-%s-%hu.instrctx", runtime->log.dump_proto_ctx->dump_proto_output_dir, encoded_signature, instruction_idx );
1051 0 : FILE * file = fopen(output_filepath, "wb");
1052 0 : if( file ) {
1053 0 : fwrite( out, 1, stream.bytes_written, file );
1054 0 : fclose( file );
1055 0 : }
1056 0 : }
1057 0 : } FD_SPAD_FRAME_END;
1058 0 : }
1059 :
1060 : void
1061 : fd_dump_txn_to_protobuf( fd_runtime_t * runtime,
1062 : fd_bank_t * bank,
1063 : fd_txn_in_t const * txn_in,
1064 0 : fd_txn_out_t * txn_out ) {
1065 0 : fd_spad_t * spad = fd_spad_join( fd_spad_new( runtime->log.dumping_mem, 1UL<<28UL ) );
1066 :
1067 0 : FD_SPAD_FRAME_BEGIN( spad ) {
1068 : // Get base58-encoded tx signature
1069 0 : const fd_ed25519_sig_t * signatures = fd_txn_get_signatures( TXN( txn_in->txn ), txn_in->txn->payload );
1070 0 : char encoded_signature[FD_BASE58_ENCODED_64_SZ];
1071 0 : fd_base58_encode_64( signatures[0], NULL, encoded_signature );
1072 :
1073 0 : fd_exec_test_txn_context_t txn_context_msg = FD_EXEC_TEST_TXN_CONTEXT_INIT_DEFAULT;
1074 0 : create_txn_context_protobuf_from_txn( &txn_context_msg, runtime, bank, txn_in, txn_out, spad );
1075 :
1076 : /* Output to file */
1077 0 : ulong out_buf_size = 100UL<<20UL; // 100 MB
1078 0 : uchar * out = fd_spad_alloc( spad, alignof(uchar), out_buf_size );
1079 0 : pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
1080 0 : if( pb_encode( &stream, FD_EXEC_TEST_TXN_CONTEXT_FIELDS, &txn_context_msg ) ) {
1081 0 : char output_filepath[ PATH_MAX ];
1082 0 : snprintf( output_filepath, PATH_MAX, "%s/txn-%s.txnctx", runtime->log.dump_proto_ctx->dump_proto_output_dir, encoded_signature );
1083 0 : FILE * file = fopen(output_filepath, "wb");
1084 0 : if( file ) {
1085 0 : fwrite( out, 1, stream.bytes_written, file );
1086 0 : fclose( file );
1087 0 : }
1088 0 : }
1089 0 : } FD_SPAD_FRAME_END;
1090 0 : }
1091 :
1092 : void
1093 : fd_dump_block_to_protobuf_collect_tx( fd_block_dump_ctx_t * dump_block_ctx,
1094 0 : fd_txn_p_t const * txn ) {
1095 0 : if( FD_UNLIKELY( dump_block_ctx->txns_to_dump_cnt>=FD_BLOCK_DUMP_CTX_MAX_TXN_CNT ) ) {
1096 0 : FD_LOG_ERR(( "Please increase FD_BLOCK_DUMP_CTX_MAX_TXN_CNT to dump more than %lu transactions.", FD_BLOCK_DUMP_CTX_MAX_TXN_CNT ));
1097 0 : return;
1098 0 : }
1099 0 : fd_memcpy( &dump_block_ctx->txns_to_dump[dump_block_ctx->txns_to_dump_cnt++], txn, sizeof(fd_txn_p_t) );
1100 0 : }
1101 :
1102 : void
1103 : fd_dump_block_to_protobuf( fd_block_dump_ctx_t * dump_block_ctx,
1104 : fd_banks_t * banks,
1105 : fd_bank_t * bank,
1106 : fd_accdb_user_t * accdb,
1107 0 : fd_dump_proto_ctx_t const * dump_proto_ctx ) {
1108 0 : FD_SPAD_FRAME_BEGIN( dump_block_ctx->spad ) {
1109 0 : if( FD_UNLIKELY( dump_proto_ctx==NULL ) ) {
1110 0 : FD_LOG_WARNING(( "Protobuf dumping context may not be NULL when dumping blocks." ));
1111 0 : return;
1112 0 : }
1113 :
1114 0 : if( FD_UNLIKELY( dump_block_ctx==NULL ) ) {
1115 0 : FD_LOG_WARNING(( "Block dumping context may not be NULL when dumping blocks." ));
1116 0 : return;
1117 0 : }
1118 :
1119 : /* Dump the block context */
1120 0 : create_block_context_protobuf_from_block( dump_block_ctx, banks, bank, accdb );
1121 :
1122 : /* Output to file */
1123 0 : ulong out_buf_size = 1UL<<30UL; /* 1 GB */
1124 0 : uint8_t * out = fd_spad_alloc( dump_block_ctx->spad, alignof(uint8_t), out_buf_size );
1125 0 : pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
1126 0 : if( pb_encode( &stream, FD_EXEC_TEST_BLOCK_CONTEXT_FIELDS, &dump_block_ctx->block_context ) ) {
1127 0 : char output_filepath[ PATH_MAX ];
1128 0 : snprintf( output_filepath, PATH_MAX, "%s/block-%lu.blockctx", dump_proto_ctx->dump_proto_output_dir, fd_bank_slot_get( bank ) );
1129 0 : FILE * file = fopen(output_filepath, "wb");
1130 0 : if( file ) {
1131 0 : fwrite( out, 1, stream.bytes_written, file );
1132 0 : fclose( file );
1133 0 : }
1134 0 : }
1135 0 : } FD_SPAD_FRAME_END;
1136 0 : }
1137 :
1138 : void
1139 : fd_dump_vm_syscall_to_protobuf( fd_vm_t const * vm,
1140 0 : char const * fn_name ) {
1141 0 : char const * syscall_name_filter = vm->instr_ctx->runtime->log.dump_proto_ctx->dump_syscall_name_filter;
1142 0 : if( syscall_name_filter && strlen( syscall_name_filter ) && strcmp( syscall_name_filter, fn_name ) ) {
1143 0 : return;
1144 0 : }
1145 :
1146 0 : fd_spad_t * spad = fd_spad_join( fd_spad_new( vm->instr_ctx->runtime->log.dumping_mem, 1UL<<28UL ) );
1147 :
1148 0 : FD_SPAD_FRAME_BEGIN( spad ) {
1149 :
1150 0 : fd_ed25519_sig_t signature;
1151 0 : memcpy( signature, (uchar const *)vm->instr_ctx->txn_in->txn->payload + TXN( vm->instr_ctx->txn_in->txn )->signature_off, sizeof(fd_ed25519_sig_t) );
1152 0 : char encoded_signature[FD_BASE58_ENCODED_64_SZ];
1153 0 : fd_base58_encode_64( signature, NULL, encoded_signature );
1154 :
1155 0 : char filename[ PATH_MAX ];
1156 0 : snprintf( filename,
1157 0 : PATH_MAX,
1158 0 : "%s/syscall-%s-%s-%d-%hhu-%lu.sysctx",
1159 0 : vm->instr_ctx->runtime->log.dump_proto_ctx->dump_proto_output_dir,
1160 0 : fn_name,
1161 0 : encoded_signature,
1162 0 : vm->instr_ctx->runtime->instr.current_idx,
1163 0 : vm->instr_ctx->runtime->instr.stack_sz,
1164 0 : vm->cu );
1165 :
1166 : /* The generated filename should be unique for every call. Silently return otherwise. */
1167 0 : if( FD_UNLIKELY( access( filename, F_OK )!=-1 ) ) {
1168 0 : return;
1169 0 : }
1170 :
1171 0 : fd_exec_test_syscall_context_t sys_ctx = FD_EXEC_TEST_SYSCALL_CONTEXT_INIT_ZERO;
1172 :
1173 : /* SyscallContext -> vm_ctx */
1174 0 : sys_ctx.has_vm_ctx = 1;
1175 :
1176 : /* SyscallContext -> vm_ctx -> heap_max */
1177 0 : sys_ctx.vm_ctx.heap_max = vm->heap_max; /* should be equiv. to txn_ctx->heap_sz */
1178 :
1179 : /* SyscallContext -> vm_ctx -> rodata */
1180 0 : sys_ctx.vm_ctx.rodata = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->rodata_sz ) );
1181 0 : sys_ctx.vm_ctx.rodata->size = (pb_size_t) vm->rodata_sz;
1182 0 : fd_memcpy( sys_ctx.vm_ctx.rodata->bytes, vm->rodata, vm->rodata_sz );
1183 :
1184 : /* SyscallContext -> vm_ctx -> r0-11 */
1185 0 : sys_ctx.vm_ctx.r0 = vm->reg[0];
1186 0 : sys_ctx.vm_ctx.r1 = vm->reg[1];
1187 0 : sys_ctx.vm_ctx.r2 = vm->reg[2];
1188 0 : sys_ctx.vm_ctx.r3 = vm->reg[3];
1189 0 : sys_ctx.vm_ctx.r4 = vm->reg[4];
1190 0 : sys_ctx.vm_ctx.r5 = vm->reg[5];
1191 0 : sys_ctx.vm_ctx.r6 = vm->reg[6];
1192 0 : sys_ctx.vm_ctx.r7 = vm->reg[7];
1193 0 : sys_ctx.vm_ctx.r8 = vm->reg[8];
1194 0 : sys_ctx.vm_ctx.r9 = vm->reg[9];
1195 0 : sys_ctx.vm_ctx.r10 = vm->reg[10];
1196 0 : sys_ctx.vm_ctx.r11 = vm->reg[11];
1197 :
1198 : /* SyscallContext -> vm_ctx -> entry_pc */
1199 0 : sys_ctx.vm_ctx.entry_pc = vm->entry_pc;
1200 :
1201 : /* SyscallContext -> vm_ctx -> return_data */
1202 0 : sys_ctx.vm_ctx.has_return_data = 1;
1203 :
1204 : /* SyscallContext -> vm_ctx -> return_data -> data */
1205 0 : sys_ctx.vm_ctx.return_data.data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->instr_ctx->txn_out->details.return_data.len ) );
1206 0 : sys_ctx.vm_ctx.return_data.data->size = (pb_size_t)vm->instr_ctx->txn_out->details.return_data.len;
1207 0 : fd_memcpy( sys_ctx.vm_ctx.return_data.data->bytes, vm->instr_ctx->txn_out->details.return_data.data, vm->instr_ctx->txn_out->details.return_data.len );
1208 :
1209 : /* SyscallContext -> vm_ctx -> return_data -> program_id */
1210 0 : sys_ctx.vm_ctx.return_data.program_id = fd_spad_alloc( spad, alignof(pb_bytes_array_t), sizeof(fd_pubkey_t) );
1211 0 : sys_ctx.vm_ctx.return_data.program_id->size = sizeof(fd_pubkey_t);
1212 0 : fd_memcpy( sys_ctx.vm_ctx.return_data.program_id->bytes, vm->instr_ctx->txn_out->details.return_data.program_id.key, sizeof(fd_pubkey_t) );
1213 :
1214 : /* SyscallContext -> vm_ctx -> sbpf_version */
1215 0 : sys_ctx.vm_ctx.sbpf_version = (uint)vm->sbpf_version;
1216 :
1217 : /* SyscallContext -> instr_ctx */
1218 0 : sys_ctx.has_instr_ctx = 1;
1219 0 : create_instr_context_protobuf_from_instructions( &sys_ctx.instr_ctx,
1220 0 : vm->instr_ctx->runtime,
1221 0 : vm->instr_ctx->bank,
1222 0 : vm->instr_ctx->txn_out,
1223 0 : vm->instr_ctx->instr,
1224 0 : spad );
1225 :
1226 : /* SyscallContext -> syscall_invocation */
1227 0 : sys_ctx.has_syscall_invocation = 1;
1228 :
1229 : /* SyscallContext -> syscall_invocation -> function_name */
1230 0 : sys_ctx.syscall_invocation.function_name.size = fd_uint_min( (uint) strlen(fn_name), sizeof(sys_ctx.syscall_invocation.function_name.bytes) );
1231 0 : fd_memcpy( sys_ctx.syscall_invocation.function_name.bytes,
1232 0 : fn_name,
1233 0 : sys_ctx.syscall_invocation.function_name.size );
1234 :
1235 : /* SyscallContext -> syscall_invocation -> heap_prefix */
1236 0 : sys_ctx.syscall_invocation.heap_prefix = fd_spad_alloc( spad, 8UL, PB_BYTES_ARRAY_T_ALLOCSIZE( vm->heap_max ) );
1237 0 : sys_ctx.syscall_invocation.heap_prefix->size = (pb_size_t) vm->instr_ctx->txn_out->details.compute_budget.heap_size;
1238 0 : fd_memcpy( sys_ctx.syscall_invocation.heap_prefix->bytes, vm->heap, vm->instr_ctx->txn_out->details.compute_budget.heap_size );
1239 :
1240 : /* SyscallContext -> syscall_invocation -> stack_prefix */
1241 0 : pb_size_t stack_sz = (pb_size_t)FD_VM_STACK_MAX;
1242 0 : sys_ctx.syscall_invocation.stack_prefix = fd_spad_alloc( spad, 8UL, PB_BYTES_ARRAY_T_ALLOCSIZE( stack_sz ) );
1243 0 : sys_ctx.syscall_invocation.stack_prefix->size = stack_sz;
1244 0 : fd_memcpy( sys_ctx.syscall_invocation.stack_prefix->bytes, vm->stack, stack_sz );
1245 :
1246 : /* Output to file */
1247 0 : ulong out_buf_size = 1UL<<29UL; /* 128 MB */
1248 0 : uint8_t * out = fd_spad_alloc( spad, alignof(uint8_t), out_buf_size );
1249 0 : pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
1250 0 : if( pb_encode( &stream, FD_EXEC_TEST_SYSCALL_CONTEXT_FIELDS, &sys_ctx ) ) {
1251 0 : FILE * file = fopen(filename, "wb");
1252 0 : if( file ) {
1253 0 : fwrite( out, 1, stream.bytes_written, file );
1254 0 : fclose( file );
1255 0 : }
1256 0 : }
1257 0 : } FD_SPAD_FRAME_END;
1258 0 : }
1259 :
1260 : void
1261 : fd_dump_elf_to_protobuf( fd_runtime_t * runtime,
1262 : fd_bank_t * bank,
1263 : fd_txn_in_t const * txn_in,
1264 : fd_pubkey_t const * acc_pubkey,
1265 0 : fd_account_meta_t const * acc_meta ) {
1266 0 : fd_spad_t * spad = fd_spad_join( fd_spad_new( runtime->log.dumping_mem, 1UL<<28UL ) );
1267 :
1268 0 : FD_SPAD_FRAME_BEGIN( spad ) {
1269 :
1270 0 : fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
1271 :
1272 0 : fd_accdb_ro_t program_acc_ro[1];
1273 0 : fd_accdb_ro_init_nodb( program_acc_ro, acc_pubkey, acc_meta );
1274 :
1275 : /* Get the programdata for the account */
1276 0 : ulong program_data_off = 0UL;
1277 0 : ulong program_data_len = 0UL;
1278 0 : fd_accdb_ro_t * program_data =
1279 0 : fd_prog_load_elf( runtime->accdb, &xid, program_acc_ro, acc_pubkey, &program_data_off );
1280 0 : if( program_data==NULL ) {
1281 0 : return;
1282 0 : }
1283 :
1284 : /* Serialize the ELF to protobuf */
1285 0 : fd_ed25519_sig_t signature;
1286 0 : memcpy( signature, (uchar const *)txn_in->txn->payload + TXN( txn_in->txn )->signature_off, sizeof(fd_ed25519_sig_t) );
1287 0 : char encoded_signature[FD_BASE58_ENCODED_64_SZ];
1288 0 : fd_base58_encode_64( signature, NULL, encoded_signature );
1289 :
1290 0 : FD_BASE58_ENCODE_32_BYTES( acc_pubkey->uc, program_acc_b58 );
1291 0 : char filename[ PATH_MAX ];
1292 0 : snprintf( filename,
1293 0 : PATH_MAX,
1294 0 : "%s/elf-%s-%s-%lu.elfctx",
1295 0 : runtime->log.dump_proto_ctx->dump_proto_output_dir,
1296 0 : encoded_signature,
1297 0 : program_acc_b58,
1298 0 : fd_bank_slot_get( bank ) );
1299 :
1300 : /* The generated filename should be unique for every call. Silently return otherwise. */
1301 0 : if( FD_UNLIKELY( access( filename, F_OK )!=-1 ) ) {
1302 0 : return;
1303 0 : }
1304 :
1305 0 : fd_exec_test_elf_loader_ctx_t elf_ctx = FD_EXEC_TEST_ELF_LOADER_CTX_INIT_ZERO;
1306 :
1307 : /* ElfLoaderCtx -> elf */
1308 0 : elf_ctx.has_elf = true;
1309 0 : elf_ctx.elf.data = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( program_data_len ) );
1310 0 : elf_ctx.elf.data->size = (pb_size_t)program_data_len;
1311 0 : fd_memcpy( elf_ctx.elf.data->bytes + program_data_off,
1312 0 : fd_accdb_ref_data_const( program_acc_ro ),
1313 0 : program_data_len );
1314 :
1315 : /* ElfLoaderCtx -> features */
1316 0 : elf_ctx.has_features = true;
1317 0 : dump_sorted_features( fd_bank_features_query( bank ), &elf_ctx.features, spad );
1318 :
1319 : /* ElfLoaderCtx -> deploy_checks
1320 : We hardcode this to true and rely the fuzzer to toggle this as it pleases */
1321 0 : elf_ctx.deploy_checks = true;
1322 :
1323 : /* Output to file */
1324 0 : ulong out_buf_size = 1UL<<29UL; /* 128 MB */
1325 0 : uint8_t * out = fd_spad_alloc( spad, alignof(uint8_t), out_buf_size );
1326 0 : pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
1327 0 : if( pb_encode( &stream, FD_EXEC_TEST_ELF_LOADER_CTX_FIELDS, &elf_ctx ) ) {
1328 0 : FILE * file = fopen(filename, "wb");
1329 0 : if( file ) {
1330 0 : fwrite( out, 1, stream.bytes_written, file );
1331 0 : fclose( file );
1332 0 : }
1333 0 : }
1334 0 : } FD_SPAD_FRAME_END;
1335 0 : }
|