Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [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 | */ |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 16 | #define LOG_TAG "HidlSupport" |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 17 | |
| 18 | #include <hidl/HidlSupport.h> |
| 19 | |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 20 | #include <unordered_map> |
| 21 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Yifan Hong | 20273f9 | 2017-01-30 14:13:19 -0800 | [diff] [blame] | 23 | #include <android-base/parseint.h> |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 24 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | |
Yifan Hong | 24332ef | 2017-03-07 16:22:19 -0800 | [diff] [blame] | 28 | namespace details { |
| 29 | bool debuggable() { |
| 30 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 31 | return true; |
| 32 | #else |
| 33 | return false; |
| 34 | #endif |
| 35 | } |
| 36 | } // namespace details |
| 37 | |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 38 | hidl_handle::hidl_handle() : mHandle(nullptr), mOwnsHandle(false) { |
| 39 | memset(mPad, 0, sizeof(mPad)); |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | hidl_handle::~hidl_handle() { |
| 43 | freeHandle(); |
| 44 | } |
| 45 | |
Steven Moreland | 5cc1691 | 2019-04-18 12:54:56 -0700 | [diff] [blame] | 46 | hidl_handle::hidl_handle(const native_handle_t* handle) : hidl_handle() { |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 47 | mHandle = handle; |
| 48 | mOwnsHandle = false; |
| 49 | } |
| 50 | |
| 51 | // copy constructor. |
Steven Moreland | 5cc1691 | 2019-04-18 12:54:56 -0700 | [diff] [blame] | 52 | hidl_handle::hidl_handle(const hidl_handle& other) : hidl_handle() { |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 53 | mOwnsHandle = false; |
| 54 | *this = other; |
| 55 | } |
| 56 | |
| 57 | // move constructor. |
Steven Moreland | 5cc1691 | 2019-04-18 12:54:56 -0700 | [diff] [blame] | 58 | hidl_handle::hidl_handle(hidl_handle&& other) noexcept : hidl_handle() { |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 59 | mOwnsHandle = false; |
| 60 | *this = std::move(other); |
| 61 | } |
| 62 | |
| 63 | // assignment operators |
| 64 | hidl_handle &hidl_handle::operator=(const hidl_handle &other) { |
| 65 | if (this == &other) { |
| 66 | return *this; |
| 67 | } |
| 68 | freeHandle(); |
| 69 | if (other.mHandle != nullptr) { |
| 70 | mHandle = native_handle_clone(other.mHandle); |
| 71 | if (mHandle == nullptr) { |
Elliott Hughes | 0e55b45 | 2017-05-01 21:38:48 -0700 | [diff] [blame] | 72 | PLOG(FATAL) << "Failed to clone native_handle in hidl_handle"; |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 73 | } |
| 74 | mOwnsHandle = true; |
| 75 | } else { |
| 76 | mHandle = nullptr; |
| 77 | mOwnsHandle = false; |
| 78 | } |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | hidl_handle &hidl_handle::operator=(const native_handle_t *native_handle) { |
| 83 | freeHandle(); |
| 84 | mHandle = native_handle; |
| 85 | mOwnsHandle = false; |
| 86 | return *this; |
| 87 | } |
| 88 | |
Chih-Hung Hsieh | 3833f20 | 2018-09-25 12:03:06 -0700 | [diff] [blame] | 89 | hidl_handle& hidl_handle::operator=(hidl_handle&& other) noexcept { |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 90 | if (this != &other) { |
| 91 | freeHandle(); |
| 92 | mHandle = other.mHandle; |
| 93 | mOwnsHandle = other.mOwnsHandle; |
| 94 | other.mHandle = nullptr; |
| 95 | other.mOwnsHandle = false; |
| 96 | } |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) { |
Scott Randolph | ca37c0e | 2017-02-15 16:38:46 -0800 | [diff] [blame] | 101 | freeHandle(); |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 102 | mHandle = handle; |
| 103 | mOwnsHandle = shouldOwn; |
| 104 | } |
| 105 | |
| 106 | const native_handle_t* hidl_handle::operator->() const { |
| 107 | return mHandle; |
| 108 | } |
| 109 | |
| 110 | // implicit conversion to const native_handle_t* |
| 111 | hidl_handle::operator const native_handle_t *() const { |
| 112 | return mHandle; |
| 113 | } |
| 114 | |
| 115 | // explicit conversion |
| 116 | const native_handle_t *hidl_handle::getNativeHandle() const { |
| 117 | return mHandle; |
| 118 | } |
| 119 | |
| 120 | void hidl_handle::freeHandle() { |
| 121 | if (mOwnsHandle && mHandle != nullptr) { |
| 122 | // This can only be true if: |
| 123 | // 1. Somebody called setTo() with shouldOwn=true, so we know the handle |
| 124 | // wasn't const to begin with. |
| 125 | // 2. Copy/assignment from another hidl_handle, in which case we have |
| 126 | // cloned the handle. |
| 127 | // 3. Move constructor from another hidl_handle, in which case the original |
| 128 | // hidl_handle must have been non-const as well. |
| 129 | native_handle_t *handle = const_cast<native_handle_t*>( |
| 130 | static_cast<const native_handle_t*>(mHandle)); |
| 131 | native_handle_close(handle); |
| 132 | native_handle_delete(handle); |
| 133 | mHandle = nullptr; |
| 134 | } |
| 135 | } |
| 136 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 137 | static const char *const kEmptyString = ""; |
| 138 | |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 139 | hidl_string::hidl_string() : mBuffer(kEmptyString), mSize(0), mOwnsBuffer(false) { |
| 140 | memset(mPad, 0, sizeof(mPad)); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | hidl_string::~hidl_string() { |
| 144 | clear(); |
| 145 | } |
| 146 | |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 147 | hidl_string::hidl_string(const char *s) : hidl_string() { |
Steven Moreland | a21d84f | 2017-02-16 09:23:45 -0800 | [diff] [blame] | 148 | if (s == nullptr) { |
| 149 | return; |
| 150 | } |
| 151 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 152 | copyFrom(s, strlen(s)); |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 155 | hidl_string::hidl_string(const char *s, size_t length) : hidl_string() { |
| 156 | copyFrom(s, length); |
| 157 | } |
| 158 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 159 | hidl_string::hidl_string(const hidl_string &other): hidl_string() { |
| 160 | copyFrom(other.c_str(), other.size()); |
| 161 | } |
| 162 | |
| 163 | hidl_string::hidl_string(const std::string &s) : hidl_string() { |
| 164 | copyFrom(s.c_str(), s.size()); |
| 165 | } |
| 166 | |
Chih-Hung Hsieh | 3833f20 | 2018-09-25 12:03:06 -0700 | [diff] [blame] | 167 | hidl_string::hidl_string(hidl_string&& other) noexcept : hidl_string() { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 168 | moveFrom(std::forward<hidl_string>(other)); |
| 169 | } |
| 170 | |
Chih-Hung Hsieh | 3833f20 | 2018-09-25 12:03:06 -0700 | [diff] [blame] | 171 | hidl_string& hidl_string::operator=(hidl_string&& other) noexcept { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 172 | if (this != &other) { |
| 173 | clear(); |
| 174 | moveFrom(std::forward<hidl_string>(other)); |
| 175 | } |
| 176 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | hidl_string &hidl_string::operator=(const hidl_string &other) { |
| 180 | if (this != &other) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 181 | clear(); |
| 182 | copyFrom(other.c_str(), other.size()); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | return *this; |
| 186 | } |
| 187 | |
| 188 | hidl_string &hidl_string::operator=(const char *s) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 189 | clear(); |
Steven Moreland | 153f87a | 2017-02-28 09:42:26 -0800 | [diff] [blame] | 190 | |
| 191 | if (s == nullptr) { |
| 192 | return *this; |
| 193 | } |
| 194 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 195 | copyFrom(s, strlen(s)); |
| 196 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 199 | hidl_string &hidl_string::operator=(const std::string &s) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 200 | clear(); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 201 | copyFrom(s.c_str(), s.size()); |
| 202 | return *this; |
| 203 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 204 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 205 | hidl_string::operator std::string() const { |
| 206 | return std::string(mBuffer, mSize); |
| 207 | } |
| 208 | |
Scott Randolph | 0c84ab4 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 209 | std::ostream& operator<<(std::ostream& os, const hidl_string& str) { |
| 210 | os << str.c_str(); |
| 211 | return os; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void hidl_string::copyFrom(const char *data, size_t size) { |
| 215 | // assume my resources are freed. |
| 216 | |
Steven Moreland | 9889380 | 2017-10-24 18:12:34 -0700 | [diff] [blame] | 217 | if (size >= UINT32_MAX) { |
Elliott Hughes | 0e55b45 | 2017-05-01 21:38:48 -0700 | [diff] [blame] | 218 | LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 219 | } |
Steven Moreland | e5b0df2 | 2021-03-29 17:28:48 +0000 | [diff] [blame] | 220 | |
| 221 | if (size == 0) { |
| 222 | mBuffer = kEmptyString; |
| 223 | mSize = 0; |
| 224 | mOwnsBuffer = false; |
| 225 | return; |
| 226 | } |
| 227 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 228 | char *buf = (char *)malloc(size + 1); |
| 229 | memcpy(buf, data, size); |
| 230 | buf[size] = '\0'; |
| 231 | mBuffer = buf; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 232 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 233 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 234 | mOwnsBuffer = true; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 235 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 236 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 237 | void hidl_string::moveFrom(hidl_string &&other) { |
| 238 | // assume my resources are freed. |
| 239 | |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 240 | mBuffer = std::move(other.mBuffer); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 241 | mSize = other.mSize; |
| 242 | mOwnsBuffer = other.mOwnsBuffer; |
| 243 | |
| 244 | other.mOwnsBuffer = false; |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 245 | other.clear(); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void hidl_string::clear() { |
| 249 | if (mOwnsBuffer && (mBuffer != kEmptyString)) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 250 | free(const_cast<char *>(static_cast<const char *>(mBuffer))); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 253 | mBuffer = kEmptyString; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 254 | mSize = 0; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 255 | mOwnsBuffer = false; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void hidl_string::setToExternal(const char *data, size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 259 | if (size > UINT32_MAX) { |
Elliott Hughes | 0e55b45 | 2017-05-01 21:38:48 -0700 | [diff] [blame] | 260 | LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 261 | } |
Steven Moreland | 57c6fcb | 2018-03-06 11:31:33 -0800 | [diff] [blame] | 262 | |
| 263 | // When the binder driver copies this data into its buffer, it must |
| 264 | // have a zero byte there because the remote process will have a pointer |
| 265 | // directly into the read-only binder buffer. If we manually copy the |
| 266 | // data now to add a zero, then we lose the efficiency of this method. |
| 267 | // Checking here (it's also checked in the parceling code later). |
| 268 | CHECK(data[size] == '\0'); |
| 269 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 270 | clear(); |
| 271 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 272 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 273 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 274 | mOwnsBuffer = false; |
| 275 | } |
| 276 | |
| 277 | const char *hidl_string::c_str() const { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 278 | return mBuffer; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | size_t hidl_string::size() const { |
| 282 | return mSize; |
| 283 | } |
| 284 | |
| 285 | bool hidl_string::empty() const { |
| 286 | return mSize == 0; |
| 287 | } |
| 288 | |
Howard Chen | 8e42f5a | 2017-11-09 13:58:28 +0800 | [diff] [blame] | 289 | sp<HidlMemory> HidlMemory::getInstance(const hidl_memory& mem) { |
| 290 | sp<HidlMemory> instance = new HidlMemory(); |
| 291 | instance->hidl_memory::operator=(mem); |
| 292 | return instance; |
| 293 | } |
| 294 | |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame] | 295 | sp<HidlMemory> HidlMemory::getInstance(hidl_memory&& mem) { |
| 296 | sp<HidlMemory> instance = new HidlMemory(); |
Howard Chen | 8e42f5a | 2017-11-09 13:58:28 +0800 | [diff] [blame] | 297 | instance->hidl_memory::operator=(std::move(mem)); |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame] | 298 | return instance; |
| 299 | } |
| 300 | |
| 301 | sp<HidlMemory> HidlMemory::getInstance(const hidl_string& name, int fd, uint64_t size) { |
| 302 | native_handle_t* handle = native_handle_create(1, 0); |
| 303 | if (!handle) { |
| 304 | close(fd); |
| 305 | LOG(ERROR) << "native_handle_create fails"; |
| 306 | return new HidlMemory(); |
| 307 | } |
| 308 | handle->data[0] = fd; |
| 309 | |
| 310 | hidl_handle hidlHandle; |
| 311 | hidlHandle.setTo(handle, true /* shouldOwn */); |
| 312 | |
| 313 | sp<HidlMemory> instance = new HidlMemory(name, std::move(hidlHandle), size); |
| 314 | return instance; |
| 315 | } |
| 316 | |
Howard Chen | 8e42f5a | 2017-11-09 13:58:28 +0800 | [diff] [blame] | 317 | HidlMemory::HidlMemory() : hidl_memory() {} |
| 318 | |
| 319 | HidlMemory::HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size) |
| 320 | : hidl_memory(name, std::move(handle), size) {} |
| 321 | |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame] | 322 | // it's required to have at least one out-of-line method to avoid weak vtable |
Alec Edgington | af78948 | 2018-06-14 21:03:14 +0100 | [diff] [blame] | 323 | HidlMemory::~HidlMemory() {} |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame] | 324 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 325 | } // namespace hardware |
| 326 | } // namespace android |
| 327 | |
| 328 | |