Home | History | Annotate | Download | only in 1950-unprepared-transform
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 
     18 #include <cstdio>
     19 #include <iostream>
     20 #include <mutex>
     21 #include <vector>
     22 
     23 #include "android-base/logging.h"
     24 #include "android-base/stringprintf.h"
     25 #include "jni.h"
     26 #include "jvmti.h"
     27 #include "scoped_local_ref.h"
     28 #include "scoped_utf_chars.h"
     29 
     30 // Test infrastructure
     31 #include "jvmti_helper.h"
     32 #include "test_env.h"
     33 
     34 namespace art {
     35 namespace Test1950UnpreparedTransform {
     36 
     37 jclass kMainClass = nullptr;
     38 jmethodID kPrepareFunc = nullptr;
     39 
     40 extern "C" JNIEXPORT void ClassLoadCallback(jvmtiEnv* jvmti ATTRIBUTE_UNUSED,
     41                                                JNIEnv* env,
     42                                                jthread thr ATTRIBUTE_UNUSED,
     43                                                jclass klass) {
     44   env->CallStaticVoidMethod(kMainClass, kPrepareFunc, klass);
     45 }
     46 
     47 extern "C" JNIEXPORT void JNICALL Java_Main_clearClassLoadHook(
     48     JNIEnv* env, jclass main ATTRIBUTE_UNUSED, jthread thr) {
     49   JvmtiErrorToException(env,
     50                         jvmti_env,
     51                         jvmti_env->SetEventNotificationMode(JVMTI_DISABLE,
     52                                                             JVMTI_EVENT_CLASS_LOAD,
     53                                                             thr));
     54 }
     55 extern "C" JNIEXPORT void JNICALL Java_Main_setupClassLoadHook(
     56     JNIEnv* env, jclass main, jthread thr) {
     57   kMainClass = reinterpret_cast<jclass>(env->NewGlobalRef(main));
     58   kPrepareFunc = env->GetStaticMethodID(main, "doClassLoad", "(Ljava/lang/Class;)V");
     59   if (env->ExceptionCheck()) {
     60     return;
     61   }
     62   current_callbacks.ClassLoad = ClassLoadCallback;
     63   if (JvmtiErrorToException(env,
     64                             jvmti_env,
     65                             jvmti_env->SetEventCallbacks(
     66                                 &current_callbacks, sizeof(current_callbacks)))) {
     67     return;
     68   }
     69   JvmtiErrorToException(env,
     70                         jvmti_env,
     71                         jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
     72                                                             JVMTI_EVENT_CLASS_LOAD,
     73                                                             thr));
     74 }
     75 
     76 }  // namespace Test1950UnpreparedTransform
     77 }  // namespace art
     78