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 #define FOR_ALL_DLFUNCS(MACRO) \
32   MACRO(DexString, ExtDexFileMakeString) \
33   MACRO(DexString, ExtDexFileGetString) \
34   MACRO(DexString, ExtDexFileFreeString) \
35   MACRO(DexFile, ExtDexFileOpenFromMemory) \
36   MACRO(DexFile, ExtDexFileOpenFromFd) \
37   MACRO(DexFile, ExtDexFileGetMethodInfoForOffset) \
38   MACRO(DexFile, ExtDexFileGetAllMethodInfos) \
39   MACRO(DexFile, ExtDexFileFree)
40 
41 #ifdef STATIC_LIB
42 #define DEFINE_DLFUNC_PTR(CLASS, DLFUNC) decltype(DLFUNC)* CLASS::g_##DLFUNC = DLFUNC;
43 #else
44 #define DEFINE_DLFUNC_PTR(CLASS, DLFUNC) decltype(DLFUNC)* CLASS::g_##DLFUNC = nullptr;
45 #endif
FOR_ALL_DLFUNCS(DEFINE_DLFUNC_PTR)46 FOR_ALL_DLFUNCS(DEFINE_DLFUNC_PTR)
47 #undef DEFINE_DLFUNC_PTR
48 
49 bool TryLoadLibdexfileExternal([[maybe_unused]] std::string* err_msg) {
50 #if defined(STATIC_LIB)
51   // Nothing to do here since all function pointers are initialised statically.
52   return true;
53 #elif defined(NO_DEXFILE_SUPPORT)
54   *err_msg = "Dex file support not available.";
55   return false;
56 #else
57   // Use a plain old mutex since we want to try again if loading fails (to set
58   // err_msg, if nothing else).
59   static std::mutex load_mutex;
60   static bool is_loaded = false;
61   std::lock_guard<std::mutex> lock(load_mutex);
62 
63   if (!is_loaded) {
64     // Check which version is already loaded to avoid loading both debug and
65     // release builds. We might also be backtracing from separate process, in
66     // which case neither is loaded.
67     const char* so_name = "libdexfiled_external.so";
68     void* handle = dlopen(so_name, RTLD_NOLOAD | RTLD_NOW | RTLD_NODELETE);
69     if (handle == nullptr) {
70       so_name = "libdexfile_external.so";
71       handle = dlopen(so_name, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
72     }
73     if (handle == nullptr) {
74       *err_msg = dlerror();
75       return false;
76     }
77 
78 #define RESOLVE_DLFUNC_PTR(CLASS, DLFUNC) \
79     decltype(DLFUNC)* DLFUNC##_ptr = reinterpret_cast<decltype(DLFUNC)*>(dlsym(handle, #DLFUNC)); \
80     if ((DLFUNC) == nullptr) { \
81       *err_msg = dlerror(); \
82       return false; \
83     }
84     FOR_ALL_DLFUNCS(RESOLVE_DLFUNC_PTR);
85 #undef RESOLVE_DLFUNC_PTR
86 
87 #define SET_DLFUNC_PTR(CLASS, DLFUNC) CLASS::g_##DLFUNC = DLFUNC##_ptr;
88     FOR_ALL_DLFUNCS(SET_DLFUNC_PTR);
89 #undef SET_DLFUNC_PTR
90 
91     is_loaded = true;
92   }
93 
94   return is_loaded;
95 #endif  // !defined(NO_DEXFILE_SUPPORT) && !defined(STATIC_LIB)
96 }
97 
LoadLibdexfileExternal()98 void LoadLibdexfileExternal() {
99 #ifndef STATIC_LIB
100   if (std::string err_msg; !TryLoadLibdexfileExternal(&err_msg)) {
101     LOG_ALWAYS_FATAL("%s", err_msg.c_str());
102   }
103 #endif
104 }
105 
~DexFile()106 DexFile::~DexFile() { g_ExtDexFileFree(ext_dex_file_); }
107 
AbsorbMethodInfo(const ExtDexFileMethodInfo & ext_method_info)108 MethodInfo DexFile::AbsorbMethodInfo(const ExtDexFileMethodInfo& ext_method_info) {
109   return {ext_method_info.offset, ext_method_info.len, DexString(ext_method_info.name)};
110 }
111 
AddMethodInfoCallback(const ExtDexFileMethodInfo * ext_method_info,void * ctx)112 void DexFile::AddMethodInfoCallback(const ExtDexFileMethodInfo* ext_method_info, void* ctx) {
113   auto vect = static_cast<MethodInfoVector*>(ctx);
114   vect->emplace_back(AbsorbMethodInfo(*ext_method_info));
115 }
116 
117 }  // namespace dex
118 }  // namespace art_api
119