Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 1 | /* 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 | |
Alex Light | c758875 | 2018-02-20 11:15:54 -0800 | [diff] [blame] | 32 | #include "ti_class_loader-inl.h" |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 33 | |
| 34 | #include <limits> |
| 35 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 36 | #include <android-base/logging.h> |
| 37 | #include <android-base/stringprintf.h> |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 38 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 39 | #include "art_field-inl.h" |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 40 | #include "art_jvmti.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 41 | #include "dex/dex_file.h" |
| 42 | #include "dex/dex_file_types.h" |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 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" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 49 | #include "jni/jni_env_ext-inl.h" |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 50 | #include "jvmti_allocator.h" |
| 51 | #include "mirror/class.h" |
| 52 | #include "mirror/class_ext.h" |
| 53 | #include "mirror/object.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 54 | #include "nativehelper/scoped_local_ref.h" |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 55 | #include "object_lock.h" |
| 56 | #include "runtime.h" |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 57 | #include "transform.h" |
| 58 | |
| 59 | namespace openjdkjvmti { |
| 60 | |
| 61 | bool ClassLoaderHelper::AddToClassLoader(art::Thread* self, |
| 62 | art::Handle<art::mirror::ClassLoader> loader, |
| 63 | const art::DexFile* dex_file) { |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 64 | art::ScopedObjectAccessUnchecked soa(self); |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 65 | art::StackHandleScope<3> hs(self); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 66 | 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 Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 72 | if (java_dex_file_obj.IsNull()) { |
| 73 | return false; |
| 74 | } |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 75 | art::Handle<art::mirror::LongArray> old_cookie(hs.NewHandle(GetDexFileCookie(java_dex_file_obj))); |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 76 | art::Handle<art::mirror::LongArray> cookie(hs.NewHandle( |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 77 | AllocateNewDexFileCookie(self, old_cookie, dex_file))); |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 78 | if (cookie.IsNull()) { |
| 79 | return false; |
| 80 | } |
| 81 | art::ScopedAssertNoThreadSuspension nts("Replacing cookie fields in j.l.DexFile object"); |
| 82 | UpdateJavaDexFile(java_dex_file_obj.Get(), cookie.Get()); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | void ClassLoaderHelper::UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file, |
| 87 | art::ObjPtr<art::mirror::LongArray> new_cookie) { |
| 88 | art::ArtField* internal_cookie_field = java_dex_file->GetClass()->FindDeclaredInstanceField( |
| 89 | "mInternalCookie", "Ljava/lang/Object;"); |
| 90 | art::ArtField* cookie_field = java_dex_file->GetClass()->FindDeclaredInstanceField( |
| 91 | "mCookie", "Ljava/lang/Object;"); |
| 92 | CHECK(internal_cookie_field != nullptr); |
| 93 | art::ObjPtr<art::mirror::LongArray> orig_internal_cookie( |
| 94 | internal_cookie_field->GetObject(java_dex_file)->AsLongArray()); |
| 95 | art::ObjPtr<art::mirror::LongArray> orig_cookie( |
| 96 | cookie_field->GetObject(java_dex_file)->AsLongArray()); |
| 97 | internal_cookie_field->SetObject<false>(java_dex_file, new_cookie); |
| 98 | if (!orig_cookie.IsNull()) { |
| 99 | cookie_field->SetObject<false>(java_dex_file, new_cookie); |
| 100 | } |
| 101 | } |
| 102 | |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 103 | art::ObjPtr<art::mirror::LongArray> ClassLoaderHelper::GetDexFileCookie( |
| 104 | art::Handle<art::mirror::Object> java_dex_file_obj) { |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 105 | // mCookie is nulled out if the DexFile has been closed but mInternalCookie sticks around until |
| 106 | // the object is finalized. Since they always point to the same array if mCookie is not null we |
| 107 | // just use the mInternalCookie field. We will update one or both of these fields later. |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 108 | art::ArtField* internal_cookie_field = java_dex_file_obj->GetClass()->FindDeclaredInstanceField( |
| 109 | "mInternalCookie", "Ljava/lang/Object;"); |
| 110 | // TODO Add check that mCookie is either null or same as mInternalCookie |
| 111 | CHECK(internal_cookie_field != nullptr); |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 112 | return internal_cookie_field->GetObject(java_dex_file_obj.Get())->AsLongArray(); |
| 113 | } |
| 114 | |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 115 | art::ObjPtr<art::mirror::LongArray> ClassLoaderHelper::AllocateNewDexFileCookie( |
| 116 | art::Thread* self, |
| 117 | art::Handle<art::mirror::LongArray> cookie, |
| 118 | const art::DexFile* dex_file) { |
| 119 | art::StackHandleScope<1> hs(self); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 120 | CHECK(cookie != nullptr); |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 121 | CHECK_GE(cookie->GetLength(), 1); |
| 122 | art::Handle<art::mirror::LongArray> new_cookie( |
| 123 | hs.NewHandle(art::mirror::LongArray::Alloc(self, cookie->GetLength() + 1))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 124 | if (new_cookie == nullptr) { |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 125 | self->AssertPendingOOMException(); |
| 126 | return nullptr; |
| 127 | } |
| 128 | // Copy the oat-dex field at the start. |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 129 | new_cookie->SetWithoutChecks<false>(0, cookie->GetWithoutChecks(0)); |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 130 | // This must match the casts in runtime/native/dalvik_system_DexFile.cc:ConvertDexFilesToJavaArray |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 131 | new_cookie->SetWithoutChecks<false>( |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 132 | 1, static_cast<int64_t>(reinterpret_cast<uintptr_t>(dex_file))); |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 133 | new_cookie->Memcpy(2, cookie.Get(), 1, cookie->GetLength() - 1); |
| 134 | return new_cookie.Get(); |
| 135 | } |
| 136 | |
Alex Light | c758875 | 2018-02-20 11:15:54 -0800 | [diff] [blame] | 137 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> ClassLoaderHelper::GetDexElementList( |
| 138 | art::Thread* self, |
| 139 | art::Handle<art::mirror::ClassLoader> loader) { |
| 140 | art::StackHandleScope<4> hs(self); |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 141 | |
Alex Light | c758875 | 2018-02-20 11:15:54 -0800 | [diff] [blame] | 142 | art::Handle<art::mirror::Class> |
| 143 | base_dex_loader_class(hs.NewHandle(self->DecodeJObject( |
| 144 | art::WellKnownClasses::dalvik_system_BaseDexClassLoader)->AsClass())); |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 145 | |
| 146 | // Get all the ArtFields so we can look in the BaseDexClassLoader |
Alex Light | c758875 | 2018-02-20 11:15:54 -0800 | [diff] [blame] | 147 | art::ArtField* path_list_field = art::jni::DecodeArtField( |
| 148 | art::WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 149 | art::ArtField* dex_path_list_element_field = |
Alex Light | c758875 | 2018-02-20 11:15:54 -0800 | [diff] [blame] | 150 | art::jni::DecodeArtField(art::WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 151 | |
| 152 | // Check if loader is a BaseDexClassLoader |
| 153 | art::Handle<art::mirror::Class> loader_class(hs.NewHandle(loader->GetClass())); |
| 154 | // Currently only base_dex_loader is allowed to actually define classes but if this changes in the |
| 155 | // future we should make sure to support all class loader types. |
| 156 | if (!loader_class->IsSubClass(base_dex_loader_class.Get())) { |
Alex Light | c758875 | 2018-02-20 11:15:54 -0800 | [diff] [blame] | 157 | LOG(ERROR) << "The classloader " << loader_class->PrettyClass() << " is not a " |
| 158 | << base_dex_loader_class->PrettyClass() << " which is currently the only " |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 159 | << "supported class loader type!"; |
| 160 | return nullptr; |
| 161 | } |
| 162 | // Start navigating the fields of the loader (now known to be a BaseDexClassLoader derivative) |
| 163 | art::Handle<art::mirror::Object> path_list( |
| 164 | hs.NewHandle(path_list_field->GetObject(loader.Get()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 165 | CHECK(path_list != nullptr); |
Alex Light | c758875 | 2018-02-20 11:15:54 -0800 | [diff] [blame] | 166 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> dex_elements_list = |
| 167 | dex_path_list_element_field->GetObject(path_list.Get())->AsObjectArray<art::mirror::Object>(); |
| 168 | return dex_elements_list; |
| 169 | } |
| 170 | |
| 171 | // TODO This should return the actual source java.lang.DexFile object for the klass being loaded. |
| 172 | art::ObjPtr<art::mirror::Object> ClassLoaderHelper::FindSourceDexFileObject( |
| 173 | art::Thread* self, art::Handle<art::mirror::ClassLoader> loader) { |
| 174 | art::ObjPtr<art::mirror::Object> res = nullptr; |
| 175 | VisitDexFileObjects(self, |
| 176 | loader, |
| 177 | [&] (art::ObjPtr<art::mirror::Object> dex_file) { |
| 178 | res = dex_file; |
| 179 | // Just stop at the first one. |
| 180 | // TODO It would be cleaner to put the art::DexFile into the |
| 181 | // dalvik.system.DexFile the class comes from but it is more annoying |
| 182 | // because we would need to find this class. It is not necessary for proper |
| 183 | // function since we just need to be in front of the classes old dex file in |
| 184 | // the path. |
| 185 | return false; |
| 186 | }); |
| 187 | return res; |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | } // namespace openjdkjvmti |