| 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 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" | 5 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" |
| 6 | 6 |
| 7 #include <assert.h> | |
| 8 | |
| 9 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 7 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 10 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" | 8 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" |
| 11 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 9 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| 10 #include "mojo/public/cpp/environment/logging.h" |
| 12 | 11 |
| 13 namespace mojo { | 12 namespace mojo { |
| 14 namespace internal { | 13 namespace internal { |
| 15 | 14 |
| 16 namespace { | 15 namespace { |
| 17 | 16 |
| 18 const size_t kAlignment = 8; | 17 const size_t kAlignment = 8; |
| 19 | 18 |
| 20 template<typename T> | 19 template<typename T> |
| 21 T AlignImpl(T t) { | 20 T AlignImpl(T t) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 37 } | 36 } |
| 38 | 37 |
| 39 void EncodePointer(const void* ptr, uint64_t* offset) { | 38 void EncodePointer(const void* ptr, uint64_t* offset) { |
| 40 if (!ptr) { | 39 if (!ptr) { |
| 41 *offset = 0; | 40 *offset = 0; |
| 42 return; | 41 return; |
| 43 } | 42 } |
| 44 | 43 |
| 45 const char* p_obj = reinterpret_cast<const char*>(ptr); | 44 const char* p_obj = reinterpret_cast<const char*>(ptr); |
| 46 const char* p_slot = reinterpret_cast<const char*>(offset); | 45 const char* p_slot = reinterpret_cast<const char*>(offset); |
| 47 assert(p_obj > p_slot); | 46 MOJO_DCHECK(p_obj > p_slot); |
| 48 | 47 |
| 49 *offset = static_cast<uint64_t>(p_obj - p_slot); | 48 *offset = static_cast<uint64_t>(p_obj - p_slot); |
| 50 } | 49 } |
| 51 | 50 |
| 52 const void* DecodePointerRaw(const uint64_t* offset) { | 51 const void* DecodePointerRaw(const uint64_t* offset) { |
| 53 if (!*offset) | 52 if (!*offset) |
| 54 return NULL; | 53 return NULL; |
| 55 return reinterpret_cast<const char*>(offset) + *offset; | 54 return reinterpret_cast<const char*>(offset) + *offset; |
| 56 } | 55 } |
| 57 | 56 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 68 } else { | 67 } else { |
| 69 handle->set_value(kEncodedInvalidHandleValue); | 68 handle->set_value(kEncodedInvalidHandleValue); |
| 70 } | 69 } |
| 71 } | 70 } |
| 72 | 71 |
| 73 void DecodeHandle(Handle* handle, std::vector<Handle>* handles) { | 72 void DecodeHandle(Handle* handle, std::vector<Handle>* handles) { |
| 74 if (handle->value() == kEncodedInvalidHandleValue) { | 73 if (handle->value() == kEncodedInvalidHandleValue) { |
| 75 *handle = Handle(); | 74 *handle = Handle(); |
| 76 return; | 75 return; |
| 77 } | 76 } |
| 78 assert(handle->value() < handles->size()); | 77 MOJO_DCHECK(handle->value() < handles->size()); |
| 79 // Just leave holes in the vector so we don't screw up other indices. | 78 // Just leave holes in the vector so we don't screw up other indices. |
| 80 *handle = FetchAndReset(&handles->at(handle->value())); | 79 *handle = FetchAndReset(&handles->at(handle->value())); |
| 81 } | 80 } |
| 82 | 81 |
| 83 bool ValidateStructHeader(const void* data, | 82 bool ValidateStructHeader(const void* data, |
| 84 uint32_t min_num_bytes, | 83 uint32_t min_num_bytes, |
| 85 uint32_t min_num_fields, | 84 uint32_t min_num_fields, |
| 86 BoundsChecker* bounds_checker) { | 85 BoundsChecker* bounds_checker) { |
| 87 assert(min_num_bytes >= sizeof(StructHeader)); | 86 MOJO_DCHECK(min_num_bytes >= sizeof(StructHeader)); |
| 88 | 87 |
| 89 if (!IsAligned(data)) { | 88 if (!IsAligned(data)) { |
| 90 ReportValidationError(VALIDATION_ERROR_MISALIGNED_OBJECT); | 89 ReportValidationError(VALIDATION_ERROR_MISALIGNED_OBJECT); |
| 91 return false; | 90 return false; |
| 92 } | 91 } |
| 93 if (!bounds_checker->IsValidRange(data, sizeof(StructHeader))) { | 92 if (!bounds_checker->IsValidRange(data, sizeof(StructHeader))) { |
| 94 ReportValidationError(VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE); | 93 ReportValidationError(VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE); |
| 95 return false; | 94 return false; |
| 96 } | 95 } |
| 97 | 96 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 109 if (!bounds_checker->ClaimMemory(data, header->num_bytes)) { | 108 if (!bounds_checker->ClaimMemory(data, header->num_bytes)) { |
| 110 ReportValidationError(VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE); | 109 ReportValidationError(VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE); |
| 111 return false; | 110 return false; |
| 112 } | 111 } |
| 113 | 112 |
| 114 return true; | 113 return true; |
| 115 } | 114 } |
| 116 | 115 |
| 117 } // namespace internal | 116 } // namespace internal |
| 118 } // namespace mojo | 117 } // namespace mojo |
| OLD | NEW |