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 | |
| 17 | #include <inttypes.h> |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 19 | #include <cstdio> |
| 20 | #include <cstring> |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "android-base/stringprintf.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include "jni.h" |
| 26 | #include "jvmti.h" |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 27 | |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 28 | #include "base/macros.h" |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 29 | #include "bytecode_utils.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 30 | #include "dex/code_item_accessors-inl.h" |
| 31 | #include "dex/dex_file.h" |
| 32 | #include "dex/dex_file_loader.h" |
| 33 | #include "dex/dex_instruction.h" |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 34 | #include "jit/jit.h" |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 35 | #include "native_stack_dump.h" |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 36 | #include "runtime.h" |
| 37 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 38 | #include "thread-current-inl.h" |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 39 | #include "thread_list.h" |
| 40 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 41 | // Test infrastructure |
| 42 | #include "jvmti_helper.h" |
| 43 | #include "test_env.h" |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 44 | |
| 45 | namespace art { |
| 46 | namespace Test983SourceTransformVerify { |
| 47 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 48 | constexpr bool kSkipInitialLoad = true; |
| 49 | |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 50 | // The hook we are using. |
| 51 | void JNICALL CheckDexFileHook(jvmtiEnv* jvmti_env ATTRIBUTE_UNUSED, |
| 52 | JNIEnv* jni_env ATTRIBUTE_UNUSED, |
| 53 | jclass class_being_redefined, |
| 54 | jobject loader ATTRIBUTE_UNUSED, |
| 55 | const char* name, |
| 56 | jobject protection_domain ATTRIBUTE_UNUSED, |
| 57 | jint class_data_len, |
| 58 | const unsigned char* class_data, |
| 59 | jint* new_class_data_len ATTRIBUTE_UNUSED, |
| 60 | unsigned char** new_class_data ATTRIBUTE_UNUSED) { |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 61 | if (kSkipInitialLoad && class_being_redefined == nullptr) { |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 62 | // Something got loaded concurrently. Just ignore it for now. |
| 63 | return; |
| 64 | } |
| 65 | std::cout << "Dex file hook for " << name << std::endl; |
| 66 | if (IsJVM()) { |
| 67 | return; |
| 68 | } |
| 69 | std::string error; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 70 | std::unique_ptr<const DexFile> dex(DexFileLoader::Open(class_data, |
| 71 | class_data_len, |
| 72 | "fake_location.dex", |
| 73 | /*location_checksum*/ 0, |
| 74 | /*oat_dex_file*/ nullptr, |
| 75 | /*verify*/ true, |
| 76 | /*verify_checksum*/ true, |
| 77 | &error)); |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 78 | if (dex.get() == nullptr) { |
| 79 | std::cout << "Failed to verify dex file for " << name << " because " << error << std::endl; |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | for (uint32_t i = 0; i < dex->NumClassDefs(); i++) { |
| 83 | const DexFile::ClassDef& def = dex->GetClassDef(i); |
| 84 | const uint8_t* data_item = dex->GetClassData(def); |
| 85 | if (data_item == nullptr) { |
| 86 | continue; |
| 87 | } |
| 88 | for (ClassDataItemIterator it(*dex, data_item); it.HasNext(); it.Next()) { |
| 89 | if (!it.IsAtMethod() || it.GetMethodCodeItem() == nullptr) { |
| 90 | continue; |
| 91 | } |
Mathieu Chartier | 73f21d4 | 2018-01-02 14:26:50 -0800 | [diff] [blame] | 92 | for (const DexInstructionPcPair& pair : |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 93 | art::CodeItemInstructionAccessor(*dex, it.GetMethodCodeItem())) { |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 94 | const Instruction& inst = pair.Inst(); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 95 | int forbiden_flags = (Instruction::kVerifyError | Instruction::kVerifyRuntimeOnly); |
| 96 | if (inst.Opcode() == Instruction::RETURN_VOID_NO_BARRIER || |
| 97 | (inst.GetVerifyExtraFlags() & forbiden_flags) != 0) { |
| 98 | std::cout << "Unexpected instruction found in " << dex->PrettyMethod(it.GetMemberIndex()) |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 99 | << " [Dex PC: 0x" << std::hex << pair.DexPc() << std::dec << "] : " |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 100 | << inst.DumpString(dex.get()) << std::endl; |
| 101 | continue; |
| 102 | } |
| 103 | } |
| 104 | } |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | // Get all capabilities except those related to retransformation. |
| 109 | jint OnLoad(JavaVM* vm, |
| 110 | char* options ATTRIBUTE_UNUSED, |
| 111 | void* reserved ATTRIBUTE_UNUSED) { |
| 112 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { |
| 113 | printf("Unable to get jvmti env!\n"); |
| 114 | return 1; |
| 115 | } |
Alex Light | 3d324fd | 2017-07-20 15:38:52 -0700 | [diff] [blame] | 116 | SetStandardCapabilities(jvmti_env); |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 117 | jvmtiEventCallbacks cb; |
| 118 | memset(&cb, 0, sizeof(cb)); |
| 119 | cb.ClassFileLoadHook = CheckDexFileHook; |
| 120 | if (jvmti_env->SetEventCallbacks(&cb, sizeof(cb)) != JVMTI_ERROR_NONE) { |
| 121 | printf("Unable to set class file load hook cb!\n"); |
| 122 | return 1; |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
Alex Light | 37aa4c9 | 2017-03-27 13:02:41 -0700 | [diff] [blame] | 127 | } // namespace Test983SourceTransformVerify |
| 128 | } // namespace art |