Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1 | /* Copyright (C) 2016 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_RUNTIME_OPENJDKJVMTI_TI_REDEFINE_H_ |
| 33 | #define ART_RUNTIME_OPENJDKJVMTI_TI_REDEFINE_H_ |
| 34 | |
| 35 | #include <string> |
| 36 | |
| 37 | #include <jni.h> |
| 38 | |
| 39 | #include "art_jvmti.h" |
| 40 | #include "art_method.h" |
| 41 | #include "class_linker.h" |
| 42 | #include "dex_file.h" |
| 43 | #include "gc_root-inl.h" |
| 44 | #include "globals.h" |
| 45 | #include "jni_env_ext-inl.h" |
| 46 | #include "jvmti.h" |
| 47 | #include "linear_alloc.h" |
| 48 | #include "mem_map.h" |
| 49 | #include "mirror/array-inl.h" |
| 50 | #include "mirror/array.h" |
| 51 | #include "mirror/class-inl.h" |
| 52 | #include "mirror/class.h" |
| 53 | #include "mirror/class_loader-inl.h" |
| 54 | #include "mirror/string-inl.h" |
| 55 | #include "oat_file.h" |
| 56 | #include "obj_ptr.h" |
| 57 | #include "scoped_thread_state_change-inl.h" |
| 58 | #include "stack.h" |
| 59 | #include "thread_list.h" |
| 60 | #include "transform.h" |
| 61 | #include "utf.h" |
| 62 | #include "utils/dex_cache_arrays_layout-inl.h" |
| 63 | |
| 64 | namespace openjdkjvmti { |
| 65 | |
| 66 | // Class that can redefine a single class's methods. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 67 | // TODO We should really make this be driven by an outside class so we can do multiple classes at |
| 68 | // the same time and have less required cleanup. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 69 | class Redefiner { |
| 70 | public: |
| 71 | // Redefine the given class with the given dex data. Note this function does not take ownership of |
| 72 | // the dex_data pointer. It is not used after this call however and may be freed if desired. |
Alex Light | 0b77257 | 2016-12-02 17:27:31 -0800 | [diff] [blame] | 73 | // The caller is responsible for freeing it. The runtime makes its own copy of the data. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 74 | static jvmtiError RedefineClass(ArtJvmTiEnv* env, |
| 75 | art::Runtime* runtime, |
| 76 | art::Thread* self, |
| 77 | jclass klass, |
| 78 | const std::string& original_dex_location, |
| 79 | jint data_len, |
| 80 | unsigned char* dex_data, |
| 81 | std::string* error_msg); |
| 82 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 83 | static jvmtiError IsModifiableClass(jvmtiEnv* env, jclass klass, jboolean* is_redefinable); |
| 84 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 85 | private: |
| 86 | jvmtiError result_; |
| 87 | art::Runtime* runtime_; |
| 88 | art::Thread* self_; |
| 89 | // Kept as a jclass since we have weird run-state changes that make keeping it around as a |
| 90 | // mirror::Class difficult and confusing. |
| 91 | jclass klass_; |
| 92 | std::unique_ptr<const art::DexFile> dex_file_; |
| 93 | std::string* error_msg_; |
| 94 | char* class_sig_; |
| 95 | |
| 96 | // TODO Maybe change jclass to a mirror::Class |
| 97 | Redefiner(art::Runtime* runtime, |
| 98 | art::Thread* self, |
| 99 | jclass klass, |
| 100 | char* class_sig, |
| 101 | std::unique_ptr<const art::DexFile>& redefined_dex_file, |
| 102 | std::string* error_msg) |
| 103 | : result_(ERR(INTERNAL)), |
| 104 | runtime_(runtime), |
| 105 | self_(self), |
| 106 | klass_(klass), |
| 107 | dex_file_(std::move(redefined_dex_file)), |
| 108 | error_msg_(error_msg), |
| 109 | class_sig_(class_sig) { } |
| 110 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 111 | static jvmtiError GetClassRedefinitionError(art::Handle<art::mirror::Class> klass, |
| 112 | /*out*/std::string* error_msg) |
| 113 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 114 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 115 | static std::unique_ptr<art::MemMap> MoveDataToMemMap(const std::string& original_location, |
| 116 | jint data_len, |
| 117 | unsigned char* dex_data, |
| 118 | std::string* error_msg); |
| 119 | |
| 120 | // TODO Put on all the lock qualifiers. |
| 121 | jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 122 | |
| 123 | bool FinishRemainingAllocations( |
| 124 | /*out*/art::MutableHandle<art::mirror::ClassLoader>* source_class_loader, |
| 125 | /*out*/art::MutableHandle<art::mirror::Object>* source_dex_file_obj, |
| 126 | /*out*/art::MutableHandle<art::mirror::LongArray>* new_dex_file_cookie, |
| 127 | /*out*/art::MutableHandle<art::mirror::DexCache>* new_dex_cache) |
| 128 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 129 | |
| 130 | // Preallocates all needed allocations in klass so that we can pause execution safely. |
| 131 | // TODO We should be able to free the arrays if they end up not being used. Investigate doing this |
| 132 | // in the future. For now we will just take the memory hit. |
| 133 | bool EnsureClassAllocationsFinished() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 134 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 135 | // Ensure that obsolete methods are deoptimized. This is needed since optimized methods may have |
| 136 | // pointers to their ArtMethods stashed in registers that they then use to attempt to hit the |
| 137 | // DexCache. |
| 138 | void EnsureObsoleteMethodsAreDeoptimized() |
| 139 | REQUIRES(art::Locks::mutator_lock_) |
| 140 | REQUIRES(!art::Locks::thread_list_lock_, |
| 141 | !art::Locks::classlinker_classes_lock_); |
| 142 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 143 | art::mirror::ClassLoader* GetClassLoader() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 144 | |
| 145 | // This finds the java.lang.DexFile we will add the native DexFile to as part of the classpath. |
| 146 | // TODO Make sure the DexFile object returned is the one that the klass_ actually comes from. |
| 147 | art::mirror::Object* FindSourceDexFileObject(art::Handle<art::mirror::ClassLoader> loader) |
| 148 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 149 | |
| 150 | art::mirror::Class* GetMirrorClass() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 151 | |
| 152 | // Allocates and fills the new DexFileCookie |
| 153 | art::mirror::LongArray* AllocateDexFileCookie(art::Handle<art::mirror::Object> java_dex_file_obj) |
| 154 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 155 | |
| 156 | art::mirror::DexCache* CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader) |
| 157 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 158 | |
| 159 | void RecordFailure(jvmtiError result, const std::string& error_msg); |
| 160 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 161 | // This will check that no constraints are violated (more than 1 class in dex file, any changes in |
| 162 | // number/declaration of methods & fields, changes in access flags, etc.) |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 163 | bool CheckRedefinitionIsValid() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 164 | |
| 165 | // Checks that the class can even be redefined. |
| 166 | bool CheckRedefinable() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 167 | |
| 168 | // Checks that the dex file does not add/remove methods. |
| 169 | bool CheckSameMethods() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 170 | LOG(WARNING) << "methods are not checked for modification currently"; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 174 | // Checks that the dex file does not modify fields |
| 175 | bool CheckSameFields() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 176 | LOG(WARNING) << "Fields are not checked for modification currently"; |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | // Checks that the dex file contains only the single expected class and that the top-level class |
| 181 | // data has not been modified in an incompatible manner. |
| 182 | bool CheckClass() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 183 | |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame^] | 184 | void UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file, |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 185 | art::ObjPtr<art::mirror::LongArray> new_cookie, |
| 186 | /*out*/art::ObjPtr<art::mirror::LongArray>* original_cookie) |
| 187 | REQUIRES(art::Locks::mutator_lock_); |
| 188 | |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame^] | 189 | void UpdateFields(art::ObjPtr<art::mirror::Class> mclass) |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 190 | REQUIRES(art::Locks::mutator_lock_); |
| 191 | |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame^] | 192 | void UpdateMethods(art::ObjPtr<art::mirror::Class> mclass, |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 193 | art::ObjPtr<art::mirror::DexCache> new_dex_cache, |
| 194 | const art::DexFile::ClassDef& class_def) |
| 195 | REQUIRES(art::Locks::mutator_lock_); |
| 196 | |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame^] | 197 | void UpdateClass(art::ObjPtr<art::mirror::Class> mclass, |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 198 | art::ObjPtr<art::mirror::DexCache> new_dex_cache) |
| 199 | REQUIRES(art::Locks::mutator_lock_); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 200 | |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame^] | 201 | void FindAndAllocateObsoleteMethods(art::mirror::Class* art_klass) |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 202 | REQUIRES(art::Locks::mutator_lock_); |
| 203 | |
| 204 | void FillObsoleteMethodMap(art::mirror::Class* art_klass, |
| 205 | const std::unordered_map<art::ArtMethod*, art::ArtMethod*>& obsoletes) |
| 206 | REQUIRES(art::Locks::mutator_lock_); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | } // namespace openjdkjvmti |
| 210 | |
| 211 | #endif // ART_RUNTIME_OPENJDKJVMTI_TI_REDEFINE_H_ |