Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: mojo/public/js/validator.js

Issue 2796253002: Associated Message Validation (Closed)
Patch Set: Remove encode & decode for Associated Interface/Request. Current test only tests validation. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 define("mojo/public/js/validator", [ 5 define("mojo/public/js/validator", [
6 "mojo/public/js/codec", 6 "mojo/public/js/codec",
7 ], function(codec) { 7 "mojo/public/js/interface_types",
8 ], function(codec, types) {
8 9
9 var validationError = { 10 var validationError = {
10 NONE: 'VALIDATION_ERROR_NONE', 11 NONE: 'VALIDATION_ERROR_NONE',
11 MISALIGNED_OBJECT: 'VALIDATION_ERROR_MISALIGNED_OBJECT', 12 MISALIGNED_OBJECT: 'VALIDATION_ERROR_MISALIGNED_OBJECT',
12 ILLEGAL_MEMORY_RANGE: 'VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE', 13 ILLEGAL_MEMORY_RANGE: 'VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE',
13 UNEXPECTED_STRUCT_HEADER: 'VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER', 14 UNEXPECTED_STRUCT_HEADER: 'VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER',
14 UNEXPECTED_ARRAY_HEADER: 'VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER', 15 UNEXPECTED_ARRAY_HEADER: 'VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER',
15 ILLEGAL_HANDLE: 'VALIDATION_ERROR_ILLEGAL_HANDLE', 16 ILLEGAL_HANDLE: 'VALIDATION_ERROR_ILLEGAL_HANDLE',
16 UNEXPECTED_INVALID_HANDLE: 'VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE', 17 UNEXPECTED_INVALID_HANDLE: 'VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE',
17 ILLEGAL_POINTER: 'VALIDATION_ERROR_ILLEGAL_POINTER', 18 ILLEGAL_POINTER: 'VALIDATION_ERROR_ILLEGAL_POINTER',
18 UNEXPECTED_NULL_POINTER: 'VALIDATION_ERROR_UNEXPECTED_NULL_POINTER', 19 UNEXPECTED_NULL_POINTER: 'VALIDATION_ERROR_UNEXPECTED_NULL_POINTER',
20 ILLEGAL_INTERFACE_ID: 'VALIDATION_ERROR_ILLEGAL_INTERFACE_ID',
21 UNEXPECTED_INVALID_INTERFACE_ID:
22 'VALIDATION_ERROR_UNEXPECTED_INVALID_INTERFACE_ID',
19 MESSAGE_HEADER_INVALID_FLAGS: 23 MESSAGE_HEADER_INVALID_FLAGS:
20 'VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS', 24 'VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS',
21 MESSAGE_HEADER_MISSING_REQUEST_ID: 25 MESSAGE_HEADER_MISSING_REQUEST_ID:
22 'VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID', 26 'VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID',
23 DIFFERENT_SIZED_ARRAYS_IN_MAP: 27 DIFFERENT_SIZED_ARRAYS_IN_MAP:
24 'VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP', 28 'VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP',
25 INVALID_UNION_SIZE: 'VALIDATION_ERROR_INVALID_UNION_SIZE', 29 INVALID_UNION_SIZE: 'VALIDATION_ERROR_INVALID_UNION_SIZE',
26 UNEXPECTED_NULL_UNION: 'VALIDATION_ERROR_UNEXPECTED_NULL_UNION', 30 UNEXPECTED_NULL_UNION: 'VALIDATION_ERROR_UNEXPECTED_NULL_UNION',
27 UNKNOWN_ENUM_VALUE: 'VALIDATION_ERROR_UNKNOWN_ENUM_VALUE', 31 UNKNOWN_ENUM_VALUE: 'VALIDATION_ERROR_UNKNOWN_ENUM_VALUE',
28 }; 32 };
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 90
87 function isInterfaceClass(cls) { 91 function isInterfaceClass(cls) {
88 return cls instanceof codec.Interface; 92 return cls instanceof codec.Interface;
89 } 93 }
90 94
91 function isInterfaceRequestClass(cls) { 95 function isInterfaceRequestClass(cls) {
92 return cls === codec.InterfaceRequest || 96 return cls === codec.InterfaceRequest ||
93 cls === codec.NullableInterfaceRequest; 97 cls === codec.NullableInterfaceRequest;
94 } 98 }
95 99
100 function isAssociatedInterfaceClass(cls) {
101 return cls === codec.AssociatedInterfacePtrInfo ||
102 cls === codec.NullableAssociatedInterfacePtrInfo;
103 }
104
105 function isAssociatedInterfaceRequestClass(cls) {
106 return cls === codec.AssociatedInterfaceRequest ||
107 cls === codec.NullableAssociatedInterfaceRequest;
108 }
109
96 function isNullable(type) { 110 function isNullable(type) {
97 return type === codec.NullableString || type === codec.NullableHandle || 111 return type === codec.NullableString || type === codec.NullableHandle ||
98 type === codec.NullableInterface || 112 type === codec.NullableInterface ||
99 type === codec.NullableInterfaceRequest || 113 type === codec.NullableInterfaceRequest ||
100 type instanceof codec.NullableArrayOf || 114 type instanceof codec.NullableArrayOf ||
101 type instanceof codec.NullablePointerTo; 115 type instanceof codec.NullablePointerTo;
102 } 116 }
103 117
104 function Validator(message) { 118 function Validator(message) {
105 this.message = message; 119 this.message = message;
106 this.offset = 0; 120 this.offset = 0;
107 this.handleIndex = 0; 121 this.handleIndex = 0;
122 this.associatedEndpointHandleIndex = 0;
108 } 123 }
109 124
110 Object.defineProperty(Validator.prototype, "offsetLimit", { 125 Object.defineProperty(Validator.prototype, "offsetLimit", {
111 get: function() { return this.message.buffer.byteLength; } 126 get: function() { return this.message.buffer.byteLength; }
112 }); 127 });
113 128
114 Object.defineProperty(Validator.prototype, "handleIndexLimit", { 129 Object.defineProperty(Validator.prototype, "handleIndexLimit", {
115 get: function() { return this.message.handles.length; } 130 get: function() { return this.message.handles.length; }
116 }); 131 });
117 132
133 Object.defineProperty(Validator.prototype, "associatedHandleIndexLimit", {
134 get: function() { return this.message.getPayloadInterfaceIds().length; }
yzshen1 2017/04/11 06:23:11 getPayloadInterfaceIds() may throw exception (if m
wangjimmy 2017/04/12 00:26:17 Done.
135 });
136
118 // True if we can safely allocate a block of bytes from start to 137 // True if we can safely allocate a block of bytes from start to
119 // to start + numBytes. 138 // to start + numBytes.
120 Validator.prototype.isValidRange = function(start, numBytes) { 139 Validator.prototype.isValidRange = function(start, numBytes) {
121 // Only positive JavaScript integers that are less than 2^53 140 // Only positive JavaScript integers that are less than 2^53
122 // (Number.MAX_SAFE_INTEGER) can be represented exactly. 141 // (Number.MAX_SAFE_INTEGER) can be represented exactly.
123 if (start < this.offset || numBytes <= 0 || 142 if (start < this.offset || numBytes <= 0 ||
124 !Number.isSafeInteger(start) || 143 !Number.isSafeInteger(start) ||
125 !Number.isSafeInteger(numBytes)) 144 !Number.isSafeInteger(numBytes))
126 return false; 145 return false;
127 146
(...skipping 17 matching lines...) Expand all
145 return true; 164 return true;
146 165
147 if (index < this.handleIndex || index >= this.handleIndexLimit) 166 if (index < this.handleIndex || index >= this.handleIndexLimit)
148 return false; 167 return false;
149 168
150 // This is safe because handle indices are uint32. 169 // This is safe because handle indices are uint32.
151 this.handleIndex = index + 1; 170 this.handleIndex = index + 1;
152 return true; 171 return true;
153 }; 172 };
154 173
174 Validator.prototype.claimAssociatedEndpointHandle = function(index) {
175 if (index === codec.kEncodedInvalidHandleValue) {
176 return true;
177 }
178
179 if (index < this.associatedEndpointHandleIndex ||
180 index >= this.associatedHandleIndexLimit) {
yzshen1 2017/04/11 06:23:11 Imagine the message has a null payload interface I
wangjimmy 2017/04/12 00:26:17 Acknowledged.
181 return false;
182 }
183
184 // This is safe because handle indices are uint32.
185 this.associatedEndpointHandleIndex = index + 1;
186 return true;
187 };
188
155 Validator.prototype.validateEnum = function(offset, enumClass) { 189 Validator.prototype.validateEnum = function(offset, enumClass) {
156 // Note: Assumes that enums are always 32 bits! But this matches 190 // Note: Assumes that enums are always 32 bits! But this matches
157 // mojom::generate::pack::PackedField::GetSizeForKind, so it should be okay. 191 // mojom::generate::pack::PackedField::GetSizeForKind, so it should be okay.
158 var value = this.message.buffer.getInt32(offset); 192 var value = this.message.buffer.getInt32(offset);
159 return enumClass.validate(value); 193 return enumClass.validate(value);
160 } 194 }
161 195
162 Validator.prototype.validateHandle = function(offset, nullable) { 196 Validator.prototype.validateHandle = function(offset, nullable) {
163 var index = this.message.buffer.getUint32(offset); 197 var index = this.message.buffer.getUint32(offset);
164 198
165 if (index === codec.kEncodedInvalidHandleValue) 199 if (index === codec.kEncodedInvalidHandleValue)
166 return nullable ? 200 return nullable ?
167 validationError.NONE : validationError.UNEXPECTED_INVALID_HANDLE; 201 validationError.NONE : validationError.UNEXPECTED_INVALID_HANDLE;
168 202
169 if (!this.claimHandle(index)) 203 if (!this.claimHandle(index))
170 return validationError.ILLEGAL_HANDLE; 204 return validationError.ILLEGAL_HANDLE;
171 205
172 return validationError.NONE; 206 return validationError.NONE;
173 }; 207 };
174 208
209 Validator.prototype.validateAssociatedEndpointHandle = function(offset,
210 nullable) {
211 var index = this.message.buffer.getUint32(offset);
212
213 if (index === codec.kEncodedInvalidHandleValue) {
214 return nullable ? validationError.NONE :
215 validationError.UNEXPECTED_INVALID_INTERFACE_ID;
216 }
217
218 if (!this.claimAssociatedEndpointHandle(index)) {
219 return validationError.ILLEGAL_INTERFACE_ID;
220 }
221
222 return validationError.NONE;
223 };
224
175 Validator.prototype.validateInterface = function(offset, nullable) { 225 Validator.prototype.validateInterface = function(offset, nullable) {
176 return this.validateHandle(offset, nullable); 226 return this.validateHandle(offset, nullable);
177 }; 227 };
178 228
179 Validator.prototype.validateInterfaceRequest = function(offset, nullable) { 229 Validator.prototype.validateInterfaceRequest = function(offset, nullable) {
180 return this.validateHandle(offset, nullable); 230 return this.validateHandle(offset, nullable);
181 }; 231 };
182 232
233 Validator.prototype.validateAssociatedInterface = function(offset,
234 nullable) {
235 return this.validateAssociatedEndpointHandle(offset, nullable);
236 };
237
238 Validator.prototype.validateAssociatedInterfaceRequest = function(
239 offset, nullable) {
240 return this.validateAssociatedEndpointHandle(offset, nullable);
241 };
242
183 Validator.prototype.validateStructHeader = function(offset, minNumBytes) { 243 Validator.prototype.validateStructHeader = function(offset, minNumBytes) {
184 if (!codec.isAligned(offset)) 244 if (!codec.isAligned(offset))
185 return validationError.MISALIGNED_OBJECT; 245 return validationError.MISALIGNED_OBJECT;
186 246
187 if (!this.isValidRange(offset, codec.kStructHeaderSize)) 247 if (!this.isValidRange(offset, codec.kStructHeaderSize))
188 return validationError.ILLEGAL_MEMORY_RANGE; 248 return validationError.ILLEGAL_MEMORY_RANGE;
189 249
190 var numBytes = this.message.buffer.getUint32(offset); 250 var numBytes = this.message.buffer.getUint32(offset);
191 251
192 if (numBytes < minNumBytes) 252 if (numBytes < minNumBytes)
(...skipping 23 matching lines...) Expand all
216 } 276 }
217 277
218 return validationError.NONE; 278 return validationError.NONE;
219 }; 279 };
220 280
221 Validator.prototype.isFieldInStructVersion = function(offset, fieldVersion) { 281 Validator.prototype.isFieldInStructVersion = function(offset, fieldVersion) {
222 var structVersion = this.message.buffer.getUint32(offset + 4); 282 var structVersion = this.message.buffer.getUint32(offset + 4);
223 return fieldVersion <= structVersion; 283 return fieldVersion <= structVersion;
224 }; 284 };
225 285
226 // TODO(wangjimmy): Add support for v2 messages.
227 Validator.prototype.validateMessageHeader = function() { 286 Validator.prototype.validateMessageHeader = function() {
228 287
229 var err = this.validateStructHeader(0, codec.kMessageHeaderSize); 288 var err = this.validateStructHeader(0, codec.kMessageHeaderSize);
230 if (err != validationError.NONE) 289 if (err != validationError.NONE)
231 return err; 290 return err;
232 291
233 var numBytes = this.message.getHeaderNumBytes(); 292 var numBytes = this.message.getHeaderNumBytes();
234 var version = this.message.getHeaderVersion(); 293 var version = this.message.getHeaderVersion();
235 294
236 var validVersionAndNumBytes = 295 var validVersionAndNumBytes =
237 (version == 0 && numBytes == codec.kMessageHeaderSize) || 296 (version == 0 && numBytes == codec.kMessageHeaderSize) ||
238 (version == 1 && 297 (version == 1 &&
239 numBytes == codec.kMessageWithRequestIDHeaderSize) || 298 numBytes == codec.kMessageWithRequestIDHeaderSize) ||
240 (version > 1 && 299 (version > 1 &&
yzshen1 2017/04/11 06:23:11 To be exact: ... || (version == 2 && numBytes == c
wangjimmy 2017/04/12 00:26:17 Done.
241 numBytes >= codec.kMessageWithRequestIDHeaderSize); 300 numBytes >= codec.kMessageV2HeaderSize);
242 if (!validVersionAndNumBytes) 301 if (!validVersionAndNumBytes)
243 return validationError.UNEXPECTED_STRUCT_HEADER; 302 return validationError.UNEXPECTED_STRUCT_HEADER;
244 303
245 var expectsResponse = this.message.expectsResponse(); 304 var expectsResponse = this.message.expectsResponse();
246 var isResponse = this.message.isResponse(); 305 var isResponse = this.message.isResponse();
247 306
248 if (version == 0 && (expectsResponse || isResponse)) 307 if (version == 0 && (expectsResponse || isResponse))
249 return validationError.MESSAGE_HEADER_MISSING_REQUEST_ID; 308 return validationError.MESSAGE_HEADER_MISSING_REQUEST_ID;
250 309
251 if (isResponse && expectsResponse) 310 if (isResponse && expectsResponse)
252 return validationError.MESSAGE_HEADER_INVALID_FLAGS; 311 return validationError.MESSAGE_HEADER_INVALID_FLAGS;
253 312
313 if (version < 2) {
314 return validationError.NONE;
315 }
316
317 if (this.message.getPayloadInterfaceIds()) {
yzshen1 2017/04/11 06:23:11 - I think you need to validate the array because t
wangjimmy 2017/04/12 00:26:17 Done.
318 for (var interfaceId of this.message.getPayloadInterfaceIds()) {
319 if (!types.isValidInterfaceId(interfaceId) ||
320 types.isMasterInterfaceId(interfaceId)) {
321 return validationError.ILLEGAL_INTERFACE_ID;
322 }
323 }
324 }
325
254 return validationError.NONE; 326 return validationError.NONE;
255 }; 327 };
256 328
257 Validator.prototype.validateMessageIsRequestWithoutResponse = function() { 329 Validator.prototype.validateMessageIsRequestWithoutResponse = function() {
258 if (this.message.isResponse() || this.message.expectsResponse()) { 330 if (this.message.isResponse() || this.message.expectsResponse()) {
259 return validationError.MESSAGE_HEADER_INVALID_FLAGS; 331 return validationError.MESSAGE_HEADER_INVALID_FLAGS;
260 } 332 }
261 return validationError.NONE; 333 return validationError.NONE;
262 }; 334 };
263 335
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 var nullable = isNullable(elementType); 516 var nullable = isNullable(elementType);
445 517
446 if (isHandleClass(elementType)) 518 if (isHandleClass(elementType))
447 return this.validateHandleElements(elementsOffset, numElements, nullable); 519 return this.validateHandleElements(elementsOffset, numElements, nullable);
448 if (isInterfaceClass(elementType)) 520 if (isInterfaceClass(elementType))
449 return this.validateInterfaceElements( 521 return this.validateInterfaceElements(
450 elementsOffset, numElements, nullable); 522 elementsOffset, numElements, nullable);
451 if (isInterfaceRequestClass(elementType)) 523 if (isInterfaceRequestClass(elementType))
452 return this.validateInterfaceRequestElements( 524 return this.validateInterfaceRequestElements(
453 elementsOffset, numElements, nullable); 525 elementsOffset, numElements, nullable);
526 if (isAssociatedInterfaceClass(elementType))
527 return this.validateAssociatedInterfaceElements(
528 elementsOffset, numElements, nullable);
529 if (isAssociatedInterfaceRequestClass(elementType))
530 return this.validateAssociatedInterfaceRequestElements(
531 elementsOffset, numElements, nullable);
454 if (isStringClass(elementType)) 532 if (isStringClass(elementType))
455 return this.validateArrayElements( 533 return this.validateArrayElements(
456 elementsOffset, numElements, codec.Uint8, nullable, [0], 0); 534 elementsOffset, numElements, codec.Uint8, nullable, [0], 0);
457 if (elementType instanceof codec.PointerTo) 535 if (elementType instanceof codec.PointerTo)
458 return this.validateStructElements( 536 return this.validateStructElements(
459 elementsOffset, numElements, elementType.cls, nullable); 537 elementsOffset, numElements, elementType.cls, nullable);
460 if (elementType instanceof codec.ArrayOf) 538 if (elementType instanceof codec.ArrayOf)
461 return this.validateArrayElements( 539 return this.validateArrayElements(
462 elementsOffset, numElements, elementType.cls, nullable, 540 elementsOffset, numElements, elementType.cls, nullable,
463 expectedDimensionSizes, currentDimension + 1); 541 expectedDimensionSizes, currentDimension + 1);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 var elementSize = codec.InterfaceRequest.encodedSize; 579 var elementSize = codec.InterfaceRequest.encodedSize;
502 for (var i = 0; i < numElements; i++) { 580 for (var i = 0; i < numElements; i++) {
503 var elementOffset = offset + i * elementSize; 581 var elementOffset = offset + i * elementSize;
504 var err = this.validateInterfaceRequest(elementOffset, nullable); 582 var err = this.validateInterfaceRequest(elementOffset, nullable);
505 if (err != validationError.NONE) 583 if (err != validationError.NONE)
506 return err; 584 return err;
507 } 585 }
508 return validationError.NONE; 586 return validationError.NONE;
509 }; 587 };
510 588
589 Validator.prototype.validateAssociatedInterfaceElements =
590 function(offset, numElements, nullable) {
591 var elementSize = codec.AssociatedInterfacePtrInfo.prototype.encodedSize;
592 for (var i = 0; i < numElements; i++) {
593 var elementOffset = offset + i * elementSize;
594 var err = this.validateAssociatedInterface(elementOffset, nullable);
595 if (err != validationError.NONE) {
596 return err;
597 }
598 }
599 return validationError.NONE;
600 };
601
602 Validator.prototype.validateAssociatedInterfaceRequestElements =
603 function(offset, numElements, nullable) {
604 var elementSize = codec.AssociatedInterfaceRequest.encodedSize;
605 for (var i = 0; i < numElements; i++) {
606 var elementOffset = offset + i * elementSize;
607 var err = this.validateAssociatedInterfaceRequest(elementOffset,
608 nullable);
609 if (err != validationError.NONE) {
610 return err;
611 }
612 }
613 return validationError.NONE;
614 };
615
511 // The elementClass parameter is the element type of the element arrays. 616 // The elementClass parameter is the element type of the element arrays.
512 Validator.prototype.validateArrayElements = 617 Validator.prototype.validateArrayElements =
513 function(offset, numElements, elementClass, nullable, 618 function(offset, numElements, elementClass, nullable,
514 expectedDimensionSizes, currentDimension) { 619 expectedDimensionSizes, currentDimension) {
515 var elementSize = codec.PointerTo.prototype.encodedSize; 620 var elementSize = codec.PointerTo.prototype.encodedSize;
516 for (var i = 0; i < numElements; i++) { 621 for (var i = 0; i < numElements; i++) {
517 var elementOffset = offset + i * elementSize; 622 var elementOffset = offset + i * elementSize;
518 var err = this.validateArrayPointer( 623 var err = this.validateArrayPointer(
519 elementOffset, elementClass.encodedSize, elementClass, nullable, 624 elementOffset, elementClass.encodedSize, elementClass, nullable,
520 expectedDimensionSizes, currentDimension); 625 expectedDimensionSizes, currentDimension);
(...skipping 30 matching lines...) Expand all
551 656
552 var exports = {}; 657 var exports = {};
553 exports.validationError = validationError; 658 exports.validationError = validationError;
554 exports.Validator = Validator; 659 exports.Validator = Validator;
555 exports.ValidationErrorObserverForTesting = ValidationErrorObserverForTesting; 660 exports.ValidationErrorObserverForTesting = ValidationErrorObserverForTesting;
556 exports.reportValidationError = reportValidationError; 661 exports.reportValidationError = reportValidationError;
557 exports.isTestingMode = isTestingMode; 662 exports.isTestingMode = isTestingMode;
558 exports.clearTestingMode = clearTestingMode; 663 exports.clearTestingMode = clearTestingMode;
559 return exports; 664 return exports;
560 }); 665 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698