1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <dlfcn.h>
17 #include <string>
18 
19 #include "hook.h"
20 #include "thread_private_data_ctl.h"
21 #include "wrapper_log.h"
22 
23 using GetGlHookTableFunc = OHOS::GlHookTable* (*)();
24 using GetGlHookTableKeyFunc = pthread_key_t(*)();
25 template<typename Func = void*>
GetEglApi(const char * procname)26 Func GetEglApi(const char* procname)
27 {
28 #if (defined(__aarch64__) || defined(__x86_64__))
29     static const char* LIB_EGL = "/system/lib64/libEGL.so";
30 #else
31     static const char* LIB_EGL = "/system/lib/platformsdk/libEGL.so";
32 #endif
33     void* dlEglHandle = dlopen(LIB_EGL, RTLD_NOW | RTLD_GLOBAL);
34 
35     void* func = dlsym(dlEglHandle, procname);
36     if (func) {
37         return reinterpret_cast<Func>(func);
38     };
39 
40     return nullptr;
41 }
42 static GetGlHookTableFunc g_pfnGetGlHookTable = GetEglApi<GetGlHookTableFunc>("GetHookTable");
43 static GetGlHookTableKeyFunc g_pfnGetGlHookTableKey = GetEglApi<GetGlHookTableKeyFunc>("GetHookTableKey");
44 
45 static pthread_key_t g_glHookTableKey = -1;
46 
GetHookTable()47 __attribute__((__always_inline__))__inline__ static OHOS::GlHookTable *GetHookTable()
48 {
49     if (__builtin_expect(g_glHookTableKey != static_cast<pthread_key_t>(-1), 1)) {
50         OHOS::GlHookTable *table = static_cast<OHOS::GlHookTable *>(pthread_getspecific(g_glHookTableKey));
51         if (__builtin_expect(table != nullptr, 1)) {
52             return table;
53         }
54     }
55 
56     OHOS::GlHookTable *table = g_pfnGetGlHookTable();
57     g_glHookTableKey = g_pfnGetGlHookTableKey();
58     return table;
59 }
60 
61 #undef CALL_HOOK_API
62 #define CALL_HOOK_API(api, ...)                                                         \
63     do {                                                                                \
64         OHOS::GlHookTable *table = GetHookTable();                               \
65         if (table && table->table3.api) {                                               \
66             table->table3.api(__VA_ARGS__);                                             \
67         } else {                                                                        \
68             WLOGE("%{public}s is invalid.", #api);                                      \
69         }                                                                               \
70     } while (0);                                                                        \
71 }
72 
73 #undef CALL_HOOK_API_RET
74 #define CALL_HOOK_API_RET(api, ...) do {                                                \
75         OHOS::GlHookTable *table = GetHookTable();                               \
76         if (table && table->table3.api) {                                               \
77             return table->table3.api(__VA_ARGS__);                                      \
78         } else {                                                                        \
79             WLOGE("%{public}s is invalid.", #api);                                      \
80             return 0;                                                                   \
81         }                                                                               \
82     } while (0);                                                                        \
83 }
84 #undef HOOK_API_ENTRY
85 #define HOOK_API_ENTRY(r, api, ...) r api(__VA_ARGS__) {                                \
86 
87 extern "C" {
88 #pragma GCC diagnostic ignored "-Wunused-parameter"
89 #include "gl3_hook_entries.in"
90 #pragma GCC diagnostic warning "-Wunused-parameter"
91 }
92