| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/c/bindings/struct.h" | 5 #include "mojo/public/c/bindings/struct.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 | 8 |
| 9 #include "mojo/public/c/bindings/lib/type_descriptor.h" | 9 #include "mojo/public/c/bindings/lib/type_descriptor.h" |
| 10 #include "mojo/public/c/bindings/lib/util.h" | |
| 11 #include "mojo/public/c/bindings/union.h" | 10 #include "mojo/public/c/bindings/union.h" |
| 12 | 11 |
| 13 size_t MojomStruct_ComputeSerializedSize( | 12 size_t MojomStruct_ComputeSerializedSize( |
| 14 const struct MojomTypeDescriptorStruct* in_type_desc, | 13 const struct MojomTypeDescriptorStruct* in_type_desc, |
| 15 const struct MojomStructHeader* in_struct) { | 14 const struct MojomStructHeader* in_struct) { |
| 16 assert(in_struct); | 15 assert(in_struct); |
| 17 assert(in_type_desc); | 16 assert(in_type_desc); |
| 18 | 17 |
| 19 size_t size = in_struct->num_bytes; | 18 size_t size = in_struct->num_bytes; |
| 20 for (size_t i = 0; i < in_type_desc->num_entries; i++) { | 19 for (size_t i = 0; i < in_type_desc->num_entries; i++) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 MojomType_DispatchDecodePointersAndHandles( | 88 MojomType_DispatchDecodePointersAndHandles( |
| 90 entry->elem_type, | 89 entry->elem_type, |
| 91 entry->elem_descriptor, | 90 entry->elem_descriptor, |
| 92 entry->nullable, | 91 entry->nullable, |
| 93 elem_data, | 92 elem_data, |
| 94 in_struct_size - ((char*)elem_data - (char*)inout_struct), | 93 in_struct_size - ((char*)elem_data - (char*)inout_struct), |
| 95 inout_handles, | 94 inout_handles, |
| 96 in_num_handles); | 95 in_num_handles); |
| 97 } | 96 } |
| 98 } | 97 } |
| 98 |
| 99 static bool is_valid_size_for_version( |
| 100 const struct MojomStructHeader* in_struct, |
| 101 const struct MojomTypeDescriptorStructVersion versions[], |
| 102 uint32_t num_versions) { |
| 103 // Scan for a version size in reverse order (assuming structs are often newer |
| 104 // versions than old). Seek to the most recent version that |in_struct| is |
| 105 // compatible with. |
| 106 uint32_t i = num_versions - 1; |
| 107 for (; i > 0 && versions[i].version > in_struct->version; i--); |
| 108 |
| 109 if (in_struct->version == versions[i].version) { |
| 110 return in_struct->num_bytes == versions[i].num_bytes; |
| 111 } else { |
| 112 return in_struct->num_bytes >= versions[i].num_bytes; |
| 113 } |
| 114 } |
| 115 |
| 116 MojomValidationResult MojomStruct_Validate( |
| 117 const struct MojomTypeDescriptorStruct* in_type_desc, |
| 118 const struct MojomStructHeader* in_struct, |
| 119 uint32_t in_struct_size, |
| 120 uint32_t in_num_handles, |
| 121 struct MojomValidationContext* inout_context) { |
| 122 assert(in_type_desc); |
| 123 assert(in_struct); |
| 124 |
| 125 // Struct header validation. |
| 126 if (in_struct_size < sizeof(struct MojomStructHeader)) |
| 127 return MOJOM_VALIDATION_ILLEGAL_MEMORY_RANGE; |
| 128 |
| 129 if (in_struct->num_bytes > in_struct_size) |
| 130 return MOJOM_VALIDATION_ILLEGAL_MEMORY_RANGE; |
| 131 |
| 132 if (!is_valid_size_for_version(in_struct, in_type_desc->versions, |
| 133 in_type_desc->num_versions)) { |
| 134 return MOJOM_VALIDATION_UNEXPECTED_STRUCT_HEADER; |
| 135 } |
| 136 |
| 137 if ((in_struct->num_bytes & 7) != 0) |
| 138 return MOJOM_VALIDATION_MISALIGNED_OBJECT; |
| 139 |
| 140 // From here on out, all pointers need to point past the end of this struct. |
| 141 inout_context->next_pointer = (char*)in_struct + in_struct->num_bytes; |
| 142 |
| 143 for (size_t i = 0; i < in_type_desc->num_entries; i++) { |
| 144 const struct MojomTypeDescriptorStructEntry* entry = |
| 145 &(in_type_desc->entries[i]); |
| 146 |
| 147 if (in_struct->version < entry->min_version) |
| 148 continue; |
| 149 |
| 150 void* elem_data = ((char*)in_struct + sizeof(struct MojomStructHeader) + |
| 151 entry->offset); |
| 152 MojomValidationResult result = MojomType_DispatchValidate( |
| 153 entry->elem_type, |
| 154 entry->elem_descriptor, |
| 155 entry->nullable, |
| 156 elem_data, |
| 157 in_struct_size - ((char*)elem_data - (char*)in_struct), |
| 158 in_num_handles, |
| 159 inout_context); |
| 160 if (result != MOJOM_VALIDATION_ERROR_NONE) |
| 161 return result; |
| 162 } |
| 163 |
| 164 return MOJOM_VALIDATION_ERROR_NONE; |
| 165 } |
| OLD | NEW |