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