blob: 8d2693f784afbb5d91015cabb5bb45acac3e4f57 [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
David Srbeckycf0c6ef2020-02-05 16:25:36 +000045class ScratchDir {
46 public:
47 ScratchDir();
48
49 ~ScratchDir();
50
51 const std::string& GetPath() const {
52 return path_;
53 }
54
55 private:
56 std::string path_;
57
58 DISALLOW_COPY_AND_ASSIGN(ScratchDir);
59};
60
David Sehrd5f8de82018-04-27 14:12:03 -070061class ScratchFile {
62 public:
63 ScratchFile();
64
65 explicit ScratchFile(const std::string& filename);
66
67 ScratchFile(const ScratchFile& other, const char* suffix);
68
Andreas Gampe44b31742018-10-01 19:30:57 -070069 ScratchFile(ScratchFile&& other) noexcept;
David Sehrd5f8de82018-04-27 14:12:03 -070070
Andreas Gampe44b31742018-10-01 19:30:57 -070071 ScratchFile& operator=(ScratchFile&& other) noexcept;
David Sehrd5f8de82018-04-27 14:12:03 -070072
73 explicit ScratchFile(File* file);
74
75 ~ScratchFile();
76
77 const std::string& GetFilename() const {
78 return filename_;
79 }
80
81 File* GetFile() const {
82 return file_.get();
83 }
84
85 int GetFd() const;
86
87 void Close();
88 void Unlink();
89
90 private:
91 std::string filename_;
92 std::unique_ptr<File> file_;
93};
94
Calin Juravle8b959952019-09-20 16:32:05 -040095// Close to store a fake dex file and its underlying data.
96class FakeDex {
97 public:
98 static std::unique_ptr<FakeDex> Create(
99 const std::string& location,
100 uint32_t checksum,
101 uint32_t num_method_ids) {
102 FakeDex* fake_dex = new FakeDex();
103 fake_dex->dex = CreateFakeDex(location, checksum, num_method_ids, &fake_dex->storage);
104 return std::unique_ptr<FakeDex>(fake_dex);
105 }
106
107 static std::unique_ptr<const DexFile> CreateFakeDex(
108 const std::string& location,
109 uint32_t checksum,
110 uint32_t num_method_ids,
111 std::vector<uint8_t>* storage) {
112 storage->resize(kPageSize);
113 CompactDexFile::Header* header =
114 const_cast<CompactDexFile::Header*>(CompactDexFile::Header::At(storage->data()));
115 CompactDexFile::WriteMagic(header->magic_);
116 CompactDexFile::WriteCurrentVersion(header->magic_);
117 header->data_off_ = 0;
118 header->data_size_ = storage->size();
119 header->method_ids_size_ = num_method_ids;
120
121 const DexFileLoader dex_file_loader;
122 std::string error_msg;
123 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(storage->data(),
124 storage->size(),
125 location,
126 checksum,
127 /*oat_dex_file=*/nullptr,
128 /*verify=*/false,
129 /*verify_checksum=*/false,
130 &error_msg));
131 CHECK(dex != nullptr) << error_msg;
132 return dex;
133 }
134
135 std::unique_ptr<const DexFile>& Dex() {
136 return dex;
137 }
138
139 private:
140 std::vector<uint8_t> storage;
141 std::unique_ptr<const DexFile> dex;
142};
143
144// Convenience class to store multiple fake dex files in order to make
145// allocation/de-allocation easier in tests.
146class FakeDexStorage {
147 public:
148 const DexFile* AddFakeDex(
149 const std::string& location,
150 uint32_t checksum,
151 uint32_t num_method_ids) {
152 fake_dex_files.push_back(FakeDex::Create(location, checksum, num_method_ids));
153 return fake_dex_files.back()->Dex().get();
154 }
155
156 private:
157 std::vector<std::unique_ptr<FakeDex>> fake_dex_files;
158};
159
David Sehrd5f8de82018-04-27 14:12:03 -0700160class CommonArtTestImpl {
161 public:
162 CommonArtTestImpl() = default;
163 virtual ~CommonArtTestImpl() = default;
164
Victor Chang64611242019-07-05 16:32:41 +0100165 // Set up ANDROID_BUILD_TOP, ANDROID_HOST_OUT, ANDROID_ROOT, ANDROID_I18N_ROOT,
Martin Stjernholme58624f2019-09-20 15:53:40 +0100166 // ANDROID_ART_ROOT, and ANDROID_TZDATA_ROOT environment variables using sensible defaults
Victor Chang64611242019-07-05 16:32:41 +0100167 // if not already set.
Neil Fuller26c43772018-11-23 17:56:43 +0000168 static void SetUpAndroidRootEnvVars();
David Sehrd5f8de82018-04-27 14:12:03 -0700169
Neil Fuller26c43772018-11-23 17:56:43 +0000170 // Set up the ANDROID_DATA environment variable, creating the directory if required.
David Sehrd5f8de82018-04-27 14:12:03 -0700171 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
172 // non-derived class, be sure to also call the corresponding tear-down below.
Neil Fuller26c43772018-11-23 17:56:43 +0000173 static void SetUpAndroidDataDir(std::string& android_data);
David Sehrd5f8de82018-04-27 14:12:03 -0700174
Neil Fuller26c43772018-11-23 17:56:43 +0000175 static void TearDownAndroidDataDir(const std::string& android_data, bool fail_on_error);
David Sehrd5f8de82018-04-27 14:12:03 -0700176
Vladimir Markob9c29f62019-03-20 14:22:51 +0000177 // Get the names of the libcore modules.
178 virtual std::vector<std::string> GetLibCoreModuleNames() const;
179
180 // Gets the paths of the libcore dex files for given modules.
181 std::vector<std::string> GetLibCoreDexFileNames(const std::vector<std::string>& modules) const;
182
David Sehrd5f8de82018-04-27 14:12:03 -0700183 // Gets the paths of the libcore dex files.
Vladimir Markob9c29f62019-03-20 14:22:51 +0000184 std::vector<std::string> GetLibCoreDexFileNames() const;
185
186 // Gets the locations of the libcore dex files for given modules.
187 std::vector<std::string> GetLibCoreDexLocations(const std::vector<std::string>& modules) const;
David Sehrd5f8de82018-04-27 14:12:03 -0700188
Vladimir Marko7a85e702018-12-03 18:47:23 +0000189 // Gets the locations of the libcore dex files.
Vladimir Markob9c29f62019-03-20 14:22:51 +0000190 std::vector<std::string> GetLibCoreDexLocations() const;
Vladimir Marko7a85e702018-12-03 18:47:23 +0000191
192 static std::string GetClassPathOption(const char* option,
193 const std::vector<std::string>& class_path);
194
David Sehrd5f8de82018-04-27 14:12:03 -0700195 // Returns bin directory which contains host's prebuild tools.
196 static std::string GetAndroidHostToolsDir();
197
198 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
199 std::string GetTestDexFileName(const char* name) const;
200
201 template <typename Mutator>
202 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
203 std::vector<std::unique_ptr<const DexFile>> dex_files;
204 std::string error_msg;
205 const ArtDexFileLoader dex_file_loader;
206 CHECK(dex_file_loader.Open(input_jar.c_str(),
207 input_jar.c_str(),
208 /*verify*/ true,
209 /*verify_checksum*/ true,
210 &error_msg,
211 &dex_files)) << error_msg;
212 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
213 const std::unique_ptr<const DexFile>& dex = dex_files[0];
214 CHECK(dex->EnableWrite()) << "Failed to enable write";
215 DexFile* dex_file = const_cast<DexFile*>(dex.get());
216 mutator(dex_file);
217 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
218 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
219 return false;
220 }
221 if (output_dex->Flush() != 0) {
222 PLOG(FATAL) << "Could not flush the output file.";
223 }
224 return true;
225 }
226
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700227 struct ForkAndExecResult {
228 enum Stage {
229 kLink,
230 kFork,
231 kWaitpid,
232 kFinished,
233 };
234 Stage stage;
235 int status_code;
236
237 bool StandardSuccess() {
238 return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0;
239 }
240 };
241 using OutputHandlerFn = std::function<void(char*, size_t)>;
242 using PostForkFn = std::function<bool()>;
243 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
244 const PostForkFn& post_fork,
245 const OutputHandlerFn& handler);
246 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
247 const PostForkFn& post_fork,
248 std::string* output);
249
David Sehrd5f8de82018-04-27 14:12:03 -0700250 protected:
251 static bool IsHost() {
252 return !kIsTargetBuild;
253 }
254
255 // Helper - find directory with the following format:
256 // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
257 static std::string GetAndroidToolsDir(const std::string& subdir1,
258 const std::string& subdir2,
259 const std::string& subdir3);
260
261 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
262 static std::string GetCoreArtLocation();
263
264 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
265 static std::string GetCoreOatLocation();
266
267 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
268
269 void ClearDirectory(const char* dirpath, bool recursive = true);
270
David Sehr7d432422018-05-25 10:49:02 -0700271 // Open a file (allows reading of framework jars).
272 std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename);
Mathieu Chartierc2109c62019-03-20 13:34:39 -0700273
274 // Open a single dex file (aborts if there are more than one).
275 std::unique_ptr<const DexFile> OpenDexFile(const char* filename);
276
David Sehr7d432422018-05-25 10:49:02 -0700277 // Open a test file (art-gtest-*.jar).
David Sehrd5f8de82018-04-27 14:12:03 -0700278 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
279
280 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
281
282
283 std::string android_data_;
284 std::string dalvik_cache_;
285
286 virtual void SetUp();
287
288 virtual void TearDown();
289
290 // Creates the class path string for the given dex files (the list of dex file locations
291 // separated by ':').
292 std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
293 // Same as CreateClassPath but add the dex file checksum after each location. The separator
294 // is '*'.
295 std::string CreateClassPathWithChecksums(
296 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
297
298 static std::string GetCoreFileLocation(const char* suffix);
299
300 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
301};
302
303template <typename TestType>
304class CommonArtTestBase : public TestType, public CommonArtTestImpl {
305 public:
306 CommonArtTestBase() {}
307 virtual ~CommonArtTestBase() {}
308
309 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100310 void SetUp() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700311 CommonArtTestImpl::SetUp();
312 }
313
Roland Levillainf73caca2018-08-24 17:19:07 +0100314 void TearDown() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700315 CommonArtTestImpl::TearDown();
316 }
317};
318
319using CommonArtTest = CommonArtTestBase<testing::Test>;
320
321template <typename Param>
322using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>;
323
324#define TEST_DISABLED_FOR_TARGET() \
325 if (kIsTargetBuild) { \
326 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
327 return; \
328 }
329
330#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
331 if (!kHostStaticBuildEnabled) { \
332 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
333 return; \
334 }
335
336#define TEST_DISABLED_FOR_MEMORY_TOOL() \
Roland Levillain05e34f42018-05-24 13:19:05 +0000337 if (kRunningOnMemoryTool) { \
David Sehrd5f8de82018-04-27 14:12:03 -0700338 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
339 return; \
340 }
341
David Sehrd5f8de82018-04-27 14:12:03 -0700342#define TEST_DISABLED_FOR_HEAP_POISONING() \
343 if (kPoisonHeapReferences) { \
344 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
345 return; \
346 }
347} // namespace art
348
Roland Levillainab4326e2018-06-19 18:10:55 +0000349#define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING() \
350 if (kRunningOnMemoryTool && kPoisonHeapReferences) { \
351 printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING\n"); \
352 return; \
353 }
354
David Sehrd5f8de82018-04-27 14:12:03 -0700355#endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_