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