| 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 <sstream> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "mojo/public/cpp/environment/logging.h" | |
| 12 #include "mojo/public/cpp/system/macros.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 namespace internal { | |
| 16 | |
| 17 enum class ValidationError { | |
| 18 // There is no validation error. | |
| 19 NONE, | |
| 20 // An object (struct or array) is not 8-byte aligned. | |
| 21 MISALIGNED_OBJECT, | |
| 22 // An object is not contained inside the message data, or it overlaps other | |
| 23 // objects. | |
| 24 ILLEGAL_MEMORY_RANGE, | |
| 25 // A struct header doesn't make sense, for example: | |
| 26 // - |num_bytes| is smaller than the size of the struct header. | |
| 27 // - |num_bytes| and |version| don't match. | |
| 28 // TODO(yzshen): Consider splitting it into two different error codes. Because | |
| 29 // the former indicates someone is misbehaving badly whereas the latter could | |
| 30 // be due to an inappropriately-modified .mojom file. | |
| 31 UNEXPECTED_STRUCT_HEADER, | |
| 32 // An array header doesn't make sense, for example: | |
| 33 // - |num_bytes| is smaller than the size of the header plus the size required | |
| 34 // to store |num_elements| elements. | |
| 35 // - For fixed-size arrays, |num_elements| is different than the specified | |
| 36 // size. | |
| 37 UNEXPECTED_ARRAY_HEADER, | |
| 38 // An encoded handle is illegal. | |
| 39 ILLEGAL_HANDLE, | |
| 40 // A non-nullable handle field is set to invalid handle. | |
| 41 UNEXPECTED_INVALID_HANDLE, | |
| 42 // An encoded pointer is illegal. | |
| 43 ILLEGAL_POINTER, | |
| 44 // A non-nullable pointer field is set to null. | |
| 45 UNEXPECTED_NULL_POINTER, | |
| 46 // |flags| in the message header is invalid. The flags are either | |
| 47 // inconsistent with one another, inconsistent with other parts of the | |
| 48 // message, or unexpected for the message receiver. For example the | |
| 49 // receiver is expecting a request message but the flags indicate that | |
| 50 // the message is a response message. | |
| 51 MESSAGE_HEADER_INVALID_FLAGS, | |
| 52 // |flags| in the message header indicates that a request ID is required but | |
| 53 // there isn't one. | |
| 54 MESSAGE_HEADER_MISSING_REQUEST_ID, | |
| 55 // The |name| field in a message header contains an unexpected value. | |
| 56 MESSAGE_HEADER_UNKNOWN_METHOD, | |
| 57 // Two parallel arrays which are supposed to represent a map have different | |
| 58 // lengths. | |
| 59 DIFFERENT_SIZED_ARRAYS_IN_MAP, | |
| 60 // A non-nullable union is set to null. (Has size 0) | |
| 61 UNEXPECTED_NULL_UNION, | |
| 62 }; | |
| 63 | |
| 64 const char* ValidationErrorToString(ValidationError error); | |
| 65 | |
| 66 // TODO(vardhan): This can die, along with |ValidationErrorObserverForTesting|. | |
| 67 void ReportValidationError(ValidationError error, | |
| 68 std::string* description = nullptr); | |
| 69 | |
| 70 // Only used by validation tests and when there is only one thread doing message | |
| 71 // validation. | |
| 72 class ValidationErrorObserverForTesting { | |
| 73 public: | |
| 74 ValidationErrorObserverForTesting(); | |
| 75 ~ValidationErrorObserverForTesting(); | |
| 76 | |
| 77 ValidationError last_error() const { return last_error_; } | |
| 78 void set_last_error(ValidationError error) { last_error_ = error; } | |
| 79 | |
| 80 private: | |
| 81 ValidationError last_error_; | |
| 82 | |
| 83 MOJO_DISALLOW_COPY_AND_ASSIGN(ValidationErrorObserverForTesting); | |
| 84 }; | |
| 85 | |
| 86 // This takes in a string pointer, and provides a string stream which you can | |
| 87 // write to. On destruction, it sets the provided string to the contents of the | |
| 88 // string stream. | |
| 89 class ValidationErrorStringStream { | |
| 90 public: | |
| 91 explicit ValidationErrorStringStream(std::string* err_msg); | |
| 92 ~ValidationErrorStringStream(); | |
| 93 std::ostringstream& stream() { return stream_; } | |
| 94 | |
| 95 private: | |
| 96 std::string* err_msg_; | |
| 97 std::ostringstream stream_; | |
| 98 | |
| 99 MOJO_DISALLOW_COPY_AND_ASSIGN(ValidationErrorStringStream); | |
| 100 }; | |
| 101 | |
| 102 } // namespace internal | |
| 103 } // namespace mojo | |
| 104 | |
| 105 // In a debug build, logs a serialization warning. | |
| 106 // TODO(vardhan): Make this work like an ostream. | |
| 107 #define MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(error, description) \ | |
| 108 MOJO_DLOG(WARNING) << "The outgoing message will trigger " \ | |
| 109 << ValidationErrorToString(error) \ | |
| 110 << " at the receiving side (" << description << ")." | |
| 111 | |
| 112 // In a debug build, this will use |ValidationErrorStringStream::stream()| and | |
| 113 // write to the supplied string if it is not null. In a non-debug + optimized | |
| 114 // build it should do nothing, while also discarding away operator<<() calls. | |
| 115 #ifdef NDEBUG | |
| 116 // The compiler will reduce the |true ? (void)0 : ...| expression to |(void)0|, | |
| 117 // thereby discarding the |ValidationErrorStringStream()| while still keeping | |
| 118 // this macro's use semantically valid. | |
| 119 #define MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err_msg) \ | |
| 120 true ? (void)0 \ | |
| 121 : ::mojo::internal::VoidifyOstream() & \ | |
| 122 ::mojo::internal::ValidationErrorStringStream(err_msg).stream() | |
| 123 #else | |
| 124 #define MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err_msg) \ | |
| 125 ::mojo::internal::ValidationErrorStringStream(err_msg).stream() | |
| 126 #endif // NDEBUG | |
| 127 | |
| 128 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_ERRORS_H_ | |
| OLD | NEW |