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 #ifndef NACL_TRUSTED_BUT_NOT_TCB | |
8 #error("This file is not meant for use in the TCB") | |
9 #endif | |
10 | |
11 #include "native_client/src/trusted/validator_x86/ncenuminsts.h" | |
12 | |
13 #include <string.h> | |
14 | |
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_inter
naltypes.h" | |
21 #include "native_client/src/trusted/validator/x86/nc_segment.h" | |
22 | |
23 /* To turn on debugging of instruction decoding, change value of | |
24 * DEBUGGING to 1. | |
25 */ | |
26 #define DEBUGGING 0 | |
27 | |
28 #include "native_client/src/shared/utils/debugging.h" | |
29 | |
30 /* Extracts parsed instruction from instruction in API NaClInstStruct. */ | |
31 #define NACL_INST(s) (&(s)->inst_buffer[(s)->cur_inst_index]) | |
32 | |
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. | |
39 */ | |
40 static NCDecoderInst dinst; | |
41 static NCValidatorState vstate; | |
42 | |
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; | |
48 } | |
49 | |
50 uint8_t NaClInstLength(NaClInstStruct *inst) { | |
51 return NACL_INST(inst)->inst.bytes.length; | |
52 } | |
53 | |
54 char *NaClInstToStr(NaClInstStruct *inst) { | |
55 return NCInstWithHexToString(NACL_INST(inst)); | |
56 } | |
57 | |
58 /* Defines a buffer size big enough to hold an instruction. */ | |
59 #define MAX_INST_TEXT_SIZE 256 | |
60 | |
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. | |
64 */ | |
65 static const char* unknown_name = "???"; | |
66 static char buffer[MAX_INST_TEXT_SIZE]; | |
67 char* str; | |
68 char* op; | |
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'; | |
76 free((void*) str); | |
77 return buffer; | |
78 } | |
79 | |
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 || | |
88 itype == NACLi_VMX); | |
89 } | |
90 | |
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)); | |
96 } | |
97 | |
98 Bool NaClInstValidates(uint8_t* mbase, | |
99 uint8_t size, | |
100 NaClPcAddress vbase, | |
101 NaClInstStruct* inst) { | |
102 NCDecoderInst* dinst = NACL_INST(inst); | |
103 | |
104 return (NaClInstDecodesCorrectly(inst) && | |
105 UnsafePartialValidateInst(dinst) && | |
106 NaClInstTypeLegal(dinst->opinfo->insttype)); | |
107 } | |
108 | |
109 Bool NaClSegmentValidates(uint8_t* mbase, | |
110 size_t size, | |
111 NaClPcAddress vbase) { | |
112 NaClCPUFeaturesX86 cpu_features; | |
113 NaClValidationStatus status; | |
114 /* TODO(pasko): Validator initialization can be slow, make it run only once. | |
115 */ | |
116 const struct NaClValidatorInterface *validator = NaClCreateValidator(); | |
117 | |
118 /* check if NaCl thinks the given code segment is valid. */ | |
119 NaClSetAllCPUFeaturesX86((NaClCPUFeatures *) &cpu_features); | |
120 status = validator->Validate( | |
121 vbase, mbase, size, | |
122 /* stubout_mode= */ FALSE, /* readonly_text= */ FALSE, | |
123 (NaClCPUFeatures *) &cpu_features, | |
124 NULL, NULL); | |
125 switch (status) { | |
126 case NaClValidationSucceeded: | |
127 return TRUE; | |
128 default: | |
129 return FALSE; | |
130 } | |
131 } | |
OLD | NEW |