1eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light/* Copyright (C) 2017 The Android Open Source Project
2eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light *
4eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * This file implements interfaces from the file jvmti.h. This implementation
5eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * is licensed under the same terms as the file jvmti.h.  The
6eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * copyright and license information for the file jvmti.h follows.
7eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light *
8eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light *
11eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * This code is free software; you can redistribute it and/or modify it
12eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * under the terms of the GNU General Public License version 2 only, as
13eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * published by the Free Software Foundation.  Oracle designates this
14eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * particular file as subject to the "Classpath" exception as provided
15eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * by Oracle in the LICENSE file that accompanied this code.
16eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light *
17eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * This code is distributed in the hope that it will be useful, but WITHOUT
18eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * version 2 for more details (a copy is included in the LICENSE file that
21eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * accompanied this code).
22eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light *
23eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * You should have received a copy of the GNU General Public License version
24eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * 2 along with this work; if not, write to the Free Software Foundation,
25eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light *
27eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * or visit www.oracle.com if you need additional information or have any
29eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light * questions.
30eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light */
31eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
32c758875461cca47d1b342c3c56a218a704b04365Alex Light#include "ti_class_loader-inl.h"
33eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
34eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include <limits>
35eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
3657943810cfc789da890d73621741729da5feaaf8Andreas Gampe#include <android-base/logging.h>
3757943810cfc789da890d73621741729da5feaaf8Andreas Gampe#include <android-base/stringprintf.h>
38eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
39a1d2f957a21319d1110bebb9a52f46fd1c67ffafAndreas Gampe#include "art_field-inl.h"
40eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "art_jvmti.h"
419e734c7ab4599d7747a05db0dc73c7b668cb6683David Sehr#include "dex/dex_file.h"
429e734c7ab4599d7747a05db0dc73c7b668cb6683David Sehr#include "dex/dex_file_types.h"
43eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "events-inl.h"
44eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "gc/allocation_listener.h"
45eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "gc/heap.h"
46eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "instrumentation.h"
47eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "jit/jit.h"
48eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "jit/jit_code_cache.h"
49eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "jni_env_ext-inl.h"
50eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "jvmti_allocator.h"
51eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "mirror/class.h"
52eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "mirror/class_ext.h"
53eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "mirror/object.h"
54373a9b5c718a45ac484afcf4fe6ce84f4bb562b3Andreas Gampe#include "nativehelper/scoped_local_ref.h"
55eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "object_lock.h"
56eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "runtime.h"
57eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light#include "transform.h"
58eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
59eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Lightnamespace openjdkjvmti {
60eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
61eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Lightbool ClassLoaderHelper::AddToClassLoader(art::Thread* self,
62eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light                                         art::Handle<art::mirror::ClassLoader> loader,
63eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light                                         const art::DexFile* dex_file) {
647916f20d9251773a8543f7f15d76ce21110acf96Alex Light  art::ScopedObjectAccessUnchecked soa(self);
651babae0f39c30e699f8580b6c28b4186aecdf690Alex Light  art::StackHandleScope<3> hs(self);
667916f20d9251773a8543f7f15d76ce21110acf96Alex Light  if (art::ClassLinker::IsBootClassLoader(soa, loader.Get())) {
677916f20d9251773a8543f7f15d76ce21110acf96Alex Light    art::Runtime::Current()->GetClassLinker()->AppendToBootClassPath(self, *dex_file);
687916f20d9251773a8543f7f15d76ce21110acf96Alex Light    return true;
697916f20d9251773a8543f7f15d76ce21110acf96Alex Light  }
707916f20d9251773a8543f7f15d76ce21110acf96Alex Light  art::Handle<art::mirror::Object> java_dex_file_obj(
717916f20d9251773a8543f7f15d76ce21110acf96Alex Light      hs.NewHandle(FindSourceDexFileObject(self, loader)));
72eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  if (java_dex_file_obj.IsNull()) {
73eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light    return false;
74eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  }
751babae0f39c30e699f8580b6c28b4186aecdf690Alex Light  art::Handle<art::mirror::LongArray> old_cookie(hs.NewHandle(GetDexFileCookie(java_dex_file_obj)));
76eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  art::Handle<art::mirror::LongArray> cookie(hs.NewHandle(
771babae0f39c30e699f8580b6c28b4186aecdf690Alex Light      AllocateNewDexFileCookie(self, old_cookie, dex_file)));
78eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  if (cookie.IsNull()) {
79eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light    return false;
80eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  }
81eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  art::ScopedAssertNoThreadSuspension nts("Replacing cookie fields in j.l.DexFile object");
82eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  UpdateJavaDexFile(java_dex_file_obj.Get(), cookie.Get());
83eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  return true;
84eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light}
85eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
86eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Lightvoid ClassLoaderHelper::UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file,
87eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light                                          art::ObjPtr<art::mirror::LongArray> new_cookie) {
88eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  art::ArtField* internal_cookie_field = java_dex_file->GetClass()->FindDeclaredInstanceField(
89eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light      "mInternalCookie", "Ljava/lang/Object;");
90eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  art::ArtField* cookie_field = java_dex_file->GetClass()->FindDeclaredInstanceField(
91eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light      "mCookie", "Ljava/lang/Object;");
92eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  CHECK(internal_cookie_field != nullptr);
93eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  art::ObjPtr<art::mirror::LongArray> orig_internal_cookie(
94eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light      internal_cookie_field->GetObject(java_dex_file)->AsLongArray());
95eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  art::ObjPtr<art::mirror::LongArray> orig_cookie(
96eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light      cookie_field->GetObject(java_dex_file)->AsLongArray());
97eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  internal_cookie_field->SetObject<false>(java_dex_file, new_cookie);
98eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  if (!orig_cookie.IsNull()) {
99eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light    cookie_field->SetObject<false>(java_dex_file, new_cookie);
100eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  }
101eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light}
102eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
1031babae0f39c30e699f8580b6c28b4186aecdf690Alex Lightart::ObjPtr<art::mirror::LongArray> ClassLoaderHelper::GetDexFileCookie(
1041babae0f39c30e699f8580b6c28b4186aecdf690Alex Light    art::Handle<art::mirror::Object> java_dex_file_obj) {
105eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  // mCookie is nulled out if the DexFile has been closed but mInternalCookie sticks around until
106eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  // the object is finalized. Since they always point to the same array if mCookie is not null we
107eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  // just use the mInternalCookie field. We will update one or both of these fields later.
108eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  art::ArtField* internal_cookie_field = java_dex_file_obj->GetClass()->FindDeclaredInstanceField(
109eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light      "mInternalCookie", "Ljava/lang/Object;");
110eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  // TODO Add check that mCookie is either null or same as mInternalCookie
111eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  CHECK(internal_cookie_field != nullptr);
1121babae0f39c30e699f8580b6c28b4186aecdf690Alex Light  return internal_cookie_field->GetObject(java_dex_file_obj.Get())->AsLongArray();
1131babae0f39c30e699f8580b6c28b4186aecdf690Alex Light}
1141babae0f39c30e699f8580b6c28b4186aecdf690Alex Light
1151babae0f39c30e699f8580b6c28b4186aecdf690Alex Lightart::ObjPtr<art::mirror::LongArray> ClassLoaderHelper::AllocateNewDexFileCookie(
1161babae0f39c30e699f8580b6c28b4186aecdf690Alex Light    art::Thread* self,
1171babae0f39c30e699f8580b6c28b4186aecdf690Alex Light    art::Handle<art::mirror::LongArray> cookie,
1181babae0f39c30e699f8580b6c28b4186aecdf690Alex Light    const art::DexFile* dex_file) {
1191babae0f39c30e699f8580b6c28b4186aecdf690Alex Light  art::StackHandleScope<1> hs(self);
120fa4333dcb481e564f54726b4e6f8153612df835eAndreas Gampe  CHECK(cookie != nullptr);
121eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  CHECK_GE(cookie->GetLength(), 1);
122eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  art::Handle<art::mirror::LongArray> new_cookie(
123eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light      hs.NewHandle(art::mirror::LongArray::Alloc(self, cookie->GetLength() + 1)));
124fa4333dcb481e564f54726b4e6f8153612df835eAndreas Gampe  if (new_cookie == nullptr) {
125eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light    self->AssertPendingOOMException();
126eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light    return nullptr;
127eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  }
128eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  // Copy the oat-dex field at the start.
129eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  new_cookie->SetWithoutChecks<false>(0, cookie->GetWithoutChecks(0));
1301babae0f39c30e699f8580b6c28b4186aecdf690Alex Light  // This must match the casts in runtime/native/dalvik_system_DexFile.cc:ConvertDexFilesToJavaArray
131eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  new_cookie->SetWithoutChecks<false>(
1321babae0f39c30e699f8580b6c28b4186aecdf690Alex Light      1, static_cast<int64_t>(reinterpret_cast<uintptr_t>(dex_file)));
133eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  new_cookie->Memcpy(2, cookie.Get(), 1, cookie->GetLength() - 1);
134eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  return new_cookie.Get();
135eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light}
136eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
137c758875461cca47d1b342c3c56a218a704b04365Alex Lightart::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> ClassLoaderHelper::GetDexElementList(
138c758875461cca47d1b342c3c56a218a704b04365Alex Light    art::Thread* self,
139c758875461cca47d1b342c3c56a218a704b04365Alex Light    art::Handle<art::mirror::ClassLoader> loader) {
140c758875461cca47d1b342c3c56a218a704b04365Alex Light  art::StackHandleScope<4> hs(self);
141eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
142c758875461cca47d1b342c3c56a218a704b04365Alex Light  art::Handle<art::mirror::Class>
143c758875461cca47d1b342c3c56a218a704b04365Alex Light      base_dex_loader_class(hs.NewHandle(self->DecodeJObject(
144c758875461cca47d1b342c3c56a218a704b04365Alex Light          art::WellKnownClasses::dalvik_system_BaseDexClassLoader)->AsClass()));
145eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
146eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  // Get all the ArtFields so we can look in the BaseDexClassLoader
147c758875461cca47d1b342c3c56a218a704b04365Alex Light  art::ArtField* path_list_field = art::jni::DecodeArtField(
148c758875461cca47d1b342c3c56a218a704b04365Alex Light      art::WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList);
149eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  art::ArtField* dex_path_list_element_field =
150c758875461cca47d1b342c3c56a218a704b04365Alex Light      art::jni::DecodeArtField(art::WellKnownClasses::dalvik_system_DexPathList_dexElements);
151eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
152eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  // Check if loader is a BaseDexClassLoader
153eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  art::Handle<art::mirror::Class> loader_class(hs.NewHandle(loader->GetClass()));
154eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  // Currently only base_dex_loader is allowed to actually define classes but if this changes in the
155eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  // future we should make sure to support all class loader types.
156eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  if (!loader_class->IsSubClass(base_dex_loader_class.Get())) {
157c758875461cca47d1b342c3c56a218a704b04365Alex Light    LOG(ERROR) << "The classloader " << loader_class->PrettyClass() << " is not a "
158c758875461cca47d1b342c3c56a218a704b04365Alex Light               << base_dex_loader_class->PrettyClass() << " which is currently the only "
159eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light               << "supported class loader type!";
160eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light    return nullptr;
161eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  }
162eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  // Start navigating the fields of the loader (now known to be a BaseDexClassLoader derivative)
163eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light  art::Handle<art::mirror::Object> path_list(
164eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light      hs.NewHandle(path_list_field->GetObject(loader.Get())));
165fa4333dcb481e564f54726b4e6f8153612df835eAndreas Gampe  CHECK(path_list != nullptr);
166c758875461cca47d1b342c3c56a218a704b04365Alex Light  art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> dex_elements_list =
167c758875461cca47d1b342c3c56a218a704b04365Alex Light      dex_path_list_element_field->GetObject(path_list.Get())->AsObjectArray<art::mirror::Object>();
168c758875461cca47d1b342c3c56a218a704b04365Alex Light  return dex_elements_list;
169c758875461cca47d1b342c3c56a218a704b04365Alex Light}
170c758875461cca47d1b342c3c56a218a704b04365Alex Light
171c758875461cca47d1b342c3c56a218a704b04365Alex Light// TODO This should return the actual source java.lang.DexFile object for the klass being loaded.
172c758875461cca47d1b342c3c56a218a704b04365Alex Lightart::ObjPtr<art::mirror::Object> ClassLoaderHelper::FindSourceDexFileObject(
173c758875461cca47d1b342c3c56a218a704b04365Alex Light    art::Thread* self, art::Handle<art::mirror::ClassLoader> loader) {
174c758875461cca47d1b342c3c56a218a704b04365Alex Light  art::ObjPtr<art::mirror::Object> res = nullptr;
175c758875461cca47d1b342c3c56a218a704b04365Alex Light  VisitDexFileObjects(self,
176c758875461cca47d1b342c3c56a218a704b04365Alex Light                      loader,
177c758875461cca47d1b342c3c56a218a704b04365Alex Light                      [&] (art::ObjPtr<art::mirror::Object> dex_file) {
178c758875461cca47d1b342c3c56a218a704b04365Alex Light                        res = dex_file;
179c758875461cca47d1b342c3c56a218a704b04365Alex Light                        // Just stop at the first one.
180c758875461cca47d1b342c3c56a218a704b04365Alex Light                        // TODO It would be cleaner to put the art::DexFile into the
181c758875461cca47d1b342c3c56a218a704b04365Alex Light                        // dalvik.system.DexFile the class comes from but it is more annoying
182c758875461cca47d1b342c3c56a218a704b04365Alex Light                        // because we would need to find this class. It is not necessary for proper
183c758875461cca47d1b342c3c56a218a704b04365Alex Light                        // function since we just need to be in front of the classes old dex file in
184c758875461cca47d1b342c3c56a218a704b04365Alex Light                        // the path.
185c758875461cca47d1b342c3c56a218a704b04365Alex Light                        return false;
186c758875461cca47d1b342c3c56a218a704b04365Alex Light                      });
187c758875461cca47d1b342c3c56a218a704b04365Alex Light  return res;
188eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light}
189eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light
190eb98b08b22b83aaaf72afac8e22f0108fe62310dAlex Light}  // namespace openjdkjvmti
191