| 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 /* Implement the ApplyValidator API for the x86-32 architecture. */ | 7 /* Implement the ApplyValidator API for the x86-32 architecture. */ |
| 8 | 8 |
| 9 #include "native_client/src/trusted/validator/ncvalidate.h" | 9 #include "native_client/src/trusted/validator/ncvalidate.h" |
| 10 #include "native_client/src/trusted/validator/validation_cache.h" | 10 #include "native_client/src/trusted/validator/validation_cache.h" |
| 11 #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncvalidate.h" | 11 #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncvalidate.h" |
| 12 #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncvalidate_detai
led.h" | 12 #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncvalidate_detai
led.h" |
| 13 /* HACK to get access to didstubout */ | 13 /* HACK to get access to didstubout */ |
| 14 #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncvalidate_inter
naltypes.h" | 14 #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncvalidate_inter
naltypes.h" |
| 15 #include <assert.h> | 15 #include <assert.h> |
| 16 | 16 |
| 17 /* Be sure the correct compile flags are defined for this. */ | 17 /* Be sure the correct compile flags are defined for this. */ |
| 18 #if NACL_ARCH(NACL_TARGET_ARCH) != NACL_x86 | 18 #if NACL_ARCH(NACL_TARGET_ARCH) != NACL_x86 |
| 19 # error("Can't compile, target is for x86-32") | 19 # error("Can't compile, target is for x86-32") |
| 20 #else | 20 #else |
| 21 # if NACL_TARGET_SUBARCH != 32 | 21 # if NACL_TARGET_SUBARCH != 32 |
| 22 # error("Can't compile, target is for x86-32") | 22 # error("Can't compile, target is for x86-32") |
| 23 # endif | 23 # endif |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 NaClValidationStatus NACL_SUBARCH_NAME(ApplyValidator, NACL_TARGET_ARCH, 32) ( | 26 static NaClValidationStatus ApplyValidator_x86_32( |
| 27 uintptr_t guest_addr, | 27 uintptr_t guest_addr, |
| 28 uint8_t *data, | 28 uint8_t *data, |
| 29 size_t size, | 29 size_t size, |
| 30 int stubout_mode, | 30 int stubout_mode, |
| 31 int readonly_text, | 31 int readonly_text, |
| 32 const NaClCPUFeaturesX86 *cpu_features, | 32 const NaClCPUFeaturesX86 *cpu_features, |
| 33 struct NaClValidationCache *cache) { | 33 struct NaClValidationCache *cache) { |
| 34 struct NCValidatorState *vstate; | 34 struct NCValidatorState *vstate; |
| 35 int validator_result = 0; | 35 int validator_result = 0; |
| 36 void *query = NULL; | 36 void *query = NULL; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 if (validator_result == 0 && !NCValidatorDidStubOut(vstate)) | 83 if (validator_result == 0 && !NCValidatorDidStubOut(vstate)) |
| 84 cache->SetKnownToValidate(query); | 84 cache->SetKnownToValidate(query); |
| 85 cache->DestroyQuery(query); | 85 cache->DestroyQuery(query); |
| 86 } | 86 } |
| 87 | 87 |
| 88 NCValidateFreeState(&vstate); | 88 NCValidateFreeState(&vstate); |
| 89 return (validator_result == 0 || stubout_mode) | 89 return (validator_result == 0 || stubout_mode) |
| 90 ? NaClValidationSucceeded : NaClValidationFailed; | 90 ? NaClValidationSucceeded : NaClValidationFailed; |
| 91 } | 91 } |
| 92 | 92 |
| 93 NaClValidationStatus NACL_SUBARCH_NAME(ApplyValidatorCodeReplacement, x86, 32) | 93 static NaClValidationStatus ApplyValidatorCodeReplacement_x86_32( |
| 94 (uintptr_t guest_addr, | 94 uintptr_t guest_addr, |
| 95 uint8_t *data_old, | 95 uint8_t *data_old, |
| 96 uint8_t *data_new, | 96 uint8_t *data_new, |
| 97 size_t size, | 97 size_t size, |
| 98 const NaClCPUFeaturesX86 *cpu_features) { | 98 const NaClCPUFeaturesX86 *cpu_features) { |
| 99 /* Check that the given parameter values are supported. */ | 99 /* Check that the given parameter values are supported. */ |
| 100 if (!NaClArchSupported(cpu_features)) | 100 if (!NaClArchSupported(cpu_features)) |
| 101 return NaClValidationFailedCpuNotSupported; | 101 return NaClValidationFailedCpuNotSupported; |
| 102 | 102 |
| 103 return NCValidateSegmentPair(data_old, data_new, guest_addr, | 103 return NCValidateSegmentPair(data_old, data_new, guest_addr, |
| 104 size, cpu_features) | 104 size, cpu_features) |
| 105 ? NaClValidationSucceeded : NaClValidationFailed; | 105 ? NaClValidationSucceeded : NaClValidationFailed; |
| 106 } | 106 } |
| 107 |
| 108 /* Copy a single instruction, avoiding the possibility of other threads |
| 109 * executing a partially changed instruction. |
| 110 */ |
| 111 static Bool CopyInstruction(NCDecoderStatePair* tthis, |
| 112 NCDecoderInst *dinst_old, |
| 113 NCDecoderInst *dinst_new) { |
| 114 NCRemainingMemory* mem_old = &dinst_old->dstate->memory; |
| 115 NCRemainingMemory* mem_new = &dinst_new->dstate->memory; |
| 116 |
| 117 return tthis->copy_func(mem_old->mpc, mem_new->mpc, mem_old->read_length); |
| 118 } |
| 119 |
| 120 /* Copies code from src to dest in a thread safe way, returns 1 on success, |
| 121 * returns 0 on error. This will likely assert on error to avoid partially |
| 122 * copied code or undefined state. |
| 123 */ |
| 124 static int NCCopyCode(uint8_t *dst, uint8_t *src, NaClPcAddress vbase, |
| 125 size_t sz, NaClCopyInstructionFunc copy_func) { |
| 126 NCDecoderState dst_dstate; |
| 127 NCDecoderInst dst_inst; |
| 128 NCDecoderState src_dstate; |
| 129 NCDecoderInst src_inst; |
| 130 NCDecoderStatePair pair; |
| 131 int result = 0; |
| 132 |
| 133 NCDecoderStateConstruct(&dst_dstate, dst, vbase, sz, &dst_inst, 1); |
| 134 NCDecoderStateConstruct(&src_dstate, src, vbase, sz, &src_inst, 1); |
| 135 NCDecoderStatePairConstruct(&pair, &dst_dstate, &src_dstate, copy_func); |
| 136 pair.action_fn = CopyInstruction; |
| 137 if (NCDecoderStatePairDecode(&pair)) result = 1; |
| 138 NCDecoderStatePairDestruct(&pair); |
| 139 NCDecoderStateDestruct(&src_dstate); |
| 140 NCDecoderStateDestruct(&dst_dstate); |
| 141 |
| 142 return result; |
| 143 } |
| 144 |
| 145 static NaClValidationStatus ApplyValidatorCopy_x86_32( |
| 146 uintptr_t guest_addr, |
| 147 uint8_t *data_old, |
| 148 uint8_t *data_new, |
| 149 size_t size, |
| 150 const NaClCPUFeaturesX86 *cpu_features, |
| 151 NaClCopyInstructionFunc copy_func) { |
| 152 if (!NaClArchSupported(cpu_features)) |
| 153 return NaClValidationFailedCpuNotSupported; |
| 154 |
| 155 return ((0 == NCCopyCode(data_old, data_new, guest_addr, size, copy_func)) |
| 156 ? NaClValidationFailed : NaClValidationSucceeded); |
| 157 } |
| 158 |
| 159 static const struct NaClValidatorInterface validator = { |
| 160 ApplyValidator_x86_32, |
| 161 ApplyValidatorCopy_x86_32, |
| 162 ApplyValidatorCodeReplacement_x86_32, |
| 163 }; |
| 164 |
| 165 const struct NaClValidatorInterface *NaClValidatorCreate_x86_32() { |
| 166 return &validator; |
| 167 } |
| OLD | NEW |