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