| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * Full-blown decoder for ia32 case. Can be used to decode instruction sequence | 8 * Full-blown decoder for ia32 case. Can be used to decode instruction sequence |
| 9 * and process it, but right now is only used in tests. | 9 * and process it, but right now is only used in tests. |
| 10 * | 10 * |
| 11 * The code is in [hand-written] "parse_instruction.rl" and in [auto-generated] | 11 * The code is in [hand-written] "parse_instruction.rl" and in [auto-generated] |
| 12 * "decoder_x86_32_instruction.rl" file. This file only includes tiny amount | 12 * "decoder_x86_32_instruction.rl" file. This file only includes tiny amount |
| 13 * of the glue code. | 13 * of the glue code. |
| 14 */ | 14 */ |
| 15 | 15 |
| 16 #include <assert.h> | 16 #include <assert.h> |
| 17 #include <stddef.h> | 17 #include <stddef.h> |
| 18 #include <stdio.h> | 18 #include <stdio.h> |
| 19 #include <stdlib.h> | 19 #include <stdlib.h> |
| 20 #include <string.h> | 20 #include <string.h> |
| 21 | 21 |
| 22 #include "native_client/src/shared/utils/types.h" | 22 #include "native_client/src/shared/utils/types.h" |
| 23 #include "native_client/src/trusted/validator_ragel/unreviewed/decoder_internal.
h" | 23 #include "native_client/src/trusted/validator_ragel/unreviewed/decoder_internal.
h" |
| 24 | 24 |
| 25 /* | |
| 26 * These prefixes are not useful in IA32 mode, but they will "cleaned up" by | |
| 27 * decoder's cleanup procedure anyway. Do nothing when that happens. | |
| 28 */ | |
| 29 #define SET_REX_PREFIX(P) | |
| 30 #define SET_VEX_PREFIX2(P) | |
| 31 #define CLEAR_SPURIOUS_REX_B() | |
| 32 #define SET_SPURIOUS_REX_B() | |
| 33 #define CLEAR_SPURIOUS_REX_X() | |
| 34 #define SET_SPURIOUS_REX_X() | |
| 35 #define CLEAR_SPURIOUS_REX_R() | |
| 36 #define SET_SPURIOUS_REX_R() | |
| 37 #define CLEAR_SPURIOUS_REX_W() | |
| 38 #define SET_SPURIOUS_REX_W() | |
| 39 | |
| 40 %%{ | 25 %%{ |
| 41 machine x86_32_decoder; | 26 machine x86_32_decoder; |
| 42 alphtype unsigned char; | 27 alphtype unsigned char; |
| 43 variable p current_position; | 28 variable p current_position; |
| 44 variable pe end_of_data; | 29 variable pe end_of_data; |
| 45 variable eof end_of_data; | 30 variable eof end_of_data; |
| 46 variable cs current_state; | 31 variable cs current_state; |
| 47 | 32 |
| 48 include byte_machine "byte_machines.rl"; | 33 include byte_machine "byte_machines.rl"; |
| 49 | 34 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 73 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 58 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
| 74 include relative_fields_decoder_actions | 59 include relative_fields_decoder_actions |
| 75 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 60 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
| 76 include relative_fields_parsing | 61 include relative_fields_parsing |
| 77 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 62 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
| 78 include cpuid_actions | 63 include cpuid_actions |
| 79 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 64 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
| 80 | 65 |
| 81 include decode_x86_32 "decoder_x86_32_instruction.rl"; | 66 include decode_x86_32 "decoder_x86_32_instruction.rl"; |
| 82 | 67 |
| 83 include decoder | 68 action end_of_instruction_cleanup { |
| 84 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 69 process_instruction(instruction_begin, current_position + 1, &instruction, |
| 70 userdata); |
| 71 instruction_begin = current_position + 1; |
| 72 SET_DISP_TYPE(DISPNONE); |
| 73 SET_IMM_TYPE(IMMNONE); |
| 74 SET_IMM2_TYPE(IMMNONE); |
| 75 SET_DATA16_PREFIX(FALSE); |
| 76 SET_LOCK_PREFIX(FALSE); |
| 77 SET_REPNZ_PREFIX(FALSE); |
| 78 SET_REPZ_PREFIX(FALSE); |
| 79 SET_BRANCH_NOT_TAKEN(FALSE); |
| 80 SET_BRANCH_TAKEN(FALSE); |
| 81 SET_VEX_PREFIX3(0x00); |
| 82 SET_ATT_INSTRUCTION_SUFFIX(NULL); |
| 83 } |
| 85 | 84 |
| 86 main := decoder; | 85 action report_fatal_error { |
| 86 process_error(current_position, userdata); |
| 87 result = FALSE; |
| 88 goto error_detected; |
| 89 } |
| 90 |
| 91 decoder := (one_instruction @end_of_instruction_cleanup)* |
| 92 $!report_fatal_error; |
| 87 }%% | 93 }%% |
| 88 | 94 |
| 89 %% write data; | 95 %% write data; |
| 90 | 96 |
| 91 int DecodeChunkIA32(const uint8_t *data, size_t size, | 97 int DecodeChunkIA32(const uint8_t *data, size_t size, |
| 92 ProcessInstructionFunc process_instruction, | 98 ProcessInstructionFunc process_instruction, |
| 93 ProcessDecodingErrorFunc process_error, void *userdata) { | 99 ProcessDecodingErrorFunc process_error, void *userdata) { |
| 94 const uint8_t *current_position = data; | 100 const uint8_t *current_position = data; |
| 95 const uint8_t *end_of_data = data + size; | 101 const uint8_t *end_of_data = data + size; |
| 96 const uint8_t *instruction_begin = current_position; | 102 const uint8_t *instruction_begin = current_position; |
| 97 uint8_t vex_prefix3 = 0x00; | 103 uint8_t vex_prefix3 = 0x00; |
| 98 enum ImmediateMode imm_operand = IMMNONE; | 104 enum ImmediateMode imm_operand = IMMNONE; |
| 99 enum ImmediateMode imm2_operand = IMMNONE; | 105 enum ImmediateMode imm2_operand = IMMNONE; |
| 100 struct Instruction instruction; | 106 struct Instruction instruction; |
| 101 int result = TRUE; | 107 int result = TRUE; |
| 102 | 108 |
| 103 int current_state; | 109 int current_state; |
| 104 | 110 |
| 105 /* Not used in ia32_mode. */ | 111 memset(&instruction, 0, sizeof instruction); |
| 106 instruction.prefix.rex = 0; | |
| 107 | |
| 108 SET_DISP_TYPE(DISPNONE); | |
| 109 SET_IMM_TYPE(IMMNONE); | |
| 110 SET_IMM2_TYPE(IMMNONE); | |
| 111 SET_DATA16_PREFIX(FALSE); | |
| 112 SET_LOCK_PREFIX(FALSE); | |
| 113 SET_REPNZ_PREFIX(FALSE); | |
| 114 SET_REPZ_PREFIX(FALSE); | |
| 115 SET_BRANCH_NOT_TAKEN(FALSE); | |
| 116 SET_BRANCH_TAKEN(FALSE); | |
| 117 SET_ATT_INSTRUCTION_SUFFIX(NULL); | |
| 118 instruction.prefix.rex_b_spurious = FALSE; | |
| 119 instruction.prefix.rex_x_spurious = FALSE; | |
| 120 instruction.prefix.rex_r_spurious = FALSE; | |
| 121 instruction.prefix.rex_w_spurious = FALSE; | |
| 122 | 112 |
| 123 %% write init; | 113 %% write init; |
| 124 %% write exec; | 114 %% write exec; |
| 125 | 115 |
| 126 error_detected: | 116 error_detected: |
| 127 return result; | 117 return result; |
| 128 } | 118 } |
| OLD | NEW |