Line data Source code
1 : #include "../execle/fd_execle_err.h"
2 : #include "../../util/pod/fd_pod_format.h"
3 : #include "../../disco/metrics/fd_metrics.h"
4 :
5 : #include "../../choreo/tower/fd_tower_serdes.h"
6 : #include "../../discof/fd_startup.h"
7 : #include "../../discof/replay/fd_execrp.h"
8 : #include "../../flamenco/runtime/fd_bank.h"
9 : #include "../../flamenco/runtime/fd_txncache.h"
10 : #include "../../flamenco/runtime/fd_runtime.h"
11 : #include "../../flamenco/runtime/fd_executor.h"
12 : #include "../../flamenco/runtime/tests/fd_dump_pb.h"
13 : #include "../../flamenco/progcache/fd_progcache_user.h"
14 : #include "../../flamenco/log_collector/fd_log_collector_base.h"
15 : #include "../../disco/metrics/fd_metrics.h"
16 :
17 : #include <time.h>
18 : #include "generated/fd_execrp_tile_seccomp.h"
19 :
20 : /* The exec tile is responsible for executing single transactions. The
21 : tile receives a parsed transaction (fd_txn_p_t) and an identifier to
22 : which bank to execute against (index into the bank pool). With this,
23 : the exec tile is able to identify the correct bank and accounts
24 : database fork to execute the transaction against. The exec tile then
25 : commits the results of the transaction to the accounts db and makes
26 : any necessary updates to the bank. */
27 :
28 : typedef struct link_ctx {
29 : ulong idx;
30 : fd_wksp_t * mem;
31 : ulong chunk;
32 : ulong chunk0;
33 : ulong wmark;
34 : } link_ctx_t;
35 :
36 : struct fd_execrp_tile {
37 : ulong tile_idx;
38 :
39 : /* link-related data structures. */
40 : link_ctx_t replay_in[ 1 ];
41 : link_ctx_t execrp_replay_out[ 1 ]; /* TODO: Remove with solcap v2 */
42 :
43 : fd_sha512_t sha_mem[ FD_TXN_ACTUAL_SIG_MAX ];
44 : fd_sha512_t * sha_lj[ FD_TXN_ACTUAL_SIG_MAX ];
45 :
46 : /* Capture context for debugging runtime execution. */
47 : fd_capture_ctx_t * capture_ctx;
48 : fd_capture_link_buf_t cap_execrp_out[1];
49 :
50 : /* Protobuf dumping context for debugging runtime execution and
51 : collecting seed corpora. */
52 : fd_dump_proto_ctx_t * dump_proto_ctx;
53 : fd_txn_dump_ctx_t * txn_dump_ctx;
54 :
55 : fd_banks_t * banks;
56 : fd_bank_t * bank;
57 : fd_accdb_t * accdb;
58 : fd_txncache_t * txncache;
59 : fd_progcache_t progcache[1];
60 :
61 : ulong txn_idx;
62 : ulong slot;
63 : ulong dispatch_time_comp;
64 :
65 : fd_log_collector_t log_collector;
66 :
67 : fd_txn_in_t txn_in;
68 : fd_txn_out_t txn_out;
69 :
70 : fd_runtime_t runtime[1];
71 :
72 : struct {
73 : ulong sigverify_cnt;
74 : ulong poh_hash_cnt;
75 :
76 : /* Ticks spent loading txn accounts */
77 : ulong txn_load_cum_ticks;
78 :
79 : /* Ticks spent validating txn invariants (e.g. status cache, fee payer) */
80 : ulong txn_check_cum_ticks;
81 :
82 : /* Ticks spent executing a txn (includes any VM time) */
83 : ulong txn_exec_cum_ticks;
84 :
85 : /* Ticks spent committing a txn (database writes) */
86 : ulong txn_commit_cum_ticks;
87 :
88 : ulong txn_result[ FD_METRICS_ENUM_TRANSACTION_RESULT_CNT ];
89 : } metrics;
90 : };
91 :
92 : typedef struct fd_execrp_tile fd_execrp_tile_t;
93 :
94 : FD_FN_CONST static inline ulong
95 0 : scratch_align( void ) {
96 0 : return 128UL;
97 0 : }
98 :
99 : FD_FN_PURE static inline ulong
100 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
101 0 : ulong l = FD_LAYOUT_INIT;
102 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_execrp_tile_t), sizeof(fd_execrp_tile_t) );
103 0 : l = FD_LAYOUT_APPEND( l, fd_txncache_align(), fd_txncache_footprint( tile->execrp.max_live_slots ) );
104 0 : l = FD_LAYOUT_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->execrp.max_live_slots ) );
105 0 : l = FD_LAYOUT_APPEND( l, FD_PROGCACHE_SCRATCH_ALIGN, FD_PROGCACHE_SCRATCH_FOOTPRINT );
106 :
107 0 : if( FD_UNLIKELY( strlen( tile->execrp.solcap_capture ) ) ) {
108 0 : l = FD_LAYOUT_APPEND( l, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
109 0 : }
110 :
111 0 : if( FD_UNLIKELY( strlen( tile->execrp.dump_proto_dir ) ) ) {
112 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_dump_proto_ctx_t), sizeof(fd_dump_proto_ctx_t) );
113 0 : l = FD_LAYOUT_APPEND( l, fd_txn_dump_context_align(), fd_txn_dump_context_footprint() );
114 0 : if( FD_UNLIKELY( tile->execrp.dump_instr_to_pb || tile->execrp.dump_syscall_to_pb || tile->execrp.dump_txn_to_pb ) ) {
115 0 : l = FD_LAYOUT_APPEND( l, FD_SPAD_ALIGN, FD_SPAD_FOOTPRINT( 1UL<<28UL ) );
116 0 : }
117 0 : }
118 :
119 0 : return FD_LAYOUT_FINI( l, scratch_align() );
120 0 : }
121 :
122 : static void
123 0 : metrics_write( fd_execrp_tile_t * ctx ) {
124 0 : FD_MCNT_SET ( EXECRP, SIGNATURE_VERIFIED, ctx->metrics.sigverify_cnt );
125 0 : FD_MCNT_SET ( EXECRP, POH_HASHED, ctx->metrics.poh_hash_cnt );
126 0 : FD_MCNT_ENUM_COPY( EXECRP, TXN_RESULT, ctx->metrics.txn_result );
127 :
128 0 : fd_progcache_metrics_t * pm = ctx->progcache->metrics;
129 0 : FD_MCNT_SET( EXECRP, PROGCACHE_LOOKUP, pm->lookup_cnt );
130 0 : FD_MCNT_SET( EXECRP, PROGCACHE_HIT, pm->hit_cnt );
131 0 : FD_MCNT_SET( EXECRP, PROGCACHE_MISS, pm->miss_cnt );
132 0 : FD_MCNT_SET( EXECRP, PROGCACHE_OOM_HEAP, pm->oom_heap_cnt );
133 0 : FD_MCNT_SET( EXECRP, PROGCACHE_OOM_DESC, pm->oom_desc_cnt );
134 0 : FD_MCNT_SET( EXECRP, PROGCACHE_FILL, pm->fill_cnt );
135 0 : FD_MCNT_SET( EXECRP, PROGCACHE_FILL_BYTES, pm->fill_tot_sz );
136 0 : FD_MCNT_SET( EXECRP, PROGCACHE_SPILL, pm->spill_cnt );
137 0 : FD_MCNT_SET( EXECRP, PROGCACHE_SPILL_BYTES, pm->spill_tot_sz );
138 0 : FD_MCNT_SET( EXECRP, PROGCACHE_EVICTION, pm->evict_cnt );
139 0 : FD_MCNT_SET( EXECRP, PROGCACHE_EVICTION_BYTES, pm->evict_tot_sz );
140 0 : FD_MCNT_SET( EXECRP, PROGCACHE_DURATION_SECONDS, pm->cum_pull_ticks );
141 0 : FD_MCNT_SET( EXECRP, PROGCACHE_LOAD_DURATION_SECONDS, pm->cum_load_ticks );
142 :
143 0 : FD_MCNT_SET( EXECRP, TXN_REGIME_DURATION_NANOS_SETUP, ctx->metrics.txn_load_cum_ticks+ctx->metrics.txn_check_cum_ticks );
144 0 : FD_MCNT_SET( EXECRP, TXN_REGIME_DURATION_NANOS_EXEC, ctx->metrics.txn_exec_cum_ticks );
145 0 : FD_MCNT_SET( EXECRP, TXN_REGIME_DURATION_NANOS_COMMIT, ctx->metrics.txn_commit_cum_ticks );
146 :
147 0 : fd_runtime_t const * runtime = ctx->runtime;
148 0 : ulong cpi_ticks = runtime->metrics.cpi_setup_cum_ticks +
149 0 : runtime->metrics.cpi_commit_cum_ticks;
150 0 : ulong exec_ticks = fd_ulong_sat_sub( runtime->metrics.vm_exec_cum_ticks, cpi_ticks );
151 0 : FD_MCNT_SET( EXECRP, VM_REGIME_DURATION_NANOS_SETUP, runtime->metrics.vm_setup_cum_ticks );
152 0 : FD_MCNT_SET( EXECRP, VM_REGIME_DURATION_NANOS_COMMIT, runtime->metrics.vm_commit_cum_ticks );
153 0 : FD_MCNT_SET( EXECRP, VM_REGIME_DURATION_NANOS_SETUP_CPI, runtime->metrics.cpi_setup_cum_ticks );
154 0 : FD_MCNT_SET( EXECRP, VM_REGIME_DURATION_NANOS_COMMIT_CPI, runtime->metrics.cpi_commit_cum_ticks );
155 0 : FD_MCNT_SET( EXECRP, VM_REGIME_DURATION_NANOS_INTERPRETER, exec_ticks );
156 :
157 0 : FD_MCNT_SET( EXECRP, CU_EXECUTED, runtime->metrics.cu_cum );
158 :
159 0 : FD_ACCDB_METRICS_WRITE( EXECRP, fd_accdb_metrics( ctx->accdb ) );
160 0 : }
161 :
162 : static void
163 : publish_txn_finalized_msg( fd_execrp_tile_t * ctx,
164 0 : fd_stem_context_t * stem ) {
165 0 : fd_execrp_task_done_msg_t * msg = fd_chunk_to_laddr( ctx->execrp_replay_out->mem, ctx->execrp_replay_out->chunk );
166 0 : msg->bank_idx = ctx->bank->idx;
167 0 : msg->txn_exec->txn_idx = ctx->txn_idx;
168 0 : msg->txn_exec->is_committable = ctx->txn_out.err.is_committable;
169 0 : msg->txn_exec->is_fees_only = ctx->txn_out.err.is_fees_only;
170 0 : msg->txn_exec->txn_err = ctx->txn_out.err.txn_err;
171 0 : msg->txn_exec->slot = ctx->slot;
172 0 : msg->txn_exec->start_shred_idx = ctx->txn_in.txn->start_shred_idx;
173 0 : msg->txn_exec->end_shred_idx = ctx->txn_in.txn->end_shred_idx;
174 :
175 0 : if( FD_UNLIKELY( !ctx->txn_out.details.is_simple_vote || !fd_txn_parse_simple_vote( TXN( ctx->txn_in.txn ), ctx->txn_in.txn->payload, msg->txn_exec->vote.identity, msg->txn_exec->vote.vote_acct, &msg->txn_exec->vote.slot ) ) ) {
176 0 : msg->txn_exec->vote.slot = ULONG_MAX;
177 0 : *msg->txn_exec->vote.identity = (fd_pubkey_t){ 0 };
178 0 : *msg->txn_exec->vote.vote_acct = (fd_pubkey_t){ 0 };
179 0 : }
180 :
181 0 : if( FD_UNLIKELY( !msg->txn_exec->is_committable ) ) {
182 0 : uchar * signature = (uchar *)ctx->txn_in.txn->payload + TXN( ctx->txn_in.txn )->signature_off;
183 0 : FD_BASE58_ENCODE_64_BYTES( signature, signature_b58 );
184 0 : FD_LOG_WARNING(( "block marked dead (slot=%lu) because of invalid transaction (signature=%s) (txn_err=%d)", ctx->slot, signature_b58, ctx->txn_out.err.txn_err ));
185 0 : }
186 :
187 0 : fd_stem_publish( stem, ctx->execrp_replay_out->idx, (FD_EXECRP_TT_TXN_EXEC<<32)|ctx->tile_idx, ctx->execrp_replay_out->chunk, sizeof(*msg), 0UL, ctx->dispatch_time_comp, fd_frag_meta_ts_comp( fd_tickcount() ) );
188 :
189 0 : ctx->execrp_replay_out->chunk = fd_dcache_compact_next( ctx->execrp_replay_out->chunk, sizeof(*msg), ctx->execrp_replay_out->chunk0, ctx->execrp_replay_out->wmark );
190 0 : }
191 :
192 : static inline int
193 : returnable_frag( fd_execrp_tile_t * ctx,
194 : ulong in_idx,
195 : ulong seq FD_PARAM_UNUSED,
196 : ulong sig,
197 : ulong chunk,
198 : ulong sz,
199 : ulong ctl FD_PARAM_UNUSED,
200 : ulong tsorig FD_PARAM_UNUSED,
201 : ulong tspub,
202 0 : fd_stem_context_t * stem ) {
203 0 : if( (sig&0xFFFFFFFFUL)!=ctx->tile_idx ) return 0;
204 :
205 0 : FD_MGAUGE_SET( EXECRP, PROCESSING, 1UL );
206 :
207 0 : if( FD_LIKELY( in_idx==ctx->replay_in->idx ) ) {
208 0 : if( FD_UNLIKELY( chunk < ctx->replay_in->chunk0 || chunk > ctx->replay_in->wmark ) ) {
209 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->replay_in->chunk0, ctx->replay_in->wmark ));
210 0 : }
211 0 : switch( sig>>32 ) {
212 0 : case FD_EXECRP_TT_TXN_EXEC: {
213 : /* Execute. */
214 0 : fd_execrp_txn_exec_msg_t * msg = fd_chunk_to_laddr( ctx->replay_in->mem, chunk );
215 0 : ctx->bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
216 0 : FD_TEST( ctx->bank );
217 0 : ctx->txn_in.txn = msg->txn;
218 :
219 : /* Set the capture txn index from the message so account updates
220 : during commit are recorded with the correct transaction index. */
221 0 : if( FD_UNLIKELY( ctx->capture_ctx ) ) {
222 0 : ctx->capture_ctx->current_txn_idx = msg->capture_txn_idx;
223 0 : }
224 :
225 0 : fd_runtime_prepare_and_execute_txn( ctx->runtime, ctx->bank, &ctx->txn_in, &ctx->txn_out );
226 :
227 0 : ctx->metrics.txn_result[ fd_execle_err_from_runtime_err( ctx->txn_out.err.txn_err ) ]++;
228 :
229 0 : if( FD_LIKELY( ctx->txn_out.err.is_committable ) ) {
230 0 : fd_runtime_commit_txn( ctx->runtime, ctx->bank, &ctx->txn_out );
231 0 : } else {
232 0 : fd_runtime_cancel_txn( ctx->runtime, &ctx->txn_out );
233 0 : }
234 :
235 0 : long const txn_end_ticks = fd_tickcount();
236 :
237 : /* Notify replay. */
238 0 : ctx->txn_idx = msg->txn_idx;
239 0 : ctx->dispatch_time_comp = tspub;
240 0 : ctx->slot = ctx->bank->f.slot;
241 0 : publish_txn_finalized_msg( ctx, stem );
242 :
243 : /* Update metrics */
244 0 : ulong load_start_ticks_dt = fd_ulong_if( ctx->txn_out.details.check_start_ticks==LONG_MAX || ctx->txn_out.details.load_start_ticks==LONG_MAX, 0UL, (ulong)( ctx->txn_out.details.check_start_ticks - ctx->txn_out.details.load_start_ticks ) );
245 0 : ulong check_start_ticks_dt = fd_ulong_if( ctx->txn_out.details.exec_start_ticks==LONG_MAX || ctx->txn_out.details.check_start_ticks==LONG_MAX, 0UL, (ulong)( ctx->txn_out.details.exec_start_ticks - ctx->txn_out.details.check_start_ticks ) );
246 0 : ulong exec_start_ticks_dt = fd_ulong_if( ctx->txn_out.details.commit_start_ticks==LONG_MAX || ctx->txn_out.details.exec_start_ticks==LONG_MAX, 0UL, (ulong)( ctx->txn_out.details.commit_start_ticks - ctx->txn_out.details.exec_start_ticks ) );
247 0 : ulong commit_ticks_dt = fd_ulong_if( txn_end_ticks==LONG_MAX || ctx->txn_out.details.commit_start_ticks==LONG_MAX, 0UL, (ulong)( txn_end_ticks - ctx->txn_out.details.commit_start_ticks ) );
248 :
249 0 : ctx->metrics.txn_load_cum_ticks += load_start_ticks_dt;
250 0 : ctx->metrics.txn_check_cum_ticks += check_start_ticks_dt;
251 0 : ctx->metrics.txn_exec_cum_ticks += exec_start_ticks_dt;
252 0 : ctx->metrics.txn_commit_cum_ticks += commit_ticks_dt;
253 :
254 0 : break;
255 0 : }
256 0 : case FD_EXECRP_TT_TXN_SIGVERIFY: {
257 0 : fd_execrp_txn_sigverify_msg_t * msg = fd_chunk_to_laddr( ctx->replay_in->mem, chunk );
258 0 : int res = fd_executor_txn_verify( msg->txn, ctx->sha_lj );
259 0 : fd_execrp_task_done_msg_t * out_msg = fd_chunk_to_laddr( ctx->execrp_replay_out->mem, ctx->execrp_replay_out->chunk );
260 0 : out_msg->bank_idx = msg->bank_idx;
261 0 : out_msg->txn_sigverify->txn_idx = msg->txn_idx;
262 0 : out_msg->txn_sigverify->err = (res!=FD_RUNTIME_EXECUTE_SUCCESS);
263 0 : fd_stem_publish( stem, ctx->execrp_replay_out->idx, (FD_EXECRP_TT_TXN_SIGVERIFY<<32)|ctx->tile_idx, ctx->execrp_replay_out->chunk, sizeof(*out_msg), 0UL, 0UL, 0UL );
264 0 : ctx->execrp_replay_out->chunk = fd_dcache_compact_next( ctx->execrp_replay_out->chunk, sizeof(*out_msg), ctx->execrp_replay_out->chunk0, ctx->execrp_replay_out->wmark );
265 0 : ctx->metrics.sigverify_cnt += TXN( msg->txn )->signature_cnt;
266 0 : break;
267 0 : }
268 0 : case FD_EXECRP_TT_POH_HASH: {
269 0 : fd_execrp_poh_hash_msg_t * msg = fd_chunk_to_laddr( ctx->replay_in->mem, chunk );
270 0 : fd_execrp_task_done_msg_t * out_msg = fd_chunk_to_laddr( ctx->execrp_replay_out->mem, ctx->execrp_replay_out->chunk );
271 0 : out_msg->bank_idx = msg->bank_idx;
272 0 : out_msg->poh_hash->mblk_idx = msg->mblk_idx;
273 0 : out_msg->poh_hash->hashcnt = msg->hashcnt;
274 0 : fd_sha256_hash_32_repeated( msg->hash, out_msg->poh_hash->hash, msg->hashcnt );
275 0 : fd_stem_publish( stem, ctx->execrp_replay_out->idx, (FD_EXECRP_TT_POH_HASH<<32)|ctx->tile_idx, ctx->execrp_replay_out->chunk, sizeof(*out_msg), 0UL, 0UL, 0UL );
276 0 : ctx->execrp_replay_out->chunk = fd_dcache_compact_next( ctx->execrp_replay_out->chunk, sizeof(*out_msg), ctx->execrp_replay_out->chunk0, ctx->execrp_replay_out->wmark );
277 0 : ctx->metrics.poh_hash_cnt += msg->hashcnt;
278 0 : break;
279 0 : }
280 0 : default: FD_LOG_CRIT(( "unexpected signature %lu", sig ));
281 0 : }
282 0 : } else FD_LOG_CRIT(( "invalid in_idx %lu", in_idx ));
283 :
284 0 : FD_MGAUGE_SET( EXECRP, PROCESSING, 0UL );
285 :
286 0 : return 0;
287 0 : }
288 :
289 : extern FD_TL int fd_wksp_oom_silent;
290 :
291 : static void
292 : unprivileged_init( fd_topo_t const * topo,
293 0 : fd_topo_tile_t const * tile ) {
294 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
295 :
296 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
297 0 : fd_execrp_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_execrp_tile_t), sizeof(fd_execrp_tile_t) );
298 0 : void * _txncache = FD_SCRATCH_ALLOC_APPEND( l, fd_txncache_align(), fd_txncache_footprint( tile->execrp.max_live_slots ) );
299 0 : void * _accdb = FD_SCRATCH_ALLOC_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->execrp.max_live_slots ) );
300 0 : uchar * pc_scratch = FD_SCRATCH_ALLOC_APPEND( l, FD_PROGCACHE_SCRATCH_ALIGN, FD_PROGCACHE_SCRATCH_FOOTPRINT );
301 :
302 0 : void * _capture_ctx = NULL;
303 0 : if( FD_UNLIKELY( strlen( tile->execrp.solcap_capture ) ) ) {
304 0 : _capture_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
305 0 : }
306 :
307 0 : void * _dump_proto_ctx = NULL;
308 0 : void * _txn_dump_ctx = NULL;
309 0 : void * _dumping = NULL;
310 0 : if( FD_UNLIKELY( strlen( tile->execrp.dump_proto_dir ) ) ) {
311 0 : _dump_proto_ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_dump_proto_ctx_t), sizeof(fd_dump_proto_ctx_t) );
312 0 : _txn_dump_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_txn_dump_context_align(), fd_txn_dump_context_footprint() );
313 0 : if( FD_UNLIKELY( tile->execrp.dump_instr_to_pb || tile->execrp.dump_syscall_to_pb || tile->execrp.dump_txn_to_pb ) ) {
314 0 : _dumping = FD_SCRATCH_ALLOC_APPEND( l, FD_SPAD_ALIGN, FD_SPAD_FOOTPRINT( 1UL<<28UL ) );
315 0 : }
316 0 : }
317 :
318 0 : for( ulong i=0UL; i<FD_TXN_ACTUAL_SIG_MAX; i++ ) {
319 0 : fd_sha512_t * sha = fd_sha512_join( fd_sha512_new( ctx->sha_mem+i ) );
320 0 : FD_TEST( sha );
321 0 : ctx->sha_lj[ i ] = sha;
322 0 : }
323 :
324 0 : ctx->txn_in.bundle.is_bundle = 0;
325 0 : ctx->tile_idx = tile->kind_id;
326 :
327 0 : ulong banks_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "banks" );
328 0 : FD_TEST( banks_obj_id!=ULONG_MAX );
329 :
330 0 : ctx->banks = fd_banks_join( fd_topo_obj_laddr( topo, banks_obj_id ) );
331 0 : FD_TEST( ctx->banks );
332 :
333 0 : FD_TEST( fd_progcache_join( ctx->progcache, fd_topo_obj_laddr( topo, tile->execrp.progcache_obj_id ), pc_scratch, FD_PROGCACHE_SCRATCH_FOOTPRINT ) );
334 :
335 0 : void * _txncache_shmem = fd_topo_obj_laddr( topo, tile->execrp.txncache_obj_id );
336 0 : fd_txncache_shmem_t * txncache_shmem = fd_txncache_shmem_join( _txncache_shmem );
337 0 : FD_TEST( txncache_shmem );
338 0 : ctx->txncache = fd_txncache_join( fd_txncache_new( _txncache, txncache_shmem ) );
339 0 : FD_TEST( ctx->txncache );
340 :
341 0 : void * _accdb_shmem = fd_topo_obj_laddr( topo, tile->execrp.accdb_obj_id );
342 0 : fd_accdb_shmem_t * accdb_shmem = fd_accdb_shmem_join( _accdb_shmem );
343 0 : FD_TEST( accdb_shmem );
344 0 : ctx->accdb = fd_accdb_join( fd_accdb_new( _accdb, accdb_shmem, FD_ACCDB_FD_RW, 0UL, NULL ) );
345 0 : FD_TEST( ctx->accdb );
346 :
347 :
348 : /* First find and setup the in-link from replay to exec. */
349 0 : ctx->replay_in->idx = fd_topo_find_tile_in_link( topo, tile, "replay_execrp", 0UL );
350 0 : FD_TEST( ctx->replay_in->idx!=ULONG_MAX );
351 0 : fd_topo_link_t const * replay_in_link = &topo->links[ tile->in_link_id[ ctx->replay_in->idx ] ];
352 0 : ctx->replay_in->mem = topo->workspaces[ topo->objs[ replay_in_link->dcache_obj_id ].wksp_id ].wksp;
353 0 : ctx->replay_in->chunk0 = fd_dcache_compact_chunk0( ctx->replay_in->mem, replay_in_link->dcache );
354 0 : ctx->replay_in->wmark = fd_dcache_compact_wmark( ctx->replay_in->mem, replay_in_link->dcache, replay_in_link->mtu );
355 0 : ctx->replay_in->chunk = ctx->replay_in->chunk0;
356 :
357 0 : ctx->execrp_replay_out->idx = fd_topo_find_tile_out_link( topo, tile, "execrp_replay", ctx->tile_idx );
358 0 : if( FD_LIKELY( ctx->execrp_replay_out->idx!=ULONG_MAX ) ) {
359 0 : fd_topo_link_t const * execrp_replay_link = &topo->links[ tile->out_link_id[ ctx->execrp_replay_out->idx ] ];
360 0 : ctx->execrp_replay_out->mem = topo->workspaces[ topo->objs[ execrp_replay_link->dcache_obj_id ].wksp_id ].wksp;
361 0 : ctx->execrp_replay_out->chunk0 = fd_dcache_compact_chunk0( ctx->execrp_replay_out->mem, execrp_replay_link->dcache );
362 0 : ctx->execrp_replay_out->wmark = fd_dcache_compact_wmark( ctx->execrp_replay_out->mem, execrp_replay_link->dcache, execrp_replay_link->mtu );
363 0 : ctx->execrp_replay_out->chunk = ctx->execrp_replay_out->chunk0;
364 0 : }
365 :
366 :
367 0 : ctx->capture_ctx = NULL;
368 0 : if( FD_UNLIKELY( strlen( tile->execrp.solcap_capture ) ) ) {
369 0 : ctx->capture_ctx = fd_capture_ctx_join( fd_capture_ctx_new( _capture_ctx ) );
370 0 : ctx->capture_ctx->solcap_start_slot = tile->execrp.capture_start_slot;
371 :
372 0 : ulong tile_idx = tile->kind_id;
373 0 : ulong idx = fd_topo_find_tile_out_link( topo, tile, "cap_execrp", tile_idx );
374 0 : FD_TEST( idx!=ULONG_MAX );
375 :
376 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ idx ] ];
377 0 : fd_capture_link_buf_t * cap_execrp_out = ctx->cap_execrp_out;
378 0 : cap_execrp_out->base.vt = &fd_capture_link_buf_vt;
379 0 : cap_execrp_out->idx = idx;
380 0 : cap_execrp_out->mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
381 0 : cap_execrp_out->chunk0 = fd_dcache_compact_chunk0( cap_execrp_out->mem, link->dcache );
382 0 : cap_execrp_out->wmark = fd_dcache_compact_wmark( cap_execrp_out->mem, link->dcache, link->mtu );
383 0 : cap_execrp_out->chunk = cap_execrp_out->chunk0;
384 0 : cap_execrp_out->mcache = link->mcache;
385 0 : cap_execrp_out->depth = fd_mcache_depth( link->mcache );
386 0 : cap_execrp_out->seq = 0UL;
387 :
388 0 : ulong consumer_tile_idx = fd_topo_find_tile(topo, "solcap", 0UL);
389 0 : fd_topo_tile_t const * consumer_tile = &topo->tiles[ consumer_tile_idx ];
390 0 : cap_execrp_out->fseq = NULL;
391 0 : for( ulong j = 0UL; j < consumer_tile->in_cnt; j++ ) {
392 0 : if( FD_UNLIKELY( consumer_tile->in_link_id[ j ] == link->id ) ) {
393 0 : cap_execrp_out->fseq = fd_fseq_join( fd_topo_obj_laddr( topo, consumer_tile->in_link_fseq_obj_id[ j ] ) );
394 0 : FD_TEST( cap_execrp_out->fseq );
395 0 : break;
396 0 : }
397 0 : }
398 :
399 0 : ctx->capture_ctx->capture_solcap = 1;
400 0 : ctx->capture_ctx->capctx_type.buf = cap_execrp_out;
401 0 : ctx->capture_ctx->capture_link = &cap_execrp_out->base;
402 0 : }
403 :
404 0 : ctx->dump_proto_ctx = NULL;
405 0 : if( FD_UNLIKELY( strlen( tile->execrp.dump_proto_dir ) ) ) {
406 0 : ctx->dump_proto_ctx = _dump_proto_ctx;
407 :
408 : /* General dumping config */
409 0 : ctx->dump_proto_ctx->dump_proto_output_dir = tile->execrp.dump_proto_dir;
410 0 : ctx->dump_proto_ctx->dump_proto_start_slot = tile->execrp.capture_start_slot;
411 :
412 : /* Syscall dumping config */
413 0 : ctx->dump_proto_ctx->dump_syscall_to_pb = !!tile->execrp.dump_syscall_to_pb;
414 0 : ctx->dump_proto_ctx->dump_syscall_name_filter = tile->execrp.dump_syscall_name_filter;
415 :
416 : /* Instruction dumping config */
417 0 : ctx->dump_proto_ctx->dump_instr_to_pb = !!tile->execrp.dump_instr_to_pb;
418 0 : ctx->dump_proto_ctx->has_dump_instr_program_id_filter = !!strlen(tile->execrp.dump_instr_program_id_filter);
419 0 : if( FD_UNLIKELY( ctx->dump_proto_ctx->has_dump_instr_program_id_filter &&
420 0 : !fd_base58_decode_32( tile->execrp.dump_instr_program_id_filter, ctx->dump_proto_ctx->dump_instr_program_id_filter ) ) ) {
421 0 : FD_LOG_ERR(( "failed to parse [capture.dump_instr_program_id_filter] %s", tile->execrp.dump_instr_program_id_filter ));
422 0 : }
423 :
424 : /* Transaction dumping config */
425 0 : ctx->dump_proto_ctx->dump_txn_to_pb = !!tile->execrp.dump_txn_to_pb;
426 0 : ctx->dump_proto_ctx->dump_txn_as_fixture = !!tile->execrp.dump_txn_as_fixture;
427 :
428 0 : if( FD_UNLIKELY( ctx->dump_proto_ctx->dump_txn_as_fixture && !ctx->dump_proto_ctx->dump_txn_to_pb ) ) {
429 0 : FD_LOG_ERR(( "[capture.dump_txn_as_fixture] requires [capture.dump_txn_to_pb] to be enabled" ));
430 0 : }
431 0 : }
432 :
433 : /* Transaction dump context (for fixture dumping) */
434 0 : ctx->txn_dump_ctx = NULL;
435 0 : if( FD_UNLIKELY( ctx->dump_proto_ctx && ctx->dump_proto_ctx->dump_txn_to_pb ) ) {
436 0 : ctx->txn_dump_ctx = fd_txn_dump_context_join( fd_txn_dump_context_new( _txn_dump_ctx ) );
437 0 : }
438 :
439 0 : ctx->runtime->accdb = ctx->accdb;
440 0 : ctx->runtime->progcache = ctx->progcache;
441 0 : ctx->runtime->status_cache = ctx->txncache;
442 0 : memset( &ctx->runtime->log, 0, sizeof(ctx->runtime->log) );
443 0 : ctx->runtime->log.log_collector = &ctx->log_collector;
444 0 : ctx->runtime->log.dumping_mem = _dumping;
445 0 : ctx->runtime->log.tracing_mem = NULL;
446 0 : ctx->runtime->log.capture_ctx = ctx->capture_ctx;
447 0 : ctx->runtime->log.dump_proto_ctx = ctx->dump_proto_ctx;
448 0 : ctx->runtime->log.txn_dump_ctx = ctx->txn_dump_ctx;
449 0 : ctx->runtime->fuzz.enabled = 0;
450 0 : ctx->runtime->fuzz.reclaim_accounts = 0;
451 0 : ctx->runtime->accounts.executable_cnt = 0UL;
452 0 : ctx->runtime->accounts.account_cnt = 0UL;
453 :
454 0 : memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
455 0 : memset( &ctx->runtime->metrics, 0, sizeof(ctx->runtime->metrics) );
456 :
457 0 : fd_wksp_oom_silent = 1;
458 :
459 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
460 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
461 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
462 :
463 0 : fd_sleep_until_replay_started( topo );
464 0 : }
465 :
466 : static ulong
467 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
468 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
469 : ulong out_cnt,
470 0 : struct sock_filter * out ) {
471 0 : populate_sock_filter_policy_fd_execrp_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)FD_ACCDB_FD_RW );
472 0 : return sock_filter_policy_fd_execrp_tile_instr_cnt;
473 0 : }
474 :
475 : static ulong
476 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
477 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
478 : ulong out_fds_cnt,
479 0 : int * out_fds ) {
480 :
481 0 : if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
482 :
483 0 : ulong out_cnt = 0UL;
484 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
485 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
486 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
487 0 : }
488 0 : out_fds[ out_cnt++ ] = FD_ACCDB_FD_RW; /* accounts db */
489 :
490 0 : return out_cnt;
491 0 : }
492 :
493 0 : #define STEM_BURST (1UL)
494 :
495 : /* Right now, depth of the replay_exec link and depth of the execrp_replay
496 : links is 16K. At 1M TPS, that's ~16ms to fill. But we also want to
497 : be conservative here, so we use 1ms. */
498 0 : #define STEM_LAZY (1000000UL)
499 :
500 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_execrp_tile_t
501 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_execrp_tile_t)
502 :
503 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
504 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
505 :
506 : #include "../../disco/stem/fd_stem.c"
507 :
508 : fd_topo_run_tile_t fd_tile_execrp = {
509 : .name = "execrp",
510 : .loose_footprint = 0UL,
511 : .populate_allowed_seccomp = populate_allowed_seccomp,
512 : .populate_allowed_fds = populate_allowed_fds,
513 : .scratch_align = scratch_align,
514 : .scratch_footprint = scratch_footprint,
515 : .unprivileged_init = unprivileged_init,
516 : .run = stem_run,
517 : };
|