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 
ok()22 Status Status::ok() {
23     return Status();
24 }
25 
fromExceptionCode(int32_t exceptionCode)26 Status Status::fromExceptionCode(int32_t exceptionCode) {
27     if (exceptionCode == EX_TRANSACTION_FAILED) {
28         return Status(exceptionCode, FAILED_TRANSACTION);
29     }
30     return Status(exceptionCode, OK);
31 }
32 
fromExceptionCode(int32_t exceptionCode,const String8 & message)33 Status Status::fromExceptionCode(int32_t exceptionCode,
34                                  const String8& message) {
35     if (exceptionCode == EX_TRANSACTION_FAILED) {
36         return Status(exceptionCode, FAILED_TRANSACTION, message);
37     }
38     return Status(exceptionCode, OK, message);
39 }
40 
fromExceptionCode(int32_t exceptionCode,const char * message)41 Status Status::fromExceptionCode(int32_t exceptionCode,
42                                  const char* message) {
43     return fromExceptionCode(exceptionCode, String8(message));
44 }
45 
fromServiceSpecificError(int32_t serviceSpecificErrorCode)46 Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) {
47     return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode);
48 }
49 
fromServiceSpecificError(int32_t serviceSpecificErrorCode,const String8 & message)50 Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
51                                         const String8& message) {
52     return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message);
53 }
54 
fromServiceSpecificError(int32_t serviceSpecificErrorCode,const char * message)55 Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
56                                         const char* message) {
57     return fromServiceSpecificError(serviceSpecificErrorCode, String8(message));
58 }
59 
fromStatusT(status_t status)60 Status Status::fromStatusT(status_t status) {
61     Status ret;
62     ret.setFromStatusT(status);
63     return ret;
64 }
65 
exceptionToString(int32_t exceptionCode)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 
Status(int32_t exceptionCode,int32_t errorCode)86 Status::Status(int32_t exceptionCode, int32_t errorCode)
87     : mException(exceptionCode),
88       mErrorCode(errorCode) {}
89 
Status(int32_t exceptionCode,int32_t errorCode,const String8 & message)90 Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message)
91     : mException(exceptionCode),
92       mErrorCode(errorCode),
93       mMessage(message) {}
94 
readFromParcel(const Parcel & parcel)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 
102     // Skip over fat response headers.  Not used (or propagated) in native code.
103     if (mException == EX_HAS_REPLY_HEADER) {
104         // Note that the header size includes the 4 byte size field.
105         const size_t header_start = parcel.dataPosition();
106         // Get available size before reading more
107         const size_t header_avail = parcel.dataAvail();
108 
109         int32_t header_size;
110         status = parcel.readInt32(&header_size);
111         if (status != OK) {
112             setFromStatusT(status);
113             return status;
114         }
115 
116         if (header_size < 0 || static_cast<size_t>(header_size) > header_avail) {
117             android_errorWriteLog(0x534e4554, "132650049");
118             setFromStatusT(UNKNOWN_ERROR);
119             return UNKNOWN_ERROR;
120         }
121 
122         parcel.setDataPosition(header_start + header_size);
123         // And fat response headers are currently only used when there are no
124         // exceptions, so act like there was no error.
125         mException = EX_NONE;
126     }
127 
128     if (mException == EX_NONE) {
129         return status;
130     }
131 
132     // The remote threw an exception.  Get the message back.
133     std::optional<String16> message;
134     status = parcel.readString16(&message);
135     if (status != OK) {
136         setFromStatusT(status);
137         return status;
138     }
139     mMessage = String8(message.value_or(String16()));
140 
141     // Skip over the remote stack trace data
142     const size_t remote_start = parcel.dataPosition();
143     // Get available size before reading more
144     const size_t remote_avail = parcel.dataAvail();
145     int32_t remote_stack_trace_header_size;
146     status = parcel.readInt32(&remote_stack_trace_header_size);
147     if (status != OK) {
148         setFromStatusT(status);
149         return status;
150     }
151     if (remote_stack_trace_header_size < 0 ||
152         static_cast<size_t>(remote_stack_trace_header_size) > remote_avail) {
153 
154         android_errorWriteLog(0x534e4554, "132650049");
155         setFromStatusT(UNKNOWN_ERROR);
156         return UNKNOWN_ERROR;
157     }
158 
159     if (remote_stack_trace_header_size != 0) {
160         parcel.setDataPosition(remote_start + remote_stack_trace_header_size);
161     }
162 
163     if (mException == EX_SERVICE_SPECIFIC) {
164         status = parcel.readInt32(&mErrorCode);
165     } else if (mException == EX_PARCELABLE) {
166         // Skip over the blob of Parcelable data
167         const size_t header_start = parcel.dataPosition();
168         // Get available size before reading more
169         const size_t header_avail = parcel.dataAvail();
170 
171         int32_t header_size;
172         status = parcel.readInt32(&header_size);
173         if (status != OK) {
174             setFromStatusT(status);
175             return status;
176         }
177 
178         if (header_size < 0 || static_cast<size_t>(header_size) > header_avail) {
179             android_errorWriteLog(0x534e4554, "132650049");
180             setFromStatusT(UNKNOWN_ERROR);
181             return UNKNOWN_ERROR;
182         }
183 
184         parcel.setDataPosition(header_start + header_size);
185     }
186     if (status != OK) {
187         setFromStatusT(status);
188         return status;
189     }
190 
191     return status;
192 }
193 
writeToParcel(Parcel * parcel) const194 status_t Status::writeToParcel(Parcel* parcel) const {
195     // Something really bad has happened, and we're not going to even
196     // try returning rich error data.
197     if (mException == EX_TRANSACTION_FAILED) {
198         return mErrorCode;
199     }
200 
201     status_t status = parcel->writeInt32(mException);
202     if (status != OK) return status;
203     if (mException == EX_NONE) {
204         // We have no more information to write.
205         return status;
206     }
207     status = parcel->writeString16(String16(mMessage));
208     if (status != OK) return status;
209     status = parcel->writeInt32(0); // Empty remote stack trace header
210     if (status != OK) return status;
211     if (mException == EX_SERVICE_SPECIFIC) {
212         status = parcel->writeInt32(mErrorCode);
213     } else if (mException == EX_PARCELABLE) {
214         // Sending Parcelable blobs currently not supported
215         status = parcel->writeInt32(0);
216     }
217     return status;
218 }
219 
writeOverParcel(Parcel * parcel) const220 status_t Status::writeOverParcel(Parcel* parcel) const {
221     parcel->setDataSize(0);
222     parcel->setDataPosition(0);
223     return writeToParcel(parcel);
224 }
225 
setException(int32_t ex,const String8 & message)226 void Status::setException(int32_t ex, const String8& message) {
227     mException = ex;
228     mErrorCode = ex == EX_TRANSACTION_FAILED ? FAILED_TRANSACTION : NO_ERROR;
229     mMessage.setTo(message);
230 }
231 
setServiceSpecificError(int32_t errorCode,const String8 & message)232 void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
233     setException(EX_SERVICE_SPECIFIC, message);
234     mErrorCode = errorCode;
235 }
236 
setFromStatusT(status_t status)237 void Status::setFromStatusT(status_t status) {
238     mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
239     mErrorCode = status;
240     mMessage.clear();
241 }
242 
toString8() const243 String8 Status::toString8() const {
244     String8 ret;
245     if (mException == EX_NONE) {
246         ret.append("No error");
247     } else {
248         ret.appendFormat("Status(%d, %s): '", mException, exceptionToString(mException).c_str());
249         if (mException == EX_SERVICE_SPECIFIC) {
250             ret.appendFormat("%d: ", mErrorCode);
251         } else if (mException == EX_TRANSACTION_FAILED) {
252             ret.appendFormat("%s: ", statusToString(mErrorCode).c_str());
253         }
254         ret.append(String8(mMessage));
255         ret.append("'");
256     }
257     return ret;
258 }
259 
260 }  // namespace binder
261 }  // namespace android
262