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" |
Andreas Gampe | 2c30e4a | 2017-08-23 11:31:32 -0700 | [diff] [blame] | 20 | #include "common_compiler_test.h" |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 21 | #include "compiled_method-inl.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 22 | #include "compiler_callbacks.h" |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 23 | #include "dex/class_accessor-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 24 | #include "dex/dex_file.h" |
Andreas Gampe | 2c30e4a | 2017-08-23 11:31:32 -0700 | [diff] [blame] | 25 | #include "driver/compiler_driver.h" |
| 26 | #include "driver/compiler_options.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 27 | #include "handle_scope-inl.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 28 | #include "mirror/class_loader.h" |
| 29 | #include "runtime.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 30 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 31 | #include "thread.h" |
| 32 | #include "verifier/method_verifier-inl.h" |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 33 | #include "verifier/verifier_deps.h" |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | |
| 37 | class DexToDexDecompilerTest : public CommonCompilerTest { |
| 38 | public: |
| 39 | void CompileAll(jobject class_loader) REQUIRES(!Locks::mutator_lock_) { |
| 40 | TimingLogger timings("CompilerDriverTest::CompileAll", false, false); |
| 41 | TimingLogger::ScopedTiming t(__FUNCTION__, &timings); |
| 42 | compiler_options_->boot_image_ = false; |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 43 | compiler_options_->SetCompilerFilter(CompilerFilter::kQuicken); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 44 | // Create the main VerifierDeps, here instead of in the compiler since we want to aggregate |
| 45 | // the results for all the dex files, not just the results for the current dex file. |
| 46 | Runtime::Current()->GetCompilerCallbacks()->SetVerifierDeps( |
| 47 | new verifier::VerifierDeps(GetDexFiles(class_loader))); |
| 48 | compiler_driver_->SetDexFilesForOatFile(GetDexFiles(class_loader)); |
Nicolas Geoffray | 1cfea7a | 2017-05-24 14:44:38 +0100 | [diff] [blame] | 49 | compiler_driver_->CompileAll(class_loader, GetDexFiles(class_loader), &timings); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void RunTest(const char* dex_name) { |
| 53 | Thread* self = Thread::Current(); |
| 54 | // First load the original dex file. |
| 55 | jobject original_class_loader; |
| 56 | { |
| 57 | ScopedObjectAccess soa(self); |
| 58 | original_class_loader = LoadDex(dex_name); |
| 59 | } |
| 60 | const DexFile* original_dex_file = GetDexFiles(original_class_loader)[0]; |
| 61 | |
| 62 | // Load the dex file again and make it writable to quicken them. |
| 63 | jobject class_loader; |
| 64 | const DexFile* updated_dex_file = nullptr; |
| 65 | { |
| 66 | ScopedObjectAccess soa(self); |
| 67 | class_loader = LoadDex(dex_name); |
| 68 | updated_dex_file = GetDexFiles(class_loader)[0]; |
| 69 | Runtime::Current()->GetClassLinker()->RegisterDexFile( |
| 70 | *updated_dex_file, soa.Decode<mirror::ClassLoader>(class_loader).Ptr()); |
| 71 | } |
| 72 | // The dex files should be identical. |
| 73 | int cmp = memcmp(original_dex_file->Begin(), |
| 74 | updated_dex_file->Begin(), |
| 75 | updated_dex_file->Size()); |
| 76 | ASSERT_EQ(0, cmp); |
| 77 | |
| 78 | updated_dex_file->EnableWrite(); |
| 79 | CompileAll(class_loader); |
| 80 | // The dex files should be different after quickening. |
| 81 | cmp = memcmp(original_dex_file->Begin(), updated_dex_file->Begin(), updated_dex_file->Size()); |
| 82 | ASSERT_NE(0, cmp); |
| 83 | |
| 84 | // Unquicken the dex file. |
| 85 | for (uint32_t i = 0; i < updated_dex_file->NumClassDefs(); ++i) { |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 86 | // Unquicken each method. |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 87 | ClassAccessor accessor(*updated_dex_file, updated_dex_file->GetClassDef(i)); |
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, |
| 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 |