Debian Codegraph @

check/internal-tests/gas_parser_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 "gas_parser_t.h"
21
22 #include <string.h>
23
24 #include <fcml_ceh.h>
25 #include <fcml_common.h>
26 #include <fcml_types.h>
27 #include <fcml_errors.h>
28 #include <fcml_parser.h>
29 #include <fcml_gas_dialect.h>
30
31 fcml_st_dialect *internal_dialect_gas = NULL;
32 fcml_st_parser_context internal_gas_context = {0};
33
34 fcml_bool fcml_tf_gas_parser_suite_init(void) {
35 fcml_fn_dialect_init_gas( FCML_GAS_DIALECT_CF_DEFAULT, &internal_dialect_gas );
36 internal_gas_context.dialect = internal_dialect_gas;
37 return internal_dialect_gas != NULL;
38 }
39
40 fcml_bool fcml_tf_gas_parser_suite_cleanup(void) {
41 if( internal_dialect_gas ) {
42 fcml_fn_dialect_free( internal_dialect_gas );
43 }
44 if( internal_gas_context.symbol_table ) {
45 fcml_fn_symbol_table_free( internal_gas_context.symbol_table );
46 }
47 return FCML_TRUE;
48 }
49
50 void fcml_tf_parser_gas_parse_test1(void) {
51 fcml_st_parser_result result;
52 fcml_fn_parser_result_prepare( &result );
53 STF_ASSERT_EQUAL( fcml_fn_parse( &internal_gas_context, "mov $80-90", &result ), FCML_CEH_GEC_NO_ERROR );
54 if( result.instruction != NULL ) {
55 STF_ASSERT_EQUAL( result.instruction->operands[0].type, FCML_OT_IMMEDIATE );
56 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.size, FCML_DS_8 );
57 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.is_signed, FCML_TRUE );
58 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.int8, (fcml_int8_t)-10 );
59 } else {
60 STF_FAIL();
61 }
62 fcml_fn_parser_result_free( &result );
63 }
64
65 void fcml_tf_parser_gas_parse_test2(void) {
66 fcml_st_parser_result result;
67 fcml_fn_parser_result_prepare( &result );
68 STF_ASSERT_EQUAL( fcml_fn_parse( &internal_gas_context, "mov (%eax)", &result ), FCML_CEH_GEC_NO_ERROR );
69 if( result.instruction != NULL ) {
70 STF_ASSERT_EQUAL( result.instruction->operands[0].type, FCML_OT_ADDRESS );
71 STF_ASSERT_EQUAL( result.instruction->operands[0].address.address_form, FCML_AF_COMBINED );
72 STF_ASSERT_EQUAL( result.instruction->operands[0].address.effective_address.base.size, FCML_DS_32 );
73 STF_ASSERT_EQUAL( result.instruction->operands[0].address.effective_address.base.reg, FCML_REG_EAX );
74 } else {
75 STF_FAIL();
76 }
77 fcml_fn_parser_result_free( &result );
78 }
79
80 void fcml_tf_parser_gas_parse_test3(void) {
81 fcml_st_parser_result result;
82 fcml_fn_parser_result_prepare( &result );
83 STF_ASSERT_EQUAL( fcml_fn_parse( &internal_gas_context, "movq $1,%cs:(%rax)", &result ), FCML_CEH_GEC_NO_ERROR );
84 if( result.instruction != NULL ) {
85 STF_ASSERT_EQUAL( result.instruction->operands[0].type, FCML_OT_IMMEDIATE );
86 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.size, FCML_DS_8 );
87 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.is_signed, FCML_FALSE );
88 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.int8, (fcml_uint8_t)(fcml_int8_t)1 );
89 STF_ASSERT_EQUAL( result.instruction->operands[1].type, FCML_OT_ADDRESS );
90 STF_ASSERT_EQUAL( result.instruction->operands[1].address.address_form, FCML_AF_COMBINED );
91 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.size, FCML_DS_64 );
92 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.reg, FCML_REG_RAX );
93 STF_ASSERT_EQUAL( result.instruction->operands[1].address.segment_selector.segment_selector.reg, FCML_REG_CS );
94 } else {
95 STF_FAIL();
96 }
97 fcml_fn_parser_result_free( &result );
98 }
99
100 void fcml_tf_parser_gas_parse_test4(void) {
101 fcml_st_parser_result result;
102 fcml_fn_parser_result_prepare( &result );
103 STF_ASSERT_EQUAL( fcml_fn_parse( &internal_gas_context, "movq $1,%cs:(%rax,%rbx)", &result ), FCML_CEH_GEC_NO_ERROR );
104 if( result.instruction != NULL ) {
105 STF_ASSERT_EQUAL( result.instruction->operands[0].type, FCML_OT_IMMEDIATE );
106 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.size, FCML_DS_8 );
107 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.is_signed, FCML_FALSE );
108 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.int8, (fcml_int8_t)1 );
109 STF_ASSERT_EQUAL( result.instruction->operands[1].type, FCML_OT_ADDRESS );
110 STF_ASSERT_EQUAL( result.instruction->operands[1].address.address_form, FCML_AF_COMBINED );
111 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.size, FCML_DS_64 );
112 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.reg, FCML_REG_RAX );
113 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.index.size, FCML_DS_64 );
114 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.index.reg, FCML_REG_RBX );
115 STF_ASSERT_EQUAL( result.instruction->operands[1].address.segment_selector.segment_selector.reg, FCML_REG_CS );
116 } else {
117 STF_FAIL();
118 }
119 fcml_fn_parser_result_free( &result );
120 }
121
122 void fcml_tf_parser_gas_parse_test5(void) {
123 /* Index only.*/
124 fcml_st_parser_result result;
125 fcml_fn_parser_result_prepare( &result );
126 STF_ASSERT_EQUAL( fcml_fn_parse( &internal_gas_context, "movq $1,%cs:(,%rbx)", &result ), FCML_CEH_GEC_NO_ERROR );
127 if( result.instruction != NULL ) {
128 STF_ASSERT_EQUAL( result.instruction->operands[0].type, FCML_OT_IMMEDIATE );
129 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.size, FCML_DS_8 );
130 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.is_signed, FCML_FALSE );
131 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.int8, (fcml_int8_t)1 );
132 STF_ASSERT_EQUAL( result.instruction->operands[1].type, FCML_OT_ADDRESS );
133 STF_ASSERT_EQUAL( result.instruction->operands[1].address.address_form, FCML_AF_COMBINED );
134 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.size, FCML_DS_UNDEF );
135 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.reg, FCML_REG_UNDEFINED );
136 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.index.size, FCML_DS_64 );
137 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.index.reg, FCML_REG_RBX );
138 STF_ASSERT_EQUAL( result.instruction->operands[1].address.segment_selector.segment_selector.reg, FCML_REG_CS );
139 } else {
140 STF_FAIL();
141 }
142 fcml_fn_parser_result_free( &result );
143 }
144
145 void fcml_tf_parser_gas_parse_test6(void) {
146 /* Index only.*/
147 fcml_st_parser_result result;
148 fcml_fn_parser_result_prepare( &result );
149 STF_ASSERT_EQUAL( fcml_fn_parse( &internal_gas_context, "movq $1,%cs:(,%rbx,4)", &result ), FCML_CEH_GEC_NO_ERROR );
150 if( result.instruction != NULL ) {
151 STF_ASSERT_EQUAL( result.instruction->operands[0].type, FCML_OT_IMMEDIATE );
152 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.size, FCML_DS_8 );
153 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.is_signed, FCML_FALSE );
154 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.int8, (fcml_int8_t)1 );
155 STF_ASSERT_EQUAL( result.instruction->operands[1].type, FCML_OT_ADDRESS );
156 STF_ASSERT_EQUAL( result.instruction->operands[1].address.address_form, FCML_AF_COMBINED );
157 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.size, FCML_DS_UNDEF );
158 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.reg, FCML_REG_UNDEFINED );
159 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.index.size, FCML_DS_64 );
160 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.index.reg, FCML_REG_RBX );
161 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.scale_factor, 4 );
162 STF_ASSERT_EQUAL( result.instruction->operands[1].address.segment_selector.segment_selector.reg, FCML_REG_CS );
163 } else {
164 STF_FAIL();
165 }
166 fcml_fn_parser_result_free( &result );
167 }
168
169 void fcml_tf_parser_gas_parse_test7(void) {
170 /* Index only.*/
171 fcml_st_parser_result result;
172 fcml_fn_parser_result_prepare( &result );
173 STF_ASSERT_NOT_EQUAL( fcml_fn_parse( &internal_gas_context, "movq $1,%cs:(,%rbx,)", &result ), FCML_CEH_GEC_NO_ERROR );
174 fcml_fn_parser_result_free( &result );
175 }
176
177 void fcml_tf_parser_gas_parse_test8(void) {
178 /* Index only.*/
179 fcml_st_parser_result result;
180 fcml_fn_parser_result_prepare( &result );
181 STF_ASSERT_EQUAL( fcml_fn_parse( &internal_gas_context, "movq $1,%cs:0x100(,%rbx,4)", &result ), FCML_CEH_GEC_NO_ERROR );
182 if( result.instruction != NULL ) {
183 STF_ASSERT_EQUAL( result.instruction->operands[0].type, FCML_OT_IMMEDIATE );
184 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.size, FCML_DS_8 );
185 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.is_signed, FCML_FALSE );
186 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.int8, (fcml_int8_t)1 );
187 STF_ASSERT_EQUAL( result.instruction->operands[1].type, FCML_OT_ADDRESS );
188 STF_ASSERT_EQUAL( result.instruction->operands[1].address.address_form, FCML_AF_COMBINED );
189 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.size, FCML_DS_UNDEF );
190 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.reg, FCML_REG_UNDEFINED );
191 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.index.size, FCML_DS_64 );
192 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.index.reg, FCML_REG_RBX );
193 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.scale_factor, 4 );
194 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.displacement.size, FCML_DS_16 );
195 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.displacement.int16, (fcml_uint16_t)0x100 );
196 STF_ASSERT_EQUAL( result.instruction->operands[1].address.segment_selector.segment_selector.reg, FCML_REG_CS );
197 } else {
198 STF_FAIL();
199 }
200 fcml_fn_parser_result_free( &result );
201 }
202
203 void fcml_tf_parser_gas_parse_test9(void) {
204 /* Index only.*/
205 fcml_st_parser_result result;
206 fcml_fn_parser_result_prepare( &result );
207 STF_ASSERT_EQUAL( fcml_fn_parse( &internal_gas_context, "movq $1,%cs:0x100", &result ), FCML_CEH_GEC_NO_ERROR );
208 if( result.instruction != NULL ) {
209 STF_ASSERT_EQUAL( result.instruction->operands[0].type, FCML_OT_IMMEDIATE );
210 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.size, FCML_DS_8 );
211 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.is_signed, FCML_FALSE );
212 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.int8, (fcml_int8_t)1 );
213 STF_ASSERT_EQUAL( result.instruction->operands[1].type, FCML_OT_ADDRESS );
214 STF_ASSERT_EQUAL( result.instruction->operands[1].address.address_form, FCML_AF_OFFSET );
215 STF_ASSERT_EQUAL( result.instruction->operands[1].address.offset.size, FCML_DS_16 );
216 STF_ASSERT_EQUAL( result.instruction->operands[1].address.offset.off16, (fcml_uint16_t)0x100 );
217 STF_ASSERT_EQUAL( result.instruction->operands[1].address.segment_selector.segment_selector.reg, FCML_REG_CS );
218 } else {
219 STF_FAIL();
220 }
221 fcml_fn_parser_result_free( &result );
222 }
223
224 void fcml_tf_parser_gas_parse_test10(void) {
225 /* Index only.*/
226 fcml_st_parser_result result;
227 fcml_fn_parser_result_prepare( &result );
228 STF_ASSERT_EQUAL( fcml_fn_parse( &internal_gas_context, "movq $1,%cs:0x100(%rax,%rbx,4)", &result ), FCML_CEH_GEC_NO_ERROR );
229 if( result.instruction != NULL ) {
230 STF_ASSERT_EQUAL( result.instruction->operands[0].type, FCML_OT_IMMEDIATE );
231 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.size, FCML_DS_8 );
232 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.is_signed, FCML_FALSE );
233 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.int8, (fcml_int8_t)1 );
234 STF_ASSERT_EQUAL( result.instruction->operands[1].type, FCML_OT_ADDRESS );
235 STF_ASSERT_EQUAL( result.instruction->operands[1].address.address_form, FCML_AF_COMBINED );
236 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.size, FCML_DS_64 );
237 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.reg, FCML_REG_RAX );
238 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.index.size, FCML_DS_64 );
239 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.index.reg, FCML_REG_RBX );
240 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.scale_factor, 4 );
241 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.displacement.size, FCML_DS_16 );
242 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.displacement.int16, (fcml_uint16_t)0x100 );
243 STF_ASSERT_EQUAL( result.instruction->operands[1].address.segment_selector.segment_selector.reg, FCML_REG_CS );
244 } else {
245 STF_FAIL();
246 }
247 fcml_fn_parser_result_free( &result );
248 }
249
250 void fcml_tf_parser_gas_parse_test11(void) {
251 /* RIP only.*/
252 fcml_st_parser_result result;
253 fcml_fn_parser_result_prepare( &result );
254 STF_ASSERT_EQUAL( fcml_fn_parse( &internal_gas_context, "movq $1,0x100(%rip)", &result ), FCML_CEH_GEC_NO_ERROR );
255 if( result.instruction != NULL ) {
256 STF_ASSERT_EQUAL( result.instruction->operands[0].type, FCML_OT_IMMEDIATE );
257 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.size, FCML_DS_8 );
258 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.is_signed, FCML_FALSE );
259 STF_ASSERT_EQUAL( result.instruction->operands[0].immediate.int8, (fcml_int8_t)1 );
260 STF_ASSERT_EQUAL( result.instruction->operands[1].type, FCML_OT_ADDRESS );
261 STF_ASSERT_EQUAL( result.instruction->operands[1].address.address_form, FCML_AF_COMBINED );
262 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.reg, FCML_REG_UNDEFINED );
263 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.size, FCML_DS_64 );
264 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.base.type, FCML_REG_IP );
265 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.displacement.size, FCML_DS_16 );
266 STF_ASSERT_EQUAL( result.instruction->operands[1].address.effective_address.displacement.int16, (fcml_uint16_t)0x100 );
267 } else {
268 STF_FAIL();
269 }
270 fcml_fn_parser_result_free( &result );
271 }
272
273 void fcml_tf_parser_gas_parse_test12(void) {
274 fcml_st_parser_result result;
275 fcml_fn_parser_result_prepare( &result );
276 STF_ASSERT_EQUAL( fcml_fn_parse( &internal_gas_context, "mov *0x80", &result ), FCML_CEH_GEC_NO_ERROR );
277 if( result.instruction != NULL ) {
278 STF_ASSERT_EQUAL( result.instruction->operands[0].type, FCML_OT_ADDRESS );
279 STF_ASSERT_EQUAL( result.instruction->hints, FCML_HINT_INDIRECT_POINTER );
280 } else {
281 STF_FAIL();
282 }
283 fcml_fn_parser_result_free( &result );
284 }
285
286 void fcml_tf_parser_gas_parse_test_symbols_1(void) {
287 fcml_st_parser_result result;
288 fcml_fn_parser_result_prepare( &result );
289 fcml_st_parser_context context = {0};
290 context.dialect = internal_dialect_gas;
291 context.configuration.alloc_symbol_table_if_needed = FCML_TRUE;
292
293 context.ip = 0x401000;
294
295 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "label:" ), &result ), FCML_CEH_GEC_NO_ERROR );
296 STF_ASSERT_PTR_NULL( result.instruction );
297 STF_ASSERT_PTR_NOT_NULL( result.symbol );
298 if( result.symbol ) {
299 STF_ASSERT_EQUAL( result.symbol->value, 0x401000 );
300 STF_ASSERT_STRING_EQUAL( result.symbol->symbol, FCML_TEXT( "label" ) );
301 }
302
303 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "_label:" ), &result ), FCML_CEH_GEC_NO_ERROR );
304 STF_ASSERT_PTR_NULL( result.instruction );
305 STF_ASSERT_PTR_NOT_NULL( result.symbol );
306 if( result.symbol ) {
307 STF_ASSERT_EQUAL( result.symbol->value, 0x401000 );
308 STF_ASSERT_STRING_EQUAL( result.symbol->symbol, FCML_TEXT( "_label" ) );
309 }
310
311 fcml_fn_parser_result_free( &result );
312 fcml_fn_symbol_table_free( context.symbol_table );
313 }
314
315 void fcml_tf_parser_gas_parse_test_symbols_2(void) {
316 fcml_st_parser_result result;
317 fcml_fn_parser_result_prepare( &result );
318 fcml_st_parser_context context = {0};
319 context.dialect = internal_dialect_gas;
320 context.configuration.override_labels = FCML_FALSE;
321 context.configuration.alloc_symbol_table_if_needed = FCML_TRUE;
322 context.ip = 0x401000;
323
324 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "label: mov %eax,1" ), &result ), FCML_CEH_GEC_NO_ERROR );
325 STF_ASSERT_PTR_NOT_NULL( result.instruction );
326 if( result.instruction ) {
327 STF_ASSERT_STRING_EQUAL( result.instruction->mnemonic, "mov" );
328 }
329 STF_ASSERT_PTR_NOT_NULL( result.symbol );
330 if( result.symbol ) {
331 STF_ASSERT_EQUAL( result.symbol->value, 0x401000 );
332 STF_ASSERT_STRING_EQUAL( result.symbol->symbol, FCML_TEXT( "label" ) );
333 }
334
335 fcml_fn_parser_result_free( &result );
336 fcml_fn_symbol_table_free( context.symbol_table );
337 }
338
339 void fcml_tf_parser_gas_parse_test_symbols_3(void) {
340
341 fcml_st_parser_result result;
342
343 fcml_fn_parser_result_prepare( &result );
344
345 fcml_st_parser_context context = {0};
346 context.dialect = internal_dialect_gas;
347 context.configuration.override_labels = FCML_FALSE;
348 context.configuration.alloc_symbol_table_if_needed = FCML_TRUE;
349 context.ip = 0x401000;
350
351 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "label: mov %eax,1" ), &result ), FCML_CEH_GEC_NO_ERROR );
352 STF_ASSERT_PTR_NOT_NULL( result.instruction );
353 if( result.instruction ) {
354 STF_ASSERT_STRING_EQUAL( result.instruction->mnemonic, "mov" );
355 }
356 STF_ASSERT_PTR_NOT_NULL( result.symbol );
357 if( result.symbol ) {
358 STF_ASSERT_EQUAL( result.symbol->value, 0x401000 );
359 STF_ASSERT_STRING_EQUAL( result.symbol->symbol, FCML_TEXT( "label" ) );
360 }
361
362 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "label: mov %eax,1" ), &result ), FCML_CEH_GEC_INVALID_INPUT );
363
364 fcml_st_ceh_error_info *error = result.errors.errors;
365 STF_ASSERT_PTR_NOT_NULL( error );
366 if( error ) {
367 STF_ASSERT_EQUAL( error->code, FCML_CEH_MEC_ERROR_SYMBOL_ALREADY_DEFINED );
368 STF_ASSERT_STRING_EQUAL( error->message, FCML_TEXT( "Symbol already defined: label." ) );
369 }
370
371 context.configuration.override_labels = FCML_TRUE;
372
373 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "label: add %eax,1" ), &result ), FCML_CEH_GEC_NO_ERROR );
374 STF_ASSERT_PTR_NOT_NULL( result.instruction );
375 if( result.instruction ) {
376 STF_ASSERT_STRING_EQUAL( result.instruction->mnemonic, "add" );
377 }
378 STF_ASSERT_PTR_NOT_NULL( result.symbol );
379 if( result.symbol ) {
380 STF_ASSERT_EQUAL( result.symbol->value, 0x401000 );
381 STF_ASSERT_STRING_EQUAL( result.symbol->symbol, FCML_TEXT( "label" ) );
382 }
383
384 fcml_fn_parser_result_free( &result );
385 fcml_fn_symbol_table_free( context.symbol_table );
386 }
387
388 /* Undefined symbol. */
389 void fcml_tf_parser_gas_parse_test_symbols_4(void) {
390 fcml_st_parser_result result;
391 fcml_fn_parser_result_prepare( &result );
392 fcml_st_parser_context context = {0};
393 context.dialect = internal_dialect_gas;
394 context.configuration.alloc_symbol_table_if_needed = FCML_TRUE;
395 context.ip = 0x401000;
396
397 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "label: mov %eax, symbol" ), &result ), FCML_CEH_GEC_UNDEFINED_SYMBOL );
398 STF_ASSERT_PTR_NULL( result.instruction );
399 STF_ASSERT_PTR_NULL( result.symbol );
400
401 fcml_st_ceh_error_info *error = result.errors.errors;
402 STF_ASSERT_PTR_NOT_NULL( error );
403 if( error ) {
404 STF_ASSERT_EQUAL( error->code, FCML_CEH_MEC_ERROR_UNDEFINED_SYMBOL );
405 STF_ASSERT_STRING_EQUAL( error->message, FCML_TEXT( "Undefined symbol: symbol." ) );
406 }
407
408 fcml_fn_parser_result_free( &result );
409 fcml_fn_symbol_table_free( context.symbol_table );
410 }
411
412 /* Ignore symbols. */
413 void fcml_tf_parser_gas_parse_test_symbols_5(void) {
414
415 fcml_st_parser_result result;
416 fcml_fn_parser_result_prepare( &result );
417 fcml_st_parser_context context = {0};
418 context.dialect = internal_dialect_gas;
419 context.ip = 0x401000;
420 context.configuration.ignore_undefined_symbols = FCML_TRUE;
421 context.configuration.alloc_symbol_table_if_needed = FCML_TRUE;
422 context.configuration.override_labels = FCML_TRUE;
423
424 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "label: mov %eax, $symbol" ), &result ), FCML_CEH_GEC_NO_ERROR );
425 STF_ASSERT_PTR_NOT_NULL( result.instruction );
426 STF_ASSERT_PTR_NOT_NULL( result.symbol );
427
428 if( result.instruction ) {
429 fcml_st_instruction *instruction = result.instruction;
430 STF_ASSERT_EQUAL( instruction->operands[1].type, FCML_OT_IMMEDIATE );
431 STF_ASSERT_EQUAL( instruction->operands[1].immediate.size, FCML_DS_8 );
432 STF_ASSERT_EQUAL( instruction->operands[1].immediate.int8, 0 );
433 STF_ASSERT_EQUAL( instruction->operands[1].immediate.is_signed, FCML_TRUE );
434 }
435
436 /* Label will be overriden. */
437 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "label: mov %eax, symbol" ), &result ), FCML_CEH_GEC_NO_ERROR );
438 STF_ASSERT_PTR_NOT_NULL( result.instruction );
439 STF_ASSERT_PTR_NOT_NULL( result.symbol );
440
441 if( result.instruction ) {
442 fcml_st_instruction *instruction = result.instruction;
443 STF_ASSERT_EQUAL( instruction->operands[1].type, FCML_OT_ADDRESS );
444 STF_ASSERT_EQUAL( instruction->operands[1].address.address_form, FCML_AF_OFFSET );
445 }
446
447 fcml_fn_parser_result_free( &result );
448 fcml_fn_symbol_table_free( context.symbol_table );
449 }
450
451 /* Use defined symbol in the instruction. */
452 void fcml_tf_parser_gas_parse_test_symbols_6(void) {
453
454 fcml_st_parser_result result;
455 fcml_fn_parser_result_prepare( &result );
456 fcml_st_parser_context context = {0};
457 context.dialect = internal_dialect_gas;
458 context.ip = 0x401000;
459 context.configuration.ignore_undefined_symbols = FCML_TRUE;
460 context.configuration.override_labels = FCML_TRUE;
461 context.configuration.alloc_symbol_table_if_needed = FCML_TRUE;
462
463 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "label: mov %eax, $label" ), &result ), FCML_CEH_GEC_NO_ERROR );
464 STF_ASSERT_PTR_NOT_NULL( result.instruction );
465 STF_ASSERT_PTR_NOT_NULL( result.symbol );
466
467 if( result.instruction ) {
468 fcml_st_instruction *instruction = result.instruction;
469 STF_ASSERT_EQUAL( instruction->operands[1].type, FCML_OT_IMMEDIATE );
470 STF_ASSERT_EQUAL( instruction->operands[1].immediate.size, FCML_DS_32 );
471 STF_ASSERT_EQUAL( instruction->operands[1].immediate.int32, 0x401000 );
472 STF_ASSERT_EQUAL( instruction->operands[1].immediate.is_signed, FCML_TRUE );
473 }
474
475 fcml_fn_parser_result_free( &result );
476 fcml_fn_symbol_table_free( context.symbol_table );
477 }
478
479 /* Pseudo operations operands parsing. */
480 void fcml_tf_parser_gas_parse_test_pseudo_operation_1(void) {
481
482 fcml_st_parser_result result;
483 fcml_fn_parser_result_prepare( &result );
484 fcml_st_parser_context context = {0};
485 context.dialect = internal_dialect_gas;
486 context.ip = 0x401000;
487 context.configuration.ignore_undefined_symbols = FCML_TRUE;
488 context.configuration.override_labels = FCML_TRUE;
489 context.configuration.alloc_symbol_table_if_needed = FCML_TRUE;
490
491 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "label: .byte 0x12" ), &result ), FCML_CEH_GEC_NO_ERROR );
492 STF_ASSERT_PTR_NOT_NULL( result.instruction );
493 STF_ASSERT_PTR_NOT_NULL( result.symbol );
494
495 if( result.instruction ) {
496 fcml_st_instruction *instruction = result.instruction;
497 STF_ASSERT_STRING_EQUAL( ".byte", result.instruction->mnemonic );
498 STF_ASSERT_EQUAL( 1, instruction->operands_count );
499 STF_ASSERT_EQUAL( instruction->operands[0].type, FCML_OT_IMMEDIATE );
500 STF_ASSERT_EQUAL( instruction->operands[0].immediate.size, FCML_DS_8 );
501 STF_ASSERT_EQUAL( instruction->operands[0].immediate.int8, 0x12 );
502 STF_ASSERT_EQUAL( instruction->operands[0].immediate.is_signed, FCML_FALSE );
503 }
504
505 fcml_fn_parser_result_free( &result );
506 fcml_fn_symbol_table_free( context.symbol_table );
507 }
508
509 /* Pseudo operations operands parsing. */
510 void fcml_tf_parser_gas_parse_test_pseudo_operation_2(void) {
511
512 fcml_st_parser_result result;
513 fcml_fn_parser_result_prepare( &result );
514 fcml_st_parser_context context = {0};
515 context.dialect = internal_dialect_gas;
516 context.ip = 0x401000;
517 context.configuration.ignore_undefined_symbols = FCML_TRUE;
518 context.configuration.override_labels = FCML_TRUE;
519 context.configuration.alloc_symbol_table_if_needed = FCML_TRUE;
520
521 STF_ASSERT_EQUAL( fcml_fn_parse( &context, FCML_TEXT( "label: .byte %rax" ), &result ), FCML_CEH_GEC_INVALID_INPUT );
522 STF_ASSERT_PTR_NULL( result.instruction );
523 STF_ASSERT_PTR_NULL( result.symbol );
524
525 fcml_fn_parser_result_free( &result );
526 fcml_fn_symbol_table_free( context.symbol_table );
527 }
528
529 void fcml_tf_parser_gas_parse_test_decorator_bcast(void) {
530 fcml_st_parser_result result;
531 fcml_fn_parser_result_prepare(&result);
532 STF_ASSERT_EQUAL(
533 fcml_fn_parse(&internal_gas_context, "vaddpd (%eax){1to8}",
534 &result), FCML_CEH_GEC_NO_ERROR);
535 if (result.instruction != NULL) {
536 fcml_st_operand *operand = &(result.instruction->operands[0]);
537 STF_ASSERT_EQUAL(operand->type, FCML_OT_ADDRESS);
538 STF_ASSERT_EQUAL(operand->address.address_form, FCML_AF_COMBINED);
539 STF_ASSERT_EQUAL(operand->address.effective_address.base.type,
540 FCML_REG_GPR);
541 STF_ASSERT_EQUAL(operand->address.effective_address.base.reg,
542 FCML_REG_EAX);
543 STF_ASSERT_EQUAL(operand->decorators.bcast.is_not_null, FCML_TRUE);
544 STF_ASSERT_EQUAL(operand->decorators.bcast.value, 8);
545 } else {
546 STF_FAIL();
547 }
548 fcml_fn_parser_result_free(&result);
549 }
550
551 void fcml_tf_parser_gas_parse_test_decorator_opmask(void) {
552 fcml_st_parser_result result;
553 fcml_fn_parser_result_prepare(&result);
554 STF_ASSERT_EQUAL(
555 fcml_fn_parse(&internal_gas_context, "vaddpd %zmm1{%k1}",
556 &result), FCML_CEH_GEC_NO_ERROR);
557 if (result.instruction != NULL) {
558 fcml_st_operand *operand = &(result.instruction->operands[0]);
559 STF_ASSERT_EQUAL(operand->type, FCML_OT_REGISTER);
560 STF_ASSERT_EQUAL(operand->reg.type, FCML_REG_SIMD);
561 STF_ASSERT_EQUAL(operand->reg.reg, FCML_REG_ZMM1);
562 STF_ASSERT_EQUAL(operand->reg.size, FCML_DS_512);
563 STF_ASSERT_EQUAL(operand->decorators.operand_mask_reg.type,
564 FCML_REG_OPMASK);
565 STF_ASSERT_EQUAL(operand->decorators.operand_mask_reg.size,
566 FCML_DS_64);
567 STF_ASSERT_EQUAL(operand->decorators.operand_mask_reg.reg,
568 FCML_REG_K1);
569 STF_ASSERT_EQUAL(operand->decorators.z, FCML_FALSE);
570
571 } else {
572 STF_FAIL();
573 }
574 fcml_fn_parser_result_free(&result);
575 }
576
577 void fcml_tf_parser_gas_parse_test_decorator_opmask_k_z_for_addressing(void) {
578 fcml_st_parser_result result;
579 fcml_fn_parser_result_prepare(&result);
580 STF_ASSERT_EQUAL(
581 fcml_fn_parse(&internal_gas_context, "vaddpd %cs:(%rax,%rbx){%k4}{z}",
582 &result), FCML_CEH_GEC_NO_ERROR);
583 if (result.instruction != NULL) {
584 fcml_st_operand *operand = &(result.instruction->operands[0]);
585
586 STF_ASSERT_EQUAL(result.instruction->operands[0].
587 type, FCML_OT_ADDRESS);
588 STF_ASSERT_EQUAL(result.instruction->operands[0].
589 address.address_form, FCML_AF_COMBINED);
590 STF_ASSERT_EQUAL(result.instruction->operands[0].
591 address.effective_address.base.size, FCML_DS_64);
592 STF_ASSERT_EQUAL(result.instruction->operands[0].
593 address.effective_address.base.reg, FCML_REG_RAX);
594 STF_ASSERT_EQUAL(result.instruction->operands[0].
595 address.effective_address.index.size, FCML_DS_64);
596 STF_ASSERT_EQUAL(result.instruction->operands[0].
597 address.effective_address.index.reg, FCML_REG_RBX);
598 STF_ASSERT_EQUAL(result.instruction->operands[0].
599 address.segment_selector.segment_selector.reg, FCML_REG_CS);
600
601 STF_ASSERT_EQUAL(operand->decorators.operand_mask_reg.type,
602 FCML_REG_OPMASK);
603 STF_ASSERT_EQUAL(operand->decorators.operand_mask_reg.size,
604 FCML_DS_64);
605 STF_ASSERT_EQUAL(operand->decorators.operand_mask_reg.reg,
606 FCML_REG_K4);
607 STF_ASSERT_EQUAL(operand->decorators.z, FCML_TRUE);
608 } else {
609 STF_FAIL();
610 }
611 fcml_fn_parser_result_free(&result);
612 }
613
614 void fcml_tf_parser_gas_parse_test_decorator_opmask_k(void) {
615 fcml_st_parser_result result;
616 fcml_fn_parser_result_prepare(&result);
617 STF_ASSERT_EQUAL(
618 fcml_fn_parse(&internal_gas_context, "vaddpd %zmm31{%k4}{z}",
619 &result), FCML_CEH_GEC_NO_ERROR);
620 if (result.instruction != NULL) {
621 fcml_st_operand *operand = &(result.instruction->operands[0]);
622 STF_ASSERT_EQUAL(operand->type, FCML_OT_REGISTER);
623 STF_ASSERT_EQUAL(operand->reg.type, FCML_REG_SIMD);
624 STF_ASSERT_EQUAL(operand->reg.reg, FCML_REG_ZMM31);
625 STF_ASSERT_EQUAL(operand->reg.size, FCML_DS_512);
626 STF_ASSERT_EQUAL(operand->decorators.operand_mask_reg.type,
627 FCML_REG_OPMASK);
628 STF_ASSERT_EQUAL(operand->decorators.operand_mask_reg.size,
629 FCML_DS_64);
630 STF_ASSERT_EQUAL(operand->decorators.operand_mask_reg.reg,
631 FCML_REG_K4);
632 STF_ASSERT_EQUAL(operand->decorators.z, FCML_TRUE);
633 } else {
634 STF_FAIL();
635 }
636 fcml_fn_parser_result_free(&result);
637 }
638
639 void fcml_tf_parser_gas_parse_test_decorator_z(void) {
640 fcml_st_parser_result result;
641 fcml_fn_parser_result_prepare(&result);
642 STF_ASSERT_EQUAL(
643 fcml_fn_parse(&internal_gas_context, "vaddpd %zmm31{z}",
644 &result), FCML_CEH_GEC_NO_ERROR);
645 if (result.instruction != NULL) {
646 fcml_st_operand *operand = &(result.instruction->operands[0]);
647 STF_ASSERT_EQUAL(operand->decorators.z, FCML_TRUE);
648 } else {
649 STF_FAIL();
650 }
651 fcml_fn_parser_result_free(&result);
652 }
653
654 void fcml_tf_parser_gas_parse_test_decorator_er(void) {
655 fcml_st_parser_result result;
656 fcml_fn_parser_result_prepare(&result);
657 STF_ASSERT_EQUAL(
658 fcml_fn_parse(&internal_gas_context, "vaddpd {ru-sae}",
659 &result), FCML_CEH_GEC_NO_ERROR);
660 if (result.instruction != NULL) {
661 fcml_st_operand *operand = &(result.instruction->operands[0]);
662 STF_ASSERT_EQUAL(operand->type, FCML_OT_VIRTUAL);
663 STF_ASSERT_EQUAL(operand->decorators.er.is_not_null, FCML_TRUE);
664 STF_ASSERT_EQUAL(operand->decorators.er.value, FCML_ER_RU_SAE);
665 } else {
666 STF_FAIL();
667 }
668 fcml_fn_parser_result_free(&result);
669 }
670
671 void fcml_tf_parser_gas_parse_test_decorator_sae(void) {
672 fcml_st_parser_result result;
673 fcml_fn_parser_result_prepare(&result);
674 STF_ASSERT_EQUAL(
675 fcml_fn_parse(&internal_gas_context, "vaddpd {sae}",
676 &result), FCML_CEH_GEC_NO_ERROR);
677 if (result.instruction != NULL) {
678 fcml_st_operand *operand = &(result.instruction->operands[0]);
679 STF_ASSERT_EQUAL(operand->type, FCML_OT_VIRTUAL);
680 STF_ASSERT_EQUAL(operand->decorators.sae, FCML_TRUE);
681 } else {
682 STF_FAIL();
683 }
684 fcml_fn_parser_result_free(&result);
685 }
686
687 fcml_stf_test_case fcml_ti_parser_gas[] = {
688 { "fcml_tf_parser_gas_parse_test1", fcml_tf_parser_gas_parse_test1 },
689 { "fcml_tf_parser_gas_parse_test2", fcml_tf_parser_gas_parse_test2 },
690 { "fcml_tf_parser_gas_parse_test3", fcml_tf_parser_gas_parse_test3 },
691 { "fcml_tf_parser_gas_parse_test4", fcml_tf_parser_gas_parse_test4 },
692 { "fcml_tf_parser_gas_parse_test5", fcml_tf_parser_gas_parse_test5 },
693 { "fcml_tf_parser_gas_parse_test6", fcml_tf_parser_gas_parse_test6 },
694 { "fcml_tf_parser_gas_parse_test7", fcml_tf_parser_gas_parse_test7 },
695 { "fcml_tf_parser_gas_parse_test8", fcml_tf_parser_gas_parse_test8 },
696 { "fcml_tf_parser_gas_parse_test9", fcml_tf_parser_gas_parse_test9 },
697 { "fcml_tf_parser_gas_parse_test10", fcml_tf_parser_gas_parse_test10 },
698 { "fcml_tf_parser_gas_parse_test11", fcml_tf_parser_gas_parse_test11 },
699 { "fcml_tf_parser_gas_parse_test12", fcml_tf_parser_gas_parse_test12 },
700 { "fcml_tf_parser_gas_parse_test_symbols_1", fcml_tf_parser_gas_parse_test_symbols_1 },
701 { "fcml_tf_parser_gas_parse_test_symbols_2", fcml_tf_parser_gas_parse_test_symbols_2 },
702 { "fcml_tf_parser_gas_parse_test_symbols_3", fcml_tf_parser_gas_parse_test_symbols_3 },
703 { "fcml_tf_parser_gas_parse_test_symbols_4", fcml_tf_parser_gas_parse_test_symbols_4 },
704 { "fcml_tf_parser_gas_parse_test_symbols_5", fcml_tf_parser_gas_parse_test_symbols_5 },
705 { "fcml_tf_parser_gas_parse_test_symbols_6", fcml_tf_parser_gas_parse_test_symbols_6 },
706 { "fcml_tf_parser_gas_parse_test_pseudo_operation_1", fcml_tf_parser_gas_parse_test_pseudo_operation_1 },
707 { "fcml_tf_parser_gas_parse_test_pseudo_operation_2", fcml_tf_parser_gas_parse_test_pseudo_operation_2 },
708 { "fcml_tf_parser_gas_parse_test_decorator_bcast",
709 fcml_tf_parser_gas_parse_test_decorator_bcast },
710 { "fcml_tf_parser_gas_parse_test_decorator_opmask",
711 fcml_tf_parser_gas_parse_test_decorator_opmask },
712 { "fcml_tf_parser_gas_parse_test_decorator_opmask_k",
713 fcml_tf_parser_gas_parse_test_decorator_opmask_k },
714 { "fcml_tf_parser_gas_parse_test_decorator_z",
715 fcml_tf_parser_gas_parse_test_decorator_z },
716 { "fcml_tf_parser_gas_parse_test_decorator_opmask_k_z_for_addressing",
717 fcml_tf_parser_gas_parse_test_decorator_opmask_k_z_for_addressing },
718 { "fcml_tf_parser_gas_parse_test_decorator_er",
719 fcml_tf_parser_gas_parse_test_decorator_er },
720 { "fcml_tf_parser_gas_parse_test_decorator_sae",
721 fcml_tf_parser_gas_parse_test_decorator_sae },
722 FCML_STF_NULL_TEST
723 };
724
725 fcml_stf_test_suite fcml_si_gas_parser = {
726 "suite-fcml-parser-gas", fcml_tf_gas_parser_suite_init, fcml_tf_gas_parser_suite_cleanup, fcml_ti_parser_gas
727 };