blob: 06711055326d62c3cfd5f963ab1dda360458d4d6 [file] [log] [blame]
Alex Lighta7e38d82017-01-19 14:57:28 -08001/* 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
32#include "ti_class_definition.h"
33
Alex Lightb7354d52017-03-30 15:17:01 -070034#include "base/array_slice.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080035#include "dex_file.h"
Alex Lightb7354d52017-03-30 15:17:01 -070036#include "fixed_up_dex_file.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080037#include "handle_scope-inl.h"
38#include "handle.h"
39#include "mirror/class-inl.h"
40#include "mirror/object-inl.h"
Alex Lightb7354d52017-03-30 15:17:01 -070041#include "reflection.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080042#include "thread.h"
43
44namespace openjdkjvmti {
45
Alex Light40528472017-03-28 09:07:36 -070046bool ArtClassDefinition::IsModified() const {
47 // RedefineClasses calls always are 'modified' since they need to change the original_dex_file of
48 // the class.
Alex Lightb7354d52017-03-30 15:17:01 -070049 if (redefined_) {
Alex Lighta7e38d82017-01-19 14:57:28 -080050 return true;
51 }
52 // Check if the dex file we want to set is the same as the current one.
Alex Light40528472017-03-28 09:07:36 -070053 // Unfortunately we need to do this check even if no modifications have been done since it could
54 // be that agents were removed in the mean-time so we still have a different dex file. The dex
55 // checksum means this is likely to be fairly fast.
Alex Lightb7354d52017-03-30 15:17:01 -070056 return static_cast<jint>(original_dex_file_.size()) != dex_len_ ||
57 memcmp(&original_dex_file_.At(0), dex_data_.get(), dex_len_) != 0;
58}
59
60jvmtiError ArtClassDefinition::InitCommon(ArtJvmTiEnv* env, jclass klass) {
61 JNIEnv* jni_env = GetJniEnv(env);
62 if (jni_env == nullptr) {
63 return ERR(INTERNAL);
64 }
65 art::ScopedObjectAccess soa(jni_env);
66 art::ObjPtr<art::mirror::Class> m_klass(soa.Decode<art::mirror::Class>(klass));
67 if (m_klass.IsNull()) {
68 return ERR(INVALID_CLASS);
69 }
70 klass_ = klass;
71 loader_ = soa.AddLocalReference<jobject>(m_klass->GetClassLoader());
72 std::string descriptor_store;
73 std::string descriptor(m_klass->GetDescriptor(&descriptor_store));
74 name_ = descriptor.substr(1, descriptor.size() - 2);
75 // Android doesn't really have protection domains.
76 protection_domain_ = nullptr;
77 return OK;
78}
79
80// Gets the data surrounding the given class.
81static jvmtiError GetDexDataForRetransformation(ArtJvmTiEnv* env,
82 art::Handle<art::mirror::Class> klass,
83 /*out*/jint* dex_data_len,
84 /*out*/unsigned char** dex_data)
85 REQUIRES_SHARED(art::Locks::mutator_lock_) {
86 art::StackHandleScope<3> hs(art::Thread::Current());
87 art::Handle<art::mirror::ClassExt> ext(hs.NewHandle(klass->GetExtData()));
88 const art::DexFile* dex_file = nullptr;
89 if (!ext.IsNull()) {
90 art::Handle<art::mirror::Object> orig_dex(hs.NewHandle(ext->GetOriginalDexFile()));
91 if (!orig_dex.IsNull()) {
92 if (orig_dex->IsArrayInstance()) {
93 DCHECK(orig_dex->GetClass()->GetComponentType()->IsPrimitiveByte());
94 art::Handle<art::mirror::ByteArray> orig_dex_bytes(
95 hs.NewHandle(art::down_cast<art::mirror::ByteArray*>(orig_dex->AsArray())));
96 *dex_data_len = static_cast<jint>(orig_dex_bytes->GetLength());
97 return CopyDataIntoJvmtiBuffer(
98 env,
99 reinterpret_cast<const unsigned char*>(orig_dex_bytes->GetData()),
100 *dex_data_len,
101 /*out*/dex_data);
102 } else if (orig_dex->IsDexCache()) {
103 dex_file = orig_dex->AsDexCache()->GetDexFile();
104 } else {
Alex Light0ecb2362017-04-12 09:01:47 -0700105 DCHECK(orig_dex->GetClass()->DescriptorEquals("Ljava/lang/Long;"))
106 << "Expected java/lang/Long but found object of type "
107 << orig_dex->GetClass()->PrettyClass();
Alex Lightb7354d52017-03-30 15:17:01 -0700108 art::ObjPtr<art::mirror::Class> prim_long_class(
109 art::Runtime::Current()->GetClassLinker()->GetClassRoot(
110 art::ClassLinker::kPrimitiveLong));
111 art::JValue val;
112 if (!art::UnboxPrimitiveForResult(orig_dex.Get(), prim_long_class, &val)) {
113 // This should never happen.
114 return ERR(INTERNAL);
115 }
116 dex_file = reinterpret_cast<const art::DexFile*>(static_cast<uintptr_t>(val.GetJ()));
117 }
118 }
119 }
120 if (dex_file == nullptr) {
121 dex_file = &klass->GetDexFile();
122 }
123 std::unique_ptr<FixedUpDexFile> fixed_dex_file(FixedUpDexFile::Create(*dex_file));
124 *dex_data_len = static_cast<jint>(fixed_dex_file->Size());
125 return CopyDataIntoJvmtiBuffer(env,
126 fixed_dex_file->Begin(),
127 fixed_dex_file->Size(),
128 /*out*/dex_data);
129}
130
131jvmtiError ArtClassDefinition::Init(ArtJvmTiEnv* env, jclass klass) {
132 jvmtiError res = InitCommon(env, klass);
133 if (res != OK) {
134 return res;
135 }
136 unsigned char* new_data = nullptr;
137 art::Thread* self = art::Thread::Current();
138 art::ScopedObjectAccess soa(self);
139 art::StackHandleScope<1> hs(self);
140 art::Handle<art::mirror::Class> m_klass(hs.NewHandle(self->DecodeJObject(klass)->AsClass()));
141 res = GetDexDataForRetransformation(env, m_klass, &dex_len_, &new_data);
142 if (res != OK) {
143 return res;
144 }
145 dex_data_ = MakeJvmtiUniquePtr(env, new_data);
146 if (m_klass->GetExtData() == nullptr || m_klass->GetExtData()->GetOriginalDexFile() == nullptr) {
147 // We have never redefined class this yet. Keep track of what the (de-quickened) dex file looks
148 // like so we can tell if anything has changed. Really we would like to just always do the
149 // 'else' block but the fact that we de-quickened stuff screws us over.
150 unsigned char* original_data_memory = nullptr;
151 res = CopyDataIntoJvmtiBuffer(env, dex_data_.get(), dex_len_, &original_data_memory);
152 original_dex_file_memory_ = MakeJvmtiUniquePtr(env, original_data_memory);
153 original_dex_file_ = art::ArraySlice<const unsigned char>(original_data_memory, dex_len_);
154 } else {
155 // We know that we have been redefined at least once (there is an original_dex_file set in
156 // the class) so we can just use the current dex file directly.
157 const art::DexFile& dex_file = m_klass->GetDexFile();
158 original_dex_file_ = art::ArraySlice<const unsigned char>(dex_file.Begin(), dex_file.Size());
159 }
160 return res;
161}
162
163jvmtiError ArtClassDefinition::Init(ArtJvmTiEnv* env, const jvmtiClassDefinition& def) {
164 jvmtiError res = InitCommon(env, def.klass);
165 if (res != OK) {
166 return res;
167 }
168 unsigned char* new_data = nullptr;
169 original_dex_file_ = art::ArraySlice<const unsigned char>(def.class_bytes, def.class_byte_count);
170 redefined_ = true;
171 dex_len_ = def.class_byte_count;
172 res = CopyDataIntoJvmtiBuffer(env, def.class_bytes, def.class_byte_count, /*out*/ &new_data);
173 dex_data_ = MakeJvmtiUniquePtr(env, new_data);
174 return res;
Alex Lighta7e38d82017-01-19 14:57:28 -0800175}
176
177} // namespace openjdkjvmti