blob: f3af1244f4ac5b30778b4315ca13d23222af0a05 [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
Steven Moreland9cbef742018-06-26 16:23:50 -070021#include <InternalStatic.h> // TODO(b/69122224): remove this include, for getOrCreateCachedBinder
Steven Moreland986fa092018-09-27 10:38:03 -070022#include <android/hidl/base/1.0/BpHwBase.h>
Steven Morelandb9173182018-08-08 20:06:08 +000023#include <hwbinder/IPCThreadState.h>
Steven Moreland9cbef742018-06-26 16:23:50 -070024
Yifan Hong777bef92017-02-01 15:50:36 -080025// C includes
Steven Moreland45f69c62018-06-06 16:28:29 -070026#include <inttypes.h>
Yifan Hong777bef92017-02-01 15:50:36 -080027#include <unistd.h>
28
29// C++ includes
30#include <fstream>
31#include <sstream>
32
Yifan Hong7f97f442016-11-14 18:31:05 -080033namespace android {
34namespace hardware {
35
Steven Moreland108d09d2017-05-05 16:15:38 -070036hidl_binder_death_recipient::hidl_binder_death_recipient(const sp<hidl_death_recipient> &recipient,
37 uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base) :
38 mRecipient(recipient), mCookie(cookie), mBase(base) {
39}
40
41void hidl_binder_death_recipient::binderDied(const wp<IBinder>& /*who*/) {
42 sp<hidl_death_recipient> recipient = mRecipient.promote();
Martijn Coenen25508802017-07-05 12:21:00 +020043 if (recipient != nullptr && mBase != nullptr) {
Steven Moreland108d09d2017-05-05 16:15:38 -070044 recipient->serviceDied(mCookie, mBase);
45 }
Martijn Coenen25508802017-07-05 12:21:00 +020046 mBase = nullptr;
Steven Moreland108d09d2017-05-05 16:15:38 -070047}
48
49wp<hidl_death_recipient> hidl_binder_death_recipient::getRecipient() {
50 return mRecipient;
51}
52
Nirav Atre564a8d22018-07-26 18:29:12 -070053const size_t hidl_handle::kOffsetOfNativeHandle = offsetof(hidl_handle, mHandle);
54static_assert(hidl_handle::kOffsetOfNativeHandle == 0, "wrong offset");
55
56status_t readEmbeddedFromParcel(const hidl_handle& /* handle */,
57 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
58 const native_handle_t *handle;
59 status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
60 parentHandle,
61 parentOffset + hidl_handle::kOffsetOfNativeHandle,
62 &handle);
63
64 return _hidl_err;
65}
66
67status_t writeEmbeddedToParcel(const hidl_handle &handle,
68 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
69 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
70 handle.getNativeHandle(),
71 parentHandle,
72 parentOffset + hidl_handle::kOffsetOfNativeHandle);
73
74 return _hidl_err;
75}
76
Martijn Coenen30791002016-12-01 15:40:46 +010077const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle);
78const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080079static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset");
80static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset");
Martijn Coenen30791002016-12-01 15:40:46 +010081
Martijn Coenen9d3eb352017-04-18 20:28:03 -070082status_t readEmbeddedFromParcel(const hidl_memory& memory,
Martijn Coenen30791002016-12-01 15:40:46 +010083 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Nirav Atre564a8d22018-07-26 18:29:12 -070084 // TODO(b/111883309): Invoke readEmbeddedFromParcel(hidl_handle, ...).
Steven Moreland1f535a22017-01-06 19:25:49 -080085 const native_handle_t *handle;
86 ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Martijn Coenen30791002016-12-01 15:40:46 +010087 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080088 parentOffset + hidl_memory::kOffsetOfHandle,
89 &handle);
Martijn Coenen30791002016-12-01 15:40:46 +010090
Steven Moreland1f535a22017-01-06 19:25:49 -080091 if (_hidl_err == ::android::OK) {
92 _hidl_err = readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -070093 memory.name(),
Steven Moreland1f535a22017-01-06 19:25:49 -080094 parcel,
95 parentHandle,
96 parentOffset + hidl_memory::kOffsetOfName);
Martijn Coenen30791002016-12-01 15:40:46 +010097 }
98
Steven Moreland45f69c62018-06-06 16:28:29 -070099 // hidl_memory's size is stored in uint64_t, but mapMemory's mmap will map
100 // size in size_t. If size is over SIZE_MAX, mapMemory could succeed
101 // but the mapped memory's actual size will be smaller than the reported size.
102 if (memory.size() > SIZE_MAX) {
103 ALOGE("Cannot use memory with %" PRId64 " bytes because it is too large.", memory.size());
104 android_errorWriteLog(0x534e4554, "79376389");
105 return BAD_VALUE;
106 }
107
Martijn Coenen30791002016-12-01 15:40:46 +0100108 return _hidl_err;
109}
110
111status_t writeEmbeddedToParcel(const hidl_memory &memory,
112 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
Nirav Atre564a8d22018-07-26 18:29:12 -0700113 // TODO(b/111883309): Invoke writeEmbeddedToParcel(hidl_handle, ...).
Martijn Coenen30791002016-12-01 15:40:46 +0100114 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
115 memory.handle(),
116 parentHandle,
117 parentOffset + hidl_memory::kOffsetOfHandle);
118
119 if (_hidl_err == ::android::OK) {
120 _hidl_err = writeEmbeddedToParcel(
121 memory.name(),
122 parcel,
123 parentHandle,
124 parentOffset + hidl_memory::kOffsetOfName);
125 }
126
127 return _hidl_err;
128}
Yifan Hong7f97f442016-11-14 18:31:05 -0800129const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800130static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset");
Yifan Hong7f97f442016-11-14 18:31:05 -0800131
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700132status_t readEmbeddedFromParcel(const hidl_string &string ,
Yifan Hong7f97f442016-11-14 18:31:05 -0800133 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -0800134 const void *out;
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700135
136 status_t status = parcel.readEmbeddedBuffer(
137 string.size() + 1,
Yifan Hong7f97f442016-11-14 18:31:05 -0800138 nullptr /* buffer_handle */,
139 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -0800140 parentOffset + hidl_string::kOffsetOfBuffer,
141 &out);
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700142
143 if (status != OK) {
144 return status;
145 }
146
147 // Always safe to access out[string.size()] because we read size+1 bytes
148 if (static_cast<const char *>(out)[string.size()] != '\0') {
149 ALOGE("Received unterminated hidl_string buffer.");
150 return BAD_VALUE;
151 }
152
153 return OK;
Yifan Hong7f97f442016-11-14 18:31:05 -0800154}
155
156status_t writeEmbeddedToParcel(const hidl_string &string,
157 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
158 return parcel->writeEmbeddedBuffer(
159 string.c_str(),
160 string.size() + 1,
161 nullptr /* handle */,
162 parentHandle,
163 parentOffset + hidl_string::kOffsetOfBuffer);
164}
165
Yifan Hong7f97f442016-11-14 18:31:05 -0800166status_t readFromParcel(Status *s, const Parcel& parcel) {
167 int32_t exception;
Yifan Hong7f97f442016-11-14 18:31:05 -0800168 status_t status = parcel.readInt32(&exception);
169 if (status != OK) {
170 s->setFromStatusT(status);
171 return status;
172 }
173
174 // Skip over fat response headers. Not used (or propagated) in native code.
175 if (exception == Status::EX_HAS_REPLY_HEADER) {
176 // Note that the header size includes the 4 byte size field.
177 const int32_t header_start = parcel.dataPosition();
178 int32_t header_size;
179 status = parcel.readInt32(&header_size);
180 if (status != OK) {
181 s->setFromStatusT(status);
182 return status;
183 }
184 parcel.setDataPosition(header_start + header_size);
185 // And fat response headers are currently only used when there are no
186 // exceptions, so act like there was no error.
187 exception = Status::EX_NONE;
188 }
189
190 if (exception == Status::EX_NONE) {
191 *s = Status::ok();
192 return status;
193 }
194
195 // The remote threw an exception. Get the message back.
196 String16 message;
197 status = parcel.readString16(&message);
198 if (status != OK) {
199 s->setFromStatusT(status);
200 return status;
201 }
202
Steven Moreland72db40f2017-03-09 18:15:27 -0800203 s->setException(exception, String8(message));
Yifan Hong7f97f442016-11-14 18:31:05 -0800204
205 return status;
206}
207
208status_t writeToParcel(const Status &s, Parcel* parcel) {
209 // Something really bad has happened, and we're not going to even
210 // try returning rich error data.
211 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
Steven Moreland872da2d2018-10-12 15:12:39 -0700212 return s.transactionError();
Yifan Hong7f97f442016-11-14 18:31:05 -0800213 }
214
215 status_t status = parcel->writeInt32(s.exceptionCode());
216 if (status != OK) { return status; }
217 if (s.exceptionCode() == Status::EX_NONE) {
218 // We have no more information to write.
219 return status;
220 }
221 status = parcel->writeString16(String16(s.exceptionMessage()));
Yifan Hong7f97f442016-11-14 18:31:05 -0800222 return status;
223}
224
Steven Moreland9cbef742018-06-26 16:23:50 -0700225sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr) {
Steven Moreland986fa092018-09-27 10:38:03 -0700226 if (ifacePtr == nullptr) {
227 return nullptr;
228 }
229
230 if (ifacePtr->isRemote()) {
231 using ::android::hidl::base::V1_0::BpHwBase;
232
233 BpHwBase* bpBase = static_cast<BpHwBase*>(ifacePtr);
234 BpHwRefBase* bpRefBase = static_cast<BpHwRefBase*>(bpBase);
235 return sp<IBinder>(bpRefBase->remote());
236 }
Steven Moreland9cbef742018-06-26 16:23:50 -0700237
238 std::string descriptor = details::getDescriptor(ifacePtr);
239 if (descriptor.empty()) {
240 // interfaceDescriptor fails
241 return nullptr;
242 }
243
244 // for get + set
Steven Morelandb1c7d062019-04-22 10:27:47 -0700245 std::unique_lock<std::mutex> _lock = details::gBnMap->lock();
Steven Moreland9cbef742018-06-26 16:23:50 -0700246
Steven Morelandb1c7d062019-04-22 10:27:47 -0700247 wp<BHwBinder> wBnObj = details::gBnMap->getLocked(ifacePtr, nullptr);
Steven Moreland9cbef742018-06-26 16:23:50 -0700248 sp<IBinder> sBnObj = wBnObj.promote();
249
250 if (sBnObj == nullptr) {
251 auto func = details::getBnConstructorMap().get(descriptor, nullptr);
252 if (!func) {
253 // TODO(b/69122224): remove this static variable when prebuilts updated
Steven Morelandb1c7d062019-04-22 10:27:47 -0700254 func = details::gBnConstructorMap->get(descriptor, nullptr);
Steven Moreland9cbef742018-06-26 16:23:50 -0700255 }
Steven Moreland9fd33872018-06-26 18:14:22 -0700256 LOG_ALWAYS_FATAL_IF(func == nullptr, "%s gBnConstructorMap returned null for %s", __func__,
257 descriptor.c_str());
Steven Moreland9cbef742018-06-26 16:23:50 -0700258
259 sBnObj = sp<IBinder>(func(static_cast<void*>(ifacePtr)));
Steven Moreland9fd33872018-06-26 18:14:22 -0700260 LOG_ALWAYS_FATAL_IF(sBnObj == nullptr, "%s Bn constructor function returned null for %s",
261 __func__, descriptor.c_str());
Steven Moreland9cbef742018-06-26 16:23:50 -0700262
Steven Morelandb1c7d062019-04-22 10:27:47 -0700263 details::gBnMap->setLocked(ifacePtr, static_cast<BHwBinder*>(sBnObj.get()));
Steven Moreland9cbef742018-06-26 16:23:50 -0700264 }
265
266 return sBnObj;
267}
268
Steven Moreland50815be2018-05-24 12:35:44 -0700269static bool gThreadPoolConfigured = false;
270
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800271void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
Steven Moreland50815be2018-05-24 12:35:44 -0700272 status_t ret = ProcessState::self()->setThreadPoolConfiguration(
273 maxThreads, callerWillJoin /*callerJoinsPool*/);
274 LOG_ALWAYS_FATAL_IF(ret != OK, "Could not setThreadPoolConfiguration: %d", ret);
275
276 gThreadPoolConfigured = true;
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800277}
278
279void joinBinderRpcThreadpool() {
Steven Morelandcd329a82018-05-29 13:30:11 -0700280 LOG_ALWAYS_FATAL_IF(!gThreadPoolConfigured,
281 "HIDL joinRpcThreadpool without calling configureRpcThreadPool.");
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800282 IPCThreadState::self()->joinThreadPool();
283}
284
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800285int setupBinderPolling() {
286 int fd;
287 int err = IPCThreadState::self()->setupPolling(&fd);
288
Steven Morelandcd329a82018-05-29 13:30:11 -0700289 LOG_ALWAYS_FATAL_IF(err != OK, "Failed to setup binder polling: %d (%s)", err, strerror(err));
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800290
291 return err == OK ? fd : -1;
292}
293
294status_t handleBinderPoll() {
295 return IPCThreadState::self()->handlePolledCommands();
296}
297
Steven Morelandd40af8e2018-05-01 16:33:21 -0700298void addPostCommandTask(const std::function<void(void)> task) {
299 IPCThreadState::self()->addPostCommandTask(task);
300}
301
Yifan Hong7f97f442016-11-14 18:31:05 -0800302} // namespace hardware
303} // namespace android