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 : uchar reject_zero_lamports,
35 : ulong acc_idx,
36 : fd_account_meta_t * * meta_out );
37 :
38 : /* Activates features in the runtime given an input feature set. Fails
39 : if a passed-in feature is unknown / not supported. */
40 : int
41 : fd_solfuzz_pb_restore_features( fd_features_t * features,
42 : fd_exec_test_feature_set_t const * feature_set );
43 :
44 : #if FD_HAS_FLATCC
45 : /* Flatbuffers variant of the above. This function call should never
46 : fail (all passed in features should be supported). Throws FD_LOG_ERR
47 : if any unsupported features are inputted. */
48 : void
49 : fd_solfuzz_fb_restore_features( fd_features_t * features,
50 : SOL_COMPAT_NS(FeatureSet_table_t) feature_set );
51 : #endif
52 :
53 : typedef ulong( exec_test_run_pb_fn_t )( fd_solfuzz_runner_t *,
54 : void const *,
55 : void **,
56 : void *,
57 : ulong );
58 :
59 : static inline void
60 : fd_solfuzz_pb_execute_wrapper( fd_solfuzz_runner_t * runner,
61 : void const * input,
62 : void ** output,
63 0 : exec_test_run_pb_fn_t * exec_test_run_fn ) {
64 0 : ulong out_bufsz = 100000000; /* 100 MB */
65 0 : void * out0 = fd_spad_alloc( runner->spad, 1UL, out_bufsz );
66 0 : FD_TEST( out_bufsz <= fd_spad_alloc_max( runner->spad, 1UL ) );
67 :
68 0 : ulong out_used = exec_test_run_fn( runner, input, output, out0, out_bufsz );
69 0 : if( FD_UNLIKELY( !out_used ) ) {
70 0 : *output = NULL;
71 0 : }
72 0 : }
73 :
74 : typedef int( exec_test_run_fb_fn_t )( fd_solfuzz_runner_t *, void const * );
75 :
76 : #if FD_HAS_FLATCC
77 : /* Returns SOL_COMPAT_V2_SUCCESS on success and SOL_COMPAT_V2_FAILURE on
78 : failure */
79 : static inline int
80 : fd_solfuzz_fb_execute_wrapper( fd_solfuzz_runner_t * runner,
81 : void const * input,
82 0 : exec_test_run_fb_fn_t * exec_test_run_fn ) {
83 0 : FD_SPAD_FRAME_BEGIN( runner->spad ) {
84 0 : flatcc_builder_reset( runner->fb_builder );
85 0 : return exec_test_run_fn( runner, input );
86 0 : } FD_SPAD_FRAME_END;
87 0 : }
88 : #endif /* FD_HAS_FLATCC */
89 :
90 : /* Utils */
91 :
92 : static FD_FN_UNUSED void *
93 : sol_compat_decode_lenient( void * decoded,
94 : uchar const * in,
95 : ulong in_sz,
96 0 : pb_msgdesc_t const * decode_type ) {
97 0 : pb_istream_t istream = pb_istream_from_buffer( in, in_sz );
98 0 : int decode_ok = pb_decode_ex( &istream, decode_type, decoded, PB_DECODE_NOINIT );
99 0 : if( !decode_ok ) {
100 0 : pb_release( decode_type, decoded );
101 0 : return NULL;
102 0 : }
103 0 : return decoded;
104 0 : }
105 :
106 : static FD_FN_UNUSED void *
107 : sol_compat_decode( void * decoded,
108 : uchar const * in,
109 : ulong in_sz,
110 0 : pb_msgdesc_t const * decode_type ) {
111 0 : pb_istream_t istream = pb_istream_from_buffer( in, in_sz );
112 0 : int decode_ok = pb_decode_ex( &istream, decode_type, decoded, PB_DECODE_NOINIT );
113 0 : if( !decode_ok ) {
114 0 : pb_release( decode_type, decoded );
115 0 : return NULL;
116 0 : }
117 0 : ulong size;
118 0 : if( FD_UNLIKELY( !pb_get_encoded_size( &size, decode_type, decoded ) ) ) {
119 0 : pb_release( decode_type, decoded );
120 0 : return NULL;
121 0 : }
122 0 : if( FD_UNLIKELY( size != in_sz ) ) {
123 0 : pb_release( decode_type, decoded );
124 0 : return NULL;
125 0 : }
126 0 : return decoded;
127 0 : }
128 :
129 : static FD_FN_UNUSED void const *
130 : sol_compat_encode( uchar * out,
131 : ulong * out_sz,
132 : void const * to_encode,
133 0 : pb_msgdesc_t const * encode_type ) {
134 0 : pb_ostream_t ostream = pb_ostream_from_buffer( out, *out_sz );
135 0 : int encode_ok = pb_encode( &ostream, encode_type, to_encode );
136 0 : if( !encode_ok ) {
137 0 : return NULL;
138 0 : }
139 0 : *out_sz = ostream.bytes_written;
140 0 : return to_encode;
141 0 : }
142 :
143 : FD_PROTOTYPES_END
144 :
145 : #endif /* HEADER_fd_src_flamenco_runtime_tests_fd_solfuzz_private_h */
|