blob: 8e38a364ff4d43701a1d06c36b3db00929fb0f89 [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"
Alex Light9c20a142016-08-23 15:05:12 -070038#include "class_linker.h"
39#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080040#include "dex_file_types.h"
Alex Light6ac57502017-01-19 15:05:06 -080041#include "events-inl.h"
Alex Light40528472017-03-28 09:07:36 -070042#include "fixed_up_dex_file.h"
Alex Light9c20a142016-08-23 15:05:12 -070043#include "gc_root-inl.h"
44#include "globals.h"
45#include "jni_env_ext-inl.h"
Alex Light6a656312017-03-29 17:18:00 -070046#include "jvalue.h"
Alex Light9c20a142016-08-23 15:05:12 -070047#include "jvmti.h"
48#include "linear_alloc.h"
49#include "mem_map.h"
50#include "mirror/array.h"
51#include "mirror/class-inl.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080052#include "mirror/class_ext.h"
Alex Light9c20a142016-08-23 15:05:12 -070053#include "mirror/class_loader-inl.h"
54#include "mirror/string-inl.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010055#include "oat_file.h"
Alex Light6a656312017-03-29 17:18:00 -070056#include "reflection.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"
62#include "utf.h"
63#include "utils/dex_cache_arrays_layout-inl.h"
64
65namespace openjdkjvmti {
66
Alex Light6ac57502017-01-19 15:05:06 -080067jvmtiError Transformer::RetransformClassesDirect(
68 ArtJvmTiEnv* env,
Andreas Gampede19eb92017-02-24 16:21:18 -080069 EventHandler* event_handler,
Alex Light6ac57502017-01-19 15:05:06 -080070 art::Thread* self,
71 /*in-out*/std::vector<ArtClassDefinition>* definitions) {
72 for (ArtClassDefinition& def : *definitions) {
73 jint new_len = -1;
74 unsigned char* new_data = nullptr;
Andreas Gampede19eb92017-02-24 16:21:18 -080075 event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
Andreas Gampe983c1752017-01-23 19:46:56 -080076 self,
77 GetJniEnv(env),
78 def.klass,
79 def.loader,
80 def.name.c_str(),
81 def.protection_domain,
82 def.dex_len,
83 static_cast<const unsigned char*>(def.dex_data.get()),
84 &new_len,
85 &new_data);
Alex Light6ac57502017-01-19 15:05:06 -080086 def.SetNewDexData(env, new_len, new_data);
87 }
88 return OK;
89}
90
91jvmtiError Transformer::RetransformClasses(ArtJvmTiEnv* env,
Andreas Gampede19eb92017-02-24 16:21:18 -080092 EventHandler* event_handler,
Alex Light6ac57502017-01-19 15:05:06 -080093 art::Runtime* runtime,
94 art::Thread* self,
95 jint class_count,
96 const jclass* classes,
97 /*out*/std::string* error_msg) {
98 if (env == nullptr) {
99 *error_msg = "env was null!";
100 return ERR(INVALID_ENVIRONMENT);
101 } else if (class_count < 0) {
102 *error_msg = "class_count was less then 0";
103 return ERR(ILLEGAL_ARGUMENT);
104 } else if (class_count == 0) {
105 // We don't actually need to do anything. Just return OK.
106 return OK;
107 } else if (classes == nullptr) {
108 *error_msg = "null classes!";
109 return ERR(NULL_POINTER);
110 }
111 // A holder that will Deallocate all the class bytes buffers on destruction.
112 std::vector<ArtClassDefinition> definitions;
113 jvmtiError res = OK;
114 for (jint i = 0; i < class_count; i++) {
Alex Lightce6ee702017-03-06 15:46:43 -0800115 jboolean is_modifiable = JNI_FALSE;
116 res = env->IsModifiableClass(classes[i], &is_modifiable);
117 if (res != OK) {
118 return res;
119 } else if (!is_modifiable) {
120 return ERR(UNMODIFIABLE_CLASS);
121 }
Alex Light6ac57502017-01-19 15:05:06 -0800122 ArtClassDefinition def;
123 res = FillInTransformationData(env, classes[i], &def);
124 if (res != OK) {
125 return res;
126 }
127 definitions.push_back(std::move(def));
128 }
Andreas Gampede19eb92017-02-24 16:21:18 -0800129 res = RetransformClassesDirect(env, event_handler, self, &definitions);
Alex Light6ac57502017-01-19 15:05:06 -0800130 if (res != OK) {
131 return res;
132 }
133 return Redefiner::RedefineClassesDirect(env, runtime, self, definitions, error_msg);
134}
135
136// TODO Move this somewhere else, ti_class?
Alex Light1e07ca62016-12-02 11:40:56 -0800137jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) {
138 JNIEnv* jni_env = nullptr;
139 jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1);
140 if (ret != JNI_OK) {
141 // TODO Different error might be better?
142 return ERR(INTERNAL);
143 }
144 art::ScopedObjectAccess soa(jni_env);
145 art::StackHandleScope<1> hs(art::Thread::Current());
146 art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass)));
147 const art::DexFile& dex = hs_klass->GetDexFile();
148 *location = dex.GetLocation();
149 return OK;
150}
151
Alex Light6ac57502017-01-19 15:05:06 -0800152jvmtiError Transformer::GetDexDataForRetransformation(ArtJvmTiEnv* env,
153 art::Handle<art::mirror::Class> klass,
154 /*out*/jint* dex_data_len,
155 /*out*/unsigned char** dex_data) {
Alex Light2f814aa2017-03-24 15:21:34 +0000156 art::StackHandleScope<3> hs(art::Thread::Current());
Alex Lighta7e38d82017-01-19 14:57:28 -0800157 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->GetExtData()));
Alex Light40528472017-03-28 09:07:36 -0700158 const art::DexFile* dex_file = nullptr;
Alex Lighta7e38d82017-01-19 14:57:28 -0800159 if (!ext.IsNull()) {
Alex Light2f814aa2017-03-24 15:21:34 +0000160 art::Handle<art::mirror::Object> orig_dex(hs.NewHandle(ext->GetOriginalDexFile()));
Alex Lighta7e38d82017-01-19 14:57:28 -0800161 if (!orig_dex.IsNull()) {
Alex Light2f814aa2017-03-24 15:21:34 +0000162 if (orig_dex->IsArrayInstance()) {
163 DCHECK(orig_dex->GetClass()->GetComponentType()->IsPrimitiveByte());
164 art::Handle<art::mirror::ByteArray> orig_dex_bytes(
165 hs.NewHandle(art::down_cast<art::mirror::ByteArray*>(orig_dex->AsArray())));
166 *dex_data_len = static_cast<jint>(orig_dex_bytes->GetLength());
167 return CopyDataIntoJvmtiBuffer(
168 env,
169 reinterpret_cast<const unsigned char*>(orig_dex_bytes->GetData()),
170 *dex_data_len,
171 /*out*/dex_data);
Alex Light6a656312017-03-29 17:18:00 -0700172 } else if (orig_dex->IsDexCache()) {
Alex Light40528472017-03-28 09:07:36 -0700173 dex_file = orig_dex->AsDexCache()->GetDexFile();
Alex Light6a656312017-03-29 17:18:00 -0700174 } else {
175 DCHECK_EQ(orig_dex->GetClass()->GetPrimitiveType(), art::Primitive::kPrimLong);
176 art::ObjPtr<art::mirror::Class> prim_long_class(
177 art::Runtime::Current()->GetClassLinker()->GetClassRoot(
178 art::ClassLinker::kPrimitiveLong));
179 art::JValue val;
180 if (!art::UnboxPrimitiveForResult(orig_dex.Get(), prim_long_class, &val)) {
181 // This should never happen.
182 return ERR(INTERNAL);
183 }
184 dex_file = reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ()));
Alex Light2f814aa2017-03-24 15:21:34 +0000185 }
Alex Lighta7e38d82017-01-19 14:57:28 -0800186 }
187 }
Alex Light40528472017-03-28 09:07:36 -0700188 if (dex_file == nullptr) {
189 dex_file = &klass->GetDexFile();
Alex Light40528472017-03-28 09:07:36 -0700190 }
191 std::unique_ptr<FixedUpDexFile> fixed_dex_file(FixedUpDexFile::Create(*dex_file));
Alex Light6a656312017-03-29 17:18:00 -0700192 *dex_data_len = static_cast<jint>(fixed_dex_file->Size());
Alex Light40528472017-03-28 09:07:36 -0700193 return CopyDataIntoJvmtiBuffer(env,
194 fixed_dex_file->Begin(),
195 fixed_dex_file->Size(),
196 /*out*/dex_data);
Alex Light6ac57502017-01-19 15:05:06 -0800197}
198
199// TODO Move this function somewhere more appropriate.
200// Gets the data surrounding the given class.
201// TODO Make this less magical.
202jvmtiError Transformer::FillInTransformationData(ArtJvmTiEnv* env,
203 jclass klass,
204 ArtClassDefinition* def) {
205 JNIEnv* jni_env = GetJniEnv(env);
206 if (jni_env == nullptr) {
207 // TODO Different error might be better?
208 return ERR(INTERNAL);
209 }
210 art::ScopedObjectAccess soa(jni_env);
211 art::StackHandleScope<3> hs(art::Thread::Current());
212 art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass)));
213 if (hs_klass.IsNull()) {
214 return ERR(INVALID_CLASS);
215 }
216 def->klass = klass;
217 def->loader = soa.AddLocalReference<jobject>(hs_klass->GetClassLoader());
Alex Light28027122017-01-26 17:21:51 -0800218 std::string descriptor_store;
219 std::string descriptor(hs_klass->GetDescriptor(&descriptor_store));
220 def->name = descriptor.substr(1, descriptor.size() - 2);
Alex Light6ac57502017-01-19 15:05:06 -0800221 // TODO is this always null?
222 def->protection_domain = nullptr;
223 if (def->dex_data.get() == nullptr) {
224 unsigned char* new_data;
225 jvmtiError res = GetDexDataForRetransformation(env, hs_klass, &def->dex_len, &new_data);
226 if (res == OK) {
227 def->dex_data = MakeJvmtiUniquePtr(env, new_data);
Alex Light40528472017-03-28 09:07:36 -0700228 // TODO This whole thing is a bit of a mess.
229 // We need to keep track of what the runtime should think an unmodified dex file is since
230 // we need to be able to tell if anything changes. This might be different then the currently
231 // loaded dex file since we need to un-quicken stuff.
232 if (hs_klass->GetExtData() == nullptr ||
233 hs_klass->GetExtData()->GetOriginalDexFile() == nullptr) {
234 // We have never redefined this yet. Keep track of what the (de-quickened) dex file looks
235 // like so we can tell if anything has changed.
236 // Really we would like to just always do the 'else' block but the fact that we de-quickened
237 // stuff screws us over.
238 unsigned char* original_data_memory = nullptr;
239 res = env->Allocate(def->dex_len, &original_data_memory);
240 if (res != OK) {
241 return res;
242 }
243 memcpy(original_data_memory, new_data, def->dex_len);
244 def->original_dex_file_memory = MakeJvmtiUniquePtr(env, original_data_memory);
245 def->original_dex_file = art::ArraySlice<const unsigned char>(original_data_memory,
246 def->dex_len);
247 } else {
248 // We know that we have been redefined at least once (there is an original_dex_file set in
249 // the class) so we can just use the current dex file directly.
250 def->original_dex_file = art::ArraySlice<const unsigned char>(
251 hs_klass->GetDexFile().Begin(), hs_klass->GetDexFile().Size());
252 }
Alex Light6ac57502017-01-19 15:05:06 -0800253 } else {
254 return res;
255 }
256 }
Alex Light9c20a142016-08-23 15:05:12 -0700257 return OK;
258}
259
Alex Light9c20a142016-08-23 15:05:12 -0700260} // namespace openjdkjvmti