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 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 66 | class RedefinitionDataHolder; |
| 67 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 68 | // Class that can redefine a single class's methods. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 69 | // TODO We should really make this be driven by an outside class so we can do multiple classes at |
| 70 | // the same time and have less required cleanup. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 71 | class Redefiner { |
| 72 | public: |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 73 | // Redefine the given classes with the given dex data. Note this function does not take ownership |
| 74 | // of the dex_data pointers. It is not used after this call however and may be freed if desired. |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame^] | 75 | // The caller is responsible for freeing it. The runtime makes its own copy of the data. This |
| 76 | // function does not call the transformation events. |
| 77 | // TODO Check modified flag of the definitions. |
| 78 | static jvmtiError RedefineClassesDirect(ArtJvmTiEnv* env, |
| 79 | art::Runtime* runtime, |
| 80 | art::Thread* self, |
| 81 | const std::vector<ArtClassDefinition>& definitions, |
| 82 | /*out*/std::string* error_msg); |
| 83 | |
| 84 | // Redefine the given classes with the given dex data. Note this function does not take ownership |
| 85 | // of the dex_data pointers. 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] | 86 | // The caller is responsible for freeing it. The runtime makes its own copy of the data. |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame^] | 87 | // TODO This function should call the transformation events. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 88 | static jvmtiError RedefineClasses(ArtJvmTiEnv* env, |
| 89 | art::Runtime* runtime, |
| 90 | art::Thread* self, |
| 91 | jint class_count, |
| 92 | const jvmtiClassDefinition* definitions, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame^] | 93 | /*out*/std::string* error_msg); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 94 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 95 | static jvmtiError IsModifiableClass(jvmtiEnv* env, jclass klass, jboolean* is_redefinable); |
| 96 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 97 | private: |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 98 | class ClassRedefinition { |
| 99 | public: |
| 100 | ClassRedefinition(Redefiner* driver, |
| 101 | jclass klass, |
| 102 | const art::DexFile* redefined_dex_file, |
| 103 | const char* class_sig) |
| 104 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 105 | |
| 106 | // NO_THREAD_SAFETY_ANALYSIS so we can unlock the class in the destructor. |
| 107 | ~ClassRedefinition() NO_THREAD_SAFETY_ANALYSIS; |
| 108 | |
| 109 | // Move constructor so we can put these into a vector. |
| 110 | ClassRedefinition(ClassRedefinition&& other) |
| 111 | : driver_(other.driver_), |
| 112 | klass_(other.klass_), |
| 113 | dex_file_(std::move(other.dex_file_)), |
| 114 | class_sig_(std::move(other.class_sig_)) { |
| 115 | other.driver_ = nullptr; |
| 116 | } |
| 117 | |
| 118 | art::mirror::Class* GetMirrorClass() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 119 | art::mirror::ClassLoader* GetClassLoader() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 120 | |
| 121 | // This finds the java.lang.DexFile we will add the native DexFile to as part of the classpath. |
| 122 | // TODO Make sure the DexFile object returned is the one that the klass_ actually comes from. |
| 123 | art::mirror::Object* FindSourceDexFileObject(art::Handle<art::mirror::ClassLoader> loader) |
| 124 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 125 | |
| 126 | art::mirror::DexCache* CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader) |
| 127 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 128 | |
| 129 | // Allocates and fills the new DexFileCookie |
| 130 | art::mirror::LongArray* AllocateDexFileCookie(art::Handle<art::mirror::Object> j_dex_file_obj) |
| 131 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 132 | |
| 133 | void RecordFailure(jvmtiError e, const std::string& err) { |
| 134 | driver_->RecordFailure(e, class_sig_, err); |
| 135 | } |
| 136 | |
| 137 | bool FinishRemainingAllocations( |
| 138 | /*out*/art::MutableHandle<art::mirror::ClassLoader>* source_class_loader, |
| 139 | /*out*/art::MutableHandle<art::mirror::Object>* source_dex_file_obj, |
| 140 | /*out*/art::MutableHandle<art::mirror::LongArray>* new_dex_file_cookie, |
| 141 | /*out*/art::MutableHandle<art::mirror::DexCache>* new_dex_cache) |
| 142 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 143 | |
| 144 | void FindAndAllocateObsoleteMethods(art::mirror::Class* art_klass) |
| 145 | REQUIRES(art::Locks::mutator_lock_); |
| 146 | |
| 147 | void FillObsoleteMethodMap( |
| 148 | art::mirror::Class* art_klass, |
| 149 | const std::unordered_map<art::ArtMethod*, art::ArtMethod*>& obsoletes) |
| 150 | REQUIRES(art::Locks::mutator_lock_); |
| 151 | |
| 152 | |
| 153 | // Checks that the dex file contains only the single expected class and that the top-level class |
| 154 | // data has not been modified in an incompatible manner. |
| 155 | bool CheckClass() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 156 | |
| 157 | // Preallocates all needed allocations in klass so that we can pause execution safely. |
| 158 | // TODO We should be able to free the arrays if they end up not being used. Investigate doing |
| 159 | // this in the future. For now we will just take the memory hit. |
| 160 | bool EnsureClassAllocationsFinished() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 161 | |
| 162 | // This will check that no constraints are violated (more than 1 class in dex file, any changes |
| 163 | // in number/declaration of methods & fields, changes in access flags, etc.) |
| 164 | bool CheckRedefinitionIsValid() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 165 | |
| 166 | // Checks that the class can even be redefined. |
| 167 | bool CheckRedefinable() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 168 | |
| 169 | // Checks that the dex file does not add/remove methods. |
| 170 | bool CheckSameMethods() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 171 | LOG(WARNING) << "methods are not checked for modification currently"; |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | // Checks that the dex file does not modify fields |
| 176 | bool CheckSameFields() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 177 | LOG(WARNING) << "Fields are not checked for modification currently"; |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | void UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file, |
| 182 | art::ObjPtr<art::mirror::LongArray> new_cookie) |
| 183 | REQUIRES(art::Locks::mutator_lock_); |
| 184 | |
| 185 | void UpdateFields(art::ObjPtr<art::mirror::Class> mclass) |
| 186 | REQUIRES(art::Locks::mutator_lock_); |
| 187 | |
| 188 | void UpdateMethods(art::ObjPtr<art::mirror::Class> mclass, |
| 189 | art::ObjPtr<art::mirror::DexCache> new_dex_cache, |
| 190 | const art::DexFile::ClassDef& class_def) |
| 191 | REQUIRES(art::Locks::mutator_lock_); |
| 192 | |
| 193 | void UpdateClass(art::ObjPtr<art::mirror::Class> mclass, |
| 194 | art::ObjPtr<art::mirror::DexCache> new_dex_cache) |
| 195 | REQUIRES(art::Locks::mutator_lock_); |
| 196 | |
| 197 | void ReleaseDexFile() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 198 | |
| 199 | private: |
| 200 | Redefiner* driver_; |
| 201 | jclass klass_; |
| 202 | std::unique_ptr<const art::DexFile> dex_file_; |
| 203 | std::string class_sig_; |
| 204 | }; |
| 205 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 206 | jvmtiError result_; |
| 207 | art::Runtime* runtime_; |
| 208 | art::Thread* self_; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 209 | std::vector<ClassRedefinition> redefinitions_; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 210 | // Kept as a jclass since we have weird run-state changes that make keeping it around as a |
| 211 | // mirror::Class difficult and confusing. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 212 | std::string* error_msg_; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 213 | |
| 214 | // TODO Maybe change jclass to a mirror::Class |
| 215 | Redefiner(art::Runtime* runtime, |
| 216 | art::Thread* self, |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 217 | std::string* error_msg) |
| 218 | : result_(ERR(INTERNAL)), |
| 219 | runtime_(runtime), |
| 220 | self_(self), |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 221 | redefinitions_(), |
| 222 | error_msg_(error_msg) { } |
| 223 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame^] | 224 | jvmtiError AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 225 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 226 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 227 | static jvmtiError GetClassRedefinitionError(art::Handle<art::mirror::Class> klass, |
| 228 | /*out*/std::string* error_msg) |
| 229 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 230 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 231 | static std::unique_ptr<art::MemMap> MoveDataToMemMap(const std::string& original_location, |
| 232 | jint data_len, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 233 | const unsigned char* dex_data, |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 234 | std::string* error_msg); |
| 235 | |
| 236 | // TODO Put on all the lock qualifiers. |
| 237 | jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 238 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 239 | bool CheckAllRedefinitionAreValid() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 240 | bool EnsureAllClassAllocationsFinished() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 241 | bool FinishAllRemainingAllocations(RedefinitionDataHolder& holder) |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 242 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 243 | void ReleaseAllDexFiles() REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 244 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 245 | // Ensure that obsolete methods are deoptimized. This is needed since optimized methods may have |
| 246 | // pointers to their ArtMethods stashed in registers that they then use to attempt to hit the |
| 247 | // DexCache. |
| 248 | void EnsureObsoleteMethodsAreDeoptimized() |
| 249 | REQUIRES(art::Locks::mutator_lock_) |
| 250 | REQUIRES(!art::Locks::thread_list_lock_, |
| 251 | !art::Locks::classlinker_classes_lock_); |
| 252 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 253 | void RecordFailure(jvmtiError result, const std::string& class_sig, const std::string& error_msg); |
| 254 | void RecordFailure(jvmtiError result, const std::string& error_msg) { |
| 255 | RecordFailure(result, "NO CLASS", error_msg); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 258 | friend struct CallbackCtx; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | } // namespace openjdkjvmti |
| 262 | |
| 263 | #endif // ART_RUNTIME_OPENJDKJVMTI_TI_REDEFINE_H_ |