Debian Codegraph @

check/public-tests/error_handling_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 "error_handling_t.h"
21
22 #include <string.h>
23
24 #include "instructions_base_t.h"
25
26 #include <fcml_assembler.h>
27 #include <fcml_parser.h>
28
29 /* Helper methods */
30
31 fcml_st_assembler_context fcml_ifn_prepare_context( fcml_en_operating_mode op_mode ) {
32 fcml_st_assembler_context context = {0};
33 context.assembler = assembler_intel;
34 context.entry_point.op_mode = op_mode;
35 context.entry_point.ip = 0x00401000;
36 context.configuration.enable_error_messages = FCML_TRUE;
37 return context;
38 }
39
40 fcml_bool fcml_check_error_message( fcml_st_ceh_error_container errors, fcml_string message, fcml_ceh_error error_code ) {
41 fcml_bool result = FCML_FALSE;
42 fcml_st_ceh_error_info *error = errors.errors;
43 while( error ) {
44 if( error->code == error_code && strcmp( error->message, message ) == 0 ) {
45 result = FCML_TRUE;
46 break;
47 }
48 error = error->next_error;
49 }
50 return result;
51 }
52
53 fcml_bool fcml_tf_check_errors( fcml_string mnemonic, fcml_string message, fcml_ceh_error message_error_code, fcml_ceh_error error_code ) {
54
55 fcml_bool result = FCML_TRUE;
56
57 fcml_st_assembler_context context = fcml_ifn_prepare_context( FCML_OM_32_BIT );
58
59 fcml_st_parser_result parsed_instruction;
60
61 fcml_fn_parser_result_prepare( &parsed_instruction );
62
63 fcml_st_assembler_result assembler_result;
64
65 fcml_fn_assembler_result_prepare( &assembler_result );
66
67 fcml_st_parser_context parser_context = {0};
68 parser_context.dialect = dialect_intel;
69
70 while( FCML_TRUE ) {
71
72 if( fcml_fn_parse( &parser_context, mnemonic, &parsed_instruction ) ) {
73 STF_FAIL("Parsing failed.");
74 break;
75 }
76
77 fcml_ceh_error error = fcml_fn_assemble( &context, parsed_instruction.instruction, &assembler_result );
78
79 if( error != error_code ) {
80 result = FCML_FALSE;
81 } else {
82 result = fcml_check_error_message( assembler_result.errors, message, message_error_code );
83 }
84
85 break;
86 }
87
88 fcml_fn_parser_result_free( &parsed_instruction );
89
90 fcml_fn_assembler_result_free( &assembler_result );
91
92 fcml_fn_symbol_table_free( parser_context.symbol_table );
93
94 return result;
95 }
96
97 /* Test suite initialization. */
98
99 fcml_bool fcml_tf_error_handling_suite_init(void) {
100 return FCML_TRUE;
101 }
102
103 fcml_bool fcml_tf_error_handling_suite_cleanup(void) {
104 return FCML_TRUE;
105 }
106
107 /* Tests. */
108
109 void fcml_tf_error_handling_explicit_reg_addressing(void) {
110 STF_ASSERT( fcml_tf_check_errors( "repne cmps dword ptr [si],byte ptr cs:[di]", FCML_TEXT("Invalid operand size."), FCML_CEH_GEC_INVALID_OPPERAND_SIZE, FCML_CEH_GEC_INVALID_OPPERAND_SIZE ) );
111 STF_ASSERT( fcml_tf_check_errors( "repne cmps byte ptr [si],byte ptr cs:[di]", FCML_TEXT("Segment register can not be overridden."), FCML_CEH_MEC_ERROR_ILLEGAL_SEG_REG_OVERRIDE, FCML_CEH_GEC_INVALID_REGISTER ) );
112 STF_ASSERT( fcml_tf_check_errors( "repne cmps byte ptr [si],byte ptr [edi]", FCML_TEXT("Invalid address size."), FCML_CEH_GEC_INVALID_ADDRESS_SIZE, FCML_CEH_GEC_INVALID_ADDRESS_SIZE ) );
113 }
114
115 fcml_stf_test_case fcml_ti_error_handling[] = {
116 { "fcml_tf_error_handling_explicit_reg_addressing", fcml_tf_error_handling_explicit_reg_addressing },
117 FCML_STF_NULL_TEST
118 };
119
120 fcml_stf_test_suite fcml_si_error_handling = {
121 "suite-fcml-error-handling", fcml_tf_error_handling_suite_init, fcml_tf_error_handling_suite_cleanup, fcml_ti_error_handling
122 };