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