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 | |
| 29 | #include "base/globals.h" |
| 30 | #include "base/mutex.h" |
| 31 | #include "base/os.h" |
| 32 | #include "base/unix_file/fd_file.h" |
| 33 | #include "dex/art_dex_file_loader.h" |
| 34 | #include "dex/compact_dex_level.h" |
| 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | using LogSeverity = android::base::LogSeverity; |
| 39 | using ScopedLogSeverity = android::base::ScopedLogSeverity; |
| 40 | |
| 41 | // OBJ pointer helpers to avoid needing .Decode everywhere. |
| 42 | #define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
| 43 | #define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
| 44 | #define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
| 45 | #define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
| 46 | |
| 47 | class DexFile; |
| 48 | |
| 49 | class ScratchFile { |
| 50 | public: |
| 51 | ScratchFile(); |
| 52 | |
| 53 | explicit ScratchFile(const std::string& filename); |
| 54 | |
| 55 | ScratchFile(const ScratchFile& other, const char* suffix); |
| 56 | |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame^] | 57 | ScratchFile(ScratchFile&& other) noexcept; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 58 | |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame^] | 59 | ScratchFile& operator=(ScratchFile&& other) noexcept; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 60 | |
| 61 | explicit ScratchFile(File* file); |
| 62 | |
| 63 | ~ScratchFile(); |
| 64 | |
| 65 | const std::string& GetFilename() const { |
| 66 | return filename_; |
| 67 | } |
| 68 | |
| 69 | File* GetFile() const { |
| 70 | return file_.get(); |
| 71 | } |
| 72 | |
| 73 | int GetFd() const; |
| 74 | |
| 75 | void Close(); |
| 76 | void Unlink(); |
| 77 | |
| 78 | private: |
| 79 | std::string filename_; |
| 80 | std::unique_ptr<File> file_; |
| 81 | }; |
| 82 | |
| 83 | class CommonArtTestImpl { |
| 84 | public: |
| 85 | CommonArtTestImpl() = default; |
| 86 | virtual ~CommonArtTestImpl() = default; |
| 87 | |
| 88 | static void SetUpAndroidRoot(); |
| 89 | |
| 90 | // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a |
| 91 | // non-derived class, be sure to also call the corresponding tear-down below. |
| 92 | static void SetUpAndroidData(std::string& android_data); |
| 93 | |
| 94 | static void TearDownAndroidData(const std::string& android_data, bool fail_on_error); |
| 95 | |
| 96 | // Gets the paths of the libcore dex files. |
| 97 | static std::vector<std::string> GetLibCoreDexFileNames(); |
| 98 | |
| 99 | // Returns bin directory which contains host's prebuild tools. |
| 100 | static std::string GetAndroidHostToolsDir(); |
| 101 | |
| 102 | // Retuerns the filename for a test dex (i.e. XandY or ManyMethods). |
| 103 | std::string GetTestDexFileName(const char* name) const; |
| 104 | |
| 105 | template <typename Mutator> |
| 106 | bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) { |
| 107 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 108 | std::string error_msg; |
| 109 | const ArtDexFileLoader dex_file_loader; |
| 110 | CHECK(dex_file_loader.Open(input_jar.c_str(), |
| 111 | input_jar.c_str(), |
| 112 | /*verify*/ true, |
| 113 | /*verify_checksum*/ true, |
| 114 | &error_msg, |
| 115 | &dex_files)) << error_msg; |
| 116 | EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported"; |
| 117 | const std::unique_ptr<const DexFile>& dex = dex_files[0]; |
| 118 | CHECK(dex->EnableWrite()) << "Failed to enable write"; |
| 119 | DexFile* dex_file = const_cast<DexFile*>(dex.get()); |
| 120 | mutator(dex_file); |
| 121 | const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum(); |
| 122 | if (!output_dex->WriteFully(dex->Begin(), dex->Size())) { |
| 123 | return false; |
| 124 | } |
| 125 | if (output_dex->Flush() != 0) { |
| 126 | PLOG(FATAL) << "Could not flush the output file."; |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 131 | struct ForkAndExecResult { |
| 132 | enum Stage { |
| 133 | kLink, |
| 134 | kFork, |
| 135 | kWaitpid, |
| 136 | kFinished, |
| 137 | }; |
| 138 | Stage stage; |
| 139 | int status_code; |
| 140 | |
| 141 | bool StandardSuccess() { |
| 142 | return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0; |
| 143 | } |
| 144 | }; |
| 145 | using OutputHandlerFn = std::function<void(char*, size_t)>; |
| 146 | using PostForkFn = std::function<bool()>; |
| 147 | static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv, |
| 148 | const PostForkFn& post_fork, |
| 149 | const OutputHandlerFn& handler); |
| 150 | static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv, |
| 151 | const PostForkFn& post_fork, |
| 152 | std::string* output); |
| 153 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 154 | protected: |
| 155 | static bool IsHost() { |
| 156 | return !kIsTargetBuild; |
| 157 | } |
| 158 | |
| 159 | // Helper - find directory with the following format: |
| 160 | // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/ |
| 161 | static std::string GetAndroidToolsDir(const std::string& subdir1, |
| 162 | const std::string& subdir2, |
| 163 | const std::string& subdir3); |
| 164 | |
| 165 | // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art |
| 166 | static std::string GetCoreArtLocation(); |
| 167 | |
| 168 | // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat |
| 169 | static std::string GetCoreOatLocation(); |
| 170 | |
| 171 | std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location); |
| 172 | |
| 173 | void ClearDirectory(const char* dirpath, bool recursive = true); |
| 174 | |
| 175 | std::string GetTestAndroidRoot(); |
| 176 | |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 177 | // Open a file (allows reading of framework jars). |
| 178 | std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename); |
| 179 | // Open a test file (art-gtest-*.jar). |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 180 | std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name); |
| 181 | |
| 182 | std::unique_ptr<const DexFile> OpenTestDexFile(const char* name); |
| 183 | |
| 184 | |
| 185 | std::string android_data_; |
| 186 | std::string dalvik_cache_; |
| 187 | |
| 188 | virtual void SetUp(); |
| 189 | |
| 190 | virtual void TearDown(); |
| 191 | |
| 192 | // Creates the class path string for the given dex files (the list of dex file locations |
| 193 | // separated by ':'). |
| 194 | std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files); |
| 195 | // Same as CreateClassPath but add the dex file checksum after each location. The separator |
| 196 | // is '*'. |
| 197 | std::string CreateClassPathWithChecksums( |
| 198 | const std::vector<std::unique_ptr<const DexFile>>& dex_files); |
| 199 | |
| 200 | static std::string GetCoreFileLocation(const char* suffix); |
| 201 | |
| 202 | std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_; |
| 203 | }; |
| 204 | |
| 205 | template <typename TestType> |
| 206 | class CommonArtTestBase : public TestType, public CommonArtTestImpl { |
| 207 | public: |
| 208 | CommonArtTestBase() {} |
| 209 | virtual ~CommonArtTestBase() {} |
| 210 | |
| 211 | protected: |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 212 | void SetUp() override { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 213 | CommonArtTestImpl::SetUp(); |
| 214 | } |
| 215 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 216 | void TearDown() override { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 217 | CommonArtTestImpl::TearDown(); |
| 218 | } |
| 219 | }; |
| 220 | |
| 221 | using CommonArtTest = CommonArtTestBase<testing::Test>; |
| 222 | |
| 223 | template <typename Param> |
| 224 | using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>; |
| 225 | |
| 226 | #define TEST_DISABLED_FOR_TARGET() \ |
| 227 | if (kIsTargetBuild) { \ |
| 228 | printf("WARNING: TEST DISABLED FOR TARGET\n"); \ |
| 229 | return; \ |
| 230 | } |
| 231 | |
| 232 | #define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \ |
| 233 | if (!kHostStaticBuildEnabled) { \ |
| 234 | printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \ |
| 235 | return; \ |
| 236 | } |
| 237 | |
| 238 | #define TEST_DISABLED_FOR_MEMORY_TOOL() \ |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 239 | if (kRunningOnMemoryTool) { \ |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 240 | printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \ |
| 241 | return; \ |
| 242 | } |
| 243 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 244 | #define TEST_DISABLED_FOR_HEAP_POISONING() \ |
| 245 | if (kPoisonHeapReferences) { \ |
| 246 | printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \ |
| 247 | return; \ |
| 248 | } |
| 249 | } // namespace art |
| 250 | |
Roland Levillain | ab4326e | 2018-06-19 18:10:55 +0000 | [diff] [blame] | 251 | #define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING() \ |
| 252 | if (kRunningOnMemoryTool && kPoisonHeapReferences) { \ |
| 253 | printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING\n"); \ |
| 254 | return; \ |
| 255 | } |
| 256 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 257 | #endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_ |