Debian Codegraph @

check/public-tests/assembler_t.c

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2021 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 "assembler_t.h"
21
22 #include <fcml_assembler.h>
23 #include <fcml_intel_dialect.h>
24 #include <fcml_gas_dialect.h>
25 #include <fcml_common_utils.h>
26
27 /* Test suite initialization. */
28
29 static fcml_bool suite_init(void) {
30 return FCML_TRUE;
31 }
32
33 static fcml_bool suite_cleanup(void) {
34 return FCML_TRUE;
35 }
36
37 /* Tests. */
38
39 void fcml_tf_assembler_db_imm8(void) {
40
41 fcml_ceh_error error;
42
43 /* Initializes the Intel dialect instance. */
44 fcml_st_dialect *dialect;
45 if ((error = fcml_fn_dialect_init_intel( FCML_INTEL_DIALECT_CF_DEFAULT,
46 &dialect))) {
47 STF_FAIL("Can not initialize Intel dialect.");
48 return;
49 }
50
51 fcml_st_assembler *assembler;
52 if ((error = fcml_fn_assembler_init(dialect, &assembler))) {
53 STF_FAIL("Can not initialize Intel assembler.");
54 fcml_fn_dialect_free(dialect);
55 return;
56 }
57
58 fcml_st_instruction instruction = { 0 };
59 instruction.mnemonic = "db";
60 instruction.operands[0] = FCML_IMM8(0x23);
61 instruction.operands_count = 1;
62
63 /* Prepares the result. */
64 fcml_st_assembler_result asm_result;
65 fcml_fn_assembler_result_prepare(&asm_result);
66
67 fcml_st_assembler_context context = { 0 };
68 context.assembler = assembler;
69 context.entry_point.ip = 0x401000;
70 context.entry_point.op_mode = FCML_OM_32_BIT;
71
72 /* Assembles the given instruction. */
73 if ((error = fcml_fn_assemble(&context, &instruction, &asm_result))) {
74 STF_FAIL("Can not assemble instruction.");
75 fcml_fn_assembler_free(assembler);
76 fcml_fn_dialect_free(dialect);
77 return;
78 }
79
80 /* Prints the instruction code. */
81 if (asm_result.chosen_instruction) {
82 fcml_st_assembled_instruction *ins_code = asm_result.chosen_instruction;
83 STF_ASSERT_EQUAL(1, asm_result.number_of_instructions);
84 STF_ASSERT_EQUAL(1, ins_code->code_length);
85 STF_ASSERT_EQUAL(0x23, ins_code->code[0]);
86 STF_ASSERT_PTR_NULL(ins_code->next);
87 STF_ASSERT_PTR_NULL(ins_code->warnings.errors);
88 STF_ASSERT_PTR_NULL(ins_code->warnings.last_error);
89 } else {
90 STF_FAIL("Can not assemble instruction.");
91 }
92
93 fcml_fn_assembler_result_free(&asm_result);
94 fcml_fn_assembler_free(assembler);
95 fcml_fn_dialect_free(dialect);
96
97 }
98
99 void fcml_tf_assembler_byte_imm8(void) {
100
101 fcml_ceh_error error;
102
103 /* Initializes the Intel dialect instance. */
104 fcml_st_dialect *dialect;
105 if ((error = fcml_fn_dialect_init_gas( FCML_GAS_DIALECT_CF_DEFAULT,
106 &dialect))) {
107 STF_FAIL("Can not initialize Intel dialect.");
108 return;
109 }
110
111 fcml_st_assembler *assembler;
112 if ((error = fcml_fn_assembler_init(dialect, &assembler))) {
113 STF_FAIL("Can not initialize Intel assembler.");
114 fcml_fn_dialect_free(dialect);
115 return;
116 }
117
118 fcml_st_instruction instruction = { 0 };
119 instruction.mnemonic = ".byte";
120 instruction.operands[0] = FCML_IMM8(0x23);
121 instruction.operands_count = 1;
122
123 /* Prepares the result. */
124 fcml_st_assembler_result asm_result;
125 fcml_fn_assembler_result_prepare(&asm_result);
126
127 fcml_st_assembler_context context = { 0 };
128 context.assembler = assembler;
129 context.entry_point.ip = 0x401000;
130 context.entry_point.op_mode = FCML_OM_32_BIT;
131
132 /* Assembles the given instruction. */
133 if ((error = fcml_fn_assemble(&context, &instruction, &asm_result))) {
134 STF_FAIL("Can not assemble instruction.");
135 fcml_fn_assembler_free(assembler);
136 fcml_fn_dialect_free(dialect);
137 return;
138 }
139
140 /* Prints the instruction code. */
141 if (asm_result.chosen_instruction) {
142 fcml_st_assembled_instruction *ins_code = asm_result.chosen_instruction;
143 STF_ASSERT_EQUAL(1, asm_result.number_of_instructions);
144 STF_ASSERT_EQUAL(1, ins_code->code_length);
145 STF_ASSERT_EQUAL(0x23, ins_code->code[0]);
146 STF_ASSERT_PTR_NULL(ins_code->next);
147 STF_ASSERT_PTR_NULL(ins_code->warnings.errors);
148 STF_ASSERT_PTR_NULL(ins_code->warnings.last_error);
149 } else {
150 STF_FAIL("Can not assemble instruction.");
151 }
152
153 fcml_fn_assembler_result_free(&asm_result);
154 fcml_fn_assembler_free(assembler);
155 fcml_fn_dialect_free(dialect);
156
157 }
158
159 void fcml_tf_assembler_db_imm32(void) {
160
161 fcml_ceh_error error;
162
163 /* Initializes the Intel dialect instance. */
164 fcml_st_dialect *dialect;
165 if ((error = fcml_fn_dialect_init_intel( FCML_INTEL_DIALECT_CF_DEFAULT,
166 &dialect))) {
167 STF_FAIL("Can not initialize Intel dialect.");
168 return;
169 }
170
171 fcml_st_assembler *assembler;
172 if ((error = fcml_fn_assembler_init(dialect, &assembler))) {
173 STF_FAIL("Can not initialize Intel assembler.");
174 fcml_fn_dialect_free(dialect);
175 return;
176 }
177
178 fcml_st_instruction instruction = { 0 };
179 instruction.mnemonic = "db";
180 instruction.operands[0] = FCML_IMM32(0x23);
181 instruction.operands_count = 1;
182
183 /* Prepares the result. */
184 fcml_st_assembler_result asm_result;
185 fcml_fn_assembler_result_prepare(&asm_result);
186
187 fcml_st_assembler_context context = { 0 };
188 context.assembler = assembler;
189 context.entry_point.ip = 0x401000;
190 context.entry_point.op_mode = FCML_OM_32_BIT;
191
192 /* Assembles the given instruction. */
193 if ((error = fcml_fn_assemble(&context, &instruction, &asm_result))) {
194 STF_FAIL("Can not assemble instruction.");
195 fcml_fn_assembler_free(assembler);
196 fcml_fn_dialect_free(dialect);
197 return;
198 }
199
200 /* Prints the instruction code. */
201 if (asm_result.chosen_instruction) {
202 fcml_st_assembled_instruction *ins_code = asm_result.chosen_instruction;
203 STF_ASSERT_EQUAL(1, asm_result.number_of_instructions);
204 STF_ASSERT_EQUAL(1, ins_code->code_length);
205 STF_ASSERT_EQUAL(0x23, ins_code->code[0]);
206 STF_ASSERT_PTR_NULL(ins_code->next);
207 STF_ASSERT_PTR_NULL(ins_code->warnings.errors);
208 STF_ASSERT_PTR_NULL(ins_code->warnings.last_error);
209 } else {
210 STF_FAIL("Can not assemble instruction.");
211 }
212
213 fcml_fn_assembler_result_free(&asm_result);
214 fcml_fn_assembler_free(assembler);
215 fcml_fn_dialect_free(dialect);
216
217 }
218
219 void fcml_tf_assembler_db_imm32_value_out_of_bound(void) {
220
221 fcml_ceh_error error;
222
223 /* Initializes the Intel dialect instance. */
224 fcml_st_dialect *dialect;
225 if ((error = fcml_fn_dialect_init_intel( FCML_INTEL_DIALECT_CF_DEFAULT,
226 &dialect))) {
227 STF_FAIL("Can not initialize Intel dialect.");
228 return;
229 }
230
231 fcml_st_assembler *assembler;
232 if ((error = fcml_fn_assembler_init(dialect, &assembler))) {
233 STF_FAIL("Can not initialize Intel assembler.");
234 fcml_fn_dialect_free(dialect);
235 return;
236 }
237
238 fcml_st_instruction instruction = { 0 };
239 instruction.mnemonic = "db";
240 instruction.operands[0] = FCML_IMM32(0x2323);
241 instruction.operands_count = 1;
242
243 /* Prepares the result. */
244 fcml_st_assembler_result asm_result;
245 fcml_fn_assembler_result_prepare(&asm_result);
246
247 fcml_st_assembler_context context = { 0 };
248 context.assembler = assembler;
249 context.entry_point.ip = 0x401000;
250 context.entry_point.op_mode = FCML_OM_32_BIT;
251
252 /* Assembles the given instruction. */
253 error = fcml_fn_assemble(&context, &instruction, &asm_result);
254
255 STF_ASSERT_EQUAL(FCML_CEH_GEC_VALUE_OUT_OF_RANGE, error);
256
257 fcml_fn_assembler_result_free(&asm_result);
258 fcml_fn_assembler_free(assembler);
259 fcml_fn_dialect_free(dialect);
260 }
261
262 fcml_stf_test_case fcml_ti_assembler[] = {
263 { "fcml_tf_assembler_db_imm8", fcml_tf_assembler_db_imm8},
264 { "fcml_tf_assembler_byte_imm8", fcml_tf_assembler_byte_imm8 },
265 { "fcml_tf_assembler_db_imm32", fcml_tf_assembler_db_imm32 },
266 {"fcml_tf_assembler_db_imm32_value_out_of_bound",
267 fcml_tf_assembler_db_imm32_value_out_of_bound },
268 FCML_STF_NULL_TEST
269 };
270
271 fcml_stf_test_suite fcml_si_assembler = {
272 "suite-fcml-assembler", suite_init, suite_cleanup, fcml_ti_assembler
273 };