blob: 1f36da435a695577d4a538f30db089fe15c00ba7 [file] [log] [blame]
Alex Light9c20a142016-08-23 15:05:12 -07001/* 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 Lightca97ada2018-02-02 09:25:31 -080032#include <stddef.h>
33#include <sys/types.h>
34
Alex Lighta01de592016-11-15 10:43:06 -080035#include <unordered_map>
36#include <unordered_set>
37
Alex Light9c20a142016-08-23 15:05:12 -070038#include "transform.h"
39
Alex Lighta01de592016-11-15 10:43:06 -080040#include "art_method.h"
Vladimir Markoe1993c72017-06-14 17:01:38 +010041#include "base/array_ref.h"
David Sehr1979c642018-04-26 14:41:18 -070042#include "base/globals.h"
Andreas Gampe85f1c572018-11-21 13:52:48 -080043#include "base/logging.h"
David Sehr79e26072018-04-06 17:58:50 -070044#include "base/mem_map.h"
Alex Light9c20a142016-08-23 15:05:12 -070045#include "class_linker.h"
David Sehr9e734c72018-01-04 17:56:19 -080046#include "dex/dex_file.h"
47#include "dex/dex_file_types.h"
David Sehr0225f8e2018-01-31 08:52:24 +000048#include "dex/utf.h"
Alex Light6ac57502017-01-19 15:05:06 -080049#include "events-inl.h"
Alex Light3732beb2019-10-04 13:35:34 -070050#include "events.h"
Alex Lightca97ada2018-02-02 09:25:31 -080051#include "fault_handler.h"
Alex Light9c20a142016-08-23 15:05:12 -070052#include "gc_root-inl.h"
Andreas Gampee15b9b12018-10-29 12:54:27 -070053#include "handle_scope-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010054#include "jni/jni_env_ext-inl.h"
Alex Light6a656312017-03-29 17:18:00 -070055#include "jvalue.h"
Alex Light9c20a142016-08-23 15:05:12 -070056#include "jvmti.h"
57#include "linear_alloc.h"
Alex Light9c20a142016-08-23 15:05:12 -070058#include "mirror/array.h"
59#include "mirror/class-inl.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080060#include "mirror/class_ext.h"
Alex Light9c20a142016-08-23 15:05:12 -070061#include "mirror/class_loader-inl.h"
62#include "mirror/string-inl.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010063#include "oat_file.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070064#include "scoped_thread_state_change-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080065#include "stack.h"
Alex Light9c20a142016-08-23 15:05:12 -070066#include "thread_list.h"
Alex Light6ac57502017-01-19 15:05:06 -080067#include "ti_redefine.h"
Alex Light3732beb2019-10-04 13:35:34 -070068#include "ti_logging.h"
Alex Light9c20a142016-08-23 15:05:12 -070069#include "transform.h"
Alex Light9c20a142016-08-23 15:05:12 -070070
71namespace openjdkjvmti {
72
Alex Light3732beb2019-10-04 13:35:34 -070073static EventHandler* gEventHandler = nullptr;
Alex Lightca97ada2018-02-02 09:25:31 -080074
Alex Light3732beb2019-10-04 13:35:34 -070075void Transformer::Register(EventHandler* eh) {
Alex Light3732beb2019-10-04 13:35:34 -070076 gEventHandler = eh;
Alex Lightca97ada2018-02-02 09:25:31 -080077}
78
Alex Light64e4c142018-01-30 13:46:37 -080079// Initialize templates.
80template
81void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
82 EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
83template
84void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
85 EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
Alex Lightd55b8442019-10-15 15:46:07 -070086template
87void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kStructuralDexFileLoadHook>(
88 EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
Alex Light64e4c142018-01-30 13:46:37 -080089
90template<ArtJvmtiEvent kEvent>
91void Transformer::TransformSingleClassDirect(EventHandler* event_handler,
92 art::Thread* self,
93 /*in-out*/ArtClassDefinition* def) {
94 static_assert(kEvent == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable ||
Alex Lightd55b8442019-10-15 15:46:07 -070095 kEvent == ArtJvmtiEvent::kClassFileLoadHookRetransformable ||
96 kEvent == ArtJvmtiEvent::kStructuralDexFileLoadHook,
Alex Light64e4c142018-01-30 13:46:37 -080097 "bad event type");
Alex Lightfe2a39d2018-02-05 11:08:08 -080098 // We don't want to do transitions between calling the event and setting the new data so change to
Nicolas Geoffrayc858e562022-02-23 10:50:31 +000099 // native state early.
Alex Lightfe2a39d2018-02-05 11:08:08 -0800100 art::ScopedThreadStateChange stsc(self, art::ThreadState::kNative);
Alex Light64e4c142018-01-30 13:46:37 -0800101 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 Lightd55b8442019-10-15 15:46:07 -0700115 def->SetNewDexData(new_len, new_data, kEvent);
Alex Light64e4c142018-01-30 13:46:37 -0800116}
117
Alex Lightd55b8442019-10-15 15:46:07 -0700118template <RedefinitionType kType>
Alex Light15ffafd2019-10-17 13:58:01 -0700119void Transformer::RetransformClassesDirect(
Alex Lightd55b8442019-10-15 15:46:07 -0700120 art::Thread* self,
121 /*in-out*/ std::vector<ArtClassDefinition>* definitions) {
122 constexpr ArtJvmtiEvent kEvent = kType == RedefinitionType::kNormal
123 ? ArtJvmtiEvent::kClassFileLoadHookRetransformable
124 : ArtJvmtiEvent::kStructuralDexFileLoadHook;
Alex Light6ac57502017-01-19 15:05:06 -0800125 for (ArtClassDefinition& def : *definitions) {
Alex Lightd55b8442019-10-15 15:46:07 -0700126 TransformSingleClassDirect<kEvent>(gEventHandler, self, &def);
Alex Light6ac57502017-01-19 15:05:06 -0800127 }
Alex Light6ac57502017-01-19 15:05:06 -0800128}
129
Alex Lightd55b8442019-10-15 15:46:07 -0700130template void Transformer::RetransformClassesDirect<RedefinitionType::kNormal>(
131 art::Thread* self, /*in-out*/std::vector<ArtClassDefinition>* definitions);
132template void Transformer::RetransformClassesDirect<RedefinitionType::kStructural>(
133 art::Thread* self, /*in-out*/std::vector<ArtClassDefinition>* definitions);
134
Alex Light3732beb2019-10-04 13:35:34 -0700135jvmtiError Transformer::RetransformClasses(jvmtiEnv* env,
Alex Light6ac57502017-01-19 15:05:06 -0800136 jint class_count,
Alex Light3732beb2019-10-04 13:35:34 -0700137 const jclass* classes) {
Alex Lightd55b8442019-10-15 15:46:07 -0700138 if (class_count < 0) {
Alex Light3732beb2019-10-04 13:35:34 -0700139 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM class_count was less then 0";
Alex Light6ac57502017-01-19 15:05:06 -0800140 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 Light3732beb2019-10-04 13:35:34 -0700145 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM null classes!";
Alex Light6ac57502017-01-19 15:05:06 -0800146 return ERR(NULL_POINTER);
147 }
Alex Light3732beb2019-10-04 13:35:34 -0700148 art::Thread* self = art::Thread::Current();
149 art::Runtime* runtime = art::Runtime::Current();
Alex Light6ac57502017-01-19 15:05:06 -0800150 // A holder that will Deallocate all the class bytes buffers on destruction.
Alex Light3732beb2019-10-04 13:35:34 -0700151 std::string error_msg;
Alex Light6ac57502017-01-19 15:05:06 -0800152 std::vector<ArtClassDefinition> definitions;
153 jvmtiError res = OK;
154 for (jint i = 0; i < class_count; i++) {
Alex Lightd55b8442019-10-15 15:46:07 -0700155 res = Redefiner::GetClassRedefinitionError<RedefinitionType::kNormal>(classes[i], &error_msg);
Alex Lightce6ee702017-03-06 15:46:43 -0800156 if (res != OK) {
Alex Light3732beb2019-10-04 13:35:34 -0700157 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg;
Alex Lightce6ee702017-03-06 15:46:43 -0800158 return res;
Alex Lightce6ee702017-03-06 15:46:43 -0800159 }
Alex Light6ac57502017-01-19 15:05:06 -0800160 ArtClassDefinition def;
Alex Light64e4c142018-01-30 13:46:37 -0800161 res = def.Init(self, classes[i]);
Alex Light6ac57502017-01-19 15:05:06 -0800162 if (res != OK) {
Alex Light3732beb2019-10-04 13:35:34 -0700163 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM definition init failed";
Alex Light6ac57502017-01-19 15:05:06 -0800164 return res;
165 }
166 definitions.push_back(std::move(def));
167 }
Alex Lightd55b8442019-10-15 15:46:07 -0700168 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 Light3732beb2019-10-04 13:35:34 -0700178 if (res != OK) {
179 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg;
180 }
181 return res;
Alex Light6ac57502017-01-19 15:05:06 -0800182}
183
184// TODO Move this somewhere else, ti_class?
Alex Light1e07ca62016-12-02 11:40:56 -0800185jvmtiError 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 Light9c20a142016-08-23 15:05:12 -0700200} // namespace openjdkjvmti