Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include <string> |
| 18 | #include <vector> |
| 19 | #include <sstream> |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 24 | #include "base/unix_file/fd_file.h" |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 25 | #include "common_runtime_test.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 26 | #include "exec_utils.h" |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 27 | #include "utils.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 31 | static const char kDexFileLayoutInputDex[] = |
| 32 | "ZGV4CjAzNQD1KW3+B8NAB0f2A/ZVIBJ0aHrGIqcpVTAUAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAH" |
| 33 | "AAAAcAAAAAQAAACMAAAAAQAAAJwAAAAAAAAAAAAAAAMAAACoAAAAAgAAAMAAAAAUAQAAAAEAADAB" |
| 34 | "AAA4AQAAQAEAAEgBAABNAQAAUgEAAGYBAAADAAAABAAAAAUAAAAGAAAABgAAAAMAAAAAAAAAAAAA" |
| 35 | "AAAAAAABAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAEAAAAAAAAAdQEAAAAAAAABAAAA" |
| 36 | "AAAAAAIAAAAAAAAAAgAAAAAAAAB/AQAAAAAAAAEAAQABAAAAaQEAAAQAAABwEAIAAAAOAAEAAQAB" |
| 37 | "AAAAbwEAAAQAAABwEAIAAAAOAAY8aW5pdD4ABkEuamF2YQAGQi5qYXZhAANMQTsAA0xCOwASTGph" |
| 38 | "dmEvbGFuZy9PYmplY3Q7AAFWAAQABw48AAQABw48AAAAAQAAgIAEgAIAAAEAAYCABJgCAAAACwAA" |
| 39 | "AAAAAAABAAAAAAAAAAEAAAAHAAAAcAAAAAIAAAAEAAAAjAAAAAMAAAABAAAAnAAAAAUAAAADAAAA" |
| 40 | "qAAAAAYAAAACAAAAwAAAAAEgAAACAAAAAAEAAAIgAAAHAAAAMAEAAAMgAAACAAAAaQEAAAAgAAAC" |
| 41 | "AAAAdQEAAAAQAAABAAAAjAEAAA=="; |
| 42 | |
| 43 | static const char kDexFileLayoutInputProfile[] = |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 44 | "cHJvADAwMwABCwABAAAAAAD1KW3+Y2xhc3Nlcy5kZXgBAA=="; |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 45 | |
| 46 | static const char kDexFileLayoutExpectedOutputDex[] = |
| 47 | "ZGV4CjAzNQD1KW3+B8NAB0f2A/ZVIBJ0aHrGIqcpVTAUAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAH" |
| 48 | "AAAAcAAAAAQAAACMAAAAAQAAAJwAAAAAAAAAAAAAAAMAAACoAAAAAgAAAMAAAAAUAQAAAAEAADAB" |
| 49 | "AAA4AQAAQAEAAEgBAABNAQAAUgEAAGYBAAADAAAABAAAAAUAAAAGAAAABgAAAAMAAAAAAAAAAAAA" |
| 50 | "AAAAAAABAAAAAAAAAAIAAAAAAAAAAQAAAAAAAAACAAAAAAAAAAIAAAAAAAAAdQEAAAAAAAAAAAAA" |
| 51 | "AAAAAAIAAAAAAAAAAQAAAAAAAAB/AQAAAAAAAAEAAQABAAAAbwEAAAQAAABwEAIAAAAOAAEAAQAB" |
| 52 | "AAAAaQEAAAQAAABwEAIAAAAOAAY8aW5pdD4ABkEuamF2YQAGQi5qYXZhAANMQTsAA0xCOwASTGph" |
| 53 | "dmEvbGFuZy9PYmplY3Q7AAFWAAQABw48AAQABw48AAAAAQABgIAEgAIAAAEAAICABJgCAAAACwAA" |
| 54 | "AAAAAAABAAAAAAAAAAEAAAAHAAAAcAAAAAIAAAAEAAAAjAAAAAMAAAABAAAAnAAAAAUAAAADAAAA" |
| 55 | "qAAAAAYAAAACAAAAwAAAAAEgAAACAAAAAAEAAAIgAAAHAAAAMAEAAAMgAAACAAAAaQEAAAAgAAAC" |
| 56 | "AAAAdQEAAAAQAAABAAAAjAEAAA=="; |
| 57 | |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame^] | 58 | // Dex file with multiple code items that have the same debug_info_off_. Constructed by a modified |
| 59 | // dexlayout on XandY. |
| 60 | static const char kDexFileDuplicateOffset[] = |
| 61 | "ZGV4CjAzNwAQfXfPCB8qCxo7MqdFhmHZQwCv8+udHD8MBAAAcAAAAHhWNBIAAAAAAAAAAFQDAAAT" |
| 62 | "AAAAcAAAAAgAAAC8AAAAAQAAANwAAAABAAAA6AAAAAUAAADwAAAAAwAAABgBAACUAgAAeAEAABQC" |
| 63 | "AAAeAgAAJgIAACsCAAAyAgAANwIAAFsCAAB7AgAAngIAALICAAC1AgAAvQIAAMUCAADIAgAA1QIA" |
| 64 | "AOkCAADvAgAA9QIAAPwCAAACAAAAAwAAAAQAAAAFAAAABgAAAAcAAAAIAAAACQAAAAkAAAAHAAAA" |
| 65 | "AAAAAAIAAQASAAAAAAAAAAEAAAABAAAAAQAAAAIAAAAAAAAAAgAAAAEAAAAGA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AAAA/wIAAAQgAAADAAAAFAMAAAAgAAADAAAAKwMAAAAQ" |
| 79 | "AAABAAAAVAMAAA=="; |
| 80 | |
| 81 | static void WriteBase64ToFile(const char* base64, File* file) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 82 | // Decode base64. |
| 83 | CHECK(base64 != nullptr); |
| 84 | size_t length; |
| 85 | std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length)); |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame^] | 86 | CHECK(bytes != nullptr); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 87 | if (!file->WriteFully(bytes.get(), length)) { |
| 88 | PLOG(FATAL) << "Failed to write base64 as file"; |
| 89 | } |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame^] | 90 | } |
| 91 | |
| 92 | static void WriteFileBase64(const char* base64, const char* location) { |
| 93 | // Write to provided file. |
| 94 | std::unique_ptr<File> file(OS::CreateEmptyFile(location)); |
| 95 | CHECK(file != nullptr); |
| 96 | WriteBase64ToFile(base64, file.get()); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 97 | if (file->FlushCloseOrErase() != 0) { |
| 98 | PLOG(FATAL) << "Could not flush and close test file."; |
| 99 | } |
| 100 | } |
| 101 | |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 102 | class DexLayoutTest : public CommonRuntimeTest { |
| 103 | protected: |
| 104 | virtual void SetUp() { |
| 105 | CommonRuntimeTest::SetUp(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 108 | // Runs FullPlainOutput test. |
| 109 | bool FullPlainOutputExec(std::string* error_msg) { |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 110 | // TODO: dexdump2 -> dexdump ? |
| 111 | ScratchFile dexdump_output; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 112 | const std::string& dexdump_filename = dexdump_output.GetFilename(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 113 | std::string dexdump = GetTestAndroidRoot() + "/bin/dexdump2"; |
| 114 | EXPECT_TRUE(OS::FileExists(dexdump.c_str())) << dexdump << " should be a valid file path"; |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 115 | |
| 116 | ScratchFile dexlayout_output; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 117 | const std::string& dexlayout_filename = dexlayout_output.GetFilename(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 118 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 119 | EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path"; |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 120 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 121 | for (const std::string &dex_file : GetLibCoreDexFileNames()) { |
| 122 | std::vector<std::string> dexdump_exec_argv = |
| 123 | { dexdump, "-d", "-f", "-h", "-l", "plain", "-o", dexdump_filename, dex_file }; |
| 124 | std::vector<std::string> dexlayout_exec_argv = |
| 125 | { dexlayout, "-d", "-f", "-h", "-l", "plain", "-o", dexlayout_filename, dex_file }; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 126 | if (!::art::Exec(dexdump_exec_argv, error_msg)) { |
| 127 | return false; |
| 128 | } |
| 129 | if (!::art::Exec(dexlayout_exec_argv, error_msg)) { |
| 130 | return false; |
| 131 | } |
| 132 | std::vector<std::string> diff_exec_argv = |
| 133 | { "/usr/bin/diff", dexdump_filename, dexlayout_filename }; |
| 134 | if (!::art::Exec(diff_exec_argv, error_msg)) { |
| 135 | return false; |
| 136 | } |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 141 | // Runs DexFileOutput test. |
| 142 | bool DexFileOutputExec(std::string* error_msg) { |
| 143 | ScratchFile tmp_file; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 144 | const std::string& tmp_name = tmp_file.GetFilename(); |
| 145 | size_t tmp_last_slash = tmp_name.rfind('/'); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 146 | std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1); |
| 147 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 148 | EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path"; |
| 149 | |
| 150 | for (const std::string &dex_file : GetLibCoreDexFileNames()) { |
| 151 | std::vector<std::string> dexlayout_exec_argv = |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 152 | { dexlayout, "-w", tmp_dir, "-o", tmp_name, dex_file }; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 153 | if (!::art::Exec(dexlayout_exec_argv, error_msg)) { |
| 154 | return false; |
| 155 | } |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 156 | size_t dex_file_last_slash = dex_file.rfind("/"); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 157 | std::string dex_file_name = dex_file.substr(dex_file_last_slash + 1); |
| 158 | std::vector<std::string> unzip_exec_argv = |
| 159 | { "/usr/bin/unzip", dex_file, "classes.dex", "-d", tmp_dir}; |
| 160 | if (!::art::Exec(unzip_exec_argv, error_msg)) { |
| 161 | return false; |
| 162 | } |
| 163 | std::vector<std::string> diff_exec_argv = |
| 164 | { "/usr/bin/diff", tmp_dir + "classes.dex" , tmp_dir + dex_file_name }; |
| 165 | if (!::art::Exec(diff_exec_argv, error_msg)) { |
| 166 | return false; |
| 167 | } |
| 168 | std::vector<std::string> rm_zip_exec_argv = { "/bin/rm", tmp_dir + "classes.dex" }; |
| 169 | if (!::art::Exec(rm_zip_exec_argv, error_msg)) { |
| 170 | return false; |
| 171 | } |
| 172 | std::vector<std::string> rm_out_exec_argv = { "/bin/rm", tmp_dir + dex_file_name }; |
| 173 | if (!::art::Exec(rm_out_exec_argv, error_msg)) { |
| 174 | return false; |
| 175 | } |
| 176 | } |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 177 | return true; |
| 178 | } |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 179 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 180 | // Runs DexFileOutput test. |
| 181 | bool DexFileLayoutExec(std::string* error_msg) { |
| 182 | ScratchFile tmp_file; |
| 183 | std::string tmp_name = tmp_file.GetFilename(); |
| 184 | size_t tmp_last_slash = tmp_name.rfind("/"); |
| 185 | std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1); |
| 186 | |
| 187 | // Write inputs and expected outputs. |
| 188 | std::string dex_file = tmp_dir + "classes.dex"; |
| 189 | WriteFileBase64(kDexFileLayoutInputDex, dex_file.c_str()); |
| 190 | std::string profile_file = tmp_dir + "primary.prof"; |
| 191 | WriteFileBase64(kDexFileLayoutInputProfile, profile_file.c_str()); |
| 192 | std::string expected_output = tmp_dir + "expected.dex"; |
| 193 | WriteFileBase64(kDexFileLayoutExpectedOutputDex, expected_output.c_str()); |
| 194 | std::string output_dex = tmp_dir + "classes.dex.new"; |
| 195 | |
| 196 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 197 | EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path"; |
| 198 | |
| 199 | std::vector<std::string> dexlayout_exec_argv = |
| 200 | { dexlayout, "-w", tmp_dir, "-o", tmp_name, "-p", profile_file, dex_file }; |
| 201 | if (!::art::Exec(dexlayout_exec_argv, error_msg)) { |
| 202 | return false; |
| 203 | } |
| 204 | std::vector<std::string> diff_exec_argv = |
| 205 | { "/usr/bin/diff", expected_output, output_dex }; |
| 206 | if (!::art::Exec(diff_exec_argv, error_msg)) { |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | std::vector<std::string> rm_exec_argv = |
| 211 | { "/bin/rm", dex_file, profile_file, expected_output, output_dex }; |
| 212 | if (!::art::Exec(rm_exec_argv, error_msg)) { |
| 213 | return false; |
| 214 | } |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 215 | return true; |
| 216 | } |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | |
| 220 | TEST_F(DexLayoutTest, FullPlainOutput) { |
Jeff Hao | 0f7eaeb | 2016-08-31 17:56:13 -0700 | [diff] [blame] | 221 | // Disable test on target. |
| 222 | TEST_DISABLED_FOR_TARGET(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 223 | std::string error_msg; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 224 | ASSERT_TRUE(FullPlainOutputExec(&error_msg)) << error_msg; |
| 225 | } |
| 226 | |
| 227 | TEST_F(DexLayoutTest, DexFileOutput) { |
| 228 | // Disable test on target. |
| 229 | TEST_DISABLED_FOR_TARGET(); |
| 230 | std::string error_msg; |
| 231 | ASSERT_TRUE(DexFileOutputExec(&error_msg)) << error_msg; |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 234 | TEST_F(DexLayoutTest, DexFileLayout) { |
| 235 | // Disable test on target. |
| 236 | TEST_DISABLED_FOR_TARGET(); |
| 237 | std::string error_msg; |
| 238 | ASSERT_TRUE(DexFileLayoutExec(&error_msg)) << error_msg; |
| 239 | } |
| 240 | |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame^] | 241 | TEST_F(DexLayoutTest, DuplicateOffset) { |
| 242 | ScratchFile temp; |
| 243 | WriteBase64ToFile(kDexFileDuplicateOffset, temp.GetFile()); |
| 244 | EXPECT_EQ(temp.GetFile()->Flush(), 0); |
| 245 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 246 | EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path"; |
| 247 | std::vector<std::string> dexlayout_exec_argv = { |
| 248 | dexlayout, |
| 249 | "-a", |
| 250 | "-i", |
| 251 | "-o", |
| 252 | "/dev/null", |
| 253 | temp.GetFilename()}; |
| 254 | std::string error_msg; |
| 255 | const bool result = ::art::Exec(dexlayout_exec_argv, &error_msg); |
| 256 | EXPECT_TRUE(result); |
| 257 | if (!result) { |
| 258 | LOG(ERROR) << "Error " << error_msg; |
| 259 | } |
| 260 | } |
| 261 | |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 262 | } // namespace art |