1 /**
2 * Copyright (c) 2021-2024 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
LoadPandFileExt()42 void LoadPandFileExt()
43 {
44 static bool dlopenOnce = false;
45 CallOnce(&dlopenOnce, []() {
46 const char pandafileext[] = "libpandafileExt.so";
47 void *hd = dlopen(pandafileext, RTLD_NOW);
48 if (hd == nullptr) {
49 return;
50 }
51
52 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
53 #define LOAD_FUNC(CLASS, FUNC) \
54 do { \
55 /* CC-OFFNXT(G.PRE.10) function scope macro */ \
56 CLASS::p##FUNC = reinterpret_cast<decltype(FUNC) *>(dlsym(hd, #FUNC)); \
57 if (CLASS::p##FUNC == nullptr) { \
58 return; /* CC-OFF(G.PRE.05) function gen */ \
59 } \
60 } while (0)
61
62 LOAD_FUNC(PandaFileWrapper, OpenPandafileFromFdExt);
63 LOAD_FUNC(PandaFileWrapper, OpenPandafileFromMemoryExt);
64 LOAD_FUNC(PandaFileWrapper, QueryMethodSymByOffsetExt);
65 LOAD_FUNC(PandaFileWrapper, QueryMethodSymAndLineByOffsetExt);
66 LOAD_FUNC(PandaFileWrapper, QueryAllMethodSymsExt);
67 #undef LOAD_FUNC
68 });
69 }
70
71 } // namespace panda_api::panda_file
72