blob: b48b4604ffc1a1a947b3571844589ee5fcd3e8f1 [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 Moreland986fa092018-09-27 10:38:03 -070021#include <android/hidl/base/1.0/BpHwBase.h>
Steven Moreland347615c2020-03-09 18:36:40 -070022#include <android/hidl/manager/1.0/BpHwServiceManager.h>
23#include <android/hidl/manager/1.1/BpHwServiceManager.h>
24#include <android/hidl/manager/1.2/BpHwServiceManager.h>
Steven Morelandb9173182018-08-08 20:06:08 +000025#include <hwbinder/IPCThreadState.h>
Steven Moreland55d2dcd2020-02-10 17:20:56 -080026#include "InternalStatic.h" // TODO(b/69122224): remove this include, for getOrCreateCachedBinder
Steven Moreland9cbef742018-06-26 16:23:50 -070027
Yifan Hong777bef92017-02-01 15:50:36 -080028// C includes
Steven Moreland45f69c62018-06-06 16:28:29 -070029#include <inttypes.h>
Yifan Hong777bef92017-02-01 15:50:36 -080030#include <unistd.h>
31
32// C++ includes
33#include <fstream>
34#include <sstream>
35
Yifan Hong7f97f442016-11-14 18:31:05 -080036namespace android {
37namespace hardware {
38
Steven Moreland108d09d2017-05-05 16:15:38 -070039hidl_binder_death_recipient::hidl_binder_death_recipient(const sp<hidl_death_recipient> &recipient,
40 uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base) :
41 mRecipient(recipient), mCookie(cookie), mBase(base) {
42}
43
44void hidl_binder_death_recipient::binderDied(const wp<IBinder>& /*who*/) {
45 sp<hidl_death_recipient> recipient = mRecipient.promote();
Martijn Coenen25508802017-07-05 12:21:00 +020046 if (recipient != nullptr && mBase != nullptr) {
Steven Moreland108d09d2017-05-05 16:15:38 -070047 recipient->serviceDied(mCookie, mBase);
48 }
Martijn Coenen25508802017-07-05 12:21:00 +020049 mBase = nullptr;
Steven Moreland108d09d2017-05-05 16:15:38 -070050}
51
52wp<hidl_death_recipient> hidl_binder_death_recipient::getRecipient() {
53 return mRecipient;
54}
55
Nirav Atre564a8d22018-07-26 18:29:12 -070056const size_t hidl_handle::kOffsetOfNativeHandle = offsetof(hidl_handle, mHandle);
57static_assert(hidl_handle::kOffsetOfNativeHandle == 0, "wrong offset");
58
59status_t readEmbeddedFromParcel(const hidl_handle& /* handle */,
60 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
61 const native_handle_t *handle;
62 status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
63 parentHandle,
64 parentOffset + hidl_handle::kOffsetOfNativeHandle,
65 &handle);
66
67 return _hidl_err;
68}
69
70status_t writeEmbeddedToParcel(const hidl_handle &handle,
71 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
72 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
73 handle.getNativeHandle(),
74 parentHandle,
75 parentOffset + hidl_handle::kOffsetOfNativeHandle);
76
77 return _hidl_err;
78}
79
Martijn Coenen30791002016-12-01 15:40:46 +010080const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle);
81const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -080082static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset");
83static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset");
Martijn Coenen30791002016-12-01 15:40:46 +010084
Martijn Coenen9d3eb352017-04-18 20:28:03 -070085status_t readEmbeddedFromParcel(const hidl_memory& memory,
Martijn Coenen30791002016-12-01 15:40:46 +010086 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Nirav Atre564a8d22018-07-26 18:29:12 -070087 // TODO(b/111883309): Invoke readEmbeddedFromParcel(hidl_handle, ...).
Steven Moreland1f535a22017-01-06 19:25:49 -080088 const native_handle_t *handle;
89 ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
Martijn Coenen30791002016-12-01 15:40:46 +010090 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -080091 parentOffset + hidl_memory::kOffsetOfHandle,
92 &handle);
Martijn Coenen30791002016-12-01 15:40:46 +010093
Steven Moreland1f535a22017-01-06 19:25:49 -080094 if (_hidl_err == ::android::OK) {
95 _hidl_err = readEmbeddedFromParcel(
Martijn Coenen9d3eb352017-04-18 20:28:03 -070096 memory.name(),
Steven Moreland1f535a22017-01-06 19:25:49 -080097 parcel,
98 parentHandle,
99 parentOffset + hidl_memory::kOffsetOfName);
Martijn Coenen30791002016-12-01 15:40:46 +0100100 }
101
Steven Moreland45f69c62018-06-06 16:28:29 -0700102 // hidl_memory's size is stored in uint64_t, but mapMemory's mmap will map
103 // size in size_t. If size is over SIZE_MAX, mapMemory could succeed
104 // but the mapped memory's actual size will be smaller than the reported size.
105 if (memory.size() > SIZE_MAX) {
106 ALOGE("Cannot use memory with %" PRId64 " bytes because it is too large.", memory.size());
107 android_errorWriteLog(0x534e4554, "79376389");
108 return BAD_VALUE;
109 }
110
Martijn Coenen30791002016-12-01 15:40:46 +0100111 return _hidl_err;
112}
113
114status_t writeEmbeddedToParcel(const hidl_memory &memory,
115 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
Nirav Atre564a8d22018-07-26 18:29:12 -0700116 // TODO(b/111883309): Invoke writeEmbeddedToParcel(hidl_handle, ...).
Martijn Coenen30791002016-12-01 15:40:46 +0100117 status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
118 memory.handle(),
119 parentHandle,
120 parentOffset + hidl_memory::kOffsetOfHandle);
121
122 if (_hidl_err == ::android::OK) {
123 _hidl_err = writeEmbeddedToParcel(
124 memory.name(),
125 parcel,
126 parentHandle,
127 parentOffset + hidl_memory::kOffsetOfName);
128 }
129
130 return _hidl_err;
131}
Yifan Hong7f97f442016-11-14 18:31:05 -0800132const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800133static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset");
Yifan Hong7f97f442016-11-14 18:31:05 -0800134
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700135status_t readEmbeddedFromParcel(const hidl_string &string ,
Yifan Hong7f97f442016-11-14 18:31:05 -0800136 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
Steven Moreland1f535a22017-01-06 19:25:49 -0800137 const void *out;
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700138
139 status_t status = parcel.readEmbeddedBuffer(
140 string.size() + 1,
Yifan Hong7f97f442016-11-14 18:31:05 -0800141 nullptr /* buffer_handle */,
142 parentHandle,
Steven Moreland1f535a22017-01-06 19:25:49 -0800143 parentOffset + hidl_string::kOffsetOfBuffer,
144 &out);
Martijn Coenen9d3eb352017-04-18 20:28:03 -0700145
146 if (status != OK) {
147 return status;
148 }
149
150 // Always safe to access out[string.size()] because we read size+1 bytes
151 if (static_cast<const char *>(out)[string.size()] != '\0') {
152 ALOGE("Received unterminated hidl_string buffer.");
153 return BAD_VALUE;
154 }
155
156 return OK;
Yifan Hong7f97f442016-11-14 18:31:05 -0800157}
158
159status_t writeEmbeddedToParcel(const hidl_string &string,
160 Parcel *parcel, size_t parentHandle, size_t parentOffset) {
161 return parcel->writeEmbeddedBuffer(
162 string.c_str(),
163 string.size() + 1,
164 nullptr /* handle */,
165 parentHandle,
166 parentOffset + hidl_string::kOffsetOfBuffer);
167}
168
Yifan Hong7f97f442016-11-14 18:31:05 -0800169status_t readFromParcel(Status *s, const Parcel& parcel) {
170 int32_t exception;
Yifan Hong7f97f442016-11-14 18:31:05 -0800171 status_t status = parcel.readInt32(&exception);
172 if (status != OK) {
173 s->setFromStatusT(status);
174 return status;
175 }
176
Yifan Hong7f97f442016-11-14 18:31:05 -0800177 if (exception == Status::EX_NONE) {
178 *s = Status::ok();
179 return status;
180 }
181
182 // The remote threw an exception. Get the message back.
183 String16 message;
184 status = parcel.readString16(&message);
185 if (status != OK) {
186 s->setFromStatusT(status);
187 return status;
188 }
189
Steven Moreland72db40f2017-03-09 18:15:27 -0800190 s->setException(exception, String8(message));
Yifan Hong7f97f442016-11-14 18:31:05 -0800191
192 return status;
193}
194
195status_t writeToParcel(const Status &s, Parcel* parcel) {
196 // Something really bad has happened, and we're not going to even
197 // try returning rich error data.
198 if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
Steven Moreland872da2d2018-10-12 15:12:39 -0700199 return s.transactionError();
Yifan Hong7f97f442016-11-14 18:31:05 -0800200 }
201
202 status_t status = parcel->writeInt32(s.exceptionCode());
203 if (status != OK) { return status; }
204 if (s.exceptionCode() == Status::EX_NONE) {
205 // We have no more information to write.
206 return status;
207 }
208 status = parcel->writeString16(String16(s.exceptionMessage()));
Yifan Hong7f97f442016-11-14 18:31:05 -0800209 return status;
210}
211
Steven Moreland347615c2020-03-09 18:36:40 -0700212// assume: iface != nullptr, iface isRemote
213// This function is to sandbox a cast through a BpHw* class into a function, so
214// that we can remove cfi sanitization from it. Do not add additional
215// functionality here.
216__attribute__((no_sanitize("cfi"))) static inline BpHwRefBase* forceGetRefBase(
217 ::android::hidl::base::V1_0::IBase* ifacePtr) {
218 using ::android::hidl::base::V1_0::BpHwBase;
219
220 // canary only
221 static_assert(sizeof(BpHwBase) == sizeof(hidl::manager::V1_0::BpHwServiceManager));
222 static_assert(sizeof(BpHwBase) == sizeof(hidl::manager::V1_1::BpHwServiceManager));
223 static_assert(sizeof(BpHwBase) == sizeof(hidl::manager::V1_2::BpHwServiceManager));
224
225 // All BpHw* are generated the same. This may be BpHwServiceManager,
226 // BpHwFoo, or any other class. For ABI compatibility, we can't modify the
227 // class hierarchy of these, so we have no way to get BpHwRefBase from a
228 // remote ifacePtr.
229 BpHwBase* bpBase = static_cast<BpHwBase*>(ifacePtr);
230 return static_cast<BpHwRefBase*>(bpBase);
231}
232
Steven Moreland9cbef742018-06-26 16:23:50 -0700233sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr) {
Steven Moreland986fa092018-09-27 10:38:03 -0700234 if (ifacePtr == nullptr) {
235 return nullptr;
236 }
237
238 if (ifacePtr->isRemote()) {
Steven Moreland347615c2020-03-09 18:36:40 -0700239 BpHwRefBase* bpRefBase = forceGetRefBase(ifacePtr);
Steven Moreland986fa092018-09-27 10:38:03 -0700240 return sp<IBinder>(bpRefBase->remote());
241 }
Steven Moreland9cbef742018-06-26 16:23:50 -0700242
243 std::string descriptor = details::getDescriptor(ifacePtr);
244 if (descriptor.empty()) {
245 // interfaceDescriptor fails
246 return nullptr;
247 }
248
249 // for get + set
Steven Morelandb1c7d062019-04-22 10:27:47 -0700250 std::unique_lock<std::mutex> _lock = details::gBnMap->lock();
Steven Moreland9cbef742018-06-26 16:23:50 -0700251
Steven Morelandb1c7d062019-04-22 10:27:47 -0700252 wp<BHwBinder> wBnObj = details::gBnMap->getLocked(ifacePtr, nullptr);
Steven Moreland9cbef742018-06-26 16:23:50 -0700253 sp<IBinder> sBnObj = wBnObj.promote();
254
255 if (sBnObj == nullptr) {
256 auto func = details::getBnConstructorMap().get(descriptor, nullptr);
257 if (!func) {
258 // TODO(b/69122224): remove this static variable when prebuilts updated
Steven Morelandb1c7d062019-04-22 10:27:47 -0700259 func = details::gBnConstructorMap->get(descriptor, nullptr);
Steven Moreland9cbef742018-06-26 16:23:50 -0700260 }
Steven Moreland9fd33872018-06-26 18:14:22 -0700261 LOG_ALWAYS_FATAL_IF(func == nullptr, "%s gBnConstructorMap returned null for %s", __func__,
262 descriptor.c_str());
Steven Moreland9cbef742018-06-26 16:23:50 -0700263
264 sBnObj = sp<IBinder>(func(static_cast<void*>(ifacePtr)));
Steven Moreland9fd33872018-06-26 18:14:22 -0700265 LOG_ALWAYS_FATAL_IF(sBnObj == nullptr, "%s Bn constructor function returned null for %s",
266 __func__, descriptor.c_str());
Steven Moreland9cbef742018-06-26 16:23:50 -0700267
Steven Morelandb1c7d062019-04-22 10:27:47 -0700268 details::gBnMap->setLocked(ifacePtr, static_cast<BHwBinder*>(sBnObj.get()));
Steven Moreland9cbef742018-06-26 16:23:50 -0700269 }
270
271 return sBnObj;
272}
273
Steven Moreland50815be2018-05-24 12:35:44 -0700274static bool gThreadPoolConfigured = false;
275
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800276void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
Steven Moreland50815be2018-05-24 12:35:44 -0700277 status_t ret = ProcessState::self()->setThreadPoolConfiguration(
278 maxThreads, callerWillJoin /*callerJoinsPool*/);
279 LOG_ALWAYS_FATAL_IF(ret != OK, "Could not setThreadPoolConfiguration: %d", ret);
280
281 gThreadPoolConfigured = true;
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800282}
283
284void joinBinderRpcThreadpool() {
Steven Morelandcd329a82018-05-29 13:30:11 -0700285 LOG_ALWAYS_FATAL_IF(!gThreadPoolConfigured,
286 "HIDL joinRpcThreadpool without calling configureRpcThreadPool.");
Steven Moreland6cf8fa22017-02-21 13:38:17 -0800287 IPCThreadState::self()->joinThreadPool();
288}
289
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800290int setupBinderPolling() {
291 int fd;
292 int err = IPCThreadState::self()->setupPolling(&fd);
293
Steven Morelandcd329a82018-05-29 13:30:11 -0700294 LOG_ALWAYS_FATAL_IF(err != OK, "Failed to setup binder polling: %d (%s)", err, strerror(err));
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -0800295
296 return err == OK ? fd : -1;
297}
298
299status_t handleBinderPoll() {
300 return IPCThreadState::self()->handlePolledCommands();
301}
302
Steven Morelandd40af8e2018-05-01 16:33:21 -0700303void addPostCommandTask(const std::function<void(void)> task) {
304 IPCThreadState::self()->addPostCommandTask(task);
305}
306
Yifan Hong7f97f442016-11-14 18:31:05 -0800307} // namespace hardware
308} // namespace android