| 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/decoder_internal.h" |
| 24 | 24 |
| 25 %%{ | 25 %%{ |
| 26 machine x86_32_decoder; | 26 machine x86_32_decoder; |
| 27 alphtype unsigned char; | 27 alphtype unsigned char; |
| 28 variable p current_position; | 28 variable p current_position; |
| 29 variable pe end_of_data; | 29 variable pe end_of_data; |
| 30 variable eof end_of_data; | 30 variable eof end_of_data; |
| 31 variable cs current_state; | 31 variable cs current_state; |
| 32 | 32 |
| 33 include byte_machine "byte_machines.rl"; | 33 include byte_machine "byte_machines.rl"; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 60 "native_client/src/trusted/validator_ragel/parse_instruction.rl"; | 60 "native_client/src/trusted/validator_ragel/parse_instruction.rl"; |
| 61 include cpuid_actions | 61 include cpuid_actions |
| 62 "native_client/src/trusted/validator_ragel/parse_instruction.rl"; | 62 "native_client/src/trusted/validator_ragel/parse_instruction.rl"; |
| 63 | 63 |
| 64 include decode_x86_32 "decoder_x86_32_instruction.rl"; | 64 include decode_x86_32 "decoder_x86_32_instruction.rl"; |
| 65 | 65 |
| 66 action end_of_instruction_cleanup { | 66 action end_of_instruction_cleanup { |
| 67 process_instruction(instruction_begin, current_position + 1, &instruction, | 67 process_instruction(instruction_begin, current_position + 1, &instruction, |
| 68 userdata); | 68 userdata); |
| 69 instruction_begin = current_position + 1; | 69 instruction_begin = current_position + 1; |
| 70 SET_DISP_TYPE(DISPNONE); | 70 SET_DISPLACEMENT_FORMAT(DISPNONE); |
| 71 SET_IMM_TYPE(IMMNONE); | 71 SET_IMMEDIATE_FORMAT(IMMNONE); |
| 72 SET_IMM2_TYPE(IMMNONE); | 72 SET_SECOND_IMMEDIATE_FORMAT(IMMNONE); |
| 73 SET_DATA16_PREFIX(FALSE); | 73 SET_DATA16_PREFIX(FALSE); |
| 74 SET_LOCK_PREFIX(FALSE); | 74 SET_LOCK_PREFIX(FALSE); |
| 75 SET_REPNZ_PREFIX(FALSE); | 75 SET_REPNZ_PREFIX(FALSE); |
| 76 SET_REPZ_PREFIX(FALSE); | 76 SET_REPZ_PREFIX(FALSE); |
| 77 SET_BRANCH_NOT_TAKEN(FALSE); | 77 SET_BRANCH_NOT_TAKEN(FALSE); |
| 78 SET_BRANCH_TAKEN(FALSE); | 78 SET_BRANCH_TAKEN(FALSE); |
| 79 SET_VEX_PREFIX3(0x00); | 79 SET_VEX_PREFIX3(0x00); |
| 80 SET_ATT_INSTRUCTION_SUFFIX(NULL); | 80 SET_ATT_INSTRUCTION_SUFFIX(NULL); |
| 81 } | 81 } |
| 82 | 82 |
| 83 action report_fatal_error { | 83 action report_fatal_error { |
| 84 process_error(current_position, userdata); | 84 process_error(current_position, userdata); |
| 85 result = FALSE; | 85 result = FALSE; |
| 86 goto error_detected; | 86 goto error_detected; |
| 87 } | 87 } |
| 88 | 88 |
| 89 decoder := (one_instruction @end_of_instruction_cleanup)* | 89 decoder := (one_instruction @end_of_instruction_cleanup)* |
| 90 $!report_fatal_error; | 90 $!report_fatal_error; |
| 91 }%% | 91 }%% |
| 92 | 92 |
| 93 /* |
| 94 * The "write data" statement causes Ragel to emit the constant static data |
| 95 * needed by the ragel machine. |
| 96 */ |
| 93 %% write data; | 97 %% write data; |
| 94 | 98 |
| 95 int DecodeChunkIA32(const uint8_t *data, size_t size, | 99 int DecodeChunkIA32(const uint8_t *data, size_t size, |
| 96 ProcessInstructionFunc process_instruction, | 100 ProcessInstructionFunc process_instruction, |
| 97 ProcessDecodingErrorFunc process_error, void *userdata) { | 101 ProcessDecodingErrorFunc process_error, void *userdata) { |
| 98 const uint8_t *current_position = data; | 102 const uint8_t *current_position = data; |
| 99 const uint8_t *end_of_data = data + size; | 103 const uint8_t *end_of_data = data + size; |
| 100 const uint8_t *instruction_begin = current_position; | 104 const uint8_t *instruction_begin = current_position; |
| 101 uint8_t vex_prefix3 = 0x00; | 105 uint8_t vex_prefix3 = 0x00; |
| 102 enum ImmediateMode imm_operand = IMMNONE; | 106 enum ImmediateMode imm_operand = IMMNONE; |
| 103 enum ImmediateMode imm2_operand = IMMNONE; | 107 enum ImmediateMode imm2_operand = IMMNONE; |
| 104 struct Instruction instruction; | 108 struct Instruction instruction; |
| 105 int result = TRUE; | 109 int result = TRUE; |
| 106 | 110 |
| 107 int current_state; | 111 int current_state; |
| 108 | 112 |
| 109 memset(&instruction, 0, sizeof instruction); | 113 memset(&instruction, 0, sizeof instruction); |
| 110 | 114 |
| 115 /* |
| 116 * The "write init" statement causes Ragel to emit initialization code. |
| 117 * This should be executed once before the ragel machine is started. |
| 118 */ |
| 111 %% write init; | 119 %% write init; |
| 120 /* |
| 121 * The "write exec" statement causes Ragel to emit the ragel machine's |
| 122 * execution code. |
| 123 */ |
| 112 %% write exec; | 124 %% write exec; |
| 113 | 125 |
| 114 error_detected: | 126 error_detected: |
| 115 return result; | 127 return result; |
| 116 } | 128 } |
| OLD | NEW |