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 #ifndef ANDROID_HIDL_INTERNAL_H
18 #define ANDROID_HIDL_INTERNAL_H
19
20 #include <cstdint>
21 #include <dirent.h>
22 #include <functional>
23 #include <string>
24 #include <vector>
25 #include <utility>
26
27 namespace android {
28 namespace hardware {
29 namespace details {
30
31 // tag for pure interfaces (e.x. IFoo)
32 struct i_tag {};
33
34 // tag for server interfaces (e.x. BnHwFoo)
35 struct bnhw_tag {};
36
37 // tag for proxy interfaces (e.x. BpHwFoo)
38 struct bphw_tag {};
39
40 // tag for passthrough interfaces (e.x. BsFoo)
41 struct bs_tag {};
42
43 //Templated classes can use the below method
44 //to avoid creating dependencies on liblog.
45 void logAlwaysFatal(const char *message);
46
47 // Returns vndk version from "ro.vndk.version" with '-' as a prefix.
48 // If "ro.vndk.version" is not set or set to "current", it returns empty string.
49 std::string getVndkVersionStr();
50
51 // Explicitly invokes the parameterized element's destructor;
52 // intended to be used alongside the placement new operator.
53 template<typename T>
destructElement(T * element)54 void destructElement(T* element) {
55 if (element != nullptr) {
56 element->~T();
57 }
58 }
59
60 // HIDL client/server code should *NOT* use this class.
61 //
62 // hidl_pointer wraps a pointer without taking ownership,
63 // and stores it in a union with a uint64_t. This ensures
64 // that we always have enough space to store a pointer,
65 // regardless of whether we're running in a 32-bit or 64-bit
66 // process.
67 template<typename T>
68 struct hidl_pointer {
hidl_pointerhidl_pointer69 hidl_pointer()
70 : _pad(0) {
71 static_assert(sizeof(*this) == 8, "wrong size");
72 }
hidl_pointerhidl_pointer73 hidl_pointer(T* ptr) : hidl_pointer() { mPointer = ptr; }
hidl_pointerhidl_pointer74 hidl_pointer(const hidl_pointer<T>& other) : hidl_pointer() { mPointer = other.mPointer; }
hidl_pointerhidl_pointer75 hidl_pointer(hidl_pointer<T>&& other) noexcept : hidl_pointer() { *this = std::move(other); }
76
77 hidl_pointer &operator=(const hidl_pointer<T>& other) {
78 mPointer = other.mPointer;
79 return *this;
80 }
81 hidl_pointer& operator=(hidl_pointer<T>&& other) noexcept {
82 mPointer = other.mPointer;
83 other.mPointer = nullptr;
84 return *this;
85 }
86 hidl_pointer &operator=(T* ptr) {
87 mPointer = ptr;
88 return *this;
89 }
90
91 operator T*() const {
92 return mPointer;
93 }
94 explicit operator void*() const { // requires explicit cast to avoid ambiguity
95 return mPointer;
96 }
97 T& operator*() const {
98 return *mPointer;
99 }
100 T* operator->() const {
101 return mPointer;
102 }
103 T &operator[](size_t index) {
104 return mPointer[index];
105 }
106 const T &operator[](size_t index) const {
107 return mPointer[index];
108 }
109
110 private:
111 union {
112 T* mPointer;
113 uint64_t _pad;
114 };
115 };
116
117 #define HAL_LIBRARY_PATH_SYSTEM_64BIT "/system/lib64/hw/"
118 #define HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION "/system/lib64/vndk-sp%s/hw/"
119 #define HAL_LIBRARY_PATH_VENDOR_64BIT "/vendor/lib64/hw/"
120 #define HAL_LIBRARY_PATH_ODM_64BIT "/odm/lib64/hw/"
121 #define HAL_LIBRARY_PATH_SYSTEM_32BIT "/system/lib/hw/"
122 #define HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION "/system/lib/vndk-sp%s/hw/"
123 #define HAL_LIBRARY_PATH_VENDOR_32BIT "/vendor/lib/hw/"
124 #define HAL_LIBRARY_PATH_ODM_32BIT "/odm/lib/hw/"
125
126 #if defined(__LP64__)
127 #define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_64BIT
128 #define HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION
129 #define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_64BIT
130 #define HAL_LIBRARY_PATH_ODM HAL_LIBRARY_PATH_ODM_64BIT
131 #else
132 #define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_32BIT
133 #define HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION
134 #define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_32BIT
135 #define HAL_LIBRARY_PATH_ODM HAL_LIBRARY_PATH_ODM_32BIT
136 #endif
137
138 // ----------------------------------------------------------------------
139 // Class that provides Hidl instrumentation utilities.
140 struct HidlInstrumentor {
141 // Event that triggers the instrumentation. e.g. enter of an API call on
142 // the server/client side, exit of an API call on the server/client side
143 // etc.
144 enum InstrumentationEvent {
145 SERVER_API_ENTRY = 0,
146 SERVER_API_EXIT,
147 CLIENT_API_ENTRY,
148 CLIENT_API_EXIT,
149 SYNC_CALLBACK_ENTRY,
150 SYNC_CALLBACK_EXIT,
151 ASYNC_CALLBACK_ENTRY,
152 ASYNC_CALLBACK_EXIT,
153 PASSTHROUGH_ENTRY,
154 PASSTHROUGH_EXIT,
155 };
156
157 // Signature of the instrumentation callback function.
158 using InstrumentationCallback = std::function<void(
159 const InstrumentationEvent event,
160 const char *package,
161 const char *version,
162 const char *interface,
163 const char *method,
164 std::vector<void *> *args)>;
165
166 explicit HidlInstrumentor(
167 const std::string &package,
168 const std::string &insterface);
169 virtual ~HidlInstrumentor();
170
171 public:
getInstrumentationCallbacksHidlInstrumentor172 const std::vector<InstrumentationCallback>& getInstrumentationCallbacks() {
173 return mInstrumentationCallbacks;
174 }
isInstrumentationEnabledHidlInstrumentor175 bool isInstrumentationEnabled() { return mEnableInstrumentation; }
176
177 protected:
178 // Set mEnableInstrumentation based on system property
179 // hal.instrumentation.enable, register/de-register instrumentation
180 // callbacks if mEnableInstrumentation is true/false.
181 void configureInstrumentation(bool log=true);
182 // Function that lookup and dynamically loads the hidl instrumentation
183 // libraries and registers the instrumentation callback functions.
184 //
185 // The instrumentation libraries should be stored under any of the following
186 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VNDK_SP,
187 // HAL_LIBRARY_PATH_VENDOR and HAL_LIBRARY_PATH_ODM.
188 // The name of instrumentation libraries should follow pattern:
189 // ^profilerPrefix(.*).profiler.so$
190 //
191 // Each instrumentation library is expected to implement the instrumentation
192 // function called HIDL_INSTRUMENTATION_FUNCTION.
193 //
194 // A no-op for user build.
195 void registerInstrumentationCallbacks(
196 std::vector<InstrumentationCallback> *instrumentationCallbacks);
197
198 // Utility function to determine whether a give file is a instrumentation
199 // library (i.e. the file name follow the expected pattern).
200 bool isInstrumentationLib(const dirent *file);
201
202 // A list of registered instrumentation callbacks.
203 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
204 // Flag whether to enable instrumentation.
205 bool mEnableInstrumentation;
206 // Prefix to lookup the instrumentation libraries.
207 std::string mInstrumentationLibPackage;
208 // Used for dlsym to load the profiling method for given interface.
209 std::string mInterfaceName;
210
211 };
212
213 } // namespace details
214 } // namespace hardware
215 } // namespace android
216
217 #endif // ANDROID_HIDL_INTERNAL_H
218