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 |
| 22 | |
Yifan Hong | 777bef9 | 2017-02-01 15:50:36 -0800 | [diff] [blame] | 23 | // C includes |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | // C++ includes |
| 27 | #include <fstream> |
| 28 | #include <sstream> |
| 29 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace hardware { |
| 32 | |
Steven Moreland | 108d09d | 2017-05-05 16:15:38 -0700 | [diff] [blame] | 33 | hidl_binder_death_recipient::hidl_binder_death_recipient(const sp<hidl_death_recipient> &recipient, |
| 34 | uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base) : |
| 35 | mRecipient(recipient), mCookie(cookie), mBase(base) { |
| 36 | } |
| 37 | |
| 38 | void hidl_binder_death_recipient::binderDied(const wp<IBinder>& /*who*/) { |
| 39 | sp<hidl_death_recipient> recipient = mRecipient.promote(); |
Martijn Coenen | 2550880 | 2017-07-05 12:21:00 +0200 | [diff] [blame] | 40 | if (recipient != nullptr && mBase != nullptr) { |
Steven Moreland | 108d09d | 2017-05-05 16:15:38 -0700 | [diff] [blame] | 41 | recipient->serviceDied(mCookie, mBase); |
| 42 | } |
Martijn Coenen | 2550880 | 2017-07-05 12:21:00 +0200 | [diff] [blame] | 43 | mBase = nullptr; |
Steven Moreland | 108d09d | 2017-05-05 16:15:38 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | wp<hidl_death_recipient> hidl_binder_death_recipient::getRecipient() { |
| 47 | return mRecipient; |
| 48 | } |
| 49 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 50 | const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle); |
| 51 | const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName); |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 52 | static_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset"); |
| 53 | static_assert(hidl_memory::kOffsetOfName == 24, "wrong offset"); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 54 | |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 55 | status_t readEmbeddedFromParcel(const hidl_memory& memory, |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 56 | const Parcel &parcel, size_t parentHandle, size_t parentOffset) { |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 57 | const native_handle_t *handle; |
| 58 | ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle( |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 59 | parentHandle, |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 60 | parentOffset + hidl_memory::kOffsetOfHandle, |
| 61 | &handle); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 62 | |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 63 | if (_hidl_err == ::android::OK) { |
| 64 | _hidl_err = readEmbeddedFromParcel( |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 65 | memory.name(), |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 66 | parcel, |
| 67 | parentHandle, |
| 68 | parentOffset + hidl_memory::kOffsetOfName); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 71 | return _hidl_err; |
| 72 | } |
| 73 | |
| 74 | status_t writeEmbeddedToParcel(const hidl_memory &memory, |
| 75 | Parcel *parcel, size_t parentHandle, size_t parentOffset) { |
| 76 | status_t _hidl_err = parcel->writeEmbeddedNativeHandle( |
| 77 | memory.handle(), |
| 78 | parentHandle, |
| 79 | parentOffset + hidl_memory::kOffsetOfHandle); |
| 80 | |
| 81 | if (_hidl_err == ::android::OK) { |
| 82 | _hidl_err = writeEmbeddedToParcel( |
| 83 | memory.name(), |
| 84 | parcel, |
| 85 | parentHandle, |
| 86 | parentOffset + hidl_memory::kOffsetOfName); |
| 87 | } |
| 88 | |
| 89 | return _hidl_err; |
| 90 | } |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 91 | const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer); |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 92 | static_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset"); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 93 | |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 94 | status_t readEmbeddedFromParcel(const hidl_string &string , |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 95 | const Parcel &parcel, size_t parentHandle, size_t parentOffset) { |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 96 | const void *out; |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 97 | |
| 98 | status_t status = parcel.readEmbeddedBuffer( |
| 99 | string.size() + 1, |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 100 | nullptr /* buffer_handle */, |
| 101 | parentHandle, |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 102 | parentOffset + hidl_string::kOffsetOfBuffer, |
| 103 | &out); |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 104 | |
| 105 | if (status != OK) { |
| 106 | return status; |
| 107 | } |
| 108 | |
| 109 | // Always safe to access out[string.size()] because we read size+1 bytes |
| 110 | if (static_cast<const char *>(out)[string.size()] != '\0') { |
| 111 | ALOGE("Received unterminated hidl_string buffer."); |
| 112 | return BAD_VALUE; |
| 113 | } |
| 114 | |
| 115 | return OK; |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | status_t writeEmbeddedToParcel(const hidl_string &string, |
| 119 | Parcel *parcel, size_t parentHandle, size_t parentOffset) { |
| 120 | return parcel->writeEmbeddedBuffer( |
| 121 | string.c_str(), |
| 122 | string.size() + 1, |
| 123 | nullptr /* handle */, |
| 124 | parentHandle, |
| 125 | parentOffset + hidl_string::kOffsetOfBuffer); |
| 126 | } |
| 127 | |
| 128 | android::status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel) { |
| 129 | return parcel.writeUint32(static_cast<uint32_t>(version.get_major()) << 16 | version.get_minor()); |
| 130 | } |
| 131 | |
| 132 | hidl_version* readFromParcel(const android::hardware::Parcel& parcel) { |
| 133 | uint32_t version; |
| 134 | android::status_t status = parcel.readUint32(&version); |
| 135 | if (status != OK) { |
| 136 | return nullptr; |
| 137 | } else { |
| 138 | return new hidl_version(version >> 16, version & 0xFFFF); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | status_t readFromParcel(Status *s, const Parcel& parcel) { |
| 143 | int32_t exception; |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 144 | status_t status = parcel.readInt32(&exception); |
| 145 | if (status != OK) { |
| 146 | s->setFromStatusT(status); |
| 147 | return status; |
| 148 | } |
| 149 | |
| 150 | // Skip over fat response headers. Not used (or propagated) in native code. |
| 151 | if (exception == Status::EX_HAS_REPLY_HEADER) { |
| 152 | // Note that the header size includes the 4 byte size field. |
| 153 | const int32_t header_start = parcel.dataPosition(); |
| 154 | int32_t header_size; |
| 155 | status = parcel.readInt32(&header_size); |
| 156 | if (status != OK) { |
| 157 | s->setFromStatusT(status); |
| 158 | return status; |
| 159 | } |
| 160 | parcel.setDataPosition(header_start + header_size); |
| 161 | // And fat response headers are currently only used when there are no |
| 162 | // exceptions, so act like there was no error. |
| 163 | exception = Status::EX_NONE; |
| 164 | } |
| 165 | |
| 166 | if (exception == Status::EX_NONE) { |
| 167 | *s = Status::ok(); |
| 168 | return status; |
| 169 | } |
| 170 | |
| 171 | // The remote threw an exception. Get the message back. |
| 172 | String16 message; |
| 173 | status = parcel.readString16(&message); |
| 174 | if (status != OK) { |
| 175 | s->setFromStatusT(status); |
| 176 | return status; |
| 177 | } |
| 178 | |
Steven Moreland | 72db40f | 2017-03-09 18:15:27 -0800 | [diff] [blame] | 179 | s->setException(exception, String8(message)); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 180 | |
| 181 | return status; |
| 182 | } |
| 183 | |
| 184 | status_t writeToParcel(const Status &s, Parcel* parcel) { |
| 185 | // Something really bad has happened, and we're not going to even |
| 186 | // try returning rich error data. |
| 187 | if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) { |
| 188 | return s.transactionError(); |
| 189 | } |
| 190 | |
| 191 | status_t status = parcel->writeInt32(s.exceptionCode()); |
| 192 | if (status != OK) { return status; } |
| 193 | if (s.exceptionCode() == Status::EX_NONE) { |
| 194 | // We have no more information to write. |
| 195 | return status; |
| 196 | } |
| 197 | status = parcel->writeString16(String16(s.exceptionMessage())); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 198 | return status; |
| 199 | } |
| 200 | |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame^] | 201 | sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr) { |
| 202 | LOG_ALWAYS_FATAL_IF(ifacePtr->isRemote(), |
| 203 | "getOrCreateCachedBinder does not have a way to construct remote binders"); |
| 204 | |
| 205 | std::string descriptor = details::getDescriptor(ifacePtr); |
| 206 | if (descriptor.empty()) { |
| 207 | // interfaceDescriptor fails |
| 208 | return nullptr; |
| 209 | } |
| 210 | |
| 211 | // for get + set |
| 212 | std::unique_lock<std::mutex> _lock = details::gBnMap.lock(); |
| 213 | |
| 214 | wp<BHwBinder> wBnObj = details::gBnMap.getLocked(ifacePtr, nullptr); |
| 215 | sp<IBinder> sBnObj = wBnObj.promote(); |
| 216 | |
| 217 | if (sBnObj == nullptr) { |
| 218 | auto func = details::getBnConstructorMap().get(descriptor, nullptr); |
| 219 | if (!func) { |
| 220 | // TODO(b/69122224): remove this static variable when prebuilts updated |
| 221 | func = details::gBnConstructorMap.get(descriptor, nullptr); |
| 222 | if (!func) { |
| 223 | return nullptr; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | sBnObj = sp<IBinder>(func(static_cast<void*>(ifacePtr))); |
| 228 | |
| 229 | if (sBnObj != nullptr) { |
| 230 | details::gBnMap.setLocked(ifacePtr, static_cast<BHwBinder*>(sBnObj.get())); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return sBnObj; |
| 235 | } |
| 236 | |
Steven Moreland | 50815be | 2018-05-24 12:35:44 -0700 | [diff] [blame] | 237 | static bool gThreadPoolConfigured = false; |
| 238 | |
Steven Moreland | 6cf8fa2 | 2017-02-21 13:38:17 -0800 | [diff] [blame] | 239 | void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) { |
Steven Moreland | 50815be | 2018-05-24 12:35:44 -0700 | [diff] [blame] | 240 | status_t ret = ProcessState::self()->setThreadPoolConfiguration( |
| 241 | maxThreads, callerWillJoin /*callerJoinsPool*/); |
| 242 | LOG_ALWAYS_FATAL_IF(ret != OK, "Could not setThreadPoolConfiguration: %d", ret); |
| 243 | |
| 244 | gThreadPoolConfigured = true; |
Steven Moreland | 6cf8fa2 | 2017-02-21 13:38:17 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void joinBinderRpcThreadpool() { |
Steven Moreland | cd329a8 | 2018-05-29 13:30:11 -0700 | [diff] [blame] | 248 | LOG_ALWAYS_FATAL_IF(!gThreadPoolConfigured, |
| 249 | "HIDL joinRpcThreadpool without calling configureRpcThreadPool."); |
Steven Moreland | 6cf8fa2 | 2017-02-21 13:38:17 -0800 | [diff] [blame] | 250 | IPCThreadState::self()->joinThreadPool(); |
| 251 | } |
| 252 | |
Martijn Coenen | 3f5ac4c | 2017-11-27 15:09:28 -0800 | [diff] [blame] | 253 | int setupBinderPolling() { |
| 254 | int fd; |
| 255 | int err = IPCThreadState::self()->setupPolling(&fd); |
| 256 | |
Steven Moreland | cd329a8 | 2018-05-29 13:30:11 -0700 | [diff] [blame] | 257 | 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] | 258 | |
| 259 | return err == OK ? fd : -1; |
| 260 | } |
| 261 | |
| 262 | status_t handleBinderPoll() { |
| 263 | return IPCThreadState::self()->handlePolledCommands(); |
| 264 | } |
| 265 | |
Steven Moreland | d40af8e | 2018-05-01 16:33:21 -0700 | [diff] [blame] | 266 | void addPostCommandTask(const std::function<void(void)> task) { |
| 267 | IPCThreadState::self()->addPostCommandTask(task); |
| 268 | } |
| 269 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 270 | } // namespace hardware |
| 271 | } // namespace android |