Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [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 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 32 | #include <unordered_map> |
| 33 | #include <unordered_set> |
| 34 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 35 | #include "transform.h" |
| 36 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 37 | #include "art_method.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 38 | #include "class_linker.h" |
| 39 | #include "dex_file.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 40 | #include "dex_file_types.h" |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 41 | #include "events-inl.h" |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 42 | #include "fixed_up_dex_file.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 43 | #include "gc_root-inl.h" |
| 44 | #include "globals.h" |
| 45 | #include "jni_env_ext-inl.h" |
Alex Light | 6a65631 | 2017-03-29 17:18:00 -0700 | [diff] [blame^] | 46 | #include "jvalue.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 47 | #include "jvmti.h" |
| 48 | #include "linear_alloc.h" |
| 49 | #include "mem_map.h" |
| 50 | #include "mirror/array.h" |
| 51 | #include "mirror/class-inl.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 52 | #include "mirror/class_ext.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 53 | #include "mirror/class_loader-inl.h" |
| 54 | #include "mirror/string-inl.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 55 | #include "oat_file.h" |
Alex Light | 6a65631 | 2017-03-29 17:18:00 -0700 | [diff] [blame^] | 56 | #include "reflection.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 57 | #include "scoped_thread_state_change-inl.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 58 | #include "stack.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 59 | #include "thread_list.h" |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 60 | #include "ti_redefine.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 61 | #include "transform.h" |
| 62 | #include "utf.h" |
| 63 | #include "utils/dex_cache_arrays_layout-inl.h" |
| 64 | |
| 65 | namespace openjdkjvmti { |
| 66 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 67 | jvmtiError Transformer::RetransformClassesDirect( |
| 68 | ArtJvmTiEnv* env, |
Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 69 | EventHandler* event_handler, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 70 | art::Thread* self, |
| 71 | /*in-out*/std::vector<ArtClassDefinition>* definitions) { |
| 72 | for (ArtClassDefinition& def : *definitions) { |
| 73 | jint new_len = -1; |
| 74 | unsigned char* new_data = nullptr; |
Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 75 | event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>( |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 76 | self, |
| 77 | GetJniEnv(env), |
| 78 | def.klass, |
| 79 | def.loader, |
| 80 | def.name.c_str(), |
| 81 | def.protection_domain, |
| 82 | def.dex_len, |
| 83 | static_cast<const unsigned char*>(def.dex_data.get()), |
| 84 | &new_len, |
| 85 | &new_data); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 86 | def.SetNewDexData(env, new_len, new_data); |
| 87 | } |
| 88 | return OK; |
| 89 | } |
| 90 | |
| 91 | jvmtiError Transformer::RetransformClasses(ArtJvmTiEnv* env, |
Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 92 | EventHandler* event_handler, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 93 | art::Runtime* runtime, |
| 94 | art::Thread* self, |
| 95 | jint class_count, |
| 96 | const jclass* classes, |
| 97 | /*out*/std::string* error_msg) { |
| 98 | if (env == nullptr) { |
| 99 | *error_msg = "env was null!"; |
| 100 | return ERR(INVALID_ENVIRONMENT); |
| 101 | } else if (class_count < 0) { |
| 102 | *error_msg = "class_count was less then 0"; |
| 103 | return ERR(ILLEGAL_ARGUMENT); |
| 104 | } else if (class_count == 0) { |
| 105 | // We don't actually need to do anything. Just return OK. |
| 106 | return OK; |
| 107 | } else if (classes == nullptr) { |
| 108 | *error_msg = "null classes!"; |
| 109 | return ERR(NULL_POINTER); |
| 110 | } |
| 111 | // A holder that will Deallocate all the class bytes buffers on destruction. |
| 112 | std::vector<ArtClassDefinition> definitions; |
| 113 | jvmtiError res = OK; |
| 114 | for (jint i = 0; i < class_count; i++) { |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 115 | jboolean is_modifiable = JNI_FALSE; |
| 116 | res = env->IsModifiableClass(classes[i], &is_modifiable); |
| 117 | if (res != OK) { |
| 118 | return res; |
| 119 | } else if (!is_modifiable) { |
| 120 | return ERR(UNMODIFIABLE_CLASS); |
| 121 | } |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 122 | ArtClassDefinition def; |
| 123 | res = FillInTransformationData(env, classes[i], &def); |
| 124 | if (res != OK) { |
| 125 | return res; |
| 126 | } |
| 127 | definitions.push_back(std::move(def)); |
| 128 | } |
Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 129 | res = RetransformClassesDirect(env, event_handler, self, &definitions); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 130 | if (res != OK) { |
| 131 | return res; |
| 132 | } |
| 133 | return Redefiner::RedefineClassesDirect(env, runtime, self, definitions, error_msg); |
| 134 | } |
| 135 | |
| 136 | // TODO Move this somewhere else, ti_class? |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 137 | jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) { |
| 138 | JNIEnv* jni_env = nullptr; |
| 139 | jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1); |
| 140 | if (ret != JNI_OK) { |
| 141 | // TODO Different error might be better? |
| 142 | return ERR(INTERNAL); |
| 143 | } |
| 144 | art::ScopedObjectAccess soa(jni_env); |
| 145 | art::StackHandleScope<1> hs(art::Thread::Current()); |
| 146 | art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass))); |
| 147 | const art::DexFile& dex = hs_klass->GetDexFile(); |
| 148 | *location = dex.GetLocation(); |
| 149 | return OK; |
| 150 | } |
| 151 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 152 | jvmtiError Transformer::GetDexDataForRetransformation(ArtJvmTiEnv* env, |
| 153 | art::Handle<art::mirror::Class> klass, |
| 154 | /*out*/jint* dex_data_len, |
| 155 | /*out*/unsigned char** dex_data) { |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 156 | art::StackHandleScope<3> hs(art::Thread::Current()); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 157 | art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->GetExtData())); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 158 | const art::DexFile* dex_file = nullptr; |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 159 | if (!ext.IsNull()) { |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 160 | art::Handle<art::mirror::Object> orig_dex(hs.NewHandle(ext->GetOriginalDexFile())); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 161 | if (!orig_dex.IsNull()) { |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 162 | if (orig_dex->IsArrayInstance()) { |
| 163 | DCHECK(orig_dex->GetClass()->GetComponentType()->IsPrimitiveByte()); |
| 164 | art::Handle<art::mirror::ByteArray> orig_dex_bytes( |
| 165 | hs.NewHandle(art::down_cast<art::mirror::ByteArray*>(orig_dex->AsArray()))); |
| 166 | *dex_data_len = static_cast<jint>(orig_dex_bytes->GetLength()); |
| 167 | return CopyDataIntoJvmtiBuffer( |
| 168 | env, |
| 169 | reinterpret_cast<const unsigned char*>(orig_dex_bytes->GetData()), |
| 170 | *dex_data_len, |
| 171 | /*out*/dex_data); |
Alex Light | 6a65631 | 2017-03-29 17:18:00 -0700 | [diff] [blame^] | 172 | } else if (orig_dex->IsDexCache()) { |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 173 | dex_file = orig_dex->AsDexCache()->GetDexFile(); |
Alex Light | 6a65631 | 2017-03-29 17:18:00 -0700 | [diff] [blame^] | 174 | } else { |
| 175 | DCHECK_EQ(orig_dex->GetClass()->GetPrimitiveType(), art::Primitive::kPrimLong); |
| 176 | art::ObjPtr<art::mirror::Class> prim_long_class( |
| 177 | art::Runtime::Current()->GetClassLinker()->GetClassRoot( |
| 178 | art::ClassLinker::kPrimitiveLong)); |
| 179 | art::JValue val; |
| 180 | if (!art::UnboxPrimitiveForResult(orig_dex.Get(), prim_long_class, &val)) { |
| 181 | // This should never happen. |
| 182 | return ERR(INTERNAL); |
| 183 | } |
| 184 | dex_file = reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ())); |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 185 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 188 | if (dex_file == nullptr) { |
| 189 | dex_file = &klass->GetDexFile(); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 190 | } |
| 191 | std::unique_ptr<FixedUpDexFile> fixed_dex_file(FixedUpDexFile::Create(*dex_file)); |
Alex Light | 6a65631 | 2017-03-29 17:18:00 -0700 | [diff] [blame^] | 192 | *dex_data_len = static_cast<jint>(fixed_dex_file->Size()); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 193 | return CopyDataIntoJvmtiBuffer(env, |
| 194 | fixed_dex_file->Begin(), |
| 195 | fixed_dex_file->Size(), |
| 196 | /*out*/dex_data); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // TODO Move this function somewhere more appropriate. |
| 200 | // Gets the data surrounding the given class. |
| 201 | // TODO Make this less magical. |
| 202 | jvmtiError Transformer::FillInTransformationData(ArtJvmTiEnv* env, |
| 203 | jclass klass, |
| 204 | ArtClassDefinition* def) { |
| 205 | JNIEnv* jni_env = GetJniEnv(env); |
| 206 | if (jni_env == nullptr) { |
| 207 | // TODO Different error might be better? |
| 208 | return ERR(INTERNAL); |
| 209 | } |
| 210 | art::ScopedObjectAccess soa(jni_env); |
| 211 | art::StackHandleScope<3> hs(art::Thread::Current()); |
| 212 | art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass))); |
| 213 | if (hs_klass.IsNull()) { |
| 214 | return ERR(INVALID_CLASS); |
| 215 | } |
| 216 | def->klass = klass; |
| 217 | def->loader = soa.AddLocalReference<jobject>(hs_klass->GetClassLoader()); |
Alex Light | 2802712 | 2017-01-26 17:21:51 -0800 | [diff] [blame] | 218 | std::string descriptor_store; |
| 219 | std::string descriptor(hs_klass->GetDescriptor(&descriptor_store)); |
| 220 | def->name = descriptor.substr(1, descriptor.size() - 2); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 221 | // TODO is this always null? |
| 222 | def->protection_domain = nullptr; |
| 223 | if (def->dex_data.get() == nullptr) { |
| 224 | unsigned char* new_data; |
| 225 | jvmtiError res = GetDexDataForRetransformation(env, hs_klass, &def->dex_len, &new_data); |
| 226 | if (res == OK) { |
| 227 | def->dex_data = MakeJvmtiUniquePtr(env, new_data); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 228 | // TODO This whole thing is a bit of a mess. |
| 229 | // We need to keep track of what the runtime should think an unmodified dex file is since |
| 230 | // we need to be able to tell if anything changes. This might be different then the currently |
| 231 | // loaded dex file since we need to un-quicken stuff. |
| 232 | if (hs_klass->GetExtData() == nullptr || |
| 233 | hs_klass->GetExtData()->GetOriginalDexFile() == nullptr) { |
| 234 | // We have never redefined this yet. Keep track of what the (de-quickened) dex file looks |
| 235 | // like so we can tell if anything has changed. |
| 236 | // Really we would like to just always do the 'else' block but the fact that we de-quickened |
| 237 | // stuff screws us over. |
| 238 | unsigned char* original_data_memory = nullptr; |
| 239 | res = env->Allocate(def->dex_len, &original_data_memory); |
| 240 | if (res != OK) { |
| 241 | return res; |
| 242 | } |
| 243 | memcpy(original_data_memory, new_data, def->dex_len); |
| 244 | def->original_dex_file_memory = MakeJvmtiUniquePtr(env, original_data_memory); |
| 245 | def->original_dex_file = art::ArraySlice<const unsigned char>(original_data_memory, |
| 246 | def->dex_len); |
| 247 | } else { |
| 248 | // We know that we have been redefined at least once (there is an original_dex_file set in |
| 249 | // the class) so we can just use the current dex file directly. |
| 250 | def->original_dex_file = art::ArraySlice<const unsigned char>( |
| 251 | hs_klass->GetDexFile().Begin(), hs_klass->GetDexFile().Size()); |
| 252 | } |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 253 | } else { |
| 254 | return res; |
| 255 | } |
| 256 | } |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 257 | return OK; |
| 258 | } |
| 259 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 260 | } // namespace openjdkjvmti |