Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 17 | #include "dex_to_dex_decompiler.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 18 | |
| 19 | #include "class_linker.h" |
| 20 | #include "compiler/common_compiler_test.h" |
| 21 | #include "compiler/compiled_method.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 22 | #include "compiler/driver/compiler_driver.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include "compiler/driver/compiler_options.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 24 | #include "compiler_callbacks.h" |
| 25 | #include "dex_file.h" |
| 26 | #include "handle_scope-inl.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 27 | #include "mirror/class_loader.h" |
| 28 | #include "runtime.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 29 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 30 | #include "thread.h" |
| 31 | #include "verifier/method_verifier-inl.h" |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame^] | 32 | #include "verifier/verifier_deps.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
| 36 | class DexToDexDecompilerTest : public CommonCompilerTest { |
| 37 | public: |
| 38 | void CompileAll(jobject class_loader) REQUIRES(!Locks::mutator_lock_) { |
| 39 | TimingLogger timings("CompilerDriverTest::CompileAll", false, false); |
| 40 | TimingLogger::ScopedTiming t(__FUNCTION__, &timings); |
| 41 | compiler_options_->boot_image_ = false; |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 42 | compiler_options_->SetCompilerFilter(CompilerFilter::kQuicken); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame^] | 43 | // Create the main VerifierDeps, here instead of in the compiler since we want to aggregate |
| 44 | // the results for all the dex files, not just the results for the current dex file. |
| 45 | Runtime::Current()->GetCompilerCallbacks()->SetVerifierDeps( |
| 46 | new verifier::VerifierDeps(GetDexFiles(class_loader))); |
| 47 | compiler_driver_->SetDexFilesForOatFile(GetDexFiles(class_loader)); |
Nicolas Geoffray | 1cfea7a | 2017-05-24 14:44:38 +0100 | [diff] [blame] | 48 | compiler_driver_->CompileAll(class_loader, GetDexFiles(class_loader), &timings); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void RunTest(const char* dex_name) { |
| 52 | Thread* self = Thread::Current(); |
| 53 | // First load the original dex file. |
| 54 | jobject original_class_loader; |
| 55 | { |
| 56 | ScopedObjectAccess soa(self); |
| 57 | original_class_loader = LoadDex(dex_name); |
| 58 | } |
| 59 | const DexFile* original_dex_file = GetDexFiles(original_class_loader)[0]; |
| 60 | |
| 61 | // Load the dex file again and make it writable to quicken them. |
| 62 | jobject class_loader; |
| 63 | const DexFile* updated_dex_file = nullptr; |
| 64 | { |
| 65 | ScopedObjectAccess soa(self); |
| 66 | class_loader = LoadDex(dex_name); |
| 67 | updated_dex_file = GetDexFiles(class_loader)[0]; |
| 68 | Runtime::Current()->GetClassLinker()->RegisterDexFile( |
| 69 | *updated_dex_file, soa.Decode<mirror::ClassLoader>(class_loader).Ptr()); |
| 70 | } |
| 71 | // The dex files should be identical. |
| 72 | int cmp = memcmp(original_dex_file->Begin(), |
| 73 | updated_dex_file->Begin(), |
| 74 | updated_dex_file->Size()); |
| 75 | ASSERT_EQ(0, cmp); |
| 76 | |
| 77 | updated_dex_file->EnableWrite(); |
| 78 | CompileAll(class_loader); |
| 79 | // The dex files should be different after quickening. |
| 80 | cmp = memcmp(original_dex_file->Begin(), updated_dex_file->Begin(), updated_dex_file->Size()); |
| 81 | ASSERT_NE(0, cmp); |
| 82 | |
| 83 | // Unquicken the dex file. |
| 84 | for (uint32_t i = 0; i < updated_dex_file->NumClassDefs(); ++i) { |
| 85 | const DexFile::ClassDef& class_def = updated_dex_file->GetClassDef(i); |
| 86 | const uint8_t* class_data = updated_dex_file->GetClassData(class_def); |
| 87 | if (class_data == nullptr) { |
| 88 | continue; |
| 89 | } |
| 90 | ClassDataItemIterator it(*updated_dex_file, class_data); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 91 | it.SkipAllFields(); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 92 | |
| 93 | // Unquicken each method. |
| 94 | while (it.HasNextDirectMethod()) { |
| 95 | uint32_t method_idx = it.GetMemberIndex(); |
| 96 | CompiledMethod* compiled_method = |
| 97 | compiler_driver_->GetCompiledMethod(MethodReference(updated_dex_file, method_idx)); |
| 98 | ArrayRef<const uint8_t> table; |
| 99 | if (compiled_method != nullptr) { |
| 100 | table = compiled_method->GetVmapTable(); |
| 101 | } |
Nicolas Geoffray | b1677e2 | 2016-12-16 16:23:16 +0000 | [diff] [blame] | 102 | optimizer::ArtDecompileDEX( |
| 103 | *it.GetMethodCodeItem(), table, /* decompile_return_instruction */ true); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 104 | it.Next(); |
| 105 | } |
| 106 | while (it.HasNextVirtualMethod()) { |
| 107 | uint32_t method_idx = it.GetMemberIndex(); |
| 108 | CompiledMethod* compiled_method = |
| 109 | compiler_driver_->GetCompiledMethod(MethodReference(updated_dex_file, method_idx)); |
| 110 | ArrayRef<const uint8_t> table; |
| 111 | if (compiled_method != nullptr) { |
| 112 | table = compiled_method->GetVmapTable(); |
| 113 | } |
Nicolas Geoffray | b1677e2 | 2016-12-16 16:23:16 +0000 | [diff] [blame] | 114 | optimizer::ArtDecompileDEX( |
| 115 | *it.GetMethodCodeItem(), table, /* decompile_return_instruction */ true); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 116 | it.Next(); |
| 117 | } |
| 118 | DCHECK(!it.HasNext()); |
| 119 | } |
| 120 | |
| 121 | // Make sure after unquickening we go back to the same contents as the original dex file. |
| 122 | cmp = memcmp(original_dex_file->Begin(), updated_dex_file->Begin(), updated_dex_file->Size()); |
| 123 | ASSERT_EQ(0, cmp); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | TEST_F(DexToDexDecompilerTest, VerifierDeps) { |
| 128 | RunTest("VerifierDeps"); |
| 129 | } |
| 130 | |
| 131 | TEST_F(DexToDexDecompilerTest, DexToDexDecompiler) { |
| 132 | RunTest("DexToDexDecompiler"); |
| 133 | } |
| 134 | |
| 135 | } // namespace art |