blob: 0d74dbb936d57372abbae1689ae9fb575097b35f [file] [log] [blame]
Andreas Gampee1459ae2016-06-29 09:36:30 -07001/*
2 * Copyright (C) 2014 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#ifndef ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_
18#define ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_
19
20#include <fstream>
21#include <string>
22#include <vector>
23
24#include <gtest/gtest.h>
25
David Sehr891a50e2017-10-27 17:01:07 -070026#include "base/file_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080027#include "base/os.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070028#include "base/stl_util.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/utils.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070030#include "common_runtime_test.h"
31#include "compiler_callbacks.h"
David Sehr013fd802018-01-11 22:55:24 -080032#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080034#include "exec_utils.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070035#include "gc/heap.h"
36#include "gc/space/image_space.h"
37#include "oat_file_assistant.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070038#include "runtime.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070039
40namespace art {
41
42// Test class that provides some helpers to set a test up for compilation using dex2oat.
43class Dex2oatEnvironmentTest : public CommonRuntimeTest {
44 public:
Roland Levillainf73caca2018-08-24 17:19:07 +010045 void SetUp() override {
Andreas Gampee1459ae2016-06-29 09:36:30 -070046 CommonRuntimeTest::SetUp();
David Sehr013fd802018-01-11 22:55:24 -080047 const ArtDexFileLoader dex_file_loader;
Andreas Gampee1459ae2016-06-29 09:36:30 -070048
49 // Create a scratch directory to work from.
Calin Juravle357c66d2017-05-04 01:57:17 +000050
51 // Get the realpath of the android data. The oat dir should always point to real location
52 // when generating oat files in dalvik-cache. This avoids complicating the unit tests
53 // when matching the expected paths.
54 UniqueCPtr<const char[]> android_data_real(realpath(android_data_.c_str(), nullptr));
55 ASSERT_TRUE(android_data_real != nullptr)
56 << "Could not get the realpath of the android data" << android_data_ << strerror(errno);
57
58 scratch_dir_.assign(android_data_real.get());
59 scratch_dir_ += "/Dex2oatEnvironmentTest";
Andreas Gampee1459ae2016-06-29 09:36:30 -070060 ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700));
61
62 // Create a subdirectory in scratch for odex files.
63 odex_oat_dir_ = scratch_dir_ + "/oat";
64 ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700));
65
66 odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA));
67 ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700));
68
69 // Verify the environment is as we expect
Richard Uhler84f50ae2017-02-06 15:12:45 +000070 std::vector<uint32_t> checksums;
Andreas Gampee1459ae2016-06-29 09:36:30 -070071 std::string error_msg;
72 ASSERT_TRUE(OS::FileExists(GetSystemImageFile().c_str()))
73 << "Expected pre-compiled boot image to be at: " << GetSystemImageFile();
74 ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str()))
75 << "Expected dex file to be at: " << GetDexSrc1();
76 ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str()))
77 << "Expected stripped dex file to be at: " << GetStrippedDexSrc1();
Mathieu Chartier79c87da2017-10-10 11:54:29 -070078 ASSERT_FALSE(
David Sehr013fd802018-01-11 22:55:24 -080079 dex_file_loader.GetMultiDexChecksums(GetStrippedDexSrc1().c_str(), &checksums, &error_msg))
Andreas Gampee1459ae2016-06-29 09:36:30 -070080 << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1();
81 ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str()))
82 << "Expected dex file to be at: " << GetDexSrc2();
83
84 // GetMultiDexSrc2 should have the same primary dex checksum as
85 // GetMultiDexSrc1, but a different secondary dex checksum.
86 static constexpr bool kVerifyChecksum = true;
87 std::vector<std::unique_ptr<const DexFile>> multi1;
David Sehr013fd802018-01-11 22:55:24 -080088 ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc1().c_str(),
89 GetMultiDexSrc1().c_str(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -070090 /* verify= */ true,
David Sehr013fd802018-01-11 22:55:24 -080091 kVerifyChecksum,
92 &error_msg,
93 &multi1)) << error_msg;
Andreas Gampee1459ae2016-06-29 09:36:30 -070094 ASSERT_GT(multi1.size(), 1u);
95
96 std::vector<std::unique_ptr<const DexFile>> multi2;
David Sehr013fd802018-01-11 22:55:24 -080097 ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc2().c_str(),
98 GetMultiDexSrc2().c_str(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -070099 /* verify= */ true,
David Sehr013fd802018-01-11 22:55:24 -0800100 kVerifyChecksum,
101 &error_msg,
102 &multi2)) << error_msg;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700103 ASSERT_GT(multi2.size(), 1u);
104
105 ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum());
106 ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum());
107 }
108
Roland Levillainf73caca2018-08-24 17:19:07 +0100109 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700110 // options->push_back(std::make_pair("-verbose:oat", nullptr));
111
112 // Set up the image location.
113 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
114 nullptr));
115 // Make sure compilercallbacks are not set so that relocation will be
116 // enabled.
117 callbacks_.reset();
118 }
119
Roland Levillainf73caca2018-08-24 17:19:07 +0100120 void TearDown() override {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700121 ClearDirectory(odex_dir_.c_str());
122 ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
123
124 ClearDirectory(odex_oat_dir_.c_str());
125 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
126
127 ClearDirectory(scratch_dir_.c_str());
128 ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
129
130 CommonRuntimeTest::TearDown();
131 }
132
133 static void Copy(const std::string& src, const std::string& dst) {
134 std::ifstream src_stream(src, std::ios::binary);
135 std::ofstream dst_stream(dst, std::ios::binary);
136
137 dst_stream << src_stream.rdbuf();
138 }
139
Richard Uhler03bc6592016-11-22 09:42:04 +0000140 // Returns the path to an image location whose contents differ from the
141 // image at GetImageLocation(). This is used for testing mismatched
142 // image checksums in the oat_file_assistant_tests.
143 std::string GetImageLocation2() const {
Richard Uhlercdcbddf2017-01-19 16:58:39 +0000144 return GetImageDirectory() + "/core-interpreter.art";
Andreas Gampee1459ae2016-06-29 09:36:30 -0700145 }
146
147 std::string GetDexSrc1() const {
148 return GetTestDexFileName("Main");
149 }
150
151 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
152 // file stripped.
153 std::string GetStrippedDexSrc1() const {
154 return GetTestDexFileName("MainStripped");
155 }
156
157 std::string GetMultiDexSrc1() const {
158 return GetTestDexFileName("MultiDex");
159 }
160
161 // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but
162 // with the contents of the secondary dex file changed.
163 std::string GetMultiDexSrc2() const {
164 return GetTestDexFileName("MultiDexModifiedSecondary");
165 }
166
167 std::string GetDexSrc2() const {
168 return GetTestDexFileName("Nested");
169 }
170
171 // Scratch directory, for dex and odex files (oat files will go in the
172 // dalvik cache).
173 const std::string& GetScratchDir() const {
174 return scratch_dir_;
175 }
176
177 // Odex directory is the subdirectory in the scratch directory where odex
178 // files should be located.
179 const std::string& GetOdexDir() const {
180 return odex_dir_;
181 }
182
183 private:
184 std::string scratch_dir_;
185 std::string odex_oat_dir_;
186 std::string odex_dir_;
187};
188
189} // namespace art
190
191#endif // ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_