Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 1 | /* 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 Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 32 | #include <unordered_map> |
| 33 | #include <unordered_set> |
| 34 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 35 | #include "transform.h" |
| 36 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 37 | #include "art_method.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 38 | #include "class_linker.h" |
| 39 | #include "dex_file.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 40 | #include "dex_file_types.h" |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 41 | #include "events-inl.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 42 | #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" |
| 50 | #include "mirror/class_loader-inl.h" |
| 51 | #include "mirror/string-inl.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 52 | #include "oat_file.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 53 | #include "scoped_thread_state_change-inl.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 54 | #include "stack.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 55 | #include "thread_list.h" |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 56 | #include "ti_redefine.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 57 | #include "transform.h" |
| 58 | #include "utf.h" |
| 59 | #include "utils/dex_cache_arrays_layout-inl.h" |
| 60 | |
| 61 | namespace openjdkjvmti { |
| 62 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 63 | jvmtiError Transformer::RetransformClassesDirect( |
| 64 | ArtJvmTiEnv* env, |
| 65 | art::Thread* self, |
| 66 | /*in-out*/std::vector<ArtClassDefinition>* definitions) { |
| 67 | for (ArtClassDefinition& def : *definitions) { |
| 68 | jint new_len = -1; |
| 69 | unsigned char* new_data = nullptr; |
| 70 | // Static casts are so that we get the right template initialization for the special event |
| 71 | // handling code required by the ClassFileLoadHooks. |
| 72 | gEventHandler.DispatchEvent(self, |
| 73 | ArtJvmtiEvent::kClassFileLoadHookRetransformable, |
| 74 | GetJniEnv(env), |
| 75 | static_cast<jclass>(def.klass), |
| 76 | static_cast<jobject>(def.loader), |
| 77 | static_cast<const char*>(def.name.c_str()), |
| 78 | static_cast<jobject>(def.protection_domain), |
| 79 | static_cast<jint>(def.dex_len), |
| 80 | static_cast<const unsigned char*>(def.dex_data.get()), |
| 81 | static_cast<jint*>(&new_len), |
| 82 | static_cast<unsigned char**>(&new_data)); |
| 83 | def.SetNewDexData(env, new_len, new_data); |
| 84 | } |
| 85 | return OK; |
| 86 | } |
| 87 | |
| 88 | jvmtiError Transformer::RetransformClasses(ArtJvmTiEnv* env, |
| 89 | art::Runtime* runtime, |
| 90 | art::Thread* self, |
| 91 | jint class_count, |
| 92 | const jclass* classes, |
| 93 | /*out*/std::string* error_msg) { |
| 94 | if (env == nullptr) { |
| 95 | *error_msg = "env was null!"; |
| 96 | return ERR(INVALID_ENVIRONMENT); |
| 97 | } else if (class_count < 0) { |
| 98 | *error_msg = "class_count was less then 0"; |
| 99 | return ERR(ILLEGAL_ARGUMENT); |
| 100 | } else if (class_count == 0) { |
| 101 | // We don't actually need to do anything. Just return OK. |
| 102 | return OK; |
| 103 | } else if (classes == nullptr) { |
| 104 | *error_msg = "null classes!"; |
| 105 | return ERR(NULL_POINTER); |
| 106 | } |
| 107 | // A holder that will Deallocate all the class bytes buffers on destruction. |
| 108 | std::vector<ArtClassDefinition> definitions; |
| 109 | jvmtiError res = OK; |
| 110 | for (jint i = 0; i < class_count; i++) { |
| 111 | ArtClassDefinition def; |
| 112 | res = FillInTransformationData(env, classes[i], &def); |
| 113 | if (res != OK) { |
| 114 | return res; |
| 115 | } |
| 116 | definitions.push_back(std::move(def)); |
| 117 | } |
| 118 | res = RetransformClassesDirect(env, self, &definitions); |
| 119 | if (res != OK) { |
| 120 | return res; |
| 121 | } |
| 122 | return Redefiner::RedefineClassesDirect(env, runtime, self, definitions, error_msg); |
| 123 | } |
| 124 | |
| 125 | // TODO Move this somewhere else, ti_class? |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 126 | jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) { |
| 127 | JNIEnv* jni_env = nullptr; |
| 128 | jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1); |
| 129 | if (ret != JNI_OK) { |
| 130 | // TODO Different error might be better? |
| 131 | return ERR(INTERNAL); |
| 132 | } |
| 133 | art::ScopedObjectAccess soa(jni_env); |
| 134 | art::StackHandleScope<1> hs(art::Thread::Current()); |
| 135 | art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass))); |
| 136 | const art::DexFile& dex = hs_klass->GetDexFile(); |
| 137 | *location = dex.GetLocation(); |
| 138 | return OK; |
| 139 | } |
| 140 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 141 | // TODO Implement this for real once transformed dex data is actually saved. |
| 142 | jvmtiError Transformer::GetDexDataForRetransformation(ArtJvmTiEnv* env, |
| 143 | art::Handle<art::mirror::Class> klass, |
| 144 | /*out*/jint* dex_data_len, |
| 145 | /*out*/unsigned char** dex_data) { |
| 146 | // TODO De-quicken the dex file before passing it to the agents. |
| 147 | LOG(WARNING) << "Dex file is not de-quickened yet! Quickened dex instructions might be present"; |
| 148 | LOG(WARNING) << "Caching of initial dex data is not yet performed! Dex data might have been " |
| 149 | << "transformed by agent already"; |
| 150 | const art::DexFile& dex = klass->GetDexFile(); |
| 151 | *dex_data_len = static_cast<jint>(dex.Size()); |
| 152 | unsigned char* new_dex_data = nullptr; |
| 153 | jvmtiError alloc_error = env->Allocate(*dex_data_len, &new_dex_data); |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 154 | if (alloc_error != OK) { |
| 155 | return alloc_error; |
| 156 | } |
| 157 | // Copy the data into a temporary buffer. |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 158 | memcpy(reinterpret_cast<void*>(new_dex_data), |
| 159 | reinterpret_cast<const void*>(dex.Begin()), |
| 160 | *dex_data_len); |
| 161 | *dex_data = new_dex_data; |
| 162 | return OK; |
| 163 | } |
| 164 | |
| 165 | // TODO Move this function somewhere more appropriate. |
| 166 | // Gets the data surrounding the given class. |
| 167 | // TODO Make this less magical. |
| 168 | jvmtiError Transformer::FillInTransformationData(ArtJvmTiEnv* env, |
| 169 | jclass klass, |
| 170 | ArtClassDefinition* def) { |
| 171 | JNIEnv* jni_env = GetJniEnv(env); |
| 172 | if (jni_env == nullptr) { |
| 173 | // TODO Different error might be better? |
| 174 | return ERR(INTERNAL); |
| 175 | } |
| 176 | art::ScopedObjectAccess soa(jni_env); |
| 177 | art::StackHandleScope<3> hs(art::Thread::Current()); |
| 178 | art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass))); |
| 179 | if (hs_klass.IsNull()) { |
| 180 | return ERR(INVALID_CLASS); |
| 181 | } |
| 182 | def->klass = klass; |
| 183 | def->loader = soa.AddLocalReference<jobject>(hs_klass->GetClassLoader()); |
| 184 | def->name = art::mirror::Class::ComputeName(hs_klass)->ToModifiedUtf8(); |
| 185 | // TODO is this always null? |
| 186 | def->protection_domain = nullptr; |
| 187 | if (def->dex_data.get() == nullptr) { |
| 188 | unsigned char* new_data; |
| 189 | jvmtiError res = GetDexDataForRetransformation(env, hs_klass, &def->dex_len, &new_data); |
| 190 | if (res == OK) { |
| 191 | def->dex_data = MakeJvmtiUniquePtr(env, new_data); |
| 192 | } else { |
| 193 | return res; |
| 194 | } |
| 195 | } |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 196 | return OK; |
| 197 | } |
| 198 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 199 | } // namespace openjdkjvmti |