OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_VALIDATION_UTIL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 10 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 | 51 |
52 // Validates that |data| contains a valid union header, in terms of alignment | 52 // Validates that |data| contains a valid union header, in terms of alignment |
53 // and size. If not inlined, it checks that the memory range | 53 // and size. If not inlined, it checks that the memory range |
54 // [data, data + num_bytes) is not marked as occupied by other objects in | 54 // [data, data + num_bytes) is not marked as occupied by other objects in |
55 // |validation_context|. On success, the memory range is marked as occupied. | 55 // |validation_context|. On success, the memory range is marked as occupied. |
56 bool ValidateUnionHeaderAndClaimMemory(const void* data, | 56 bool ValidateUnionHeaderAndClaimMemory(const void* data, |
57 bool inlined, | 57 bool inlined, |
58 ValidationContext* validation_context); | 58 ValidationContext* validation_context); |
59 | 59 |
60 // Validates that the message is a request which doesn't expect a response. | 60 // Validates that the message is a request which doesn't expect a response. |
61 bool ValidateMessageIsRequestWithoutResponse( | 61 inline bool ValidateMessageIsRequestWithoutResponse( |
62 const Message* message, | 62 const Message* message, |
63 ValidationContext* validation_context); | 63 ValidationContext* validation_context) { |
| 64 if (message->has_flag(Message::kFlagIsResponse) || |
| 65 message->has_flag(Message::kFlagExpectsResponse)) { |
| 66 ReportValidationError(validation_context, |
| 67 VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS); |
| 68 return false; |
| 69 } |
| 70 return true; |
| 71 } |
64 | 72 |
65 // Validates that the message is a request expecting a response. | 73 // Validates that the message is a request expecting a response. |
66 bool ValidateMessageIsRequestExpectingResponse( | 74 inline bool ValidateMessageIsRequestExpectingResponse( |
67 const Message* message, | 75 const Message* message, |
68 ValidationContext* validation_context); | 76 ValidationContext* validation_context) { |
| 77 if (message->has_flag(Message::kFlagIsResponse) || |
| 78 !message->has_flag(Message::kFlagExpectsResponse)) { |
| 79 ReportValidationError(validation_context, |
| 80 VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS); |
| 81 return false; |
| 82 } |
| 83 return true; |
| 84 } |
69 | 85 |
70 // Validates that the message is a response. | 86 // Validates that the message is a response. |
71 bool ValidateMessageIsResponse(const Message* message, | 87 inline bool ValidateMessageIsResponse(const Message* message, |
72 ValidationContext* validation_context); | 88 ValidationContext* validation_context) { |
| 89 if (message->has_flag(Message::kFlagExpectsResponse) || |
| 90 !message->has_flag(Message::kFlagIsResponse)) { |
| 91 ReportValidationError(validation_context, |
| 92 VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS); |
| 93 return false; |
| 94 } |
| 95 return true; |
| 96 } |
73 | 97 |
74 // Validates that the message payload is a valid struct of type ParamsType. | 98 // Validates that the message payload is a valid struct of type ParamsType. |
75 template <typename ParamsType> | 99 template <typename ParamsType> |
76 bool ValidateMessagePayload(const Message* message, | 100 bool ValidateMessagePayload(const Message* message, |
77 ValidationContext* validation_context) { | 101 ValidationContext* validation_context) { |
78 return ParamsType::Validate(message->payload(), validation_context); | 102 return ParamsType::Validate(message->payload(), validation_context); |
79 } | 103 } |
80 | 104 |
81 // The following methods validate control messages defined in | 105 // The following methods validate control messages defined in |
82 // interface_control_messages.mojom. | 106 // interface_control_messages.mojom. |
(...skipping 23 matching lines...) Expand all Loading... |
106 ValidationContext* validation_context) { | 130 ValidationContext* validation_context) { |
107 if (!input.is_null()) | 131 if (!input.is_null()) |
108 return true; | 132 return true; |
109 | 133 |
110 ReportValidationError(validation_context, | 134 ReportValidationError(validation_context, |
111 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 135 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
112 error_message); | 136 error_message); |
113 return false; | 137 return false; |
114 } | 138 } |
115 | 139 |
116 bool IsHandleOrInterfaceValid(const AssociatedInterface_Data& input); | 140 inline bool IsHandleOrInterfaceValid(const AssociatedInterface_Data& input) { |
117 bool IsHandleOrInterfaceValid(const AssociatedInterfaceRequest_Data& input); | 141 return IsValidInterfaceId(input.interface_id); |
118 bool IsHandleOrInterfaceValid(const Interface_Data& input); | 142 } |
119 bool IsHandleOrInterfaceValid(const Handle_Data& input); | |
120 | 143 |
121 bool ValidateHandleOrInterfaceNonNullable( | 144 inline bool IsHandleOrInterfaceValid( |
| 145 const AssociatedInterfaceRequest_Data& input) { |
| 146 return IsValidInterfaceId(input.interface_id); |
| 147 } |
| 148 |
| 149 inline bool IsHandleOrInterfaceValid(const Interface_Data& input) { |
| 150 return input.handle.is_valid(); |
| 151 } |
| 152 |
| 153 inline bool IsHandleOrInterfaceValid(const Handle_Data& input) { |
| 154 return input.is_valid(); |
| 155 } |
| 156 |
| 157 inline bool ValidateHandleOrInterfaceNonNullable( |
122 const AssociatedInterface_Data& input, | 158 const AssociatedInterface_Data& input, |
123 const char* error_message, | 159 const char* error_message, |
124 ValidationContext* validation_context); | 160 ValidationContext* validation_context) { |
125 bool ValidateHandleOrInterfaceNonNullable( | 161 if (IsHandleOrInterfaceValid(input)) |
| 162 return true; |
| 163 |
| 164 ReportValidationError(validation_context, |
| 165 VALIDATION_ERROR_UNEXPECTED_INVALID_INTERFACE_ID, |
| 166 error_message); |
| 167 return false; |
| 168 } |
| 169 |
| 170 inline bool ValidateHandleOrInterfaceNonNullable( |
126 const AssociatedInterfaceRequest_Data& input, | 171 const AssociatedInterfaceRequest_Data& input, |
127 const char* error_message, | 172 const char* error_message, |
128 ValidationContext* validation_context); | 173 ValidationContext* validation_context) { |
129 bool ValidateHandleOrInterfaceNonNullable( | 174 if (IsHandleOrInterfaceValid(input)) |
| 175 return true; |
| 176 |
| 177 ReportValidationError(validation_context, |
| 178 VALIDATION_ERROR_UNEXPECTED_INVALID_INTERFACE_ID, |
| 179 error_message); |
| 180 return false; |
| 181 } |
| 182 |
| 183 inline bool ValidateHandleOrInterfaceNonNullable( |
130 const Interface_Data& input, | 184 const Interface_Data& input, |
131 const char* error_message, | 185 const char* error_message, |
132 ValidationContext* validation_context); | 186 ValidationContext* validation_context) { |
133 bool ValidateHandleOrInterfaceNonNullable( | 187 if (IsHandleOrInterfaceValid(input)) |
| 188 return true; |
| 189 |
| 190 ReportValidationError(validation_context, |
| 191 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, |
| 192 error_message); |
| 193 return false; |
| 194 } |
| 195 |
| 196 inline bool ValidateHandleOrInterfaceNonNullable( |
134 const Handle_Data& input, | 197 const Handle_Data& input, |
135 const char* error_message, | 198 const char* error_message, |
136 ValidationContext* validation_context); | 199 ValidationContext* validation_context) { |
| 200 if (IsHandleOrInterfaceValid(input)) |
| 201 return true; |
| 202 |
| 203 ReportValidationError(validation_context, |
| 204 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, |
| 205 error_message); |
| 206 return false; |
| 207 } |
| 208 |
| 209 inline bool ValidateHandleOrInterface(const AssociatedInterface_Data& input, |
| 210 ValidationContext* validation_context) { |
| 211 if (!IsMasterInterfaceId(input.interface_id)) |
| 212 return true; |
| 213 |
| 214 ReportValidationError(validation_context, |
| 215 VALIDATION_ERROR_ILLEGAL_INTERFACE_ID); |
| 216 return false; |
| 217 } |
| 218 |
| 219 inline bool ValidateHandleOrInterface( |
| 220 const AssociatedInterfaceRequest_Data& input, |
| 221 ValidationContext* validation_context) { |
| 222 if (!IsMasterInterfaceId(input.interface_id)) |
| 223 return true; |
| 224 |
| 225 ReportValidationError(validation_context, |
| 226 VALIDATION_ERROR_ILLEGAL_INTERFACE_ID); |
| 227 return false; |
| 228 } |
| 229 |
| 230 inline bool ValidateHandleOrInterface(const Interface_Data& input, |
| 231 ValidationContext* validation_context) { |
| 232 if (validation_context->ClaimHandle(input.handle)) |
| 233 return true; |
| 234 |
| 235 ReportValidationError(validation_context, VALIDATION_ERROR_ILLEGAL_HANDLE); |
| 236 return false; |
| 237 } |
| 238 |
| 239 inline bool ValidateHandleOrInterface(const Handle_Data& input, |
| 240 ValidationContext* validation_context) { |
| 241 if (validation_context->ClaimHandle(input)) |
| 242 return true; |
| 243 |
| 244 ReportValidationError(validation_context, VALIDATION_ERROR_ILLEGAL_HANDLE); |
| 245 return false; |
| 246 } |
137 | 247 |
138 template <typename T> | 248 template <typename T> |
139 bool ValidateContainer(const Pointer<T>& input, | 249 bool ValidateContainer(const Pointer<T>& input, |
140 ValidationContext* validation_context, | 250 ValidationContext* validation_context, |
141 const ContainerValidateParams* validate_params) { | 251 const ContainerValidateParams* validate_params) { |
142 return ValidatePointer(input, validation_context) && | 252 return ValidatePointer(input, validation_context) && |
143 T::Validate(input.Get(), validation_context, validate_params); | 253 T::Validate(input.Get(), validation_context, validate_params); |
144 } | 254 } |
145 | 255 |
146 template <typename T> | 256 template <typename T> |
147 bool ValidateStruct(const Pointer<T>& input, | 257 bool ValidateStruct(const Pointer<T>& input, |
148 ValidationContext* validation_context) { | 258 ValidationContext* validation_context) { |
149 return ValidatePointer(input, validation_context) && | 259 return ValidatePointer(input, validation_context) && |
150 T::Validate(input.Get(), validation_context); | 260 T::Validate(input.Get(), validation_context); |
151 } | 261 } |
152 | 262 |
153 template <typename T> | 263 template <typename T> |
154 bool ValidateInlinedUnion(const T& input, | 264 bool ValidateInlinedUnion(const T& input, |
155 ValidationContext* validation_context) { | 265 ValidationContext* validation_context) { |
156 return T::Validate(&input, validation_context, true); | 266 return T::Validate(&input, validation_context, true); |
157 } | 267 } |
158 | 268 |
159 template <typename T> | 269 template <typename T> |
160 bool ValidateNonInlinedUnion(const Pointer<T>& input, | 270 bool ValidateNonInlinedUnion(const Pointer<T>& input, |
161 ValidationContext* validation_context) { | 271 ValidationContext* validation_context) { |
162 return ValidatePointer(input, validation_context) && | 272 return ValidatePointer(input, validation_context) && |
163 T::Validate(input.Get(), validation_context, false); | 273 T::Validate(input.Get(), validation_context, false); |
164 } | 274 } |
165 | 275 |
166 bool ValidateHandleOrInterface(const AssociatedInterface_Data& input, | |
167 ValidationContext* validation_context); | |
168 bool ValidateHandleOrInterface(const AssociatedInterfaceRequest_Data& input, | |
169 ValidationContext* validation_context); | |
170 bool ValidateHandleOrInterface(const Interface_Data& input, | |
171 ValidationContext* validation_context); | |
172 bool ValidateHandleOrInterface(const Handle_Data& input, | |
173 ValidationContext* validation_context); | |
174 | 276 |
175 } // namespace internal | 277 } // namespace internal |
176 } // namespace mojo | 278 } // namespace mojo |
177 | 279 |
178 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ | 280 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ |
OLD | NEW |