2 * Copyright (c) 2011 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.
8 * Defines an instruction (decoder) iterator that processes code segments.
11 #include "native_client/src/trusted/validator/x86/decoder/nc_inst_iter.h"
16 #include "native_client/src/shared/platform/nacl_log.h"
17 #include "native_client/src/trusted/validator/x86/decoder/nc_inst_state.h"
18 #include "native_client/src/trusted/validator/x86/decoder/nc_inst_state_internal.h"
19 #include "native_client/src/trusted/validator/x86/decoder/nc_inst_trans.h"
20 #include "native_client/src/trusted/validator/x86/decoder/ncop_exps.h"
22 /* To turn on debugging of instruction decoding, change value of
27 #include "native_client/src/shared/utils/debugging.h"
29 #include "native_client/src/trusted/validator/x86/decoder/nc_inst_iter_inl.c"
31 static void NaClInstIterLogError(const char* error_message) {
32 NaClLog(LOG_ERROR, "*ERROR* %s\n", error_message);
35 void NaClInstIterFatal(const char* error_message) {
36 NaClInstIterLogError(error_message);
40 /* Default handler for errors found while parsing the memory segment.*/
41 static void NaClInstIterReportRemainingMemoryError(
42 NCRemainingMemoryError error,
43 struct NCRemainingMemory* memory) {
44 NaClInstIterLogError(NCRemainingMemoryErrorMessage(error));
47 NaClInstIter* NaClInstIterCreateWithLookback(
48 const struct NaClDecodeTables* decoder_tables,
50 size_t lookback_size) {
52 /* Guarantee we don't wrap around while computing buffer index updates. */
53 assert(((lookback_size + 1) * 2 + 1) > lookback_size);
54 iter = (NaClInstIter*) malloc(sizeof(NaClInstIter));
56 iter->segment = segment;
57 NCRemainingMemoryInit(segment->mbase, segment->size, &iter->memory);
58 iter->memory.error_fn = NaClInstIterReportRemainingMemoryError;
61 iter->buffer_size = lookback_size + 1;
62 iter->buffer_index = 0;
63 iter->buffer = (NaClInstState*)
64 calloc(iter->buffer_size, sizeof iter->buffer[0]);
65 if (NULL == iter->buffer) {
70 for (i = 0; i < iter->buffer_size; ++i) {
71 iter->buffer[i].inst = NULL;
72 NCInstBytesInitMemory(&iter->buffer[i].bytes, &iter->memory);
76 iter->decoder_tables = (struct NaClDecodeTables*) decoder_tables;
80 NaClInstIter* NaClInstIterCreate(
81 const struct NaClDecodeTables* decoder_tables,
82 NaClSegment* segment) {
83 return NaClInstIterCreateWithLookback(decoder_tables, segment, 0);
86 void NaClInstIterDestroy(NaClInstIter* iter) {
93 NaClInstState* NaClInstIterGetUndecodedState(NaClInstIter* iter) {
94 return NaClInstIterGetUndecodedStateInline(iter);
97 NaClInstState* NaClInstIterGetState(NaClInstIter* iter) {
98 return NaClInstIterGetStateInline(iter);
101 Bool NaClInstIterHasLookbackState(NaClInstIter* iter, size_t distance) {
102 return NaClInstIterHasLookbackStateInline(iter, distance);
105 NaClInstState* NaClInstIterGetLookbackState(NaClInstIter* iter,
107 return NaClInstIterGetLookbackStateInline(iter, distance);
110 Bool NaClInstIterHasNext(NaClInstIter* iter) {
111 return NaClInstIterHasNextInline(iter);
114 void NaClInstIterAdvance(NaClInstIter* iter) {
115 NaClInstIterAdvanceInline(iter);
118 uint8_t* NaClInstIterGetInstMemory(NaClInstIter* iter) {
119 return NaClInstIterGetInstMemoryInline(iter);