blob: af4fb7187afe2fcc75376806831a9949fef81aac [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 Light9c20a142016-08-23 15:05:12 -070042#include "gc_root-inl.h"
43#include "globals.h"
44#include "jni_env_ext-inl.h"
45#include "jvmti.h"
46#include "linear_alloc.h"
47#include "mem_map.h"
48#include "mirror/array.h"
49#include "mirror/class-inl.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080050#include "mirror/class_ext.h"
Alex Light9c20a142016-08-23 15:05:12 -070051#include "mirror/class_loader-inl.h"
52#include "mirror/string-inl.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010053#include "oat_file.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070054#include "scoped_thread_state_change-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080055#include "stack.h"
Alex Light9c20a142016-08-23 15:05:12 -070056#include "thread_list.h"
Alex Light6ac57502017-01-19 15:05:06 -080057#include "ti_redefine.h"
Alex Light9c20a142016-08-23 15:05:12 -070058#include "transform.h"
59#include "utf.h"
60#include "utils/dex_cache_arrays_layout-inl.h"
61
62namespace openjdkjvmti {
63
Alex Light6ac57502017-01-19 15:05:06 -080064jvmtiError Transformer::RetransformClassesDirect(
65 ArtJvmTiEnv* env,
66 art::Thread* self,
67 /*in-out*/std::vector<ArtClassDefinition>* definitions) {
68 for (ArtClassDefinition& def : *definitions) {
69 jint new_len = -1;
70 unsigned char* new_data = nullptr;
71 // Static casts are so that we get the right template initialization for the special event
72 // handling code required by the ClassFileLoadHooks.
73 gEventHandler.DispatchEvent(self,
74 ArtJvmtiEvent::kClassFileLoadHookRetransformable,
75 GetJniEnv(env),
76 static_cast<jclass>(def.klass),
77 static_cast<jobject>(def.loader),
78 static_cast<const char*>(def.name.c_str()),
79 static_cast<jobject>(def.protection_domain),
80 static_cast<jint>(def.dex_len),
81 static_cast<const unsigned char*>(def.dex_data.get()),
82 static_cast<jint*>(&new_len),
83 static_cast<unsigned char**>(&new_data));
84 def.SetNewDexData(env, new_len, new_data);
85 }
86 return OK;
87}
88
89jvmtiError Transformer::RetransformClasses(ArtJvmTiEnv* env,
90 art::Runtime* runtime,
91 art::Thread* self,
92 jint class_count,
93 const jclass* classes,
94 /*out*/std::string* error_msg) {
95 if (env == nullptr) {
96 *error_msg = "env was null!";
97 return ERR(INVALID_ENVIRONMENT);
98 } else if (class_count < 0) {
99 *error_msg = "class_count was less then 0";
100 return ERR(ILLEGAL_ARGUMENT);
101 } else if (class_count == 0) {
102 // We don't actually need to do anything. Just return OK.
103 return OK;
104 } else if (classes == nullptr) {
105 *error_msg = "null classes!";
106 return ERR(NULL_POINTER);
107 }
108 // A holder that will Deallocate all the class bytes buffers on destruction.
109 std::vector<ArtClassDefinition> definitions;
110 jvmtiError res = OK;
111 for (jint i = 0; i < class_count; i++) {
112 ArtClassDefinition def;
113 res = FillInTransformationData(env, classes[i], &def);
114 if (res != OK) {
115 return res;
116 }
117 definitions.push_back(std::move(def));
118 }
119 res = RetransformClassesDirect(env, self, &definitions);
120 if (res != OK) {
121 return res;
122 }
123 return Redefiner::RedefineClassesDirect(env, runtime, self, definitions, error_msg);
124}
125
126// TODO Move this somewhere else, ti_class?
Alex Light1e07ca62016-12-02 11:40:56 -0800127jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) {
128 JNIEnv* jni_env = nullptr;
129 jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1);
130 if (ret != JNI_OK) {
131 // TODO Different error might be better?
132 return ERR(INTERNAL);
133 }
134 art::ScopedObjectAccess soa(jni_env);
135 art::StackHandleScope<1> hs(art::Thread::Current());
136 art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass)));
137 const art::DexFile& dex = hs_klass->GetDexFile();
138 *location = dex.GetLocation();
139 return OK;
140}
141
Alex Lighta7e38d82017-01-19 14:57:28 -0800142static jvmtiError CopyDataIntoJvmtiBuffer(ArtJvmTiEnv* env,
143 const unsigned char* source,
144 jint len,
145 /*out*/unsigned char** dest) {
146 jvmtiError res = env->Allocate(len, dest);
147 if (res != OK) {
148 return res;
149 }
150 memcpy(reinterpret_cast<void*>(*dest),
151 reinterpret_cast<const void*>(source),
152 len);
153 return OK;
154}
155
Alex Light6ac57502017-01-19 15:05:06 -0800156jvmtiError Transformer::GetDexDataForRetransformation(ArtJvmTiEnv* env,
157 art::Handle<art::mirror::Class> klass,
158 /*out*/jint* dex_data_len,
159 /*out*/unsigned char** dex_data) {
Alex Lighta7e38d82017-01-19 14:57:28 -0800160 art::StackHandleScope<2> hs(art::Thread::Current());
161 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->GetExtData()));
162 if (!ext.IsNull()) {
163 art::Handle<art::mirror::ByteArray> orig_dex(hs.NewHandle(ext->GetOriginalDexFileBytes()));
164 if (!orig_dex.IsNull()) {
165 *dex_data_len = static_cast<jint>(orig_dex->GetLength());
166 return CopyDataIntoJvmtiBuffer(env,
167 reinterpret_cast<const unsigned char*>(orig_dex->GetData()),
168 *dex_data_len,
169 /*out*/dex_data);
170 }
171 }
Alex Light6ac57502017-01-19 15:05:06 -0800172 // TODO De-quicken the dex file before passing it to the agents.
173 LOG(WARNING) << "Dex file is not de-quickened yet! Quickened dex instructions might be present";
Alex Light6ac57502017-01-19 15:05:06 -0800174 const art::DexFile& dex = klass->GetDexFile();
175 *dex_data_len = static_cast<jint>(dex.Size());
Alex Lighta7e38d82017-01-19 14:57:28 -0800176 return CopyDataIntoJvmtiBuffer(env, dex.Begin(), *dex_data_len, /*out*/dex_data);
Alex Light6ac57502017-01-19 15:05:06 -0800177}
178
179// TODO Move this function somewhere more appropriate.
180// Gets the data surrounding the given class.
181// TODO Make this less magical.
182jvmtiError Transformer::FillInTransformationData(ArtJvmTiEnv* env,
183 jclass klass,
184 ArtClassDefinition* def) {
185 JNIEnv* jni_env = GetJniEnv(env);
186 if (jni_env == nullptr) {
187 // TODO Different error might be better?
188 return ERR(INTERNAL);
189 }
190 art::ScopedObjectAccess soa(jni_env);
191 art::StackHandleScope<3> hs(art::Thread::Current());
192 art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass)));
193 if (hs_klass.IsNull()) {
194 return ERR(INVALID_CLASS);
195 }
196 def->klass = klass;
197 def->loader = soa.AddLocalReference<jobject>(hs_klass->GetClassLoader());
198 def->name = art::mirror::Class::ComputeName(hs_klass)->ToModifiedUtf8();
199 // TODO is this always null?
200 def->protection_domain = nullptr;
201 if (def->dex_data.get() == nullptr) {
202 unsigned char* new_data;
203 jvmtiError res = GetDexDataForRetransformation(env, hs_klass, &def->dex_len, &new_data);
204 if (res == OK) {
205 def->dex_data = MakeJvmtiUniquePtr(env, new_data);
206 } else {
207 return res;
208 }
209 }
Alex Light9c20a142016-08-23 15:05:12 -0700210 return OK;
211}
212
Alex Light9c20a142016-08-23 15:05:12 -0700213} // namespace openjdkjvmti