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 | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 32 | #include <stddef.h> |
| 33 | #include <sys/types.h> |
| 34 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 35 | #include <unordered_map> |
| 36 | #include <unordered_set> |
| 37 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 38 | #include "transform.h" |
| 39 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 40 | #include "art_method.h" |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 41 | #include "base/array_ref.h" |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 42 | #include "base/globals.h" |
Andreas Gampe | 85f1c57 | 2018-11-21 13:52:48 -0800 | [diff] [blame] | 43 | #include "base/logging.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 44 | #include "base/mem_map.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 45 | #include "class_linker.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 46 | #include "dex/dex_file.h" |
| 47 | #include "dex/dex_file_types.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 48 | #include "dex/utf.h" |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 49 | #include "events-inl.h" |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 50 | #include "events.h" |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 51 | #include "fault_handler.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 52 | #include "gc_root-inl.h" |
Andreas Gampe | e15b9b1 | 2018-10-29 12:54:27 -0700 | [diff] [blame] | 53 | #include "handle_scope-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 54 | #include "jni/jni_env_ext-inl.h" |
Alex Light | 6a65631 | 2017-03-29 17:18:00 -0700 | [diff] [blame] | 55 | #include "jvalue.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 56 | #include "jvmti.h" |
| 57 | #include "linear_alloc.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 58 | #include "mirror/array.h" |
| 59 | #include "mirror/class-inl.h" |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 60 | #include "mirror/class_ext.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 61 | #include "mirror/class_loader-inl.h" |
| 62 | #include "mirror/string-inl.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 63 | #include "oat_file.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 64 | #include "scoped_thread_state_change-inl.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 65 | #include "stack.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 66 | #include "thread_list.h" |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 67 | #include "ti_redefine.h" |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 68 | #include "ti_logging.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 69 | #include "transform.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 70 | |
| 71 | namespace openjdkjvmti { |
| 72 | |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 73 | static EventHandler* gEventHandler = nullptr; |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 74 | |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 75 | void Transformer::Register(EventHandler* eh) { |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 76 | gEventHandler = eh; |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 79 | // Initialize templates. |
| 80 | template |
| 81 | void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>( |
| 82 | EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def); |
| 83 | template |
| 84 | void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>( |
| 85 | EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def); |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 86 | template |
| 87 | void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kStructuralDexFileLoadHook>( |
| 88 | EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def); |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 89 | |
| 90 | template<ArtJvmtiEvent kEvent> |
| 91 | void Transformer::TransformSingleClassDirect(EventHandler* event_handler, |
| 92 | art::Thread* self, |
| 93 | /*in-out*/ArtClassDefinition* def) { |
| 94 | static_assert(kEvent == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable || |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 95 | kEvent == ArtJvmtiEvent::kClassFileLoadHookRetransformable || |
| 96 | kEvent == ArtJvmtiEvent::kStructuralDexFileLoadHook, |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 97 | "bad event type"); |
Alex Light | fe2a39d | 2018-02-05 11:08:08 -0800 | [diff] [blame] | 98 | // We don't want to do transitions between calling the event and setting the new data so change to |
Nicolas Geoffray | c858e56 | 2022-02-23 10:50:31 +0000 | [diff] [blame] | 99 | // native state early. |
Alex Light | fe2a39d | 2018-02-05 11:08:08 -0800 | [diff] [blame] | 100 | art::ScopedThreadStateChange stsc(self, art::ThreadState::kNative); |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 101 | jint new_len = -1; |
| 102 | unsigned char* new_data = nullptr; |
| 103 | art::ArrayRef<const unsigned char> dex_data = def->GetDexData(); |
| 104 | event_handler->DispatchEvent<kEvent>( |
| 105 | self, |
| 106 | static_cast<JNIEnv*>(self->GetJniEnv()), |
| 107 | def->GetClass(), |
| 108 | def->GetLoader(), |
| 109 | def->GetName().c_str(), |
| 110 | def->GetProtectionDomain(), |
| 111 | static_cast<jint>(dex_data.size()), |
| 112 | dex_data.data(), |
| 113 | /*out*/&new_len, |
| 114 | /*out*/&new_data); |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 115 | def->SetNewDexData(new_len, new_data, kEvent); |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 118 | template <RedefinitionType kType> |
Alex Light | 15ffafd | 2019-10-17 13:58:01 -0700 | [diff] [blame] | 119 | void Transformer::RetransformClassesDirect( |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 120 | art::Thread* self, |
| 121 | /*in-out*/ std::vector<ArtClassDefinition>* definitions) { |
| 122 | constexpr ArtJvmtiEvent kEvent = kType == RedefinitionType::kNormal |
| 123 | ? ArtJvmtiEvent::kClassFileLoadHookRetransformable |
| 124 | : ArtJvmtiEvent::kStructuralDexFileLoadHook; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 125 | for (ArtClassDefinition& def : *definitions) { |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 126 | TransformSingleClassDirect<kEvent>(gEventHandler, self, &def); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 127 | } |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 130 | template void Transformer::RetransformClassesDirect<RedefinitionType::kNormal>( |
| 131 | art::Thread* self, /*in-out*/std::vector<ArtClassDefinition>* definitions); |
| 132 | template void Transformer::RetransformClassesDirect<RedefinitionType::kStructural>( |
| 133 | art::Thread* self, /*in-out*/std::vector<ArtClassDefinition>* definitions); |
| 134 | |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 135 | jvmtiError Transformer::RetransformClasses(jvmtiEnv* env, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 136 | jint class_count, |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 137 | const jclass* classes) { |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 138 | if (class_count < 0) { |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 139 | JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM class_count was less then 0"; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 140 | return ERR(ILLEGAL_ARGUMENT); |
| 141 | } else if (class_count == 0) { |
| 142 | // We don't actually need to do anything. Just return OK. |
| 143 | return OK; |
| 144 | } else if (classes == nullptr) { |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 145 | JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM null classes!"; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 146 | return ERR(NULL_POINTER); |
| 147 | } |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 148 | art::Thread* self = art::Thread::Current(); |
| 149 | art::Runtime* runtime = art::Runtime::Current(); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 150 | // A holder that will Deallocate all the class bytes buffers on destruction. |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 151 | std::string error_msg; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 152 | std::vector<ArtClassDefinition> definitions; |
| 153 | jvmtiError res = OK; |
| 154 | for (jint i = 0; i < class_count; i++) { |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 155 | res = Redefiner::GetClassRedefinitionError<RedefinitionType::kNormal>(classes[i], &error_msg); |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 156 | if (res != OK) { |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 157 | JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg; |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 158 | return res; |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 159 | } |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 160 | ArtClassDefinition def; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 161 | res = def.Init(self, classes[i]); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 162 | if (res != OK) { |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 163 | JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM definition init failed"; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 164 | return res; |
| 165 | } |
| 166 | definitions.push_back(std::move(def)); |
| 167 | } |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 168 | RetransformClassesDirect<RedefinitionType::kStructural>(self, &definitions); |
| 169 | RetransformClassesDirect<RedefinitionType::kNormal>(self, &definitions); |
| 170 | RedefinitionType redef_type = |
| 171 | std::any_of(definitions.cbegin(), |
| 172 | definitions.cend(), |
| 173 | [](const auto& it) { return it.HasStructuralChanges(); }) |
| 174 | ? RedefinitionType::kStructural |
| 175 | : RedefinitionType::kNormal; |
| 176 | res = Redefiner::RedefineClassesDirect( |
| 177 | ArtJvmTiEnv::AsArtJvmTiEnv(env), runtime, self, definitions, redef_type, &error_msg); |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 178 | if (res != OK) { |
| 179 | JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg; |
| 180 | } |
| 181 | return res; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // TODO Move this somewhere else, ti_class? |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 185 | jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) { |
| 186 | JNIEnv* jni_env = nullptr; |
| 187 | jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1); |
| 188 | if (ret != JNI_OK) { |
| 189 | // TODO Different error might be better? |
| 190 | return ERR(INTERNAL); |
| 191 | } |
| 192 | art::ScopedObjectAccess soa(jni_env); |
| 193 | art::StackHandleScope<1> hs(art::Thread::Current()); |
| 194 | art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass))); |
| 195 | const art::DexFile& dex = hs_klass->GetDexFile(); |
| 196 | *location = dex.GetLocation(); |
| 197 | return OK; |
| 198 | } |
| 199 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 200 | } // namespace openjdkjvmti |