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