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