Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 1 | /* |
| 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 Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 21 | #include <InternalStatic.h> // TODO(b/69122224): remove this include, for getOrCreateCachedBinder |
Steven Moreland | 986fa09 | 2018-09-27 10:38:03 -0700 | [diff] [blame] | 22 | #include <android/hidl/base/1.0/BpHwBase.h> |
Steven Moreland | b917318 | 2018-08-08 20:06:08 +0000 | [diff] [blame] | 23 | #include <hwbinder/IPCThreadState.h> |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 24 | |
Yifan Hong | 777bef9 | 2017-02-01 15:50:36 -0800 | [diff] [blame] | 25 | // C includes |
Steven Moreland | 45f69c6 | 2018-06-06 16:28:29 -0700 | [diff] [blame] | 26 | #include <inttypes.h> |
Yifan Hong | 777bef9 | 2017-02-01 15:50:36 -0800 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
| 29 | // C++ includes |
| 30 | #include <fstream> |
| 31 | #include <sstream> |
| 32 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace hardware { |
| 35 | |
Steven Moreland | 108d09d | 2017-05-05 16:15:38 -0700 | [diff] [blame] | 36 | hidl_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 | |
| 41 | void hidl_binder_death_recipient::binderDied(const wp<IBinder>& /*who*/) { |
| 42 | sp<hidl_death_recipient> recipient = mRecipient.promote(); |
Martijn Coenen | 2550880 | 2017-07-05 12:21:00 +0200 | [diff] [blame] | 43 | if (recipient != nullptr && mBase != nullptr) { |
Steven Moreland | 108d09d | 2017-05-05 16:15:38 -0700 | [diff] [blame] | 44 | recipient->serviceDied(mCookie, mBase); |
| 45 | } |
Martijn Coenen | 2550880 | 2017-07-05 12:21:00 +0200 | [diff] [blame] | 46 | mBase = nullptr; |
Steven Moreland | 108d09d | 2017-05-05 16:15:38 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | wp<hidl_death_recipient> hidl_binder_death_recipient::getRecipient() { |
| 50 | return mRecipient; |
| 51 | } |
| 52 | |
Nirav Atre | 564a8d2 | 2018-07-26 18:29:12 -0700 | [diff] [blame] | 53 | const size_t hidl_handle::kOffsetOfNativeHandle = offsetof(hidl_handle, mHandle); |
| 54 | static_assert(hidl_handle::kOffsetOfNativeHandle == 0, "wrong offset"); |
| 55 | |
| 56 | status_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 | |
| 67 | status_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 Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 77 | const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle); |
| 78 | const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName); |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 79 | static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset"); |
| 80 | static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset"); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 81 | |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 82 | status_t readEmbeddedFromParcel(const hidl_memory& memory, |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 83 | const Parcel &parcel, size_t parentHandle, size_t parentOffset) { |
Nirav Atre | 564a8d2 | 2018-07-26 18:29:12 -0700 | [diff] [blame] | 84 | // TODO(b/111883309): Invoke readEmbeddedFromParcel(hidl_handle, ...). |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 85 | const native_handle_t *handle; |
| 86 | ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle( |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 87 | parentHandle, |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 88 | parentOffset + hidl_memory::kOffsetOfHandle, |
| 89 | &handle); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 90 | |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 91 | if (_hidl_err == ::android::OK) { |
| 92 | _hidl_err = readEmbeddedFromParcel( |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 93 | memory.name(), |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 94 | parcel, |
| 95 | parentHandle, |
| 96 | parentOffset + hidl_memory::kOffsetOfName); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Steven Moreland | 45f69c6 | 2018-06-06 16:28:29 -0700 | [diff] [blame] | 99 | // 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 Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 108 | return _hidl_err; |
| 109 | } |
| 110 | |
| 111 | status_t writeEmbeddedToParcel(const hidl_memory &memory, |
| 112 | Parcel *parcel, size_t parentHandle, size_t parentOffset) { |
Nirav Atre | 564a8d2 | 2018-07-26 18:29:12 -0700 | [diff] [blame] | 113 | // TODO(b/111883309): Invoke writeEmbeddedToParcel(hidl_handle, ...). |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 114 | 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 Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 129 | const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer); |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 130 | static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset"); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 131 | |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 132 | status_t readEmbeddedFromParcel(const hidl_string &string , |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 133 | const Parcel &parcel, size_t parentHandle, size_t parentOffset) { |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 134 | const void *out; |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 135 | |
| 136 | status_t status = parcel.readEmbeddedBuffer( |
| 137 | string.size() + 1, |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 138 | nullptr /* buffer_handle */, |
| 139 | parentHandle, |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 140 | parentOffset + hidl_string::kOffsetOfBuffer, |
| 141 | &out); |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 142 | |
| 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 Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | status_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 Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 166 | status_t readFromParcel(Status *s, const Parcel& parcel) { |
| 167 | int32_t exception; |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 168 | status_t status = parcel.readInt32(&exception); |
| 169 | if (status != OK) { |
| 170 | s->setFromStatusT(status); |
| 171 | return status; |
| 172 | } |
| 173 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 174 | if (exception == Status::EX_NONE) { |
| 175 | *s = Status::ok(); |
| 176 | return status; |
| 177 | } |
| 178 | |
| 179 | // The remote threw an exception. Get the message back. |
| 180 | String16 message; |
| 181 | status = parcel.readString16(&message); |
| 182 | if (status != OK) { |
| 183 | s->setFromStatusT(status); |
| 184 | return status; |
| 185 | } |
| 186 | |
Steven Moreland | 72db40f | 2017-03-09 18:15:27 -0800 | [diff] [blame] | 187 | s->setException(exception, String8(message)); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 188 | |
| 189 | return status; |
| 190 | } |
| 191 | |
| 192 | status_t writeToParcel(const Status &s, Parcel* parcel) { |
| 193 | // Something really bad has happened, and we're not going to even |
| 194 | // try returning rich error data. |
| 195 | if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) { |
Steven Moreland | 872da2d | 2018-10-12 15:12:39 -0700 | [diff] [blame] | 196 | return s.transactionError(); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | status_t status = parcel->writeInt32(s.exceptionCode()); |
| 200 | if (status != OK) { return status; } |
| 201 | if (s.exceptionCode() == Status::EX_NONE) { |
| 202 | // We have no more information to write. |
| 203 | return status; |
| 204 | } |
| 205 | status = parcel->writeString16(String16(s.exceptionMessage())); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 206 | return status; |
| 207 | } |
| 208 | |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 209 | sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr) { |
Steven Moreland | 986fa09 | 2018-09-27 10:38:03 -0700 | [diff] [blame] | 210 | if (ifacePtr == nullptr) { |
| 211 | return nullptr; |
| 212 | } |
| 213 | |
| 214 | if (ifacePtr->isRemote()) { |
| 215 | using ::android::hidl::base::V1_0::BpHwBase; |
| 216 | |
| 217 | BpHwBase* bpBase = static_cast<BpHwBase*>(ifacePtr); |
| 218 | BpHwRefBase* bpRefBase = static_cast<BpHwRefBase*>(bpBase); |
| 219 | return sp<IBinder>(bpRefBase->remote()); |
| 220 | } |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 221 | |
| 222 | std::string descriptor = details::getDescriptor(ifacePtr); |
| 223 | if (descriptor.empty()) { |
| 224 | // interfaceDescriptor fails |
| 225 | return nullptr; |
| 226 | } |
| 227 | |
| 228 | // for get + set |
Steven Moreland | b1c7d06 | 2019-04-22 10:27:47 -0700 | [diff] [blame] | 229 | std::unique_lock<std::mutex> _lock = details::gBnMap->lock(); |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 230 | |
Steven Moreland | b1c7d06 | 2019-04-22 10:27:47 -0700 | [diff] [blame] | 231 | wp<BHwBinder> wBnObj = details::gBnMap->getLocked(ifacePtr, nullptr); |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 232 | sp<IBinder> sBnObj = wBnObj.promote(); |
| 233 | |
| 234 | if (sBnObj == nullptr) { |
| 235 | auto func = details::getBnConstructorMap().get(descriptor, nullptr); |
| 236 | if (!func) { |
| 237 | // TODO(b/69122224): remove this static variable when prebuilts updated |
Steven Moreland | b1c7d06 | 2019-04-22 10:27:47 -0700 | [diff] [blame] | 238 | func = details::gBnConstructorMap->get(descriptor, nullptr); |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 239 | } |
Steven Moreland | 9fd3387 | 2018-06-26 18:14:22 -0700 | [diff] [blame] | 240 | LOG_ALWAYS_FATAL_IF(func == nullptr, "%s gBnConstructorMap returned null for %s", __func__, |
| 241 | descriptor.c_str()); |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 242 | |
| 243 | sBnObj = sp<IBinder>(func(static_cast<void*>(ifacePtr))); |
Steven Moreland | 9fd3387 | 2018-06-26 18:14:22 -0700 | [diff] [blame] | 244 | LOG_ALWAYS_FATAL_IF(sBnObj == nullptr, "%s Bn constructor function returned null for %s", |
| 245 | __func__, descriptor.c_str()); |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 246 | |
Steven Moreland | b1c7d06 | 2019-04-22 10:27:47 -0700 | [diff] [blame] | 247 | details::gBnMap->setLocked(ifacePtr, static_cast<BHwBinder*>(sBnObj.get())); |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | return sBnObj; |
| 251 | } |
| 252 | |
Steven Moreland | 50815be | 2018-05-24 12:35:44 -0700 | [diff] [blame] | 253 | static bool gThreadPoolConfigured = false; |
| 254 | |
Steven Moreland | 6cf8fa2 | 2017-02-21 13:38:17 -0800 | [diff] [blame] | 255 | void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) { |
Steven Moreland | 50815be | 2018-05-24 12:35:44 -0700 | [diff] [blame] | 256 | status_t ret = ProcessState::self()->setThreadPoolConfiguration( |
| 257 | maxThreads, callerWillJoin /*callerJoinsPool*/); |
| 258 | LOG_ALWAYS_FATAL_IF(ret != OK, "Could not setThreadPoolConfiguration: %d", ret); |
| 259 | |
| 260 | gThreadPoolConfigured = true; |
Steven Moreland | 6cf8fa2 | 2017-02-21 13:38:17 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void joinBinderRpcThreadpool() { |
Steven Moreland | cd329a8 | 2018-05-29 13:30:11 -0700 | [diff] [blame] | 264 | LOG_ALWAYS_FATAL_IF(!gThreadPoolConfigured, |
| 265 | "HIDL joinRpcThreadpool without calling configureRpcThreadPool."); |
Steven Moreland | 6cf8fa2 | 2017-02-21 13:38:17 -0800 | [diff] [blame] | 266 | IPCThreadState::self()->joinThreadPool(); |
| 267 | } |
| 268 | |
Martijn Coenen | 3f5ac4c | 2017-11-27 15:09:28 -0800 | [diff] [blame] | 269 | int setupBinderPolling() { |
| 270 | int fd; |
| 271 | int err = IPCThreadState::self()->setupPolling(&fd); |
| 272 | |
Steven Moreland | cd329a8 | 2018-05-29 13:30:11 -0700 | [diff] [blame] | 273 | LOG_ALWAYS_FATAL_IF(err != OK, "Failed to setup binder polling: %d (%s)", err, strerror(err)); |
Martijn Coenen | 3f5ac4c | 2017-11-27 15:09:28 -0800 | [diff] [blame] | 274 | |
| 275 | return err == OK ? fd : -1; |
| 276 | } |
| 277 | |
| 278 | status_t handleBinderPoll() { |
| 279 | return IPCThreadState::self()->handlePolledCommands(); |
| 280 | } |
| 281 | |
Steven Moreland | d40af8e | 2018-05-01 16:33:21 -0700 | [diff] [blame] | 282 | void addPostCommandTask(const std::function<void(void)> task) { |
| 283 | IPCThreadState::self()->addPostCommandTask(task); |
| 284 | } |
| 285 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 286 | } // namespace hardware |
| 287 | } // namespace android |