blob: a9bb95480effc594e3ef2fca3addbbbb2a9d3591 [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(),
87 kVerifyChecksum,
88 &error_msg,
89 &multi1)) << error_msg;
Andreas Gampee1459ae2016-06-29 09:36:30 -070090 ASSERT_GT(multi1.size(), 1u);
91
92 std::vector<std::unique_ptr<const DexFile>> multi2;
Mathieu Chartier79c87da2017-10-10 11:54:29 -070093 ASSERT_TRUE(DexFileLoader::Open(GetMultiDexSrc2().c_str(),
94 GetMultiDexSrc2().c_str(),
95 kVerifyChecksum,
96 &error_msg,
97 &multi2)) << error_msg;
Andreas Gampee1459ae2016-06-29 09:36:30 -070098 ASSERT_GT(multi2.size(), 1u);
99
100 ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum());
101 ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum());
102 }
103
104 virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
105 // options->push_back(std::make_pair("-verbose:oat", nullptr));
106
107 // Set up the image location.
108 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
109 nullptr));
110 // Make sure compilercallbacks are not set so that relocation will be
111 // enabled.
112 callbacks_.reset();
113 }
114
115 virtual void TearDown() OVERRIDE {
116 ClearDirectory(odex_dir_.c_str());
117 ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
118
119 ClearDirectory(odex_oat_dir_.c_str());
120 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
121
122 ClearDirectory(scratch_dir_.c_str());
123 ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
124
125 CommonRuntimeTest::TearDown();
126 }
127
128 static void Copy(const std::string& src, const std::string& dst) {
129 std::ifstream src_stream(src, std::ios::binary);
130 std::ofstream dst_stream(dst, std::ios::binary);
131
132 dst_stream << src_stream.rdbuf();
133 }
134
135 // Returns the directory where the pre-compiled core.art can be found.
136 // TODO: We should factor out this into common tests somewhere rather than
137 // re-hardcoding it here (This was copied originally from the elf writer
138 // test).
139 std::string GetImageDirectory() const {
140 if (IsHost()) {
141 const char* host_dir = getenv("ANDROID_HOST_OUT");
142 CHECK(host_dir != nullptr);
143 return std::string(host_dir) + "/framework";
144 } else {
145 return std::string("/data/art-test");
146 }
147 }
148
149 std::string GetImageLocation() const {
150 return GetImageDirectory() + "/core.art";
151 }
152
153 std::string GetSystemImageFile() const {
154 return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA)
155 + "/core.art";
156 }
157
Richard Uhler03bc6592016-11-22 09:42:04 +0000158 bool GetCachedImageFile(const std::string& image_location,
159 /*out*/std::string* image,
160 /*out*/std::string* error_msg) const {
Richard Uhler55b58b62016-08-12 09:05:13 -0700161 std::string cache;
162 bool have_android_data;
163 bool dalvik_cache_exists;
164 bool is_global_cache;
165 GetDalvikCache(GetInstructionSetString(kRuntimeISA),
166 true,
167 &cache,
168 &have_android_data,
169 &dalvik_cache_exists,
170 &is_global_cache);
171 if (!dalvik_cache_exists) {
172 *error_msg = "Failed to create dalvik cache";
173 return false;
174 }
Richard Uhler03bc6592016-11-22 09:42:04 +0000175 return GetDalvikCacheFilename(image_location.c_str(), cache.c_str(), image, error_msg);
176 }
177
178 // Returns the path to an image location whose contents differ from the
179 // image at GetImageLocation(). This is used for testing mismatched
180 // image checksums in the oat_file_assistant_tests.
181 std::string GetImageLocation2() const {
Richard Uhlercdcbddf2017-01-19 16:58:39 +0000182 return GetImageDirectory() + "/core-interpreter.art";
Andreas Gampee1459ae2016-06-29 09:36:30 -0700183 }
184
185 std::string GetDexSrc1() const {
186 return GetTestDexFileName("Main");
187 }
188
189 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
190 // file stripped.
191 std::string GetStrippedDexSrc1() const {
192 return GetTestDexFileName("MainStripped");
193 }
194
195 std::string GetMultiDexSrc1() const {
196 return GetTestDexFileName("MultiDex");
197 }
198
199 // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but
200 // with the contents of the secondary dex file changed.
201 std::string GetMultiDexSrc2() const {
202 return GetTestDexFileName("MultiDexModifiedSecondary");
203 }
204
205 std::string GetDexSrc2() const {
206 return GetTestDexFileName("Nested");
207 }
208
209 // Scratch directory, for dex and odex files (oat files will go in the
210 // dalvik cache).
211 const std::string& GetScratchDir() const {
212 return scratch_dir_;
213 }
214
215 // Odex directory is the subdirectory in the scratch directory where odex
216 // files should be located.
217 const std::string& GetOdexDir() const {
218 return odex_dir_;
219 }
220
221 private:
222 std::string scratch_dir_;
223 std::string odex_oat_dir_;
224 std::string odex_dir_;
225};
226
227} // namespace art
228
229#endif // ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_