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.
7 #ifndef NACL_TRUSTED_BUT_NOT_TCB
8 #error("This file is not meant for use in the TCB")
11 #include "native_client/src/trusted/validator_x86/ncenuminsts.h"
15 #include "native_client/src/shared/platform/nacl_log.h"
16 #include "native_client/src/trusted/validator/ncvalidate.h"
17 #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncdecode.h"
18 #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncdecode_aux.h"
19 #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncdecode_verbose.h"
20 #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncvalidate_internaltypes.h"
21 #include "native_client/src/trusted/validator/x86/nc_segment.h"
23 /* To turn on debugging of instruction decoding, change value of
28 #include "native_client/src/shared/utils/debugging.h"
30 /* Extracts parsed instruction from instruction in API NaClInstStruct. */
31 #define NACL_INST(s) (&(s)->inst_buffer[(s)->cur_inst_index])
33 NaClInstStruct *NaClParseInst(uint8_t* ibytes, size_t isize,
34 const NaClPcAddress vbase) {
35 /* WARNING: This version of the code uses a global to return the
36 * decoded instruction, forcing the use to be in a single thread.
37 * The following two (static) locals are used to hold the decoded
38 * instruction until the next call to the function.
40 static NCDecoderInst dinst;
41 static NCValidatorState vstate;
43 /* Hand coded to only recognize a single instruction!. */
44 NCDecoderStateConstruct(&vstate.dstate, ibytes, vbase, isize, &dinst, 1);
45 NCDecoderStateNewSegment(&vstate.dstate);
46 NCConsumeNextInstruction(&dinst);
47 return &vstate.dstate;
50 uint8_t NaClInstLength(NaClInstStruct *inst) {
51 return NACL_INST(inst)->inst.bytes.length;
54 char *NaClInstToStr(NaClInstStruct *inst) {
55 return NCInstWithHexToString(NACL_INST(inst));
58 /* Defines a buffer size big enough to hold an instruction. */
59 #define MAX_INST_TEXT_SIZE 256
61 const char *NaClOpcodeName(NaClInstStruct *inst) {
62 /* WARNING: This version of the code uses a global to return the
63 * generated string, forcing the use to be in a single thread.
65 static const char* unknown_name = "???";
66 static char buffer[MAX_INST_TEXT_SIZE];
69 str = NCInstWithoutHexToString(NACL_INST(inst));
70 if (str == NULL) return unknown_name;
71 op = strtok(str, " \t\n");
72 if (op == NULL) return unknown_name;
73 /* Force op length to fit into buffer, and null terminate. */
74 strncpy(buffer, op, MAX_INST_TEXT_SIZE);
75 op[MAX_INST_TEXT_SIZE - 1] = '\0';
80 static Bool NaClInstTypeLegal(NaClInstType itype) {
81 return !(itype == NACLi_UNDEFINED ||
82 itype == NACLi_ILLEGAL ||
83 itype == NACLi_INVALID ||
84 itype == NACLi_SYSTEM ||
85 itype == NACLi_RETURN ||
86 itype == NACLi_SYSCALL ||
87 itype == NACLi_SYSENTER ||
91 Bool NaClInstDecodesCorrectly(NaClInstStruct *inst) {
92 NCDecoderInst* dinst = NACL_INST(inst);
93 return ((dinst->inst_addr < inst->size) &&
94 (0 == inst->memory.overflow_count) &&
95 NaClInstTypeLegal(dinst->opinfo->insttype));
98 Bool NaClInstValidates(uint8_t* mbase,
101 NaClInstStruct* inst) {
102 NCDecoderInst* dinst = NACL_INST(inst);
104 return (NaClInstDecodesCorrectly(inst) &&
105 UnsafePartialValidateInst(dinst) &&
106 NaClInstTypeLegal(dinst->opinfo->insttype));
109 Bool NaClSegmentValidates(uint8_t* mbase,
111 NaClPcAddress vbase) {
112 NaClCPUFeaturesX86 cpu_features;
113 NaClValidationStatus status;
114 /* TODO(pasko): Validator initialization can be slow, make it run only once.
116 const struct NaClValidatorInterface *validator = NaClCreateValidator();
118 /* check if NaCl thinks the given code segment is valid. */
119 NaClSetAllCPUFeaturesX86((NaClCPUFeatures *) &cpu_features);
120 status = validator->Validate(
122 /* stubout_mode= */ FALSE, /* readonly_text= */ FALSE,
123 (NaClCPUFeatures *) &cpu_features,
126 case NaClValidationSucceeded: