Home | History | Annotate | Download | only in openjdkjvmti
      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 
     32 #include <unordered_map>
     33 #include <unordered_set>
     34 
     35 #include "transform.h"
     36 
     37 #include "art_method.h"
     38 #include "base/array_ref.h"
     39 #include "class_linker.h"
     40 #include "dex_file.h"
     41 #include "dex_file_types.h"
     42 #include "events-inl.h"
     43 #include "gc_root-inl.h"
     44 #include "globals.h"
     45 #include "jni_env_ext-inl.h"
     46 #include "jvalue.h"
     47 #include "jvmti.h"
     48 #include "linear_alloc.h"
     49 #include "mem_map.h"
     50 #include "mirror/array.h"
     51 #include "mirror/class-inl.h"
     52 #include "mirror/class_ext.h"
     53 #include "mirror/class_loader-inl.h"
     54 #include "mirror/string-inl.h"
     55 #include "oat_file.h"
     56 #include "scoped_thread_state_change-inl.h"
     57 #include "stack.h"
     58 #include "thread_list.h"
     59 #include "ti_redefine.h"
     60 #include "transform.h"
     61 #include "utf.h"
     62 #include "utils/dex_cache_arrays_layout-inl.h"
     63 
     64 namespace openjdkjvmti {
     65 
     66 jvmtiError Transformer::RetransformClassesDirect(
     67       ArtJvmTiEnv* env,
     68       EventHandler* event_handler,
     69       art::Thread* self,
     70       /*in-out*/std::vector<ArtClassDefinition>* definitions) {
     71   for (ArtClassDefinition& def : *definitions) {
     72     jint new_len = -1;
     73     unsigned char* new_data = nullptr;
     74     art::ArrayRef<const unsigned char> dex_data = def.GetDexData();
     75     event_handler->DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
     76         self,
     77         GetJniEnv(env),
     78         def.GetClass(),
     79         def.GetLoader(),
     80         def.GetName().c_str(),
     81         def.GetProtectionDomain(),
     82         static_cast<jint>(dex_data.size()),
     83         dex_data.data(),
     84         /*out*/&new_len,
     85         /*out*/&new_data);
     86     def.SetNewDexData(env, new_len, new_data);
     87   }
     88   return OK;
     89 }
     90 
     91 jvmtiError Transformer::RetransformClasses(ArtJvmTiEnv* env,
     92                                            EventHandler* event_handler,
     93                                            art::Runtime* runtime,
     94                                            art::Thread* self,
     95                                            jint class_count,
     96                                            const jclass* classes,
     97                                            /*out*/std::string* error_msg) {
     98   if (env == nullptr) {
     99     *error_msg = "env was null!";
    100     return ERR(INVALID_ENVIRONMENT);
    101   } else if (class_count < 0) {
    102     *error_msg = "class_count was less then 0";
    103     return ERR(ILLEGAL_ARGUMENT);
    104   } else if (class_count == 0) {
    105     // We don't actually need to do anything. Just return OK.
    106     return OK;
    107   } else if (classes == nullptr) {
    108     *error_msg = "null classes!";
    109     return ERR(NULL_POINTER);
    110   }
    111   // A holder that will Deallocate all the class bytes buffers on destruction.
    112   std::vector<ArtClassDefinition> definitions;
    113   jvmtiError res = OK;
    114   for (jint i = 0; i < class_count; i++) {
    115     jboolean is_modifiable = JNI_FALSE;
    116     res = env->IsModifiableClass(classes[i], &is_modifiable);
    117     if (res != OK) {
    118       return res;
    119     } else if (!is_modifiable) {
    120       return ERR(UNMODIFIABLE_CLASS);
    121     }
    122     ArtClassDefinition def;
    123     res = def.Init(env, classes[i]);
    124     if (res != OK) {
    125       return res;
    126     }
    127     definitions.push_back(std::move(def));
    128   }
    129   res = RetransformClassesDirect(env, event_handler, self, &definitions);
    130   if (res != OK) {
    131     return res;
    132   }
    133   return Redefiner::RedefineClassesDirect(env, runtime, self, definitions, error_msg);
    134 }
    135 
    136 // TODO Move this somewhere else, ti_class?
    137 jvmtiError GetClassLocation(ArtJvmTiEnv* env, jclass klass, /*out*/std::string* location) {
    138   JNIEnv* jni_env = nullptr;
    139   jint ret = env->art_vm->GetEnv(reinterpret_cast<void**>(&jni_env), JNI_VERSION_1_1);
    140   if (ret != JNI_OK) {
    141     // TODO Different error might be better?
    142     return ERR(INTERNAL);
    143   }
    144   art::ScopedObjectAccess soa(jni_env);
    145   art::StackHandleScope<1> hs(art::Thread::Current());
    146   art::Handle<art::mirror::Class> hs_klass(hs.NewHandle(soa.Decode<art::mirror::Class>(klass)));
    147   const art::DexFile& dex = hs_klass->GetDexFile();
    148   *location = dex.GetLocation();
    149   return OK;
    150 }
    151 
    152 }  // namespace openjdkjvmti
    153