Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 59484b9 | 2018-02-14 14:00:46 -0800 | [diff] [blame] | 17 | #include "source_transform.h" |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | 59484b9 | 2018-02-14 14:00:46 -0800 | [diff] [blame] | 19 | #include "jni.h" |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 20 | |
| 21 | #include "android-base/stringprintf.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 22 | #include "jvmti.h" |
Alex Light | 8fd0856 | 2018-02-13 10:09:03 -0800 | [diff] [blame] | 23 | #include "scoped_local_ref.h" |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 24 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 25 | // Test infrastructure |
| 26 | #include "jvmti_helper.h" |
| 27 | #include "test_env.h" |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | namespace Test983SourceTransformVerify { |
| 31 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 32 | constexpr bool kSkipInitialLoad = true; |
| 33 | |
Andreas Gampe | 59484b9 | 2018-02-14 14:00:46 -0800 | [diff] [blame] | 34 | static void Println(JNIEnv* env, const char* msg) { |
Alex Light | 8fd0856 | 2018-02-13 10:09:03 -0800 | [diff] [blame] | 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"); |
Andreas Gampe | 59484b9 | 2018-02-14 14:00:46 -0800 | [diff] [blame] | 39 | ScopedLocalRef<jstring> data(env, env->NewStringUTF(msg)); |
Alex Light | 8fd0856 | 2018-02-13 10:09:03 -0800 | [diff] [blame] | 40 | env->CallStaticVoidMethod(test_klass.get(), println_method, data.get()); |
| 41 | } |
| 42 | |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 43 | // The hook we are using. |
| 44 | void JNICALL CheckDexFileHook(jvmtiEnv* jvmti_env ATTRIBUTE_UNUSED, |
Alex Light | 8fd0856 | 2018-02-13 10:09:03 -0800 | [diff] [blame] | 45 | JNIEnv* env, |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 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) { |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 54 | if (kSkipInitialLoad && class_being_redefined == nullptr) { |
Alex Light | 8fd0856 | 2018-02-13 10:09:03 -0800 | [diff] [blame] | 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. |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 57 | return; |
| 58 | } |
Andreas Gampe | 59484b9 | 2018-02-14 14:00:46 -0800 | [diff] [blame] | 59 | Println(env, android::base::StringPrintf("Dex file hook for %s", name).c_str()); |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 60 | if (IsJVM()) { |
| 61 | return; |
| 62 | } |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 63 | |
Andreas Gampe | 59484b9 | 2018-02-14 14:00:46 -0800 | [diff] [blame] | 64 | VerifyClassData(class_data_len, class_data); |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Get all capabilities except those related to retransformation. |
Alex Light | 8fd0856 | 2018-02-13 10:09:03 -0800 | [diff] [blame] | 68 | extern "C" JNIEXPORT void JNICALL Java_art_Test983_setupLoadHook(JNIEnv* env, jclass) { |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 69 | jvmtiEventCallbacks cb; |
| 70 | memset(&cb, 0, sizeof(cb)); |
| 71 | cb.ClassFileLoadHook = CheckDexFileHook; |
Alex Light | 8fd0856 | 2018-02-13 10:09:03 -0800 | [diff] [blame] | 72 | JvmtiErrorToException(env, jvmti_env, jvmti_env->SetEventCallbacks(&cb, sizeof(cb))); |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 75 | } // namespace Test983SourceTransformVerify |
| 76 | } // namespace art |