OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <new> | 10 #include <new> |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 140 |
141 static void EncodePointers(const ArrayHeader* header, | 141 static void EncodePointers(const ArrayHeader* header, |
142 ElementType* elements) {} | 142 ElementType* elements) {} |
143 | 143 |
144 static void DecodePointers(const ArrayHeader* header, | 144 static void DecodePointers(const ArrayHeader* header, |
145 ElementType* elements) {} | 145 ElementType* elements) {} |
146 | 146 |
147 static bool ValidateElements(const ArrayHeader* header, | 147 static bool ValidateElements(const ArrayHeader* header, |
148 const ElementType* elements, | 148 const ElementType* elements, |
149 BoundsChecker* bounds_checker, | 149 BoundsChecker* bounds_checker, |
150 const ArrayValidateParams* validate_params) { | 150 const ContainerValidateParams* validate_params) { |
151 DCHECK(!validate_params->element_is_nullable) | 151 DCHECK(!validate_params->element_is_nullable) |
152 << "Primitive type should be non-nullable"; | 152 << "Primitive type should be non-nullable"; |
153 DCHECK(!validate_params->element_validate_params) | 153 DCHECK(!validate_params->element_validate_params) |
154 << "Primitive type should not have array validate params"; | 154 << "Primitive type should not have array validate params"; |
155 | 155 |
156 if (!validate_params->validate_enum_func) | 156 if (!validate_params->validate_enum_func) |
157 return true; | 157 return true; |
158 | 158 |
159 // Enum validation. | 159 // Enum validation. |
160 for (uint32_t i = 0; i < header->num_elements; ++i) { | 160 for (uint32_t i = 0; i < header->num_elements; ++i) { |
(...skipping 10 matching lines...) Expand all Loading... |
171 | 171 |
172 static void EncodePointers(const ArrayHeader* header, | 172 static void EncodePointers(const ArrayHeader* header, |
173 ElementType* elements) {} | 173 ElementType* elements) {} |
174 | 174 |
175 static void DecodePointers(const ArrayHeader* header, | 175 static void DecodePointers(const ArrayHeader* header, |
176 ElementType* elements) {} | 176 ElementType* elements) {} |
177 | 177 |
178 static bool ValidateElements(const ArrayHeader* header, | 178 static bool ValidateElements(const ArrayHeader* header, |
179 const ElementType* elements, | 179 const ElementType* elements, |
180 BoundsChecker* bounds_checker, | 180 BoundsChecker* bounds_checker, |
181 const ArrayValidateParams* validate_params) { | 181 const ContainerValidateParams* validate_params) { |
182 DCHECK(!validate_params->element_validate_params) | 182 DCHECK(!validate_params->element_validate_params) |
183 << "Handle type should not have array validate params"; | 183 << "Handle type should not have array validate params"; |
184 | 184 |
185 for (uint32_t i = 0; i < header->num_elements; ++i) { | 185 for (uint32_t i = 0; i < header->num_elements; ++i) { |
186 if (!validate_params->element_is_nullable && !elements[i].is_valid()) { | 186 if (!validate_params->element_is_nullable && !elements[i].is_valid()) { |
187 ReportValidationError( | 187 ReportValidationError( |
188 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, | 188 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, |
189 MakeMessageWithArrayIndex( | 189 MakeMessageWithArrayIndex( |
190 "invalid handle in array expecting valid handles", | 190 "invalid handle in array expecting valid handles", |
191 header->num_elements, | 191 header->num_elements, |
(...skipping 19 matching lines...) Expand all Loading... |
211 } | 211 } |
212 | 212 |
213 static void DecodePointers(const ArrayHeader* header, ElementType* elements) { | 213 static void DecodePointers(const ArrayHeader* header, ElementType* elements) { |
214 for (uint32_t i = 0; i < header->num_elements; ++i) | 214 for (uint32_t i = 0; i < header->num_elements; ++i) |
215 Decode(&elements[i]); | 215 Decode(&elements[i]); |
216 } | 216 } |
217 | 217 |
218 static bool ValidateElements(const ArrayHeader* header, | 218 static bool ValidateElements(const ArrayHeader* header, |
219 const ElementType* elements, | 219 const ElementType* elements, |
220 BoundsChecker* bounds_checker, | 220 BoundsChecker* bounds_checker, |
221 const ArrayValidateParams* validate_params) { | 221 const ContainerValidateParams* validate_params) { |
222 for (uint32_t i = 0; i < header->num_elements; ++i) { | 222 for (uint32_t i = 0; i < header->num_elements; ++i) { |
223 if (!validate_params->element_is_nullable && !elements[i].offset) { | 223 if (!validate_params->element_is_nullable && !elements[i].offset) { |
224 ReportValidationError( | 224 ReportValidationError( |
225 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 225 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
226 MakeMessageWithArrayIndex("null in array expecting valid pointers", | 226 MakeMessageWithArrayIndex("null in array expecting valid pointers", |
227 header->num_elements, | 227 header->num_elements, |
228 i).c_str()); | 228 i).c_str()); |
229 return false; | 229 return false; |
230 } | 230 } |
231 if (!ValidateEncodedPointer(&elements[i].offset)) { | 231 if (!ValidateEncodedPointer(&elements[i].offset)) { |
232 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER); | 232 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER); |
233 return false; | 233 return false; |
234 } | 234 } |
235 if (!ValidateCaller<P>::Run(DecodePointerRaw(&elements[i].offset), | 235 if (!ValidateCaller<P>::Run(DecodePointerRaw(&elements[i].offset), |
236 bounds_checker, | 236 bounds_checker, |
237 validate_params->element_validate_params)) { | 237 validate_params->element_validate_params)) { |
238 return false; | 238 return false; |
239 } | 239 } |
240 } | 240 } |
241 return true; | 241 return true; |
242 } | 242 } |
243 | 243 |
244 private: | 244 private: |
245 template <typename T> | 245 template <typename T> |
246 struct ValidateCaller { | 246 struct ValidateCaller { |
247 static bool Run(const void* data, | 247 static bool Run(const void* data, |
248 BoundsChecker* bounds_checker, | 248 BoundsChecker* bounds_checker, |
249 const ArrayValidateParams* validate_params) { | 249 const ContainerValidateParams* validate_params) { |
250 DCHECK(!validate_params) | 250 DCHECK(!validate_params) |
251 << "Struct type should not have array validate params"; | 251 << "Struct type should not have array validate params"; |
252 | 252 |
253 return T::Validate(data, bounds_checker); | 253 return T::Validate(data, bounds_checker); |
254 } | 254 } |
255 }; | 255 }; |
256 | 256 |
257 template <typename Key, typename Value> | 257 template <typename Key, typename Value> |
258 struct ValidateCaller<Map_Data<Key, Value>> { | 258 struct ValidateCaller<Map_Data<Key, Value>> { |
259 static bool Run(const void* data, | 259 static bool Run(const void* data, |
260 BoundsChecker* bounds_checker, | 260 BoundsChecker* bounds_checker, |
261 const ArrayValidateParams* validate_params) { | 261 const ContainerValidateParams* validate_params) { |
262 return Map_Data<Key, Value>::Validate(data, bounds_checker, | 262 return Map_Data<Key, Value>::Validate(data, bounds_checker, |
263 validate_params); | 263 validate_params); |
264 } | 264 } |
265 }; | 265 }; |
266 | 266 |
267 template <typename T> | 267 template <typename T> |
268 struct ValidateCaller<Array_Data<T>> { | 268 struct ValidateCaller<Array_Data<T>> { |
269 static bool Run(const void* data, | 269 static bool Run(const void* data, |
270 BoundsChecker* bounds_checker, | 270 BoundsChecker* bounds_checker, |
271 const ArrayValidateParams* validate_params) { | 271 const ContainerValidateParams* validate_params) { |
272 return Array_Data<T>::Validate(data, bounds_checker, validate_params); | 272 return Array_Data<T>::Validate(data, bounds_checker, validate_params); |
273 } | 273 } |
274 }; | 274 }; |
275 }; | 275 }; |
276 | 276 |
277 template <typename U> | 277 template <typename U> |
278 struct ArraySerializationHelper<U, true> { | 278 struct ArraySerializationHelper<U, true> { |
279 typedef typename ArrayDataTraits<U>::StorageType ElementType; | 279 typedef typename ArrayDataTraits<U>::StorageType ElementType; |
280 | 280 |
281 static void EncodePointers(const ArrayHeader* header, ElementType* elements) { | 281 static void EncodePointers(const ArrayHeader* header, ElementType* elements) { |
282 for (uint32_t i = 0; i < header->num_elements; ++i) | 282 for (uint32_t i = 0; i < header->num_elements; ++i) |
283 elements[i].EncodePointers(); | 283 elements[i].EncodePointers(); |
284 } | 284 } |
285 | 285 |
286 static void DecodePointers(const ArrayHeader* header, ElementType* elements) { | 286 static void DecodePointers(const ArrayHeader* header, ElementType* elements) { |
287 for (uint32_t i = 0; i < header->num_elements; ++i) | 287 for (uint32_t i = 0; i < header->num_elements; ++i) |
288 elements[i].DecodePointers(); | 288 elements[i].DecodePointers(); |
289 } | 289 } |
290 | 290 |
291 static bool ValidateElements(const ArrayHeader* header, | 291 static bool ValidateElements(const ArrayHeader* header, |
292 const ElementType* elements, | 292 const ElementType* elements, |
293 BoundsChecker* bounds_checker, | 293 BoundsChecker* bounds_checker, |
294 const ArrayValidateParams* validate_params) { | 294 const ContainerValidateParams* validate_params) { |
295 for (uint32_t i = 0; i < header->num_elements; ++i) { | 295 for (uint32_t i = 0; i < header->num_elements; ++i) { |
296 if (!validate_params->element_is_nullable && elements[i].is_null()) { | 296 if (!validate_params->element_is_nullable && elements[i].is_null()) { |
297 ReportValidationError( | 297 ReportValidationError( |
298 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 298 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
299 MakeMessageWithArrayIndex("null in array expecting valid unions", | 299 MakeMessageWithArrayIndex("null in array expecting valid unions", |
300 header->num_elements, i) | 300 header->num_elements, i) |
301 .c_str()); | 301 .c_str()); |
302 return false; | 302 return false; |
303 } | 303 } |
304 if (!ElementType::Validate(elements + i, bounds_checker, true)) | 304 if (!ElementType::Validate(elements + i, bounds_checker, true)) |
(...skipping 20 matching lines...) Expand all Loading... |
325 return nullptr; | 325 return nullptr; |
326 | 326 |
327 uint32_t num_bytes = | 327 uint32_t num_bytes = |
328 Traits::GetStorageSize(static_cast<uint32_t>(num_elements)); | 328 Traits::GetStorageSize(static_cast<uint32_t>(num_elements)); |
329 return new (buf->Allocate(num_bytes)) | 329 return new (buf->Allocate(num_bytes)) |
330 Array_Data<T>(num_bytes, static_cast<uint32_t>(num_elements)); | 330 Array_Data<T>(num_bytes, static_cast<uint32_t>(num_elements)); |
331 } | 331 } |
332 | 332 |
333 static bool Validate(const void* data, | 333 static bool Validate(const void* data, |
334 BoundsChecker* bounds_checker, | 334 BoundsChecker* bounds_checker, |
335 const ArrayValidateParams* validate_params) { | 335 const ContainerValidateParams* validate_params) { |
336 if (!data) | 336 if (!data) |
337 return true; | 337 return true; |
338 if (!IsAligned(data)) { | 338 if (!IsAligned(data)) { |
339 ReportValidationError(VALIDATION_ERROR_MISALIGNED_OBJECT); | 339 ReportValidationError(VALIDATION_ERROR_MISALIGNED_OBJECT); |
340 return false; | 340 return false; |
341 } | 341 } |
342 if (!bounds_checker->IsValidRange(data, sizeof(ArrayHeader))) { | 342 if (!bounds_checker->IsValidRange(data, sizeof(ArrayHeader))) { |
343 ReportValidationError(VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE); | 343 ReportValidationError(VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE); |
344 return false; | 344 return false; |
345 } | 345 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 }; | 407 }; |
408 static_assert(sizeof(Array_Data<char>) == 8, "Bad sizeof(Array_Data)"); | 408 static_assert(sizeof(Array_Data<char>) == 8, "Bad sizeof(Array_Data)"); |
409 | 409 |
410 // UTF-8 encoded | 410 // UTF-8 encoded |
411 using String_Data = Array_Data<char>; | 411 using String_Data = Array_Data<char>; |
412 | 412 |
413 } // namespace internal | 413 } // namespace internal |
414 } // namespace mojo | 414 } // namespace mojo |
415 | 415 |
416 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ | 416 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
OLD | NEW |