Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -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 "HidlInternal" |
| 18 | |
| 19 | #include <hidl/HidlInternal.h> |
| 20 | |
| 21 | #include <android-base/logging.h> |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 22 | #include <android-base/properties.h> |
| 23 | #include <android-base/stringprintf.h> |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 24 | #include <cutils/properties.h> |
| 25 | |
| 26 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 27 | #include <dirent.h> |
| 28 | #include <dlfcn.h> |
Ryan Campbell | 21d59d9 | 2017-10-19 14:12:49 -0700 | [diff] [blame] | 29 | #include <link.h> |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 30 | #include <utils/misc.h> |
Ryan Campbell | 21d59d9 | 2017-10-19 14:12:49 -0700 | [diff] [blame] | 31 | #include <regex> |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 32 | |
| 33 | extern "C" __attribute__((weak)) void __sanitizer_cov_dump(); |
Logan Chien | 73fe94d | 2018-10-23 13:44:45 +0800 | [diff] [blame^] | 34 | |
| 35 | const char kGcovPrefixEnvVar[] = "GCOV_PREFIX"; |
| 36 | const char kGcovPrefixOverrideEnvVar[] = "GCOV_PREFIX_OVERRIDE"; |
| 37 | const char kGcovPrefixPath[] = "/data/misc/trace/"; |
| 38 | const char kSysPropHalCoverage[] = "hal.coverage.enable"; |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 39 | #if defined(__LP64__) |
Logan Chien | 73fe94d | 2018-10-23 13:44:45 +0800 | [diff] [blame^] | 40 | const char kSysPropInstrumentationPath[] = "hal.instrumentation.lib.path.64"; |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 41 | #else |
Logan Chien | 73fe94d | 2018-10-23 13:44:45 +0800 | [diff] [blame^] | 42 | const char kSysPropInstrumentationPath[] = "hal.instrumentation.lib.path.32"; |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 43 | #endif |
Logan Chien | 73fe94d | 2018-10-23 13:44:45 +0800 | [diff] [blame^] | 44 | #endif // LIBHIDL_TARGET_DEBUGGABLE |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | namespace hardware { |
| 48 | namespace details { |
| 49 | |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 50 | void logAlwaysFatal(const char* message) { |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 51 | LOG(FATAL) << message; |
| 52 | } |
| 53 | |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 54 | std::string getVndkVersionStr() { |
| 55 | static std::string vndkVersion("0"); |
| 56 | // "0" means the vndkVersion must be initialized with the property value. |
| 57 | // Otherwise, return the value. |
| 58 | if (vndkVersion == "0") { |
| 59 | vndkVersion = android::base::GetProperty("ro.vndk.version", ""); |
| 60 | if (vndkVersion != "" && vndkVersion != "current") { |
| 61 | vndkVersion = "-" + vndkVersion; |
| 62 | } else { |
| 63 | vndkVersion = ""; |
| 64 | } |
| 65 | } |
| 66 | return vndkVersion; |
| 67 | } |
| 68 | |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 69 | // ---------------------------------------------------------------------- |
| 70 | // HidlInstrumentor implementation. |
Steven Moreland | 384792b | 2017-06-19 18:24:40 -0700 | [diff] [blame] | 71 | HidlInstrumentor::HidlInstrumentor(const std::string& package, const std::string& interface) |
| 72 | : mEnableInstrumentation(false), |
| 73 | mInstrumentationLibPackage(package), |
| 74 | mInterfaceName(interface) { |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 75 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 76 | configureInstrumentation(false); |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 77 | if (__sanitizer_cov_dump != nullptr) { |
| 78 | ::android::add_sysprop_change_callback( |
| 79 | []() { |
Ryan Campbell | 21d59d9 | 2017-10-19 14:12:49 -0700 | [diff] [blame] | 80 | bool enableCoverage = property_get_bool(kSysPropHalCoverage, false); |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 81 | if (enableCoverage) { |
| 82 | __sanitizer_cov_dump(); |
| 83 | } |
| 84 | }, |
| 85 | 0); |
| 86 | } |
Ryan Campbell | 21d59d9 | 2017-10-19 14:12:49 -0700 | [diff] [blame] | 87 | if (property_get_bool("ro.vts.coverage", false)) { |
Ryan Campbell | de6c76a | 2017-11-14 18:15:13 -0800 | [diff] [blame] | 88 | const char* prefixOverride = getenv(kGcovPrefixOverrideEnvVar); |
| 89 | if (prefixOverride == nullptr || strcmp(prefixOverride, "true") != 0) { |
| 90 | const std::string gcovPath = kGcovPrefixPath + std::to_string(getpid()); |
| 91 | setenv(kGcovPrefixEnvVar, gcovPath.c_str(), true /* overwrite */); |
| 92 | } |
Ryan Campbell | 21d59d9 | 2017-10-19 14:12:49 -0700 | [diff] [blame] | 93 | ::android::add_sysprop_change_callback( |
| 94 | []() { |
| 95 | const bool enableCoverage = property_get_bool(kSysPropHalCoverage, false); |
| 96 | if (enableCoverage) { |
| 97 | dl_iterate_phdr( |
| 98 | [](struct dl_phdr_info* info, size_t /* size */, void* /* data */) { |
| 99 | if (strlen(info->dlpi_name) == 0) return 0; |
| 100 | |
| 101 | void* handle = dlopen(info->dlpi_name, RTLD_LAZY); |
| 102 | if (handle == nullptr) { |
| 103 | LOG(INFO) << "coverage dlopen failed: " << dlerror(); |
| 104 | return 0; |
| 105 | } |
| 106 | void (*flush)() = (void (*)())dlsym(handle, "__gcov_flush"); |
| 107 | if (flush == nullptr) { |
| 108 | return 0; |
| 109 | } |
| 110 | flush(); |
| 111 | return 0; |
| 112 | }, |
| 113 | nullptr /* data */); |
| 114 | } |
| 115 | }, |
| 116 | 0 /* priority */); |
| 117 | } |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 118 | #endif |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 121 | HidlInstrumentor::~HidlInstrumentor() {} |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 122 | |
| 123 | void HidlInstrumentor::configureInstrumentation(bool log) { |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 124 | mEnableInstrumentation = property_get_bool("hal.instrumentation.enable", false); |
| 125 | if (mEnableInstrumentation) { |
| 126 | if (log) { |
| 127 | LOG(INFO) << "Enable instrumentation."; |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 128 | } |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 129 | mInstrumentationCallbacks.clear(); |
| 130 | registerInstrumentationCallbacks(&mInstrumentationCallbacks); |
| 131 | } else { |
| 132 | if (log) { |
| 133 | LOG(INFO) << "Disable instrumentation."; |
| 134 | } |
| 135 | mInstrumentationCallbacks.clear(); |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | void HidlInstrumentor::registerInstrumentationCallbacks( |
| 140 | std::vector<InstrumentationCallback> *instrumentationCallbacks) { |
| 141 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 142 | std::vector<std::string> instrumentationLibPaths; |
Steven Moreland | 384792b | 2017-06-19 18:24:40 -0700 | [diff] [blame] | 143 | char instrumentationLibPath[PROPERTY_VALUE_MAX]; |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 144 | if (property_get(kSysPropInstrumentationPath, instrumentationLibPath, "") > 0) { |
Steven Moreland | 384792b | 2017-06-19 18:24:40 -0700 | [diff] [blame] | 145 | instrumentationLibPaths.push_back(instrumentationLibPath); |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 146 | } else { |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 147 | static std::string halLibPathVndkSp = android::base::StringPrintf( |
| 148 | HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, getVndkVersionStr().c_str()); |
Justin Yun | 6a323ed | 2018-10-18 18:41:56 +0900 | [diff] [blame] | 149 | #ifndef __ANDROID_VNDK__ |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 150 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM); |
Justin Yun | 6a323ed | 2018-10-18 18:41:56 +0900 | [diff] [blame] | 151 | #endif |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 152 | instrumentationLibPaths.push_back(halLibPathVndkSp); |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 153 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR); |
| 154 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM); |
| 155 | } |
| 156 | |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 157 | for (const auto& path : instrumentationLibPaths) { |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 158 | DIR *dir = opendir(path.c_str()); |
Yi Kong | 277f977 | 2018-07-24 14:59:46 -0700 | [diff] [blame] | 159 | if (dir == nullptr) { |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 160 | LOG(WARNING) << path << " does not exist. "; |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | struct dirent *file; |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 165 | while ((file = readdir(dir)) != nullptr) { |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 166 | if (!isInstrumentationLib(file)) |
| 167 | continue; |
| 168 | |
| 169 | void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW); |
| 170 | char *error; |
| 171 | if (handle == nullptr) { |
| 172 | LOG(WARNING) << "couldn't load file: " << file->d_name |
| 173 | << " error: " << dlerror(); |
| 174 | continue; |
| 175 | } |
| 176 | |
| 177 | dlerror(); /* Clear any existing error */ |
| 178 | |
Steven Moreland | 384792b | 2017-06-19 18:24:40 -0700 | [diff] [blame] | 179 | using cbFun = void (*)( |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 180 | const InstrumentationEvent, |
| 181 | const char *, |
| 182 | const char *, |
| 183 | const char *, |
| 184 | const char *, |
| 185 | std::vector<void *> *); |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 186 | std::string package = mInstrumentationLibPackage; |
| 187 | for (size_t i = 0; i < package.size(); i++) { |
| 188 | if (package[i] == '.') { |
| 189 | package[i] = '_'; |
| 190 | continue; |
| 191 | } |
| 192 | |
| 193 | if (package[i] == '@') { |
| 194 | package[i] = '_'; |
| 195 | package.insert(i + 1, "V"); |
| 196 | continue; |
| 197 | } |
| 198 | } |
Steven Moreland | 384792b | 2017-06-19 18:24:40 -0700 | [diff] [blame] | 199 | auto cb = (cbFun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_" |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 200 | + package + "_" + mInterfaceName).c_str()); |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 201 | if ((error = dlerror()) != nullptr) { |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 202 | LOG(WARNING) |
| 203 | << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_" |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 204 | << package << "_" << mInterfaceName << ", error: " << error; |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 205 | continue; |
| 206 | } |
| 207 | instrumentationCallbacks->push_back(cb); |
| 208 | LOG(INFO) << "Register instrumentation callback from " |
| 209 | << file->d_name; |
| 210 | } |
| 211 | closedir(dir); |
| 212 | } |
| 213 | #else |
| 214 | // No-op for user builds. |
Steven Moreland | ead33e2 | 2017-02-21 19:19:39 -0800 | [diff] [blame] | 215 | (void) instrumentationCallbacks; |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 216 | return; |
| 217 | #endif |
| 218 | } |
| 219 | |
| 220 | bool HidlInstrumentor::isInstrumentationLib(const dirent *file) { |
| 221 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 222 | if (file->d_type != DT_REG) return false; |
| 223 | std::cmatch cm; |
| 224 | std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$"); |
| 225 | if (std::regex_match(file->d_name, cm, e)) return true; |
Steven Moreland | ead33e2 | 2017-02-21 19:19:39 -0800 | [diff] [blame] | 226 | #else |
| 227 | (void) file; |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 228 | #endif |
| 229 | return false; |
| 230 | } |
| 231 | |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 232 | } // namespace details |
| 233 | } // namespace hardware |
| 234 | } // namespace android |