Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_runtime_tests_fd_solfuzz_private_h
2 : #define HEADER_fd_src_flamenco_runtime_tests_fd_solfuzz_private_h
3 :
4 : /* fd_solfuzz_private.h contains internal components for the solfuzz
5 : Protobuf shim. */
6 :
7 : #include "fd_solfuzz.h"
8 : #include "../../features/fd_features.h"
9 : #include "../../../ballet/nanopb/pb_encode.h"
10 : #include "../../../ballet/nanopb/pb_decode.h"
11 : #include "generated/context.pb.h"
12 :
13 : #if FD_HAS_FLATCC
14 : #include <flatcc/flatcc_builder.h>
15 : #include "flatbuffers/generated/context_reader.h"
16 : #endif
17 :
18 : FD_PROTOTYPES_BEGIN
19 :
20 : #undef ns
21 0 : #define SOL_COMPAT_NS(x) FLATBUFFERS_WRAP_NAMESPACE(fd_org_solana_sealevel_v2, x)
22 :
23 0 : #define SOL_COMPAT_V2_SUCCESS (0)
24 : #define SOL_COMPAT_V2_FAILURE (-1)
25 :
26 : /* Creates / overwrites an account in funk given an input account state.
27 : On success, loads the account into acc. Optionally, reject any
28 : zero-lamport accounts from being loaded in. */
29 : int
30 : fd_solfuzz_pb_load_account( fd_runtime_t * runtime,
31 : fd_accdb_user_t * accdb,
32 : fd_funk_txn_xid_t const * xid,
33 : fd_exec_test_acct_state_t const * state,
34 : ulong acc_idx );
35 :
36 : /* Restores the fee rate governor in the bank from the given protobuf
37 : fee rate governor. */
38 : void
39 : fd_solfuzz_pb_restore_fee_rate_governor( fd_bank_t * bank,
40 : fd_exec_test_fee_rate_governor_t const * fee_rate_governor );
41 :
42 : /* Restores the epoch schedule in the bank from the given protobuf
43 : epoch schedule. */
44 : void
45 : fd_solfuzz_pb_restore_epoch_schedule( fd_bank_t * bank,
46 : fd_exec_test_epoch_schedule_t const * epoch_schedule );
47 :
48 : /* Initializes the blockhash queue in the bank from the given protobuf
49 : blockhash queue entries. */
50 : void
51 : fd_solfuzz_pb_restore_blockhash_queue( fd_bank_t * bank,
52 : fd_exec_test_blockhash_queue_entry_t const * entries,
53 : ulong entries_cnt );
54 :
55 : /* Retrieves the slot number from the clock sysvar account within the
56 : given account states list. Throws FD_LOG_ERR if the clock sysvar
57 : is not found or is malformed. */
58 : ulong
59 : fd_solfuzz_pb_get_slot( fd_exec_test_acct_state_t const * acct_states,
60 : ulong acct_states_cnt );
61 :
62 : /* Activates features in the runtime given an input feature set. Fails
63 : if a passed-in feature is unknown / not supported. */
64 : int
65 : fd_solfuzz_pb_restore_features( fd_features_t * features,
66 : fd_exec_test_feature_set_t const * feature_set );
67 :
68 : #if FD_HAS_FLATCC
69 : /* Flatbuffers variant of the above. This function call should never
70 : fail (all passed in features should be supported). Throws FD_LOG_ERR
71 : if any unsupported features are inputted. */
72 : void
73 : fd_solfuzz_fb_restore_features( fd_features_t * features,
74 : SOL_COMPAT_NS(FeatureSet_table_t) feature_set );
75 : #endif
76 :
77 : typedef ulong( exec_test_run_pb_fn_t )( fd_solfuzz_runner_t *,
78 : void const *,
79 : void **,
80 : void *,
81 : ulong );
82 :
83 : static inline void
84 : fd_solfuzz_pb_execute_wrapper( fd_solfuzz_runner_t * runner,
85 : void const * input,
86 : void ** output,
87 0 : exec_test_run_pb_fn_t * exec_test_run_fn ) {
88 0 : ulong out_bufsz = 100000000; /* 100 MB */
89 0 : void * out0 = fd_spad_alloc( runner->spad, 1UL, out_bufsz );
90 0 : FD_TEST( out_bufsz <= fd_spad_alloc_max( runner->spad, 1UL ) );
91 :
92 0 : ulong out_used = exec_test_run_fn( runner, input, output, out0, out_bufsz );
93 0 : if( FD_UNLIKELY( !out_used ) ) {
94 0 : *output = NULL;
95 0 : }
96 0 : }
97 :
98 : typedef int( exec_test_run_fb_fn_t )( fd_solfuzz_runner_t *, void const * );
99 :
100 : #if FD_HAS_FLATCC
101 : /* Returns SOL_COMPAT_V2_SUCCESS on success and SOL_COMPAT_V2_FAILURE on
102 : failure */
103 : static inline int
104 : fd_solfuzz_fb_execute_wrapper( fd_solfuzz_runner_t * runner,
105 : void const * input,
106 0 : exec_test_run_fb_fn_t * exec_test_run_fn ) {
107 0 : FD_SPAD_FRAME_BEGIN( runner->spad ) {
108 0 : flatcc_builder_reset( runner->fb_builder );
109 0 : return exec_test_run_fn( runner, input );
110 0 : } FD_SPAD_FRAME_END;
111 0 : }
112 : #endif /* FD_HAS_FLATCC */
113 :
114 : /* Utils */
115 :
116 : static FD_FN_UNUSED void *
117 : sol_compat_decode_lenient( void * decoded,
118 : uchar const * in,
119 : ulong in_sz,
120 0 : pb_msgdesc_t const * decode_type ) {
121 0 : pb_istream_t istream = pb_istream_from_buffer( in, in_sz );
122 0 : int decode_ok = pb_decode_ex( &istream, decode_type, decoded, PB_DECODE_NOINIT );
123 0 : if( !decode_ok ) {
124 0 : pb_release( decode_type, decoded );
125 0 : return NULL;
126 0 : }
127 0 : return decoded;
128 0 : }
129 :
130 : static FD_FN_UNUSED void *
131 : sol_compat_decode( void * decoded,
132 : uchar const * in,
133 : ulong in_sz,
134 0 : pb_msgdesc_t const * decode_type ) {
135 0 : pb_istream_t istream = pb_istream_from_buffer( in, in_sz );
136 0 : int decode_ok = pb_decode_ex( &istream, decode_type, decoded, PB_DECODE_NOINIT );
137 0 : if( !decode_ok ) {
138 0 : pb_release( decode_type, decoded );
139 0 : return NULL;
140 0 : }
141 0 : ulong size;
142 0 : if( FD_UNLIKELY( !pb_get_encoded_size( &size, decode_type, decoded ) ) ) {
143 0 : pb_release( decode_type, decoded );
144 0 : return NULL;
145 0 : }
146 0 : if( FD_UNLIKELY( size != in_sz ) ) {
147 0 : pb_release( decode_type, decoded );
148 0 : return NULL;
149 0 : }
150 0 : return decoded;
151 0 : }
152 :
153 : static FD_FN_UNUSED void const *
154 : sol_compat_encode( uchar * out,
155 : ulong * out_sz,
156 : void const * to_encode,
157 0 : pb_msgdesc_t const * encode_type ) {
158 0 : pb_ostream_t ostream = pb_ostream_from_buffer( out, *out_sz );
159 0 : int encode_ok = pb_encode( &ostream, encode_type, to_encode );
160 0 : if( !encode_ok ) {
161 0 : return NULL;
162 0 : }
163 0 : *out_sz = ostream.bytes_written;
164 0 : return to_encode;
165 0 : }
166 :
167 : FD_PROTOTYPES_END
168 :
169 : #endif /* HEADER_fd_src_flamenco_runtime_tests_fd_solfuzz_private_h */
|