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