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