Debian Codegraph @

check/public-tests/disassembler_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 "disassembler_t.h"
21
22 #include <fcml_disassembler.h>
23 #include <fcml_intel_dialect.h>
24
25 /* Test suite initialization. */
26
27 fcml_bool fcml_tf_disassembler_suite_init(void) {
28 return FCML_TRUE;
29 }
30
31 fcml_bool fcml_tf_disassembler_suite_cleanup(void) {
32 return FCML_TRUE;
33 }
34
35 /* Tests. */
36
37 struct fcml_st_td_code_sample {
38 fcml_uint8_t *code;
39 fcml_size len;
40 };
41
42 fcml_uint8_t td_sample_instruction_1[] = {0x48, 0x66, 0xFF, 0x57, 0x01};
43 fcml_uint8_t td_sample_instruction_2[] = {0x0F};
44
45 struct fcml_st_td_code_sample fcml_td_code_samples[] = {
46 {td_sample_instruction_1, sizeof(td_sample_instruction_1)},
47 {td_sample_instruction_2, sizeof(td_sample_instruction_2)},
48 {NULL, 0}
49 };
50
51 void fcml_tf_disassembler_no_instruction_found(void) {
52
53 struct fcml_st_td_code_sample *code = &fcml_td_code_samples[0];
54
55 while(code->code) {
56
57 fcml_ceh_error error;
58
59 /* Initializes the Intel dialect instance. */
60 fcml_st_dialect *dialect;
61 if( ( error = fcml_fn_dialect_init_intel( FCML_INTEL_DIALECT_CF_DEFAULT, &dialect ) ) ) {
62 STF_FAIL("Can not initialize Dialect.");
63 return;
64 }
65
66 /* Initializes disassembler for the Intel dialect. */
67 fcml_st_disassembler *disassembler;
68 if( ( error = fcml_fn_disassembler_init( dialect, &disassembler ) ) ) {
69 STF_FAIL("Can not initialize Intel.");
70 fcml_fn_dialect_free( dialect );
71 return;
72 }
73
74 /* Prepares disassembler result. */
75 fcml_st_disassembler_result dis_result;
76 fcml_fn_disassembler_result_prepare( &dis_result );
77
78 /* Disassembles the code. */
79 fcml_st_disassembler_context context = {0};
80 context.disassembler = disassembler;
81 context.configuration.fail_if_unknown_instruction = FCML_FALSE;
82 context.entry_point.ip = 0x401000;
83 context.entry_point.op_mode = FCML_OM_64_BIT;
84 context.code = code->code;
85 context.code_length = code->len;
86
87 if( ( error = fcml_fn_disassemble( &context, &dis_result ) ) ) {
88 STF_FAIL("Disassembling failed.");
89 fcml_fn_disassembler_free( disassembler );
90 fcml_fn_dialect_free( dialect );
91 return;
92 }
93
94 STF_ASSERT_EQUAL( 1, dis_result.instruction.operands_count );
95 STF_ASSERT_EQUAL( FCML_OT_IMMEDIATE, dis_result.instruction.operands[0].type );
96 STF_ASSERT_EQUAL( FCML_DS_8, dis_result.instruction.operands[0].immediate.size );
97 STF_ASSERT_EQUAL( code->code[0], dis_result.instruction.operands[0].immediate.int8 );
98 STF_ASSERT_EQUAL( FCML_FALSE, dis_result.instruction.operands[0].immediate.is_signed );
99 STF_ASSERT_EQUAL( FCML_OT_IMMEDIATE, dis_result.instruction.operands[0].type );
100 STF_ASSERT_EQUAL( FP_DB, dis_result.instruction_details.pseudo_op );
101 STF_ASSERT_EQUAL( FCML_FALSE, dis_result.instruction.is_conditional );
102 STF_ASSERT_EQUAL( 0, dis_result.instruction.hints );
103 STF_ASSERT_EQUAL( 0, dis_result.instruction.prefixes );
104 STF_ASSERT_EQUAL( 0, dis_result.instruction_details.addr_mode );
105 STF_ASSERT_EQUAL( 0, dis_result.instruction_details.instruction );
106 STF_ASSERT_EQUAL( 1, dis_result.instruction_details.instruction_size );
107 STF_ASSERT_EQUAL( FCML_FALSE, dis_result.instruction_details.is_shortcut );
108 STF_ASSERT_EQUAL( FCML_FALSE, dis_result.instruction_details.modrm_details.is_modrm );
109 STF_ASSERT_EQUAL( FCML_FALSE, dis_result.instruction_details.modrm_details.is_rip );
110 STF_ASSERT_EQUAL( 0x00, dis_result.instruction_details.modrm_details.modrm );
111 STF_ASSERT_EQUAL( FCML_FALSE, dis_result.instruction_details.modrm_details.sib.is_not_null );
112 STF_ASSERT_EQUAL( 0x00, dis_result.instruction_details.opcode_field_s_bit );
113 STF_ASSERT_EQUAL( 0x00, dis_result.instruction_details.opcode_field_w_bit );
114 STF_ASSERT_EQUAL( 0, dis_result.instruction_details.prefixes_details.prefixes_count );
115
116 STF_ASSERT_STRING_EQUAL( "db", dis_result.instruction.mnemonic );
117 STF_ASSERT_EQUAL( F_UNKNOWN, dis_result.instruction_details.instruction );
118
119 /* Free everything. */
120 fcml_fn_disassembler_result_free( &dis_result );
121 fcml_fn_disassembler_free( disassembler );
122 fcml_fn_dialect_free( dialect );
123
124 code++;
125 }
126 }
127
128 fcml_stf_test_case fcml_ti_disassembler[] = {
129 { "fcml_tf_disassembler_no_instruction_found", fcml_tf_disassembler_no_instruction_found },
130 FCML_STF_NULL_TEST
131 };
132
133 fcml_stf_test_suite fcml_si_disassembler = {
134 "suite-fcml-disassembler", fcml_tf_disassembler_suite_init, fcml_tf_disassembler_suite_cleanup, fcml_ti_disassembler
135 };