blob: 7320f3f745485252ad291079580fb588ebef2f18 [file] [log] [blame]
Martijn Coenen72110162016-08-19 14:28:25 +02001/*
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 Coenen30791002016-12-01 15:40:46 +010016#define LOG_TAG "HidlSupport"
Martijn Coenen72110162016-08-19 14:28:25 +020017
18#include <hidl/HidlSupport.h>
19
Yifan Hong20273f92017-01-30 14:13:19 -080020#include <unordered_map>
21
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070022#include <android-base/logging.h>
Yifan Hong20273f92017-01-30 14:13:19 -080023#include <android-base/parseint.h>
24#include <hidl-util/FQName.h>
Yifan Hong44c0e572017-01-20 15:41:52 -080025#include <vintf/VendorManifest.h>
26#include <vintf/parse_string.h>
Martijn Coenen30791002016-12-01 15:40:46 +010027
Martijn Coenen4ca39a02016-11-11 15:58:51 +010028#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070029#include <cutils/properties.h>
Steven Moreland337e6b62017-01-18 17:25:13 -080030#include <dlfcn.h>
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070031#include <regex>
Yifan Hong602b85a2016-10-24 13:40:01 -070032#include <utility>
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070033#endif
34
Martijn Coenen72110162016-08-19 14:28:25 +020035namespace android {
36namespace hardware {
Yifan Hong20273f92017-01-30 14:13:19 -080037vintf::Transport getTransportForFrameworkPackages(const std::string &name) {
38 // TODO(b/34772739): check with VNDK team to see if this should be in an XML.
39 const static std::unordered_map<std::string, vintf::Transport> sTransports {
40 {"android.hidl.manager@1.0::IServiceManager", vintf::Transport::HWBINDER},
41 {"android.hidl.memory@1.0::IAllocator" , vintf::Transport::HWBINDER},
42 {"android.hidl.memory@1.0::IMapper" , vintf::Transport::PASSTHROUGH},
43 {"android.hidl.memory@1.0::IMemory" , vintf::Transport::PASSTHROUGH},
44 };
45 auto it = sTransports.find(name);
46 if (it == sTransports.end()) {
47 LOG(WARNING) << "getTransportForFrameworkPackages: Cannot find entry "
48 << name << " in the static map.";
49 return vintf::Transport::EMPTY;
50 } else {
51 LOG(INFO) << "getTransportForFrameworkPackages: " << name
52 << " declares transport method " << to_string(it->second);
53 }
54 return it->second;
55}
Martijn Coenen72110162016-08-19 14:28:25 +020056
Yifan Hong20273f92017-01-30 14:13:19 -080057vintf::Transport getTransportForHals(const FQName &fqName) {
58 const std::string package = fqName.package();
Yifan Hong44c0e572017-01-20 15:41:52 -080059 const vintf::VendorManifest *vm = vintf::VendorManifest::Get();
60 if (vm == nullptr) {
Steven Moreland01732732017-02-02 18:42:49 -080061 LOG(WARNING) << "getTransportFromManifest: Cannot find vendor interface manifest.";
Yifan Hong44c0e572017-01-20 15:41:52 -080062 return vintf::Transport::EMPTY;
63 }
Yifan Hong20273f92017-01-30 14:13:19 -080064 size_t majorVer;
65 size_t minorVer;
66 if ( !::android::base::ParseUint(fqName.getPackageMajorVersion(), &majorVer)
67 || !::android::base::ParseUint(fqName.getPackageMinorVersion(), &minorVer)) {
68 LOG(ERROR) << "getTransportFromManifest: " << fqName.string()
69 << " does not specify a version.";
70 return vintf::Transport::EMPTY;
71 }
72 vintf::Transport tr = vm->getTransport(package, vintf::Version{majorVer, minorVer});
Yifan Hong44c0e572017-01-20 15:41:52 -080073 if (tr == vintf::Transport::EMPTY) {
74 LOG(WARNING) << "getTransportFromManifest: Cannot find entry "
Yifan Hong20273f92017-01-30 14:13:19 -080075 << package << fqName.atVersion() << " in vendor interface manifest.";
Yifan Hong44c0e572017-01-20 15:41:52 -080076 } else {
Yifan Hong20273f92017-01-30 14:13:19 -080077 LOG(INFO) << "getTransportFromManifest: " << package << fqName.atVersion()
Yifan Hong44c0e572017-01-20 15:41:52 -080078 << " declares transport method " << to_string(tr);
Yifan Hong20273f92017-01-30 14:13:19 -080079 }
Yifan Hong44c0e572017-01-20 15:41:52 -080080 return tr;
81}
82
Yifan Hong20273f92017-01-30 14:13:19 -080083vintf::Transport getTransport(const std::string &name) {
84 FQName fqName(name);
85 if (!fqName.isValid()) {
86 LOG(WARNING) << name << " is not a valid fully-qualified name.";
87 return vintf::Transport::EMPTY;
88 }
89 if (fqName.inPackage("android.hidl")) {
90 return getTransportForFrameworkPackages(name);
91 }
92 return getTransportForHals(fqName);
93}
94
Martijn Coenen04b91c02017-01-19 14:14:21 +010095hidl_handle::hidl_handle() {
96 mHandle = nullptr;
97 mOwnsHandle = false;
98}
99
100hidl_handle::~hidl_handle() {
101 freeHandle();
102}
103
104hidl_handle::hidl_handle(const native_handle_t *handle) {
105 mHandle = handle;
106 mOwnsHandle = false;
107}
108
109// copy constructor.
110hidl_handle::hidl_handle(const hidl_handle &other) {
111 mOwnsHandle = false;
112 *this = other;
113}
114
115// move constructor.
116hidl_handle::hidl_handle(hidl_handle &&other) {
117 mOwnsHandle = false;
118 *this = std::move(other);
119}
120
121// assignment operators
122hidl_handle &hidl_handle::operator=(const hidl_handle &other) {
123 if (this == &other) {
124 return *this;
125 }
126 freeHandle();
127 if (other.mHandle != nullptr) {
128 mHandle = native_handle_clone(other.mHandle);
129 if (mHandle == nullptr) {
130 LOG(FATAL) << "Failed to clone native_handle in hidl_handle.";
131 }
132 mOwnsHandle = true;
133 } else {
134 mHandle = nullptr;
135 mOwnsHandle = false;
136 }
137 return *this;
138}
139
140hidl_handle &hidl_handle::operator=(const native_handle_t *native_handle) {
141 freeHandle();
142 mHandle = native_handle;
143 mOwnsHandle = false;
144 return *this;
145}
146
147hidl_handle &hidl_handle::operator=(hidl_handle &&other) {
148 if (this != &other) {
149 freeHandle();
150 mHandle = other.mHandle;
151 mOwnsHandle = other.mOwnsHandle;
152 other.mHandle = nullptr;
153 other.mOwnsHandle = false;
154 }
155 return *this;
156}
157
158void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) {
Scott Randolphca37c0e2017-02-15 16:38:46 -0800159 freeHandle();
Martijn Coenen04b91c02017-01-19 14:14:21 +0100160 mHandle = handle;
161 mOwnsHandle = shouldOwn;
162}
163
164const native_handle_t* hidl_handle::operator->() const {
165 return mHandle;
166}
167
168// implicit conversion to const native_handle_t*
169hidl_handle::operator const native_handle_t *() const {
170 return mHandle;
171}
172
173// explicit conversion
174const native_handle_t *hidl_handle::getNativeHandle() const {
175 return mHandle;
176}
177
178void hidl_handle::freeHandle() {
179 if (mOwnsHandle && mHandle != nullptr) {
180 // This can only be true if:
181 // 1. Somebody called setTo() with shouldOwn=true, so we know the handle
182 // wasn't const to begin with.
183 // 2. Copy/assignment from another hidl_handle, in which case we have
184 // cloned the handle.
185 // 3. Move constructor from another hidl_handle, in which case the original
186 // hidl_handle must have been non-const as well.
187 native_handle_t *handle = const_cast<native_handle_t*>(
188 static_cast<const native_handle_t*>(mHandle));
189 native_handle_close(handle);
190 native_handle_delete(handle);
191 mHandle = nullptr;
192 }
193}
194
Martijn Coenen72110162016-08-19 14:28:25 +0200195static const char *const kEmptyString = "";
196
197hidl_string::hidl_string()
Yifan Hong602b85a2016-10-24 13:40:01 -0700198 : mBuffer(kEmptyString),
Martijn Coenen72110162016-08-19 14:28:25 +0200199 mSize(0),
Yifan Hong602b85a2016-10-24 13:40:01 -0700200 mOwnsBuffer(false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200201}
202
203hidl_string::~hidl_string() {
204 clear();
205}
206
Steven Morelande03c0872016-10-24 10:43:50 -0700207hidl_string::hidl_string(const char *s) : hidl_string() {
Yifan Hong602b85a2016-10-24 13:40:01 -0700208 copyFrom(s, strlen(s));
Steven Morelande03c0872016-10-24 10:43:50 -0700209}
210
Steven Moreland53120f72017-01-12 09:39:26 -0800211hidl_string::hidl_string(const char *s, size_t length) : hidl_string() {
212 copyFrom(s, length);
213}
214
Yifan Hong602b85a2016-10-24 13:40:01 -0700215hidl_string::hidl_string(const hidl_string &other): hidl_string() {
216 copyFrom(other.c_str(), other.size());
217}
218
219hidl_string::hidl_string(const std::string &s) : hidl_string() {
220 copyFrom(s.c_str(), s.size());
221}
222
223hidl_string::hidl_string(hidl_string &&other): hidl_string() {
224 moveFrom(std::forward<hidl_string>(other));
225}
226
227hidl_string &hidl_string::operator=(hidl_string &&other) {
228 if (this != &other) {
229 clear();
230 moveFrom(std::forward<hidl_string>(other));
231 }
232 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200233}
234
235hidl_string &hidl_string::operator=(const hidl_string &other) {
236 if (this != &other) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700237 clear();
238 copyFrom(other.c_str(), other.size());
Martijn Coenen72110162016-08-19 14:28:25 +0200239 }
240
241 return *this;
242}
243
244hidl_string &hidl_string::operator=(const char *s) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700245 clear();
246 copyFrom(s, strlen(s));
247 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200248}
249
Yifan Hong602b85a2016-10-24 13:40:01 -0700250hidl_string &hidl_string::operator=(const std::string &s) {
Martijn Coenen72110162016-08-19 14:28:25 +0200251 clear();
Yifan Hong602b85a2016-10-24 13:40:01 -0700252 copyFrom(s.c_str(), s.size());
253 return *this;
254}
Martijn Coenen72110162016-08-19 14:28:25 +0200255
Steven Morelanda2a81842017-01-20 14:48:15 -0800256bool hidl_string::operator< (const hidl_string &rhs) const {
257 return strcmp(mBuffer, rhs.mBuffer) < 0;
258}
259
Yifan Hong602b85a2016-10-24 13:40:01 -0700260hidl_string::operator std::string() const {
261 return std::string(mBuffer, mSize);
262}
263
264hidl_string::operator const char *() const {
265 return mBuffer;
266}
267
268void hidl_string::copyFrom(const char *data, size_t size) {
269 // assume my resources are freed.
270
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100271 if (size > UINT32_MAX) {
272 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
273 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700274 char *buf = (char *)malloc(size + 1);
275 memcpy(buf, data, size);
276 buf[size] = '\0';
277 mBuffer = buf;
Martijn Coenen72110162016-08-19 14:28:25 +0200278
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100279 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200280 mOwnsBuffer = true;
Yifan Hong602b85a2016-10-24 13:40:01 -0700281}
Martijn Coenen72110162016-08-19 14:28:25 +0200282
Yifan Hong602b85a2016-10-24 13:40:01 -0700283void hidl_string::moveFrom(hidl_string &&other) {
284 // assume my resources are freed.
285
286 mBuffer = other.mBuffer;
287 mSize = other.mSize;
288 mOwnsBuffer = other.mOwnsBuffer;
289
290 other.mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200291}
292
293void hidl_string::clear() {
294 if (mOwnsBuffer && (mBuffer != kEmptyString)) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100295 free(const_cast<char *>(static_cast<const char *>(mBuffer)));
Martijn Coenen72110162016-08-19 14:28:25 +0200296 }
297
Yifan Hong602b85a2016-10-24 13:40:01 -0700298 mBuffer = kEmptyString;
Martijn Coenen72110162016-08-19 14:28:25 +0200299 mSize = 0;
Yifan Hong602b85a2016-10-24 13:40:01 -0700300 mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200301}
302
303void hidl_string::setToExternal(const char *data, size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100304 if (size > UINT32_MAX) {
305 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
306 }
Martijn Coenen72110162016-08-19 14:28:25 +0200307 clear();
308
Yifan Hong602b85a2016-10-24 13:40:01 -0700309 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100310 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200311 mOwnsBuffer = false;
312}
313
314const char *hidl_string::c_str() const {
Yifan Hong602b85a2016-10-24 13:40:01 -0700315 return mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200316}
317
318size_t hidl_string::size() const {
319 return mSize;
320}
321
322bool hidl_string::empty() const {
323 return mSize == 0;
324}
325
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700326// ----------------------------------------------------------------------
327// HidlInstrumentor implementation.
Zhuoyao Zhang93c1e3a2017-01-23 17:37:49 -0800328HidlInstrumentor::HidlInstrumentor(
329 const std::string &package,
330 const std::string &interface)
331 : mInstrumentationLibPackage(package), mInterfaceName(interface) {
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800332 configureInstrumentation(false);
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700333}
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700334
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700335HidlInstrumentor:: ~HidlInstrumentor() {}
336
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800337void HidlInstrumentor::configureInstrumentation(bool log) {
338 bool enable_instrumentation = property_get_bool(
339 "hal.instrumentation.enable",
340 false);
341 if (enable_instrumentation != mEnableInstrumentation) {
342 mEnableInstrumentation = enable_instrumentation;
343 if (mEnableInstrumentation) {
344 if (log) {
345 LOG(INFO) << "Enable instrumentation.";
346 }
347 registerInstrumentationCallbacks (&mInstrumentationCallbacks);
348 } else {
349 if (log) {
350 LOG(INFO) << "Disable instrumentation.";
351 }
352 mInstrumentationCallbacks.clear();
353 }
354 }
355}
356
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700357void HidlInstrumentor::registerInstrumentationCallbacks(
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700358 std::vector<InstrumentationCallback> *instrumentationCallbacks) {
Dan Willemsen6365a872016-09-13 16:29:54 -0700359#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700360 std::vector<std::string> instrumentationLibPaths;
Zhuoyao Zhang8bed09f2016-10-26 18:12:47 -0700361 char instrumentation_lib_path[PROPERTY_VALUE_MAX];
362 if (property_get("hal.instrumentation.lib.path",
363 instrumentation_lib_path,
364 "") > 0) {
365 instrumentationLibPaths.push_back(instrumentation_lib_path);
366 } else {
367 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
368 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR);
369 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
370 }
371
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700372 for (auto path : instrumentationLibPaths) {
373 DIR *dir = opendir(path.c_str());
374 if (dir == 0) {
Steven Moreland7096f542016-11-07 11:20:33 -0800375 LOG(WARNING) << path << " does not exist. ";
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700376 return;
377 }
378
379 struct dirent *file;
380 while ((file = readdir(dir)) != NULL) {
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800381 if (!isInstrumentationLib(file))
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700382 continue;
383
384 void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
Zhuoyao Zhang93c1e3a2017-01-23 17:37:49 -0800385 char *error;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700386 if (handle == nullptr) {
387 LOG(WARNING) << "couldn't load file: " << file->d_name
388 << " error: " << dlerror();
389 continue;
390 }
Zhuoyao Zhang93c1e3a2017-01-23 17:37:49 -0800391
392 dlerror(); /* Clear any existing error */
393
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700394 using cb_fun = void (*)(
395 const InstrumentationEvent,
396 const char *,
397 const char *,
398 const char *,
399 const char *,
400 std::vector<void *> *);
Zhuoyao Zhang2236bd12017-02-09 15:45:22 -0800401 FQName package_name = FQName(mInstrumentationLibPackage);
402 auto cb = (cb_fun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_"
403 + package_name.tokenName() + "_"
404 + mInterfaceName).c_str());
Zhuoyao Zhang93c1e3a2017-01-23 17:37:49 -0800405 if ((error = dlerror()) != NULL) {
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700406 LOG(WARNING)
Zhuoyao Zhang93c1e3a2017-01-23 17:37:49 -0800407 << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_"
408 << mInterfaceName << ", error: " << error;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700409 continue;
410 }
411 instrumentationCallbacks->push_back(cb);
412 LOG(INFO) << "Register instrumentation callback from "
413 << file->d_name;
414 }
415 closedir(dir);
416 }
417#else
418 // No-op for user builds.
419 return;
420#endif
421}
422
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800423bool HidlInstrumentor::isInstrumentationLib(const dirent *file) {
Dan Willemsen6365a872016-09-13 16:29:54 -0700424#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700425 if (file->d_type != DT_REG) return false;
426 std::cmatch cm;
Zhuoyao Zhang93c1e3a2017-01-23 17:37:49 -0800427 std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$");
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700428 if (std::regex_match(file->d_name, cm, e)) return true;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700429#endif
430 return false;
431}
432
Martijn Coenen72110162016-08-19 14:28:25 +0200433} // namespace hardware
434} // namespace android
435
436