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