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