Line data Source code
1 : #include "fd_vm.h"
2 : #include "../../ballet/sbpf/fd_sbpf_instr.h"
3 : #include "../../ballet/sbpf/fd_sbpf_opcodes.h"
4 : #include "../../ballet/murmur3/fd_murmur3.h"
5 :
6 : /* fd_vm_disasm_printf appends to the *_len string in the max byte
7 : buffer buf the printf of the remaining args. On input, assumes *_len
8 : is strlen(buf) and *_len is in [0,max). On output, even on error
9 : cases, the leading string in buf will be unchanged, *len will be
10 : strlen(buf) and *len will be [*len_as_it_was_on_input,max).
11 :
12 : Returns:
13 :
14 : FD_VM_SUCCESS - success. buf and *len updated.
15 :
16 : FD_VM_ERR_FULL - not enough room in buf to hold result. As many
17 : bytes as possible were written to buf and *len==max-1 on return.
18 :
19 : FD_VM_ERR_IO - printf format parse error. buf and *len unchanged but
20 : trailing bytes of buf might have been clobbered. */
21 :
22 : /* FIXME: REWORK API TO USE FD_CSTR_PRINTF HERE? (OR CONSIDER ADDING
23 : FD_VM_DISASM_PRINTF AS A FD_CSTR API) */
24 :
25 : #include <stdio.h>
26 : #include <stdarg.h>
27 :
28 : static int
29 : fd_vm_disasm_printf( char * buf,
30 : ulong max,
31 : ulong * _len,
32 : char const * fmt, ... ) __attribute__((format(printf,4,5)));
33 :
34 : static int
35 : fd_vm_disasm_printf( char * buf,
36 : ulong max,
37 : ulong * _len,
38 28108332 : char const * fmt, ... ) {
39 28108332 : ulong len = *_len; /* In [0,max) */
40 28108332 : ulong rem = max - len; /* In (0,max] */
41 :
42 28108332 : va_list ap;
43 28108332 : va_start( ap, fmt );
44 28108332 : int ret = vsnprintf( buf + len, rem, fmt, ap );
45 28108332 : va_end( ap );
46 :
47 28108332 : if( FD_UNLIKELY( ret<0 ) ) { /* Parse error */
48 0 : buf[len] = '\0'; /* Guarantee '\0' termination */
49 0 : return FD_VM_ERR_IO;
50 0 : }
51 :
52 28108332 : ulong append_len = (ulong)ret; /* Guaranteed safe */
53 :
54 28108332 : if( FD_UNLIKELY( append_len>=rem ) ) { /* Truncated output */
55 0 : buf[max-1UL] = '\0'; /* Guarantee '\0' termination */
56 0 : *_len = max-1UL;
57 0 : return FD_VM_ERR_FULL;
58 0 : }
59 :
60 28108332 : *_len = len + append_len;
61 28108332 : return FD_VM_SUCCESS;
62 28108332 : }
63 :
64 : /* OUT_PRINTF is a convenience macro to do boilerplate error trapping
65 : on fd_vm_disasm_printf. */
66 :
67 28108332 : #define OUT_PRINTF( ... ) do { \
68 28108332 : int _err = fd_vm_disasm_printf( out, out_max, _out_len, __VA_ARGS__ ); \
69 28108332 : if( FD_UNLIKELY( _err ) ) return _err; \
70 28108332 : } while(0)
71 :
72 : /* fd_vm_disasm_instr_* are pretty printers for single word instructions.
73 : They do not validate their input arguments. Return out, out_max,
74 : _out_len and return error code have the same interpretation as their
75 : public facing wrappers. */
76 :
77 : static int
78 : fd_vm_disasm_instr_alu( fd_sbpf_instr_t instr,
79 : char const * suffix,
80 : char * out,
81 : ulong out_max,
82 7491786 : ulong * _out_len ) {
83 :
84 7491786 : char * op_name;
85 7491786 : switch( instr.opcode.normal.op_mode ) {
86 469560 : case FD_SBPF_OPCODE_ALU_OP_MODE_ADD: op_name = "add"; break;
87 466773 : case FD_SBPF_OPCODE_ALU_OP_MODE_SUB: op_name = "sub"; break;
88 467028 : case FD_SBPF_OPCODE_ALU_OP_MODE_MUL: op_name = "mul"; break;
89 468531 : case FD_SBPF_OPCODE_ALU_OP_MODE_DIV: op_name = "div"; break;
90 468024 : case FD_SBPF_OPCODE_ALU_OP_MODE_OR: op_name = "or"; break;
91 467013 : case FD_SBPF_OPCODE_ALU_OP_MODE_AND: op_name = "and"; break;
92 469251 : case FD_SBPF_OPCODE_ALU_OP_MODE_LSH: op_name = "lsh"; break;
93 467364 : case FD_SBPF_OPCODE_ALU_OP_MODE_RSH: op_name = "rsh"; break;
94 467880 : case FD_SBPF_OPCODE_ALU_OP_MODE_NEG: op_name = "neg"; break;
95 469461 : case FD_SBPF_OPCODE_ALU_OP_MODE_MOD: op_name = "mod"; break;
96 468945 : case FD_SBPF_OPCODE_ALU_OP_MODE_XOR: op_name = "xor"; break;
97 468300 : case FD_SBPF_OPCODE_ALU_OP_MODE_MOV: op_name = "mov"; break;
98 468492 : case FD_SBPF_OPCODE_ALU_OP_MODE_ARSH: op_name = "arsh"; break;
99 468606 : case FD_SBPF_OPCODE_ALU_OP_MODE_END:
100 468606 : OUT_PRINTF( "%s%u r%d", instr.opcode.normal.op_src==FD_SBPF_OPCODE_SOURCE_MODE_REG ? "be" : "le",
101 468606 : instr.imm, instr.dst_reg );
102 468606 : return FD_VM_SUCCESS;
103 936558 : default: return FD_VM_ERR_INVAL;
104 7491786 : }
105 :
106 6086622 : if( FD_UNLIKELY( instr.opcode.normal.op_mode==FD_SBPF_OPCODE_ALU_OP_MODE_NEG ) ) {
107 467880 : OUT_PRINTF( "%s%s r%d", op_name, suffix, instr.dst_reg );
108 467880 : return FD_VM_SUCCESS;
109 467880 : }
110 :
111 5618742 : switch( instr.opcode.normal.op_src ) {
112 2809119 : case FD_SBPF_OPCODE_SOURCE_MODE_IMM:
113 2809119 : OUT_PRINTF( "%s%s r%d, %d", op_name, suffix, instr.dst_reg, (int)instr.imm );
114 2809119 : return FD_VM_SUCCESS;
115 2809623 : case FD_SBPF_OPCODE_SOURCE_MODE_REG:
116 2809623 : OUT_PRINTF( "%s%s r%d, r%d", op_name, suffix, instr.dst_reg, instr.src_reg );
117 2809623 : return FD_VM_SUCCESS;
118 0 : default: break;
119 5618742 : }
120 :
121 0 : return FD_VM_ERR_INVAL;
122 5618742 : }
123 :
124 : static int
125 : fd_vm_disasm_instr_jmp( fd_sbpf_instr_t instr,
126 : ulong pc,
127 : char const * suffix,
128 : fd_sbpf_syscalls_t const * syscalls,
129 : char * out,
130 : ulong out_max,
131 7502157 : ulong * _out_len ) {
132 :
133 7502157 : char * op_name;
134 7502157 : switch( instr.opcode.normal.op_mode ) {
135 469752 : case FD_SBPF_OPCODE_JMP_OP_MODE_JA: op_name = "ja"; break;
136 466641 : case FD_SBPF_OPCODE_JMP_OP_MODE_JEQ: op_name = "jeq"; break;
137 471039 : case FD_SBPF_OPCODE_JMP_OP_MODE_JGT: op_name = "jgt"; break;
138 467022 : case FD_SBPF_OPCODE_JMP_OP_MODE_JGE: op_name = "jge"; break;
139 470964 : case FD_SBPF_OPCODE_JMP_OP_MODE_JSET: op_name = "jset"; break;
140 466776 : case FD_SBPF_OPCODE_JMP_OP_MODE_JNE: op_name = "jne"; break;
141 467802 : case FD_SBPF_OPCODE_JMP_OP_MODE_JSGT: op_name = "jsgt"; break;
142 469077 : case FD_SBPF_OPCODE_JMP_OP_MODE_JSGE: op_name = "jsge"; break;
143 469821 : case FD_SBPF_OPCODE_JMP_OP_MODE_CALL: op_name = "call"; break;
144 469578 : case FD_SBPF_OPCODE_JMP_OP_MODE_EXIT: op_name = "exit"; break;
145 470478 : case FD_SBPF_OPCODE_JMP_OP_MODE_JLT: op_name = "jlt"; break;
146 467139 : case FD_SBPF_OPCODE_JMP_OP_MODE_JLE: op_name = "jle"; break;
147 467706 : case FD_SBPF_OPCODE_JMP_OP_MODE_JSLT: op_name = "jslt"; break;
148 468159 : case FD_SBPF_OPCODE_JMP_OP_MODE_JSLE: op_name = "jsle"; break;
149 940203 : default: return FD_VM_ERR_INVAL;
150 7502157 : }
151 :
152 6561954 : if( FD_UNLIKELY( instr.opcode.normal.op_mode==FD_SBPF_OPCODE_JMP_OP_MODE_CALL ) ) {
153 469821 : switch ( instr.opcode.normal.op_src ) {
154 234801 : case FD_SBPF_OPCODE_SOURCE_MODE_IMM: {
155 234801 : fd_sbpf_syscalls_t const * syscall = syscalls ? fd_sbpf_syscalls_query_const( syscalls, (ulong)instr.imm, NULL ) : NULL;
156 234801 : if( syscall ) { /* FIXME: THESE CODE PATHS CURRENTLY NOT EXERCISED BY UNIT TEST */
157 0 : char const * name = syscall->name;
158 0 : if( name ) OUT_PRINTF( "syscall%s %s", suffix, name );
159 0 : else OUT_PRINTF( "syscall%s 0x%08x", suffix, instr.imm );
160 234801 : } else {
161 234801 : uint pc = fd_pchash_inverse( instr.imm ); /* FIXME: is pchash in the right place? */
162 234801 : if( pc<(10<<17) ) OUT_PRINTF( "%s%s function_%u", op_name, suffix, pc ); /* FIXME: hardcoded constant */
163 88044 : else OUT_PRINTF( "%s%s function_%#x", op_name, suffix, instr.imm );
164 234801 : }
165 234801 : return FD_VM_SUCCESS;
166 234801 : }
167 235020 : case FD_SBPF_OPCODE_SOURCE_MODE_REG:
168 235020 : OUT_PRINTF( "%sx%s r%u", op_name, suffix, instr.imm );
169 235020 : return FD_VM_SUCCESS;
170 0 : default: break;
171 469821 : }
172 0 : return FD_VM_ERR_INVAL;
173 469821 : }
174 :
175 6092133 : if( FD_UNLIKELY( instr.opcode.normal.op_mode==FD_SBPF_OPCODE_JMP_OP_MODE_EXIT ) ) {
176 469578 : OUT_PRINTF( "%s%s", op_name, suffix );
177 469578 : return FD_VM_SUCCESS;
178 469578 : }
179 :
180 5622555 : if( FD_UNLIKELY( instr.opcode.normal.op_mode==FD_SBPF_OPCODE_JMP_OP_MODE_JA ) ) {
181 469752 : OUT_PRINTF( "%s%s lbb_%ld", op_name, suffix, (long)pc+(long)instr.offset+1L );
182 469752 : return FD_VM_SUCCESS;
183 469752 : }
184 :
185 5152803 : switch( instr.opcode.normal.op_src ) {
186 2577315 : case FD_SBPF_OPCODE_SOURCE_MODE_IMM:
187 2577315 : OUT_PRINTF( "%s%s r%d, %d, lbb_%ld", op_name, suffix, instr.dst_reg, (int)instr.imm, (long)pc+(long)instr.offset+1L );
188 2577315 : return FD_VM_SUCCESS;
189 2575488 : case FD_SBPF_OPCODE_SOURCE_MODE_REG:
190 2575488 : OUT_PRINTF( "%s%s r%d, r%d, lbb_%ld", op_name, suffix, instr.dst_reg, instr.src_reg, (long)pc+(long)instr.offset+1L );
191 2575488 : return FD_VM_SUCCESS;
192 0 : break;
193 0 : default: break;
194 5152803 : }
195 :
196 0 : return FD_VM_ERR_INVAL;
197 5152803 : }
198 :
199 : static int
200 : fd_vm_disasm_instr_ldx( fd_sbpf_instr_t instr,
201 : char * out,
202 : ulong out_max,
203 3749811 : ulong * _out_len ) {
204 :
205 3749811 : char * op_name;
206 3749811 : switch( instr.opcode.mem.op_size ) {
207 937140 : case FD_SBPF_OPCODE_SIZE_MODE_WORD: op_name = "ldxw"; break;
208 935409 : case FD_SBPF_OPCODE_SIZE_MODE_HALF: op_name = "ldxh"; break;
209 938931 : case FD_SBPF_OPCODE_SIZE_MODE_BYTE: op_name = "ldxb"; break;
210 938331 : case FD_SBPF_OPCODE_SIZE_MODE_DOUB: op_name = "ldxdw"; break;
211 0 : default: return FD_VM_ERR_INVAL;
212 3749811 : }
213 :
214 3749811 : if( instr.offset<0 ) OUT_PRINTF( "%s r%d, [r%d-0x%x]", op_name, instr.dst_reg, instr.src_reg, (ushort)-instr.offset );
215 1876230 : else OUT_PRINTF( "%s r%d, [r%d+0x%x]", op_name, instr.dst_reg, instr.src_reg, (ushort) instr.offset );
216 3749811 : return FD_VM_SUCCESS;
217 3749811 : }
218 :
219 : static int
220 : fd_vm_disasm_instr_stx( fd_sbpf_instr_t instr,
221 : char * out,
222 : ulong out_max,
223 3752205 : ulong * _out_len ) {
224 :
225 3752205 : char * op_name;
226 3752205 : switch( instr.opcode.mem.op_size ) {
227 939231 : case FD_SBPF_OPCODE_SIZE_MODE_WORD: op_name = "stxw"; break;
228 937776 : case FD_SBPF_OPCODE_SIZE_MODE_HALF: op_name = "stxh"; break;
229 938133 : case FD_SBPF_OPCODE_SIZE_MODE_BYTE: op_name = "stxb"; break;
230 937065 : case FD_SBPF_OPCODE_SIZE_MODE_DOUB: op_name = "stxdw"; break;
231 0 : default: return FD_VM_ERR_INVAL;
232 3752205 : }
233 :
234 3752205 : if( instr.offset<0 ) OUT_PRINTF( "%s [r%d-0x%x], r%d", op_name, instr.dst_reg, (ushort)-instr.offset, instr.src_reg );
235 1872702 : else OUT_PRINTF( "%s [r%d+0x%x], r%d", op_name, instr.dst_reg, (ushort) instr.offset, instr.src_reg );
236 3752205 : return FD_VM_SUCCESS;
237 3752205 : }
238 :
239 : int
240 : fd_vm_disasm_instr( ulong const * text,
241 : ulong text_cnt,
242 : ulong pc,
243 : fd_sbpf_syscalls_t const * syscalls,
244 : char * out,
245 : ulong out_max,
246 30000057 : ulong * _out_len ) {
247 :
248 30000057 : if( FD_UNLIKELY( (!text) | (!text_cnt) | (!out) | (!out_max) | (!_out_len) ) ) return FD_VM_ERR_INVAL;
249 30000042 : if( FD_UNLIKELY( (*_out_len)>=out_max ) ) return FD_VM_ERR_INVAL;
250 :
251 30000039 : fd_sbpf_instr_t i0 = fd_sbpf_instr( text[0] );
252 :
253 30000039 : switch( i0.opcode.any.op_class ) {
254 :
255 3753159 : case FD_SBPF_OPCODE_CLASS_LD: {
256 3753159 : if( FD_UNLIKELY( text_cnt<2UL ) ) return FD_VM_ERR_INVAL;
257 3738213 : fd_sbpf_instr_t i1 = fd_sbpf_instr( text[1] );
258 : /* FIXME: VALIDATE I1 IS PROPER */
259 3738213 : OUT_PRINTF( "lddw r%d, 0x%lx", i0.dst_reg, (ulong)((ulong)i0.imm | (ulong)((ulong)i1.imm << 32UL)) );
260 3738213 : return FD_VM_SUCCESS;
261 3738213 : }
262 :
263 3750921 : case FD_SBPF_OPCODE_CLASS_ST: { /* FIXME: FIGURE OUT WHAT'S UP HERE */
264 3750921 : OUT_PRINTF( "FIXME: %016lx (ST)", text[0] );
265 3750921 : return FD_VM_SUCCESS;
266 3750921 : }
267 :
268 3749811 : case FD_SBPF_OPCODE_CLASS_LDX: return fd_vm_disasm_instr_ldx( i0, out, out_max, _out_len);
269 3752205 : case FD_SBPF_OPCODE_CLASS_STX: return fd_vm_disasm_instr_stx( i0, out, out_max, _out_len );
270 3747042 : case FD_SBPF_OPCODE_CLASS_ALU: return fd_vm_disasm_instr_alu( i0, "", out, out_max, _out_len );
271 3751272 : case FD_SBPF_OPCODE_CLASS_JMP: return fd_vm_disasm_instr_jmp( i0, pc, "", syscalls, out, out_max, _out_len );
272 3750885 : case FD_SBPF_OPCODE_CLASS_JMP32: return fd_vm_disasm_instr_jmp( i0, pc, "32", syscalls, out, out_max, _out_len );
273 3744744 : case FD_SBPF_OPCODE_CLASS_ALU64: return fd_vm_disasm_instr_alu( i0, "64", out, out_max, _out_len );
274 0 : default: break;
275 30000039 : }
276 0 : return FD_VM_ERR_INVAL;
277 30000039 : }
278 :
279 : int
280 : fd_vm_disasm_program( ulong const * text,
281 : ulong text_cnt,
282 : fd_sbpf_syscalls_t const * syscalls,
283 : char * out,
284 : ulong out_max,
285 15 : ulong * _out_len ) {
286 :
287 15 : if( FD_UNLIKELY( ((!text) & (!!text_cnt)) | (!out) | (!out_max) | (!_out_len) ) ) return FD_VM_ERR_INVAL;
288 3 : if( FD_UNLIKELY( (*_out_len)>=out_max ) ) return FD_VM_ERR_INVAL;
289 :
290 : /* Construct the mapping of pc to labels and functions. FIXME: This
291 : is currently not an algo efficient implementation. Note: if the
292 : same instruction is the targeted by multiple calls / exits / jmps,
293 : it will appear multiple times in the label_pc and/or func_pc
294 : arrays. But that's okay because use the target instruction as the
295 : label and function name. */
296 :
297 0 : ulong func_pc [ 65536 ]; ulong func_cnt = 0UL;
298 0 : ulong label_pc[ 65536 ]; ulong label_cnt = 0UL;
299 :
300 0 : for( ulong i=0UL; i<text_cnt; i++ ) {
301 0 : fd_sbpf_instr_t instr = fd_sbpf_instr( text[i] );
302 0 : if ( instr.opcode.raw==FD_SBPF_OP_CALL_IMM ) func_cnt++;
303 0 : else if( instr.opcode.raw==FD_SBPF_OP_EXIT ) func_cnt++;
304 0 : else if( instr.opcode.raw==FD_SBPF_OP_CALL_REG ) continue;
305 0 : else if( ( (instr.opcode.any.op_class==FD_SBPF_OPCODE_CLASS_JMP ) |
306 0 : (instr.opcode.any.op_class==FD_SBPF_OPCODE_CLASS_JMP32) ) ) label_cnt++;
307 0 : }
308 :
309 0 : if( FD_UNLIKELY( (func_cnt>65536UL) | (label_cnt>65536UL) ) ) return FD_VM_ERR_UNSUP;
310 :
311 0 : func_cnt = 0UL;
312 0 : label_cnt = 0UL;
313 :
314 0 : for( ulong i=0UL; i<text_cnt; i++ ) {
315 0 : fd_sbpf_instr_t instr = fd_sbpf_instr( text[i] );
316 0 : if ( instr.opcode.raw==FD_SBPF_OP_CALL_IMM ) func_pc[ func_cnt++ ] = i + instr.imm + 1UL; /* FIXME: what if out of bounds? */
317 0 : else if( instr.opcode.raw==FD_SBPF_OP_EXIT ) func_pc[ func_cnt++ ] = i + instr.imm + 1UL; /* FIXME: what if out of bounds? */
318 0 : else if( instr.opcode.raw==FD_SBPF_OP_CALL_REG ) continue;
319 0 : else if( ( (instr.opcode.any.op_class==FD_SBPF_OPCODE_CLASS_JMP ) |
320 0 : (instr.opcode.any.op_class==FD_SBPF_OPCODE_CLASS_JMP32) ) )
321 0 : label_pc[ label_cnt++ ] = (ulong)((long)i + (long)instr.offset + 1L); /* FIXME: casting and what if out of bounds? */
322 0 : }
323 :
324 : /* Output the program */
325 :
326 0 : OUT_PRINTF( "function_0:\n" );
327 :
328 0 : for( ulong i=0UL; i<text_cnt; i++ ) {
329 :
330 : /* Print functions / labels */
331 : /* FIXME: What if there is a func_pc and a label_pc that target
332 : for the same instruction? It is possible given the above logic.
333 : Probably should print both. */
334 : /* FIXME: Algo efficiency! */
335 :
336 0 : int found = 0;
337 0 : for( ulong j=0UL; j<label_cnt; j++ ) if( label_pc[j]==i ) { found = 1; OUT_PRINTF( "lbb_%lu:\n", i ); break; }
338 0 : if( !found ) for( ulong j=0UL; j<func_cnt; j++ ) if( func_pc[j]==i ) { OUT_PRINTF( "\nfunction_%lu:\n", i ); break; }
339 :
340 : /* Print instruction */
341 :
342 : /* FIXME: WHAT ABOUT LABELS IN THE MIDDLE OF MULTIWORD INSTRUCTIONS!
343 : AND NOT JUST FOR DISASSEMBLY ... POTENTIAL CONSENSUS FAILURE
344 : MECHANISM! */
345 :
346 0 : fd_sbpf_instr_t instr = fd_sbpf_instr( text[i] );
347 0 : ulong extra_cnt = fd_ulong_if( instr.opcode.any.op_class==FD_SBPF_OPCODE_CLASS_LD, 1UL, 0UL );
348 0 : if( FD_UNLIKELY( (i+extra_cnt)>=text_cnt ) ) return FD_VM_ERR_INVAL; /* Truncated multiword instruction at end of text */
349 :
350 0 : OUT_PRINTF( " " );
351 0 : int err = fd_vm_disasm_instr( text+i, text_cnt-i, i, syscalls, out, out_max, _out_len );
352 0 : if( FD_UNLIKELY( err ) ) return err;
353 0 : OUT_PRINTF( "\n" );
354 :
355 0 : i += extra_cnt;
356 :
357 : /* Print any trailing function */
358 : /* FIXME: this is probably not necessary if the function/label print
359 : above just prints both, unless trying print a function label that
360 : happens to immediately after the end of a program. */
361 : /* FIXME: Algo efficiency? */
362 :
363 0 : if( FD_UNLIKELY( (instr.opcode.raw==FD_SBPF_OP_JA) & ((i+1UL)<text_cnt) ) ) {
364 0 : found = 0;
365 0 : for( ulong j=0UL; j<label_cnt; j++ ) if( label_pc[j]==(i+1UL) ) { found = 1; break; }
366 0 : if( !found ) OUT_PRINTF( "\nfunction_%lu:\n", i+1UL );
367 0 : }
368 0 : }
369 :
370 0 : return FD_VM_SUCCESS;
371 0 : }
372 :
373 : #undef OUT_PRINTF
|