Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_ |
| 18 | #define ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_ |
| 19 | |
| 20 | #include <fstream> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 26 | #include "base/file_utils.h" |
David Sehr | 8f4b056 | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 27 | #include "base/os.h" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 28 | #include "base/stl_util.h" |
David Sehr | 8f4b056 | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 29 | #include "base/utils.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 30 | #include "common_runtime_test.h" |
| 31 | #include "compiler_callbacks.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 32 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 33 | #include "dex/dex_file_loader.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 34 | #include "exec_utils.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 35 | #include "gc/heap.h" |
| 36 | #include "gc/space/image_space.h" |
| 37 | #include "oat_file_assistant.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 38 | #include "runtime.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | |
Calin Juravle | 5a0381f | 2020-11-23 19:21:09 -0800 | [diff] [blame^] | 42 | static constexpr bool kDebugArgs = false; |
| 43 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 44 | // Test class that provides some helpers to set a test up for compilation using dex2oat. |
| 45 | class Dex2oatEnvironmentTest : public CommonRuntimeTest { |
| 46 | public: |
| 47 | virtual void SetUp() OVERRIDE { |
| 48 | CommonRuntimeTest::SetUp(); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 49 | const ArtDexFileLoader dex_file_loader; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 50 | |
| 51 | // Create a scratch directory to work from. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 52 | |
| 53 | // Get the realpath of the android data. The oat dir should always point to real location |
| 54 | // when generating oat files in dalvik-cache. This avoids complicating the unit tests |
| 55 | // when matching the expected paths. |
| 56 | UniqueCPtr<const char[]> android_data_real(realpath(android_data_.c_str(), nullptr)); |
| 57 | ASSERT_TRUE(android_data_real != nullptr) |
| 58 | << "Could not get the realpath of the android data" << android_data_ << strerror(errno); |
| 59 | |
| 60 | scratch_dir_.assign(android_data_real.get()); |
| 61 | scratch_dir_ += "/Dex2oatEnvironmentTest"; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 62 | ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700)); |
| 63 | |
| 64 | // Create a subdirectory in scratch for odex files. |
| 65 | odex_oat_dir_ = scratch_dir_ + "/oat"; |
| 66 | ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700)); |
| 67 | |
| 68 | odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA)); |
| 69 | ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700)); |
| 70 | |
| 71 | // Verify the environment is as we expect |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 72 | std::vector<uint32_t> checksums; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 73 | std::string error_msg; |
| 74 | ASSERT_TRUE(OS::FileExists(GetSystemImageFile().c_str())) |
| 75 | << "Expected pre-compiled boot image to be at: " << GetSystemImageFile(); |
| 76 | ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str())) |
| 77 | << "Expected dex file to be at: " << GetDexSrc1(); |
| 78 | ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str())) |
| 79 | << "Expected stripped dex file to be at: " << GetStrippedDexSrc1(); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 80 | ASSERT_FALSE( |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 81 | dex_file_loader.GetMultiDexChecksums(GetStrippedDexSrc1().c_str(), &checksums, &error_msg)) |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 82 | << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1(); |
| 83 | ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str())) |
| 84 | << "Expected dex file to be at: " << GetDexSrc2(); |
| 85 | |
| 86 | // GetMultiDexSrc2 should have the same primary dex checksum as |
| 87 | // GetMultiDexSrc1, but a different secondary dex checksum. |
| 88 | static constexpr bool kVerifyChecksum = true; |
| 89 | std::vector<std::unique_ptr<const DexFile>> multi1; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 90 | ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc1().c_str(), |
| 91 | GetMultiDexSrc1().c_str(), |
| 92 | /* verify */ true, |
| 93 | kVerifyChecksum, |
| 94 | &error_msg, |
| 95 | &multi1)) << error_msg; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 96 | ASSERT_GT(multi1.size(), 1u); |
| 97 | |
| 98 | std::vector<std::unique_ptr<const DexFile>> multi2; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 99 | ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc2().c_str(), |
| 100 | GetMultiDexSrc2().c_str(), |
| 101 | /* verify */ true, |
| 102 | kVerifyChecksum, |
| 103 | &error_msg, |
| 104 | &multi2)) << error_msg; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 105 | ASSERT_GT(multi2.size(), 1u); |
| 106 | |
| 107 | ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum()); |
| 108 | ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum()); |
| 109 | } |
| 110 | |
| 111 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE { |
| 112 | // options->push_back(std::make_pair("-verbose:oat", nullptr)); |
| 113 | |
| 114 | // Set up the image location. |
| 115 | options->push_back(std::make_pair("-Ximage:" + GetImageLocation(), |
| 116 | nullptr)); |
| 117 | // Make sure compilercallbacks are not set so that relocation will be |
| 118 | // enabled. |
| 119 | callbacks_.reset(); |
| 120 | } |
| 121 | |
| 122 | virtual void TearDown() OVERRIDE { |
| 123 | ClearDirectory(odex_dir_.c_str()); |
| 124 | ASSERT_EQ(0, rmdir(odex_dir_.c_str())); |
| 125 | |
| 126 | ClearDirectory(odex_oat_dir_.c_str()); |
| 127 | ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str())); |
| 128 | |
| 129 | ClearDirectory(scratch_dir_.c_str()); |
| 130 | ASSERT_EQ(0, rmdir(scratch_dir_.c_str())); |
| 131 | |
| 132 | CommonRuntimeTest::TearDown(); |
| 133 | } |
| 134 | |
| 135 | static void Copy(const std::string& src, const std::string& dst) { |
| 136 | std::ifstream src_stream(src, std::ios::binary); |
| 137 | std::ofstream dst_stream(dst, std::ios::binary); |
| 138 | |
| 139 | dst_stream << src_stream.rdbuf(); |
| 140 | } |
| 141 | |
| 142 | // Returns the directory where the pre-compiled core.art can be found. |
| 143 | // TODO: We should factor out this into common tests somewhere rather than |
| 144 | // re-hardcoding it here (This was copied originally from the elf writer |
| 145 | // test). |
| 146 | std::string GetImageDirectory() const { |
| 147 | if (IsHost()) { |
| 148 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 149 | CHECK(host_dir != nullptr); |
| 150 | return std::string(host_dir) + "/framework"; |
| 151 | } else { |
| 152 | return std::string("/data/art-test"); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | std::string GetImageLocation() const { |
| 157 | return GetImageDirectory() + "/core.art"; |
| 158 | } |
| 159 | |
| 160 | std::string GetSystemImageFile() const { |
| 161 | return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA) |
| 162 | + "/core.art"; |
| 163 | } |
| 164 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 165 | // Returns the path to an image location whose contents differ from the |
| 166 | // image at GetImageLocation(). This is used for testing mismatched |
| 167 | // image checksums in the oat_file_assistant_tests. |
| 168 | std::string GetImageLocation2() const { |
Richard Uhler | cdcbddf | 2017-01-19 16:58:39 +0000 | [diff] [blame] | 169 | return GetImageDirectory() + "/core-interpreter.art"; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | std::string GetDexSrc1() const { |
| 173 | return GetTestDexFileName("Main"); |
| 174 | } |
| 175 | |
| 176 | // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex |
| 177 | // file stripped. |
| 178 | std::string GetStrippedDexSrc1() const { |
| 179 | return GetTestDexFileName("MainStripped"); |
| 180 | } |
| 181 | |
| 182 | std::string GetMultiDexSrc1() const { |
| 183 | return GetTestDexFileName("MultiDex"); |
| 184 | } |
| 185 | |
| 186 | // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but |
| 187 | // with the contents of the secondary dex file changed. |
| 188 | std::string GetMultiDexSrc2() const { |
| 189 | return GetTestDexFileName("MultiDexModifiedSecondary"); |
| 190 | } |
| 191 | |
| 192 | std::string GetDexSrc2() const { |
| 193 | return GetTestDexFileName("Nested"); |
| 194 | } |
| 195 | |
| 196 | // Scratch directory, for dex and odex files (oat files will go in the |
| 197 | // dalvik cache). |
| 198 | const std::string& GetScratchDir() const { |
| 199 | return scratch_dir_; |
| 200 | } |
| 201 | |
| 202 | // Odex directory is the subdirectory in the scratch directory where odex |
| 203 | // files should be located. |
| 204 | const std::string& GetOdexDir() const { |
| 205 | return odex_dir_; |
| 206 | } |
| 207 | |
Calin Juravle | 5a0381f | 2020-11-23 19:21:09 -0800 | [diff] [blame^] | 208 | int Dex2Oat( |
| 209 | const std::vector<std::string>& dex2oat_args, |
| 210 | std::string* output, |
| 211 | std::string* error_msg) { |
| 212 | Runtime* runtime = Runtime::Current(); |
| 213 | |
| 214 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
| 215 | runtime->GetHeap()->GetBootImageSpaces(); |
| 216 | if (image_spaces.empty()) { |
| 217 | *error_msg = "No image location found for Dex2Oat."; |
| 218 | return false; |
| 219 | } |
| 220 | std::string image_location = image_spaces[0]->GetImageLocation(); |
| 221 | |
| 222 | std::vector<std::string> argv; |
| 223 | argv.push_back(runtime->GetCompilerExecutable()); |
| 224 | |
| 225 | if (runtime->IsJavaDebuggable()) { |
| 226 | argv.push_back("--debuggable"); |
| 227 | } |
| 228 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
| 229 | |
| 230 | if (!runtime->IsVerificationEnabled()) { |
| 231 | argv.push_back("--compiler-filter=assume-verified"); |
| 232 | } |
| 233 | |
| 234 | if (runtime->MustRelocateIfPossible()) { |
| 235 | argv.push_back("--runtime-arg"); |
| 236 | argv.push_back("-Xrelocate"); |
| 237 | } else { |
| 238 | argv.push_back("--runtime-arg"); |
| 239 | argv.push_back("-Xnorelocate"); |
| 240 | } |
| 241 | |
| 242 | if (!kIsTargetBuild) { |
| 243 | argv.push_back("--host"); |
| 244 | } |
| 245 | |
| 246 | argv.push_back("--boot-image=" + image_location); |
| 247 | |
| 248 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 249 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 250 | |
| 251 | argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end()); |
| 252 | |
| 253 | // We must set --android-root. |
| 254 | const char* android_root = getenv("ANDROID_ROOT"); |
| 255 | CHECK(android_root != nullptr); |
| 256 | argv.push_back("--android-root=" + std::string(android_root)); |
| 257 | |
| 258 | if (kDebugArgs) { |
| 259 | std::string all_args; |
| 260 | for (const std::string& arg : argv) { |
| 261 | all_args += arg + " "; |
| 262 | } |
| 263 | LOG(ERROR) << all_args; |
| 264 | } |
| 265 | |
| 266 | int link[2]; |
| 267 | |
| 268 | if (pipe(link) == -1) { |
| 269 | ::testing::AssertionFailure() << "Failed to pipe: " << *error_msg; |
| 270 | } |
| 271 | |
| 272 | pid_t pid = fork(); |
| 273 | if (pid == -1) { |
| 274 | ::testing::AssertionFailure() << "Failed to fork: " << *error_msg; |
| 275 | } |
| 276 | |
| 277 | if (pid == 0) { |
| 278 | // We need dex2oat to actually log things. |
| 279 | setenv("ANDROID_LOG_TAGS", "*:d", 1); |
| 280 | dup2(link[1], STDERR_FILENO); |
| 281 | close(link[0]); |
| 282 | close(link[1]); |
| 283 | std::vector<const char*> c_args; |
| 284 | for (const std::string& str : argv) { |
| 285 | c_args.push_back(str.c_str()); |
| 286 | } |
| 287 | c_args.push_back(nullptr); |
| 288 | execv(c_args[0], const_cast<char* const*>(c_args.data())); |
| 289 | exit(1); |
| 290 | UNREACHABLE(); |
| 291 | } else { |
| 292 | close(link[1]); |
| 293 | char buffer[128]; |
| 294 | memset(buffer, 0, 128); |
| 295 | ssize_t bytes_read = 0; |
| 296 | |
| 297 | while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) { |
| 298 | *output += std::string(buffer, bytes_read); |
| 299 | } |
| 300 | close(link[0]); |
| 301 | int status = -1; |
| 302 | if (waitpid(pid, &status, 0) != -1) { |
| 303 | ::testing::AssertionFailure() << "dex2oat fork/exec failed: " << *error_msg; |
| 304 | } |
| 305 | return status; |
| 306 | } |
| 307 | } |
| 308 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 309 | private: |
| 310 | std::string scratch_dir_; |
| 311 | std::string odex_oat_dir_; |
| 312 | std::string odex_dir_; |
| 313 | }; |
| 314 | |
| 315 | } // namespace art |
| 316 | |
| 317 | #endif // ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_ |