blob: 75de2382115b4f946de3c7786d411cfa310dd855 [file] [log] [blame]
Nicolas Geoffray01b70e82016-11-17 10:58:36 +00001/*
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 Geoffray4e868fa2017-04-21 17:16:44 +010017#include "dex_to_dex_decompiler.h"
Nicolas Geoffray01b70e82016-11-17 10:58:36 +000018
19#include "class_linker.h"
Andreas Gampe2c30e4a2017-08-23 11:31:32 -070020#include "common_compiler_test.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010021#include "compiled_method-inl.h"
Nicolas Geoffray01b70e82016-11-17 10:58:36 +000022#include "compiler_callbacks.h"
Mathieu Chartierc2b4db62018-05-18 13:58:12 -070023#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080024#include "dex/dex_file.h"
Andreas Gampe2c30e4a2017-08-23 11:31:32 -070025#include "driver/compiler_driver.h"
26#include "driver/compiler_options.h"
Nicolas Geoffray01b70e82016-11-17 10:58:36 +000027#include "handle_scope-inl.h"
Nicolas Geoffray01b70e82016-11-17 10:58:36 +000028#include "mirror/class_loader.h"
29#include "runtime.h"
Nicolas Geoffray01b70e82016-11-17 10:58:36 +000030#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "thread.h"
32#include "verifier/method_verifier-inl.h"
Mathieu Chartier72041a02017-07-14 18:23:25 -070033#include "verifier/verifier_deps.h"
Nicolas Geoffray01b70e82016-11-17 10:58:36 +000034
35namespace art {
36
37class 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 Geoffray49cda062017-04-21 13:08:25 +010043 compiler_options_->SetCompilerFilter(CompilerFilter::kQuicken);
Mathieu Chartier72041a02017-07-14 18:23:25 -070044 // 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 Geoffray1cfea7a2017-05-24 14:44:38 +010049 compiler_driver_->CompileAll(class_loader, GetDexFiles(class_loader), &timings);
Nicolas Geoffray01b70e82016-11-17 10:58:36 +000050 }
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 Geoffray01b70e82016-11-17 10:58:36 +000086 // Unquicken each method.
Mathieu Chartierc2b4db62018-05-18 13:58:12 -070087 ClassAccessor accessor(*updated_dex_file, updated_dex_file->GetClassDef(i));
Mathieu Chartier0d896bd2018-05-25 00:20:27 -070088 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
Mathieu Chartierc2b4db62018-05-18 13:58:12 -070089 CompiledMethod* compiled_method = compiler_driver_->GetCompiledMethod(
Mathieu Chartier0d896bd2018-05-25 00:20:27 -070090 method.GetReference());
Nicolas Geoffray01b70e82016-11-17 10:58:36 +000091 ArrayRef<const uint8_t> table;
92 if (compiled_method != nullptr) {
93 table = compiled_method->GetVmapTable();
94 }
Mathieu Chartier6238c832018-01-04 09:55:13 -080095 optimizer::ArtDecompileDEX(*updated_dex_file,
Mathieu Chartierc2b4db62018-05-18 13:58:12 -070096 *accessor.GetCodeItem(method),
Mathieu Chartier73f21d42018-01-02 14:26:50 -080097 table,
98 /* decompile_return_instruction */ true);
Mathieu Chartier0d896bd2018-05-25 00:20:27 -070099 }
Nicolas Geoffray01b70e82016-11-17 10:58:36 +0000100 }
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
108TEST_F(DexToDexDecompilerTest, VerifierDeps) {
109 RunTest("VerifierDeps");
110}
111
112TEST_F(DexToDexDecompilerTest, DexToDexDecompiler) {
113 RunTest("DexToDexDecompiler");
114}
115
116} // namespace art