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" |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 43 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 44 | namespace openjdkjvmti { |
| 45 | |
| 46 | // A struct that stores data needed for redefining/transforming classes. This structure should only |
| 47 | // even be accessed from a single thread and must not survive past the completion of the |
| 48 | // redefinition/retransformation function that created it. |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 49 | class ArtClassDefinition { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 50 | public: |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 51 | // If we support doing a on-demand dex-dequickening using signal handlers. |
Alex Light | fe2a39d | 2018-02-05 11:08:08 -0800 | [diff] [blame] | 52 | static constexpr bool kEnableOnDemandDexDequicken = true; |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 53 | |
Alex Light | 5127178 | 2017-03-24 17:28:30 -0700 | [diff] [blame] | 54 | ArtClassDefinition() |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 55 | : klass_(nullptr), |
| 56 | loader_(nullptr), |
| 57 | name_(), |
| 58 | protection_domain_(nullptr), |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 59 | dex_data_mmap_(nullptr), |
| 60 | temp_mmap_(nullptr), |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 61 | dex_data_memory_(), |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 62 | initial_dex_file_unquickened_(nullptr), |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 63 | dex_data_(), |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 64 | current_dex_memory_(), |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 65 | current_dex_file_(), |
| 66 | redefined_(false), |
| 67 | from_class_ext_(false), |
| 68 | initialized_(false) {} |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 69 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 70 | void InitFirstLoad(const char* descriptor, |
| 71 | art::Handle<art::mirror::ClassLoader> klass_loader, |
| 72 | const art::DexFile& dex_file); |
| 73 | jvmtiError Init(art::Thread* self, jclass klass); |
| 74 | jvmtiError Init(art::Thread* self, const jvmtiClassDefinition& def); |
Alex Light | 5127178 | 2017-03-24 17:28:30 -0700 | [diff] [blame] | 75 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 76 | ArtClassDefinition(ArtClassDefinition&& o) = default; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 77 | ArtClassDefinition& operator=(ArtClassDefinition&& o) = default; |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 78 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 79 | void SetNewDexData(jint new_dex_len, unsigned char* new_dex_data) { |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 80 | DCHECK(IsInitialized()); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 81 | if (new_dex_data == nullptr) { |
| 82 | return; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 83 | } else { |
| 84 | art::ArrayRef<const unsigned char> new_data(new_dex_data, new_dex_len); |
| 85 | if (new_data != dex_data_) { |
| 86 | dex_data_memory_.resize(new_dex_len); |
| 87 | memcpy(dex_data_memory_.data(), new_dex_data, new_dex_len); |
| 88 | dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_); |
| 89 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 93 | art::ArrayRef<const unsigned char> GetNewOriginalDexFile() const { |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 94 | DCHECK(IsInitialized()); |
| 95 | if (redefined_) { |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 96 | return current_dex_file_; |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 97 | } else { |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 98 | return art::ArrayRef<const unsigned char>(); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 102 | bool ContainsAddress(uintptr_t ptr) const { |
| 103 | return dex_data_mmap_ != nullptr && |
| 104 | reinterpret_cast<uintptr_t>(dex_data_mmap_->Begin()) <= ptr && |
| 105 | reinterpret_cast<uintptr_t>(dex_data_mmap_->End()) > ptr; |
| 106 | } |
| 107 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 108 | bool IsModified() const REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 109 | |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 110 | bool IsInitialized() const { |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 111 | return initialized_; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | jclass GetClass() const { |
| 115 | DCHECK(IsInitialized()); |
| 116 | return klass_; |
| 117 | } |
| 118 | |
| 119 | jobject GetLoader() const { |
| 120 | DCHECK(IsInitialized()); |
| 121 | return loader_; |
| 122 | } |
| 123 | |
| 124 | const std::string& GetName() const { |
| 125 | DCHECK(IsInitialized()); |
| 126 | return name_; |
| 127 | } |
| 128 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 129 | bool IsLazyDefinition() const { |
| 130 | DCHECK(IsInitialized()); |
| 131 | return dex_data_mmap_ != nullptr && |
| 132 | dex_data_.data() == dex_data_mmap_->Begin() && |
| 133 | dex_data_mmap_->GetProtect() == PROT_NONE; |
| 134 | } |
| 135 | |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 136 | jobject GetProtectionDomain() const { |
| 137 | DCHECK(IsInitialized()); |
| 138 | return protection_domain_; |
| 139 | } |
| 140 | |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 141 | art::ArrayRef<const unsigned char> GetDexData() const { |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 142 | DCHECK(IsInitialized()); |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 143 | return dex_data_; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 146 | void InitializeMemory() const; |
| 147 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 148 | private: |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 149 | jvmtiError InitCommon(art::Thread* self, jclass klass); |
| 150 | |
| 151 | template<typename GetOriginalDexFile> |
| 152 | void InitWithDex(GetOriginalDexFile get_original, const art::DexFile* quick_dex) |
| 153 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 154 | |
| 155 | jclass klass_; |
| 156 | jobject loader_; |
| 157 | std::string name_; |
| 158 | jobject protection_domain_; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 159 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 160 | // Mmap that will be filled with the original-dex-file lazily if it needs to be de-quickened or |
| 161 | // de-compact-dex'd |
| 162 | mutable std::unique_ptr<art::MemMap> dex_data_mmap_; |
| 163 | // This is a temporary mmap we will use to be able to fill the dex file data atomically. |
| 164 | mutable std::unique_ptr<art::MemMap> temp_mmap_; |
| 165 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 166 | // A unique_ptr to the current dex_data if it needs to be cleaned up. |
| 167 | std::vector<unsigned char> dex_data_memory_; |
| 168 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 169 | const art::DexFile* initial_dex_file_unquickened_; |
| 170 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 171 | // A ref to the current dex data. This is either dex_data_memory_, or current_dex_file_. This is |
| 172 | // what the dex file will be turned into. |
| 173 | art::ArrayRef<const unsigned char> dex_data_; |
| 174 | |
| 175 | // This is only used if we failed to create a mmap to store the dequickened data |
| 176 | std::vector<unsigned char> current_dex_memory_; |
| 177 | |
| 178 | // This is a dequickened version of what is loaded right now. It is either current_dex_memory_ (if |
| 179 | // no other redefinition has ever happened to this) or the current dex_file_ directly (if this |
| 180 | // class has been redefined, thus it cannot have any quickened stuff). |
| 181 | art::ArrayRef<const unsigned char> current_dex_file_; |
| 182 | |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 183 | bool redefined_; |
| 184 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 185 | // If we got the initial dex_data_ from a class_ext |
| 186 | bool from_class_ext_; |
| 187 | |
| 188 | bool initialized_; |
| 189 | |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 190 | DISALLOW_COPY_AND_ASSIGN(ArtClassDefinition); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | } // namespace openjdkjvmti |
| 194 | |
Andreas Gampe | 06c42a5 | 2017-07-26 14:17:14 -0700 | [diff] [blame] | 195 | #endif // ART_OPENJDKJVMTI_TI_CLASS_DEFINITION_H_ |