• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #include "source_transform.h"
18 
19 #include "jni.h"
20 
21 #include "android-base/stringprintf.h"
22 #include "jvmti.h"
23 #include "scoped_local_ref.h"
24 
25 // Test infrastructure
26 #include "jvmti_helper.h"
27 #include "test_env.h"
28 
29 namespace art {
30 namespace Test983SourceTransformVerify {
31 
32 constexpr bool kSkipInitialLoad = true;
33 
Println(JNIEnv * env,const char * msg)34 static void Println(JNIEnv* env, const char* msg) {
35   ScopedLocalRef<jclass> test_klass(env, env->FindClass("art/Test983"));
36   jmethodID println_method = env->GetStaticMethodID(test_klass.get(),
37                                                     "doPrintln",
38                                                     "(Ljava/lang/String;)V");
39   ScopedLocalRef<jstring> data(env, env->NewStringUTF(msg));
40   env->CallStaticVoidMethod(test_klass.get(), println_method, data.get());
41 }
42 
43 // The hook we are using.
CheckDexFileHook(jvmtiEnv * jvmti_env ATTRIBUTE_UNUSED,JNIEnv * env,jclass class_being_redefined,jobject loader ATTRIBUTE_UNUSED,const char * name,jobject protection_domain ATTRIBUTE_UNUSED,jint class_data_len,const unsigned char * class_data,jint * new_class_data_len ATTRIBUTE_UNUSED,unsigned char ** new_class_data ATTRIBUTE_UNUSED)44 void JNICALL CheckDexFileHook(jvmtiEnv* jvmti_env ATTRIBUTE_UNUSED,
45                               JNIEnv* env,
46                               jclass class_being_redefined,
47                               jobject loader ATTRIBUTE_UNUSED,
48                               const char* name,
49                               jobject protection_domain ATTRIBUTE_UNUSED,
50                               jint class_data_len,
51                               const unsigned char* class_data,
52                               jint* new_class_data_len ATTRIBUTE_UNUSED,
53                               unsigned char** new_class_data ATTRIBUTE_UNUSED) {
54   if (kSkipInitialLoad && class_being_redefined == nullptr) {
55     // Something got loaded concurrently. Just ignore it for now. To make sure the test is
56     // repeatable we only care about things that come from RetransformClasses.
57     return;
58   }
59   Println(env, android::base::StringPrintf("Dex file hook for %s", name).c_str());
60   if (IsJVM()) {
61     return;
62   }
63 
64   VerifyClassData(class_data_len, class_data);
65 }
66 
67 // Get all capabilities except those related to retransformation.
Java_art_Test983_setupLoadHook(JNIEnv * env,jclass)68 extern "C" JNIEXPORT void JNICALL Java_art_Test983_setupLoadHook(JNIEnv* env, jclass) {
69   jvmtiEventCallbacks cb;
70   memset(&cb, 0, sizeof(cb));
71   cb.ClassFileLoadHook = CheckDexFileHook;
72   JvmtiErrorToException(env, jvmti_env, jvmti_env->SetEventCallbacks(&cb, sizeof(cb)));
73 }
74 
75 }  // namespace Test983SourceTransformVerify
76 }  // namespace art
77