| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_NACL_MOJO_SYSCALL_INTERNAL_H_ | 5 #ifndef MOJO_NACL_MOJO_SYSCALL_INTERNAL_H_ |
| 6 #define MOJO_NACL_MOJO_SYSCALL_INTERNAL_H_ | 6 #define MOJO_NACL_MOJO_SYSCALL_INTERNAL_H_ |
| 7 | 7 |
| 8 #include <type_traits> |
| 9 |
| 8 #include "native_client/src/trusted/service_runtime/nacl_copy.h" | 10 #include "native_client/src/trusted/service_runtime/nacl_copy.h" |
| 9 #include "native_client/src/trusted/service_runtime/sel_ldr.h" | 11 #include "native_client/src/trusted/service_runtime/sel_ldr.h" |
| 10 | 12 |
| 11 namespace { | 13 namespace { |
| 12 | 14 |
| 13 class ScopedCopyLock { | 15 class ScopedCopyLock { |
| 14 public: | 16 public: |
| 15 explicit ScopedCopyLock(struct NaClApp* nap) : nap_(nap) { | 17 explicit ScopedCopyLock(struct NaClApp* nap) : nap_(nap) { |
| 16 NaClCopyTakeLock(nap_); | 18 NaClCopyTakeLock(nap_); |
| 17 } | 19 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 44 const void* src, | 46 const void* src, |
| 45 size_t n) { | 47 size_t n) { |
| 46 char volatile* c_dst = static_cast<char volatile*>(dst); | 48 char volatile* c_dst = static_cast<char volatile*>(dst); |
| 47 const char* c_src = static_cast<const char*>(src); | 49 const char* c_src = static_cast<const char*>(src); |
| 48 for (size_t i = 0; i < n; i++) { | 50 for (size_t i = 0; i < n; i++) { |
| 49 c_dst[i] = c_src[i]; | 51 c_dst[i] = c_src[i]; |
| 50 } | 52 } |
| 51 } | 53 } |
| 52 | 54 |
| 53 template <typename T> | 55 template <typename T> |
| 56 bool ConvertPointerInput(struct NaClApp* nap, uint32_t user_ptr, T** value) { |
| 57 if (user_ptr) { |
| 58 uintptr_t temp = NaClUserToSysAddr(nap, user_ptr); |
| 59 if (temp != kNaClBadAddress) { |
| 60 *value = reinterpret_cast<T*>(temp); |
| 61 return true; |
| 62 } |
| 63 } else { |
| 64 *value = nullptr; |
| 65 return true; |
| 66 } |
| 67 *value = nullptr; // Paranoia. |
| 68 return false; |
| 69 } |
| 70 |
| 71 template <typename T> |
| 72 bool ConvertPointerInOut(struct NaClApp* nap, |
| 73 uint32_t user_ptr, |
| 74 bool optional, |
| 75 T** value, |
| 76 uint32_t volatile** sys_ptr) { |
| 77 if (user_ptr) { |
| 78 uintptr_t temp = NaClUserToSysAddrRange(nap, user_ptr, sizeof(uint32_t)); |
| 79 if (temp != kNaClBadAddress) { |
| 80 uint32_t volatile* converted_ptr = |
| 81 reinterpret_cast<uint32_t volatile*>(temp); |
| 82 uint32_t raw_value = *converted_ptr; |
| 83 if (!raw_value) { |
| 84 *sys_ptr = converted_ptr; |
| 85 *value = nullptr; |
| 86 return true; |
| 87 } |
| 88 uintptr_t temp = NaClUserToSysAddr(nap, raw_value); |
| 89 if (temp != kNaClBadAddress) { |
| 90 *sys_ptr = converted_ptr; |
| 91 *value = reinterpret_cast<T*>(temp); |
| 92 return true; |
| 93 } |
| 94 } |
| 95 } else if (optional) { |
| 96 *sys_ptr = nullptr; |
| 97 *value = nullptr; // Paranoia. |
| 98 return true; |
| 99 } |
| 100 *sys_ptr = nullptr; // Paranoia. |
| 101 *value = nullptr; // Paranoia. |
| 102 return false; |
| 103 } |
| 104 |
| 105 template <typename T> |
| 106 void CopyOutPointer(struct NaClApp* nap, T* value, uint32_t volatile* sys_ptr) { |
| 107 if (value) { |
| 108 // Will kill the process if value if the pointer does not lie in the |
| 109 // sandbox. |
| 110 uintptr_t temp = NaClSysToUser(nap, reinterpret_cast<uintptr_t>(value)); |
| 111 *sys_ptr = static_cast<uint32_t>(temp); |
| 112 } else { |
| 113 *sys_ptr = 0; |
| 114 } |
| 115 } |
| 116 |
| 117 template <typename T> |
| 54 bool ConvertScalarInput(struct NaClApp* nap, uint32_t user_ptr, T* value) { | 118 bool ConvertScalarInput(struct NaClApp* nap, uint32_t user_ptr, T* value) { |
| 119 static_assert(!std::is_pointer<T>::value, "do not use for pointer types"); |
| 55 if (user_ptr) { | 120 if (user_ptr) { |
| 56 uintptr_t temp = NaClUserToSysAddrRange(nap, user_ptr, sizeof(T)); | 121 uintptr_t temp = NaClUserToSysAddrRange(nap, user_ptr, sizeof(T)); |
| 57 if (temp != kNaClBadAddress) { | 122 if (temp != kNaClBadAddress) { |
| 58 *value = *reinterpret_cast<T volatile*>(temp); | 123 *value = *reinterpret_cast<T volatile*>(temp); |
| 59 return true; | 124 return true; |
| 60 } | 125 } |
| 61 } | 126 } |
| 62 return false; | 127 return false; |
| 63 } | 128 } |
| 64 | 129 |
| 65 template <typename T> | 130 template <typename T> |
| 66 bool ConvertScalarOutput(struct NaClApp* nap, | 131 bool ConvertScalarOutput(struct NaClApp* nap, |
| 67 uint32_t user_ptr, | 132 uint32_t user_ptr, |
| 68 bool optional, | 133 bool optional, |
| 69 T volatile** sys_ptr) { | 134 T volatile** sys_ptr) { |
| 135 static_assert(!std::is_pointer<T>::value, "do not use for pointer types"); |
| 70 if (user_ptr) { | 136 if (user_ptr) { |
| 71 uintptr_t temp = NaClUserToSysAddrRange(nap, user_ptr, sizeof(T)); | 137 uintptr_t temp = NaClUserToSysAddrRange(nap, user_ptr, sizeof(T)); |
| 72 if (temp != kNaClBadAddress) { | 138 if (temp != kNaClBadAddress) { |
| 73 *sys_ptr = reinterpret_cast<T volatile*>(temp); | 139 *sys_ptr = reinterpret_cast<T volatile*>(temp); |
| 74 return true; | 140 return true; |
| 75 } | 141 } |
| 76 } else if (optional) { | 142 } else if (optional) { |
| 77 *sys_ptr = 0; | 143 *sys_ptr = 0; |
| 78 return true; | 144 return true; |
| 79 } | 145 } |
| 80 *sys_ptr = 0; // Paranoia. | 146 *sys_ptr = 0; // Paranoia. |
| 81 return false; | 147 return false; |
| 82 } | 148 } |
| 83 | 149 |
| 84 template <typename T> | 150 template <typename T> |
| 85 bool ConvertScalarInOut(struct NaClApp* nap, | 151 bool ConvertScalarInOut(struct NaClApp* nap, |
| 86 uint32_t user_ptr, | 152 uint32_t user_ptr, |
| 87 bool optional, | 153 bool optional, |
| 88 T* value, | 154 T* value, |
| 89 T volatile** sys_ptr) { | 155 T volatile** sys_ptr) { |
| 156 static_assert(!std::is_pointer<T>::value, "do not use for pointer types"); |
| 90 if (user_ptr) { | 157 if (user_ptr) { |
| 91 uintptr_t temp = NaClUserToSysAddrRange(nap, user_ptr, sizeof(T)); | 158 uintptr_t temp = NaClUserToSysAddrRange(nap, user_ptr, sizeof(T)); |
| 92 if (temp != kNaClBadAddress) { | 159 if (temp != kNaClBadAddress) { |
| 93 T volatile* converted = reinterpret_cast<T volatile*>(temp); | 160 T volatile* converted = reinterpret_cast<T volatile*>(temp); |
| 94 *sys_ptr = converted; | 161 *sys_ptr = converted; |
| 95 *value = *converted; | 162 *value = *converted; |
| 96 return true; | 163 return true; |
| 97 } | 164 } |
| 98 } else if (optional) { | 165 } else if (optional) { |
| 99 *sys_ptr = 0; | 166 *sys_ptr = 0; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 return false; | 212 return false; |
| 146 } | 213 } |
| 147 | 214 |
| 148 // TODO(ncbray): size validation and complete copy. | 215 // TODO(ncbray): size validation and complete copy. |
| 149 // TODO(ncbray): ensure non-null / missized structs are covered by a test case. | 216 // TODO(ncbray): ensure non-null / missized structs are covered by a test case. |
| 150 template <typename T> | 217 template <typename T> |
| 151 bool ConvertExtensibleStructInput(struct NaClApp* nap, | 218 bool ConvertExtensibleStructInput(struct NaClApp* nap, |
| 152 uint32_t user_ptr, | 219 uint32_t user_ptr, |
| 153 bool optional, | 220 bool optional, |
| 154 T** sys_ptr) { | 221 T** sys_ptr) { |
| 222 static_assert(!std::is_pointer<T>::value, "do not use for pointer types"); |
| 155 if (user_ptr) { | 223 if (user_ptr) { |
| 156 uintptr_t temp = NaClUserToSysAddrRange(nap, user_ptr, sizeof(T)); | 224 uintptr_t temp = NaClUserToSysAddrRange(nap, user_ptr, sizeof(T)); |
| 157 if (temp != kNaClBadAddress) { | 225 if (temp != kNaClBadAddress) { |
| 158 *sys_ptr = reinterpret_cast<T*>(temp); | 226 *sys_ptr = reinterpret_cast<T*>(temp); |
| 159 return true; | 227 return true; |
| 160 } | 228 } |
| 161 } else if (optional) { | 229 } else if (optional) { |
| 162 *sys_ptr = 0; | 230 *sys_ptr = 0; |
| 163 return true; | 231 return true; |
| 164 } | 232 } |
| 165 return false; | 233 return false; |
| 166 } | 234 } |
| 167 | 235 |
| 168 } // namespace | 236 } // namespace |
| 169 | 237 |
| 170 #endif // MOJO_NACL_MOJO_SYSCALL_INTERNAL_H_ | 238 #endif // MOJO_NACL_MOJO_SYSCALL_INTERNAL_H_ |
| OLD | NEW |