1 /*
2  * Copyright (C) 2018 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 #include "art_api/dex_file_support.h"
18 
19 #include <dlfcn.h>
20 #include <mutex>
21 
22 #ifndef STATIC_LIB
23 // Not used in the static lib, so avoid a dependency on this header in
24 // libdexfile_support_static.
25 #include <log/log.h>
26 #endif
27 
28 namespace art_api {
29 namespace dex {
30 
31 #ifdef STATIC_LIB
32 #define DEFINE_DLFUNC_PTR(CLASS, DLFUNC) decltype(DLFUNC)* CLASS::g_##DLFUNC = DLFUNC
33 #else
34 #define DEFINE_DLFUNC_PTR(CLASS, DLFUNC) decltype(DLFUNC)* CLASS::g_##DLFUNC = nullptr
35 #endif
36 
37 DEFINE_DLFUNC_PTR(DexString, ExtDexFileMakeString);
38 DEFINE_DLFUNC_PTR(DexString, ExtDexFileGetString);
39 DEFINE_DLFUNC_PTR(DexString, ExtDexFileFreeString);
40 DEFINE_DLFUNC_PTR(DexFile, ExtDexFileOpenFromMemory);
41 DEFINE_DLFUNC_PTR(DexFile, ExtDexFileOpenFromFd);
42 DEFINE_DLFUNC_PTR(DexFile, ExtDexFileGetMethodInfoForOffset);
43 DEFINE_DLFUNC_PTR(DexFile, ExtDexFileGetAllMethodInfos);
44 DEFINE_DLFUNC_PTR(DexFile, ExtDexFileFree);
45 
46 #undef DEFINE_DLFUNC_PTR
47 
LoadLibdexfileExternal()48 void LoadLibdexfileExternal() {
49 #if defined(STATIC_LIB)
50   // Nothing to do here since all function pointers are initialised statically.
51 #elif defined(NO_DEXFILE_SUPPORT)
52   LOG_FATAL("Dex file support not available.");
53 #else
54   static std::once_flag dlopen_once;
55   std::call_once(dlopen_once, []() {
56     constexpr char kLibdexfileExternalLib[] = "libdexfile_external.so";
57     void* handle =
58         dlopen(kLibdexfileExternalLib, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
59     LOG_ALWAYS_FATAL_IF(handle == nullptr, "Failed to load %s: %s",
60                         kLibdexfileExternalLib, dlerror());
61 
62 #define SET_DLFUNC_PTR(CLASS, DLFUNC) \
63   do { \
64     CLASS::g_##DLFUNC = reinterpret_cast<decltype(DLFUNC)*>(dlsym(handle, #DLFUNC)); \
65     LOG_ALWAYS_FATAL_IF(CLASS::g_##DLFUNC == nullptr, \
66                         "Failed to find %s in %s: %s", \
67                         #DLFUNC, \
68                         kLibdexfileExternalLib, \
69                         dlerror()); \
70   } while (0)
71 
72     SET_DLFUNC_PTR(DexString, ExtDexFileMakeString);
73     SET_DLFUNC_PTR(DexString, ExtDexFileGetString);
74     SET_DLFUNC_PTR(DexString, ExtDexFileFreeString);
75     SET_DLFUNC_PTR(DexFile, ExtDexFileOpenFromMemory);
76     SET_DLFUNC_PTR(DexFile, ExtDexFileOpenFromFd);
77     SET_DLFUNC_PTR(DexFile, ExtDexFileGetMethodInfoForOffset);
78     SET_DLFUNC_PTR(DexFile, ExtDexFileGetAllMethodInfos);
79     SET_DLFUNC_PTR(DexFile, ExtDexFileFree);
80 
81 #undef SET_DLFUNC_PTR
82   });
83 #endif  // !defined(NO_DEXFILE_SUPPORT) && !defined(STATIC_LIB)
84 }
85 
~DexFile()86 DexFile::~DexFile() { g_ExtDexFileFree(ext_dex_file_); }
87 
AbsorbMethodInfo(const ExtDexFileMethodInfo & ext_method_info)88 MethodInfo DexFile::AbsorbMethodInfo(const ExtDexFileMethodInfo& ext_method_info) {
89   return {ext_method_info.offset, ext_method_info.len, DexString(ext_method_info.name)};
90 }
91 
AddMethodInfoCallback(const ExtDexFileMethodInfo * ext_method_info,void * ctx)92 void DexFile::AddMethodInfoCallback(const ExtDexFileMethodInfo* ext_method_info, void* ctx) {
93   auto vect = static_cast<MethodInfoVector*>(ctx);
94   vect->emplace_back(AbsorbMethodInfo(*ext_method_info));
95 }
96 
97 }  // namespace dex
98 }  // namespace art_api
99