xref: /aosp_15_r20/art/test/983-source-transform-verify/source_transform.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "source_transform.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "jni.h"
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
22*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
23*795d594fSAndroid Build Coastguard Worker #include "scoped_local_ref.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker // Test infrastructure
26*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
27*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker namespace art {
30*795d594fSAndroid Build Coastguard Worker namespace Test983SourceTransformVerify {
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker constexpr bool kSkipInitialLoad = true;
33*795d594fSAndroid Build Coastguard Worker 
Println(JNIEnv * env,const char * msg)34*795d594fSAndroid Build Coastguard Worker static void Println(JNIEnv* env, const char* msg) {
35*795d594fSAndroid Build Coastguard Worker   ScopedLocalRef<jclass> test_klass(env, env->FindClass("art/Test983"));
36*795d594fSAndroid Build Coastguard Worker   jmethodID println_method = env->GetStaticMethodID(test_klass.get(),
37*795d594fSAndroid Build Coastguard Worker                                                     "doPrintln",
38*795d594fSAndroid Build Coastguard Worker                                                     "(Ljava/lang/String;)V");
39*795d594fSAndroid Build Coastguard Worker   ScopedLocalRef<jstring> data(env, env->NewStringUTF(msg));
40*795d594fSAndroid Build Coastguard Worker   env->CallStaticVoidMethod(test_klass.get(), println_method, data.get());
41*795d594fSAndroid Build Coastguard Worker }
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker // The hook we are using.
CheckDexFileHook(jvmtiEnv * jvmti_env,JNIEnv * env,jclass class_being_redefined,jobject loader,const char * name,jobject protection_domain,jint class_data_len,const unsigned char * class_data,jint * new_class_data_len,unsigned char ** new_class_data)44*795d594fSAndroid Build Coastguard Worker void JNICALL CheckDexFileHook([[maybe_unused]] jvmtiEnv* jvmti_env,
45*795d594fSAndroid Build Coastguard Worker                               JNIEnv* env,
46*795d594fSAndroid Build Coastguard Worker                               jclass class_being_redefined,
47*795d594fSAndroid Build Coastguard Worker                               [[maybe_unused]] jobject loader,
48*795d594fSAndroid Build Coastguard Worker                               const char* name,
49*795d594fSAndroid Build Coastguard Worker                               [[maybe_unused]] jobject protection_domain,
50*795d594fSAndroid Build Coastguard Worker                               jint class_data_len,
51*795d594fSAndroid Build Coastguard Worker                               const unsigned char* class_data,
52*795d594fSAndroid Build Coastguard Worker                               [[maybe_unused]] jint* new_class_data_len,
53*795d594fSAndroid Build Coastguard Worker                               [[maybe_unused]] unsigned char** new_class_data) {
54*795d594fSAndroid Build Coastguard Worker   if (kSkipInitialLoad && class_being_redefined == nullptr) {
55*795d594fSAndroid Build Coastguard Worker     // Something got loaded concurrently. Just ignore it for now. To make sure the test is
56*795d594fSAndroid Build Coastguard Worker     // repeatable we only care about things that come from RetransformClasses.
57*795d594fSAndroid Build Coastguard Worker     return;
58*795d594fSAndroid Build Coastguard Worker   }
59*795d594fSAndroid Build Coastguard Worker   Println(env, android::base::StringPrintf("Dex file hook for %s", name).c_str());
60*795d594fSAndroid Build Coastguard Worker   if (IsJVM()) {
61*795d594fSAndroid Build Coastguard Worker     return;
62*795d594fSAndroid Build Coastguard Worker   }
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker   VerifyClassData(class_data_len, class_data);
65*795d594fSAndroid Build Coastguard Worker }
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker // Get all capabilities except those related to retransformation.
Java_art_Test983_setupLoadHook(JNIEnv * env,jclass)68*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Test983_setupLoadHook(JNIEnv* env, jclass) {
69*795d594fSAndroid Build Coastguard Worker   jvmtiEventCallbacks cb;
70*795d594fSAndroid Build Coastguard Worker   memset(&cb, 0, sizeof(cb));
71*795d594fSAndroid Build Coastguard Worker   cb.ClassFileLoadHook = CheckDexFileHook;
72*795d594fSAndroid Build Coastguard Worker   JvmtiErrorToException(env, jvmti_env, jvmti_env->SetEventCallbacks(&cb, sizeof(cb)));
73*795d594fSAndroid Build Coastguard Worker }
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker }  // namespace Test983SourceTransformVerify
76*795d594fSAndroid Build Coastguard Worker }  // namespace art
77