• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-SP hw path according to "ro.vndk.version"
48 #if defined(__LP64__)
49 std::string getVndkSpHwPath(const char* lib = "lib64");
50 #else
51 std::string getVndkSpHwPath(const char* lib = "lib");
52 #endif
53 
54 // Explicitly invokes the parameterized element's destructor;
55 // intended to be used alongside the placement new operator.
56 template<typename T>
destructElement(T * element)57 void destructElement(T* element) {
58     if (element != nullptr) {
59         element->~T();
60     }
61 }
62 
63 // HIDL client/server code should *NOT* use this class.
64 //
65 // hidl_pointer wraps a pointer without taking ownership,
66 // and stores it in a union with a uint64_t. This ensures
67 // that we always have enough space to store a pointer,
68 // regardless of whether we're running in a 32-bit or 64-bit
69 // process.
70 template<typename T>
71 struct hidl_pointer {
hidl_pointerhidl_pointer72     hidl_pointer()
73         : _pad(0) {
74         static_assert(sizeof(*this) == 8, "wrong size");
75     }
hidl_pointerhidl_pointer76     hidl_pointer(T* ptr) : hidl_pointer() { mPointer = ptr; }
hidl_pointerhidl_pointer77     hidl_pointer(const hidl_pointer<T>& other) : hidl_pointer() { mPointer = other.mPointer; }
hidl_pointerhidl_pointer78     hidl_pointer(hidl_pointer<T>&& other) noexcept : hidl_pointer() { *this = std::move(other); }
79 
80     hidl_pointer &operator=(const hidl_pointer<T>& other) {
81         mPointer = other.mPointer;
82         return *this;
83     }
84     hidl_pointer& operator=(hidl_pointer<T>&& other) noexcept {
85         mPointer = other.mPointer;
86         other.mPointer = nullptr;
87         return *this;
88     }
89     hidl_pointer &operator=(T* ptr) {
90         mPointer = ptr;
91         return *this;
92     }
93 
94     operator T*() const {
95         return mPointer;
96     }
97     explicit operator void*() const { // requires explicit cast to avoid ambiguity
98         return mPointer;
99     }
100     T& operator*() const {
101         return *mPointer;
102     }
103     T* operator->() const {
104         return mPointer;
105     }
106     T &operator[](size_t index) {
107         return mPointer[index];
108     }
109     const T &operator[](size_t index) const {
110         return mPointer[index];
111     }
112 
113 private:
114     union {
115         T* mPointer;
116         uint64_t _pad;
117     };
118 };
119 
120 #define HAL_LIBRARY_PATH_SYSTEM_64BIT "/system/lib64/hw/"
121 #define HAL_LIBRARY_PATH_SYSTEM_EXT_64BIT "/system_ext/lib64/hw/"
122 #define HAL_LIBRARY_PATH_VENDOR_64BIT "/vendor/lib64/hw/"
123 #define HAL_LIBRARY_PATH_ODM_64BIT    "/odm/lib64/hw/"
124 #define HAL_LIBRARY_PATH_SYSTEM_32BIT "/system/lib/hw/"
125 #define HAL_LIBRARY_PATH_SYSTEM_EXT_32BIT "/system_ext/lib/hw/"
126 #define HAL_LIBRARY_PATH_VENDOR_32BIT "/vendor/lib/hw/"
127 #define HAL_LIBRARY_PATH_ODM_32BIT    "/odm/lib/hw/"
128 
129 #if defined(__LP64__)
130 #define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_64BIT
131 #define HAL_LIBRARY_PATH_SYSTEM_EXT HAL_LIBRARY_PATH_SYSTEM_EXT_64BIT
132 #define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_64BIT
133 #define HAL_LIBRARY_PATH_ODM    HAL_LIBRARY_PATH_ODM_64BIT
134 #else
135 #define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_32BIT
136 #define HAL_LIBRARY_PATH_SYSTEM_EXT HAL_LIBRARY_PATH_SYSTEM_EXT_32BIT
137 #define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_32BIT
138 #define HAL_LIBRARY_PATH_ODM    HAL_LIBRARY_PATH_ODM_32BIT
139 #endif
140 
141 // ----------------------------------------------------------------------
142 // Class that provides Hidl instrumentation utilities.
143 struct HidlInstrumentor {
144     // Event that triggers the instrumentation. e.g. enter of an API call on
145     // the server/client side, exit of an API call on the server/client side
146     // etc.
147     enum InstrumentationEvent {
148         SERVER_API_ENTRY = 0,
149         SERVER_API_EXIT,
150         CLIENT_API_ENTRY,
151         CLIENT_API_EXIT,
152         SYNC_CALLBACK_ENTRY,
153         SYNC_CALLBACK_EXIT,
154         ASYNC_CALLBACK_ENTRY,
155         ASYNC_CALLBACK_EXIT,
156         PASSTHROUGH_ENTRY,
157         PASSTHROUGH_EXIT,
158     };
159 
160     // Signature of the instrumentation callback function.
161     using InstrumentationCallback = std::function<void(
162             const InstrumentationEvent event,
163             const char *package,
164             const char *version,
165             const char *interface,
166             const char *method,
167             std::vector<void *> *args)>;
168 
169     explicit HidlInstrumentor(
170             const std::string &package,
171             const std::string &insterface);
172     virtual ~HidlInstrumentor();
173 
174    public:
getInstrumentationCallbacksHidlInstrumentor175     const std::vector<InstrumentationCallback>& getInstrumentationCallbacks() {
176         return mInstrumentationCallbacks;
177     }
isInstrumentationEnabledHidlInstrumentor178     bool isInstrumentationEnabled() { return mEnableInstrumentation; }
179 
180    protected:
181     // Set mEnableInstrumentation based on system property
182     // hal.instrumentation.enable, register/de-register instrumentation
183     // callbacks if mEnableInstrumentation is true/false.
184     void configureInstrumentation(bool log=true);
185     // Function that lookup and dynamically loads the hidl instrumentation
186     // libraries and registers the instrumentation callback functions.
187     //
188     // The instrumentation libraries should be stored under any of the following
189     // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VNDK_SP,
190     // HAL_LIBRARY_PATH_VENDOR and HAL_LIBRARY_PATH_ODM.
191     // The name of instrumentation libraries should follow pattern:
192     // ^profilerPrefix(.*).profiler.so$
193     //
194     // Each instrumentation library is expected to implement the instrumentation
195     // function called HIDL_INSTRUMENTATION_FUNCTION.
196     //
197     // A no-op for user build.
198     void registerInstrumentationCallbacks(
199             std::vector<InstrumentationCallback> *instrumentationCallbacks);
200 
201     // Utility function to determine whether a give file is a instrumentation
202     // library (i.e. the file name follow the expected pattern).
203     bool isInstrumentationLib(const dirent *file);
204 
205     // A list of registered instrumentation callbacks.
206     std::vector<InstrumentationCallback> mInstrumentationCallbacks;
207     // Flag whether to enable instrumentation.
208     union {
209         bool mEnableInstrumentation;
210         void* mReserved0;
211     };
212     // Prefix to lookup the instrumentation libraries.
213     std::string mInstrumentationLibPackage;
214     // Used for dlsym to load the profiling method for given interface.
215     std::string mInterfaceName;
216 
217 };
218 
219 #ifdef __LP64__
220 static_assert(sizeof(HidlInstrumentor) == 88, "HidlInstrumentor size frozen by prebuilts");
221 #else
222 static_assert(sizeof(HidlInstrumentor) == 44, "HidlInstrumentor size frozen by prebuilts");
223 #endif
224 
225 }  // namespace details
226 }  // namespace hardware
227 }  // namespace android
228 
229 #endif  // ANDROID_HIDL_INTERNAL_H
230