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 32 #ifndef ART_OPENJDKJVMTI_TI_CLASS_DEFINITION_H_ 33 #define ART_OPENJDKJVMTI_TI_CLASS_DEFINITION_H_ 34 35 #include <stddef.h> 36 #include <sys/mman.h> 37 #include <sys/types.h> 38 39 #include "art_jvmti.h" 40 41 #include "base/array_ref.h" 42 #include "base/mem_map.h" 43 #include "events.h" 44 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. 50 class ArtClassDefinition { 51 public: 52 // If we support doing a on-demand dex-dequickening using signal handlers. 53 static constexpr bool kEnableOnDemandDexDequicken = true; 54 ArtClassDefinition()55 ArtClassDefinition() 56 : klass_(nullptr), 57 loader_(nullptr), 58 name_(), 59 protection_domain_(nullptr), 60 dex_data_mmap_(), 61 temp_mmap_(), 62 dex_data_memory_(), 63 initial_dex_file_unquickened_(nullptr), 64 dex_data_(), 65 current_dex_memory_(), 66 current_dex_file_(), 67 redefined_(false), 68 from_class_ext_(false), 69 initialized_(false), 70 structural_transform_update_(false) {} 71 72 void InitFirstLoad(const char* descriptor, 73 art::Handle<art::mirror::ClassLoader> klass_loader, 74 const art::DexFile& dex_file); 75 jvmtiError Init(art::Thread* self, jclass klass); 76 jvmtiError Init(art::Thread* self, const jvmtiClassDefinition& def); 77 78 ArtClassDefinition(ArtClassDefinition&& o) = default; 79 ArtClassDefinition& operator=(ArtClassDefinition&& o) = default; 80 SetNewDexData(jint new_dex_len,unsigned char * new_dex_data,ArtJvmtiEvent event)81 void SetNewDexData(jint new_dex_len, unsigned char* new_dex_data, ArtJvmtiEvent event) { 82 DCHECK(IsInitialized()); 83 if (new_dex_data == nullptr) { 84 return; 85 } else { 86 art::ArrayRef<const unsigned char> new_data(new_dex_data, new_dex_len); 87 if (new_data != dex_data_) { 88 dex_data_memory_.resize(new_dex_len); 89 memcpy(dex_data_memory_.data(), new_dex_data, new_dex_len); 90 dex_data_ = art::ArrayRef<const unsigned char>(dex_data_memory_); 91 if (event == ArtJvmtiEvent::kStructuralDexFileLoadHook) { 92 structural_transform_update_ = true; 93 } 94 } 95 } 96 } 97 HasStructuralChanges()98 bool HasStructuralChanges() const { 99 return structural_transform_update_; 100 } 101 GetNewOriginalDexFile()102 art::ArrayRef<const unsigned char> GetNewOriginalDexFile() const { 103 DCHECK(IsInitialized()); 104 if (redefined_) { 105 return current_dex_file_; 106 } else { 107 return art::ArrayRef<const unsigned char>(); 108 } 109 } 110 ContainsAddress(uintptr_t ptr)111 bool ContainsAddress(uintptr_t ptr) const { 112 return dex_data_mmap_.IsValid() && 113 reinterpret_cast<uintptr_t>(dex_data_mmap_.Begin()) <= ptr && 114 reinterpret_cast<uintptr_t>(dex_data_mmap_.End()) > ptr; 115 } 116 117 bool IsModified() const REQUIRES_SHARED(art::Locks::mutator_lock_); 118 IsInitialized()119 bool IsInitialized() const { 120 return initialized_; 121 } 122 GetClass()123 jclass GetClass() const { 124 DCHECK(IsInitialized()); 125 return klass_; 126 } 127 GetLoader()128 jobject GetLoader() const { 129 DCHECK(IsInitialized()); 130 return loader_; 131 } 132 GetName()133 const std::string& GetName() const { 134 DCHECK(IsInitialized()); 135 return name_; 136 } 137 IsLazyDefinition()138 bool IsLazyDefinition() const { 139 DCHECK(IsInitialized()); 140 return dex_data_mmap_.IsValid() && 141 dex_data_.data() == dex_data_mmap_.Begin() && 142 dex_data_mmap_.GetProtect() == PROT_NONE; 143 } 144 GetProtectionDomain()145 jobject GetProtectionDomain() const { 146 DCHECK(IsInitialized()); 147 return protection_domain_; 148 } 149 GetDexData()150 art::ArrayRef<const unsigned char> GetDexData() const { 151 DCHECK(IsInitialized()); 152 return dex_data_; 153 } 154 155 void InitializeMemory() const; 156 157 private: 158 jvmtiError InitCommon(art::Thread* self, jclass klass); 159 160 template<typename GetOriginalDexFile> 161 void InitWithDex(GetOriginalDexFile get_original, const art::DexFile* quick_dex) 162 REQUIRES_SHARED(art::Locks::mutator_lock_); 163 164 jclass klass_; 165 jobject loader_; 166 std::string name_; 167 jobject protection_domain_; 168 169 // Mmap that will be filled with the original-dex-file lazily if it needs to be de-quickened or 170 // de-compact-dex'd 171 mutable art::MemMap dex_data_mmap_; 172 // This is a temporary mmap we will use to be able to fill the dex file data atomically. 173 mutable art::MemMap temp_mmap_; 174 175 // A unique_ptr to the current dex_data if it needs to be cleaned up. 176 std::vector<unsigned char> dex_data_memory_; 177 178 const art::DexFile* initial_dex_file_unquickened_; 179 180 // A ref to the current dex data. This is either dex_data_memory_, or current_dex_file_. This is 181 // what the dex file will be turned into. 182 art::ArrayRef<const unsigned char> dex_data_; 183 184 // This is only used if we failed to create a mmap to store the dequickened data 185 std::vector<unsigned char> current_dex_memory_; 186 187 // This is a dequickened version of what is loaded right now. It is either current_dex_memory_ (if 188 // no other redefinition has ever happened to this) or the current dex_file_ directly (if this 189 // class has been redefined, thus it cannot have any quickened stuff). 190 art::ArrayRef<const unsigned char> current_dex_file_; 191 192 bool redefined_; 193 194 // If we got the initial dex_data_ from a class_ext 195 bool from_class_ext_; 196 197 bool initialized_; 198 199 // Set if we had a new dex from the given transform type. 200 bool structural_transform_update_; 201 202 DISALLOW_COPY_AND_ASSIGN(ArtClassDefinition); 203 }; 204 205 } // namespace openjdkjvmti 206 207 #endif // ART_OPENJDKJVMTI_TI_CLASS_DEFINITION_H_ 208