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 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include <sstream> |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <vector> |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 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 | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 26 | #include "dex/art_dex_file_loader.h" |
David Sehr | e112340 | 2018-02-01 02:46:18 -0800 | [diff] [blame^] | 27 | #include "dex/base64_test_util.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 28 | #include "dex/code_item_accessors-inl.h" |
| 29 | #include "dex/dex_file-inl.h" |
| 30 | #include "dex/dex_file_loader.h" |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 31 | #include "dexlayout.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 32 | #include "exec_utils.h" |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 33 | #include "jit/profile_compilation_info.h" |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 34 | #include "utils.h" |
| 35 | |
| 36 | namespace art { |
| 37 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 38 | static const char kDexFileLayoutInputDex[] = |
| 39 | "ZGV4CjAzNQD1KW3+B8NAB0f2A/ZVIBJ0aHrGIqcpVTAUAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAH" |
| 40 | "AAAAcAAAAAQAAACMAAAAAQAAAJwAAAAAAAAAAAAAAAMAAACoAAAAAgAAAMAAAAAUAQAAAAEAADAB" |
| 41 | "AAA4AQAAQAEAAEgBAABNAQAAUgEAAGYBAAADAAAABAAAAAUAAAAGAAAABgAAAAMAAAAAAAAAAAAA" |
| 42 | "AAAAAAABAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAEAAAAAAAAAdQEAAAAAAAABAAAA" |
| 43 | "AAAAAAIAAAAAAAAAAgAAAAAAAAB/AQAAAAAAAAEAAQABAAAAaQEAAAQAAABwEAIAAAAOAAEAAQAB" |
| 44 | "AAAAbwEAAAQAAABwEAIAAAAOAAY8aW5pdD4ABkEuamF2YQAGQi5qYXZhAANMQTsAA0xCOwASTGph" |
| 45 | "dmEvbGFuZy9PYmplY3Q7AAFWAAQABw48AAQABw48AAAAAQAAgIAEgAIAAAEAAYCABJgCAAAACwAA" |
| 46 | "AAAAAAABAAAAAAAAAAEAAAAHAAAAcAAAAAIAAAAEAAAAjAAAAAMAAAABAAAAnAAAAAUAAAADAAAA" |
| 47 | "qAAAAAYAAAACAAAAwAAAAAEgAAACAAAAAAEAAAIgAAAHAAAAMAEAAAMgAAACAAAAaQEAAAAgAAAC" |
| 48 | "AAAAdQEAAAAQAAABAAAAjAEAAA=="; |
| 49 | |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 50 | // Dex file with catch handler unreferenced by try blocks. |
| 51 | // Constructed by building a dex file with try/catch blocks and hex editing. |
| 52 | static const char kUnreferencedCatchHandlerInputDex[] = |
| 53 | "ZGV4CjAzNQD+exd52Y0f9nY5x5GmInXq5nXrO6Kl2RV4AwAAcAAAAHhWNBIAAAAAAAAAANgCAAAS" |
| 54 | "AAAAcAAAAAgAAAC4AAAAAwAAANgAAAABAAAA/AAAAAQAAAAEAQAAAQAAACQBAAA0AgAARAEAANYB" |
| 55 | "AADeAQAA5gEAAO4BAAAAAgAADwIAACYCAAA9AgAAUQIAAGUCAAB5AgAAfwIAAIUCAACIAgAAjAIA" |
| 56 | "AKECAACnAgAArAIAAAQAAAAFAAAABgAAAAcAAAAIAAAACQAAAAwAAAAOAAAADAAAAAYAAAAAAAAA" |
| 57 | "DQAAAAYAAADIAQAADQAAAAYAAADQAQAABQABABAAAAAAAAAAAAAAAAAAAgAPAAAAAQABABEAAAAD" |
| 58 | "AAAAAAAAAAAAAAABAAAAAwAAAAAAAAADAAAAAAAAAMgCAAAAAAAAAQABAAEAAAC1AgAABAAAAHAQ" |
| 59 | "AwAAAA4AAwABAAIAAgC6AgAAIQAAAGIAAAAaAQoAbiACABAAYgAAABoBCwBuIAIAEAAOAA0AYgAA" |
| 60 | "ABoBAQBuIAIAEAAo8A0AYgAAABoBAgBuIAIAEAAo7gAAAAAAAAcAAQAHAAAABwABAAIBAg8BAhgA" |
| 61 | "AQAAAAQAAAABAAAABwAGPGluaXQ+AAZDYXRjaDEABkNhdGNoMgAQSGFuZGxlclRlc3QuamF2YQAN" |
| 62 | "TEhhbmRsZXJUZXN0OwAVTGphdmEvaW8vUHJpbnRTdHJlYW07ABVMamF2YS9sYW5nL0V4Y2VwdGlv" |
| 63 | "bjsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABJMamF2YS9sYW5nL1N5" |
| 64 | "c3RlbTsABFRyeTEABFRyeTIAAVYAAlZMABNbTGphdmEvbGFuZy9TdHJpbmc7AARtYWluAANvdXQA" |
| 65 | "B3ByaW50bG4AAQAHDgAEAQAHDn17AncdHoseAAAAAgAAgYAExAIBCdwCAAANAAAAAAAAAAEAAAAA" |
| 66 | "AAAAAQAAABIAAABwAAAAAgAAAAgAAAC4AAAAAwAAAAMAAADYAAAABAAAAAEAAAD8AAAABQAAAAQA" |
| 67 | "AAAEAQAABgAAAAEAAAAkAQAAASAAAAIAAABEAQAAARAAAAIAAADIAQAAAiAAABIAAADWAQAAAyAA" |
| 68 | "AAIAAAC1AgAAACAAAAEAAADIAgAAABAAAAEAAADYAgAA"; |
| 69 | |
Jeff Hao | ac46271 | 2017-03-02 10:59:43 -0800 | [diff] [blame] | 70 | // Dex file with 0-size (catch all only) catch handler unreferenced by try blocks. |
| 71 | // Constructed by building a dex file with try/catch blocks and hex editing. |
| 72 | static const char kUnreferenced0SizeCatchHandlerInputDex[] = |
| 73 | "ZGV4CjAzNQCEbEEvMstSNpQpjPdfMEfUBS48cis2QRJoAwAAcAAAAHhWNBIAAAAAAAAAAMgCAAAR" |
| 74 | "AAAAcAAAAAcAAAC0AAAAAwAAANAAAAABAAAA9AAAAAQAAAD8AAAAAQAAABwBAAAsAgAAPAEAAOoB" |
| 75 | "AADyAQAABAIAABMCAAAqAgAAPgIAAFICAABmAgAAaQIAAG0CAACCAgAAhgIAAIoCAACQAgAAlQIA" |
| 76 | "AJ4CAACiAgAAAgAAAAMAAAAEAAAABQAAAAYAAAAHAAAACQAAAAcAAAAFAAAAAAAAAAgAAAAFAAAA" |
| 77 | "3AEAAAgAAAAFAAAA5AEAAAQAAQANAAAAAAAAAAAAAAAAAAIADAAAAAEAAQAOAAAAAgAAAAAAAAAA" |
| 78 | "AAAAAQAAAAIAAAAAAAAAAQAAAAAAAAC5AgAAAAAAAAEAAQABAAAApgIAAAQAAABwEAMAAAAOAAQA" |
| 79 | "AQACAAIAqwIAAC8AAABiAAAAGgEPAG4gAgAQAGIAAAAaAQoAbiACABAAYgAAABoBEABuIAIAEABi" |
| 80 | "AAAAGgELAG4gAgAQAA4ADQBiAQAAGgIKAG4gAgAhACcADQBiAQAAGgILAG4gAgAhACcAAAAAAAAA" |
| 81 | "BwABAA4AAAAHAAEAAgAdACYAAAABAAAAAwAAAAEAAAAGAAY8aW5pdD4AEEhhbmRsZXJUZXN0Lmph" |
| 82 | "dmEADUxIYW5kbGVyVGVzdDsAFUxqYXZhL2lvL1ByaW50U3RyZWFtOwASTGphdmEvbGFuZy9PYmpl" |
| 83 | "Y3Q7ABJMamF2YS9sYW5nL1N0cmluZzsAEkxqYXZhL2xhbmcvU3lzdGVtOwABVgACVkwAE1tMamF2" |
| 84 | "YS9sYW5nL1N0cmluZzsAAmYxAAJmMgAEbWFpbgADb3V0AAdwcmludGxuAAJ0MQACdDIAAQAHDgAE" |
| 85 | "AQAHDnl7eXkCeB2bAAAAAgAAgYAEvAIBCdQCAA0AAAAAAAAAAQAAAAAAAAABAAAAEQAAAHAAAAAC" |
| 86 | "AAAABwAAALQAAAADAAAAAwAAANAAAAAEAAAAAQAAAPQAAAAFAAAABAAAAPwAAAAGAAAAAQAAABwB" |
| 87 | "AAABIAAAAgAAADwBAAABEAAAAgAAANwBAAACIAAAEQAAAOoBAAADIAAAAgAAAKYCAAAAIAAAAQAA" |
| 88 | "ALkCAAAAEAAAAQAAAMgCAAA="; |
| 89 | |
Jeff Hao | a64a64c | 2017-03-06 15:05:38 -0800 | [diff] [blame] | 90 | // Dex file with an unreferenced catch handler at end of code item. |
| 91 | // Constructed by building a dex file with try/catch blocks and hex editing. |
| 92 | static const char kUnreferencedEndingCatchHandlerInputDex[] = |
| 93 | "ZGV4CjAzNQCEflufI6xGTDDRmLpbfYi6ujPrDLIwvYcEBAAAcAAAAHhWNBIAAAAAAAAAAGQDAAAT" |
| 94 | "AAAAcAAAAAgAAAC8AAAAAwAAANwAAAABAAAAAAEAAAUAAAAIAQAAAQAAADABAAC0AgAAUAEAAE4C" |
| 95 | "AABWAgAAXgIAAGYCAAB4AgAAhwIAAJ4CAAC1AgAAyQIAAN0CAADxAgAA9wIAAP0CAAAAAwAABAMA" |
| 96 | "ABkDAAAcAwAAIgMAACcDAAAEAAAABQAAAAYAAAAHAAAACAAAAAkAAAAMAAAADgAAAAwAAAAGAAAA" |
| 97 | "AAAAAA0AAAAGAAAAQAIAAA0AAAAGAAAASAIAAAUAAQARAAAAAAAAAAAAAAAAAAAADwAAAAAAAgAQ" |
| 98 | "AAAAAQABABIAAAADAAAAAAAAAAAAAAABAAAAAwAAAAAAAAADAAAAAAAAAFADAAAAAAAAAQABAAEA" |
| 99 | "AAAwAwAABAAAAHAQBAAAAA4AAgAAAAIAAgA1AwAAIQAAAGIAAAAaAQoAbiADABAAYgAAABoBCwBu" |
| 100 | "IAMAEAAOAA0AYgAAABoBAQBuIAMAEAAo8A0AYgAAABoBAgBuIAMAEAAo7gAAAAAAAAcAAQAHAAAA" |
| 101 | "BwABAAIBAg8BAhgAAwABAAIAAgBCAwAAIQAAAGIAAAAaAQoAbiADABAAYgAAABoBCwBuIAMAEAAO" |
| 102 | "AA0AYgAAABoBAQBuIAMAEAAo8A0AYgAAABoBAgBuIAMAEAAo7gAAAAAAAAcAAQAHAAAABwABAAIB" |
| 103 | "Ag8BAhgAAQAAAAQAAAABAAAABwAGPGluaXQ+AAZDYXRjaDEABkNhdGNoMgAQSGFuZGxlclRlc3Qu" |
| 104 | "amF2YQANTEhhbmRsZXJUZXN0OwAVTGphdmEvaW8vUHJpbnRTdHJlYW07ABVMamF2YS9sYW5nL0V4" |
| 105 | "Y2VwdGlvbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABJMamF2YS9s" |
| 106 | "YW5nL1N5c3RlbTsABFRyeTEABFRyeTIAAVYAAlZMABNbTGphdmEvbGFuZy9TdHJpbmc7AAFhAARt" |
| 107 | "YWluAANvdXQAB3ByaW50bG4AAQAHDgAEAAcOfHsCeB0eih4AEQEABw59ewJ3HR6LHgAAAAMAAIGA" |
| 108 | "BNACAQnoAgEJ1AMAAA0AAAAAAAAAAQAAAAAAAAABAAAAEwAAAHAAAAACAAAACAAAALwAAAADAAAA" |
| 109 | "AwAAANwAAAAEAAAAAQAAAAABAAAFAAAABQAAAAgBAAAGAAAAAQAAADABAAABIAAAAwAAAFABAAAB" |
| 110 | "EAAAAgAAAEACAAACIAAAEwAAAE4CAAADIAAAAwAAADADAAAAIAAAAQAAAFADAAAAEAAAAQAAAGQD" |
| 111 | "AAA="; |
| 112 | |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame] | 113 | // Dex file with multiple code items that have the same debug_info_off_. Constructed by a modified |
| 114 | // dexlayout on XandY. |
| 115 | static const char kDexFileDuplicateOffset[] = |
| 116 | "ZGV4CjAzNwAQfXfPCB8qCxo7MqdFhmHZQwCv8+udHD8MBAAAcAAAAHhWNBIAAAAAAAAAAFQDAAAT" |
| 117 | "AAAAcAAAAAgAAAC8AAAAAQAAANwAAAABAAAA6AAAAAUAAADwAAAAAwAAABgBAACUAgAAeAEAABQC" |
| 118 | "AAAeAgAAJgIAACsCAAAyAgAANwIAAFsCAAB7AgAAngIAALICAAC1AgAAvQIAAMUCAADIAgAA1QIA" |
| 119 | "AOkCAADvAgAA9QIAAPwCAAACAAAAAwAAAAQAAAAFAAAABgAAAAcAAAAIAAAACQAAAAkAAAAHAAAA" |
| 120 | "AAAAAAIAAQASAAAAAAAAAAEAAAABAAAAAQAAAAIAAAAAAAAAAgAAAAEAAAAGAAAAAQAAAAAAAAAA" |
| 121 | "AAAABgAAAAAAAAAKAAAAAAAAACsDAAAAAAAAAQAAAAAAAAAGAAAAAAAAAAsAAAD0AQAANQMAAAAA" |
| 122 | "AAACAAAAAAAAAAAAAAAAAAAACwAAAAQCAAA/AwAAAAAAAAIAAAAUAwAAGgMAAAEAAAAjAwAAAQAB" |
| 123 | "AAEAAAAFAAAABAAAAHAQBAAAAA4AAQABAAEAAAAFAAAABAAAAHAQBAAAAA4AAQAAAAEAAAAFAAAA" |
| 124 | "CAAAACIAAQBwEAEAAABpAAAADgABAAEAAQAAAAUAAAAEAAAAcBAAAAAADgB4AQAAAAAAAAAAAAAA" |
| 125 | "AAAAhAEAAAAAAAAAAAAAAAAAAAg8Y2xpbml0PgAGPGluaXQ+AANMWDsABUxZJFo7AANMWTsAIkxk" |
| 126 | "YWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2aWsvYW5ub3RhdGlvbi9Jbm5l" |
| 127 | "ckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNzZXM7ABJMamF2YS9sYW5nL09i" |
| 128 | "amVjdDsAAVYABlguamF2YQAGWS5qYXZhAAFaAAthY2Nlc3NGbGFncwASZW1pdHRlcjogamFjay00" |
| 129 | "LjI1AARuYW1lAAR0aGlzAAV2YWx1ZQABegARAAcOABMABw4AEgAHDnYAEQAHDgACAwERGAICBAIN" |
| 130 | "BAgPFwwCBQERHAEYAQAAAQAAgIAEjAMAAAEAAYCABKQDAQACAAAIAoiABLwDAYCABNwDAAAADwAA" |
| 131 | "AAAAAAABAAAAAAAAAAEAAAATAAAAcAAAAAIAAAAIAAAAvAAAAAMAAAABAAAA3AAAAAQAAAABAAAA" |
| 132 | "6AAAAAUAAAAFAAAA8AAAAAYAAAADAAAAGAEAAAMQAAACAAAAeAEAAAEgAAAEAAAAjAEAAAYgAAAC" |
| 133 | "AAAA9AEAAAIgAAATAAAAFAIAAAMgAAAEAAAA/wIAAAQgAAADAAAAFAMAAAAgAAADAAAAKwMAAAAQ" |
| 134 | "AAABAAAAVAMAAA=="; |
| 135 | |
Jeff Hao | f914f8e | 2017-02-21 16:14:23 -0800 | [diff] [blame] | 136 | // Dex file with null value for annotations_off in the annotation_set_ref_list. |
| 137 | // Constructed by building a dex file with annotations and hex editing. |
| 138 | static const char kNullSetRefListElementInputDex[] = |
| 139 | "ZGV4CjAzNQB1iA+7ZwgkF+7E6ZesYFc2lRAR3qnRAanwAwAAcAAAAHhWNBIAAAAAAAAAACADAAAS" |
| 140 | "AAAAcAAAAAgAAAC4AAAAAwAAANgAAAABAAAA/AAAAAQAAAAEAQAAAgAAACQBAACMAgAAZAEAAOgB" |
| 141 | "AADwAQAAAAIAAAMCAAAQAgAAIAIAADQCAABIAgAAawIAAI0CAAC1AgAAyAIAANECAADUAgAA2QIA" |
| 142 | "ANwCAADjAgAA6QIAAAMAAAAEAAAABQAAAAYAAAAHAAAACAAAAAkAAAAMAAAAAgAAAAMAAAAAAAAA" |
| 143 | "DAAAAAcAAAAAAAAADQAAAAcAAADgAQAABgAGAAsAAAAAAAEAAAAAAAAAAgAOAAAAAQAAABAAAAAC" |
| 144 | "AAEAAAAAAAAAAAAAAAAAAgAAAAAAAAABAAAAsAEAAAgDAAAAAAAAAQAAAAEmAAACAAAA2AEAAAoA" |
| 145 | "AADIAQAAFgMAAAAAAAACAAAAAAAAAHwBAAABAAAA/AIAAAAAAAABAAAAAgMAAAEAAQABAAAA8AIA" |
| 146 | "AAQAAABwEAMAAAAOAAIAAgAAAAAA9QIAAAEAAAAOAAAAAAAAAAAAAAAAAAAAAQAAAAEAAABkAQAA" |
| 147 | "cAEAAAAAAAAAAAAAAAAAAAEAAAAEAAAAAgAAAAMAAwAGPGluaXQ+AA5Bbm5vQ2xhc3MuamF2YQAB" |
| 148 | "TAALTEFubm9DbGFzczsADkxNeUFubm90YXRpb247ABJMamF2YS9sYW5nL09iamVjdDsAEkxqYXZh" |
| 149 | "L2xhbmcvU3RyaW5nOwAhTGphdmEvbGFuZy9hbm5vdGF0aW9uL0Fubm90YXRpb247ACBMamF2YS9s" |
| 150 | "YW5nL2Fubm90YXRpb24vUmV0ZW50aW9uOwAmTGphdmEvbGFuZy9hbm5vdGF0aW9uL1JldGVudGlv" |
| 151 | "blBvbGljeTsAEU15QW5ub3RhdGlvbi5qYXZhAAdSVU5USU1FAAFWAANWTEwAAWEABWFOYW1lAARu" |
| 152 | "YW1lAAV2YWx1ZQABAAcOAAICAAAHDgABBQERGwABAQEQFw8AAAIAAICABIQDAQmcAwAAAAECgQgA" |
| 153 | "AAARAAAAAAAAAAEAAAAAAAAAAQAAABIAAABwAAAAAgAAAAgAAAC4AAAAAwAAAAMAAADYAAAABAAA" |
| 154 | "AAEAAAD8AAAABQAAAAQAAAAEAQAABgAAAAIAAAAkAQAAAhAAAAEAAABkAQAAAxAAAAMAAABwAQAA" |
| 155 | "ASAAAAIAAACEAQAABiAAAAIAAACwAQAAARAAAAIAAADYAQAAAiAAABIAAADoAQAAAyAAAAIAAADw" |
| 156 | "AgAABCAAAAIAAAD8AgAAACAAAAIAAAAIAwAAABAAAAEAAAAgAwAA"; |
| 157 | |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 158 | // Dex file with shared empty class data item for multiple class defs. |
| 159 | // Constructing by building a dex file with multiple classes and hex editing. |
| 160 | static const char kMultiClassDataInputDex[] = |
| 161 | "ZGV4CjAzNQALJgF9TtnLq748xVe/+wyxETrT9lTEiW6YAQAAcAAAAHhWNBIAAAAAAAAAADQBAAAI" |
| 162 | "AAAAcAAAAAQAAACQAAAAAAAAAAAAAAACAAAAoAAAAAAAAAAAAAAAAgAAALAAAACoAAAA8AAAAPAA" |
| 163 | "AAD4AAAAAAEAAAMBAAAIAQAADQEAACEBAAAkAQAAAgAAAAMAAAAEAAAABQAAAAEAAAAGAAAAAgAA" |
| 164 | "AAcAAAABAAAAAQYAAAMAAAAAAAAAAAAAAAAAAAAnAQAAAAAAAAIAAAABBgAAAwAAAAAAAAABAAAA" |
| 165 | "AAAAACcBAAAAAAAABkEuamF2YQAGQi5qYXZhAAFJAANMQTsAA0xCOwASTGphdmEvbGFuZy9PYmpl" |
| 166 | "Y3Q7AAFhAAFiAAAAAAABAAAAARkAAAAIAAAAAAAAAAEAAAAAAAAAAQAAAAgAAABwAAAAAgAAAAQA" |
| 167 | "AACQAAAABAAAAAIAAACgAAAABgAAAAIAAACwAAAAAiAAAAgAAADwAAAAACAAAAIAAAAnAQAAABAA" |
| 168 | "AAEAAAA0AQAA"; |
| 169 | |
| 170 | // Dex file with code info followed by non 4-byte aligned section. |
| 171 | // Constructed a dex file with code info followed by string data and hex edited. |
| 172 | static const char kUnalignedCodeInfoInputDex[] = |
| 173 | "ZGV4CjAzNQDXJzXNb4iWn2SLhmLydW/8h1K9moERIw7UAQAAcAAAAHhWNBIAAAAAAAAAAEwBAAAG" |
| 174 | "AAAAcAAAAAMAAACIAAAAAQAAAJQAAAAAAAAAAAAAAAMAAACgAAAAAQAAALgAAAD8AAAA2AAAAAIB" |
| 175 | "AAAKAQAAEgEAABcBAAArAQAALgEAAAIAAAADAAAABAAAAAQAAAACAAAAAAAAAAAAAAAAAAAAAAAA" |
| 176 | "AAUAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAABAAAAAAAAADsBAAAAAAAAAQABAAEAAAAxAQAA" |
| 177 | "BAAAAHAQAgAAAA4AAQABAAAAAAA2AQAAAQAAAA4ABjxpbml0PgAGQS5qYXZhAANMQTsAEkxqYXZh" |
| 178 | "L2xhbmcvT2JqZWN0OwABVgABYQABAAcOAAMABw4AAAABAQCBgATYAQEB8AEAAAALAAAAAAAAAAEA" |
| 179 | "AAAAAAAAAQAAAAYAAABwAAAAAgAAAAMAAACIAAAAAwAAAAEAAACUAAAABQAAAAMAAACgAAAABgAA" |
| 180 | "AAEAAAC4AAAAASAAAAIAAADYAAAAAiAAAAYAAAACAQAAAyAAAAIAAAAxAQAAACAAAAEAAAA7AQAA" |
| 181 | "ABAAAAEAAABMAQAA"; |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 182 | |
Jeff Hao | 32a60ab | 2017-03-01 16:44:50 -0800 | [diff] [blame] | 183 | // Dex file with class data section preceding code items. |
| 184 | // Constructed by passing dex file through dexmerger tool and hex editing. |
| 185 | static const char kClassDataBeforeCodeInputDex[] = |
| 186 | "ZGV4CjAzNQCZKmCu3XXn4zvxCh5VH0gZNNobEAcsc49EAgAAcAAAAHhWNBIAAAAAAAAAAAQBAAAJ" |
| 187 | "AAAAcAAAAAQAAACUAAAAAgAAAKQAAAAAAAAAAAAAAAUAAAC8AAAAAQAAAOQAAABAAQAABAEAAPgB" |
| 188 | "AAAAAgAACAIAAAsCAAAQAgAAJAIAACcCAAAqAgAALQIAAAIAAAADAAAABAAAAAUAAAACAAAAAAAA" |
| 189 | "AAAAAAAFAAAAAwAAAAAAAAABAAEAAAAAAAEAAAAGAAAAAQAAAAcAAAABAAAACAAAAAIAAQAAAAAA" |
| 190 | "AQAAAAEAAAACAAAAAAAAAAEAAAAAAAAAjAEAAAAAAAALAAAAAAAAAAEAAAAAAAAAAQAAAAkAAABw" |
| 191 | "AAAAAgAAAAQAAACUAAAAAwAAAAIAAACkAAAABQAAAAUAAAC8AAAABgAAAAEAAADkAAAAABAAAAEA" |
| 192 | "AAAEAQAAACAAAAEAAACMAQAAASAAAAQAAACkAQAAAiAAAAkAAAD4AQAAAyAAAAQAAAAwAgAAAAAB" |
| 193 | "AwCBgASkAwEBvAMBAdADAQHkAwAAAQABAAEAAAAwAgAABAAAAHAQBAAAAA4AAgABAAAAAAA1AgAA" |
| 194 | "AgAAABIQDwACAAEAAAAAADoCAAACAAAAEiAPAAIAAQAAAAAAPwIAAAIAAAASMA8ABjxpbml0PgAG" |
| 195 | "QS5qYXZhAAFJAANMQTsAEkxqYXZhL2xhbmcvT2JqZWN0OwABVgABYQABYgABYwABAAcOAAMABw4A" |
| 196 | "BgAHDgAJAAcOAA=="; |
| 197 | |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 198 | // Dex file with local info containing a null type descriptor. |
| 199 | // Constructed a dex file with debug info sequence containing DBG_RESTART_LOCAL without any |
| 200 | // DBG_START_LOCAL to give it a declared type. |
| 201 | static const char kUnknownTypeDebugInfoInputDex[] = |
| 202 | "ZGV4CjAzNQBtKqZfzjHLNSNwW2A6Bz9FuCEX0sL+FF38AQAAcAAAAHhWNBIAAAAAAAAAAHQBAAAI" |
| 203 | "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAMAQAA8AAAABwB" |
| 204 | "AAAkAQAALAEAAC8BAAA0AQAASAEAAEsBAABOAQAAAgAAAAMAAAAEAAAABQAAAAIAAAAAAAAAAAAA" |
| 205 | "AAUAAAADAAAAAAAAAAEAAQAAAAAAAQAAAAYAAAACAAEAAAAAAAEAAAABAAAAAgAAAAAAAAABAAAA" |
| 206 | "AAAAAGMBAAAAAAAAAQABAAEAAABUAQAABAAAAHAQAgAAAA4AAgABAAAAAABZAQAAAgAAABIQDwAG" |
| 207 | "PGluaXQ+AAZBLmphdmEAAUkAA0xBOwASTGphdmEvbGFuZy9PYmplY3Q7AAFWAAFhAAR0aGlzAAEA" |
| 208 | "Bw4AAwAHDh4GAAYAAAAAAQEAgYAE8AEBAYgCAAAACwAAAAAAAAABAAAAAAAAAAEAAAAIAAAAcAAA" |
| 209 | "AAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAAuAAAAAYAAAABAAAA0AAAAAEgAAACAAAA" |
| 210 | "8AAAAAIgAAAIAAAAHAEAAAMgAAACAAAAVAEAAAAgAAABAAAAYwEAAAAQAAABAAAAdAEAAA=="; |
| 211 | |
Jeff Hao | d212d5b | 2017-04-26 12:09:06 -0700 | [diff] [blame] | 212 | // Dex file with multiple class data items pointing to the same code item. |
| 213 | // Constructed by hex editing. |
| 214 | static const char kDuplicateCodeItemInputDex[] = |
| 215 | "ZGV4CjAzNQCwKtVglQOmLWuHwldN5jkBOInC7mTMhJMAAgAAcAAAAHhWNBIAAAAAAAAAAHgBAAAH" |
| 216 | "AAAAcAAAAAMAAACMAAAAAQAAAJgAAAAAAAAAAAAAAAQAAACkAAAAAQAAAMQAAAAcAQAA5AAAACQB" |
| 217 | "AAAsAQAANAEAADkBAABNAQAAUAEAAFMBAAACAAAAAwAAAAQAAAAEAAAAAgAAAAAAAAAAAAAAAAAA" |
| 218 | "AAAAAAAFAAAAAAAAAAYAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAABAAAAAAAAAGUBAAAAAAAA" |
| 219 | "AQABAAEAAABWAQAABAAAAHAQAwAAAA4AAQABAAAAAABbAQAAAQAAAA4AAAABAAEAAAAAAGABAAAB" |
| 220 | "AAAADgAAAAY8aW5pdD4ABkEuamF2YQADTEE7ABJMamF2YS9sYW5nL09iamVjdDsAAVYAAWEAAWIA" |
| 221 | "AQAHDgADAAcOAAUABw4AAAABAgCBgATkAQEA/AEBAPwBAAsAAAAAAAAAAQAAAAAAAAABAAAABwAA" |
| 222 | "AHAAAAACAAAAAwAAAIwAAAADAAAAAQAAAJgAAAAFAAAABAAAAKQAAAAGAAAAAQAAAMQAAAABIAAA" |
| 223 | "AwAAAOQAAAACIAAABwAAACQBAAADIAAAAwAAAFYBAAAAIAAAAQAAAGUBAAAAEAAAAQAAAHgBAAA="; |
| 224 | |
Mathieu Chartier | a27af08 | 2018-02-03 16:12:23 -0800 | [diff] [blame] | 225 | // Returns the default compact dex option for dexlayout based on kDefaultCompactDexLevel. |
| 226 | static std::vector<std::string> DefaultCompactDexOption() { |
| 227 | return (kDefaultCompactDexLevel == CompactDexLevel::kCompactDexLevelFast) ? |
| 228 | std::vector<std::string>{"-x", "fast"} : std::vector<std::string>{"-x", "none"}; |
| 229 | } |
| 230 | |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame] | 231 | static void WriteBase64ToFile(const char* base64, File* file) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 232 | // Decode base64. |
| 233 | CHECK(base64 != nullptr); |
| 234 | size_t length; |
| 235 | std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length)); |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame] | 236 | CHECK(bytes != nullptr); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 237 | if (!file->WriteFully(bytes.get(), length)) { |
| 238 | PLOG(FATAL) << "Failed to write base64 as file"; |
| 239 | } |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static void WriteFileBase64(const char* base64, const char* location) { |
| 243 | // Write to provided file. |
| 244 | std::unique_ptr<File> file(OS::CreateEmptyFile(location)); |
| 245 | CHECK(file != nullptr); |
| 246 | WriteBase64ToFile(base64, file.get()); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 247 | if (file->FlushCloseOrErase() != 0) { |
| 248 | PLOG(FATAL) << "Could not flush and close test file."; |
| 249 | } |
| 250 | } |
| 251 | |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 252 | class DexLayoutTest : public CommonRuntimeTest { |
| 253 | protected: |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 254 | std::string GetDexLayoutPath() { |
| 255 | return GetTestAndroidRoot() + "/bin/dexlayoutd"; |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 258 | // Runs FullPlainOutput test. |
| 259 | bool FullPlainOutputExec(std::string* error_msg) { |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 260 | // TODO: dexdump2 -> dexdump ? |
| 261 | ScratchFile dexdump_output; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 262 | const std::string& dexdump_filename = dexdump_output.GetFilename(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 263 | std::string dexdump = GetTestAndroidRoot() + "/bin/dexdump2"; |
| 264 | 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] | 265 | |
| 266 | ScratchFile dexlayout_output; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 267 | const std::string& dexlayout_filename = dexlayout_output.GetFilename(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 268 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 269 | for (const std::string &dex_file : GetLibCoreDexFileNames()) { |
| 270 | std::vector<std::string> dexdump_exec_argv = |
| 271 | { dexdump, "-d", "-f", "-h", "-l", "plain", "-o", dexdump_filename, dex_file }; |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 272 | std::vector<std::string> dexlayout_args = |
| 273 | { "-d", "-f", "-h", "-l", "plain", "-o", dexlayout_filename, dex_file }; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 274 | if (!::art::Exec(dexdump_exec_argv, error_msg)) { |
| 275 | return false; |
| 276 | } |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 277 | if (!DexLayoutExec(dexlayout_args, error_msg)) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | std::vector<std::string> diff_exec_argv = |
| 281 | { "/usr/bin/diff", dexdump_filename, dexlayout_filename }; |
| 282 | if (!::art::Exec(diff_exec_argv, error_msg)) { |
| 283 | return false; |
| 284 | } |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 285 | } |
| 286 | return true; |
| 287 | } |
| 288 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 289 | // Runs DexFileOutput test. |
| 290 | bool DexFileOutputExec(std::string* error_msg) { |
| 291 | ScratchFile tmp_file; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 292 | const std::string& tmp_name = tmp_file.GetFilename(); |
| 293 | size_t tmp_last_slash = tmp_name.rfind('/'); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 294 | std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 295 | |
| 296 | for (const std::string &dex_file : GetLibCoreDexFileNames()) { |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 297 | std::vector<std::string> dexlayout_args = |
| 298 | { "-w", tmp_dir, "-o", tmp_name, dex_file }; |
Mathieu Chartier | a27af08 | 2018-02-03 16:12:23 -0800 | [diff] [blame] | 299 | if (!DexLayoutExec(dexlayout_args, error_msg, /*pass_default_cdex_option*/ false)) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |
Andreas Gampe | 5555dd1 | 2017-08-24 13:50:21 -0700 | [diff] [blame] | 302 | size_t dex_file_last_slash = dex_file.rfind('/'); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 303 | std::string dex_file_name = dex_file.substr(dex_file_last_slash + 1); |
| 304 | std::vector<std::string> unzip_exec_argv = |
| 305 | { "/usr/bin/unzip", dex_file, "classes.dex", "-d", tmp_dir}; |
| 306 | if (!::art::Exec(unzip_exec_argv, error_msg)) { |
| 307 | return false; |
| 308 | } |
| 309 | std::vector<std::string> diff_exec_argv = |
| 310 | { "/usr/bin/diff", tmp_dir + "classes.dex" , tmp_dir + dex_file_name }; |
| 311 | if (!::art::Exec(diff_exec_argv, error_msg)) { |
| 312 | return false; |
| 313 | } |
Orion Hodson | 9827367 | 2018-02-06 11:50:27 +0000 | [diff] [blame] | 314 | if (!UnlinkFile(tmp_dir + "classes.dex")) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 315 | return false; |
| 316 | } |
Orion Hodson | 9827367 | 2018-02-06 11:50:27 +0000 | [diff] [blame] | 317 | if (!UnlinkFile(tmp_dir + dex_file_name)) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | } |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 321 | return true; |
| 322 | } |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 323 | |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 324 | template <typename Mutator> |
| 325 | bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) { |
| 326 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 327 | std::string error_msg; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 328 | const ArtDexFileLoader dex_file_loader; |
| 329 | CHECK(dex_file_loader.Open(input_jar.c_str(), |
| 330 | input_jar.c_str(), |
| 331 | /*verify*/ true, |
| 332 | /*verify_checksum*/ true, |
| 333 | &error_msg, |
| 334 | &dex_files)) << error_msg; |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 335 | EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported"; |
| 336 | for (const std::unique_ptr<const DexFile>& dex : dex_files) { |
| 337 | CHECK(dex->EnableWrite()) << "Failed to enable write"; |
| 338 | mutator(const_cast<DexFile*>(dex.get())); |
| 339 | if (!output_dex->WriteFully(dex->Begin(), dex->Size())) { |
| 340 | return false; |
| 341 | } |
| 342 | } |
| 343 | if (output_dex->Flush() != 0) { |
| 344 | PLOG(FATAL) << "Could not flush the output file."; |
| 345 | } |
| 346 | return true; |
| 347 | } |
| 348 | |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 349 | // Create a profile with some subset of methods and classes. |
| 350 | void CreateProfile(const std::string& input_dex, |
| 351 | const std::string& out_profile, |
| 352 | const std::string& dex_location) { |
| 353 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 354 | std::string error_msg; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 355 | const ArtDexFileLoader dex_file_loader; |
| 356 | bool result = dex_file_loader.Open(input_dex.c_str(), |
| 357 | input_dex, |
| 358 | /*verify*/ true, |
| 359 | /*verify_checksum*/ false, |
| 360 | &error_msg, |
| 361 | &dex_files); |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 362 | |
| 363 | ASSERT_TRUE(result) << error_msg; |
| 364 | ASSERT_GE(dex_files.size(), 1u); |
| 365 | |
| 366 | size_t profile_methods = 0; |
| 367 | size_t profile_classes = 0; |
| 368 | ProfileCompilationInfo pfi; |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 369 | std::set<DexCacheResolvedClasses> classes; |
| 370 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 371 | for (uint32_t i = 0; i < dex_file->NumMethodIds(); i += 2) { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 372 | uint8_t flags = 0u; |
| 373 | |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 374 | if ((i & 3) != 0) { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 375 | flags |= ProfileCompilationInfo::MethodHotness::kFlagHot; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 376 | ++profile_methods; |
| 377 | } else if ((i & 2) != 0) { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 378 | flags |= ProfileCompilationInfo::MethodHotness::kFlagStartup; |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 379 | ++profile_methods; |
| 380 | } |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 381 | pfi.AddMethodIndex(static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags), |
| 382 | dex_location, |
| 383 | dex_file->GetLocationChecksum(), |
| 384 | /*dex_method_idx*/i, |
| 385 | dex_file->NumMethodIds()); |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 386 | } |
| 387 | DexCacheResolvedClasses cur_classes(dex_location, |
| 388 | dex_location, |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 389 | dex_file->GetLocationChecksum(), |
| 390 | dex_file->NumMethodIds()); |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 391 | // Add every even class too. |
| 392 | for (uint32_t i = 0; i < dex_file->NumClassDefs(); i += 1) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 393 | if ((i & 2) == 0) { |
| 394 | cur_classes.AddClass(dex_file->GetClassDef(i).class_idx_); |
| 395 | ++profile_classes; |
| 396 | } |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 397 | } |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 398 | classes.insert(cur_classes); |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 399 | } |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 400 | pfi.AddClasses(classes); |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 401 | // Write to provided file. |
| 402 | std::unique_ptr<File> file(OS::CreateEmptyFile(out_profile.c_str())); |
| 403 | ASSERT_TRUE(file != nullptr); |
| 404 | pfi.Save(file->Fd()); |
| 405 | if (file->FlushCloseOrErase() != 0) { |
| 406 | PLOG(FATAL) << "Could not flush and close test file."; |
| 407 | } |
| 408 | EXPECT_GE(profile_methods, 0u); |
| 409 | EXPECT_GE(profile_classes, 0u); |
| 410 | } |
| 411 | |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 412 | // Runs DexFileLayout test. |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 413 | bool DexFileLayoutExec(std::string* error_msg) { |
| 414 | ScratchFile tmp_file; |
Andreas Gampe | 641a473 | 2017-08-24 13:21:35 -0700 | [diff] [blame] | 415 | const std::string& tmp_name = tmp_file.GetFilename(); |
Andreas Gampe | 5555dd1 | 2017-08-24 13:50:21 -0700 | [diff] [blame] | 416 | size_t tmp_last_slash = tmp_name.rfind('/'); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 417 | std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1); |
| 418 | |
| 419 | // Write inputs and expected outputs. |
| 420 | std::string dex_file = tmp_dir + "classes.dex"; |
| 421 | WriteFileBase64(kDexFileLayoutInputDex, dex_file.c_str()); |
| 422 | std::string profile_file = tmp_dir + "primary.prof"; |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 423 | CreateProfile(dex_file, profile_file, dex_file); |
| 424 | // WriteFileBase64(kDexFileLayoutInputProfile, profile_file.c_str()); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 425 | std::string output_dex = tmp_dir + "classes.dex.new"; |
| 426 | |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 427 | std::vector<std::string> dexlayout_args = |
| 428 | { "-v", "-w", tmp_dir, "-o", tmp_name, "-p", profile_file, dex_file }; |
| 429 | if (!DexLayoutExec(dexlayout_args, error_msg)) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 430 | return false; |
| 431 | } |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 432 | |
| 433 | // -v makes sure that the layout did not corrupt the dex file. |
Orion Hodson | 9827367 | 2018-02-06 11:50:27 +0000 | [diff] [blame] | 434 | if (!UnlinkFile(dex_file) || !UnlinkFile(profile_file) || !UnlinkFile(output_dex)) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 435 | return false; |
| 436 | } |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 437 | return true; |
| 438 | } |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 439 | |
Bharadwaj Kalandhabhatta | 02ef640 | 2017-05-17 17:13:39 -0700 | [diff] [blame] | 440 | // Runs DexFileLayout test twice (second time is run on output of first time) |
| 441 | // for behavior consistency. |
| 442 | bool DexFileLayoutFixedPointExec(std::string* error_msg) { |
| 443 | ScratchFile tmp_file; |
Andreas Gampe | 641a473 | 2017-08-24 13:21:35 -0700 | [diff] [blame] | 444 | const std::string& tmp_name = tmp_file.GetFilename(); |
Andreas Gampe | 5555dd1 | 2017-08-24 13:50:21 -0700 | [diff] [blame] | 445 | size_t tmp_last_slash = tmp_name.rfind('/'); |
Bharadwaj Kalandhabhatta | 02ef640 | 2017-05-17 17:13:39 -0700 | [diff] [blame] | 446 | std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1); |
| 447 | |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 448 | // Unzip the test dex file to the classes.dex destination. It is required to unzip since |
| 449 | // opening from jar recalculates the dex location checksum. |
Bharadwaj Kalandhabhatta | 02ef640 | 2017-05-17 17:13:39 -0700 | [diff] [blame] | 450 | std::string dex_file = tmp_dir + "classes.dex"; |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 451 | |
| 452 | std::vector<std::string> unzip_args = { |
| 453 | "/usr/bin/unzip", |
| 454 | GetTestDexFileName("ManyMethods"), |
| 455 | "classes.dex", |
| 456 | "-d", |
| 457 | tmp_dir, |
| 458 | }; |
| 459 | if (!art::Exec(unzip_args, error_msg)) { |
| 460 | LOG(ERROR) << "Failed to unzip dex"; |
| 461 | return false; |
| 462 | } |
| 463 | |
Bharadwaj Kalandhabhatta | 02ef640 | 2017-05-17 17:13:39 -0700 | [diff] [blame] | 464 | std::string profile_file = tmp_dir + "primary.prof"; |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 465 | CreateProfile(dex_file, profile_file, dex_file); |
Bharadwaj Kalandhabhatta | 02ef640 | 2017-05-17 17:13:39 -0700 | [diff] [blame] | 466 | std::string output_dex = tmp_dir + "classes.dex.new"; |
| 467 | std::string second_output_dex = tmp_dir + "classes.dex.new.new"; |
| 468 | |
Bharadwaj Kalandhabhatta | 02ef640 | 2017-05-17 17:13:39 -0700 | [diff] [blame] | 469 | // -v makes sure that the layout did not corrupt the dex file. |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 470 | std::vector<std::string> dexlayout_args = |
| 471 | { "-i", "-v", "-w", tmp_dir, "-o", tmp_name, "-p", profile_file, dex_file }; |
Mathieu Chartier | a27af08 | 2018-02-03 16:12:23 -0800 | [diff] [blame] | 472 | if (!DexLayoutExec(dexlayout_args, error_msg, /*pass_default_cdex_option*/ false)) { |
Bharadwaj Kalandhabhatta | 02ef640 | 2017-05-17 17:13:39 -0700 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 476 | // Recreate the profile with the new dex location. This is required so that the profile dex |
| 477 | // location matches. |
| 478 | CreateProfile(dex_file, profile_file, output_dex); |
| 479 | |
Bharadwaj Kalandhabhatta | 02ef640 | 2017-05-17 17:13:39 -0700 | [diff] [blame] | 480 | // -v makes sure that the layout did not corrupt the dex file. |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 481 | // -i since the checksum won't match from the first layout. |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 482 | std::vector<std::string> second_dexlayout_args = |
| 483 | { "-i", "-v", "-w", tmp_dir, "-o", tmp_name, "-p", profile_file, output_dex }; |
Mathieu Chartier | a27af08 | 2018-02-03 16:12:23 -0800 | [diff] [blame] | 484 | if (!DexLayoutExec(second_dexlayout_args, error_msg, /*pass_default_cdex_option*/ false)) { |
Bharadwaj Kalandhabhatta | 02ef640 | 2017-05-17 17:13:39 -0700 | [diff] [blame] | 485 | return false; |
| 486 | } |
| 487 | |
| 488 | bool diff_result = true; |
| 489 | std::vector<std::string> diff_exec_argv = |
| 490 | { "/usr/bin/diff", output_dex, second_output_dex }; |
| 491 | if (!::art::Exec(diff_exec_argv, error_msg)) { |
| 492 | diff_result = false; |
| 493 | } |
| 494 | |
Orion Hodson | 9827367 | 2018-02-06 11:50:27 +0000 | [diff] [blame] | 495 | std::vector<std::string> test_files = { dex_file, profile_file, output_dex, second_output_dex }; |
| 496 | for (auto test_file : test_files) { |
| 497 | if (!UnlinkFile(test_file)) { |
| 498 | return false; |
| 499 | } |
Bharadwaj Kalandhabhatta | 02ef640 | 2017-05-17 17:13:39 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | return diff_result; |
| 503 | } |
| 504 | |
Jeff Hao | ac46271 | 2017-03-02 10:59:43 -0800 | [diff] [blame] | 505 | // Runs UnreferencedCatchHandlerTest & Unreferenced0SizeCatchHandlerTest. |
| 506 | bool UnreferencedCatchHandlerExec(std::string* error_msg, const char* filename) { |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 507 | ScratchFile tmp_file; |
Andreas Gampe | 641a473 | 2017-08-24 13:21:35 -0700 | [diff] [blame] | 508 | const std::string& tmp_name = tmp_file.GetFilename(); |
Andreas Gampe | 5555dd1 | 2017-08-24 13:50:21 -0700 | [diff] [blame] | 509 | size_t tmp_last_slash = tmp_name.rfind('/'); |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 510 | std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1); |
| 511 | |
| 512 | // Write inputs and expected outputs. |
| 513 | std::string input_dex = tmp_dir + "classes.dex"; |
Jeff Hao | ac46271 | 2017-03-02 10:59:43 -0800 | [diff] [blame] | 514 | WriteFileBase64(filename, input_dex.c_str()); |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 515 | std::string output_dex = tmp_dir + "classes.dex.new"; |
| 516 | |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 517 | std::vector<std::string> dexlayout_args = { "-w", tmp_dir, "-o", "/dev/null", input_dex }; |
Mathieu Chartier | a27af08 | 2018-02-03 16:12:23 -0800 | [diff] [blame] | 518 | if (!DexLayoutExec(dexlayout_args, error_msg, /*pass_default_cdex_option*/ false)) { |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 519 | return false; |
| 520 | } |
| 521 | |
| 522 | // Diff input and output. They should be the same. |
| 523 | std::vector<std::string> diff_exec_argv = { "/usr/bin/diff", input_dex, output_dex }; |
| 524 | if (!::art::Exec(diff_exec_argv, error_msg)) { |
| 525 | return false; |
| 526 | } |
| 527 | |
Orion Hodson | 9827367 | 2018-02-06 11:50:27 +0000 | [diff] [blame] | 528 | std::vector<std::string> dex_files = { input_dex, output_dex }; |
| 529 | for (auto dex_file : dex_files) { |
| 530 | if (!UnlinkFile(dex_file)) { |
| 531 | return false; |
| 532 | } |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 533 | } |
| 534 | return true; |
| 535 | } |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 536 | |
| 537 | bool DexLayoutExec(ScratchFile* dex_file, |
| 538 | const char* dex_filename, |
| 539 | ScratchFile* profile_file, |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 540 | const std::vector<std::string>& dexlayout_args) { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 541 | if (dex_filename != nullptr) { |
| 542 | WriteBase64ToFile(dex_filename, dex_file->GetFile()); |
| 543 | EXPECT_EQ(dex_file->GetFile()->Flush(), 0); |
| 544 | } |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 545 | if (profile_file != nullptr) { |
Mathieu Chartier | d00e02b | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 546 | CreateProfile(dex_file->GetFilename(), profile_file->GetFilename(), dex_file->GetFilename()); |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 547 | } |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 548 | |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 549 | std::string error_msg; |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 550 | const bool result = DexLayoutExec(dexlayout_args, &error_msg); |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 551 | if (!result) { |
| 552 | LOG(ERROR) << "Error: " << error_msg; |
| 553 | return false; |
| 554 | } |
| 555 | return true; |
| 556 | } |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 557 | |
Mathieu Chartier | a27af08 | 2018-02-03 16:12:23 -0800 | [diff] [blame] | 558 | bool DexLayoutExec(const std::vector<std::string>& dexlayout_args, |
| 559 | std::string* error_msg, |
| 560 | bool pass_default_cdex_option = true) { |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 561 | std::vector<std::string> argv; |
| 562 | |
| 563 | std::string dexlayout = GetDexLayoutPath(); |
| 564 | CHECK(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path"; |
| 565 | argv.push_back(dexlayout); |
Mathieu Chartier | a27af08 | 2018-02-03 16:12:23 -0800 | [diff] [blame] | 566 | if (pass_default_cdex_option) { |
| 567 | std::vector<std::string> cdex_level = DefaultCompactDexOption(); |
| 568 | argv.insert(argv.end(), cdex_level.begin(), cdex_level.end()); |
| 569 | } |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 570 | |
| 571 | argv.insert(argv.end(), dexlayout_args.begin(), dexlayout_args.end()); |
| 572 | |
| 573 | return ::art::Exec(argv, error_msg); |
| 574 | } |
Orion Hodson | 9827367 | 2018-02-06 11:50:27 +0000 | [diff] [blame] | 575 | |
| 576 | bool UnlinkFile(const std::string& file_path) { |
| 577 | return unix_file::FdFile(file_path, 0, false).Unlink(); |
| 578 | } |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 579 | }; |
| 580 | |
| 581 | |
| 582 | TEST_F(DexLayoutTest, FullPlainOutput) { |
Jeff Hao | 0f7eaeb | 2016-08-31 17:56:13 -0700 | [diff] [blame] | 583 | // Disable test on target. |
| 584 | TEST_DISABLED_FOR_TARGET(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 585 | std::string error_msg; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 586 | ASSERT_TRUE(FullPlainOutputExec(&error_msg)) << error_msg; |
| 587 | } |
| 588 | |
| 589 | TEST_F(DexLayoutTest, DexFileOutput) { |
| 590 | // Disable test on target. |
| 591 | TEST_DISABLED_FOR_TARGET(); |
| 592 | std::string error_msg; |
| 593 | ASSERT_TRUE(DexFileOutputExec(&error_msg)) << error_msg; |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 596 | TEST_F(DexLayoutTest, DexFileLayout) { |
| 597 | // Disable test on target. |
| 598 | TEST_DISABLED_FOR_TARGET(); |
| 599 | std::string error_msg; |
| 600 | ASSERT_TRUE(DexFileLayoutExec(&error_msg)) << error_msg; |
| 601 | } |
| 602 | |
Bharadwaj Kalandhabhatta | 02ef640 | 2017-05-17 17:13:39 -0700 | [diff] [blame] | 603 | TEST_F(DexLayoutTest, DexFileLayoutFixedPoint) { |
| 604 | // Disable test on target. |
| 605 | TEST_DISABLED_FOR_TARGET(); |
| 606 | std::string error_msg; |
| 607 | ASSERT_TRUE(DexFileLayoutFixedPointExec(&error_msg)) << error_msg; |
| 608 | } |
| 609 | |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 610 | TEST_F(DexLayoutTest, UnreferencedCatchHandler) { |
| 611 | // Disable test on target. |
| 612 | TEST_DISABLED_FOR_TARGET(); |
| 613 | std::string error_msg; |
Jeff Hao | ac46271 | 2017-03-02 10:59:43 -0800 | [diff] [blame] | 614 | ASSERT_TRUE(UnreferencedCatchHandlerExec(&error_msg, |
| 615 | kUnreferencedCatchHandlerInputDex)) << error_msg; |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 616 | } |
Jeff Hao | ac46271 | 2017-03-02 10:59:43 -0800 | [diff] [blame] | 617 | |
| 618 | TEST_F(DexLayoutTest, Unreferenced0SizeCatchHandler) { |
| 619 | // Disable test on target. |
| 620 | TEST_DISABLED_FOR_TARGET(); |
| 621 | std::string error_msg; |
| 622 | ASSERT_TRUE(UnreferencedCatchHandlerExec(&error_msg, |
| 623 | kUnreferenced0SizeCatchHandlerInputDex)) << error_msg; |
| 624 | } |
| 625 | |
Jeff Hao | a64a64c | 2017-03-06 15:05:38 -0800 | [diff] [blame] | 626 | TEST_F(DexLayoutTest, UnreferencedEndingCatchHandler) { |
| 627 | // Disable test on target. |
| 628 | TEST_DISABLED_FOR_TARGET(); |
| 629 | std::string error_msg; |
| 630 | ASSERT_TRUE(UnreferencedCatchHandlerExec(&error_msg, |
| 631 | kUnreferencedEndingCatchHandlerInputDex)) << error_msg; |
| 632 | } |
| 633 | |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame] | 634 | TEST_F(DexLayoutTest, DuplicateOffset) { |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 635 | ScratchFile temp_dex; |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 636 | std::vector<std::string> dexlayout_args = |
| 637 | { "-a", "-i", "-o", "/dev/null", temp_dex.GetFilename() }; |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 638 | ASSERT_TRUE(DexLayoutExec(&temp_dex, |
| 639 | kDexFileDuplicateOffset, |
| 640 | nullptr /* profile_file */, |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 641 | dexlayout_args)); |
Mathieu Chartier | 6b0dbef | 2017-02-21 12:45:30 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Jeff Hao | f914f8e | 2017-02-21 16:14:23 -0800 | [diff] [blame] | 644 | TEST_F(DexLayoutTest, NullSetRefListElement) { |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 645 | ScratchFile temp_dex; |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 646 | std::vector<std::string> dexlayout_args = { "-o", "/dev/null", temp_dex.GetFilename() }; |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 647 | ASSERT_TRUE(DexLayoutExec(&temp_dex, |
| 648 | kNullSetRefListElementInputDex, |
| 649 | nullptr /* profile_file */, |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 650 | dexlayout_args)); |
Jeff Hao | f914f8e | 2017-02-21 16:14:23 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 653 | TEST_F(DexLayoutTest, MultiClassData) { |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 654 | ScratchFile temp_dex; |
| 655 | ScratchFile temp_profile; |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 656 | std::vector<std::string> dexlayout_args = |
| 657 | { "-p", temp_profile.GetFilename(), "-o", "/dev/null", temp_dex.GetFilename() }; |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 658 | ASSERT_TRUE(DexLayoutExec(&temp_dex, |
| 659 | kMultiClassDataInputDex, |
| 660 | &temp_profile, |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 661 | dexlayout_args)); |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | TEST_F(DexLayoutTest, UnalignedCodeInfo) { |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 665 | ScratchFile temp_dex; |
| 666 | ScratchFile temp_profile; |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 667 | std::vector<std::string> dexlayout_args = |
| 668 | { "-p", temp_profile.GetFilename(), "-o", "/dev/null", temp_dex.GetFilename() }; |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 669 | ASSERT_TRUE(DexLayoutExec(&temp_dex, |
| 670 | kUnalignedCodeInfoInputDex, |
| 671 | &temp_profile, |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 672 | dexlayout_args)); |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Jeff Hao | 32a60ab | 2017-03-01 16:44:50 -0800 | [diff] [blame] | 675 | TEST_F(DexLayoutTest, ClassDataBeforeCode) { |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 676 | ScratchFile temp_dex; |
| 677 | ScratchFile temp_profile; |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 678 | std::vector<std::string> dexlayout_args = |
| 679 | { "-p", temp_profile.GetFilename(), "-o", "/dev/null", temp_dex.GetFilename() }; |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 680 | ASSERT_TRUE(DexLayoutExec(&temp_dex, |
| 681 | kClassDataBeforeCodeInputDex, |
| 682 | &temp_profile, |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 683 | dexlayout_args)); |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | TEST_F(DexLayoutTest, UnknownTypeDebugInfo) { |
| 687 | ScratchFile temp_dex; |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 688 | std::vector<std::string> dexlayout_args = { "-o", "/dev/null", temp_dex.GetFilename() }; |
Jeff Hao | d9be768 | 2017-04-05 09:29:43 -0700 | [diff] [blame] | 689 | ASSERT_TRUE(DexLayoutExec(&temp_dex, |
| 690 | kUnknownTypeDebugInfoInputDex, |
| 691 | nullptr /* profile_file */, |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 692 | dexlayout_args)); |
Jeff Hao | 32a60ab | 2017-03-01 16:44:50 -0800 | [diff] [blame] | 693 | } |
| 694 | |
Jeff Hao | d212d5b | 2017-04-26 12:09:06 -0700 | [diff] [blame] | 695 | TEST_F(DexLayoutTest, DuplicateCodeItem) { |
| 696 | ScratchFile temp_dex; |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 697 | std::vector<std::string> dexlayout_args = { "-o", "/dev/null", temp_dex.GetFilename() }; |
Jeff Hao | d212d5b | 2017-04-26 12:09:06 -0700 | [diff] [blame] | 698 | ASSERT_TRUE(DexLayoutExec(&temp_dex, |
| 699 | kDuplicateCodeItemInputDex, |
| 700 | nullptr /* profile_file */, |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 701 | dexlayout_args)); |
Jeff Hao | d212d5b | 2017-04-26 12:09:06 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 704 | // Test that instructions that go past the end of the code items don't cause crashes. |
| 705 | TEST_F(DexLayoutTest, CodeItemOverrun) { |
| 706 | ScratchFile temp_dex; |
| 707 | MutateDexFile(temp_dex.GetFile(), GetTestDexFileName("ManyMethods"), [] (DexFile* dex) { |
| 708 | bool mutated_successfully = false; |
| 709 | // Change the dex instructions to make an opcode that spans past the end of the code item. |
| 710 | for (size_t i = 0; i < dex->NumClassDefs(); ++i) { |
| 711 | const DexFile::ClassDef& def = dex->GetClassDef(i); |
| 712 | const uint8_t* data = dex->GetClassData(def); |
| 713 | if (data == nullptr) { |
| 714 | continue; |
| 715 | } |
| 716 | ClassDataItemIterator it(*dex, data); |
| 717 | it.SkipAllFields(); |
Mathieu Chartier | b7c273c | 2017-11-10 18:07:56 -0800 | [diff] [blame] | 718 | while (it.HasNextMethod()) { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 719 | DexFile::CodeItem* item = const_cast<DexFile::CodeItem*>(it.GetMethodCodeItem()); |
| 720 | if (item != nullptr) { |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 721 | CodeItemInstructionAccessor instructions(*dex, item); |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 722 | if (instructions.begin() != instructions.end()) { |
| 723 | DexInstructionIterator last_instruction = instructions.begin(); |
| 724 | for (auto dex_it = instructions.begin(); dex_it != instructions.end(); ++dex_it) { |
| 725 | last_instruction = dex_it; |
| 726 | } |
| 727 | if (last_instruction->SizeInCodeUnits() == 1) { |
| 728 | // Set the opcode to something that will go past the end of the code item. |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 729 | const_cast<Instruction&>(last_instruction.Inst()).SetOpcode( |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 730 | Instruction::CONST_STRING_JUMBO); |
| 731 | mutated_successfully = true; |
Mathieu Chartier | 176190c | 2017-10-31 09:58:07 -0700 | [diff] [blame] | 732 | // Test that the safe iterator doesn't go past the end. |
| 733 | SafeDexInstructionIterator it2(instructions.begin(), instructions.end()); |
| 734 | while (!it2.IsErrorState()) { |
| 735 | ++it2; |
| 736 | } |
| 737 | EXPECT_TRUE(it2 == last_instruction); |
| 738 | EXPECT_TRUE(it2 < instructions.end()); |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | } |
| 742 | it.Next(); |
| 743 | } |
| 744 | } |
| 745 | CHECK(mutated_successfully) |
| 746 | << "Failed to find candidate code item with only one code unit in last instruction."; |
| 747 | }); |
Mathieu Chartier | a27af08 | 2018-02-03 16:12:23 -0800 | [diff] [blame] | 748 | |
| 749 | std::string error_msg; |
| 750 | |
| 751 | ScratchFile tmp_file; |
| 752 | const std::string& tmp_name = tmp_file.GetFilename(); |
| 753 | size_t tmp_last_slash = tmp_name.rfind('/'); |
| 754 | std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1); |
| 755 | ScratchFile profile_file; |
| 756 | |
| 757 | std::vector<std::string> dexlayout_args = |
| 758 | { "-i", |
| 759 | "-v", |
| 760 | "-w", tmp_dir, |
| 761 | "-o", tmp_name, |
| 762 | "-p", profile_file.GetFilename(), |
| 763 | temp_dex.GetFilename() |
| 764 | }; |
| 765 | // -v makes sure that the layout did not corrupt the dex file. |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 766 | ASSERT_TRUE(DexLayoutExec(&temp_dex, |
| 767 | /*dex_filename*/ nullptr, |
Mathieu Chartier | a27af08 | 2018-02-03 16:12:23 -0800 | [diff] [blame] | 768 | &profile_file, |
Andreas Gampe | 693bfbf | 2017-11-10 12:23:31 -0800 | [diff] [blame] | 769 | dexlayout_args)); |
Orion Hodson | 9827367 | 2018-02-06 11:50:27 +0000 | [diff] [blame] | 770 | ASSERT_TRUE(UnlinkFile(temp_dex.GetFilename() + ".new")); |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Mathieu Chartier | 2f36d2f | 2017-11-20 15:45:25 -0800 | [diff] [blame] | 773 | // Test that link data is written out (or at least the header is updated). |
| 774 | TEST_F(DexLayoutTest, LinkData) { |
Mathieu Chartier | fe9185a | 2017-11-21 19:01:10 -0800 | [diff] [blame] | 775 | TEST_DISABLED_FOR_TARGET(); |
Mathieu Chartier | 2f36d2f | 2017-11-20 15:45:25 -0800 | [diff] [blame] | 776 | ScratchFile temp_dex; |
| 777 | size_t file_size = 0; |
| 778 | MutateDexFile(temp_dex.GetFile(), GetTestDexFileName("ManyMethods"), [&] (DexFile* dex) { |
| 779 | DexFile::Header& header = const_cast<DexFile::Header&>(dex->GetHeader()); |
| 780 | header.link_off_ = header.file_size_; |
| 781 | header.link_size_ = 16 * KB; |
| 782 | header.file_size_ += header.link_size_; |
| 783 | file_size = header.file_size_; |
| 784 | }); |
| 785 | TEMP_FAILURE_RETRY(temp_dex.GetFile()->SetLength(file_size)); |
| 786 | |
| 787 | std::string error_msg; |
| 788 | |
| 789 | ScratchFile tmp_file; |
| 790 | const std::string& tmp_name = tmp_file.GetFilename(); |
| 791 | size_t tmp_last_slash = tmp_name.rfind('/'); |
| 792 | std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1); |
| 793 | ScratchFile profile_file; |
| 794 | |
| 795 | std::vector<std::string> dexlayout_args = |
| 796 | { "-i", |
| 797 | "-v", |
| 798 | "-w", tmp_dir, |
| 799 | "-o", tmp_name, |
| 800 | "-p", profile_file.GetFilename(), |
| 801 | temp_dex.GetFilename() |
| 802 | }; |
| 803 | // -v makes sure that the layout did not corrupt the dex file. |
| 804 | ASSERT_TRUE(DexLayoutExec(&temp_dex, |
| 805 | /*dex_filename*/ nullptr, |
| 806 | &profile_file, |
| 807 | dexlayout_args)); |
Orion Hodson | 9827367 | 2018-02-06 11:50:27 +0000 | [diff] [blame] | 808 | ASSERT_TRUE(UnlinkFile(temp_dex.GetFilename() + ".new")); |
Mathieu Chartier | 2f36d2f | 2017-11-20 15:45:25 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 811 | TEST_F(DexLayoutTest, ClassFilter) { |
| 812 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 813 | std::string error_msg; |
| 814 | const ArtDexFileLoader dex_file_loader; |
| 815 | const std::string input_jar = GetTestDexFileName("ManyMethods"); |
| 816 | CHECK(dex_file_loader.Open(input_jar.c_str(), |
| 817 | input_jar.c_str(), |
| 818 | /*verify*/ true, |
| 819 | /*verify_checksum*/ true, |
| 820 | &error_msg, |
| 821 | &dex_files)) << error_msg; |
| 822 | ASSERT_EQ(dex_files.size(), 1u); |
| 823 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 824 | EXPECT_GT(dex_file->NumClassDefs(), 1u); |
| 825 | for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 826 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 827 | LOG(INFO) << dex_file->GetClassDescriptor(class_def); |
| 828 | } |
| 829 | Options options; |
| 830 | // Filter out all the classes other than the one below based on class descriptor. |
| 831 | options.class_filter_.insert("LManyMethods$Strings;"); |
| 832 | DexLayout dexlayout(options, |
| 833 | /*info*/ nullptr, |
| 834 | /*out_file*/ nullptr, |
| 835 | /*header*/ nullptr); |
| 836 | std::unique_ptr<DexContainer> out; |
| 837 | dexlayout.ProcessDexFile(dex_file->GetLocation().c_str(), |
| 838 | dex_file.get(), |
| 839 | /*dex_file_index*/ 0, |
| 840 | &out); |
| 841 | std::unique_ptr<const DexFile> output_dex_file( |
| 842 | dex_file_loader.OpenWithDataSection( |
| 843 | out->GetMainSection()->Begin(), |
| 844 | out->GetMainSection()->Size(), |
| 845 | out->GetDataSection()->Begin(), |
| 846 | out->GetDataSection()->Size(), |
| 847 | dex_file->GetLocation().c_str(), |
| 848 | /* checksum */ 0, |
| 849 | /*oat_dex_file*/ nullptr, |
| 850 | /* verify */ true, |
| 851 | /*verify_checksum*/ false, |
| 852 | &error_msg)); |
| 853 | ASSERT_TRUE(output_dex_file != nullptr); |
| 854 | |
Mathieu Chartier | 807b21b | 2018-01-29 05:18:24 -0800 | [diff] [blame] | 855 | ASSERT_EQ(output_dex_file->NumClassDefs(), options.class_filter_.size()); |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 856 | for (uint32_t i = 0; i < output_dex_file->NumClassDefs(); ++i) { |
Mathieu Chartier | 807b21b | 2018-01-29 05:18:24 -0800 | [diff] [blame] | 857 | // Check that every class in the output dex file is in the filter. |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 858 | const DexFile::ClassDef& class_def = output_dex_file->GetClassDef(i); |
| 859 | ASSERT_TRUE(options.class_filter_.find(output_dex_file->GetClassDescriptor(class_def)) != |
| 860 | options.class_filter_.end()); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 865 | } // namespace art |