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 | |
Andreas Gampe | dbf5403 | 2018-06-18 14:47:01 -0700 | [diff] [blame] | 19 | #include "base/casts.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 20 | #include "class_linker.h" |
Vladimir Marko | 815d5e5 | 2019-03-04 10:18:31 +0000 | [diff] [blame] | 21 | #include "common_compiler_driver_test.h" |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 22 | #include "compiled_method-inl.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 23 | #include "compiler_callbacks.h" |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 24 | #include "dex/class_accessor-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 25 | #include "dex/dex_file.h" |
Andreas Gampe | 2c30e4a | 2017-08-23 11:31:32 -0700 | [diff] [blame] | 26 | #include "driver/compiler_driver.h" |
| 27 | #include "driver/compiler_options.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 28 | #include "handle_scope-inl.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 29 | #include "mirror/class_loader.h" |
Andreas Gampe | dbf5403 | 2018-06-18 14:47:01 -0700 | [diff] [blame] | 30 | #include "quick_compiler_callbacks.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 31 | #include "runtime.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 32 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 33 | #include "thread.h" |
| 34 | #include "verifier/method_verifier-inl.h" |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 35 | #include "verifier/verifier_deps.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | |
Vladimir Marko | 815d5e5 | 2019-03-04 10:18:31 +0000 | [diff] [blame] | 39 | class DexToDexDecompilerTest : public CommonCompilerDriverTest { |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 40 | public: |
| 41 | void CompileAll(jobject class_loader) REQUIRES(!Locks::mutator_lock_) { |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 42 | TimingLogger timings("DexToDexDecompilerTest::CompileAll", false, false); |
Vladimir Marko | 9c4b970 | 2018-11-14 15:09:02 +0000 | [diff] [blame] | 43 | compiler_options_->image_type_ = CompilerOptions::ImageType::kNone; |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 44 | compiler_options_->SetCompilerFilter(CompilerFilter::kQuicken); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 45 | // Create the main VerifierDeps, here instead of in the compiler since we want to aggregate |
| 46 | // the results for all the dex files, not just the results for the current dex file. |
Andreas Gampe | dbf5403 | 2018-06-18 14:47:01 -0700 | [diff] [blame] | 47 | down_cast<QuickCompilerCallbacks*>(Runtime::Current()->GetCompilerCallbacks())->SetVerifierDeps( |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 48 | new verifier::VerifierDeps(GetDexFiles(class_loader))); |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 49 | std::vector<const DexFile*> dex_files = GetDexFiles(class_loader); |
Vladimir Marko | 815d5e5 | 2019-03-04 10:18:31 +0000 | [diff] [blame] | 50 | CommonCompilerDriverTest::CompileAll(class_loader, dex_files, &timings); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void RunTest(const char* dex_name) { |
| 54 | Thread* self = Thread::Current(); |
| 55 | // First load the original dex file. |
| 56 | jobject original_class_loader; |
| 57 | { |
| 58 | ScopedObjectAccess soa(self); |
| 59 | original_class_loader = LoadDex(dex_name); |
| 60 | } |
| 61 | const DexFile* original_dex_file = GetDexFiles(original_class_loader)[0]; |
| 62 | |
| 63 | // Load the dex file again and make it writable to quicken them. |
| 64 | jobject class_loader; |
| 65 | const DexFile* updated_dex_file = nullptr; |
| 66 | { |
| 67 | ScopedObjectAccess soa(self); |
| 68 | class_loader = LoadDex(dex_name); |
| 69 | updated_dex_file = GetDexFiles(class_loader)[0]; |
| 70 | Runtime::Current()->GetClassLinker()->RegisterDexFile( |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 71 | *updated_dex_file, soa.Decode<mirror::ClassLoader>(class_loader)); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 72 | } |
| 73 | // The dex files should be identical. |
| 74 | int cmp = memcmp(original_dex_file->Begin(), |
| 75 | updated_dex_file->Begin(), |
| 76 | updated_dex_file->Size()); |
| 77 | ASSERT_EQ(0, cmp); |
| 78 | |
| 79 | updated_dex_file->EnableWrite(); |
| 80 | CompileAll(class_loader); |
| 81 | // The dex files should be different after quickening. |
| 82 | cmp = memcmp(original_dex_file->Begin(), updated_dex_file->Begin(), updated_dex_file->Size()); |
| 83 | ASSERT_NE(0, cmp); |
| 84 | |
| 85 | // Unquicken the dex file. |
Mathieu Chartier | 18e2687 | 2018-06-04 17:19:02 -0700 | [diff] [blame] | 86 | for (ClassAccessor accessor : updated_dex_file->GetClasses()) { |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 87 | // Unquicken each method. |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 88 | for (const ClassAccessor::Method& method : accessor.GetMethods()) { |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 89 | CompiledMethod* compiled_method = compiler_driver_->GetCompiledMethod( |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 90 | method.GetReference()); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 91 | ArrayRef<const uint8_t> table; |
| 92 | if (compiled_method != nullptr) { |
| 93 | table = compiled_method->GetVmapTable(); |
| 94 | } |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 95 | optimizer::ArtDecompileDEX(*updated_dex_file, |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 96 | *accessor.GetCodeItem(method), |
Mathieu Chartier | 73f21d4 | 2018-01-02 14:26:50 -0800 | [diff] [blame] | 97 | table, |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 98 | /* decompile_return_instruction= */ true); |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 99 | } |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Make sure after unquickening we go back to the same contents as the original dex file. |
| 103 | cmp = memcmp(original_dex_file->Begin(), updated_dex_file->Begin(), updated_dex_file->Size()); |
| 104 | ASSERT_EQ(0, cmp); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | TEST_F(DexToDexDecompilerTest, VerifierDeps) { |
| 109 | RunTest("VerifierDeps"); |
| 110 | } |
| 111 | |
| 112 | TEST_F(DexToDexDecompilerTest, DexToDexDecompiler) { |
| 113 | RunTest("DexToDexDecompiler"); |
| 114 | } |
| 115 | |
| 116 | } // namespace art |