| 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" | 
| Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 38 | #include "base/array_ref.h" | 
| Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 39 | #include "class_linker.h" | 
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 40 | #include "dex/dex_file.h" | 
|  | 41 | #include "dex/dex_file_types.h" | 
| David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame^] | 42 | #include "dex/utf.h" | 
| Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 43 | #include "events-inl.h" | 
| Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 44 | #include "gc_root-inl.h" | 
|  | 45 | #include "globals.h" | 
|  | 46 | #include "jni_env_ext-inl.h" | 
| Alex Light | 6a65631 | 2017-03-29 17:18:00 -0700 | [diff] [blame] | 47 | #include "jvalue.h" | 
| Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 48 | #include "jvmti.h" | 
|  | 49 | #include "linear_alloc.h" | 
|  | 50 | #include "mem_map.h" | 
|  | 51 | #include "mirror/array.h" | 
|  | 52 | #include "mirror/class-inl.h" | 
| Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 53 | #include "mirror/class_ext.h" | 
| Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 54 | #include "mirror/class_loader-inl.h" | 
|  | 55 | #include "mirror/string-inl.h" | 
| Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 56 | #include "oat_file.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" | 
| Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 62 | #include "utils/dex_cache_arrays_layout-inl.h" | 
|  | 63 |  | 
|  | 64 | namespace openjdkjvmti { | 
|  | 65 |  | 
| Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 66 | // Initialize templates. | 
|  | 67 | template | 
|  | 68 | void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>( | 
|  | 69 | EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def); | 
|  | 70 | template | 
|  | 71 | void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>( | 
|  | 72 | EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def); | 
|  | 73 |  | 
|  | 74 | template<ArtJvmtiEvent kEvent> | 
|  | 75 | void Transformer::TransformSingleClassDirect(EventHandler* event_handler, | 
|  | 76 | art::Thread* self, | 
|  | 77 | /*in-out*/ArtClassDefinition* def) { | 
|  | 78 | static_assert(kEvent == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable || | 
|  | 79 | kEvent == ArtJvmtiEvent::kClassFileLoadHookRetransformable, | 
|  | 80 | "bad event type"); | 
|  | 81 | jint new_len = -1; | 
|  | 82 | unsigned char* new_data = nullptr; | 
|  | 83 | art::ArrayRef<const unsigned char> dex_data = def->GetDexData(); | 
|  | 84 | event_handler->DispatchEvent<kEvent>( | 
|  | 85 | self, | 
|  | 86 | static_cast<JNIEnv*>(self->GetJniEnv()), | 
|  | 87 | def->GetClass(), | 
|  | 88 | def->GetLoader(), | 
|  | 89 | def->GetName().c_str(), | 
|  | 90 | def->GetProtectionDomain(), | 
|  | 91 | static_cast<jint>(dex_data.size()), | 
|  | 92 | dex_data.data(), | 
|  | 93 | /*out*/&new_len, | 
|  | 94 | /*out*/&new_data); | 
|  | 95 | def->SetNewDexData(new_len, new_data); | 
|  | 96 | } | 
|  | 97 |  | 
| Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 98 | jvmtiError Transformer::RetransformClassesDirect( | 
| Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 99 | EventHandler* event_handler, | 
| Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 100 | art::Thread* self, | 
|  | 101 | /*in-out*/std::vector<ArtClassDefinition>* definitions) { | 
|  | 102 | for (ArtClassDefinition& def : *definitions) { | 
| Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 103 | TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(event_handler, | 
|  | 104 | self, | 
|  | 105 | &def); | 
| Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 106 | } | 
|  | 107 | return OK; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | jvmtiError Transformer::RetransformClasses(ArtJvmTiEnv* env, | 
| Andreas Gampe | de19eb9 | 2017-02-24 16:21:18 -0800 | [diff] [blame] | 111 | EventHandler* event_handler, | 
| Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 112 | art::Runtime* runtime, | 
|  | 113 | art::Thread* self, | 
|  | 114 | jint class_count, | 
|  | 115 | const jclass* classes, | 
|  | 116 | /*out*/std::string* error_msg) { | 
|  | 117 | if (env == nullptr) { | 
|  | 118 | *error_msg = "env was null!"; | 
|  | 119 | return ERR(INVALID_ENVIRONMENT); | 
|  | 120 | } else if (class_count < 0) { | 
|  | 121 | *error_msg = "class_count was less then 0"; | 
|  | 122 | return ERR(ILLEGAL_ARGUMENT); | 
|  | 123 | } else if (class_count == 0) { | 
|  | 124 | // We don't actually need to do anything. Just return OK. | 
|  | 125 | return OK; | 
|  | 126 | } else if (classes == nullptr) { | 
|  | 127 | *error_msg = "null classes!"; | 
|  | 128 | return ERR(NULL_POINTER); | 
|  | 129 | } | 
|  | 130 | // A holder that will Deallocate all the class bytes buffers on destruction. | 
|  | 131 | std::vector<ArtClassDefinition> definitions; | 
|  | 132 | jvmtiError res = OK; | 
|  | 133 | for (jint i = 0; i < class_count; i++) { | 
| Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 134 | jboolean is_modifiable = JNI_FALSE; | 
|  | 135 | res = env->IsModifiableClass(classes[i], &is_modifiable); | 
|  | 136 | if (res != OK) { | 
|  | 137 | return res; | 
|  | 138 | } else if (!is_modifiable) { | 
|  | 139 | return ERR(UNMODIFIABLE_CLASS); | 
|  | 140 | } | 
| Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 141 | ArtClassDefinition def; | 
| Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 142 | res = def.Init(self, classes[i]); | 
| Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 143 | if (res != OK) { | 
|  | 144 | return res; | 
|  | 145 | } | 
|  | 146 | definitions.push_back(std::move(def)); | 
|  | 147 | } | 
| Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 148 | res = RetransformClassesDirect(event_handler, self, &definitions); | 
| Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 149 | if (res != OK) { | 
|  | 150 | return res; | 
|  | 151 | } | 
|  | 152 | return Redefiner::RedefineClassesDirect(env, runtime, self, definitions, error_msg); | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | // TODO Move this somewhere else, ti_class? | 
| Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 156 | jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) { | 
|  | 157 | JNIEnv* jni_env = nullptr; | 
|  | 158 | jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1); | 
|  | 159 | if (ret != JNI_OK) { | 
|  | 160 | // TODO Different error might be better? | 
|  | 161 | return ERR(INTERNAL); | 
|  | 162 | } | 
|  | 163 | art::ScopedObjectAccess soa(jni_env); | 
|  | 164 | art::StackHandleScope<1> hs(art::Thread::Current()); | 
|  | 165 | art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass))); | 
|  | 166 | const art::DexFile& dex = hs_klass->GetDexFile(); | 
|  | 167 | *location = dex.GetLocation(); | 
|  | 168 | return OK; | 
|  | 169 | } | 
|  | 170 |  | 
| Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 171 | }  // namespace openjdkjvmti |