Martin Stjernholm | b3d2e83 | 2018-11-15 18:09:35 +0000 | [diff] [blame] | 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 | |
Martin Stjernholm | 2ccc9c3 | 2019-01-09 23:21:36 +0000 | [diff] [blame] | 17 | #include "art_api/dex_file_support.h" |
Martin Stjernholm | b3d2e83 | 2018-11-15 18:09:35 +0000 | [diff] [blame] | 18 | |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 19 | #include <dlfcn.h> |
David Srbecky | 2ddb98b | 2021-03-09 00:37:04 +0000 | [diff] [blame] | 20 | #include <inttypes.h> |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 21 | #include <mutex> |
David Srbecky | 2ddb98b | 2021-03-09 00:37:04 +0000 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | |
| 24 | #include <android-base/mapped_file.h> |
| 25 | #include <android-base/stringprintf.h> |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 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 | |
Martin Stjernholm | b3d2e83 | 2018-11-15 18:09:35 +0000 | [diff] [blame] | 33 | namespace art_api { |
| 34 | namespace dex { |
| 35 | |
David Srbecky | 30d4d2f | 2021-03-24 16:11:49 +0000 | [diff] [blame] | 36 | #if defined(STATIC_LIB) |
| 37 | #define DEFINE_ADEX_FILE_SYMBOL(DLFUNC) decltype(DLFUNC)* g_##DLFUNC = DLFUNC; |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 38 | #else |
David Srbecky | 30d4d2f | 2021-03-24 16:11:49 +0000 | [diff] [blame] | 39 | #define DEFINE_ADEX_FILE_SYMBOL(DLFUNC) decltype(DLFUNC)* g_##DLFUNC = nullptr; |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 40 | #endif |
David Srbecky | 30d4d2f | 2021-03-24 16:11:49 +0000 | [diff] [blame] | 41 | FOR_EACH_ADEX_FILE_SYMBOL(DEFINE_ADEX_FILE_SYMBOL) |
| 42 | #undef DEFINE_ADEX_FILE_SYMBOL |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 43 | |
David Srbecky | 30d4d2f | 2021-03-24 16:11:49 +0000 | [diff] [blame] | 44 | bool TryLoadLibdexfile([[maybe_unused]] std::string* err_msg) { |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 45 | #if defined(STATIC_LIB) |
| 46 | // Nothing to do here since all function pointers are initialised statically. |
Martin Stjernholm | 4490112 | 2019-10-04 01:03:48 +0100 | [diff] [blame] | 47 | return true; |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 48 | #elif defined(NO_DEXFILE_SUPPORT) |
Martin Stjernholm | 4490112 | 2019-10-04 01:03:48 +0100 | [diff] [blame] | 49 | *err_msg = "Dex file support not available."; |
| 50 | return false; |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 51 | #else |
Martin Stjernholm | 4490112 | 2019-10-04 01:03:48 +0100 | [diff] [blame] | 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. |
Martin Stjernholm | d3e9ff3 | 2021-03-16 00:44:25 +0000 | [diff] [blame] | 62 | const char* so_name = "libdexfiled.so"; |
David Srbecky | 7711c35 | 2019-04-10 17:50:12 +0100 | [diff] [blame] | 63 | void* handle = dlopen(so_name, RTLD_NOLOAD | RTLD_NOW | RTLD_NODELETE); |
| 64 | if (handle == nullptr) { |
Martin Stjernholm | d3e9ff3 | 2021-03-16 00:44:25 +0000 | [diff] [blame] | 65 | so_name = "libdexfile.so"; |
David Srbecky | 7711c35 | 2019-04-10 17:50:12 +0100 | [diff] [blame] | 66 | handle = dlopen(so_name, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE); |
| 67 | } |
Martin Stjernholm | 4490112 | 2019-10-04 01:03:48 +0100 | [diff] [blame] | 68 | if (handle == nullptr) { |
| 69 | *err_msg = dlerror(); |
| 70 | return false; |
| 71 | } |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 72 | |
David Srbecky | 30d4d2f | 2021-03-24 16:11:49 +0000 | [diff] [blame] | 73 | #define RESOLVE_ADEX_FILE_SYMBOL(DLFUNC) \ |
| 74 | auto DLFUNC##_ptr = reinterpret_cast<decltype(DLFUNC)*>(dlsym(handle, #DLFUNC)); \ |
| 75 | if (DLFUNC##_ptr == nullptr) { \ |
Martin Stjernholm | 4490112 | 2019-10-04 01:03:48 +0100 | [diff] [blame] | 76 | *err_msg = dlerror(); \ |
| 77 | return false; \ |
| 78 | } |
David Srbecky | 30d4d2f | 2021-03-24 16:11:49 +0000 | [diff] [blame] | 79 | FOR_EACH_ADEX_FILE_SYMBOL(RESOLVE_ADEX_FILE_SYMBOL) |
| 80 | #undef RESOLVE_ADEX_FILE_SYMBOL |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 81 | |
David Srbecky | 30d4d2f | 2021-03-24 16:11:49 +0000 | [diff] [blame] | 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 |
Martin Stjernholm | 4490112 | 2019-10-04 01:03:48 +0100 | [diff] [blame] | 85 | |
| 86 | is_loaded = true; |
| 87 | } |
| 88 | |
| 89 | return is_loaded; |
Martin Stjernholm | fe94b1c | 2019-01-31 17:40:39 +0000 | [diff] [blame] | 90 | #endif // !defined(NO_DEXFILE_SUPPORT) && !defined(STATIC_LIB) |
| 91 | } |
| 92 | |
David Srbecky | 30d4d2f | 2021-03-24 16:11:49 +0000 | [diff] [blame] | 93 | void LoadLibdexfile() { |
Martin Stjernholm | 4490112 | 2019-10-04 01:03:48 +0100 | [diff] [blame] | 94 | #ifndef STATIC_LIB |
David Srbecky | 30d4d2f | 2021-03-24 16:11:49 +0000 | [diff] [blame] | 95 | if (std::string err_msg; !TryLoadLibdexfile(&err_msg)) { |
Martin Stjernholm | 4490112 | 2019-10-04 01:03:48 +0100 | [diff] [blame] | 96 | LOG_ALWAYS_FATAL("%s", err_msg.c_str()); |
| 97 | } |
| 98 | #endif |
| 99 | } |
| 100 | |
Martin Stjernholm | b3d2e83 | 2018-11-15 18:09:35 +0000 | [diff] [blame] | 101 | } // namespace dex |
| 102 | } // namespace art_api |