Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [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 | #include <binder/Status.h> |
| 18 | |
| 19 | namespace android { |
| 20 | namespace binder { |
| 21 | |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 22 | Status Status::ok() { |
| 23 | return Status(); |
| 24 | } |
| 25 | |
| 26 | Status Status::fromExceptionCode(int32_t exceptionCode) { |
Steven Moreland | fb26e72 | 2018-10-05 13:43:24 -0700 | [diff] [blame] | 27 | if (exceptionCode == EX_TRANSACTION_FAILED) { |
| 28 | return Status(exceptionCode, FAILED_TRANSACTION); |
| 29 | } |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 30 | return Status(exceptionCode, OK); |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | Status Status::fromExceptionCode(int32_t exceptionCode, |
| 34 | const String8& message) { |
Steven Moreland | fb26e72 | 2018-10-05 13:43:24 -0700 | [diff] [blame] | 35 | if (exceptionCode == EX_TRANSACTION_FAILED) { |
| 36 | return Status(exceptionCode, FAILED_TRANSACTION, message); |
| 37 | } |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 38 | return Status(exceptionCode, OK, message); |
| 39 | } |
| 40 | |
Christopher Wiley | 1651ced | 2016-05-06 09:19:44 -0700 | [diff] [blame] | 41 | Status Status::fromExceptionCode(int32_t exceptionCode, |
| 42 | const char* message) { |
| 43 | return fromExceptionCode(exceptionCode, String8(message)); |
| 44 | } |
| 45 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 46 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) { |
| 47 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode); |
| 48 | } |
| 49 | |
| 50 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode, |
| 51 | const String8& message) { |
| 52 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Christopher Wiley | 1651ced | 2016-05-06 09:19:44 -0700 | [diff] [blame] | 55 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode, |
| 56 | const char* message) { |
| 57 | return fromServiceSpecificError(serviceSpecificErrorCode, String8(message)); |
| 58 | } |
| 59 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 60 | Status Status::fromStatusT(status_t status) { |
| 61 | Status ret; |
| 62 | ret.setFromStatusT(status); |
| 63 | return ret; |
| 64 | } |
| 65 | |
Jeongik Cha | 89b3f8b | 2019-03-18 14:38:33 +0900 | [diff] [blame] | 66 | std::string Status::exceptionToString(int32_t exceptionCode) { |
| 67 | switch (exceptionCode) { |
| 68 | #define EXCEPTION_TO_CASE(EXCEPTION) case EXCEPTION: return #EXCEPTION; |
| 69 | EXCEPTION_TO_CASE(EX_NONE) |
| 70 | EXCEPTION_TO_CASE(EX_SECURITY) |
| 71 | EXCEPTION_TO_CASE(EX_BAD_PARCELABLE) |
| 72 | EXCEPTION_TO_CASE(EX_ILLEGAL_ARGUMENT) |
| 73 | EXCEPTION_TO_CASE(EX_NULL_POINTER) |
| 74 | EXCEPTION_TO_CASE(EX_ILLEGAL_STATE) |
| 75 | EXCEPTION_TO_CASE(EX_NETWORK_MAIN_THREAD) |
| 76 | EXCEPTION_TO_CASE(EX_UNSUPPORTED_OPERATION) |
| 77 | EXCEPTION_TO_CASE(EX_SERVICE_SPECIFIC) |
| 78 | EXCEPTION_TO_CASE(EX_PARCELABLE) |
| 79 | EXCEPTION_TO_CASE(EX_HAS_REPLY_HEADER) |
| 80 | EXCEPTION_TO_CASE(EX_TRANSACTION_FAILED) |
| 81 | #undef EXCEPTION_TO_CASE |
| 82 | default: return std::to_string(exceptionCode); |
| 83 | } |
| 84 | } |
| 85 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 86 | Status::Status(int32_t exceptionCode, int32_t errorCode) |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 87 | : mException(exceptionCode), |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 88 | mErrorCode(errorCode) {} |
| 89 | |
| 90 | Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message) |
| 91 | : mException(exceptionCode), |
| 92 | mErrorCode(errorCode), |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 93 | mMessage(message) {} |
| 94 | |
| 95 | status_t Status::readFromParcel(const Parcel& parcel) { |
| 96 | status_t status = parcel.readInt32(&mException); |
| 97 | if (status != OK) { |
| 98 | setFromStatusT(status); |
| 99 | return status; |
| 100 | } |
| 101 | |
qinyige1 | edee03a | 2024-08-14 17:50:30 +0800 | [diff] [blame] | 102 | if (mException == EX_HAS_NOTED_APPOPS_REPLY_HEADER) { |
| 103 | status = skipUnusedHeader(parcel); |
| 104 | if (status != OK) { |
| 105 | setFromStatusT(status); |
| 106 | return status; |
| 107 | } |
| 108 | // Read next exception code. |
| 109 | status = parcel.readInt32(&mException); |
| 110 | if (status != OK) { |
| 111 | setFromStatusT(status); |
| 112 | return status; |
| 113 | } |
| 114 | } |
| 115 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 116 | // Skip over fat response headers. Not used (or propagated) in native code. |
| 117 | if (mException == EX_HAS_REPLY_HEADER) { |
qinyige1 | edee03a | 2024-08-14 17:50:30 +0800 | [diff] [blame] | 118 | status = skipUnusedHeader(parcel); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 119 | if (status != OK) { |
| 120 | setFromStatusT(status); |
| 121 | return status; |
| 122 | } |
Steven Moreland | 0038364 | 2019-05-20 15:28:13 -0700 | [diff] [blame] | 123 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 124 | // And fat response headers are currently only used when there are no |
| 125 | // exceptions, so act like there was no error. |
| 126 | mException = EX_NONE; |
| 127 | } |
| 128 | |
| 129 | if (mException == EX_NONE) { |
| 130 | return status; |
| 131 | } |
| 132 | |
| 133 | // The remote threw an exception. Get the message back. |
Steven Moreland | 4245498 | 2021-07-13 01:09:26 +0000 | [diff] [blame] | 134 | std::optional<String16> message; |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 135 | status = parcel.readString16(&message); |
| 136 | if (status != OK) { |
| 137 | setFromStatusT(status); |
| 138 | return status; |
| 139 | } |
Steven Moreland | 4245498 | 2021-07-13 01:09:26 +0000 | [diff] [blame] | 140 | mMessage = String8(message.value_or(String16())); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 141 | |
Fyodor Kupolov | d9ac374 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 142 | // Skip over the remote stack trace data |
Steven Moreland | 6653bde | 2022-06-07 17:26:17 +0000 | [diff] [blame] | 143 | const size_t remote_start = parcel.dataPosition(); |
| 144 | // Get available size before reading more |
| 145 | const size_t remote_avail = parcel.dataAvail(); |
Fyodor Kupolov | d9ac374 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 146 | int32_t remote_stack_trace_header_size; |
| 147 | status = parcel.readInt32(&remote_stack_trace_header_size); |
| 148 | if (status != OK) { |
| 149 | setFromStatusT(status); |
| 150 | return status; |
| 151 | } |
Steven Moreland | 0038364 | 2019-05-20 15:28:13 -0700 | [diff] [blame] | 152 | if (remote_stack_trace_header_size < 0 || |
Steven Moreland | 6653bde | 2022-06-07 17:26:17 +0000 | [diff] [blame] | 153 | static_cast<size_t>(remote_stack_trace_header_size) > remote_avail) { |
Steven Moreland | 0038364 | 2019-05-20 15:28:13 -0700 | [diff] [blame] | 154 | |
| 155 | android_errorWriteLog(0x534e4554, "132650049"); |
| 156 | setFromStatusT(UNKNOWN_ERROR); |
| 157 | return UNKNOWN_ERROR; |
| 158 | } |
Steven Moreland | 6653bde | 2022-06-07 17:26:17 +0000 | [diff] [blame] | 159 | |
| 160 | if (remote_stack_trace_header_size != 0) { |
| 161 | parcel.setDataPosition(remote_start + remote_stack_trace_header_size); |
| 162 | } |
Fyodor Kupolov | d9ac374 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 163 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 164 | if (mException == EX_SERVICE_SPECIFIC) { |
| 165 | status = parcel.readInt32(&mErrorCode); |
Jeff Sharkey | c8dc4f0 | 2017-01-17 13:53:04 -0700 | [diff] [blame] | 166 | } else if (mException == EX_PARCELABLE) { |
| 167 | // Skip over the blob of Parcelable data |
Steven Moreland | 0038364 | 2019-05-20 15:28:13 -0700 | [diff] [blame] | 168 | const size_t header_start = parcel.dataPosition(); |
| 169 | // Get available size before reading more |
| 170 | const size_t header_avail = parcel.dataAvail(); |
| 171 | |
Jeff Sharkey | c8dc4f0 | 2017-01-17 13:53:04 -0700 | [diff] [blame] | 172 | int32_t header_size; |
| 173 | status = parcel.readInt32(&header_size); |
| 174 | if (status != OK) { |
| 175 | setFromStatusT(status); |
| 176 | return status; |
| 177 | } |
Steven Moreland | 0038364 | 2019-05-20 15:28:13 -0700 | [diff] [blame] | 178 | |
| 179 | if (header_size < 0 || static_cast<size_t>(header_size) > header_avail) { |
| 180 | android_errorWriteLog(0x534e4554, "132650049"); |
| 181 | setFromStatusT(UNKNOWN_ERROR); |
| 182 | return UNKNOWN_ERROR; |
| 183 | } |
| 184 | |
Jeff Sharkey | c8dc4f0 | 2017-01-17 13:53:04 -0700 | [diff] [blame] | 185 | parcel.setDataPosition(header_start + header_size); |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 186 | } |
| 187 | if (status != OK) { |
| 188 | setFromStatusT(status); |
| 189 | return status; |
| 190 | } |
| 191 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 192 | return status; |
| 193 | } |
| 194 | |
| 195 | status_t Status::writeToParcel(Parcel* parcel) const { |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 196 | // Something really bad has happened, and we're not going to even |
| 197 | // try returning rich error data. |
| 198 | if (mException == EX_TRANSACTION_FAILED) { |
Steven Moreland | fb26e72 | 2018-10-05 13:43:24 -0700 | [diff] [blame] | 199 | return mErrorCode; |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 202 | status_t status = parcel->writeInt32(mException); |
Steven Moreland | 7862647 | 2020-04-09 11:47:39 -0700 | [diff] [blame] | 203 | if (status != OK) return status; |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 204 | if (mException == EX_NONE) { |
| 205 | // We have no more information to write. |
| 206 | return status; |
| 207 | } |
| 208 | status = parcel->writeString16(String16(mMessage)); |
Steven Moreland | 7862647 | 2020-04-09 11:47:39 -0700 | [diff] [blame] | 209 | if (status != OK) return status; |
Fyodor Kupolov | d9ac374 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 210 | status = parcel->writeInt32(0); // Empty remote stack trace header |
Steven Moreland | 7862647 | 2020-04-09 11:47:39 -0700 | [diff] [blame] | 211 | if (status != OK) return status; |
Jeff Sharkey | c8dc4f0 | 2017-01-17 13:53:04 -0700 | [diff] [blame] | 212 | if (mException == EX_SERVICE_SPECIFIC) { |
| 213 | status = parcel->writeInt32(mErrorCode); |
| 214 | } else if (mException == EX_PARCELABLE) { |
| 215 | // Sending Parcelable blobs currently not supported |
| 216 | status = parcel->writeInt32(0); |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 217 | } |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 218 | return status; |
| 219 | } |
| 220 | |
Steven Moreland | cf37369 | 2022-01-21 23:55:15 +0000 | [diff] [blame] | 221 | status_t Status::writeOverParcel(Parcel* parcel) const { |
| 222 | parcel->setDataSize(0); |
| 223 | parcel->setDataPosition(0); |
| 224 | return writeToParcel(parcel); |
| 225 | } |
| 226 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 227 | void Status::setException(int32_t ex, const String8& message) { |
| 228 | mException = ex; |
Steven Moreland | fb26e72 | 2018-10-05 13:43:24 -0700 | [diff] [blame] | 229 | mErrorCode = ex == EX_TRANSACTION_FAILED ? FAILED_TRANSACTION : NO_ERROR; |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 230 | mMessage.setTo(message); |
| 231 | } |
| 232 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 233 | void Status::setServiceSpecificError(int32_t errorCode, const String8& message) { |
| 234 | setException(EX_SERVICE_SPECIFIC, message); |
| 235 | mErrorCode = errorCode; |
| 236 | } |
| 237 | |
| 238 | void Status::setFromStatusT(status_t status) { |
| 239 | mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED; |
| 240 | mErrorCode = status; |
| 241 | mMessage.clear(); |
| 242 | } |
| 243 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 244 | String8 Status::toString8() const { |
| 245 | String8 ret; |
| 246 | if (mException == EX_NONE) { |
| 247 | ret.append("No error"); |
| 248 | } else { |
Jeongik Cha | 89b3f8b | 2019-03-18 14:38:33 +0900 | [diff] [blame] | 249 | ret.appendFormat("Status(%d, %s): '", mException, exceptionToString(mException).c_str()); |
Steven Moreland | 56b2c4b | 2019-11-15 00:15:33 -0800 | [diff] [blame] | 250 | if (mException == EX_SERVICE_SPECIFIC) { |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 251 | ret.appendFormat("%d: ", mErrorCode); |
Steven Moreland | 56b2c4b | 2019-11-15 00:15:33 -0800 | [diff] [blame] | 252 | } else if (mException == EX_TRANSACTION_FAILED) { |
| 253 | ret.appendFormat("%s: ", statusToString(mErrorCode).c_str()); |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 254 | } |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 255 | ret.append(String8(mMessage)); |
| 256 | ret.append("'"); |
| 257 | } |
| 258 | return ret; |
| 259 | } |
| 260 | |
qinyige1 | edee03a | 2024-08-14 17:50:30 +0800 | [diff] [blame] | 261 | status_t Status::skipUnusedHeader(const Parcel& parcel) { |
| 262 | // Note that the header size includes the 4 byte size field. |
| 263 | const size_t header_start = parcel.dataPosition(); |
| 264 | // Get available size before reading more |
| 265 | const size_t header_avail = parcel.dataAvail(); |
| 266 | |
| 267 | int32_t header_size; |
| 268 | status_t status = parcel.readInt32(&header_size); |
| 269 | ALOGD("Skip unused header. exception code: %d, start: %zu, size: %d.", |
| 270 | mException, header_start, header_size); |
| 271 | if (status != OK) { |
| 272 | return status; |
| 273 | } |
| 274 | |
| 275 | if (header_size < 0 || static_cast<size_t>(header_size) > header_avail) { |
| 276 | android_errorWriteLog(0x534e4554, "132650049"); |
| 277 | return UNKNOWN_ERROR; |
| 278 | } |
| 279 | |
| 280 | parcel.setDataPosition(header_start + header_size); |
| 281 | return OK; |
| 282 | } |
| 283 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 284 | } // namespace binder |
| 285 | } // namespace android |