Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1 | /* Copyright (C) 2017 The Android Open Source Project |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | * |
| 4 | * This file implements interfaces from the file jvmti.h. This implementation |
| 5 | * is licensed under the same terms as the file jvmti.h. The |
| 6 | * copyright and license information for the file jvmti.h follows. |
| 7 | * |
| 8 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
| 9 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 10 | * |
| 11 | * This code is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License version 2 only, as |
| 13 | * published by the Free Software Foundation. Oracle designates this |
| 14 | * particular file as subject to the "Classpath" exception as provided |
| 15 | * by Oracle in the LICENSE file that accompanied this code. |
| 16 | * |
| 17 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * version 2 for more details (a copy is included in the LICENSE file that |
| 21 | * accompanied this code). |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License version |
| 24 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 25 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | * |
| 27 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 28 | * or visit www.oracle.com if you need additional information or have any |
| 29 | * questions. |
| 30 | */ |
| 31 | |
Andreas Gampe | 06c42a5 | 2017-07-26 14:17:14 -0700 | [diff] [blame] | 32 | #ifndef ART_OPENJDKJVMTI_TI_CLASS_DEFINITION_H_ |
| 33 | #define ART_OPENJDKJVMTI_TI_CLASS_DEFINITION_H_ |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 34 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 35 | #include <stddef.h> |
| 36 | #include <sys/mman.h> |
| 37 | #include <sys/types.h> |
| 38 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 39 | #include "art_jvmti.h" |
| 40 | |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 41 | #include "base/array_ref.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 42 | #include "base/mem_map.h" |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 43 | #include "events.h" |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 44 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 45 | namespace openjdkjvmti { |
| 46 | |
| 47 | // A struct that stores data needed for redefining/transforming classes. This structure should only |
| 48 | // even be accessed from a single thread and must not survive past the completion of the |
| 49 | // redefinition/retransformation function that created it. |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 50 | class ArtClassDefinition { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 51 | public: |
Jack Nudelman | 139a5b9 | 2021-09-30 20:04:13 +0000 | [diff] [blame] | 52 | // If we support doing a on-demand dex-dequickening using signal handlers. |
Muhammad Qureshi | ce73d81 | 2022-02-03 00:51:04 +0000 | [diff] [blame] | 53 | static constexpr bool kEnableOnDemandDexDequicken = true; |
Jack Nudelman | 139a5b9 | 2021-09-30 20:04:13 +0000 | [diff] [blame] | 54 | |
Alex Light | 5127178 | 2017-03-24 17:28:30 -0700 | [diff] [blame] | 55 | ArtClassDefinition() |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 56 | : klass_(nullptr), |
| 57 | loader_(nullptr), |
| 58 | name_(), |
| 59 | protection_domain_(nullptr), |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 60 | dex_data_mmap_(), |
| 61 | temp_mmap_(), |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 62 | dex_data_memory_(), |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 63 | initial_dex_file_unquickened_(nullptr), |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 64 | dex_data_(), |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 65 | current_dex_memory_(), |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 66 | current_dex_file_(), |
| 67 | redefined_(false), |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 68 | initialized_(false), |
| 69 | structural_transform_update_(false) {} |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 70 | |
Nicolas Geoffray | ad67bbb | 2022-02-04 17:26:40 +0000 | [diff] [blame^] | 71 | jvmtiError InitFirstLoad(const char* descriptor, |
| 72 | art::Handle<art::mirror::ClassLoader> klass_loader, |
| 73 | const art::DexFile& dex_file); |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 74 | jvmtiError Init(art::Thread* self, jclass klass); |
| 75 | jvmtiError Init(art::Thread* self, const jvmtiClassDefinition& def); |
Alex Light | 5127178 | 2017-03-24 17:28:30 -0700 | [diff] [blame] | 76 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 77 | ArtClassDefinition(ArtClassDefinition&& o) = default; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 78 | ArtClassDefinition& operator=(ArtClassDefinition&& o) = default; |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 79 | |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 80 | void SetNewDexData(jint new_dex_len, unsigned char* new_dex_data, ArtJvmtiEvent event) { |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 81 | DCHECK(IsInitialized()); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 82 | if (new_dex_data == nullptr) { |
| 83 | return; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 84 | } else { |
| 85 | art::ArrayRef<const unsigned char> new_data(new_dex_data, new_dex_len); |
| 86 | if (new_data != dex_data_) { |
| 87 | dex_data_memory_.resize(new_dex_len); |
| 88 | memcpy(dex_data_memory_.data(), new_dex_data, new_dex_len); |
| 89 | dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_); |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 90 | if (event == ArtJvmtiEvent::kStructuralDexFileLoadHook) { |
| 91 | structural_transform_update_ = true; |
| 92 | } |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 93 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 97 | bool HasStructuralChanges() const { |
| 98 | return structural_transform_update_; |
| 99 | } |
| 100 | |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 101 | art::ArrayRef<const unsigned char> GetNewOriginalDexFile() const { |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 102 | DCHECK(IsInitialized()); |
| 103 | if (redefined_) { |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 104 | return current_dex_file_; |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 105 | } else { |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 106 | return art::ArrayRef<const unsigned char>(); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 110 | bool ContainsAddress(uintptr_t ptr) const { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 111 | return dex_data_mmap_.IsValid() && |
| 112 | reinterpret_cast<uintptr_t>(dex_data_mmap_.Begin()) <= ptr && |
| 113 | reinterpret_cast<uintptr_t>(dex_data_mmap_.End()) > ptr; |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 116 | bool IsModified() const REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 117 | |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 118 | bool IsInitialized() const { |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 119 | return initialized_; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | jclass GetClass() const { |
| 123 | DCHECK(IsInitialized()); |
| 124 | return klass_; |
| 125 | } |
| 126 | |
| 127 | jobject GetLoader() const { |
| 128 | DCHECK(IsInitialized()); |
| 129 | return loader_; |
| 130 | } |
| 131 | |
| 132 | const std::string& GetName() const { |
| 133 | DCHECK(IsInitialized()); |
| 134 | return name_; |
| 135 | } |
| 136 | |
Jack Nudelman | 139a5b9 | 2021-09-30 20:04:13 +0000 | [diff] [blame] | 137 | bool IsLazyDefinition() const { |
| 138 | DCHECK(IsInitialized()); |
| 139 | return dex_data_mmap_.IsValid() && |
| 140 | dex_data_.data() == dex_data_mmap_.Begin() && |
| 141 | dex_data_mmap_.GetProtect() == PROT_NONE; |
| 142 | } |
| 143 | |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 144 | jobject GetProtectionDomain() const { |
| 145 | DCHECK(IsInitialized()); |
| 146 | return protection_domain_; |
| 147 | } |
| 148 | |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 149 | art::ArrayRef<const unsigned char> GetDexData() const { |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 150 | DCHECK(IsInitialized()); |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 151 | return dex_data_; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 154 | void InitializeMemory() const; |
| 155 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 156 | private: |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 157 | jvmtiError InitCommon(art::Thread* self, jclass klass); |
Nicolas Geoffray | ad67bbb | 2022-02-04 17:26:40 +0000 | [diff] [blame^] | 158 | jvmtiError Init(const art::DexFile& dex_file); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 159 | |
| 160 | jclass klass_; |
| 161 | jobject loader_; |
| 162 | std::string name_; |
| 163 | jobject protection_domain_; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 164 | |
Jack Nudelman | 139a5b9 | 2021-09-30 20:04:13 +0000 | [diff] [blame] | 165 | // Mmap that will be filled with the original-dex-file lazily if it needs to be de-quickened or |
| 166 | // de-compact-dex'd |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 167 | mutable art::MemMap dex_data_mmap_; |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 168 | // This is a temporary mmap we will use to be able to fill the dex file data atomically. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 169 | mutable art::MemMap temp_mmap_; |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 170 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 171 | // A unique_ptr to the current dex_data if it needs to be cleaned up. |
| 172 | std::vector<unsigned char> dex_data_memory_; |
| 173 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 174 | const art::DexFile* initial_dex_file_unquickened_; |
| 175 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 176 | // A ref to the current dex data. This is either dex_data_memory_, or current_dex_file_. This is |
| 177 | // what the dex file will be turned into. |
| 178 | art::ArrayRef<const unsigned char> dex_data_; |
| 179 | |
| 180 | // This is only used if we failed to create a mmap to store the dequickened data |
| 181 | std::vector<unsigned char> current_dex_memory_; |
| 182 | |
| 183 | // This is a dequickened version of what is loaded right now. It is either current_dex_memory_ (if |
| 184 | // no other redefinition has ever happened to this) or the current dex_file_ directly (if this |
| 185 | // class has been redefined, thus it cannot have any quickened stuff). |
| 186 | art::ArrayRef<const unsigned char> current_dex_file_; |
| 187 | |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 188 | bool redefined_; |
| 189 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 190 | bool initialized_; |
| 191 | |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 192 | // Set if we had a new dex from the given transform type. |
| 193 | bool structural_transform_update_; |
| 194 | |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 195 | DISALLOW_COPY_AND_ASSIGN(ArtClassDefinition); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | } // namespace openjdkjvmti |
| 199 | |
Andreas Gampe | 06c42a5 | 2017-07-26 14:17:14 -0700 | [diff] [blame] | 200 | #endif // ART_OPENJDKJVMTI_TI_CLASS_DEFINITION_H_ |