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 #include "native_client/src/trusted/validator_mips/ncvalidate.h" |
| 8 |
| 9 #include <vector> |
| 10 |
| 11 #include "native_client/src/include/nacl_string.h" |
| 12 #include "native_client/src/include/portability.h" |
| 13 #include "native_client/src/trusted/service_runtime/arch/mips/sel_ldr_mips.h" |
| 14 #include "native_client/src/trusted/validator_mips/validator.h" |
| 15 #include "native_client/src/trusted/validator_mips/model.h" |
| 16 #include "native_client/src/trusted/validator/ncvalidate.h" |
| 17 |
| 18 using nacl_mips_val::SfiValidator; |
| 19 using nacl_mips_val::CodeSegment; |
| 20 using nacl_mips_dec::Register; |
| 21 using nacl_mips_dec::kRegisterStack; |
| 22 using nacl_mips_dec::kRegListReserved; |
| 23 using std::vector; |
| 24 |
| 25 |
| 26 class EarlyExitProblemSink : public nacl_mips_val::ProblemSink { |
| 27 private: |
| 28 bool problems_; |
| 29 |
| 30 public: |
| 31 EarlyExitProblemSink() : nacl_mips_val::ProblemSink(), problems_(false) {} |
| 32 |
| 33 virtual void ReportProblem(uint32_t vaddr, |
| 34 nacl_mips_dec::SafetyLevel safety, |
| 35 const nacl::string &problem_code, |
| 36 uint32_t ref_vaddr) { |
| 37 UNREFERENCED_PARAMETER(vaddr); |
| 38 UNREFERENCED_PARAMETER(safety); |
| 39 UNREFERENCED_PARAMETER(problem_code); |
| 40 UNREFERENCED_PARAMETER(ref_vaddr); |
| 41 |
| 42 problems_ = true; |
| 43 } |
| 44 virtual bool ShouldContinue() { |
| 45 return !problems_; |
| 46 } |
| 47 }; |
| 48 |
| 49 |
| 50 class StuboutProblemSink : public nacl_mips_val::ProblemSink { |
| 51 private: |
| 52 bool problems_; |
| 53 uint32_t const kNaClFullStop; |
| 54 |
| 55 public: |
| 56 StuboutProblemSink() : nacl_mips_val::ProblemSink(), problems_(false), |
| 57 kNaClFullStop(NACL_HALT_OPCODE) {} |
| 58 |
| 59 virtual void ReportProblem(uint32_t vaddr, |
| 60 nacl_mips_dec::SafetyLevel safety, |
| 61 const nacl::string &problem_code, |
| 62 uint32_t ref_vaddr) { |
| 63 UNREFERENCED_PARAMETER(safety); |
| 64 UNREFERENCED_PARAMETER(problem_code); |
| 65 UNREFERENCED_PARAMETER(ref_vaddr); |
| 66 stub_out_instr(vaddr); |
| 67 |
| 68 problems_ = true; |
| 69 } |
| 70 virtual bool ShouldContinue() { |
| 71 return true; |
| 72 } |
| 73 |
| 74 private: |
| 75 void stub_out_instr(uint32_t vaddr) { |
| 76 #ifdef __BIG_ENDIAN__ |
| 77 CHECK(0); |
| 78 #endif |
| 79 *reinterpret_cast<uint32_t *>(vaddr) = kNaClFullStop; |
| 80 } |
| 81 }; |
| 82 |
| 83 EXTERN_C_BEGIN |
| 84 |
| 85 int NCValidateSegment(uint8_t *mbase, uint32_t vbase, size_t size, |
| 86 bool stubout_mode) { |
| 87 SfiValidator validator( |
| 88 16, // 64, // bytes per bundle |
| 89 1U * NACL_DATA_SEGMENT_START, // bytes of code space |
| 90 1U * (1<<NACL_MAX_ADDR_BITS), // bytes of data space // keep in sync w/ |
| 91 // SConstruct: irt_compatible_rodata_addr |
| 92 kRegListReserved, // read only register(s) |
| 93 kRegisterStack); // data addressing register(s) |
| 94 bool success = false; |
| 95 |
| 96 vector<CodeSegment> segments; |
| 97 segments.push_back(CodeSegment(mbase, vbase, size)); |
| 98 |
| 99 if (stubout_mode) { |
| 100 StuboutProblemSink sink; |
| 101 success = validator.Validate(segments, &sink); |
| 102 } else { |
| 103 EarlyExitProblemSink sink; |
| 104 success = validator.Validate(segments, &sink); |
| 105 } |
| 106 if (!success) return 2; |
| 107 |
| 108 return 0; |
| 109 } |
| 110 |
| 111 static NaClValidationStatus ApplyValidatorMips( |
| 112 uintptr_t guest_addr, |
| 113 uint8_t *data, |
| 114 size_t size, |
| 115 int stubout_mode, |
| 116 int readonly_text, |
| 117 const NaClCPUFeatures *cpu_features, |
| 118 struct NaClValidationCache *cache) { |
| 119 NaClValidationStatus status = NaClValidationFailedNotImplemented; |
| 120 UNREFERENCED_PARAMETER(cpu_features); |
| 121 UNREFERENCED_PARAMETER(cache); |
| 122 if (stubout_mode) { |
| 123 NCValidateSegment(data, guest_addr, size, true); |
| 124 status = NaClValidationSucceeded; |
| 125 } else if (readonly_text) { |
| 126 status = NaClValidationFailedNotImplemented; |
| 127 } else { |
| 128 status = ((0 == NCValidateSegment(data, guest_addr, size, false)) |
| 129 ? NaClValidationSucceeded : NaClValidationFailed); |
| 130 } |
| 131 return status; |
| 132 } |
| 133 |
| 134 static NaClValidationStatus ValidatorCodeReplacementNotImplemented( |
| 135 uintptr_t guest_addr, |
| 136 uint8_t *data_old, |
| 137 uint8_t *data_new, |
| 138 size_t size, |
| 139 const NaClCPUFeatures *cpu_features) { |
| 140 UNREFERENCED_PARAMETER(guest_addr); |
| 141 UNREFERENCED_PARAMETER(data_old); |
| 142 UNREFERENCED_PARAMETER(data_new); |
| 143 UNREFERENCED_PARAMETER(size); |
| 144 UNREFERENCED_PARAMETER(cpu_features); |
| 145 return NaClValidationFailedNotImplemented; |
| 146 } |
| 147 |
| 148 static NaClValidationStatus ValidatorCopyNotImplemented( |
| 149 uintptr_t guest_addr, |
| 150 uint8_t *data_old, |
| 151 uint8_t *data_new, |
| 152 size_t size, |
| 153 const NaClCPUFeatures *cpu_features, |
| 154 NaClCopyInstructionFunc copy_func) { |
| 155 UNREFERENCED_PARAMETER(guest_addr); |
| 156 UNREFERENCED_PARAMETER(data_old); |
| 157 UNREFERENCED_PARAMETER(data_new); |
| 158 UNREFERENCED_PARAMETER(size); |
| 159 UNREFERENCED_PARAMETER(cpu_features); |
| 160 UNREFERENCED_PARAMETER(copy_func); |
| 161 return NaClValidationFailedNotImplemented; |
| 162 } |
| 163 |
| 164 static struct NaClValidatorInterface validator = { |
| 165 ApplyValidatorMips, |
| 166 ValidatorCopyNotImplemented, |
| 167 ValidatorCodeReplacementNotImplemented, |
| 168 }; |
| 169 |
| 170 const struct NaClValidatorInterface *NaClValidatorCreateMips() { |
| 171 return &validator; |
| 172 } |
| 173 |
| 174 /* |
| 175 * When safe instruction copying gets implemented for MIPS, it should be |
| 176 * moved to be part of sel_ldr, not the validator. |
| 177 */ |
| 178 int NaClCopyInstruction(uint8_t *dst, uint8_t *src, uint8_t sz) { |
| 179 UNREFERENCED_PARAMETER(dst); |
| 180 UNREFERENCED_PARAMETER(src); |
| 181 UNREFERENCED_PARAMETER(sz); |
| 182 |
| 183 return 0; |
| 184 } |
| 185 |
| 186 EXTERN_C_END |
OLD | NEW |