OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_ERRORS_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_ERRORS_H_ | |
7 | |
8 #include "mojo/public/cpp/system/macros.h" | |
9 | |
10 namespace mojo { | |
11 namespace internal { | |
12 | |
13 enum ValidationError { | |
14 // There is no validation error. | |
15 VALIDATION_ERROR_NONE, | |
16 // An object (struct or array) is not 8-byte aligned. | |
17 VALIDATION_ERROR_MISALIGNED_OBJECT, | |
18 // An object is not contained inside the message data, or it overlaps other | |
19 // objects. | |
20 VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE, | |
21 // A struct header doesn't make sense, for example: | |
22 // - |num_bytes| is smaller than the size of the oldest version that we | |
23 // support. | |
24 // - |num_fields| is smaller than the field number of the oldest version that | |
25 // we support. | |
26 // - |num_bytes| and |num_fields| don't match. | |
27 VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER, | |
28 // An array header doesn't make sense, for example: | |
29 // - |num_bytes| is smaller than the size of the header plus the size required | |
30 // to store |num_elements| elements. | |
31 // - For fixed-size arrays, |num_elements| is different than the specified | |
32 // size. | |
33 VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER, | |
34 // An encoded handle is illegal. | |
35 VALIDATION_ERROR_ILLEGAL_HANDLE, | |
36 // A non-nullable handle field is set to invalid handle. | |
37 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, | |
38 // An encoded pointer is illegal. | |
39 VALIDATION_ERROR_ILLEGAL_POINTER, | |
40 // A non-nullable pointer field is set to null. | |
41 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | |
42 // |flags| in the message header is an invalid flag combination. | |
43 VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAG_COMBINATION, | |
44 // |flags| in the message header indicates that a request ID is required but | |
45 // there isn't one. | |
46 VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID, | |
47 // Two parallel arrays which are supposed to represent a map have different | |
48 // lengths. | |
49 VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP | |
50 }; | |
51 | |
52 const char* ValidationErrorToString(ValidationError error); | |
53 | |
54 void ReportValidationError(ValidationError error, | |
55 const char* description = nullptr); | |
56 | |
57 // Only used by validation tests and when there is only one thread doing message | |
58 // validation. | |
59 class ValidationErrorObserverForTesting { | |
60 public: | |
61 ValidationErrorObserverForTesting(); | |
62 ~ValidationErrorObserverForTesting(); | |
63 | |
64 ValidationError last_error() const { return last_error_; } | |
65 void set_last_error(ValidationError error) { last_error_ = error; } | |
66 | |
67 private: | |
68 ValidationError last_error_; | |
69 | |
70 MOJO_DISALLOW_COPY_AND_ASSIGN(ValidationErrorObserverForTesting); | |
71 }; | |
72 | |
73 // Used only by MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING. Don't use it directly. | |
74 // | |
75 // The function returns true if the error is recorded (by a | |
76 // SerializationWarningObserverForTesting object), false otherwise. | |
77 bool ReportSerializationWarning(ValidationError error); | |
78 | |
79 // Only used by serialization tests and when there is only one thread doing | |
80 // message serialization. | |
81 class SerializationWarningObserverForTesting { | |
82 public: | |
83 SerializationWarningObserverForTesting(); | |
84 ~SerializationWarningObserverForTesting(); | |
85 | |
86 ValidationError last_warning() const { return last_warning_; } | |
87 void set_last_warning(ValidationError error) { last_warning_ = error; } | |
88 | |
89 private: | |
90 ValidationError last_warning_; | |
91 | |
92 MOJO_DISALLOW_COPY_AND_ASSIGN(SerializationWarningObserverForTesting); | |
93 }; | |
94 | |
95 } // namespace internal | |
96 } // namespace mojo | |
97 | |
98 // In debug build, logs a serialization warning if |condition| evaluates to | |
99 // true: | |
100 // - if there is a SerializationWarningObserverForTesting object alive, | |
101 // records |error| in it; | |
102 // - otherwise, logs a fatal-level message. | |
103 // |error| is the validation error that will be triggered by the receiver | |
104 // of the serialzation result. | |
105 // | |
106 // In non-debug build, does nothing (not even compiling |condition|). | |
107 #define MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( \ | |
108 condition, error, description) \ | |
109 MOJO_DLOG_IF(FATAL, (condition) && !ReportSerializationWarning(error)) \ | |
110 << "The outgoing message will trigger " \ | |
111 << ValidationErrorToString(error) << " at the receiving side (" \ | |
112 << description << ")."; | |
113 | |
114 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_ERRORS_H_ | |
OLD | NEW |