blob: 57cef3de47651af91cda740b937e4bc9c718ffc5 [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
Andreas Gampe5678db52017-06-08 14:11:18 -070026#include "base/stl_util.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070027#include "common_runtime_test.h"
28#include "compiler_callbacks.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070029#include "dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080030#include "exec_utils.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070031#include "gc/heap.h"
32#include "gc/space/image_space.h"
33#include "oat_file_assistant.h"
34#include "os.h"
35#include "runtime.h"
36#include "utils.h"
37
38namespace art {
39
40// Test class that provides some helpers to set a test up for compilation using dex2oat.
41class Dex2oatEnvironmentTest : public CommonRuntimeTest {
42 public:
43 virtual void SetUp() OVERRIDE {
44 CommonRuntimeTest::SetUp();
45
46 // Create a scratch directory to work from.
Calin Juravle357c66d2017-05-04 01:57:17 +000047
48 // Get the realpath of the android data. The oat dir should always point to real location
49 // when generating oat files in dalvik-cache. This avoids complicating the unit tests
50 // when matching the expected paths.
51 UniqueCPtr<const char[]> android_data_real(realpath(android_data_.c_str(), nullptr));
52 ASSERT_TRUE(android_data_real != nullptr)
53 << "Could not get the realpath of the android data" << android_data_ << strerror(errno);
54
55 scratch_dir_.assign(android_data_real.get());
56 scratch_dir_ += "/Dex2oatEnvironmentTest";
Andreas Gampee1459ae2016-06-29 09:36:30 -070057 ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700));
58
59 // Create a subdirectory in scratch for odex files.
60 odex_oat_dir_ = scratch_dir_ + "/oat";
61 ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700));
62
63 odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA));
64 ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700));
65
66 // Verify the environment is as we expect
Richard Uhler84f50ae2017-02-06 15:12:45 +000067 std::vector<uint32_t> checksums;
Andreas Gampee1459ae2016-06-29 09:36:30 -070068 std::string error_msg;
69 ASSERT_TRUE(OS::FileExists(GetSystemImageFile().c_str()))
70 << "Expected pre-compiled boot image to be at: " << GetSystemImageFile();
71 ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str()))
72 << "Expected dex file to be at: " << GetDexSrc1();
73 ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str()))
74 << "Expected stripped dex file to be at: " << GetStrippedDexSrc1();
Mathieu Chartier79c87da2017-10-10 11:54:29 -070075 ASSERT_FALSE(
76 DexFileLoader::GetMultiDexChecksums(GetStrippedDexSrc1().c_str(), &checksums, &error_msg))
Andreas Gampee1459ae2016-06-29 09:36:30 -070077 << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1();
78 ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str()))
79 << "Expected dex file to be at: " << GetDexSrc2();
80
81 // GetMultiDexSrc2 should have the same primary dex checksum as
82 // GetMultiDexSrc1, but a different secondary dex checksum.
83 static constexpr bool kVerifyChecksum = true;
84 std::vector<std::unique_ptr<const DexFile>> multi1;
Mathieu Chartier79c87da2017-10-10 11:54:29 -070085 ASSERT_TRUE(DexFileLoader::Open(GetMultiDexSrc1().c_str(),
86 GetMultiDexSrc1().c_str(),
Nicolas Geoffray095c6c92017-10-19 13:59:55 +010087 /* verify */ true,
Mathieu Chartier79c87da2017-10-10 11:54:29 -070088 kVerifyChecksum,
89 &error_msg,
90 &multi1)) << error_msg;
Andreas Gampee1459ae2016-06-29 09:36:30 -070091 ASSERT_GT(multi1.size(), 1u);
92
93 std::vector<std::unique_ptr<const DexFile>> multi2;
Mathieu Chartier79c87da2017-10-10 11:54:29 -070094 ASSERT_TRUE(DexFileLoader::Open(GetMultiDexSrc2().c_str(),
95 GetMultiDexSrc2().c_str(),
Nicolas Geoffray095c6c92017-10-19 13:59:55 +010096 /* verify */ true,
Mathieu Chartier79c87da2017-10-10 11:54:29 -070097 kVerifyChecksum,
98 &error_msg,
99 &multi2)) << error_msg;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700100 ASSERT_GT(multi2.size(), 1u);
101
102 ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum());
103 ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum());
104 }
105
106 virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
107 // options->push_back(std::make_pair("-verbose:oat", nullptr));
108
109 // Set up the image location.
110 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
111 nullptr));
112 // Make sure compilercallbacks are not set so that relocation will be
113 // enabled.
114 callbacks_.reset();
115 }
116
117 virtual void TearDown() OVERRIDE {
118 ClearDirectory(odex_dir_.c_str());
119 ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
120
121 ClearDirectory(odex_oat_dir_.c_str());
122 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
123
124 ClearDirectory(scratch_dir_.c_str());
125 ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
126
127 CommonRuntimeTest::TearDown();
128 }
129
130 static void Copy(const std::string& src, const std::string& dst) {
131 std::ifstream src_stream(src, std::ios::binary);
132 std::ofstream dst_stream(dst, std::ios::binary);
133
134 dst_stream << src_stream.rdbuf();
135 }
136
137 // Returns the directory where the pre-compiled core.art can be found.
138 // TODO: We should factor out this into common tests somewhere rather than
139 // re-hardcoding it here (This was copied originally from the elf writer
140 // test).
141 std::string GetImageDirectory() const {
142 if (IsHost()) {
143 const char* host_dir = getenv("ANDROID_HOST_OUT");
144 CHECK(host_dir != nullptr);
145 return std::string(host_dir) + "/framework";
146 } else {
147 return std::string("/data/art-test");
148 }
149 }
150
151 std::string GetImageLocation() const {
152 return GetImageDirectory() + "/core.art";
153 }
154
155 std::string GetSystemImageFile() const {
156 return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA)
157 + "/core.art";
158 }
159
Richard Uhler03bc6592016-11-22 09:42:04 +0000160 bool GetCachedImageFile(const std::string& image_location,
161 /*out*/std::string* image,
162 /*out*/std::string* error_msg) const {
Richard Uhler55b58b62016-08-12 09:05:13 -0700163 std::string cache;
164 bool have_android_data;
165 bool dalvik_cache_exists;
166 bool is_global_cache;
167 GetDalvikCache(GetInstructionSetString(kRuntimeISA),
168 true,
169 &cache,
170 &have_android_data,
171 &dalvik_cache_exists,
172 &is_global_cache);
173 if (!dalvik_cache_exists) {
174 *error_msg = "Failed to create dalvik cache";
175 return false;
176 }
Richard Uhler03bc6592016-11-22 09:42:04 +0000177 return GetDalvikCacheFilename(image_location.c_str(), cache.c_str(), image, error_msg);
178 }
179
180 // Returns the path to an image location whose contents differ from the
181 // image at GetImageLocation(). This is used for testing mismatched
182 // image checksums in the oat_file_assistant_tests.
183 std::string GetImageLocation2() const {
Richard Uhlercdcbddf2017-01-19 16:58:39 +0000184 return GetImageDirectory() + "/core-interpreter.art";
Andreas Gampee1459ae2016-06-29 09:36:30 -0700185 }
186
187 std::string GetDexSrc1() const {
188 return GetTestDexFileName("Main");
189 }
190
191 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
192 // file stripped.
193 std::string GetStrippedDexSrc1() const {
194 return GetTestDexFileName("MainStripped");
195 }
196
197 std::string GetMultiDexSrc1() const {
198 return GetTestDexFileName("MultiDex");
199 }
200
201 // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but
202 // with the contents of the secondary dex file changed.
203 std::string GetMultiDexSrc2() const {
204 return GetTestDexFileName("MultiDexModifiedSecondary");
205 }
206
207 std::string GetDexSrc2() const {
208 return GetTestDexFileName("Nested");
209 }
210
211 // Scratch directory, for dex and odex files (oat files will go in the
212 // dalvik cache).
213 const std::string& GetScratchDir() const {
214 return scratch_dir_;
215 }
216
217 // Odex directory is the subdirectory in the scratch directory where odex
218 // files should be located.
219 const std::string& GetOdexDir() const {
220 return odex_dir_;
221 }
222
223 private:
224 std::string scratch_dir_;
225 std::string odex_oat_dir_;
226 std::string odex_dir_;
227};
228
229} // namespace art
230
231#endif // ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_