Line data Source code
1 : #include "../tiles.h"
2 :
3 : #include "generated/fd_plugin_tile_seccomp.h"
4 :
5 : #include "../plugin/fd_plugin.h"
6 : #include "../../flamenco/types/fd_types.h"
7 :
8 0 : #define IN_KIND_REPLAY (0)
9 0 : #define IN_KIND_GOSSIP (1)
10 0 : #define IN_KIND_STAKE (2)
11 0 : #define IN_KIND_POH (3)
12 0 : #define IN_KIND_VOTE (4)
13 0 : #define IN_KIND_STARTP (5)
14 0 : #define IN_KIND_VOTEL (6)
15 0 : #define IN_KIND_BUNDLE (7)
16 0 : #define IN_KIND_VALCFG (8)
17 :
18 : typedef struct {
19 : fd_wksp_t * mem;
20 : ulong chunk0;
21 : ulong wmark;
22 : ulong mtu;
23 : } fd_plugin_in_ctx_t;
24 :
25 : typedef struct {
26 : int in_kind[ 64UL ];
27 : fd_plugin_in_ctx_t in[ 64UL ];
28 :
29 : fd_wksp_t * out_mem;
30 : ulong out_chunk0;
31 : ulong out_wmark;
32 : ulong out_chunk;
33 : } fd_plugin_ctx_t;
34 :
35 : FD_FN_CONST static inline ulong
36 3 : scratch_align( void ) {
37 3 : return 128UL;
38 3 : }
39 :
40 : FD_FN_PURE static inline ulong
41 3 : scratch_footprint( fd_topo_tile_t const * tile ) {
42 3 : (void)tile;
43 3 : ulong l = FD_LAYOUT_INIT;
44 3 : l = FD_LAYOUT_APPEND( l, alignof( fd_plugin_ctx_t ), sizeof( fd_plugin_ctx_t ) );
45 3 : return FD_LAYOUT_FINI( l, scratch_align() );
46 3 : }
47 :
48 : static inline void
49 : during_frag( fd_plugin_ctx_t * ctx,
50 : ulong in_idx,
51 : ulong seq FD_PARAM_UNUSED,
52 : ulong sig,
53 : ulong chunk,
54 : ulong sz,
55 0 : ulong ctl FD_PARAM_UNUSED ) {
56 :
57 0 : uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
58 0 : ulong * dst = (ulong *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
59 :
60 : /* ... todo... sigh, sz is not correct since it's too big */
61 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP && sig==FD_PLUGIN_MSG_GOSSIP_UPDATE ) ) {
62 0 : ulong peer_cnt = ((ulong *)src)[ 0 ];
63 0 : FD_TEST( peer_cnt<=40200 );
64 0 : sz = 8UL + peer_cnt*FD_GOSSIP_LINK_MSG_SIZE;
65 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP || ctx->in_kind[ in_idx ]==IN_KIND_POH || ctx->in_kind[ in_idx ]==IN_KIND_VOTE ) && FD_LIKELY( sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE ) ) {
66 0 : ulong peer_cnt = ((ulong *)src)[ 0 ];
67 0 : FD_TEST( peer_cnt<=40200 );
68 0 : sz = 8UL + peer_cnt*112UL;
69 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_STAKE ) ) {
70 0 : ulong staked_cnt = ((ulong *)src)[ 1 ];
71 0 : FD_TEST( staked_cnt<=50000UL );
72 0 : sz = 40UL + staked_cnt*40UL;
73 0 : }
74 :
75 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
76 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
77 :
78 0 : fd_memcpy( dst, src, sz );
79 0 : }
80 :
81 : static inline void
82 : after_frag( fd_plugin_ctx_t * ctx,
83 : ulong in_idx,
84 : ulong seq,
85 : ulong sig,
86 : ulong sz,
87 : ulong tsorig,
88 : ulong tspub,
89 0 : fd_stem_context_t * stem ) {
90 0 : (void)seq;
91 0 : (void)tsorig;
92 0 : (void)tspub;
93 0 : (void)stem;
94 :
95 0 : switch( ctx->in_kind[ in_idx ] ) {
96 0 : case IN_KIND_REPLAY: {
97 0 : FD_TEST( sig==FD_PLUGIN_MSG_SLOT_ROOTED || sig==FD_PLUGIN_MSG_SLOT_OPTIMISTICALLY_CONFIRMED || sig==FD_PLUGIN_MSG_SLOT_COMPLETED || sig==FD_PLUGIN_MSG_SLOT_RESET || sig==FD_PLUGIN_MSG_START_PROGRESS || sig==FD_PLUGIN_MSG_GENESIS_HASH_KNOWN );
98 0 : break;
99 0 : }
100 0 : case IN_KIND_GOSSIP: {
101 0 : FD_TEST( sig==FD_PLUGIN_MSG_GOSSIP_UPDATE || sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE || sig==FD_PLUGIN_MSG_BALANCE );
102 0 : break;
103 0 : }
104 0 : case IN_KIND_STAKE: {
105 0 : sig = FD_PLUGIN_MSG_LEADER_SCHEDULE;
106 0 : break;
107 0 : }
108 0 : case IN_KIND_POH: {
109 0 : FD_TEST( sig==FD_PLUGIN_MSG_SLOT_START || sig==FD_PLUGIN_MSG_SLOT_END );
110 0 : break;
111 0 : }
112 0 : case IN_KIND_VOTE: {
113 0 : FD_TEST( sig==FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE );
114 0 : break;
115 0 : }
116 0 : case IN_KIND_STARTP: {
117 0 : FD_TEST( sig==FD_PLUGIN_MSG_START_PROGRESS );
118 0 : break;
119 0 : }
120 0 : case IN_KIND_VOTEL: {
121 0 : FD_TEST( sig==FD_PLUGIN_MSG_SLOT_OPTIMISTICALLY_CONFIRMED );
122 0 : break;
123 0 : }
124 0 : case IN_KIND_BUNDLE: {
125 0 : FD_TEST( sig==FD_PLUGIN_MSG_BLOCK_ENGINE_UPDATE );
126 0 : break;
127 0 : }
128 0 : case IN_KIND_VALCFG: {
129 0 : FD_TEST( sig==FD_PLUGIN_MSG_VALIDATOR_INFO );
130 0 : break;
131 0 : }
132 0 : default: FD_LOG_ERR(( "bad in_idx" ));
133 0 : }
134 :
135 0 : ulong true_size = sz;
136 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP || ( ctx->in_kind[ in_idx ]==IN_KIND_VOTE ) ) ) true_size = 8UL + 40200UL*(58UL+12UL*34UL);
137 0 : else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_STAKE ) ) true_size = 40UL + 40200UL*40UL; /* ... todo... sigh, sz is not correct since it's too big */
138 :
139 0 : fd_stem_publish( stem, 0UL, sig, ctx->out_chunk, sz, 0UL, 0UL, 0UL ); /* Not true_sz which might not fit */
140 0 : ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, true_size, ctx->out_chunk0, ctx->out_wmark );
141 0 : }
142 :
143 : static void
144 : unprivileged_init( fd_topo_t * topo,
145 0 : fd_topo_tile_t * tile ) {
146 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
147 :
148 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
149 0 : fd_plugin_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_plugin_ctx_t ), sizeof( fd_plugin_ctx_t ) );
150 :
151 0 : FD_TEST( tile->in_cnt<=sizeof( ctx->in )/sizeof( ctx->in[ 0 ] ) );
152 0 : for( ulong i=0; i<tile->in_cnt; i++ ) {
153 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
154 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
155 :
156 0 : ctx->in[ i ].mem = link_wksp->wksp;
157 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
158 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
159 0 : ctx->in[ i ].mtu = link->mtu;
160 :
161 0 : FD_TEST( link->mtu<=topo->links[ tile->out_link_id[ 0 ] ].mtu );
162 :
163 0 : if( FD_UNLIKELY( !strcmp( link->name, "replay_plugi" ) ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY;
164 0 : else if( FD_UNLIKELY( !strcmp( link->name, "gossip_plugi" ) ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP;
165 0 : else if( FD_UNLIKELY( !strcmp( link->name, "stake_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_STAKE;
166 0 : else if( FD_UNLIKELY( !strcmp( link->name, "poh_plugin" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH;
167 0 : else if( FD_UNLIKELY( !strcmp( link->name, "votes_plugin" ) ) ) ctx->in_kind[ i ] = IN_KIND_VOTE;
168 0 : else if( FD_UNLIKELY( !strcmp( link->name, "startp_plugi" ) ) ) ctx->in_kind[ i ] = IN_KIND_STARTP;
169 0 : else if( FD_UNLIKELY( !strcmp( link->name, "votel_plugin" ) ) ) ctx->in_kind[ i ] = IN_KIND_VOTEL;
170 0 : else if( FD_UNLIKELY( !strcmp( link->name, "bundle_plugi" ) ) ) ctx->in_kind[ i ] = IN_KIND_BUNDLE;
171 0 : else if( FD_UNLIKELY( !strcmp( link->name, "valcfg_plugi" ) ) ) ctx->in_kind[ i ] = IN_KIND_VALCFG;
172 0 : else FD_LOG_ERR(( "unexpected link name %s", link->name ));
173 0 : }
174 :
175 0 : ctx->out_mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
176 0 : ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
177 0 : ctx->out_wmark = fd_dcache_compact_wmark ( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
178 0 : ctx->out_chunk = ctx->out_chunk0;
179 :
180 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
181 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
182 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
183 0 : }
184 :
185 : static ulong
186 : populate_allowed_seccomp( fd_topo_t const * topo,
187 : fd_topo_tile_t const * tile,
188 : ulong out_cnt,
189 0 : struct sock_filter * out ) {
190 0 : (void)topo;
191 0 : (void)tile;
192 :
193 0 : populate_sock_filter_policy_fd_plugin_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
194 0 : return sock_filter_policy_fd_plugin_tile_instr_cnt;
195 0 : }
196 :
197 : static ulong
198 : populate_allowed_fds( fd_topo_t const * topo,
199 : fd_topo_tile_t const * tile,
200 : ulong out_fds_cnt,
201 0 : int * out_fds ) {
202 0 : (void)topo;
203 0 : (void)tile;
204 :
205 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
206 :
207 0 : ulong out_cnt = 0UL;
208 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
209 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
210 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
211 0 : return out_cnt;
212 0 : }
213 :
214 0 : #define STEM_BURST (1UL)
215 :
216 : /* See explanation in fd_pack */
217 0 : #define STEM_LAZY (128L*3000L)
218 :
219 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_plugin_ctx_t
220 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_plugin_ctx_t)
221 :
222 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
223 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
224 :
225 : #include "../stem/fd_stem.c"
226 :
227 : fd_topo_run_tile_t fd_tile_plugin = {
228 : .name = "plugin",
229 : .populate_allowed_seccomp = populate_allowed_seccomp,
230 : .populate_allowed_fds = populate_allowed_fds,
231 : .scratch_align = scratch_align,
232 : .scratch_footprint = scratch_footprint,
233 : .unprivileged_init = unprivileged_init,
234 : .run = stem_run,
235 : };
|