blob: af838d62b970355e865f41bd98e1d604a225bf86 [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 Lighta01de592016-11-15 10:43:06 -080032#include <unordered_map>
33#include <unordered_set>
34
Alex Light9c20a142016-08-23 15:05:12 -070035#include "transform.h"
36
Alex Lighta01de592016-11-15 10:43:06 -080037#include "art_method.h"
Vladimir Markoe1993c72017-06-14 17:01:38 +010038#include "base/array_ref.h"
Alex Light9c20a142016-08-23 15:05:12 -070039#include "class_linker.h"
David Sehr9e734c72018-01-04 17:56:19 -080040#include "dex/dex_file.h"
41#include "dex/dex_file_types.h"
David Sehr0225f8e2018-01-31 08:52:24 +000042#include "dex/utf.h"
Alex Light6ac57502017-01-19 15:05:06 -080043#include "events-inl.h"
Alex Light9c20a142016-08-23 15:05:12 -070044#include "gc_root-inl.h"
45#include "globals.h"
46#include "jni_env_ext-inl.h"
Alex Light6a656312017-03-29 17:18:00 -070047#include "jvalue.h"
Alex Light9c20a142016-08-23 15:05:12 -070048#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 Lighta7e38d82017-01-19 14:57:28 -080053#include "mirror/class_ext.h"
Alex Light9c20a142016-08-23 15:05:12 -070054#include "mirror/class_loader-inl.h"
55#include "mirror/string-inl.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010056#include "oat_file.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070057#include "scoped_thread_state_change-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080058#include "stack.h"
Alex Light9c20a142016-08-23 15:05:12 -070059#include "thread_list.h"
Alex Light6ac57502017-01-19 15:05:06 -080060#include "ti_redefine.h"
Alex Light9c20a142016-08-23 15:05:12 -070061#include "transform.h"
Alex Light9c20a142016-08-23 15:05:12 -070062#include "utils/dex_cache_arrays_layout-inl.h"
63
64namespace openjdkjvmti {
65
Alex Light64e4c142018-01-30 13:46:37 -080066// Initialize templates.
67template
68void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
69 EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
70template
71void Transformer::TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
72 EventHandler* event_handler, art::Thread* self, /*in-out*/ArtClassDefinition* def);
73
74template<ArtJvmtiEvent kEvent>
75void 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 Light6ac57502017-01-19 15:05:06 -080098jvmtiError Transformer::RetransformClassesDirect(
Andreas Gampede19eb92017-02-24 16:21:18 -080099 EventHandler* event_handler,
Alex Light6ac57502017-01-19 15:05:06 -0800100 art::Thread* self,
101 /*in-out*/std::vector<ArtClassDefinition>* definitions) {
102 for (ArtClassDefinition& def : *definitions) {
Alex Light64e4c142018-01-30 13:46:37 -0800103 TransformSingleClassDirect<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(event_handler,
104 self,
105 &def);
Alex Light6ac57502017-01-19 15:05:06 -0800106 }
107 return OK;
108}
109
110jvmtiError Transformer::RetransformClasses(ArtJvmTiEnv* env,
Andreas Gampede19eb92017-02-24 16:21:18 -0800111 EventHandler* event_handler,
Alex Light6ac57502017-01-19 15:05:06 -0800112 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 Lightce6ee702017-03-06 15:46:43 -0800134 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 Light6ac57502017-01-19 15:05:06 -0800141 ArtClassDefinition def;
Alex Light64e4c142018-01-30 13:46:37 -0800142 res = def.Init(self, classes[i]);
Alex Light6ac57502017-01-19 15:05:06 -0800143 if (res != OK) {
144 return res;
145 }
146 definitions.push_back(std::move(def));
147 }
Alex Light64e4c142018-01-30 13:46:37 -0800148 res = RetransformClassesDirect(event_handler, self, &definitions);
Alex Light6ac57502017-01-19 15:05:06 -0800149 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 Light1e07ca62016-12-02 11:40:56 -0800156jvmtiError 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 Light9c20a142016-08-23 15:05:12 -0700171} // namespace openjdkjvmti