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 <inttypes.h>
21 #include <mutex>
22 #include <sys/stat.h>
23 
24 #ifndef STATIC_LIB
25 // Not used in the static lib, so avoid a dependency on this header in
26 // libdexfile_support_static.
27 #include <log/log.h>
28 #endif
29 
30 namespace art_api {
31 namespace dex {
32 
33 #if defined(STATIC_LIB)
34 #define DEFINE_ADEX_FILE_SYMBOL(DLFUNC) decltype(DLFUNC)* g_##DLFUNC = DLFUNC;
35 #else
36 #define DEFINE_ADEX_FILE_SYMBOL(DLFUNC) decltype(DLFUNC)* g_##DLFUNC = nullptr;
37 #endif
FOR_EACH_ADEX_FILE_SYMBOL(DEFINE_ADEX_FILE_SYMBOL)38 FOR_EACH_ADEX_FILE_SYMBOL(DEFINE_ADEX_FILE_SYMBOL)
39 #undef DEFINE_ADEX_FILE_SYMBOL
40 
41 bool TryLoadLibdexfile([[maybe_unused]] std::string* err_msg) {
42 #if defined(STATIC_LIB)
43   // Nothing to do here since all function pointers are initialised statically.
44   return true;
45 #elif defined(NO_DEXFILE_SUPPORT)
46   *err_msg = "Dex file support not available.";
47   return false;
48 #else
49   // Use a plain old mutex since we want to try again if loading fails (to set
50   // err_msg, if nothing else).
51   static std::mutex load_mutex;
52   static bool is_loaded = false;
53   std::lock_guard<std::mutex> lock(load_mutex);
54 
55   if (!is_loaded) {
56     // Check which version is already loaded to avoid loading both debug and
57     // release builds. We might also be backtracing from separate process, in
58     // which case neither is loaded.
59     const char* so_name = "libdexfiled.so";
60     void* handle = dlopen(so_name, RTLD_NOLOAD | RTLD_NOW | RTLD_NODELETE);
61     if (handle == nullptr) {
62       so_name = "libdexfile.so";
63       handle = dlopen(so_name, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
64     }
65     if (handle == nullptr) {
66       *err_msg = dlerror();
67       return false;
68     }
69 
70 #define RESOLVE_ADEX_FILE_SYMBOL(DLFUNC) \
71     auto DLFUNC##_ptr = reinterpret_cast<decltype(DLFUNC)*>(dlsym(handle, #DLFUNC)); \
72     if (DLFUNC##_ptr == nullptr) { \
73       *err_msg = dlerror(); \
74       return false; \
75     }
76 FOR_EACH_ADEX_FILE_SYMBOL(RESOLVE_ADEX_FILE_SYMBOL)
77 #undef RESOLVE_ADEX_FILE_SYMBOL
78 
79 #define SET_ADEX_FILE_SYMBOL(DLFUNC) g_##DLFUNC = DLFUNC##_ptr;
80     FOR_EACH_ADEX_FILE_SYMBOL(SET_ADEX_FILE_SYMBOL);
81 #undef SET_ADEX_FILE_SYMBOL
82 
83     is_loaded = true;
84   }
85 
86   return is_loaded;
87 #endif  // !defined(NO_DEXFILE_SUPPORT) && !defined(STATIC_LIB)
88 }
89 
LoadLibdexfile()90 void LoadLibdexfile() {
91 #ifndef STATIC_LIB
92   if (std::string err_msg; !TryLoadLibdexfile(&err_msg)) {
93     LOG_ALWAYS_FATAL("%s", err_msg.c_str());
94   }
95 #endif
96 }
97 
98 }  // namespace dex
99 }  // namespace art_api
100