blob: 142720a09ba3a882a70f35d011fc58230cd44261 [file] [log] [blame]
Yifan Hong7f97f442016-11-14 18:31:05 -08001/*
2 * Copyright (C) 2016 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#define LOG_TAG "HidlSupport"
18
19#include <hidl/HidlBinderSupport.h>
20
Yifan Hong777bef92017-02-01 15:50:36 -080021// C includes
Steven Moreland45f69c62018-06-06 16:28:29 -070022#include <inttypes.h>
Yifan Hong777bef92017-02-01 15:50:36 -080023#include <unistd.h>
24
25// C++ includes
26#include <fstream>
27#include <sstream>
28
Yifan Hong7f97f442016-11-14 18:31:05 -080029namespace android {
30namespace hardware {
31
Martijn Coenen30791002016-12-01 15:40:46 +010032const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle);
33const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080034static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset");
35static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset");
Martijn Coenen30791002016-12-01 15:40:46 +010036
Martijn Coenen5286ad52017-04-18 20:28:03 -070037status_t readEmbeddedFromParcel(const hidl_memory& memory,
Martijn Coenen30791002016-12-01 15:40:46 +010038 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -080039 const native_handle_t *handle;
40 ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Martijn Coenen30791002016-12-01 15:40:46 +010041 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080042 parentOffset + hidl_memory::kOffsetOfHandle,
43 &handle);
Martijn Coenen30791002016-12-01 15:40:46 +010044
Steven Moreland1f535a22017-01-06 19:25:49 -080045 if (_hidl_err == ::android::OK) {
46 _hidl_err = readEmbeddedFromParcel(
Martijn Coenen5286ad52017-04-18 20:28:03 -070047 memory.name(),
Steven Moreland1f535a22017-01-06 19:25:49 -080048 parcel,
49 parentHandle,
50 parentOffset + hidl_memory::kOffsetOfName);
Martijn Coenen30791002016-12-01 15:40:46 +010051 }
52
Steven Moreland45f69c62018-06-06 16:28:29 -070053 // hidl_memory's size is stored in uint64_t, but mapMemory's mmap will map
54 // size in size_t. If size is over SIZE_MAX, mapMemory could succeed
55 // but the mapped memory's actual size will be smaller than the reported size.
56 if (memory.size() > SIZE_MAX) {
57 ALOGE("Cannot use memory with %" PRId64 " bytes because it is too large.", memory.size());
58 android_errorWriteLog(0x534e4554, "79376389");
59 return BAD_VALUE;
60 }
61
Martijn Coenen30791002016-12-01 15:40:46 +010062 return _hidl_err;
63}
64
65status_t writeEmbeddedToParcel(const hidl_memory &memory,
66 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
67 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
68 memory.handle(),
69 parentHandle,
70 parentOffset + hidl_memory::kOffsetOfHandle);
71
72 if (_hidl_err == ::android::OK) {
73 _hidl_err = writeEmbeddedToParcel(
74 memory.name(),
75 parcel,
76 parentHandle,
77 parentOffset + hidl_memory::kOffsetOfName);
78 }
79
80 return _hidl_err;
81}
Yifan Hong7f97f442016-11-14 18:31:05 -080082// static
83const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080084static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset");
Yifan Hong7f97f442016-11-14 18:31:05 -080085
Martijn Coenen5286ad52017-04-18 20:28:03 -070086status_t readEmbeddedFromParcel(const hidl_string &string ,
Yifan Hong7f97f442016-11-14 18:31:05 -080087 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -080088 const void *out;
Martijn Coenen5286ad52017-04-18 20:28:03 -070089
90 status_t status = parcel.readEmbeddedBuffer(
91 string.size() + 1,
Yifan Hong7f97f442016-11-14 18:31:05 -080092 nullptr /* buffer_handle */,
93 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080094 parentOffset + hidl_string::kOffsetOfBuffer,
95 &out);
Martijn Coenen5286ad52017-04-18 20:28:03 -070096
97 if (status != OK) {
98 return status;
99 }
100
101 // Always safe to access out[string.size()] because we read size+1 bytes
102 if (static_cast<const char *>(out)[string.size()] != '\0') {
103 ALOGE("Received unterminated hidl_string buffer.");
104 return BAD_VALUE;
105 }
106
107 return OK;
Yifan Hong7f97f442016-11-14 18:31:05 -0800108}
109
110status_t writeEmbeddedToParcel(const hidl_string &string,
111 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
112 return parcel->writeEmbeddedBuffer(
113 string.c_str(),
114 string.size() + 1,
115 nullptr /* handle */,
116 parentHandle,
117 parentOffset + hidl_string::kOffsetOfBuffer);
118}
119
120android::status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel) {
121 return parcel.writeUint32(static_cast<uint32_t>(version.get_major()) << 16 | version.get_minor());
122}
123
124hidl_version* readFromParcel(const android::hardware::Parcel& parcel) {
125 uint32_t version;
126 android::status_t status = parcel.readUint32(&version);
127 if (status != OK) {
128 return nullptr;
129 } else {
130 return new hidl_version(version >> 16, version & 0xFFFF);
131 }
132}
133
134status_t readFromParcel(Status *s, const Parcel& parcel) {
135 int32_t exception;
Yifan Hong7f97f442016-11-14 18:31:05 -0800136 status_t status = parcel.readInt32(&exception);
137 if (status != OK) {
138 s->setFromStatusT(status);
139 return status;
140 }
141
142 // Skip over fat response headers. Not used (or propagated) in native code.
143 if (exception == Status::EX_HAS_REPLY_HEADER) {
144 // Note that the header size includes the 4 byte size field.
145 const int32_t header_start = parcel.dataPosition();
146 int32_t header_size;
147 status = parcel.readInt32(&header_size);
148 if (status != OK) {
149 s->setFromStatusT(status);
150 return status;
151 }
152 parcel.setDataPosition(header_start + header_size);
153 // And fat response headers are currently only used when there are no
154 // exceptions, so act like there was no error.
155 exception = Status::EX_NONE;
156 }
157
158 if (exception == Status::EX_NONE) {
159 *s = Status::ok();
160 return status;
161 }
162
163 // The remote threw an exception. Get the message back.
164 String16 message;
165 status = parcel.readString16(&message);
166 if (status != OK) {
167 s->setFromStatusT(status);
168 return status;
169 }
170
Steven Moreland72db40f2017-03-09 18:15:27 -0800171 s->setException(exception, String8(message));
Yifan Hong7f97f442016-11-14 18:31:05 -0800172
173 return status;
174}
175
176status_t writeToParcel(const Status &s, Parcel* parcel) {
177 // Something really bad has happened, and we're not going to even
178 // try returning rich error data.
179 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
180 return s.transactionError();
181 }
182
183 status_t status = parcel->writeInt32(s.exceptionCode());
184 if (status != OK) { return status; }
185 if (s.exceptionCode() == Status::EX_NONE) {
186 // We have no more information to write.
187 return status;
188 }
189 status = parcel->writeString16(String16(s.exceptionMessage()));
Yifan Hong7f97f442016-11-14 18:31:05 -0800190 return status;
191}
192
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800193void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
194 ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/);
195}
196
197void joinBinderRpcThreadpool() {
198 IPCThreadState::self()->joinThreadPool();
199}
200
Yifan Hong7f97f442016-11-14 18:31:05 -0800201} // namespace hardware
202} // namespace android