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 | "AAAAAAIAAQASAAAAAAAAAAEAAAABAAAAAQAAAAIAAAAAAAAAAgAAAAEAAAAGAAAAAQAAAAAAAAAA" |
| 66 | "AAAABgAAAAAAAAAKAAAAAAAAACsDAAAAAAAAAQAAAAAAAAAGAAAAAAAAAAsAAAD0AQAANQMAAAAA" |
| 67 | "AAACAAAAAAAAAAAAAAAAAAAACwAAAAQCAAA/AwAAAAAAAAIAAAAUAwAAGgMAAAEAAAAjAwAAAQAB" |
| 68 | "AAEAAAAFAAAABAAAAHAQBAAAAA4AAQABAAEAAAAFAAAABAAAAHAQBAAAAA4AAQAAAAEAAAAFAAAA" |
| 69 | "CAAAACIAAQBwEAEAAABpAAAADgABAAEAAQAAAAUAAAAEAAAAcBAAAAAADgB4AQAAAAAAAAAAAAAA" |
| 70 | "AAAAhAEAAAAAAAAAAAAAAAAAAAg8Y2xpbml0PgAGPGluaXQ+AANMWDsABUxZJFo7AANMWTsAIkxk" |
| 71 | "YWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2aWsvYW5ub3RhdGlvbi9Jbm5l" |
| 72 | "ckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNzZXM7ABJMamF2YS9sYW5nL09i" |
| 73 | "amVjdDsAAVYABlguamF2YQAGWS5qYXZhAAFaAAthY2Nlc3NGbGFncwASZW1pdHRlcjogamFjay00" |
| 74 | "LjI1AARuYW1lAAR0aGlzAAV2YWx1ZQABegARAAcOABMABw4AEgAHDnYAEQAHDgACAwERGAICBAIN" |
| 75 | "BAgPFwwCBQERHAEYAQAAAQAAgIAEjAMAAAEAAYCABKQDAQACAAAIAoiABLwDAYCABNwDAAAADwAA" |
| 76 | "AAAAAAABAAAAAAAAAAEAAAATAAAAcAAAAAIAAAAIAAAAvAAAAAMAAAABAAAA3AAAAAQAAAABAAAA" |
| 77 | "6AAAAAUAAAAFAAAA8AAAAAYAAAADAAAAGAEAAAMQAAACAAAAeAEAAAEgAAAEAAAAjAEAAAYgAAAC" |
| 78 | "AAAA9AEAAAIgAAATAAAAFAIAAAMgAAAEAAAA/wIAAAQgAAADAAAAFAMAAAAgAAADAAAAKwMAAAAQ" |
| 79 | "AAABAAAAVAMAAA=="; |
| 80 | |
Jeff Hao | f914f8e | 2017-02-21 16:14:23 -0800 | [diff] [blame] | 81 | // Dex file with null value for annotations_off in the annotation_set_ref_list. |
| 82 | // Constructed by building a dex file with annotations and hex editing. |
| 83 | static const char kNullSetRefListElementInputDex[] = |
| 84 | "ZGV4CjAzNQB1iA+7ZwgkF+7E6ZesYFc2lRAR3qnRAanwAwAAcAAAAHhWNBIAAAAAAAAAACADAAAS" |
| 85 | "AAAAcAAAAAgAAAC4AAAAAwAAANgAAAABAAAA/AAAAAQAAAAEAQAAAgAAACQBAACMAgAAZAEAAOgB" |
| 86 | "AADwAQAAAAIAAAMCAAAQAgAAIAIAADQCAABIAgAAawIAAI0CAAC1AgAAyAIAANECAADUAgAA2QIA" |
| 87 | "ANwCAADjAgAA6QIAAAMAAAAEAAAABQAAAAYAAAAHAAAACAAAAAkAAAAMAAAAAgAAAAMAAAAAAAAA" |
| 88 | "DAAAAAcAAAAAAAAADQAAAAcAAADgAQAABgAGAAsAAAAAAAEAAAAAAAAAAgAOAAAAAQAAABAAAAAC" |
| 89 | "AAEAAAAAAAAAAAAAAAAAAgAAAAAAAAABAAAAsAEAAAgDAAAAAAAAAQAAAAEmAAACAAAA2AEAAAoA" |
| 90 | "AADIAQAAFgMAAAAAAAACAAAAAAAAAHwBAAABAAAA/AIAAAAAAAABAAAAAgMAAAEAAQABAAAA8AIA" |
| 91 | "AAQAAABwEAMAAAAOAAIAAgAAAAAA9QIAAAEAAAAOAAAAAAAAAAAAAAAAAAAAAQAAAAEAAABkAQAA" |
| 92 | "cAEAAAAAAAAAAAAAAAAAAAEAAAAEAAAAAgAAAAMAAwAGPGluaXQ+AA5Bbm5vQ2xhc3MuamF2YQAB" |
| 93 | "TAALTEFubm9DbGFzczsADkxNeUFubm90YXRpb247ABJMamF2YS9sYW5nL09iamVjdDsAEkxqYXZh" |
| 94 | "L2xhbmcvU3RyaW5nOwAhTGphdmEvbGFuZy9hbm5vdGF0aW9uL0Fubm90YXRpb247ACBMamF2YS9s" |
| 95 | "YW5nL2Fubm90YXRpb24vUmV0ZW50aW9uOwAmTGphdmEvbGFuZy9hbm5vdGF0aW9uL1JldGVudGlv" |
| 96 | "blBvbGljeTsAEU15QW5ub3RhdGlvbi5qYXZhAAdSVU5USU1FAAFWAANWTEwAAWEABWFOYW1lAARu" |
| 97 | "YW1lAAV2YWx1ZQABAAcOAAICAAAHDgABBQERGwABAQEQFw8AAAIAAICABIQDAQmcAwAAAAECgQgA" |
| 98 | "AAARAAAAAAAAAAEAAAAAAAAAAQAAABIAAABwAAAAAgAAAAgAAAC4AAAAAwAAAAMAAADYAAAABAAA" |
| 99 | "AAEAAAD8AAAABQAAAAQAAAAEAQAABgAAAAIAAAAkAQAAAhAAAAEAAABkAQAAAxAAAAMAAABwAQAA" |
| 100 | "ASAAAAIAAACEAQAABiAAAAIAAACwAQAAARAAAAIAAADYAQAAAiAAABIAAADoAQAAAyAAAAIAAADw" |
| 101 | "AgAABCAAAAIAAAD8AgAAACAAAAIAAAAIAwAAABAAAAEAAAAgAwAA"; |
| 102 | |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame^] | 103 | // Dex file with catch handler unreferenced by try blocks. |
| 104 | // Constructed by building a dex file with try/catch blocks and hex editing. |
| 105 | static const char kUnreferencedCatchHandlerInputDex[] = |
| 106 | "ZGV4CjAzNQD+exd52Y0f9nY5x5GmInXq5nXrO6Kl2RV4AwAAcAAAAHhWNBIAAAAAAAAAANgCAAAS" |
| 107 | "AAAAcAAAAAgAAAC4AAAAAwAAANgAAAABAAAA/AAAAAQAAAAEAQAAAQAAACQBAAA0AgAARAEAANYB" |
| 108 | "AADeAQAA5gEAAO4BAAAAAgAADwIAACYCAAA9AgAAUQIAAGUCAAB5AgAAfwIAAIUCAACIAgAAjAIA" |
| 109 | "AKECAACnAgAArAIAAAQAAAAFAAAABgAAAAcAAAAIAAAACQAAAAwAAAAOAAAADAAAAAYAAAAAAAAA" |
| 110 | "DQAAAAYAAADIAQAADQAAAAYAAADQAQAABQABABAAAAAAAAAAAAAAAAAAAgAPAAAAAQABABEAAAAD" |
| 111 | "AAAAAAAAAAAAAAABAAAAAwAAAAAAAAADAAAAAAAAAMgCAAAAAAAAAQABAAEAAAC1AgAABAAAAHAQ" |
| 112 | "AwAAAA4AAwABAAIAAgC6AgAAIQAAAGIAAAAaAQoAbiACABAAYgAAABoBCwBuIAIAEAAOAA0AYgAA" |
| 113 | "ABoBAQBuIAIAEAAo8A0AYgAAABoBAgBuIAIAEAAo7gAAAAAAAAcAAQAHAAAABwABAAIBAg8BAhgA" |
| 114 | "AQAAAAQAAAABAAAABwAGPGluaXQ+AAZDYXRjaDEABkNhdGNoMgAQSGFuZGxlclRlc3QuamF2YQAN" |
| 115 | "TEhhbmRsZXJUZXN0OwAVTGphdmEvaW8vUHJpbnRTdHJlYW07ABVMamF2YS9sYW5nL0V4Y2VwdGlv" |
| 116 | "bjsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABJMamF2YS9sYW5nL1N5" |
| 117 | "c3RlbTsABFRyeTEABFRyeTIAAVYAAlZMABNbTGphdmEvbGFuZy9TdHJpbmc7AARtYWluAANvdXQA" |
| 118 | "B3ByaW50bG4AAQAHDgAEAQAHDn17AncdHoseAAAAAgAAgYAExAIBCdwCAAANAAAAAAAAAAEAAAAA" |
| 119 | "AAAAAQAAABIAAABwAAAAAgAAAAgAAAC4AAAAAwAAAAMAAADYAAAABAAAAAEAAAD8AAAABQAAAAQA" |
| 120 | "AAAEAQAABgAAAAEAAAAkAQAAASAAAAIAAABEAQAAARAAAAIAAADIAQAAAiAAABIAAADWAQAAAyAA" |
| 121 | "AAIAAAC1AgAAACAAAAEAAADIAgAAABAAAAEAAADYAgAA"; |
| 122 | |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame] | 123 | static void WriteBase64ToFile(const char* base64, File* file) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 124 | // Decode base64. |
| 125 | CHECK(base64 != nullptr); |
| 126 | size_t length; |
| 127 | std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length)); |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame] | 128 | CHECK(bytes != nullptr); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 129 | if (!file->WriteFully(bytes.get(), length)) { |
| 130 | PLOG(FATAL) << "Failed to write base64 as file"; |
| 131 | } |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void WriteFileBase64(const char* base64, const char* location) { |
| 135 | // Write to provided file. |
| 136 | std::unique_ptr<File> file(OS::CreateEmptyFile(location)); |
| 137 | CHECK(file != nullptr); |
| 138 | WriteBase64ToFile(base64, file.get()); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 139 | if (file->FlushCloseOrErase() != 0) { |
| 140 | PLOG(FATAL) << "Could not flush and close test file."; |
| 141 | } |
| 142 | } |
| 143 | |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 144 | class DexLayoutTest : public CommonRuntimeTest { |
| 145 | protected: |
| 146 | virtual void SetUp() { |
| 147 | CommonRuntimeTest::SetUp(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 150 | // Runs FullPlainOutput test. |
| 151 | bool FullPlainOutputExec(std::string* error_msg) { |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 152 | // TODO: dexdump2 -> dexdump ? |
| 153 | ScratchFile dexdump_output; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 154 | const std::string& dexdump_filename = dexdump_output.GetFilename(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 155 | std::string dexdump = GetTestAndroidRoot() + "/bin/dexdump2"; |
| 156 | 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] | 157 | |
| 158 | ScratchFile dexlayout_output; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 159 | const std::string& dexlayout_filename = dexlayout_output.GetFilename(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 160 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 161 | 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] | 162 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 163 | for (const std::string &dex_file : GetLibCoreDexFileNames()) { |
| 164 | std::vector<std::string> dexdump_exec_argv = |
| 165 | { dexdump, "-d", "-f", "-h", "-l", "plain", "-o", dexdump_filename, dex_file }; |
| 166 | std::vector<std::string> dexlayout_exec_argv = |
| 167 | { dexlayout, "-d", "-f", "-h", "-l", "plain", "-o", dexlayout_filename, dex_file }; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 168 | if (!::art::Exec(dexdump_exec_argv, error_msg)) { |
| 169 | return false; |
| 170 | } |
| 171 | if (!::art::Exec(dexlayout_exec_argv, error_msg)) { |
| 172 | return false; |
| 173 | } |
| 174 | std::vector<std::string> diff_exec_argv = |
| 175 | { "/usr/bin/diff", dexdump_filename, dexlayout_filename }; |
| 176 | if (!::art::Exec(diff_exec_argv, error_msg)) { |
| 177 | return false; |
| 178 | } |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 183 | // Runs DexFileOutput test. |
| 184 | bool DexFileOutputExec(std::string* error_msg) { |
| 185 | ScratchFile tmp_file; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 186 | const std::string& tmp_name = tmp_file.GetFilename(); |
| 187 | size_t tmp_last_slash = tmp_name.rfind('/'); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 188 | std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1); |
| 189 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 190 | EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path"; |
| 191 | |
| 192 | for (const std::string &dex_file : GetLibCoreDexFileNames()) { |
| 193 | std::vector<std::string> dexlayout_exec_argv = |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 194 | { dexlayout, "-w", tmp_dir, "-o", tmp_name, dex_file }; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 195 | if (!::art::Exec(dexlayout_exec_argv, error_msg)) { |
| 196 | return false; |
| 197 | } |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 198 | size_t dex_file_last_slash = dex_file.rfind("/"); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 199 | std::string dex_file_name = dex_file.substr(dex_file_last_slash + 1); |
| 200 | std::vector<std::string> unzip_exec_argv = |
| 201 | { "/usr/bin/unzip", dex_file, "classes.dex", "-d", tmp_dir}; |
| 202 | if (!::art::Exec(unzip_exec_argv, error_msg)) { |
| 203 | return false; |
| 204 | } |
| 205 | std::vector<std::string> diff_exec_argv = |
| 206 | { "/usr/bin/diff", tmp_dir + "classes.dex" , tmp_dir + dex_file_name }; |
| 207 | if (!::art::Exec(diff_exec_argv, error_msg)) { |
| 208 | return false; |
| 209 | } |
| 210 | std::vector<std::string> rm_zip_exec_argv = { "/bin/rm", tmp_dir + "classes.dex" }; |
| 211 | if (!::art::Exec(rm_zip_exec_argv, error_msg)) { |
| 212 | return false; |
| 213 | } |
| 214 | std::vector<std::string> rm_out_exec_argv = { "/bin/rm", tmp_dir + dex_file_name }; |
| 215 | if (!::art::Exec(rm_out_exec_argv, error_msg)) { |
| 216 | return false; |
| 217 | } |
| 218 | } |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 219 | return true; |
| 220 | } |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 221 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 222 | // Runs DexFileOutput test. |
| 223 | bool DexFileLayoutExec(std::string* error_msg) { |
| 224 | ScratchFile tmp_file; |
| 225 | std::string tmp_name = tmp_file.GetFilename(); |
| 226 | size_t tmp_last_slash = tmp_name.rfind("/"); |
| 227 | std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1); |
| 228 | |
| 229 | // Write inputs and expected outputs. |
| 230 | std::string dex_file = tmp_dir + "classes.dex"; |
| 231 | WriteFileBase64(kDexFileLayoutInputDex, dex_file.c_str()); |
| 232 | std::string profile_file = tmp_dir + "primary.prof"; |
| 233 | WriteFileBase64(kDexFileLayoutInputProfile, profile_file.c_str()); |
| 234 | std::string expected_output = tmp_dir + "expected.dex"; |
| 235 | WriteFileBase64(kDexFileLayoutExpectedOutputDex, expected_output.c_str()); |
| 236 | std::string output_dex = tmp_dir + "classes.dex.new"; |
| 237 | |
| 238 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 239 | EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path"; |
| 240 | |
| 241 | std::vector<std::string> dexlayout_exec_argv = |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame^] | 242 | { dexlayout, "-w", tmp_dir, "-o", tmp_name, "-p", profile_file, dex_file }; |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 243 | if (!::art::Exec(dexlayout_exec_argv, error_msg)) { |
| 244 | return false; |
| 245 | } |
| 246 | std::vector<std::string> diff_exec_argv = |
| 247 | { "/usr/bin/diff", expected_output, output_dex }; |
| 248 | if (!::art::Exec(diff_exec_argv, error_msg)) { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | std::vector<std::string> rm_exec_argv = |
| 253 | { "/bin/rm", dex_file, profile_file, expected_output, output_dex }; |
| 254 | if (!::art::Exec(rm_exec_argv, error_msg)) { |
| 255 | return false; |
| 256 | } |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 257 | return true; |
| 258 | } |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame^] | 259 | |
| 260 | // Runs UnreferencedCatchHandlerTest. |
| 261 | bool UnreferencedCatchHandlerExec(std::string* error_msg) { |
| 262 | ScratchFile tmp_file; |
| 263 | std::string tmp_name = tmp_file.GetFilename(); |
| 264 | size_t tmp_last_slash = tmp_name.rfind("/"); |
| 265 | std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1); |
| 266 | |
| 267 | // Write inputs and expected outputs. |
| 268 | std::string input_dex = tmp_dir + "classes.dex"; |
| 269 | WriteFileBase64(kUnreferencedCatchHandlerInputDex, input_dex.c_str()); |
| 270 | std::string output_dex = tmp_dir + "classes.dex.new"; |
| 271 | |
| 272 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 273 | EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path"; |
| 274 | |
| 275 | std::vector<std::string> dexlayout_exec_argv = |
| 276 | { dexlayout, "-w", tmp_dir, "-o", "/dev/null", input_dex }; |
| 277 | if (!::art::Exec(dexlayout_exec_argv, error_msg)) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | // Diff input and output. They should be the same. |
| 282 | std::vector<std::string> diff_exec_argv = { "/usr/bin/diff", input_dex, output_dex }; |
| 283 | if (!::art::Exec(diff_exec_argv, error_msg)) { |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | std::vector<std::string> rm_exec_argv = { "/bin/rm", input_dex, output_dex }; |
| 288 | if (!::art::Exec(rm_exec_argv, error_msg)) { |
| 289 | return false; |
| 290 | } |
| 291 | return true; |
| 292 | } |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | |
| 296 | TEST_F(DexLayoutTest, FullPlainOutput) { |
Jeff Hao | 0f7eaeb | 2016-08-31 17:56:13 -0700 | [diff] [blame] | 297 | // Disable test on target. |
| 298 | TEST_DISABLED_FOR_TARGET(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 299 | std::string error_msg; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 300 | ASSERT_TRUE(FullPlainOutputExec(&error_msg)) << error_msg; |
| 301 | } |
| 302 | |
| 303 | TEST_F(DexLayoutTest, DexFileOutput) { |
| 304 | // Disable test on target. |
| 305 | TEST_DISABLED_FOR_TARGET(); |
| 306 | std::string error_msg; |
| 307 | ASSERT_TRUE(DexFileOutputExec(&error_msg)) << error_msg; |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 310 | TEST_F(DexLayoutTest, DexFileLayout) { |
| 311 | // Disable test on target. |
| 312 | TEST_DISABLED_FOR_TARGET(); |
| 313 | std::string error_msg; |
| 314 | ASSERT_TRUE(DexFileLayoutExec(&error_msg)) << error_msg; |
| 315 | } |
| 316 | |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame] | 317 | TEST_F(DexLayoutTest, DuplicateOffset) { |
| 318 | ScratchFile temp; |
| 319 | WriteBase64ToFile(kDexFileDuplicateOffset, temp.GetFile()); |
| 320 | EXPECT_EQ(temp.GetFile()->Flush(), 0); |
| 321 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 322 | EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path"; |
| 323 | std::vector<std::string> dexlayout_exec_argv = { |
| 324 | dexlayout, |
| 325 | "-a", |
| 326 | "-i", |
| 327 | "-o", |
| 328 | "/dev/null", |
| 329 | temp.GetFilename()}; |
| 330 | std::string error_msg; |
| 331 | const bool result = ::art::Exec(dexlayout_exec_argv, &error_msg); |
| 332 | EXPECT_TRUE(result); |
| 333 | if (!result) { |
| 334 | LOG(ERROR) << "Error " << error_msg; |
| 335 | } |
| 336 | } |
| 337 | |
Jeff Hao | f914f8e | 2017-02-21 16:14:23 -0800 | [diff] [blame] | 338 | TEST_F(DexLayoutTest, NullSetRefListElement) { |
| 339 | ScratchFile temp; |
| 340 | WriteBase64ToFile(kNullSetRefListElementInputDex, temp.GetFile()); |
| 341 | EXPECT_EQ(temp.GetFile()->Flush(), 0); |
| 342 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 343 | EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path"; |
| 344 | std::vector<std::string> dexlayout_exec_argv = |
| 345 | { dexlayout, "-o", "/dev/null", temp.GetFilename() }; |
| 346 | std::string error_msg; |
| 347 | const bool result = ::art::Exec(dexlayout_exec_argv, &error_msg); |
| 348 | EXPECT_TRUE(result); |
| 349 | if (!result) { |
| 350 | LOG(ERROR) << "Error " << error_msg; |
| 351 | } |
| 352 | } |
| 353 | |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame^] | 354 | TEST_F(DexLayoutTest, UnreferencedCatchHandler) { |
| 355 | // Disable test on target. |
| 356 | TEST_DISABLED_FOR_TARGET(); |
| 357 | std::string error_msg; |
| 358 | ASSERT_TRUE(UnreferencedCatchHandlerExec(&error_msg)) << error_msg; |
| 359 | } |
| 360 | |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 361 | } // namespace art |