Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_HARDWARE_BINDER_STATUS_H |
| 18 | #define ANDROID_HARDWARE_BINDER_STATUS_H |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <sstream> |
| 22 | |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame] | 23 | #include <hidl/HidlInternal.h> |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 24 | #include <utils/Errors.h> |
Martijn Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 25 | #include <utils/StrongPointer.h> |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | |
Steven Moreland | 779a714 | 2019-03-05 16:59:26 -0800 | [diff] [blame] | 30 | // HIDL formally separates transport error codes from interface error codes. When developing a HIDL |
| 31 | // interface, errors relevant to a service should be placed in the interface design for that HAL. |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 32 | // |
Steven Moreland | 779a714 | 2019-03-05 16:59:26 -0800 | [diff] [blame] | 33 | // For instance: |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 34 | // |
Steven Moreland | 779a714 | 2019-03-05 16:59:26 -0800 | [diff] [blame] | 35 | // interface I* { |
| 36 | // enum FooStatus { NO_FOO, NO_BAR }; // service-specific errors |
| 37 | // doFoo(...) generates (FooStatus foo); |
| 38 | // }; |
| 39 | // |
| 40 | // When calling into this interface, a Return<*> (in this case Return<FooStatus> object will be |
| 41 | // returned). For most clients, it's expected that they'll just get the result from this function |
| 42 | // and use it directly. If there is a transport error, the process will just abort. In general, |
| 43 | // transport errors are expected only in extremely rare circumstances (bug in the |
| 44 | // code/cosmic radiation/etc..). Aborting allows process to restart using their normal happy path |
| 45 | // code. |
| 46 | // |
| 47 | // For certain processes though which are critical to the functionality of the phone (e.g. |
| 48 | // hwservicemanager/init), these errors must be handled. Return<*>::isOk and |
| 49 | // Return<*>::isDeadObject are provided for these cases. Whenever this is done, special attention |
| 50 | // should be paid to testing the unhappy paths to make sure that error handling is handled |
| 51 | // properly. |
| 52 | |
| 53 | // Transport implementation detail. HIDL implementors, see Return below. HAL implementations should |
| 54 | // return HIDL-defined errors rather than use this. |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 55 | class Status final { |
| 56 | public: |
Steven Moreland | 779a714 | 2019-03-05 16:59:26 -0800 | [diff] [blame] | 57 | // Note: forked from |
| 58 | // - frameworks/base/core/java/android/os/android/os/Parcel.java. |
| 59 | // - frameworks/native/libs/binder/include/binder/Status.h |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 60 | enum Exception { |
| 61 | EX_NONE = 0, |
| 62 | EX_SECURITY = -1, |
| 63 | EX_BAD_PARCELABLE = -2, |
| 64 | EX_ILLEGAL_ARGUMENT = -3, |
| 65 | EX_NULL_POINTER = -4, |
| 66 | EX_ILLEGAL_STATE = -5, |
| 67 | EX_NETWORK_MAIN_THREAD = -6, |
| 68 | EX_UNSUPPORTED_OPERATION = -7, |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 69 | |
| 70 | // This is special and Java specific; see Parcel.java. |
| 71 | EX_HAS_REPLY_HEADER = -128, |
| 72 | // This is special, and indicates to C++ binder proxies that the |
| 73 | // transaction has failed at a low level. |
| 74 | EX_TRANSACTION_FAILED = -129, |
| 75 | }; |
| 76 | |
| 77 | // A more readable alias for the default constructor. |
| 78 | static Status ok(); |
| 79 | // Authors should explicitly pick whether their integer is: |
| 80 | // - an exception code (EX_* above) |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 81 | // - status_t |
| 82 | // |
Steven Moreland | 72db40f | 2017-03-09 18:15:27 -0800 | [diff] [blame] | 83 | // Prefer a generic exception code when possible or a status_t |
| 84 | // for low level transport errors. Service specific errors |
| 85 | // should be at a higher level in HIDL. |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 86 | static Status fromExceptionCode(int32_t exceptionCode); |
| 87 | static Status fromExceptionCode(int32_t exceptionCode, |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 88 | const char *message); |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 89 | static Status fromStatusT(status_t status); |
| 90 | |
| 91 | Status() = default; |
| 92 | ~Status() = default; |
| 93 | |
| 94 | // Status objects are copyable and contain just simple data. |
| 95 | Status(const Status& status) = default; |
| 96 | Status(Status&& status) = default; |
| 97 | Status& operator=(const Status& status) = default; |
| 98 | |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 99 | // Set one of the pre-defined exception types defined above. |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 100 | void setException(int32_t ex, const char *message); |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 101 | // Setting a |status| != OK causes generated code to return |status| |
| 102 | // from Binder transactions, rather than writing an exception into the |
| 103 | // reply Parcel. This is the least preferable way of reporting errors. |
| 104 | void setFromStatusT(status_t status); |
| 105 | |
| 106 | // Get information about an exception. |
| 107 | int32_t exceptionCode() const { return mException; } |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 108 | const char *exceptionMessage() const { return mMessage.c_str(); } |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 109 | status_t transactionError() const { |
| 110 | return mException == EX_TRANSACTION_FAILED ? mErrorCode : OK; |
| 111 | } |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 112 | |
| 113 | bool isOk() const { return mException == EX_NONE; } |
| 114 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 115 | // For debugging purposes only |
| 116 | std::string description() const; |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | Status(int32_t exceptionCode, int32_t errorCode); |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 120 | Status(int32_t exceptionCode, int32_t errorCode, const char *message); |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 121 | |
| 122 | // If |mException| == EX_TRANSACTION_FAILED, generated code will return |
| 123 | // |mErrorCode| as the result of the transaction rather than write an |
| 124 | // exception to the reply parcel. |
| 125 | // |
| 126 | // Otherwise, we always write |mException| to the parcel. |
| 127 | // If |mException| != EX_NONE, we write |mMessage| as well. |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 128 | int32_t mException = EX_NONE; |
| 129 | int32_t mErrorCode = 0; |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 130 | std::string mMessage; |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 131 | }; // class Status |
| 132 | |
| 133 | // For gtest output logging |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 134 | std::ostream& operator<< (std::ostream& stream, const Status& s); |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 135 | |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame] | 136 | template<typename T> class Return; |
| 137 | |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 138 | namespace details { |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 139 | class return_status { |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 140 | private: |
| 141 | Status mStatus {}; |
| 142 | mutable bool mCheckedStatus = false; |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame] | 143 | |
| 144 | template <typename T, typename U> |
| 145 | friend Return<U> StatusOf(const Return<T> &other); |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 146 | public: |
Steven Moreland | eda1f92 | 2019-03-05 19:37:51 -0800 | [diff] [blame^] | 147 | void assertOk() const; |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 148 | return_status() {} |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 149 | return_status(const Status& s) : mStatus(s) {} |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 150 | |
Yifan Hong | 603cde9 | 2017-02-17 14:50:44 -0800 | [diff] [blame] | 151 | return_status(const return_status &) = delete; |
| 152 | return_status &operator=(const return_status &) = delete; |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 153 | |
Chih-Hung Hsieh | 3833f20 | 2018-09-25 12:03:06 -0700 | [diff] [blame] | 154 | return_status(return_status&& other) noexcept { *this = std::move(other); } |
| 155 | return_status& operator=(return_status&& other) noexcept; |
Yifan Hong | c43bd9d | 2017-02-17 14:11:48 -0800 | [diff] [blame] | 156 | |
Steven Moreland | e780c45 | 2017-01-17 17:22:54 -0800 | [diff] [blame] | 157 | ~return_status(); |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 158 | |
Steven Moreland | ff189a0 | 2017-09-15 16:22:48 -0700 | [diff] [blame] | 159 | bool isOkUnchecked() const { |
| 160 | // someone else will have to check |
| 161 | return mStatus.isOk(); |
| 162 | } |
| 163 | |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 164 | bool isOk() const { |
| 165 | mCheckedStatus = true; |
| 166 | return mStatus.isOk(); |
| 167 | } |
| 168 | |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame] | 169 | // Check if underlying error is DEAD_OBJECT. |
Steven Moreland | 13f7f62 | 2017-06-19 18:02:27 -0700 | [diff] [blame] | 170 | // Check mCheckedStatus only if this method returns true. |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame] | 171 | bool isDeadObject() const { |
Steven Moreland | 13f7f62 | 2017-06-19 18:02:27 -0700 | [diff] [blame] | 172 | bool dead = mStatus.transactionError() == DEAD_OBJECT; |
| 173 | |
| 174 | // This way, if you only check isDeadObject your process will |
| 175 | // only be killed for more serious unchecked errors |
| 176 | if (dead) { |
| 177 | mCheckedStatus = true; |
| 178 | } |
| 179 | |
| 180 | return dead; |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Yifan Hong | 43298f9 | 2016-12-20 16:42:39 -0800 | [diff] [blame] | 183 | // For debugging purposes only |
| 184 | std::string description() const { |
| 185 | // Doesn't consider checked. |
| 186 | return mStatus.description(); |
| 187 | } |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 188 | }; |
| 189 | } // namespace details |
| 190 | |
Steven Moreland | eda1f92 | 2019-03-05 19:37:51 -0800 | [diff] [blame^] | 191 | enum class HidlReturnRestriction { |
| 192 | // Okay to ignore checking transport errors. This would instead rely on init to reset state |
| 193 | // after an error in the underlying transport. This is the default and expected for most |
| 194 | // usecases. |
| 195 | NONE, |
| 196 | // Log when there is an unchecked error. |
| 197 | ERROR_IF_UNCHECKED, |
| 198 | // Fatal when there is an unchecked error. |
| 199 | FATAL_IF_UNCHECKED, |
| 200 | }; |
| 201 | |
| 202 | /** |
| 203 | * This should be called during process initialization (e.g. before binder threadpool is created). |
| 204 | * |
| 205 | * Note: default of HidlReturnRestriction::NONE should be good for most usecases. See above. |
| 206 | * |
| 207 | * The restriction will be applied when Return objects are deconstructed. |
| 208 | */ |
| 209 | void setProcessHidlReturnRestriction(HidlReturnRestriction restriction); |
| 210 | |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 211 | template<typename T> class Return : public details::return_status { |
Iliyan Malchev | 170c189 | 2016-09-08 13:55:53 -0700 | [diff] [blame] | 212 | private: |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 213 | T mVal {}; |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 214 | public: |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 215 | Return(T v) : details::return_status(), mVal{v} {} |
| 216 | Return(Status s) : details::return_status(s) {} |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 217 | |
Yifan Hong | c43bd9d | 2017-02-17 14:11:48 -0800 | [diff] [blame] | 218 | // move-able. |
| 219 | // precondition: "this" has checked status |
| 220 | // postcondition: other is safe to destroy after moving to *this. |
Chih-Hung Hsieh | 3833f20 | 2018-09-25 12:03:06 -0700 | [diff] [blame] | 221 | Return(Return&& other) noexcept = default; |
| 222 | Return& operator=(Return&&) noexcept = default; |
Yifan Hong | c43bd9d | 2017-02-17 14:11:48 -0800 | [diff] [blame] | 223 | |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 224 | ~Return() = default; |
Steven Moreland | 1cfaee7 | 2016-12-01 08:34:19 -0800 | [diff] [blame] | 225 | |
| 226 | operator T() const { |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame] | 227 | assertOk(); |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 228 | return mVal; |
| 229 | } |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 230 | |
Steven Moreland | 4917296 | 2017-04-28 16:08:43 -0700 | [diff] [blame] | 231 | T withDefault(T t) { |
| 232 | return isOk() ? mVal : t; |
| 233 | } |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 234 | }; |
| 235 | |
Martijn Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 236 | template<typename T> class Return<sp<T>> : public details::return_status { |
| 237 | private: |
| 238 | sp<T> mVal {}; |
| 239 | public: |
| 240 | Return(sp<T> v) : details::return_status(), mVal{v} {} |
| 241 | Return(T* v) : details::return_status(), mVal{v} {} |
| 242 | // Constructors matching a different type (that is related by inheritance) |
| 243 | template<typename U> Return(sp<U> v) : details::return_status(), mVal{v} {} |
| 244 | template<typename U> Return(U* v) : details::return_status(), mVal{v} {} |
| 245 | Return(Status s) : details::return_status(s) {} |
| 246 | |
Yifan Hong | c43bd9d | 2017-02-17 14:11:48 -0800 | [diff] [blame] | 247 | // move-able. |
| 248 | // precondition: "this" has checked status |
| 249 | // postcondition: other is safe to destroy after moving to *this. |
Chih-Hung Hsieh | 3833f20 | 2018-09-25 12:03:06 -0700 | [diff] [blame] | 250 | Return(Return&& other) noexcept = default; |
| 251 | Return& operator=(Return&&) noexcept = default; |
Yifan Hong | c43bd9d | 2017-02-17 14:11:48 -0800 | [diff] [blame] | 252 | |
Martijn Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 253 | ~Return() = default; |
| 254 | |
| 255 | operator sp<T>() const { |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame] | 256 | assertOk(); |
Martijn Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 257 | return mVal; |
| 258 | } |
Steven Moreland | 4917296 | 2017-04-28 16:08:43 -0700 | [diff] [blame] | 259 | |
| 260 | sp<T> withDefault(sp<T> t) { |
| 261 | return isOk() ? mVal : t; |
| 262 | } |
Martijn Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 266 | template<> class Return<void> : public details::return_status { |
Iliyan Malchev | 170c189 | 2016-09-08 13:55:53 -0700 | [diff] [blame] | 267 | public: |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 268 | Return() : details::return_status() {} |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 269 | Return(const Status& s) : details::return_status(s) {} |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 270 | |
Yifan Hong | c43bd9d | 2017-02-17 14:11:48 -0800 | [diff] [blame] | 271 | // move-able. |
| 272 | // precondition: "this" has checked status |
| 273 | // postcondition: other is safe to destroy after moving to *this. |
| 274 | Return(Return &&) = default; |
| 275 | Return &operator=(Return &&) = default; |
| 276 | |
Steven Moreland | 7596933 | 2016-12-12 16:00:25 -0800 | [diff] [blame] | 277 | ~Return() = default; |
Iliyan Malchev | 170c189 | 2016-09-08 13:55:53 -0700 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | static inline Return<void> Void() { |
| 281 | return Return<void>(); |
| 282 | } |
| 283 | |
Yifan Hong | a7b2bb7 | 2017-03-29 03:36:07 -0700 | [diff] [blame] | 284 | namespace details { |
| 285 | // Create a Return<U> from the Status of Return<T>. The provided |
| 286 | // Return<T> must have an error status and have it checked. |
| 287 | template <typename T, typename U> |
| 288 | Return<U> StatusOf(const Return<T> &other) { |
| 289 | if (other.mStatus.isOk() || !other.mCheckedStatus) { |
| 290 | details::logAlwaysFatal("cannot call statusOf on an OK Status or an unchecked status"); |
| 291 | } |
| 292 | return Return<U>{other.mStatus}; |
| 293 | } |
| 294 | } // namespace details |
| 295 | |
Martijn Coenen | bb5e9bb | 2016-09-01 01:36:18 +0200 | [diff] [blame] | 296 | } // namespace hardware |
| 297 | } // namespace android |
| 298 | |
| 299 | #endif // ANDROID_HARDWARE_BINDER_STATUS_H |