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 | c431b9d | 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 | c431b9d | 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 | 858cfd8 | 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: |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 47 | void SetUp() override { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 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; |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 73 | std::vector<std::string> dex_locations; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 74 | std::string error_msg; |
| 75 | ASSERT_TRUE(OS::FileExists(GetSystemImageFile().c_str())) |
| 76 | << "Expected pre-compiled boot image to be at: " << GetSystemImageFile(); |
| 77 | ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str())) |
| 78 | << "Expected dex file to be at: " << GetDexSrc1(); |
Calin Juravle | 5ff2393 | 2020-12-11 18:26:14 -0800 | [diff] [blame] | 79 | ASSERT_TRUE(OS::FileExists(GetResourceOnlySrc1().c_str())) |
| 80 | << "Expected stripped dex file to be at: " << GetResourceOnlySrc1(); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 81 | ASSERT_FALSE( |
Calin Juravle | 6e6f1b2 | 2020-12-15 19:13:19 -0800 | [diff] [blame] | 82 | dex_file_loader.GetMultiDexChecksums( |
| 83 | GetResourceOnlySrc1().c_str(), &checksums, &dex_locations, &error_msg)) |
Calin Juravle | 5ff2393 | 2020-12-11 18:26:14 -0800 | [diff] [blame] | 84 | << "Expected stripped dex file to be stripped: " << GetResourceOnlySrc1(); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 85 | ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str())) |
| 86 | << "Expected dex file to be at: " << GetDexSrc2(); |
| 87 | |
| 88 | // GetMultiDexSrc2 should have the same primary dex checksum as |
| 89 | // GetMultiDexSrc1, but a different secondary dex checksum. |
| 90 | static constexpr bool kVerifyChecksum = true; |
| 91 | std::vector<std::unique_ptr<const DexFile>> multi1; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 92 | ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc1().c_str(), |
| 93 | GetMultiDexSrc1().c_str(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 94 | /* verify= */ true, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 95 | kVerifyChecksum, |
| 96 | &error_msg, |
| 97 | &multi1)) << error_msg; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 98 | ASSERT_GT(multi1.size(), 1u); |
| 99 | |
| 100 | std::vector<std::unique_ptr<const DexFile>> multi2; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 101 | ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc2().c_str(), |
| 102 | GetMultiDexSrc2().c_str(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 103 | /* verify= */ true, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 104 | kVerifyChecksum, |
| 105 | &error_msg, |
| 106 | &multi2)) << error_msg; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 107 | ASSERT_GT(multi2.size(), 1u); |
| 108 | |
| 109 | ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum()); |
| 110 | ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum()); |
| 111 | } |
| 112 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 113 | void SetUpRuntimeOptions(RuntimeOptions* options) override { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 114 | // options->push_back(std::make_pair("-verbose:oat", nullptr)); |
| 115 | |
| 116 | // Set up the image location. |
| 117 | options->push_back(std::make_pair("-Ximage:" + GetImageLocation(), |
| 118 | nullptr)); |
| 119 | // Make sure compilercallbacks are not set so that relocation will be |
| 120 | // enabled. |
| 121 | callbacks_.reset(); |
| 122 | } |
| 123 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 124 | void TearDown() override { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 125 | ClearDirectory(odex_dir_.c_str()); |
| 126 | ASSERT_EQ(0, rmdir(odex_dir_.c_str())); |
| 127 | |
| 128 | ClearDirectory(odex_oat_dir_.c_str()); |
| 129 | ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str())); |
| 130 | |
| 131 | ClearDirectory(scratch_dir_.c_str()); |
| 132 | ASSERT_EQ(0, rmdir(scratch_dir_.c_str())); |
| 133 | |
| 134 | CommonRuntimeTest::TearDown(); |
| 135 | } |
| 136 | |
| 137 | static void Copy(const std::string& src, const std::string& dst) { |
| 138 | std::ifstream src_stream(src, std::ios::binary); |
| 139 | std::ofstream dst_stream(dst, std::ios::binary); |
| 140 | |
| 141 | dst_stream << src_stream.rdbuf(); |
| 142 | } |
| 143 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 144 | std::string GetDexSrc1() const { |
| 145 | return GetTestDexFileName("Main"); |
| 146 | } |
| 147 | |
| 148 | // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex |
| 149 | // file stripped. |
Calin Juravle | 5ff2393 | 2020-12-11 18:26:14 -0800 | [diff] [blame] | 150 | std::string GetResourceOnlySrc1() const { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 151 | return GetTestDexFileName("MainStripped"); |
| 152 | } |
| 153 | |
| 154 | std::string GetMultiDexSrc1() const { |
| 155 | return GetTestDexFileName("MultiDex"); |
| 156 | } |
| 157 | |
| 158 | // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but |
| 159 | // with the contents of the secondary dex file changed. |
| 160 | std::string GetMultiDexSrc2() const { |
| 161 | return GetTestDexFileName("MultiDexModifiedSecondary"); |
| 162 | } |
| 163 | |
| 164 | std::string GetDexSrc2() const { |
| 165 | return GetTestDexFileName("Nested"); |
| 166 | } |
| 167 | |
| 168 | // Scratch directory, for dex and odex files (oat files will go in the |
| 169 | // dalvik cache). |
| 170 | const std::string& GetScratchDir() const { |
| 171 | return scratch_dir_; |
| 172 | } |
| 173 | |
| 174 | // Odex directory is the subdirectory in the scratch directory where odex |
| 175 | // files should be located. |
| 176 | const std::string& GetOdexDir() const { |
| 177 | return odex_dir_; |
| 178 | } |
| 179 | |
Calin Juravle | 858cfd8 | 2020-11-23 19:21:09 -0800 | [diff] [blame] | 180 | int Dex2Oat(const std::vector<std::string>& dex2oat_args, |
| 181 | std::string* output, |
| 182 | std::string* error_msg) { |
| 183 | std::vector<std::string> argv; |
| 184 | if (!CommonRuntimeTest::StartDex2OatCommandLine(&argv, error_msg)) { |
| 185 | ::testing::AssertionFailure() << "Could not start dex2oat cmd line " << *error_msg; |
| 186 | } |
| 187 | |
| 188 | Runtime* runtime = Runtime::Current(); |
| 189 | if (!runtime->IsVerificationEnabled()) { |
| 190 | argv.push_back("--compiler-filter=assume-verified"); |
| 191 | } |
| 192 | |
| 193 | if (runtime->MustRelocateIfPossible()) { |
| 194 | argv.push_back("--runtime-arg"); |
| 195 | argv.push_back("-Xrelocate"); |
| 196 | } else { |
| 197 | argv.push_back("--runtime-arg"); |
| 198 | argv.push_back("-Xnorelocate"); |
| 199 | } |
| 200 | |
| 201 | if (!kIsTargetBuild) { |
| 202 | argv.push_back("--host"); |
| 203 | } |
| 204 | |
| 205 | argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end()); |
| 206 | |
| 207 | // We must set --android-root. |
| 208 | const char* android_root = getenv("ANDROID_ROOT"); |
| 209 | CHECK(android_root != nullptr); |
| 210 | argv.push_back("--android-root=" + std::string(android_root)); |
| 211 | |
| 212 | if (kDebugArgs) { |
| 213 | std::string all_args; |
| 214 | for (const std::string& arg : argv) { |
| 215 | all_args += arg + " "; |
| 216 | } |
| 217 | LOG(ERROR) << all_args; |
| 218 | } |
| 219 | |
| 220 | // We need dex2oat to actually log things. |
| 221 | auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:d", 1) == 0; }; |
| 222 | ForkAndExecResult res = ForkAndExec(argv, post_fork_fn, output); |
| 223 | if (res.stage != ForkAndExecResult::kFinished) { |
| 224 | *error_msg = strerror(errno); |
| 225 | ::testing::AssertionFailure() << "Failed to finish dex2oat invocation: " << *error_msg; |
| 226 | } |
| 227 | |
| 228 | if (!res.StandardSuccess()) { |
| 229 | // We cannot use ASSERT_TRUE since the method returns an int and not void. |
| 230 | ::testing::AssertionFailure() << "dex2oat fork/exec failed: " << *error_msg; |
| 231 | } |
| 232 | |
| 233 | return res.status_code; |
| 234 | } |
| 235 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 236 | private: |
| 237 | std::string scratch_dir_; |
| 238 | std::string odex_oat_dir_; |
| 239 | std::string odex_dir_; |
| 240 | }; |
| 241 | |
| 242 | } // namespace art |
| 243 | |
| 244 | #endif // ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_ |