blob: 011bd717b42c9d962295b201c576b0c5d3b24930 [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
72namespace openjdkjvmti {
73
Alex Lightca97ada2018-02-02 09:25:31 -080074// A FaultHandler that will deal with initializing ClassDefinitions when they are actually needed.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010075class TransformationFaultHandler final : public art::FaultHandler {
Alex Lightca97ada2018-02-02 09:25:31 -080076 public:
77 explicit TransformationFaultHandler(art::FaultManager* manager)
78 : art::FaultHandler(manager),
79 uninitialized_class_definitions_lock_("JVMTI Initialized class definitions lock",
80 art::LockLevel::kSignalHandlingLock),
81 class_definition_initialized_cond_("JVMTI Initialized class definitions condition",
82 uninitialized_class_definitions_lock_) {
Andreas Gampe6e897762018-10-16 13:09:32 -070083 manager->AddHandler(this, /* generated_code= */ false);
Alex Lightca97ada2018-02-02 09:25:31 -080084 }
85
86 ~TransformationFaultHandler() {
87 art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_);
88 uninitialized_class_definitions_.clear();
89 }
90
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010091 bool Action(int sig, siginfo_t* siginfo, void* context ATTRIBUTE_UNUSED) override {
Alex Lightca97ada2018-02-02 09:25:31 -080092 DCHECK_EQ(sig, SIGSEGV);
93 art::Thread* self = art::Thread::Current();
Alex Lightca97ada2018-02-02 09:25:31 -080094 uintptr_t ptr = reinterpret_cast<uintptr_t>(siginfo->si_addr);
Alex Light842e9c82020-06-24 08:28:29 -070095 if (UNLIKELY(uninitialized_class_definitions_lock_.IsExclusiveHeld(self))) {
96 // It's possible this is just some other unrelated segv that should be
97 // handled separately, continue to later handlers. This is likely due to
98 // running out of memory somewhere along the FixedUpDexFile pipeline and
99 // is likely unrecoverable. By returning false here though we will get a
100 // better, more accurate, stack-trace later that points to the actual
101 // issue.
102 LOG(WARNING) << "Recursive SEGV occurred during Transformation dequickening at 0x" << std::hex
103 << ptr;
104 return false;
105 }
Alex Lightca97ada2018-02-02 09:25:31 -0800106 ArtClassDefinition* res = nullptr;
107
108 {
109 // NB Technically using a mutex and condition variables here is non-posix compliant but
110 // everything should be fine since both glibc and bionic implementations of mutexs and
111 // condition variables work fine so long as the thread was not interrupted during a
112 // lock/unlock (which it wasn't) on all architectures we care about.
113 art::MutexLock mu(self, uninitialized_class_definitions_lock_);
114 auto it = std::find_if(uninitialized_class_definitions_.begin(),
115 uninitialized_class_definitions_.end(),
116 [&](const auto op) { return op->ContainsAddress(ptr); });
117 if (it != uninitialized_class_definitions_.end()) {
118 res = *it;
119 // Remove the class definition.
120 uninitialized_class_definitions_.erase(it);
121 // Put it in the initializing list
122 initializing_class_definitions_.push_back(res);
123 } else {
124 // Wait for the ptr to be initialized (if it is currently initializing).
125 while (DefinitionIsInitializing(ptr)) {
126 WaitForClassInitializationToFinish();
127 }
128 // Return true (continue with user code) if we find that the definition has been
129 // initialized. Return false (continue on to next signal handler) if the definition is not
130 // initialized or found.
131 return std::find_if(initialized_class_definitions_.begin(),
132 initialized_class_definitions_.end(),
133 [&](const auto op) { return op->ContainsAddress(ptr); }) !=
Alex Light12ee56b2018-02-12 13:00:34 -0800134 initialized_class_definitions_.end();
Alex Lightca97ada2018-02-02 09:25:31 -0800135 }
136 }
137
Alex Lightfe2a39d2018-02-05 11:08:08 -0800138 if (LIKELY(self != nullptr)) {
139 CHECK_EQ(self->GetState(), art::ThreadState::kNative)
140 << "Transformation fault handler occurred outside of native mode";
141 }
142
Alex Lightca97ada2018-02-02 09:25:31 -0800143 VLOG(signals) << "Lazy initialization of dex file for transformation of " << res->GetName()
144 << " during SEGV";
145 res->InitializeMemory();
146
147 {
148 art::MutexLock mu(self, uninitialized_class_definitions_lock_);
149 // Move to initialized state and notify waiters.
150 initializing_class_definitions_.erase(std::find(initializing_class_definitions_.begin(),
151 initializing_class_definitions_.end(),
152 res));
153 initialized_class_definitions_.push_back(res);
154 class_definition_initialized_cond_.Broadcast(self);
155 }
156
157 return true;
158 }
159
160 void RemoveDefinition(ArtClassDefinition* def) REQUIRES(!uninitialized_class_definitions_lock_) {
161 art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_);
162 auto it = std::find(uninitialized_class_definitions_.begin(),
163 uninitialized_class_definitions_.end(),
164 def);
165 if (it != uninitialized_class_definitions_.end()) {
166 uninitialized_class_definitions_.erase(it);
167 return;
168 }
169 while (std::find(initializing_class_definitions_.begin(),
170 initializing_class_definitions_.end(),
171 def) != initializing_class_definitions_.end()) {
172 WaitForClassInitializationToFinish();
173 }
174 it = std::find(initialized_class_definitions_.begin(),
175 initialized_class_definitions_.end(),
176 def);
177 CHECK(it != initialized_class_definitions_.end()) << "Could not find class definition for "
178 << def->GetName();
179 initialized_class_definitions_.erase(it);
180 }
181
182 void AddArtDefinition(ArtClassDefinition* def) REQUIRES(!uninitialized_class_definitions_lock_) {
183 DCHECK(def->IsLazyDefinition());
184 art::MutexLock mu(art::Thread::Current(), uninitialized_class_definitions_lock_);
185 uninitialized_class_definitions_.push_back(def);
186 }
187
188 private:
189 bool DefinitionIsInitializing(uintptr_t ptr) REQUIRES(uninitialized_class_definitions_lock_) {
190 return std::find_if(initializing_class_definitions_.begin(),
191 initializing_class_definitions_.end(),
192 [&](const auto op) { return op->ContainsAddress(ptr); }) !=
193 initializing_class_definitions_.end();
194 }
195
196 void WaitForClassInitializationToFinish() REQUIRES(uninitialized_class_definitions_lock_) {
197 class_definition_initialized_cond_.Wait(art::Thread::Current());
198 }
199
200 art::Mutex uninitialized_class_definitions_lock_ ACQUIRED_BEFORE(art::Locks::abort_lock_);
201 art::ConditionVariable class_definition_initialized_cond_
202 GUARDED_BY(uninitialized_class_definitions_lock_);
203
204 // A list of the class definitions that have a non-readable map.
205 std::vector<ArtClassDefinition*> uninitialized_class_definitions_
206 GUARDED_BY(uninitialized_class_definitions_lock_);
207
208 // A list of class definitions that are currently undergoing unquickening. Threads should wait
209 // until the definition is no longer in this before returning.
210 std::vector<ArtClassDefinition*> initializing_class_definitions_
211 GUARDED_BY(uninitialized_class_definitions_lock_);
212
213 // A list of class definitions that are already unquickened. Threads should immediately return if
214 // it is here.
215 std::vector<ArtClassDefinition*> initialized_class_definitions_
216 GUARDED_BY(uninitialized_class_definitions_lock_);
217};
218
219static TransformationFaultHandler* gTransformFaultHandler = nullptr;
Alex Light3732beb2019-10-04 13:35:34 -0700220static EventHandler* gEventHandler = nullptr;
Alex Lightca97ada2018-02-02 09:25:31 -0800221
Alex Light3732beb2019-10-04 13:35:34 -0700222
223void Transformer::Register(EventHandler* eh) {
Alex Lightca97ada2018-02-02 09:25:31 -0800224 // Although we create this the fault handler is actually owned by the 'art::fault_manager' which
225 // will take care of destroying it.
226 if (art::MemMap::kCanReplaceMapping && ArtClassDefinition::kEnableOnDemandDexDequicken) {
227 gTransformFaultHandler = new TransformationFaultHandler(&art::fault_manager);
228 }
Alex Light3732beb2019-10-04 13:35:34 -0700229 gEventHandler = eh;
Alex Lightca97ada2018-02-02 09:25:31 -0800230}
231
232// Simple helper to add and remove the class definition from the fault handler.
233class ScopedDefinitionHandler {
234 public:
235 explicit ScopedDefinitionHandler(ArtClassDefinition* def)
236 : def_(def), is_lazy_(def_->IsLazyDefinition()) {
237 if (is_lazy_) {
238 gTransformFaultHandler->AddArtDefinition(def_);
239 }
240 }
241
242 ~ScopedDefinitionHandler() {
243 if (is_lazy_) {
244 gTransformFaultHandler->RemoveDefinition(def_);
245 }
246 }
247
248 private:
249 ArtClassDefinition* def_;
250 bool is_lazy_;
251};
252
Alex Light64e4c142018-01-30 13:46:37 -0800253// Initialize templates.
254template
255void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
256 EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
257template
258void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
259 EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
Alex Lightd55b8442019-10-15 15:46:07 -0700260template
261void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kStructuralDexFileLoadHook>(
262 EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
Alex Light64e4c142018-01-30 13:46:37 -0800263
264template<ArtJvmtiEvent kEvent>
265void Transformer::TransformSingleClassDirect(EventHandler* event_handler,
266 art::Thread* self,
267 /*in-out*/ArtClassDefinition* def) {
268 static_assert(kEvent == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable ||
Alex Lightd55b8442019-10-15 15:46:07 -0700269 kEvent == ArtJvmtiEvent::kClassFileLoadHookRetransformable ||
270 kEvent == ArtJvmtiEvent::kStructuralDexFileLoadHook,
Alex Light64e4c142018-01-30 13:46:37 -0800271 "bad event type");
Alex Lightfe2a39d2018-02-05 11:08:08 -0800272 // We don't want to do transitions between calling the event and setting the new data so change to
273 // native state early. This also avoids any problems that the FaultHandler might have in
274 // determining if an access to the dex_data is from generated code or not.
275 art::ScopedThreadStateChange stsc(self, art::ThreadState::kNative);
Alex Lightca97ada2018-02-02 09:25:31 -0800276 ScopedDefinitionHandler handler(def);
Alex Light64e4c142018-01-30 13:46:37 -0800277 jint new_len = -1;
278 unsigned char* new_data = nullptr;
279 art::ArrayRef<const unsigned char> dex_data = def->GetDexData();
280 event_handler->DispatchEvent<kEvent>(
281 self,
282 static_cast<JNIEnv*>(self->GetJniEnv()),
283 def->GetClass(),
284 def->GetLoader(),
285 def->GetName().c_str(),
286 def->GetProtectionDomain(),
287 static_cast<jint>(dex_data.size()),
288 dex_data.data(),
289 /*out*/&new_len,
290 /*out*/&new_data);
Alex Lightd55b8442019-10-15 15:46:07 -0700291 def->SetNewDexData(new_len, new_data, kEvent);
Alex Light64e4c142018-01-30 13:46:37 -0800292}
293
Alex Lightd55b8442019-10-15 15:46:07 -0700294template <RedefinitionType kType>
Alex Light15ffafd2019-10-17 13:58:01 -0700295void Transformer::RetransformClassesDirect(
Alex Lightd55b8442019-10-15 15:46:07 -0700296 art::Thread* self,
297 /*in-out*/ std::vector<ArtClassDefinition>* definitions) {
298 constexpr ArtJvmtiEvent kEvent = kType == RedefinitionType::kNormal
299 ? ArtJvmtiEvent::kClassFileLoadHookRetransformable
300 : ArtJvmtiEvent::kStructuralDexFileLoadHook;
Alex Light6ac57502017-01-19 15:05:06 -0800301 for (ArtClassDefinition& def : *definitions) {
Alex Lightd55b8442019-10-15 15:46:07 -0700302 TransformSingleClassDirect<kEvent>(gEventHandler, self, &def);
Alex Light6ac57502017-01-19 15:05:06 -0800303 }
Alex Light6ac57502017-01-19 15:05:06 -0800304}
305
Alex Lightd55b8442019-10-15 15:46:07 -0700306template void Transformer::RetransformClassesDirect<RedefinitionType::kNormal>(
307 art::Thread* self, /*in-out*/std::vector<ArtClassDefinition>* definitions);
308template void Transformer::RetransformClassesDirect<RedefinitionType::kStructural>(
309 art::Thread* self, /*in-out*/std::vector<ArtClassDefinition>* definitions);
310
Alex Light3732beb2019-10-04 13:35:34 -0700311jvmtiError Transformer::RetransformClasses(jvmtiEnv* env,
Alex Light6ac57502017-01-19 15:05:06 -0800312 jint class_count,
Alex Light3732beb2019-10-04 13:35:34 -0700313 const jclass* classes) {
Alex Lightd55b8442019-10-15 15:46:07 -0700314 if (class_count < 0) {
Alex Light3732beb2019-10-04 13:35:34 -0700315 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM class_count was less then 0";
Alex Light6ac57502017-01-19 15:05:06 -0800316 return ERR(ILLEGAL_ARGUMENT);
317 } else if (class_count == 0) {
318 // We don't actually need to do anything. Just return OK.
319 return OK;
320 } else if (classes == nullptr) {
Alex Light3732beb2019-10-04 13:35:34 -0700321 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM null classes!";
Alex Light6ac57502017-01-19 15:05:06 -0800322 return ERR(NULL_POINTER);
323 }
Alex Light3732beb2019-10-04 13:35:34 -0700324 art::Thread* self = art::Thread::Current();
325 art::Runtime* runtime = art::Runtime::Current();
Alex Light6ac57502017-01-19 15:05:06 -0800326 // A holder that will Deallocate all the class bytes buffers on destruction.
Alex Light3732beb2019-10-04 13:35:34 -0700327 std::string error_msg;
Alex Light6ac57502017-01-19 15:05:06 -0800328 std::vector<ArtClassDefinition> definitions;
329 jvmtiError res = OK;
330 for (jint i = 0; i < class_count; i++) {
Alex Lightd55b8442019-10-15 15:46:07 -0700331 res = Redefiner::GetClassRedefinitionError<RedefinitionType::kNormal>(classes[i], &error_msg);
Alex Lightce6ee702017-03-06 15:46:43 -0800332 if (res != OK) {
Alex Light3732beb2019-10-04 13:35:34 -0700333 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg;
Alex Lightce6ee702017-03-06 15:46:43 -0800334 return res;
Alex Lightce6ee702017-03-06 15:46:43 -0800335 }
Alex Light6ac57502017-01-19 15:05:06 -0800336 ArtClassDefinition def;
Alex Light64e4c142018-01-30 13:46:37 -0800337 res = def.Init(self, classes[i]);
Alex Light6ac57502017-01-19 15:05:06 -0800338 if (res != OK) {
Alex Light3732beb2019-10-04 13:35:34 -0700339 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM definition init failed";
Alex Light6ac57502017-01-19 15:05:06 -0800340 return res;
341 }
342 definitions.push_back(std::move(def));
343 }
Alex Lightd55b8442019-10-15 15:46:07 -0700344 RetransformClassesDirect<RedefinitionType::kStructural>(self, &definitions);
345 RetransformClassesDirect<RedefinitionType::kNormal>(self, &definitions);
346 RedefinitionType redef_type =
347 std::any_of(definitions.cbegin(),
348 definitions.cend(),
349 [](const auto& it) { return it.HasStructuralChanges(); })
350 ? RedefinitionType::kStructural
351 : RedefinitionType::kNormal;
352 res = Redefiner::RedefineClassesDirect(
353 ArtJvmTiEnv::AsArtJvmTiEnv(env), runtime, self, definitions, redef_type, &error_msg);
Alex Light3732beb2019-10-04 13:35:34 -0700354 if (res != OK) {
355 JVMTI_LOG(WARNING, env) << "FAILURE TO RETRANSFORM " << error_msg;
356 }
357 return res;
Alex Light6ac57502017-01-19 15:05:06 -0800358}
359
360// TODO Move this somewhere else, ti_class?
Alex Light1e07ca62016-12-02 11:40:56 -0800361jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) {
362 JNIEnv* jni_env = nullptr;
363 jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1);
364 if (ret != JNI_OK) {
365 // TODO Different error might be better?
366 return ERR(INTERNAL);
367 }
368 art::ScopedObjectAccess soa(jni_env);
369 art::StackHandleScope<1> hs(art::Thread::Current());
370 art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass)));
371 const art::DexFile& dex = hs_klass->GetDexFile();
372 *location = dex.GetLocation();
373 return OK;
374}
375
Alex Light9c20a142016-08-23 15:05:12 -0700376} // namespace openjdkjvmti