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-64 architecture. */ | 7 /* Implement the ApplyValidator API for the x86-64 architecture. */ |
8 #include <assert.h> | 8 #include <assert.h> |
| 9 #include "native_client/src/shared/platform/nacl_log.h" |
9 #include "native_client/src/trusted/validator/ncvalidate.h" | 10 #include "native_client/src/trusted/validator/ncvalidate.h" |
10 | 11 #include "native_client/src/trusted/validator/validation_cache.h" |
11 #include "native_client/src/shared/platform/nacl_log.h" | |
12 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/ncvalidate_iter.
h" | 12 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/ncvalidate_iter.
h" |
13 | 13 |
14 /* Be sure the correct compile flags are defined for this. */ | 14 /* Be sure the correct compile flags are defined for this. */ |
15 #if NACL_ARCH(NACL_TARGET_ARCH) != NACL_x86 | 15 #if NACL_ARCH(NACL_TARGET_ARCH) != NACL_x86 |
16 # error("Can't compile, target is for x86-64") | 16 # error("Can't compile, target is for x86-64") |
17 #else | 17 #else |
18 # if NACL_TARGET_SUBARCH != 64 | 18 # if NACL_TARGET_SUBARCH != 64 |
19 # error("Can't compile, target is for x86-64") | 19 # error("Can't compile, target is for x86-64") |
20 # endif | 20 # endif |
21 #endif | 21 #endif |
22 | 22 |
23 NaClValidationStatus NaClValidatorSetup_x86_64( | 23 NaClValidationStatus NaClValidatorSetup_x86_64( |
24 uintptr_t guest_addr, | 24 uintptr_t guest_addr, |
25 size_t size, | 25 size_t size, |
26 int bundle_size, | 26 int bundle_size, |
27 NaClCPUFeaturesX86 *cpu_features, | 27 NaClCPUFeaturesX86 *cpu_features, |
28 struct NaClValidatorState** vstate_ptr) { | 28 struct NaClValidatorState** vstate_ptr) { |
29 *vstate_ptr = NaClValidatorStateCreate(guest_addr, size, bundle_size, RegR15, | 29 *vstate_ptr = NaClValidatorStateCreate(guest_addr, size, bundle_size, RegR15, |
30 cpu_features); | 30 cpu_features); |
31 return (*vstate_ptr == NULL) | 31 return (*vstate_ptr == NULL) |
32 ? NaClValidationFailedOutOfMemory | 32 ? NaClValidationFailedOutOfMemory |
33 : NaClValidationSucceeded; /* or at least to this point! */ | 33 : NaClValidationSucceeded; /* or at least to this point! */ |
34 } | 34 } |
35 | 35 |
36 Bool NaClSegmentValidate_x86_64( | |
37 uintptr_t guest_addr, | |
38 uint8_t *data, | |
39 size_t size, | |
40 struct NaClValidatorState* vstate) { | |
41 Bool is_ok; | |
42 NaClValidateSegment(data, guest_addr, size, vstate); | |
43 is_ok = NaClValidatesOk(vstate); | |
44 NaClValidatorStateDestroy(vstate); | |
45 return is_ok; | |
46 } | |
47 | |
48 static NaClValidationStatus NaClApplyValidatorSilently_x86_64( | 36 static NaClValidationStatus NaClApplyValidatorSilently_x86_64( |
49 uintptr_t guest_addr, | 37 uintptr_t guest_addr, |
50 uint8_t *data, | 38 uint8_t *data, |
51 size_t size, | 39 size_t size, |
52 int bundle_size, | 40 int bundle_size, |
53 NaClCPUFeaturesX86 *cpu_features) { | 41 NaClCPUFeaturesX86 *cpu_features, |
| 42 struct NaClValidationCache *cache) { |
54 struct NaClValidatorState *vstate; | 43 struct NaClValidatorState *vstate; |
55 NaClValidationStatus status = | 44 NaClValidationStatus status; |
| 45 void *query = NULL; |
| 46 |
| 47 if (cache != NULL) |
| 48 query = cache->CreateQuery(cache->handle); |
| 49 |
| 50 if (query != NULL) { |
| 51 const char validator_id[] = "x86-64"; |
| 52 cache->AddData(query, (uint8_t *) validator_id, sizeof(validator_id)); |
| 53 cache->AddData(query, (uint8_t *) cpu_features, sizeof(*cpu_features)); |
| 54 cache->AddData(query, data, size); |
| 55 if (cache->QueryKnownToValidate(query)) { |
| 56 cache->DestroyQuery(query); |
| 57 return NaClValidationSucceeded; |
| 58 } |
| 59 } |
| 60 |
| 61 status = |
56 NaClValidatorSetup_x86_64(guest_addr, size, bundle_size, cpu_features, | 62 NaClValidatorSetup_x86_64(guest_addr, size, bundle_size, cpu_features, |
57 &vstate); | 63 &vstate); |
58 if (status != NaClValidationSucceeded) return status; | 64 |
| 65 if (status != NaClValidationSucceeded) { |
| 66 if (query != NULL) |
| 67 cache->DestroyQuery(query); |
| 68 return status; |
| 69 } |
59 NaClValidatorStateSetLogVerbosity(vstate, LOG_ERROR); | 70 NaClValidatorStateSetLogVerbosity(vstate, LOG_ERROR); |
60 return NaClSegmentValidate_x86_64(guest_addr, data, size, vstate) | 71 NaClValidateSegment(data, guest_addr, size, vstate); |
61 ? NaClValidationSucceeded : NaClValidationFailed; | 72 status = |
| 73 NaClValidatesOk(vstate) ? NaClValidationSucceeded : NaClValidationFailed; |
| 74 |
| 75 if (query != NULL) { |
| 76 /* Don't cache the result if the code is modified. */ |
| 77 if (status == NaClValidationSucceeded && !NaClValidatorDidStubOut(vstate)) |
| 78 cache->SetKnownToValidate(query); |
| 79 cache->DestroyQuery(query); |
| 80 } |
| 81 NaClValidatorStateDestroy(vstate); |
| 82 return status; |
62 } | 83 } |
63 | 84 |
64 NaClValidationStatus NaClApplyValidatorStubout_x86_64( | 85 NaClValidationStatus NaClApplyValidatorStubout_x86_64( |
65 uintptr_t guest_addr, | 86 uintptr_t guest_addr, |
66 uint8_t *data, | 87 uint8_t *data, |
67 size_t size, | 88 size_t size, |
68 int bundle_size, | 89 int bundle_size, |
69 NaClCPUFeaturesX86 *cpu_features) { | 90 NaClCPUFeaturesX86 *cpu_features) { |
70 struct NaClValidatorState *vstate; | 91 struct NaClValidatorState *vstate; |
71 NaClValidationStatus status = | 92 NaClValidationStatus status = |
72 NaClValidatorSetup_x86_64(guest_addr, size, bundle_size, cpu_features, | 93 NaClValidatorSetup_x86_64(guest_addr, size, bundle_size, cpu_features, |
73 &vstate); | 94 &vstate); |
74 if (status != NaClValidationSucceeded) return status; | 95 if (status != NaClValidationSucceeded) return status; |
75 NaClValidatorStateSetDoStubOut(vstate, TRUE); | 96 NaClValidatorStateSetDoStubOut(vstate, TRUE); |
76 NaClSegmentValidate_x86_64(guest_addr, data, size, vstate); | 97 NaClValidateSegment(data, guest_addr, size, vstate); |
| 98 NaClValidatorStateDestroy(vstate); |
77 return NaClValidationSucceeded; | 99 return NaClValidationSucceeded; |
78 } | 100 } |
79 | 101 |
80 NaClValidationStatus NACL_SUBARCH_NAME(ApplyValidator, x86, 64) ( | 102 NaClValidationStatus NACL_SUBARCH_NAME(ApplyValidator, x86, 64) ( |
81 enum NaClSBKind sb_kind, | 103 enum NaClSBKind sb_kind, |
82 NaClApplyValidationKind kind, | 104 NaClApplyValidationKind kind, |
83 uintptr_t guest_addr, | 105 uintptr_t guest_addr, |
84 uint8_t *data, | 106 uint8_t *data, |
85 size_t size, | 107 size_t size, |
86 int bundle_size, | 108 int bundle_size, |
87 NaClCPUFeaturesX86 *cpu_features) { | 109 NaClCPUFeaturesX86 *cpu_features, |
| 110 struct NaClValidationCache *cache) { |
88 NaClValidationStatus status = NaClValidationFailedNotImplemented; | 111 NaClValidationStatus status = NaClValidationFailedNotImplemented; |
89 assert(NACL_SB_DEFAULT == sb_kind); | 112 assert(NACL_SB_DEFAULT == sb_kind); |
90 if (bundle_size == 16 || bundle_size == 32) { | 113 if (bundle_size == 16 || bundle_size == 32) { |
91 if (!NaClArchSupported(cpu_features)) | 114 if (!NaClArchSupported(cpu_features)) |
92 return NaClValidationFailedCpuNotSupported; | 115 return NaClValidationFailedCpuNotSupported; |
93 switch (kind) { | 116 switch (kind) { |
94 case NaClApplyCodeValidation: | 117 case NaClApplyCodeValidation: |
95 status = NaClApplyValidatorSilently_x86_64( | 118 status = NaClApplyValidatorSilently_x86_64( |
96 guest_addr, data, size, bundle_size, cpu_features); | 119 guest_addr, data, size, bundle_size, cpu_features, cache); |
97 break; | 120 break; |
98 case NaClApplyValidationDoStubout: | 121 case NaClApplyValidationDoStubout: |
99 status = NaClApplyValidatorStubout_x86_64( | 122 status = NaClApplyValidatorStubout_x86_64( |
100 guest_addr, data, size, bundle_size, cpu_features); | 123 guest_addr, data, size, bundle_size, cpu_features); |
101 break; | 124 break; |
102 default: | 125 default: |
103 break; | 126 break; |
104 } | 127 } |
105 } | 128 } |
106 return status; | 129 return status; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 if (bundle_size == 16 || bundle_size == 32) { | 162 if (bundle_size == 16 || bundle_size == 32) { |
140 if (!NaClArchSupported(cpu_features)) { | 163 if (!NaClArchSupported(cpu_features)) { |
141 status = NaClValidationFailedCpuNotSupported; | 164 status = NaClValidationFailedCpuNotSupported; |
142 } else { | 165 } else { |
143 status = NaClApplyValidatorPair(guest_addr, data_old, data_new, | 166 status = NaClApplyValidatorPair(guest_addr, data_old, data_new, |
144 size, bundle_size, cpu_features); | 167 size, bundle_size, cpu_features); |
145 } | 168 } |
146 } | 169 } |
147 return status; | 170 return status; |
148 } | 171 } |
OLD | NEW |