blob: 613368525e4761a6369e4e7175be97fa2731cb26 [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 Light3732beb2019-10-04 13:35:34 -070032#include <error.h>
Alex Lightca97ada2018-02-02 09:25:31 -080033#include <stddef.h>
34#include <sys/types.h>
35
Alex Lighta01de592016-11-15 10:43:06 -080036#include <unordered_map>
37#include <unordered_set>
38
Alex Light9c20a142016-08-23 15:05:12 -070039#include "transform.h"
40
Alex Lighta01de592016-11-15 10:43:06 -080041#include "art_method.h"
Vladimir Markoe1993c72017-06-14 17:01:38 +010042#include "base/array_ref.h"
David Sehr1979c642018-04-26 14:41:18 -070043#include "base/globals.h"
Andreas Gampe85f1c572018-11-21 13:52:48 -080044#include "base/logging.h"
David Sehr79e26072018-04-06 17:58:50 -070045#include "base/mem_map.h"
Alex Light9c20a142016-08-23 15:05:12 -070046#include "class_linker.h"
David Sehr9e734c72018-01-04 17:56:19 -080047#include "dex/dex_file.h"
48#include "dex/dex_file_types.h"
David Sehr0225f8e2018-01-31 08:52:24 +000049#include "dex/utf.h"
Alex Light6ac57502017-01-19 15:05:06 -080050#include "events-inl.h"
Alex Light3732beb2019-10-04 13:35:34 -070051#include "events.h"
Alex Lightca97ada2018-02-02 09:25:31 -080052#include "fault_handler.h"
Alex Light9c20a142016-08-23 15:05:12 -070053#include "gc_root-inl.h"
Andreas Gampee15b9b12018-10-29 12:54:27 -070054#include "handle_scope-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010055#include "jni/jni_env_ext-inl.h"
Alex Light6a656312017-03-29 17:18:00 -070056#include "jvalue.h"
Alex Light9c20a142016-08-23 15:05:12 -070057#include "jvmti.h"
58#include "linear_alloc.h"
Alex Light9c20a142016-08-23 15:05:12 -070059#include "mirror/array.h"
60#include "mirror/class-inl.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080061#include "mirror/class_ext.h"
Alex Light9c20a142016-08-23 15:05:12 -070062#include "mirror/class_loader-inl.h"
63#include "mirror/string-inl.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010064#include "oat_file.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070065#include "scoped_thread_state_change-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080066#include "stack.h"
Alex Light9c20a142016-08-23 15:05:12 -070067#include "thread_list.h"
Alex Light6ac57502017-01-19 15:05:06 -080068#include "ti_redefine.h"
Alex Light3732beb2019-10-04 13:35:34 -070069#include "ti_logging.h"
Alex Light9c20a142016-08-23 15:05:12 -070070#include "transform.h"
Alex Light9c20a142016-08-23 15:05:12 -070071#include "utils/dex_cache_arrays_layout-inl.h"
72
73namespace openjdkjvmti {
74
Alex Lightca97ada2018-02-02 09:25:31 -080075// A FaultHandler that will deal with initializing ClassDefinitions when they are actually needed.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010076class TransformationFaultHandler final : public art::FaultHandler {
Alex Lightca97ada2018-02-02 09:25:31 -080077 public:
78 explicit TransformationFaultHandler(art::FaultManager* manager)
79 : art::FaultHandler(manager),
80 uninitialized_class_definitions_lock_("JVMTI Initialized class definitions lock",
81 art::LockLevel::kSignalHandlingLock),
82 class_definition_initialized_cond_("JVMTI Initialized class definitions condition",
83 uninitialized_class_definitions_lock_) {
Andreas Gampe6e897762018-10-16 13:09:32 -070084 manager->AddHandler(this, /* generated_code= */ false);
Alex Lightca97ada2018-02-02 09:25:31 -080085 }
86
87 ~TransformationFaultHandler() {
88 art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_);
89 uninitialized_class_definitions_.clear();
90 }
91
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010092 bool Action(int sig, siginfo_t* siginfo, void* context ATTRIBUTE_UNUSED) override {
Alex Lightca97ada2018-02-02 09:25:31 -080093 DCHECK_EQ(sig, SIGSEGV);
94 art::Thread* self = art::Thread::Current();
95 if (UNLIKELY(uninitialized_class_definitions_lock_.IsExclusiveHeld(self))) {
96 if (self != nullptr) {
97 LOG(FATAL) << "Recursive call into Transformation fault handler!";
98 UNREACHABLE();
99 } else {
100 LOG(ERROR) << "Possible deadlock due to recursive signal delivery of segv.";
101 }
102 }
103 uintptr_t ptr = reinterpret_cast<uintptr_t>(siginfo->si_addr);
104 ArtClassDefinition* res = nullptr;
105
106 {
107 // NB Technically using a mutex and condition variables here is non-posix compliant but
108 // everything should be fine since both glibc and bionic implementations of mutexs and
109 // condition variables work fine so long as the thread was not interrupted during a
110 // lock/unlock (which it wasn't) on all architectures we care about.
111 art::MutexLock mu(self, uninitialized_class_definitions_lock_);
112 auto it = std::find_if(uninitialized_class_definitions_.begin(),
113 uninitialized_class_definitions_.end(),
114 [&](const auto op) { return op->ContainsAddress(ptr); });
115 if (it != uninitialized_class_definitions_.end()) {
116 res = *it;
117 // Remove the class definition.
118 uninitialized_class_definitions_.erase(it);
119 // Put it in the initializing list
120 initializing_class_definitions_.push_back(res);
121 } else {
122 // Wait for the ptr to be initialized (if it is currently initializing).
123 while (DefinitionIsInitializing(ptr)) {
124 WaitForClassInitializationToFinish();
125 }
126 // Return true (continue with user code) if we find that the definition has been
127 // initialized. Return false (continue on to next signal handler) if the definition is not
128 // initialized or found.
129 return std::find_if(initialized_class_definitions_.begin(),
130 initialized_class_definitions_.end(),
131 [&](const auto op) { return op->ContainsAddress(ptr); }) !=
Alex Light12ee56b2018-02-12 13:00:34 -0800132 initialized_class_definitions_.end();
Alex Lightca97ada2018-02-02 09:25:31 -0800133 }
134 }
135
Alex Lightfe2a39d2018-02-05 11:08:08 -0800136 if (LIKELY(self != nullptr)) {
137 CHECK_EQ(self->GetState(), art::ThreadState::kNative)
138 << "Transformation fault handler occurred outside of native mode";
139 }
140
Alex Lightca97ada2018-02-02 09:25:31 -0800141 VLOG(signals) << "Lazy initialization of dex file for transformation of " << res->GetName()
142 << " during SEGV";
143 res->InitializeMemory();
144
145 {
146 art::MutexLock mu(self, uninitialized_class_definitions_lock_);
147 // Move to initialized state and notify waiters.
148 initializing_class_definitions_.erase(std::find(initializing_class_definitions_.begin(),
149 initializing_class_definitions_.end(),
150 res));
151 initialized_class_definitions_.push_back(res);
152 class_definition_initialized_cond_.Broadcast(self);
153 }
154
155 return true;
156 }
157
158 void RemoveDefinition(ArtClassDefinition* def) REQUIRES(!uninitialized_class_definitions_lock_) {
159 art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_);
160 auto it = std::find(uninitialized_class_definitions_.begin(),
161 uninitialized_class_definitions_.end(),
162 def);
163 if (it != uninitialized_class_definitions_.end()) {
164 uninitialized_class_definitions_.erase(it);
165 return;
166 }
167 while (std::find(initializing_class_definitions_.begin(),
168 initializing_class_definitions_.end(),
169 def) != initializing_class_definitions_.end()) {
170 WaitForClassInitializationToFinish();
171 }
172 it = std::find(initialized_class_definitions_.begin(),
173 initialized_class_definitions_.end(),
174 def);
175 CHECK(it != initialized_class_definitions_.end()) << "Could not find class definition for "
176 << def->GetName();
177 initialized_class_definitions_.erase(it);
178 }
179
180 void AddArtDefinition(ArtClassDefinition* def) REQUIRES(!uninitialized_class_definitions_lock_) {
181 DCHECK(def->IsLazyDefinition());
182 art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_);
183 uninitialized_class_definitions_.push_back(def);
184 }
185
186 private:
187 bool DefinitionIsInitializing(uintptr_t ptr) REQUIRES(uninitialized_class_definitions_lock_) {
188 return std::find_if(initializing_class_definitions_.begin(),
189 initializing_class_definitions_.end(),
190 [&](const auto op) { return op->ContainsAddress(ptr); }) !=
191 initializing_class_definitions_.end();
192 }
193
194 void WaitForClassInitializationToFinish() REQUIRES(uninitialized_class_definitions_lock_) {
195 class_definition_initialized_cond_.Wait(art::Thread::Current());
196 }
197
198 art::Mutex uninitialized_class_definitions_lock_ ACQUIRED_BEFORE(art::Locks::abort_lock_);
199 art::ConditionVariable class_definition_initialized_cond_
200 GUARDED_BY(uninitialized_class_definitions_lock_);
201
202 // A list of the class definitions that have a non-readable map.
203 std::vector<ArtClassDefinition*> uninitialized_class_definitions_
204 GUARDED_BY(uninitialized_class_definitions_lock_);
205
206 // A list of class definitions that are currently undergoing unquickening. Threads should wait
207 // until the definition is no longer in this before returning.
208 std::vector<ArtClassDefinition*> initializing_class_definitions_
209 GUARDED_BY(uninitialized_class_definitions_lock_);
210
211 // A list of class definitions that are already unquickened. Threads should immediately return if
212 // it is here.
213 std::vector<ArtClassDefinition*> initialized_class_definitions_
214 GUARDED_BY(uninitialized_class_definitions_lock_);
215};
216
217static TransformationFaultHandler* gTransformFaultHandler = nullptr;
Alex Light3732beb2019-10-04 13:35:34 -0700218static EventHandler* gEventHandler = nullptr;
Alex Lightca97ada2018-02-02 09:25:31 -0800219
Alex Light3732beb2019-10-04 13:35:34 -0700220
221void Transformer::Register(EventHandler* eh) {
Alex Lightca97ada2018-02-02 09:25:31 -0800222 // Although we create this the fault handler is actually owned by the 'art::fault_manager' which
223 // will take care of destroying it.
224 if (art::MemMap::kCanReplaceMapping && ArtClassDefinition::kEnableOnDemandDexDequicken) {
225 gTransformFaultHandler = new TransformationFaultHandler(&art::fault_manager);
226 }
Alex Light3732beb2019-10-04 13:35:34 -0700227 gEventHandler = eh;
Alex Lightca97ada2018-02-02 09:25:31 -0800228}
229
230// Simple helper to add and remove the class definition from the fault handler.
231class ScopedDefinitionHandler {
232 public:
233 explicit ScopedDefinitionHandler(ArtClassDefinition* def)
234 : def_(def), is_lazy_(def_->IsLazyDefinition()) {
235 if (is_lazy_) {
236 gTransformFaultHandler->AddArtDefinition(def_);
237 }
238 }
239
240 ~ScopedDefinitionHandler() {
241 if (is_lazy_) {
242 gTransformFaultHandler->RemoveDefinition(def_);
243 }
244 }
245
246 private:
247 ArtClassDefinition* def_;
248 bool is_lazy_;
249};
250
Alex Light64e4c142018-01-30 13:46:37 -0800251// Initialize templates.
252template
253void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
254 EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
255template
256void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
257 EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
Alex Lightd55b8442019-10-15 15:46:07 -0700258template
259void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kStructuralDexFileLoadHook>(
260 EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
Alex Light64e4c142018-01-30 13:46:37 -0800261
262template<ArtJvmtiEvent kEvent>
263void Transformer::TransformSingleClassDirect(EventHandler* event_handler,
264 art::Thread* self,
265 /*in-out*/ArtClassDefinition* def) {
266 static_assert(kEvent == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable ||
Alex Lightd55b8442019-10-15 15:46:07 -0700267 kEvent == ArtJvmtiEvent::kClassFileLoadHookRetransformable ||
268 kEvent == ArtJvmtiEvent::kStructuralDexFileLoadHook,
Alex Light64e4c142018-01-30 13:46:37 -0800269 "bad event type");
Alex Lightfe2a39d2018-02-05 11:08:08 -0800270 // We don't want to do transitions between calling the event and setting the new data so change to
271 // native state early. This also avoids any problems that the FaultHandler might have in
272 // determining if an access to the dex_data is from generated code or not.
273 art::ScopedThreadStateChange stsc(self, art::ThreadState::kNative);
Alex Lightca97ada2018-02-02 09:25:31 -0800274 ScopedDefinitionHandler handler(def);
Alex Light64e4c142018-01-30 13:46:37 -0800275 jint new_len = -1;
276 unsigned char* new_data = nullptr;
277 art::ArrayRef<const unsigned char> dex_data = def->GetDexData();
278 event_handler->DispatchEvent<kEvent>(
279 self,
280 static_cast<JNIEnv*>(self->GetJniEnv()),
281 def->GetClass(),
282 def->GetLoader(),
283 def->GetName().c_str(),
284 def->GetProtectionDomain(),
285 static_cast<jint>(dex_data.size()),
286 dex_data.data(),
287 /*out*/&new_len,
288 /*out*/&new_data);
Alex Lightd55b8442019-10-15 15:46:07 -0700289 def->SetNewDexData(new_len, new_data, kEvent);
Alex Light64e4c142018-01-30 13:46:37 -0800290}
291
Alex Lightd55b8442019-10-15 15:46:07 -0700292template <RedefinitionType kType>
Alex Light15ffafd2019-10-17 13:58:01 -0700293void Transformer::RetransformClassesDirect(
Alex Lightd55b8442019-10-15 15:46:07 -0700294 art::Thread* self,
295 /*in-out*/ std::vector<ArtClassDefinition>* definitions) {
296 constexpr ArtJvmtiEvent kEvent = kType == RedefinitionType::kNormal
297 ? ArtJvmtiEvent::kClassFileLoadHookRetransformable
298 : ArtJvmtiEvent::kStructuralDexFileLoadHook;
Alex Light6ac57502017-01-19 15:05:06 -0800299 for (ArtClassDefinition& def : *definitions) {
Alex Lightd55b8442019-10-15 15:46:07 -0700300 TransformSingleClassDirect<kEvent>(gEventHandler, self, &def);
Alex Light6ac57502017-01-19 15:05:06 -0800301 }
Alex Light6ac57502017-01-19 15:05:06 -0800302}
303
Alex Lightd55b8442019-10-15 15:46:07 -0700304template void Transformer::RetransformClassesDirect<RedefinitionType::kNormal>(
305 art::Thread* self, /*in-out*/std::vector<ArtClassDefinition>* definitions);
306template void Transformer::RetransformClassesDirect<RedefinitionType::kStructural>(
307 art::Thread* self, /*in-out*/std::vector<ArtClassDefinition>* definitions);
308
Alex Light3732beb2019-10-04 13:35:34 -0700309jvmtiError Transformer::RetransformClasses(jvmtiEnv* env,
Alex Light6ac57502017-01-19 15:05:06 -0800310 jint class_count,
Alex Light3732beb2019-10-04 13:35:34 -0700311 const jclass* classes) {
Alex Lightd55b8442019-10-15 15:46:07 -0700312 if (class_count < 0) {
Alex Light3732beb2019-10-04 13:35:34 -0700313 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM class_count was less then 0";
Alex Light6ac57502017-01-19 15:05:06 -0800314 return ERR(ILLEGAL_ARGUMENT);
315 } else if (class_count == 0) {
316 // We don't actually need to do anything. Just return OK.
317 return OK;
318 } else if (classes == nullptr) {
Alex Light3732beb2019-10-04 13:35:34 -0700319 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM null classes!";
Alex Light6ac57502017-01-19 15:05:06 -0800320 return ERR(NULL_POINTER);
321 }
Alex Light3732beb2019-10-04 13:35:34 -0700322 art::Thread* self = art::Thread::Current();
323 art::Runtime* runtime = art::Runtime::Current();
Alex Light6ac57502017-01-19 15:05:06 -0800324 // A holder that will Deallocate all the class bytes buffers on destruction.
Alex Light3732beb2019-10-04 13:35:34 -0700325 std::string error_msg;
Alex Light6ac57502017-01-19 15:05:06 -0800326 std::vector<ArtClassDefinition> definitions;
327 jvmtiError res = OK;
328 for (jint i = 0; i < class_count; i++) {
Alex Lightd55b8442019-10-15 15:46:07 -0700329 res = Redefiner::GetClassRedefinitionError<RedefinitionType::kNormal>(classes[i], &error_msg);
Alex Lightce6ee702017-03-06 15:46:43 -0800330 if (res != OK) {
Alex Light3732beb2019-10-04 13:35:34 -0700331 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg;
Alex Lightce6ee702017-03-06 15:46:43 -0800332 return res;
Alex Lightce6ee702017-03-06 15:46:43 -0800333 }
Alex Light6ac57502017-01-19 15:05:06 -0800334 ArtClassDefinition def;
Alex Light64e4c142018-01-30 13:46:37 -0800335 res = def.Init(self, classes[i]);
Alex Light6ac57502017-01-19 15:05:06 -0800336 if (res != OK) {
Alex Light3732beb2019-10-04 13:35:34 -0700337 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM definition init failed";
Alex Light6ac57502017-01-19 15:05:06 -0800338 return res;
339 }
340 definitions.push_back(std::move(def));
341 }
Alex Lightd55b8442019-10-15 15:46:07 -0700342 RetransformClassesDirect<RedefinitionType::kStructural>(self, &definitions);
343 RetransformClassesDirect<RedefinitionType::kNormal>(self, &definitions);
344 RedefinitionType redef_type =
345 std::any_of(definitions.cbegin(),
346 definitions.cend(),
347 [](const auto& it) { return it.HasStructuralChanges(); })
348 ? RedefinitionType::kStructural
349 : RedefinitionType::kNormal;
350 res = Redefiner::RedefineClassesDirect(
351 ArtJvmTiEnv::AsArtJvmTiEnv(env), runtime, self, definitions, redef_type, &error_msg);
Alex Light3732beb2019-10-04 13:35:34 -0700352 if (res != OK) {
353 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg;
354 }
355 return res;
Alex Light6ac57502017-01-19 15:05:06 -0800356}
357
358// TODO Move this somewhere else, ti_class?
Alex Light1e07ca62016-12-02 11:40:56 -0800359jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) {
360 JNIEnv* jni_env = nullptr;
361 jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1);
362 if (ret != JNI_OK) {
363 // TODO Different error might be better?
364 return ERR(INTERNAL);
365 }
366 art::ScopedObjectAccess soa(jni_env);
367 art::StackHandleScope<1> hs(art::Thread::Current());
368 art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass)));
369 const art::DexFile& dex = hs_klass->GetDexFile();
370 *location = dex.GetLocation();
371 return OK;
372}
373
Alex Light9c20a142016-08-23 15:05:12 -0700374} // namespace openjdkjvmti