Debian Codegraph @

check/internal-tests/lag_assembler_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 "lag_assembler_t.h"
21
22 #include <stdio.h>
23
24 #include <fcml_stf.h>
25 #include <fcml_gas_dialect.h>
26 #include <fcml_intel_dialect.h>
27 #include <fcml_lag_assembler.h>
28
29 fcml_bool fcml_tf_lag_assembler_suite_init(void) {
30 return FCML_TRUE;
31 }
32
33 fcml_bool fcml_tf_lag_assembler_suite_cleanup(void) {
34 return FCML_TRUE;
35 }
36
37 struct fcml_ist_t_lag_test_case {
38 const fcml_string *code_str;
39 const fcml_uint8_t *code_bin;
40 const fcml_int code_size;
41 };
42
43 const fcml_string fcml_iarr_lag_assembler_code_1_src[] = {
44 "start: mov ebx, 1",
45 "loop_big: inc ebx",
46 " cmp ebx, 10",
47 " je finish",
48 "loop_small: mov eax, 1",
49 "increment: inc eax",
50 " cmp eax, 10",
51 " je finish_small",
52 " jmp increment",
53 "finish_small:",
54 " jmp loop_big",
55 "finish: ret",
56 NULL
57 };
58
59 const fcml_uint8_t fcml_iarr_lag_assembler_code_1_bin[] = {
60 0xBB, 0x01, 0x00, 0x00, 0x00,
61 0x43,
62 0x83, 0xFB, 0x0A,
63 0x74, 0x0F,
64 0xB8, 0x01, 0x00, 0x00, 0x00,
65 0x40,
66 0x83, 0xF8, 0x0A,
67 0x74, 0x02,
68 0xEB, 0xF8,
69 0xEB, 0xEB,
70 0xC3
71 };
72
73 const fcml_string fcml_iarr_lag_assembler_code_2_src[] = {
74 "jmp second",
75 "start: jmp finish",
76 " nop",
77 " nop",
78 " nop",
79 " nop",
80 " nop",
81 " nop",
82 " nop",
83 " nop",
84 " nop",
85 " nop",
86 " nop",
87 " nop",
88 " nop",
89 " nop",
90 " nop",
91 " nop",
92 " nop",
93 " nop",
94 " nop",
95 " nop",
96 " nop",
97 " nop",
98 " nop",
99 " nop",
100 " nop",
101 " nop",
102 " nop",
103 " nop",
104 " nop",
105 " nop",
106 " nop",
107 " nop",
108 " nop",
109 " nop",
110 " nop",
111 " nop",
112 " nop",
113 " nop",
114 " nop",
115 " nop",
116 " nop",
117 " nop",
118 " nop",
119 " nop",
120 " nop",
121 " nop",
122 " nop",
123 " nop",
124 " nop",
125 " nop",
126 " nop",
127 " nop",
128 " nop",
129 " nop",
130 " nop",
131 " nop",
132 " nop",
133 " nop",
134 " nop",
135 " nop",
136 " nop",
137 " nop",
138 " nop",
139 " nop",
140 " nop",
141 " nop",
142 " nop",
143 " nop",
144 " nop",
145 " nop",
146 " nop",
147 " nop",
148 " nop",
149 " nop",
150 " nop",
151 " nop",
152 " nop",
153 " nop",
154 " nop",
155 " nop",
156 " nop",
157 " nop",
158 " nop",
159 " nop",
160 " nop",
161 " nop",
162 " nop",
163 " nop",
164 " nop",
165 " nop",
166 " nop",
167 " nop",
168 " nop",
169 " nop",
170 " nop",
171 " nop",
172 " nop",
173 " nop",
174 " nop",
175 " nop",
176 " nop",
177 " nop",
178 " nop",
179 " nop",
180 " nop",
181 " nop",
182 " nop",
183 " nop",
184 " nop",
185 " nop",
186 " nop",
187 " nop",
188 " nop",
189 " nop",
190 " nop",
191 " nop",
192 " nop",
193 " nop",
194 " nop",
195 " nop",
196 " nop",
197 " nop",
198 " nop",
199 " nop",
200 " nop",
201 "second: jmp second+249-(finish-start)",
202 "finish: ret",
203 NULL
204 };
205
206 const fcml_uint8_t fcml_iarr_lag_assembler_code_2_bin[] = {
207 0xE9, 0x82, 0x00, 0x00, 0x00, 0xE9, 0x7F, 0x00, 0x00, 0x00, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
208 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
209 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
210 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
211 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
212 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
213 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
214 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
215 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0xEB, 0x73, 0xC3
216 };
217
218 const fcml_string fcml_iarr_lag_assembler_code_3_src[] = {
219 "je finish",
220 "ret",
221 NULL
222 };
223
224 struct fcml_ist_t_lag_test_case fcml_iarr_lag_test_cases[] = {
225 { fcml_iarr_lag_assembler_code_1_src, fcml_iarr_lag_assembler_code_1_bin, sizeof( fcml_iarr_lag_assembler_code_1_bin ) },
226 { fcml_iarr_lag_assembler_code_2_src, fcml_iarr_lag_assembler_code_2_bin, sizeof( fcml_iarr_lag_assembler_code_2_bin ) },
227 { NULL, NULL, 0 }
228 };
229
230 fcml_bool fcml_fn_lag_assemble_result_check( const fcml_st_lag_assembler_result *result, const fcml_uint8_t *code, const fcml_int size ) {
231 fcml_int code_index = 0;
232 fcml_st_assembled_instruction *current = result->instructions;
233 while( current && code_index <= size ) {
234 fcml_int code_length = current->code_length;
235 int i;
236 for( i = 0; i < code_length && code_index <= size; i++ ) {
237 if( current->code[i] != code[code_index++] ) {
238 printf( "Index %d should be: 0x%x was 0x%x.\n", code_index - 1, code[code_index - 1], current->code[i] );
239 return FCML_FALSE;
240 }
241 }
242 current = current->next;
243 }
244 if( code_index != size ) {
245 printf( "Instruction code do not match." );
246 return FCML_FALSE;
247 }
248 return FCML_TRUE;
249 }
250
251 void fcml_tf_lag_assembler_test(void) {
252
253 fcml_ceh_error error;
254
255 fcml_st_lag_assembler_result result;
256
257 fcml_fn_lag_assembler_result_prepare( &result );
258
259 fcml_st_dialect *dialect;
260
261 error = fcml_fn_dialect_init_intel( FCML_INTEL_DIALECT_CF_DEFAULT, &dialect );
262 if( error ) {
263 STF_FAIL("Can not initialize dialect.");
264 return;
265 }
266
267 fcml_st_assembler *assembler;
268
269 error = fcml_fn_assembler_init( dialect, &assembler );
270 if( error ) {
271 STF_FAIL("Can not initialize assembler.");
272 return;
273 }
274
275 struct fcml_ist_t_lag_test_case *test_cases = &(fcml_iarr_lag_test_cases[0]);
276
277 fcml_int counter = 0;
278
279 while( test_cases->code_bin ) {
280
281 fcml_st_lag_assembler_context context = {0};
282 context.assembler = assembler;
283 context.entry_point.op_mode = FCML_OM_32_BIT;
284 context.entry_point.ip = 0x00401000;
285
286 error = fcml_fn_lag_assemble( &context, test_cases->code_str, &result );
287 if( error ) {
288 STF_FAIL("Failed to assemble code.");
289 return;
290 }
291
292 fcml_bool ok = fcml_fn_lag_assemble_result_check( &result, test_cases->code_bin, test_cases->code_size );
293 STF_ASSERT( ok );
294 if( !ok ) {
295 printf("Failed instruction nr: %d.\n", counter );
296 }
297
298 fcml_fn_symbol_table_free( context.symbol_table );
299
300 fcml_fn_lag_assembler_result_free( &result );
301
302 test_cases++;
303
304 counter++;
305 }
306
307 fcml_fn_assembler_free( assembler );
308
309 fcml_fn_dialect_free( dialect );
310
311 }
312
313 void fcml_tf_lag_assembler_test_1(void) {
314
315 fcml_ceh_error error;
316
317 fcml_st_lag_assembler_result result;
318
319 fcml_fn_lag_assembler_result_prepare( &result );
320
321 fcml_st_dialect *dialect;
322
323 //error = fcml_fn_dialect_init_gas( FCML_GAS_DIALECT_CF_DEFAULT, &dialect );
324 error = fcml_fn_dialect_init_intel( FCML_INTEL_DIALECT_CF_DEFAULT, &dialect );
325 if( error ) {
326 STF_FAIL("Can not initialize dialect.");
327 return;
328 }
329
330 fcml_st_assembler *assembler;
331
332 error = fcml_fn_assembler_init( dialect, &assembler );
333 if( error ) {
334 STF_FAIL("Can not initialize assembler.");
335 return;
336 }
337
338 fcml_st_lag_assembler_context context = {0};
339 context.configuration.enable_error_messages = FCML_TRUE;
340 context.assembler = assembler;
341 context.entry_point.op_mode = FCML_OM_32_BIT;
342 context.entry_point.ip = 0x00401000;
343
344 error = fcml_fn_lag_assemble( &context, fcml_iarr_lag_assembler_code_3_src, &result );
345 STF_ASSERT_EQUAL( error, FCML_CEH_GEC_UNDEFINED_SYMBOL );
346 STF_ASSERT_PTR_NOT_NULL( result.errors.errors );
347 if( result.errors.errors ) {
348 STF_ASSERT_STRING_EQUAL( result.errors.errors->message, FCML_TEXT( "Undefined symbol: finish." ) );
349 }
350
351 fcml_fn_symbol_table_free( context.symbol_table );
352
353 fcml_fn_lag_assembler_result_free( &result );
354
355 fcml_fn_assembler_free( assembler );
356
357 fcml_fn_dialect_free( dialect );
358 }
359
360 fcml_stf_test_case fcml_ti_lag_assembler[] = {
361 { "fcml_tf_lag_assembler_test", fcml_tf_lag_assembler_test },
362 { "fcml_tf_lag_assembler_test_1", fcml_tf_lag_assembler_test_1 },
363 FCML_STF_NULL_TEST
364 };
365
366 fcml_stf_test_suite fcml_si_lag_assembler = {
367 "suite-fcml-lag-assembler", fcml_tf_lag_assembler_suite_init, fcml_tf_lag_assembler_suite_cleanup, fcml_ti_lag_assembler
368 };