Debian Codegraph @

check/public-tests/instructions_base_t.c

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2020 Slawomir Wojtasiak
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "instructions_base_t.h"
21
22 #include <string.h>
23 #include <stdio.h>
24
25 #include <fcml_assembler.h>
26 #include <fcml_disassembler.h>
27 #include <fcml_intel_dialect.h>
28 #include <fcml_renderer.h>
29 #include <fcml_parser.h>
30 #include <fcml_types.h>
31
32 fcml_st_assembler *assembler_intel;
33 fcml_st_disassembler *disassembler_intel;
34 fcml_st_dialect *dialect_intel;
35
36 fcml_st_assembler *assembler_gas;
37 fcml_st_disassembler *disassembler_gas;
38 fcml_st_dialect *dialect_gas;
39
40 #ifdef FCML_MSCC
41
42 int fcml_fn_test_number_of_arguments( fcml_string code ) {
43 int count = 1;
44 for( int i = 0; code[i] != '\0'; i++ ) {
45 if( code[i] == ',' ) {
46 count++;
47 }
48 }
49 return count;
50 }
51
52 #endif
53
54 void fcml_ifn_ts_set_ip(fcml_ip *ip, fcml_en_operating_mode op_mode) {
55 switch (op_mode) {
56 case FCML_OM_16_BIT:
57 case FCML_OM_32_BIT:
58 *ip = 0x00401000;
59 break;
60 case FCML_OM_64_BIT:
61 *ip = 0x0000800000401000;
62 break;
63 }
64 }
65
66 fcml_bool fcml_fn_ts_instruction_test(fcml_uint8_t *code, fcml_int size,
67 fcml_en_operating_mode op_mode, fcml_string mnemonic,
68 fcml_uint32_t t_flags, fcml_uint32_t rend_flags) {
69
70 fcml_st_assembler *assembler = assembler_intel;
71 fcml_st_disassembler *disassembler = disassembler_intel;
72 fcml_st_dialect *dialect = dialect_intel;
73
74 fcml_bool is_gas = FCML_TSF_GAS_DIALECT & t_flags;
75 if (is_gas) {
76 assembler = assembler_gas;
77 disassembler = disassembler_gas;
78 dialect = dialect_gas;
79 }
80
81 fcml_bool success = FCML_TRUE;
82
83 fcml_st_disassembler_result dis_result;
84
85 fcml_fn_disassembler_result_prepare(&dis_result);
86
87 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
88
89 if (!(t_flags & FCML_TSF_ASM_ONLY)) {
90
91 fcml_st_disassembler_context context = { 0 };
92 context.configuration.enable_error_messages = FCML_TRUE;
93 context.configuration.short_forms = FCML_FALSE;
94 context.configuration.extend_disp_to_asa = FCML_TRUE;
95 context.configuration.conditional_group = FCML_DASM_CONDITIONAL_GROUP_1;
96 context.configuration.carry_flag_conditional_suffix = FCML_TRUE;
97 context.configuration.extend_disp_to_asa = FCML_TRUE;
98 context.configuration.increment_ip = FCML_FALSE;
99 context.configuration.fail_if_unknown_instruction = FCML_TRUE;
100 context.disassembler = disassembler;
101 context.entry_point.op_mode = op_mode;
102 context.entry_point.address_size_attribute = 0;
103 context.entry_point.operand_size_attribute = 0;
104 if (!(t_flags & FCML_TSF_MULTI_ASM_RESULTS)) {
105 context.code = code;
106 context.code_length = size;
107 } else {
108 context.code = code + 2;
109 context.code_length = code[1];
110 }
111
112 fcml_ifn_ts_set_ip(&(context.entry_point.ip), op_mode);
113
114 /* Disassemble.*/
115 error = fcml_fn_disassemble(&context, &dis_result);
116
117 }
118
119 if (error == FCML_CEH_GEC_NO_ERROR) {
120
121 fcml_bool is_67 = FCML_FALSE;
122 fcml_bool is_66 = FCML_FALSE;
123
124 if (!(t_flags & FCML_TSF_ASM_ONLY)) {
125
126 if ((t_flags & FCML_TSF_PRINT_ONLY)) {
127 printf("Instruction: %s\n", mnemonic);
128 }
129
130 /* Looking for 0x67 prefix.*/
131 int i;
132 for (i = 0; i < FCML_DASM_PREFIXES_COUNT; i++) {
133 if (dis_result.instruction_details.prefixes_details.prefixes[i].prefix
134 == 0x67) {
135 is_67 = FCML_TRUE;
136 }
137 if (dis_result.instruction_details.prefixes_details.prefixes[i].prefix
138 == 0x66) {
139 is_66 = FCML_TRUE;
140 }
141 }
142
143 /* Print.*/
144 fcml_char buffer[FCML_REND_MAX_BUFF_LEN] = { 0 };
145
146 fcml_st_render_config config = { 0 };
147 config.render_flags = FCML_REND_FLAG_HEX_IMM
148 | FCML_REND_FLAG_COND_SHOW_CARRY
149 | FCML_REND_FLAG_HEX_DISPLACEMENT | rend_flags;
150
151 error = fcml_fn_render(dialect, &config, buffer, sizeof(buffer),
152 &dis_result);
153 if (error) {
154 printf("Disassembling failed, with error code: %d\n", error);
155 if (!(t_flags & FCML_TSF_PRINT_ONLY)) {
156 success = FCML_FALSE;
157 }
158 fcml_fn_disassembler_result_free(&dis_result);
159 return success;
160 }
161
162 if (strcmp(buffer, mnemonic) != 0) {
163 if (t_flags & FCML_TSF_SHOULD_FAIL) {
164 fcml_fn_disassembler_result_free(&dis_result);
165 return FCML_TRUE;
166 }
167 printf("Disassembling failed, should be: %s (Was: %s)\n",
168 mnemonic, buffer);
169 if (!(t_flags & FCML_TSF_PRINT_ONLY)) {
170 success = FCML_FALSE;
171 }
172 fcml_fn_disassembler_result_free(&dis_result);
173 return success;
174 }
175
176 if (t_flags & FCML_TSF_SHOULD_FAIL) {
177 printf("Should fail: %s\n", mnemonic);
178 success = FCML_FALSE;
179 fcml_fn_disassembler_result_free(&dis_result);
180 return success;
181 }
182
183 }
184
185 /* Parsing.*/
186
187 fcml_st_parser_result result;
188 fcml_fn_parser_result_prepare(&result);
189 fcml_st_parser_context parser_context = { 0 };
190 parser_context.dialect = dialect;
191
192 fcml_ceh_error error = fcml_fn_parse(&parser_context, mnemonic,
193 &result);
194 if (error) {
195 printf("Can not parse: %s\n", mnemonic);
196 if (!(t_flags & FCML_TSF_PRINT_ONLY)) {
197 success = FCML_FALSE;
198 }
199 fcml_fn_parser_result_free(&result);
200 fcml_fn_disassembler_result_free(&dis_result);
201 return success;
202 }
203
204 fcml_fn_symbol_table_free(parser_context.symbol_table);
205
206 fcml_uint16_t opt_flags = 0;
207
208 /* Assembling.*/
209 fcml_st_assembler_context context = { 0 };
210 context.assembler = assembler;
211 context.entry_point.address_size_attribute = 0;
212 context.entry_point.operand_size_attribute = 0;
213 context.entry_point.op_mode = op_mode;
214
215 if (is_67) {
216 if (op_mode == FCML_OM_64_BIT) {
217 opt_flags = FCML_OPTF_ASA_32;
218 } else {
219 opt_flags = FCML_OPTF_ASA_16;
220 }
221 }
222 if (is_66) {
223 opt_flags |= FCML_OPTF_OSA_16;
224 }
225
226 context.configuration.enable_error_messages = FCML_TRUE;
227 context.configuration.choose_sib_encoding = FCML_FALSE;
228 context.configuration.choose_abs_encoding = !(t_flags
229 & FCML_TSF_ENABLE_RIP);
230 context.configuration.chooser = NULL;
231 context.configuration.optimizer = NULL;
232 context.configuration.optimizer_flags = opt_flags;
233 context.configuration.force_rex_prefix = FCML_FALSE;
234 context.configuration.force_three_byte_VEX = FCML_FALSE;
235 context.configuration.increment_ip = FCML_FALSE;
236
237 fcml_ifn_ts_set_ip(&(context.entry_point.ip), op_mode);
238
239 fcml_st_assembler_result asm_result;
240
241 fcml_fn_assembler_result_prepare(&asm_result);
242
243 error = fcml_fn_assemble(&context, result.instruction, &asm_result);
244 if (error) {
245 if (!t_flags & FCML_TSF_SHOULD_FAIL) {
246 printf("Can not assemble: %s (error: %d)\n", mnemonic, error);
247 }
248 if (!(t_flags & FCML_TSF_PRINT_ONLY)) {
249 success = t_flags & FCML_TSF_SHOULD_FAIL;
250 }
251 } else if (t_flags & FCML_TSF_SHOULD_FAIL) {
252 printf("should fail: %s\n", mnemonic);
253 if (!(t_flags & FCML_TSF_PRINT_ONLY)) {
254 success = FCML_FALSE;
255 }
256 } else {
257
258 fcml_bool looking_for_instruction = FCML_TRUE;
259 fcml_bool found = FCML_FALSE;
260
261 fcml_uint8_t *assembled_code[100];
262 int assembled_code_len[100];
263 int assembled_code_index = 0;
264
265 for (int j = 0; j < 2; j++) {
266
267 fcml_st_assembled_instruction *instructions =
268 asm_result.instructions;
269
270 assembled_code_index = 0;
271
272 /* Fill code array.*/
273 fcml_st_assembled_instruction *assembled_instruction =
274 instructions;
275 while (assembled_instruction) {
276 assembled_code_len[assembled_code_index] =
277 assembled_instruction->code_length;
278 assembled_code[assembled_code_index++] =
279 assembled_instruction->code;
280 assembled_instruction = assembled_instruction->next;
281 }
282
283 if (!(t_flags & FCML_TSF_PRINT_ONLY)) {
284 if (!(t_flags & FCML_TSF_MULTI_ASM_RESULTS)
285 && asm_result.number_of_instructions != 1) {
286 found = FCML_FALSE;
287 break;
288 }
289 /* Check if number of assembled instructions match.*/
290 if ((t_flags & FCML_TSF_MULTI_ASM_RESULTS)
291 && asm_result.number_of_instructions != code[0]) {
292 found = FCML_FALSE;
293 break;
294 }
295 }
296
297 while (instructions) {
298 fcml_st_assembled_instruction *assembled_instruction =
299 (fcml_st_assembled_instruction*) instructions;
300 fcml_bool differ = FCML_FALSE;
301 if (!(t_flags & FCML_TSF_PRINT_ONLY)) {
302 int i;
303 if ((t_flags & FCML_TSF_MULTI_ASM_RESULTS)) {
304 int code_index = 0;
305 fcml_uint8_t instruction_count = code[code_index++];
306 for (i = 0; i < instruction_count; i++) {
307 fcml_uint8_t code_size = code[code_index++];
308 fcml_uint8_t *code_buff = &(code[code_index]);
309 code_index += code_size;
310 fcml_uint8_t k;
311 differ = FCML_FALSE;
312 if (code_size
313 != assembled_instruction->code_length) {
314 differ = FCML_TRUE;
315 } else {
316 for (k = 0; k < code_size; k++) {
317 if (code_buff[k]
318 != assembled_instruction->code[k]) {
319 differ = FCML_TRUE;
320 break;
321 }
322 }
323 }
324 if (!differ) {
325 break;
326 }
327 }
328 } else {
329 if (size == assembled_instruction->code_length) {
330 for (i = 0; i < size; i++) {
331 if (code[i]
332 != assembled_instruction->code[i]) {
333 differ = FCML_TRUE;
334 break;
335 }
336 }
337 } else {
338 differ = FCML_TRUE;
339 }
340 }
341 if (differ) {
342 found = FCML_FALSE;
343 } else {
344 found = FCML_TRUE;
345 }
346 }
347
348 if ((t_flags & FCML_TSF_PRINT_ONLY)
349 || (differ && !looking_for_instruction)) {
350 #ifdef FCML_DEBUG
351 printf("Index: %d\nOriginal code : ",
352 assembled_instruction->__def_index);
353 #else
354 printf( "Original code : " );
355 #endif
356 fcml_int i;
357 for (i = 0; i < size; i++) {
358 printf( FCML_PRI_INT8_HEX, code[i]);
359 }
360
361 printf("\nAssembled code: ");
362 for (i = 0;
363 i
364 < (fcml_int) assembled_instruction->code_length;
365 i++) {
366 printf( FCML_PRI_INT8_HEX,
367 assembled_instruction->code[i]);
368 }
369
370 printf("\n");
371 }
372 instructions = instructions->next;
373 }
374
375 if (found) {
376 break;
377 }
378
379 looking_for_instruction = FCML_FALSE;
380
381 if ((t_flags & FCML_TSF_PRINT_ONLY)) {
382 break;
383 }
384 }
385
386 if (!found) {
387 if (!(t_flags & FCML_TSF_PRINT_ONLY)
388 && !(t_flags & FCML_TSF_SHOULD_FAIL)) {
389 printf("Can not assemble: %s\n", mnemonic);
390 }
391 if (!(t_flags & FCML_TSF_PRINT_ONLY)) {
392 success = t_flags & FCML_TSF_SHOULD_FAIL;
393 }
394 }
395
396 /* Prints test code.*/
397 if (!(t_flags & FCML_TSF_SHOULD_FAIL) && assembled_code_index >= 1
398 && ((t_flags & FCML_TSF_PRINT_ONLY) || !found)) {
399
400 /* Mnemonic.*/
401 fcml_string macro;
402 switch (op_mode) {
403 case FCML_OM_16_BIT:
404 macro = (assembled_code_index > 1) ?
405 (is_gas ? "FCML_A16_M" : "FCML_I16_M") :
406 (is_gas ? "FCML_A16" : "FCML_I16");
407 break;
408 case FCML_OM_32_BIT:
409 macro = (assembled_code_index > 1) ?
410 (is_gas ? "FCML_A32_M" : "FCML_I32_M") :
411 (is_gas ? "FCML_A32" : "FCML_I32");
412 break;
413 case FCML_OM_64_BIT:
414 macro = (assembled_code_index > 1) ?
415 (is_gas ? "FCML_A64_M" : "FCML_I64_M") :
416 (is_gas ? "FCML_A64" : "FCML_I64");
417 break;
418 }
419
420 printf("%s(\"%s\", ", macro, mnemonic);
421
422 /* Code.*/
423
424 if (assembled_code_index > 1) {
425 /* multi.*/
426 printf("%d, ", assembled_code_index);
427 int i, j;
428 for (i = 0; i < assembled_code_index; i++) {
429 if (i > 0) {
430 printf(", ");
431 }
432 printf("FCML_MI(");
433 for (j = 0; j < assembled_code_len[i]; j++) {
434 if (j > 0) {
435 printf(", ");
436 }
437 printf("0x" FCML_PRI_INT8_HEX,
438 assembled_code[i][j]);
439 }
440 printf(")");
441 }
442 } else if (assembled_code_index == 1) {
443 int i;
444 for (i = 0; i < assembled_code_len[0]; i++) {
445 if (i > 0) {
446 printf(", ");
447 }
448 printf("0x" FCML_PRI_INT8_HEX, assembled_code[0][i]);
449 }
450 }
451
452 printf(");\n");
453 }
454 }
455
456 fcml_fn_assembler_result_free(&asm_result);
457
458 fcml_fn_parser_result_free(&result);
459
460 } else {
461 success = t_flags & FCML_TSF_SHOULD_FAIL;
462 }
463
464 fcml_fn_disassembler_result_free(&dis_result);
465
466 return success;
467 }
468
469 fcml_bool fcml_fn_ts_instruction_test_diss(fcml_uint8_t *code, fcml_int size,
470 fcml_en_operating_mode op_mode, fcml_string mnemonic,
471 fcml_uint32_t t_flags, fcml_uint32_t ren_flags) {
472
473 fcml_bool success = FCML_TRUE;
474
475 fcml_st_disassembler *disassembler = disassembler_intel;
476 fcml_st_dialect *dialect = dialect_intel;
477
478 if ( FCML_TSF_GAS_DIALECT & t_flags) {
479 disassembler = disassembler_gas;
480 dialect = dialect_gas;
481 }
482
483 fcml_st_disassembler_result dis_result;
484
485 fcml_fn_disassembler_result_prepare(&dis_result);
486
487 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
488
489 fcml_st_disassembler_context context = { 0 };
490 context.configuration.enable_error_messages = FCML_TRUE;
491 context.configuration.short_forms =
492 (t_flags & FCML_TSF_SHORT) ? FCML_TRUE : FCML_FALSE;
493 context.configuration.extend_disp_to_asa = FCML_TRUE;
494 context.configuration.conditional_group = FCML_DASM_CONDITIONAL_GROUP_1;
495 context.configuration.carry_flag_conditional_suffix = FCML_TRUE;
496 context.disassembler = disassembler;
497 context.entry_point.op_mode = op_mode;
498 context.entry_point.address_size_attribute = 0;
499 context.entry_point.operand_size_attribute = 0;
500 context.code = code;
501 context.code_length = size;
502
503 fcml_ifn_ts_set_ip(&(context.entry_point.ip), op_mode);
504
505 /* Disassemble.*/
506 error = fcml_fn_disassemble(&context, &dis_result);
507
508 if (!error) {
509
510 /* Print.*/
511 char buffer[FCML_REND_MAX_BUFF_LEN] = { 0 };
512
513 fcml_st_render_config config = { 0 };
514 config.render_flags = FCML_REND_FLAG_HEX_IMM
515 | FCML_REND_FLAG_COND_SHOW_CARRY
516 | FCML_REND_FLAG_HEX_DISPLACEMENT | ren_flags;
517
518 error = fcml_fn_render(dialect, &config, buffer, sizeof(buffer),
519 &dis_result);
520
521 if (t_flags & FCML_TSF_PRINT_ONLY) {
522 printf("Disassembled instruction: %s Renderer error code: %d\n",
523 buffer, error);
524 } else {
525 if (error || strcmp(buffer, mnemonic) != 0) {
526 if (!(t_flags & FCML_TSF_SHOULD_FAIL)) {
527 printf("Disassembling failed, should be: %s (Was: %s)\n",
528 mnemonic, buffer);
529 }
530 success =
531 (t_flags & FCML_TSF_SHOULD_FAIL) ?
532 FCML_TRUE : FCML_FALSE;
533 } else {
534 if (dis_result.instruction_details.instruction_size != size) {
535 printf(
536 "Instruction size: %d Disassembled code size: %d (%s)\n",
537 (fcml_uint32_t) dis_result.instruction_details.instruction_size,
538 size, mnemonic);
539 success = FCML_FALSE;
540 } else {
541 if (!(t_flags & FCML_TSF_PRINT_ONLY)) {
542 if ((t_flags & FCML_TSF_SHOULD_FAIL)) {
543 printf("Should fail: %s\n", mnemonic);
544 success = FCML_FALSE;
545 } else {
546 STF_ASSERT(FCML_TRUE);
547 }
548 }
549 }
550 }
551 }
552 } else {
553
554 if (t_flags & FCML_TSF_PRINT_ONLY) {
555 printf("Error code: %d\n", error);
556 }
557
558 if (t_flags & FCML_TSF_SHOULD_FAIL) {
559 if (!(t_flags & FCML_TSF_PRINT_ONLY)) {
560 STF_ASSERT(FCML_TRUE);
561 }
562 success = FCML_TRUE;
563 } else {
564 success = FCML_FALSE;
565 }
566 }
567
568 fcml_fn_disassembler_result_free(&dis_result);
569
570 return success;
571 }
572