blob: 9e65a9964c037b8faf76e3eb23399574af77c8ca [file] [log] [blame]
Alex Light37aa4c92017-03-27 13:02:41 -07001/*
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
Andreas Gampe59484b92018-02-14 14:00:46 -080017#include "source_transform.h"
Alex Light37aa4c92017-03-27 13:02:41 -070018
Andreas Gampe59484b92018-02-14 14:00:46 -080019#include "jni.h"
Alex Light37aa4c92017-03-27 13:02:41 -070020
21#include "android-base/stringprintf.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include "jvmti.h"
Alex Light8fd08562018-02-13 10:09:03 -080023#include "scoped_local_ref.h"
Alex Light37aa4c92017-03-27 13:02:41 -070024
Andreas Gampe3f46c962017-03-30 10:26:59 -070025// Test infrastructure
26#include "jvmti_helper.h"
27#include "test_env.h"
Alex Light37aa4c92017-03-27 13:02:41 -070028
29namespace art {
30namespace Test983SourceTransformVerify {
31
Alex Light40528472017-03-28 09:07:36 -070032constexpr bool kSkipInitialLoad = true;
33
Andreas Gampe59484b92018-02-14 14:00:46 -080034static void Println(JNIEnv* env, const char* msg) {
Alex Light8fd08562018-02-13 10:09:03 -080035 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");
Andreas Gampe59484b92018-02-14 14:00:46 -080039 ScopedLocalRef<jstring> data(env, env->NewStringUTF(msg));
Alex Light8fd08562018-02-13 10:09:03 -080040 env->CallStaticVoidMethod(test_klass.get(), println_method, data.get());
41}
42
Alex Light37aa4c92017-03-27 13:02:41 -070043// The hook we are using.
44void JNICALL CheckDexFileHook(jvmtiEnv* jvmti_env ATTRIBUTE_UNUSED,
Alex Light8fd08562018-02-13 10:09:03 -080045 JNIEnv* env,
Alex Light37aa4c92017-03-27 13:02:41 -070046 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) {
Alex Light40528472017-03-28 09:07:36 -070054 if (kSkipInitialLoad && class_being_redefined == nullptr) {
Alex Light8fd08562018-02-13 10:09:03 -080055 // 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.
Alex Light37aa4c92017-03-27 13:02:41 -070057 return;
58 }
Andreas Gampe59484b92018-02-14 14:00:46 -080059 Println(env, android::base::StringPrintf("Dex file hook for %s", name).c_str());
Alex Light37aa4c92017-03-27 13:02:41 -070060 if (IsJVM()) {
61 return;
62 }
Alex Lightca97ada2018-02-02 09:25:31 -080063
Andreas Gampe59484b92018-02-14 14:00:46 -080064 VerifyClassData(class_data_len, class_data);
Alex Light37aa4c92017-03-27 13:02:41 -070065}
66
67// Get all capabilities except those related to retransformation.
Alex Light8fd08562018-02-13 10:09:03 -080068extern "C" JNIEXPORT void JNICALL Java_art_Test983_setupLoadHook(JNIEnv* env, jclass) {
Alex Light37aa4c92017-03-27 13:02:41 -070069 jvmtiEventCallbacks cb;
70 memset(&cb, 0, sizeof(cb));
71 cb.ClassFileLoadHook = CheckDexFileHook;
Alex Light8fd08562018-02-13 10:09:03 -080072 JvmtiErrorToException(env, jvmti_env, jvmti_env->SetEventCallbacks(&cb, sizeof(cb)));
Alex Light37aa4c92017-03-27 13:02:41 -070073}
74
Alex Light37aa4c92017-03-27 13:02:41 -070075} // namespace Test983SourceTransformVerify
76} // namespace art