blob: 6124ed9e2a2d1c76946f459c8fbbc04fc63ae267 [file] [log] [blame]
Martin Stjernholm4fb51112021-04-30 11:53:52 +01001/*
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
android-t13d2c5b22022-10-12 13:43:18 +080020#include <sys/types.h>
21#include <sys/wait.h>
Martin Stjernholm4fb51112021-04-30 11:53:52 +010022
23#include <functional>
24#include <string>
android-t13d2c5b22022-10-12 13:43:18 +080025#include <vector>
Martin Stjernholm4fb51112021-04-30 11:53:52 +010026
android-t13d2c5b22022-10-12 13:43:18 +080027#include "android-base/logging.h"
Martin Stjernholm4fb51112021-04-30 11:53:52 +010028#include "base/file_utils.h"
29#include "base/globals.h"
30#include "base/memory_tool.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"
Martin Stjernholm4fb51112021-04-30 11:53:52 +010035#include "dex/compact_dex_file.h"
android-t13d2c5b22022-10-12 13:43:18 +080036#include "dex/compact_dex_level.h"
37#include "gtest/gtest.h"
Martin Stjernholm4fb51112021-04-30 11:53:52 +010038
39namespace art {
40
41using LogSeverity = android::base::LogSeverity;
42using ScopedLogSeverity = android::base::ScopedLogSeverity;
43
44class DexFile;
45
46class ScratchDir {
47 public:
48 explicit ScratchDir(bool keep_files = false);
49
50 ~ScratchDir();
51
52 const std::string& GetPath() const {
53 return path_;
54 }
55
56 private:
57 std::string path_;
58 bool keep_files_; // Useful for debugging.
59
60 DISALLOW_COPY_AND_ASSIGN(ScratchDir);
61};
62
63class ScratchFile {
64 public:
65 ScratchFile();
66
67 explicit ScratchFile(const std::string& filename);
68
69 ScratchFile(const ScratchFile& other, const char* suffix);
70
71 ScratchFile(ScratchFile&& other) noexcept;
72
73 ScratchFile& operator=(ScratchFile&& other) noexcept;
74
75 explicit ScratchFile(File* file);
76
77 ~ScratchFile();
78
79 const std::string& GetFilename() const {
80 return filename_;
81 }
82
83 File* GetFile() const {
84 return file_.get();
85 }
86
87 int GetFd() const;
88
89 void Close();
90 void Unlink();
91
92 private:
93 std::string filename_;
94 std::unique_ptr<File> file_;
95};
96
97// Helper class that removes an environment variable whilst in scope.
98class ScopedUnsetEnvironmentVariable {
99 public:
100 explicit ScopedUnsetEnvironmentVariable(const char* variable)
101 : variable_{variable}, old_value_{GetOldValue(variable)} {
102 unsetenv(variable);
103 }
104
105 ~ScopedUnsetEnvironmentVariable() {
106 if (old_value_.has_value()) {
107 static constexpr int kReplace = 1; // tidy-issue: replace argument has libc dependent name.
108 setenv(variable_, old_value_.value().c_str(), kReplace);
109 } else {
110 unsetenv(variable_);
111 }
112 }
113
114 private:
115 static std::optional<std::string> GetOldValue(const char* variable) {
116 const char* value = getenv(variable);
117 return value != nullptr ? std::optional<std::string>{value} : std::nullopt;
118 }
119
120 const char* variable_;
121 std::optional<std::string> old_value_;
122 DISALLOW_COPY_AND_ASSIGN(ScopedUnsetEnvironmentVariable);
123};
124
125class CommonArtTestImpl {
126 public:
127 CommonArtTestImpl() = default;
128 virtual ~CommonArtTestImpl() = default;
129
130 // Set up ANDROID_BUILD_TOP, ANDROID_HOST_OUT, ANDROID_ROOT, ANDROID_I18N_ROOT,
131 // ANDROID_ART_ROOT, and ANDROID_TZDATA_ROOT environment variables using sensible defaults
132 // if not already set.
133 static void SetUpAndroidRootEnvVars();
134
135 // Set up the ANDROID_DATA environment variable, creating the directory if required.
136 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
137 // non-derived class, be sure to also call the corresponding tear-down below.
138 static void SetUpAndroidDataDir(std::string& android_data);
139
140 static void TearDownAndroidDataDir(const std::string& android_data, bool fail_on_error);
141
142 // Get the names of the libcore modules.
143 virtual std::vector<std::string> GetLibCoreModuleNames() const;
144
145 // Gets the paths of the libcore dex files for given modules.
146 std::vector<std::string> GetLibCoreDexFileNames(const std::vector<std::string>& modules) const;
147
148 // Gets the paths of the libcore dex files.
149 std::vector<std::string> GetLibCoreDexFileNames() const;
150
151 // Gets the locations of the libcore dex files for given modules.
152 std::vector<std::string> GetLibCoreDexLocations(const std::vector<std::string>& modules) const;
153
154 // Gets the locations of the libcore dex files.
155 std::vector<std::string> GetLibCoreDexLocations() const;
156
157 static std::string GetClassPathOption(const char* option,
158 const std::vector<std::string>& class_path);
159
160 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
161 std::string GetTestDexFileName(const char* name) const;
162
163 template <typename Mutator>
164 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
165 std::vector<std::unique_ptr<const DexFile>> dex_files;
166 std::string error_msg;
167 const ArtDexFileLoader dex_file_loader;
168 CHECK(dex_file_loader.Open(input_jar.c_str(),
169 input_jar.c_str(),
170 /*verify*/ true,
171 /*verify_checksum*/ true,
172 &error_msg,
173 &dex_files)) << error_msg;
174 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
175 const std::unique_ptr<const DexFile>& dex = dex_files[0];
176 CHECK(dex->EnableWrite()) << "Failed to enable write";
177 DexFile* dex_file = const_cast<DexFile*>(dex.get());
178 mutator(dex_file);
179 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
180 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
181 return false;
182 }
183 if (output_dex->Flush() != 0) {
184 PLOG(FATAL) << "Could not flush the output file.";
185 }
186 return true;
187 }
188
189 struct ForkAndExecResult {
190 enum Stage {
191 kLink,
192 kFork,
193 kWaitpid,
194 kFinished,
195 };
196 Stage stage;
197 int status_code;
198
199 bool StandardSuccess() {
200 return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0;
201 }
202 };
203 using OutputHandlerFn = std::function<void(char*, size_t)>;
204 using PostForkFn = std::function<bool()>;
205 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
206 const PostForkFn& post_fork,
207 const OutputHandlerFn& handler);
208 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
209 const PostForkFn& post_fork,
210 std::string* output);
211
212 // Helper - find prebuilt tool (e.g. objdump).
213 static std::string GetAndroidTool(const char* name, InstructionSet isa = InstructionSet::kX86_64);
214
215 protected:
216 static bool IsHost() {
217 return !kIsTargetBuild;
218 }
219
220 // Returns ${ANDROID_BUILD_TOP}. Ensure it has tailing /.
221 static std::string GetAndroidBuildTop();
222
223 // Returns ${ANDROID_HOST_OUT}.
224 static std::string GetAndroidHostOut();
225
226 // File location to boot.art, e.g. /apex/com.android.art/javalib/boot.art
227 static std::string GetCoreArtLocation();
228
229 // File location to boot.oat, e.g. /apex/com.android.art/javalib/boot.oat
230 static std::string GetCoreOatLocation();
231
232 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
233
234 void ClearDirectory(const char* dirpath, bool recursive = true);
235
236 // Open a file (allows reading of framework jars).
237 std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename);
238
239 // Open a single dex file (aborts if there are more than one).
240 std::unique_ptr<const DexFile> OpenDexFile(const char* filename);
241
242 // Open a test file (art-gtest-*.jar).
243 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
244
245 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
246
247 std::string android_data_;
248 std::string android_system_ext_;
249 std::string dalvik_cache_;
250
251 virtual void SetUp();
252
253 virtual void TearDown();
254
255 // Creates the class path string for the given dex files (the list of dex file locations
256 // separated by ':').
257 std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
258 // Same as CreateClassPath but add the dex file checksum after each location. The separator
259 // is '*'.
260 std::string CreateClassPathWithChecksums(
261 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
262
263 static std::string GetImageDirectory();
264 static std::string GetCoreFileLocation(const char* suffix);
265
266 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
267};
268
269template <typename TestType>
270class CommonArtTestBase : public TestType, public CommonArtTestImpl {
271 public:
272 CommonArtTestBase() {}
273 virtual ~CommonArtTestBase() {}
274
275 protected:
276 void SetUp() override {
277 CommonArtTestImpl::SetUp();
278 }
279
280 void TearDown() override {
281 CommonArtTestImpl::TearDown();
282 }
283};
284
285using CommonArtTest = CommonArtTestBase<testing::Test>;
286
287template <typename Param>
288using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>;
289
android-t13d2c5b22022-10-12 13:43:18 +0800290// Returns a list of PIDs of the processes whose process name (the first commandline argument) fully
291// matches the given name.
292std::vector<pid_t> GetPidByName(const std::string& process_name);
293
Martin Stjernholm4fb51112021-04-30 11:53:52 +0100294#define TEST_DISABLED_FOR_TARGET() \
295 if (kIsTargetBuild) { \
296 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
297 return; \
298 }
299
android-t13d2c5b22022-10-12 13:43:18 +0800300#define TEST_DISABLED_FOR_HOST() \
301 if (!kIsTargetBuild) { \
302 printf("WARNING: TEST DISABLED FOR HOST\n"); \
303 return; \
304 }
305
Martin Stjernholm4fb51112021-04-30 11:53:52 +0100306#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
307 if (!kHostStaticBuildEnabled) { \
308 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
309 return; \
310 }
311
312#define TEST_DISABLED_FOR_MEMORY_TOOL() \
313 if (kRunningOnMemoryTool) { \
314 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
315 return; \
316 }
317
318#define TEST_DISABLED_FOR_HEAP_POISONING() \
319 if (kPoisonHeapReferences) { \
320 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
321 return; \
322 }
323} // namespace art
324
325#define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING() \
326 if (kRunningOnMemoryTool && kPoisonHeapReferences) { \
327 printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING\n"); \
328 return; \
329 }
330
331#endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_