HidlSupport.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #define LOG_TAG "HidlSupport"
  17. #include <hidl/HidlSupport.h>
  18. #include <unordered_map>
  19. #include <android-base/logging.h>
  20. #include <android-base/parseint.h>
  21. namespace android {
  22. namespace hardware {
  23. namespace details {
  24. bool debuggable() {
  25. #ifdef LIBHIDL_TARGET_DEBUGGABLE
  26. return true;
  27. #else
  28. return false;
  29. #endif
  30. }
  31. } // namespace details
  32. hidl_handle::hidl_handle() {
  33. memset(this, 0, sizeof(*this));
  34. // mHandle = nullptr;
  35. // mOwnsHandle = false;
  36. }
  37. hidl_handle::~hidl_handle() {
  38. freeHandle();
  39. }
  40. hidl_handle::hidl_handle(const native_handle_t* handle) : hidl_handle() {
  41. mHandle = handle;
  42. mOwnsHandle = false;
  43. }
  44. // copy constructor.
  45. hidl_handle::hidl_handle(const hidl_handle& other) : hidl_handle() {
  46. mOwnsHandle = false;
  47. *this = other;
  48. }
  49. // move constructor.
  50. hidl_handle::hidl_handle(hidl_handle&& other) noexcept : hidl_handle() {
  51. mOwnsHandle = false;
  52. *this = std::move(other);
  53. }
  54. // assignment operators
  55. hidl_handle &hidl_handle::operator=(const hidl_handle &other) {
  56. if (this == &other) {
  57. return *this;
  58. }
  59. freeHandle();
  60. if (other.mHandle != nullptr) {
  61. mHandle = native_handle_clone(other.mHandle);
  62. if (mHandle == nullptr) {
  63. PLOG(FATAL) << "Failed to clone native_handle in hidl_handle";
  64. }
  65. mOwnsHandle = true;
  66. } else {
  67. mHandle = nullptr;
  68. mOwnsHandle = false;
  69. }
  70. return *this;
  71. }
  72. hidl_handle &hidl_handle::operator=(const native_handle_t *native_handle) {
  73. freeHandle();
  74. mHandle = native_handle;
  75. mOwnsHandle = false;
  76. return *this;
  77. }
  78. hidl_handle& hidl_handle::operator=(hidl_handle&& other) noexcept {
  79. if (this != &other) {
  80. freeHandle();
  81. mHandle = other.mHandle;
  82. mOwnsHandle = other.mOwnsHandle;
  83. other.mHandle = nullptr;
  84. other.mOwnsHandle = false;
  85. }
  86. return *this;
  87. }
  88. void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) {
  89. freeHandle();
  90. mHandle = handle;
  91. mOwnsHandle = shouldOwn;
  92. }
  93. const native_handle_t* hidl_handle::operator->() const {
  94. return mHandle;
  95. }
  96. // implicit conversion to const native_handle_t*
  97. hidl_handle::operator const native_handle_t *() const {
  98. return mHandle;
  99. }
  100. // explicit conversion
  101. const native_handle_t *hidl_handle::getNativeHandle() const {
  102. return mHandle;
  103. }
  104. void hidl_handle::freeHandle() {
  105. if (mOwnsHandle && mHandle != nullptr) {
  106. // This can only be true if:
  107. // 1. Somebody called setTo() with shouldOwn=true, so we know the handle
  108. // wasn't const to begin with.
  109. // 2. Copy/assignment from another hidl_handle, in which case we have
  110. // cloned the handle.
  111. // 3. Move constructor from another hidl_handle, in which case the original
  112. // hidl_handle must have been non-const as well.
  113. native_handle_t *handle = const_cast<native_handle_t*>(
  114. static_cast<const native_handle_t*>(mHandle));
  115. native_handle_close(handle);
  116. native_handle_delete(handle);
  117. mHandle = nullptr;
  118. }
  119. }
  120. static const char *const kEmptyString = "";
  121. hidl_string::hidl_string() {
  122. memset(this, 0, sizeof(*this));
  123. // mSize is zero
  124. // mOwnsBuffer is false
  125. mBuffer = kEmptyString;
  126. }
  127. hidl_string::~hidl_string() {
  128. clear();
  129. }
  130. hidl_string::hidl_string(const char *s) : hidl_string() {
  131. if (s == nullptr) {
  132. return;
  133. }
  134. copyFrom(s, strlen(s));
  135. }
  136. hidl_string::hidl_string(const char *s, size_t length) : hidl_string() {
  137. copyFrom(s, length);
  138. }
  139. hidl_string::hidl_string(const hidl_string &other): hidl_string() {
  140. copyFrom(other.c_str(), other.size());
  141. }
  142. hidl_string::hidl_string(const std::string &s) : hidl_string() {
  143. copyFrom(s.c_str(), s.size());
  144. }
  145. hidl_string::hidl_string(hidl_string&& other) noexcept : hidl_string() {
  146. moveFrom(std::forward<hidl_string>(other));
  147. }
  148. hidl_string& hidl_string::operator=(hidl_string&& other) noexcept {
  149. if (this != &other) {
  150. clear();
  151. moveFrom(std::forward<hidl_string>(other));
  152. }
  153. return *this;
  154. }
  155. hidl_string &hidl_string::operator=(const hidl_string &other) {
  156. if (this != &other) {
  157. clear();
  158. copyFrom(other.c_str(), other.size());
  159. }
  160. return *this;
  161. }
  162. hidl_string &hidl_string::operator=(const char *s) {
  163. clear();
  164. if (s == nullptr) {
  165. return *this;
  166. }
  167. copyFrom(s, strlen(s));
  168. return *this;
  169. }
  170. hidl_string &hidl_string::operator=(const std::string &s) {
  171. clear();
  172. copyFrom(s.c_str(), s.size());
  173. return *this;
  174. }
  175. hidl_string::operator std::string() const {
  176. return std::string(mBuffer, mSize);
  177. }
  178. std::ostream& operator<<(std::ostream& os, const hidl_string& str) {
  179. os << str.c_str();
  180. return os;
  181. }
  182. void hidl_string::copyFrom(const char *data, size_t size) {
  183. // assume my resources are freed.
  184. if (size >= UINT32_MAX) {
  185. LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size;
  186. }
  187. char *buf = (char *)malloc(size + 1);
  188. memcpy(buf, data, size);
  189. buf[size] = '\0';
  190. mBuffer = buf;
  191. mSize = static_cast<uint32_t>(size);
  192. mOwnsBuffer = true;
  193. }
  194. void hidl_string::moveFrom(hidl_string &&other) {
  195. // assume my resources are freed.
  196. mBuffer = std::move(other.mBuffer);
  197. mSize = other.mSize;
  198. mOwnsBuffer = other.mOwnsBuffer;
  199. other.mOwnsBuffer = false;
  200. other.clear();
  201. }
  202. void hidl_string::clear() {
  203. if (mOwnsBuffer && (mBuffer != kEmptyString)) {
  204. free(const_cast<char *>(static_cast<const char *>(mBuffer)));
  205. }
  206. mBuffer = kEmptyString;
  207. mSize = 0;
  208. mOwnsBuffer = false;
  209. }
  210. void hidl_string::setToExternal(const char *data, size_t size) {
  211. if (size > UINT32_MAX) {
  212. LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size;
  213. }
  214. // When the binder driver copies this data into its buffer, it must
  215. // have a zero byte there because the remote process will have a pointer
  216. // directly into the read-only binder buffer. If we manually copy the
  217. // data now to add a zero, then we lose the efficiency of this method.
  218. // Checking here (it's also checked in the parceling code later).
  219. CHECK(data[size] == '\0');
  220. clear();
  221. mBuffer = data;
  222. mSize = static_cast<uint32_t>(size);
  223. mOwnsBuffer = false;
  224. }
  225. const char *hidl_string::c_str() const {
  226. return mBuffer;
  227. }
  228. size_t hidl_string::size() const {
  229. return mSize;
  230. }
  231. bool hidl_string::empty() const {
  232. return mSize == 0;
  233. }
  234. sp<HidlMemory> HidlMemory::getInstance(const hidl_memory& mem) {
  235. sp<HidlMemory> instance = new HidlMemory();
  236. instance->hidl_memory::operator=(mem);
  237. return instance;
  238. }
  239. sp<HidlMemory> HidlMemory::getInstance(hidl_memory&& mem) {
  240. sp<HidlMemory> instance = new HidlMemory();
  241. instance->hidl_memory::operator=(std::move(mem));
  242. return instance;
  243. }
  244. sp<HidlMemory> HidlMemory::getInstance(const hidl_string& name, int fd, uint64_t size) {
  245. native_handle_t* handle = native_handle_create(1, 0);
  246. if (!handle) {
  247. close(fd);
  248. LOG(ERROR) << "native_handle_create fails";
  249. return new HidlMemory();
  250. }
  251. handle->data[0] = fd;
  252. hidl_handle hidlHandle;
  253. hidlHandle.setTo(handle, true /* shouldOwn */);
  254. sp<HidlMemory> instance = new HidlMemory(name, std::move(hidlHandle), size);
  255. return instance;
  256. }
  257. HidlMemory::HidlMemory() : hidl_memory() {}
  258. HidlMemory::HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size)
  259. : hidl_memory(name, std::move(handle), size) {}
  260. // it's required to have at least one out-of-line method to avoid weak vtable
  261. HidlMemory::~HidlMemory() {}
  262. } // namespace hardware
  263. } // namespace android