David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 22 | #include <functional> |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 25 | #include <sys/wait.h> |
| 26 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 27 | #include <android-base/logging.h> |
| 28 | |
Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 29 | #include "base/file_utils.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 30 | #include "base/globals.h" |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 31 | #include "base/memory_tool.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 32 | #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 Juravle | 8b95995 | 2019-09-20 16:32:05 -0400 | [diff] [blame] | 37 | #include "dex/compact_dex_file.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
| 41 | using LogSeverity = android::base::LogSeverity; |
| 42 | using ScopedLogSeverity = android::base::ScopedLogSeverity; |
| 43 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 44 | class DexFile; |
| 45 | |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 46 | class 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 Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 62 | class ScratchFile { |
| 63 | public: |
| 64 | ScratchFile(); |
| 65 | |
| 66 | explicit ScratchFile(const std::string& filename); |
| 67 | |
| 68 | ScratchFile(const ScratchFile& other, const char* suffix); |
| 69 | |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame] | 70 | ScratchFile(ScratchFile&& other) noexcept; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 71 | |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame] | 72 | ScratchFile& operator=(ScratchFile&& other) noexcept; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 73 | |
| 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 Juravle | 8b95995 | 2019-09-20 16:32:05 -0400 | [diff] [blame] | 96 | // Close to store a fake dex file and its underlying data. |
| 97 | class 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. |
| 147 | class 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 Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 161 | class CommonArtTestImpl { |
| 162 | public: |
| 163 | CommonArtTestImpl() = default; |
| 164 | virtual ~CommonArtTestImpl() = default; |
| 165 | |
Victor Chang | 6461124 | 2019-07-05 16:32:41 +0100 | [diff] [blame] | 166 | // Set up ANDROID_BUILD_TOP, ANDROID_HOST_OUT, ANDROID_ROOT, ANDROID_I18N_ROOT, |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 167 | // ANDROID_ART_ROOT, and ANDROID_TZDATA_ROOT environment variables using sensible defaults |
Victor Chang | 6461124 | 2019-07-05 16:32:41 +0100 | [diff] [blame] | 168 | // if not already set. |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 169 | static void SetUpAndroidRootEnvVars(); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 170 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 171 | // Set up the ANDROID_DATA environment variable, creating the directory if required. |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 172 | // 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 Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 174 | static void SetUpAndroidDataDir(std::string& android_data); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 175 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 176 | static void TearDownAndroidDataDir(const std::string& android_data, bool fail_on_error); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 177 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 178 | // 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 Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 184 | // Gets the paths of the libcore dex files. |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 185 | 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 Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 189 | |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 190 | // Gets the locations of the libcore dex files. |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 191 | std::vector<std::string> GetLibCoreDexLocations() const; |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 192 | |
| 193 | static std::string GetClassPathOption(const char* option, |
| 194 | const std::vector<std::string>& class_path); |
| 195 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 196 | // 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 Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 228 | 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 Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 251 | protected: |
| 252 | static bool IsHost() { |
| 253 | return !kIsTargetBuild; |
| 254 | } |
| 255 | |
David Srbecky | d31def5 | 2020-03-30 17:57:10 +0100 | [diff] [blame] | 256 | // Returns ${ANDROID_BUILD_TOP}. Ensure it has tailing /. |
| 257 | static std::string GetAndroidBuildTop(); |
| 258 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 259 | // 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 Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 275 | // Open a file (allows reading of framework jars). |
| 276 | std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename); |
Mathieu Chartier | c2109c6 | 2019-03-20 13:34:39 -0700 | [diff] [blame] | 277 | |
| 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 Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 281 | // Open a test file (art-gtest-*.jar). |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 282 | 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 | |
| 307 | template <typename TestType> |
| 308 | class CommonArtTestBase : public TestType, public CommonArtTestImpl { |
| 309 | public: |
| 310 | CommonArtTestBase() {} |
| 311 | virtual ~CommonArtTestBase() {} |
| 312 | |
| 313 | protected: |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 314 | void SetUp() override { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 315 | CommonArtTestImpl::SetUp(); |
| 316 | } |
| 317 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 318 | void TearDown() override { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 319 | CommonArtTestImpl::TearDown(); |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | using CommonArtTest = CommonArtTestBase<testing::Test>; |
| 324 | |
| 325 | template <typename Param> |
| 326 | using 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 Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 341 | if (kRunningOnMemoryTool) { \ |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 342 | printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \ |
| 343 | return; \ |
| 344 | } |
| 345 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 346 | #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 Levillain | ab4326e | 2018-06-19 18:10:55 +0000 | [diff] [blame] | 353 | #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 Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 359 | #endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_ |