| OLD | NEW |
| (Empty) |
| 1 /* | |
| 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 | |
| 4 * found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 /* | |
| 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. | |
| 10 * | |
| 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 | |
| 13 * of the glue code. | |
| 14 */ | |
| 15 | |
| 16 #include <assert.h> | |
| 17 #include <stddef.h> | |
| 18 #include <stdio.h> | |
| 19 #include <stdlib.h> | |
| 20 #include <string.h> | |
| 21 | |
| 22 #include "native_client/src/shared/utils/types.h" | |
| 23 #include "native_client/src/trusted/validator_ragel/unreviewed/decoder_internal.
h" | |
| 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 %%{ | |
| 41 machine x86_32_decoder; | |
| 42 alphtype unsigned char; | |
| 43 variable p current_position; | |
| 44 variable pe end_of_data; | |
| 45 variable eof end_of_data; | |
| 46 variable cs current_state; | |
| 47 | |
| 48 include byte_machine "byte_machines.rl"; | |
| 49 | |
| 50 include prefix_actions | |
| 51 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 52 include prefixes_parsing | |
| 53 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 54 include vex_actions_ia32 | |
| 55 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 56 include vex_parsing_ia32 | |
| 57 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 58 include att_suffix_actions | |
| 59 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 60 include set_spurious_prefixes | |
| 61 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 62 include displacement_fields_actions | |
| 63 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 64 include displacement_fields_parsing | |
| 65 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 66 include modrm_actions_ia32 | |
| 67 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 68 include modrm_parsing_ia32 | |
| 69 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 70 include operand_actions_ia32 | |
| 71 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 72 include immediate_fields_actions | |
| 73 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 74 include immediate_fields_parsing_ia32 | |
| 75 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 76 include relative_fields_decoder_actions | |
| 77 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 78 include relative_fields_parsing | |
| 79 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 80 include cpuid_actions | |
| 81 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 82 | |
| 83 include decode_x86_32 "decoder_x86_32_instruction.rl"; | |
| 84 | |
| 85 include decoder | |
| 86 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 87 | |
| 88 main := decoder; | |
| 89 }%% | |
| 90 | |
| 91 %% write data; | |
| 92 | |
| 93 int DecodeChunkIA32(const uint8_t *data, size_t size, | |
| 94 ProcessInstructionFunc process_instruction, | |
| 95 ProcessDecodingErrorFunc process_error, void *userdata) { | |
| 96 const uint8_t *current_position = data; | |
| 97 const uint8_t *end_of_data = data + size; | |
| 98 const uint8_t *instruction_begin = current_position; | |
| 99 uint8_t vex_prefix3 = 0x00; | |
| 100 enum ImmediateMode imm_operand = IMMNONE; | |
| 101 enum ImmediateMode imm2_operand = IMMNONE; | |
| 102 struct Instruction instruction; | |
| 103 int result = TRUE; | |
| 104 | |
| 105 int current_state; | |
| 106 | |
| 107 /* Not used in ia32_mode. */ | |
| 108 instruction.prefix.rex = 0; | |
| 109 | |
| 110 SET_DISP_TYPE(DISPNONE); | |
| 111 SET_IMM_TYPE(IMMNONE); | |
| 112 SET_IMM2_TYPE(IMMNONE); | |
| 113 SET_DATA16_PREFIX(FALSE); | |
| 114 SET_LOCK_PREFIX(FALSE); | |
| 115 SET_REPNZ_PREFIX(FALSE); | |
| 116 SET_REPZ_PREFIX(FALSE); | |
| 117 SET_BRANCH_NOT_TAKEN(FALSE); | |
| 118 SET_BRANCH_TAKEN(FALSE); | |
| 119 SET_ATT_INSTRUCTION_SUFFIX(NULL); | |
| 120 instruction.prefix.rex_b_spurious = FALSE; | |
| 121 instruction.prefix.rex_x_spurious = FALSE; | |
| 122 instruction.prefix.rex_r_spurious = FALSE; | |
| 123 instruction.prefix.rex_w_spurious = FALSE; | |
| 124 | |
| 125 %% write init; | |
| 126 %% write exec; | |
| 127 | |
| 128 error_detected: | |
| 129 return result; | |
| 130 } | |
| OLD | NEW |