blob: 41af7110199273a77755d16320276071e4e74c82 [file] [log] [blame]
David Sehrd5f8de82018-04-27 14:12:03 -07001/*
2 * Copyright (C) 2018 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_LIBARTBASE_BASE_COMMON_ART_TEST_H_
18#define ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_
19
20#include <gtest/gtest.h>
21
Andreas Gampe38aa0b52018-07-10 23:26:55 -070022#include <functional>
David Sehrd5f8de82018-04-27 14:12:03 -070023#include <string>
24
Andreas Gampe38aa0b52018-07-10 23:26:55 -070025#include <sys/wait.h>
26
David Sehrd5f8de82018-04-27 14:12:03 -070027#include <android-base/logging.h>
28
Roland Levillainfb6a5c02019-03-29 20:20:16 +000029#include "base/file_utils.h"
David Sehrd5f8de82018-04-27 14:12:03 -070030#include "base/globals.h"
31#include "base/mutex.h"
32#include "base/os.h"
33#include "base/unix_file/fd_file.h"
34#include "dex/art_dex_file_loader.h"
35#include "dex/compact_dex_level.h"
Calin Juravle8b959952019-09-20 16:32:05 -040036#include "dex/compact_dex_file.h"
David Sehrd5f8de82018-04-27 14:12:03 -070037
38namespace art {
39
40using LogSeverity = android::base::LogSeverity;
41using ScopedLogSeverity = android::base::ScopedLogSeverity;
42
David Sehrd5f8de82018-04-27 14:12:03 -070043class DexFile;
44
45class ScratchFile {
46 public:
47 ScratchFile();
48
49 explicit ScratchFile(const std::string& filename);
50
51 ScratchFile(const ScratchFile& other, const char* suffix);
52
Andreas Gampe44b31742018-10-01 19:30:57 -070053 ScratchFile(ScratchFile&& other) noexcept;
David Sehrd5f8de82018-04-27 14:12:03 -070054
Andreas Gampe44b31742018-10-01 19:30:57 -070055 ScratchFile& operator=(ScratchFile&& other) noexcept;
David Sehrd5f8de82018-04-27 14:12:03 -070056
57 explicit ScratchFile(File* file);
58
59 ~ScratchFile();
60
61 const std::string& GetFilename() const {
62 return filename_;
63 }
64
65 File* GetFile() const {
66 return file_.get();
67 }
68
69 int GetFd() const;
70
71 void Close();
72 void Unlink();
73
74 private:
75 std::string filename_;
76 std::unique_ptr<File> file_;
77};
78
Calin Juravle8b959952019-09-20 16:32:05 -040079// Close to store a fake dex file and its underlying data.
80class FakeDex {
81 public:
82 static std::unique_ptr<FakeDex> Create(
83 const std::string& location,
84 uint32_t checksum,
85 uint32_t num_method_ids) {
86 FakeDex* fake_dex = new FakeDex();
87 fake_dex->dex = CreateFakeDex(location, checksum, num_method_ids, &fake_dex->storage);
88 return std::unique_ptr<FakeDex>(fake_dex);
89 }
90
91 static std::unique_ptr<const DexFile> CreateFakeDex(
92 const std::string& location,
93 uint32_t checksum,
94 uint32_t num_method_ids,
95 std::vector<uint8_t>* storage) {
96 storage->resize(kPageSize);
97 CompactDexFile::Header* header =
98 const_cast<CompactDexFile::Header*>(CompactDexFile::Header::At(storage->data()));
99 CompactDexFile::WriteMagic(header->magic_);
100 CompactDexFile::WriteCurrentVersion(header->magic_);
101 header->data_off_ = 0;
102 header->data_size_ = storage->size();
103 header->method_ids_size_ = num_method_ids;
104
105 const DexFileLoader dex_file_loader;
106 std::string error_msg;
107 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(storage->data(),
108 storage->size(),
109 location,
110 checksum,
111 /*oat_dex_file=*/nullptr,
112 /*verify=*/false,
113 /*verify_checksum=*/false,
114 &error_msg));
115 CHECK(dex != nullptr) << error_msg;
116 return dex;
117 }
118
119 std::unique_ptr<const DexFile>& Dex() {
120 return dex;
121 }
122
123 private:
124 std::vector<uint8_t> storage;
125 std::unique_ptr<const DexFile> dex;
126};
127
128// Convenience class to store multiple fake dex files in order to make
129// allocation/de-allocation easier in tests.
130class FakeDexStorage {
131 public:
132 const DexFile* AddFakeDex(
133 const std::string& location,
134 uint32_t checksum,
135 uint32_t num_method_ids) {
136 fake_dex_files.push_back(FakeDex::Create(location, checksum, num_method_ids));
137 return fake_dex_files.back()->Dex().get();
138 }
139
140 private:
141 std::vector<std::unique_ptr<FakeDex>> fake_dex_files;
142};
143
David Sehrd5f8de82018-04-27 14:12:03 -0700144class CommonArtTestImpl {
145 public:
146 CommonArtTestImpl() = default;
147 virtual ~CommonArtTestImpl() = default;
148
Victor Chang64611242019-07-05 16:32:41 +0100149 // Set up ANDROID_BUILD_TOP, ANDROID_HOST_OUT, ANDROID_ROOT, ANDROID_I18N_ROOT,
Martin Stjernholme58624f2019-09-20 15:53:40 +0100150 // ANDROID_ART_ROOT, and ANDROID_TZDATA_ROOT environment variables using sensible defaults
Victor Chang64611242019-07-05 16:32:41 +0100151 // if not already set.
Neil Fuller26c43772018-11-23 17:56:43 +0000152 static void SetUpAndroidRootEnvVars();
David Sehrd5f8de82018-04-27 14:12:03 -0700153
Neil Fuller26c43772018-11-23 17:56:43 +0000154 // Set up the ANDROID_DATA environment variable, creating the directory if required.
David Sehrd5f8de82018-04-27 14:12:03 -0700155 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
156 // non-derived class, be sure to also call the corresponding tear-down below.
Neil Fuller26c43772018-11-23 17:56:43 +0000157 static void SetUpAndroidDataDir(std::string& android_data);
David Sehrd5f8de82018-04-27 14:12:03 -0700158
Neil Fuller26c43772018-11-23 17:56:43 +0000159 static void TearDownAndroidDataDir(const std::string& android_data, bool fail_on_error);
David Sehrd5f8de82018-04-27 14:12:03 -0700160
Vladimir Markob9c29f62019-03-20 14:22:51 +0000161 // Get the names of the libcore modules.
162 virtual std::vector<std::string> GetLibCoreModuleNames() const;
163
164 // Gets the paths of the libcore dex files for given modules.
165 std::vector<std::string> GetLibCoreDexFileNames(const std::vector<std::string>& modules) const;
166
David Sehrd5f8de82018-04-27 14:12:03 -0700167 // Gets the paths of the libcore dex files.
Vladimir Markob9c29f62019-03-20 14:22:51 +0000168 std::vector<std::string> GetLibCoreDexFileNames() const;
169
170 // Gets the locations of the libcore dex files for given modules.
171 std::vector<std::string> GetLibCoreDexLocations(const std::vector<std::string>& modules) const;
David Sehrd5f8de82018-04-27 14:12:03 -0700172
Vladimir Marko7a85e702018-12-03 18:47:23 +0000173 // Gets the locations of the libcore dex files.
Vladimir Markob9c29f62019-03-20 14:22:51 +0000174 std::vector<std::string> GetLibCoreDexLocations() const;
Vladimir Marko7a85e702018-12-03 18:47:23 +0000175
176 static std::string GetClassPathOption(const char* option,
177 const std::vector<std::string>& class_path);
178
David Sehrd5f8de82018-04-27 14:12:03 -0700179 // Returns bin directory which contains host's prebuild tools.
180 static std::string GetAndroidHostToolsDir();
181
182 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
183 std::string GetTestDexFileName(const char* name) const;
184
185 template <typename Mutator>
186 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
187 std::vector<std::unique_ptr<const DexFile>> dex_files;
188 std::string error_msg;
189 const ArtDexFileLoader dex_file_loader;
190 CHECK(dex_file_loader.Open(input_jar.c_str(),
191 input_jar.c_str(),
192 /*verify*/ true,
193 /*verify_checksum*/ true,
194 &error_msg,
195 &dex_files)) << error_msg;
196 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
197 const std::unique_ptr<const DexFile>& dex = dex_files[0];
198 CHECK(dex->EnableWrite()) << "Failed to enable write";
199 DexFile* dex_file = const_cast<DexFile*>(dex.get());
200 mutator(dex_file);
201 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
202 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
203 return false;
204 }
205 if (output_dex->Flush() != 0) {
206 PLOG(FATAL) << "Could not flush the output file.";
207 }
208 return true;
209 }
210
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700211 struct ForkAndExecResult {
212 enum Stage {
213 kLink,
214 kFork,
215 kWaitpid,
216 kFinished,
217 };
218 Stage stage;
219 int status_code;
220
221 bool StandardSuccess() {
222 return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0;
223 }
224 };
225 using OutputHandlerFn = std::function<void(char*, size_t)>;
226 using PostForkFn = std::function<bool()>;
227 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
228 const PostForkFn& post_fork,
229 const OutputHandlerFn& handler);
230 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
231 const PostForkFn& post_fork,
232 std::string* output);
233
David Sehrd5f8de82018-04-27 14:12:03 -0700234 protected:
235 static bool IsHost() {
236 return !kIsTargetBuild;
237 }
238
239 // Helper - find directory with the following format:
240 // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
241 static std::string GetAndroidToolsDir(const std::string& subdir1,
242 const std::string& subdir2,
243 const std::string& subdir3);
244
245 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
246 static std::string GetCoreArtLocation();
247
248 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
249 static std::string GetCoreOatLocation();
250
251 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
252
253 void ClearDirectory(const char* dirpath, bool recursive = true);
254
David Sehr7d432422018-05-25 10:49:02 -0700255 // Open a file (allows reading of framework jars).
256 std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename);
Mathieu Chartierc2109c62019-03-20 13:34:39 -0700257
258 // Open a single dex file (aborts if there are more than one).
259 std::unique_ptr<const DexFile> OpenDexFile(const char* filename);
260
David Sehr7d432422018-05-25 10:49:02 -0700261 // Open a test file (art-gtest-*.jar).
David Sehrd5f8de82018-04-27 14:12:03 -0700262 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
263
264 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
265
266
267 std::string android_data_;
268 std::string dalvik_cache_;
269
270 virtual void SetUp();
271
272 virtual void TearDown();
273
274 // Creates the class path string for the given dex files (the list of dex file locations
275 // separated by ':').
276 std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
277 // Same as CreateClassPath but add the dex file checksum after each location. The separator
278 // is '*'.
279 std::string CreateClassPathWithChecksums(
280 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
281
282 static std::string GetCoreFileLocation(const char* suffix);
283
284 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
285};
286
287template <typename TestType>
288class CommonArtTestBase : public TestType, public CommonArtTestImpl {
289 public:
290 CommonArtTestBase() {}
291 virtual ~CommonArtTestBase() {}
292
293 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100294 void SetUp() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700295 CommonArtTestImpl::SetUp();
296 }
297
Roland Levillainf73caca2018-08-24 17:19:07 +0100298 void TearDown() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700299 CommonArtTestImpl::TearDown();
300 }
301};
302
303using CommonArtTest = CommonArtTestBase<testing::Test>;
304
305template <typename Param>
306using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>;
307
308#define TEST_DISABLED_FOR_TARGET() \
309 if (kIsTargetBuild) { \
310 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
311 return; \
312 }
313
314#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
315 if (!kHostStaticBuildEnabled) { \
316 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
317 return; \
318 }
319
320#define TEST_DISABLED_FOR_MEMORY_TOOL() \
Roland Levillain05e34f42018-05-24 13:19:05 +0000321 if (kRunningOnMemoryTool) { \
David Sehrd5f8de82018-04-27 14:12:03 -0700322 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
323 return; \
324 }
325
David Sehrd5f8de82018-04-27 14:12:03 -0700326#define TEST_DISABLED_FOR_HEAP_POISONING() \
327 if (kPoisonHeapReferences) { \
328 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
329 return; \
330 }
331} // namespace art
332
Roland Levillainab4326e2018-06-19 18:10:55 +0000333#define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING() \
334 if (kRunningOnMemoryTool && kPoisonHeapReferences) { \
335 printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING\n"); \
336 return; \
337 }
338
David Sehrd5f8de82018-04-27 14:12:03 -0700339#endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_