blob: c2f17924da28eb5cfdf195ebd2f01fbf9d0ba9f4 [file] [log] [blame]
Alex Lighteb98b082017-01-25 13:02:32 -08001/* Copyright (C) 2017 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_loader.h"
33
34#include <limits>
35
36#include "android-base/stringprintf.h"
37
38#include "art_jvmti.h"
39#include "base/array_slice.h"
40#include "base/logging.h"
41#include "dex_file.h"
42#include "dex_file_types.h"
43#include "events-inl.h"
44#include "gc/allocation_listener.h"
45#include "gc/heap.h"
46#include "instrumentation.h"
47#include "jit/jit.h"
48#include "jit/jit_code_cache.h"
49#include "jni_env_ext-inl.h"
50#include "jvmti_allocator.h"
51#include "mirror/class.h"
52#include "mirror/class_ext.h"
53#include "mirror/object.h"
54#include "object_lock.h"
55#include "runtime.h"
56#include "ScopedLocalRef.h"
57#include "transform.h"
58
59namespace openjdkjvmti {
60
61bool ClassLoaderHelper::AddToClassLoader(art::Thread* self,
62 art::Handle<art::mirror::ClassLoader> loader,
63 const art::DexFile* dex_file) {
Alex Light7916f202017-01-27 09:00:15 -080064 art::ScopedObjectAccessUnchecked soa(self);
Alex Lighteb98b082017-01-25 13:02:32 -080065 art::StackHandleScope<2> hs(self);
Alex Light7916f202017-01-27 09:00:15 -080066 if (art::ClassLinker::IsBootClassLoader(soa, loader.Get())) {
67 art::Runtime::Current()->GetClassLinker()->AppendToBootClassPath(self, *dex_file);
68 return true;
69 }
70 art::Handle<art::mirror::Object> java_dex_file_obj(
71 hs.NewHandle(FindSourceDexFileObject(self, loader)));
Alex Lighteb98b082017-01-25 13:02:32 -080072 if (java_dex_file_obj.IsNull()) {
73 return false;
74 }
75 art::Handle<art::mirror::LongArray> cookie(hs.NewHandle(
76 AllocateNewDexFileCookie(self, java_dex_file_obj, dex_file)));
77 if (cookie.IsNull()) {
78 return false;
79 }
80 art::ScopedAssertNoThreadSuspension nts("Replacing cookie fields in j.l.DexFile object");
81 UpdateJavaDexFile(java_dex_file_obj.Get(), cookie.Get());
82 return true;
83}
84
85void ClassLoaderHelper::UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file,
86 art::ObjPtr<art::mirror::LongArray> new_cookie) {
87 art::ArtField* internal_cookie_field = java_dex_file->GetClass()->FindDeclaredInstanceField(
88 "mInternalCookie", "Ljava/lang/Object;");
89 art::ArtField* cookie_field = java_dex_file->GetClass()->FindDeclaredInstanceField(
90 "mCookie", "Ljava/lang/Object;");
91 CHECK(internal_cookie_field != nullptr);
92 art::ObjPtr<art::mirror::LongArray> orig_internal_cookie(
93 internal_cookie_field->GetObject(java_dex_file)->AsLongArray());
94 art::ObjPtr<art::mirror::LongArray> orig_cookie(
95 cookie_field->GetObject(java_dex_file)->AsLongArray());
96 internal_cookie_field->SetObject<false>(java_dex_file, new_cookie);
97 if (!orig_cookie.IsNull()) {
98 cookie_field->SetObject<false>(java_dex_file, new_cookie);
99 }
100}
101
102// TODO Really wishing I had that mirror of java.lang.DexFile now.
103art::ObjPtr<art::mirror::LongArray> ClassLoaderHelper::AllocateNewDexFileCookie(
104 art::Thread* self,
105 art::Handle<art::mirror::Object> java_dex_file_obj,
106 const art::DexFile* dex_file) {
107 art::StackHandleScope<2> hs(self);
108 // mCookie is nulled out if the DexFile has been closed but mInternalCookie sticks around until
109 // the object is finalized. Since they always point to the same array if mCookie is not null we
110 // just use the mInternalCookie field. We will update one or both of these fields later.
111 // TODO Should I get the class from the classloader or directly?
112 art::ArtField* internal_cookie_field = java_dex_file_obj->GetClass()->FindDeclaredInstanceField(
113 "mInternalCookie", "Ljava/lang/Object;");
114 // TODO Add check that mCookie is either null or same as mInternalCookie
115 CHECK(internal_cookie_field != nullptr);
116 art::Handle<art::mirror::LongArray> cookie(
117 hs.NewHandle(internal_cookie_field->GetObject(java_dex_file_obj.Get())->AsLongArray()));
118 // TODO Maybe make these non-fatal.
119 CHECK(cookie.Get() != nullptr);
120 CHECK_GE(cookie->GetLength(), 1);
121 art::Handle<art::mirror::LongArray> new_cookie(
122 hs.NewHandle(art::mirror::LongArray::Alloc(self, cookie->GetLength() + 1)));
123 if (new_cookie.Get() == nullptr) {
124 self->AssertPendingOOMException();
125 return nullptr;
126 }
127 // Copy the oat-dex field at the start.
128 // TODO Should I clear this field?
129 // TODO This is a really crappy thing here with the first element being different.
130 new_cookie->SetWithoutChecks<false>(0, cookie->GetWithoutChecks(0));
131 new_cookie->SetWithoutChecks<false>(
132 1, static_cast<int64_t>(reinterpret_cast<intptr_t>(dex_file)));
133 new_cookie->Memcpy(2, cookie.Get(), 1, cookie->GetLength() - 1);
134 return new_cookie.Get();
135}
136
137// TODO This should return the actual source java.lang.DexFile object for the klass being loaded.
138art::ObjPtr<art::mirror::Object> ClassLoaderHelper::FindSourceDexFileObject(
139 art::Thread* self, art::Handle<art::mirror::ClassLoader> loader) {
140 const char* dex_path_list_element_array_name = "[Ldalvik/system/DexPathList$Element;";
141 const char* dex_path_list_element_name = "Ldalvik/system/DexPathList$Element;";
142 const char* dex_file_name = "Ldalvik/system/DexFile;";
143 const char* dex_path_list_name = "Ldalvik/system/DexPathList;";
144 const char* dex_class_loader_name = "Ldalvik/system/BaseDexClassLoader;";
145
146 CHECK(!self->IsExceptionPending());
147 art::StackHandleScope<5> hs(self);
148 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
149
150 art::Handle<art::mirror::ClassLoader> null_loader(hs.NewHandle<art::mirror::ClassLoader>(
151 nullptr));
152 art::Handle<art::mirror::Class> base_dex_loader_class(hs.NewHandle(class_linker->FindClass(
153 self, dex_class_loader_name, null_loader)));
154
155 // Get all the ArtFields so we can look in the BaseDexClassLoader
156 art::ArtField* path_list_field = base_dex_loader_class->FindDeclaredInstanceField(
157 "pathList", dex_path_list_name);
158 CHECK(path_list_field != nullptr);
159
160 art::ArtField* dex_path_list_element_field =
161 class_linker->FindClass(self, dex_path_list_name, null_loader)
162 ->FindDeclaredInstanceField("dexElements", dex_path_list_element_array_name);
163 CHECK(dex_path_list_element_field != nullptr);
164
165 art::ArtField* element_dex_file_field =
166 class_linker->FindClass(self, dex_path_list_element_name, null_loader)
167 ->FindDeclaredInstanceField("dexFile", dex_file_name);
168 CHECK(element_dex_file_field != nullptr);
169
170 // Check if loader is a BaseDexClassLoader
171 art::Handle<art::mirror::Class> loader_class(hs.NewHandle(loader->GetClass()));
172 // Currently only base_dex_loader is allowed to actually define classes but if this changes in the
173 // future we should make sure to support all class loader types.
174 if (!loader_class->IsSubClass(base_dex_loader_class.Get())) {
175 LOG(ERROR) << "The classloader is not a BaseDexClassLoader which is currently the only "
176 << "supported class loader type!";
177 return nullptr;
178 }
179 // Start navigating the fields of the loader (now known to be a BaseDexClassLoader derivative)
180 art::Handle<art::mirror::Object> path_list(
181 hs.NewHandle(path_list_field->GetObject(loader.Get())));
182 CHECK(path_list.Get() != nullptr);
183 CHECK(!self->IsExceptionPending());
184 art::Handle<art::mirror::ObjectArray<art::mirror::Object>> dex_elements_list(hs.NewHandle(
185 dex_path_list_element_field->GetObject(path_list.Get())->
186 AsObjectArray<art::mirror::Object>()));
187 CHECK(!self->IsExceptionPending());
188 CHECK(dex_elements_list.Get() != nullptr);
189 size_t num_elements = dex_elements_list->GetLength();
190 // Iterate over the DexPathList$Element to find the right one
191 for (size_t i = 0; i < num_elements; i++) {
192 art::ObjPtr<art::mirror::Object> current_element = dex_elements_list->Get(i);
193 CHECK(!current_element.IsNull());
194 // TODO It would be cleaner to put the art::DexFile into the dalvik.system.DexFile the class
195 // comes from but it is more annoying because we would need to find this class. It is not
196 // necessary for proper function since we just need to be in front of the classes old dex file
197 // in the path.
198 art::ObjPtr<art::mirror::Object> first_dex_file(
199 element_dex_file_field->GetObject(current_element));
200 if (!first_dex_file.IsNull()) {
201 return first_dex_file;
202 }
203 }
204 return nullptr;
205}
206
207} // namespace openjdkjvmti