1 /**
2 * Copyright (c) 2021-2025 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 "file_ext.h"
17 #include "panda_file_external.h"
18 #include "os/mutex.h"
19 #include <dlfcn.h>
20 #include "libpandafile/class_data_accessor-inl.h"
21 #include "libpandafile/code_data_accessor-inl.h"
22
23 namespace panda_api::panda_file {
24
25 decltype(OpenPandafileFromFdExt) *PandaFileWrapper::pOpenPandafileFromFdExt = nullptr;
26 decltype(OpenPandafileFromMemoryExt) *PandaFileWrapper::pOpenPandafileFromMemoryExt = nullptr;
27 decltype(QueryMethodSymByOffsetExt) *PandaFileWrapper::pQueryMethodSymByOffsetExt = nullptr;
28 decltype(QueryMethodSymAndLineByOffsetExt) *PandaFileWrapper::pQueryMethodSymAndLineByOffsetExt = nullptr;
29 decltype(QueryAllMethodSymsExt) *PandaFileWrapper::pQueryAllMethodSymsExt = nullptr;
30
31 using OnceCallback = void();
CallOnce(bool * onceFlag,OnceCallback callBack)32 static void CallOnce(bool *onceFlag, OnceCallback callBack)
33 {
34 static ark::os::memory::Mutex onceLock;
35 ark::os::memory::LockHolder lock(onceLock);
36 if (!(*onceFlag)) {
37 callBack();
38 *onceFlag = true;
39 }
40 }
41
42 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
43 #define LOAD_FUNC_OR_RETURN(CLASS, FUNC, HANDLE) \
44 do { \
45 CLASS::p##FUNC = reinterpret_cast<decltype(CLASS::p##FUNC)>(dlsym(HANDLE, #FUNC)); \
46 if (CLASS::p##FUNC == nullptr) { \
47 ClearAllLoadedFuncs(); \
48 dlclose(HANDLE); \
49 return; \
50 } \
51 } while (0)
52
ClearAllLoadedFuncs()53 static void ClearAllLoadedFuncs()
54 {
55 PandaFileWrapper::pOpenPandafileFromFdExt = nullptr;
56 PandaFileWrapper::pOpenPandafileFromMemoryExt = nullptr;
57 PandaFileWrapper::pQueryMethodSymByOffsetExt = nullptr;
58 PandaFileWrapper::pQueryMethodSymAndLineByOffsetExt = nullptr;
59 PandaFileWrapper::pQueryAllMethodSymsExt = nullptr;
60 }
61
LoadPandFileExt()62 void LoadPandFileExt()
63 {
64 static bool dlopenOnce = false;
65 CallOnce(&dlopenOnce, []() {
66 const char pandafileext[] = "libarkfileExt.so";
67 void *hd = dlopen(pandafileext, RTLD_NOW);
68 if (hd == nullptr) {
69 return;
70 }
71
72 LOAD_FUNC_OR_RETURN(PandaFileWrapper, OpenPandafileFromFdExt, hd);
73 LOAD_FUNC_OR_RETURN(PandaFileWrapper, OpenPandafileFromMemoryExt, hd);
74 LOAD_FUNC_OR_RETURN(PandaFileWrapper, QueryMethodSymByOffsetExt, hd);
75 LOAD_FUNC_OR_RETURN(PandaFileWrapper, QueryMethodSymAndLineByOffsetExt, hd);
76 LOAD_FUNC_OR_RETURN(PandaFileWrapper, QueryAllMethodSymsExt, hd);
77 });
78 }
79 #undef LOAD_FUNC_OR_RETURN
80
81 } // namespace panda_api::panda_file
82