1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <string>
18 #include <vector>
19 #include <sstream>
20 
21 #include <sys/types.h>
22 #include <unistd.h>
23 
24 #include "base/unix_file/fd_file.h"
25 #include "common_runtime_test.h"
26 #include "exec_utils.h"
27 #include "utils.h"
28 
29 namespace art {
30 
31 static const char kDexFileLayoutInputDex[] =
32     "ZGV4CjAzNQD1KW3+B8NAB0f2A/ZVIBJ0aHrGIqcpVTAUAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAH"
33     "AAAAcAAAAAQAAACMAAAAAQAAAJwAAAAAAAAAAAAAAAMAAACoAAAAAgAAAMAAAAAUAQAAAAEAADAB"
34     "AAA4AQAAQAEAAEgBAABNAQAAUgEAAGYBAAADAAAABAAAAAUAAAAGAAAABgAAAAMAAAAAAAAAAAAA"
35     "AAAAAAABAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAEAAAAAAAAAdQEAAAAAAAABAAAA"
36     "AAAAAAIAAAAAAAAAAgAAAAAAAAB/AQAAAAAAAAEAAQABAAAAaQEAAAQAAABwEAIAAAAOAAEAAQAB"
37     "AAAAbwEAAAQAAABwEAIAAAAOAAY8aW5pdD4ABkEuamF2YQAGQi5qYXZhAANMQTsAA0xCOwASTGph"
38     "dmEvbGFuZy9PYmplY3Q7AAFWAAQABw48AAQABw48AAAAAQAAgIAEgAIAAAEAAYCABJgCAAAACwAA"
39     "AAAAAAABAAAAAAAAAAEAAAAHAAAAcAAAAAIAAAAEAAAAjAAAAAMAAAABAAAAnAAAAAUAAAADAAAA"
40     "qAAAAAYAAAACAAAAwAAAAAEgAAACAAAAAAEAAAIgAAAHAAAAMAEAAAMgAAACAAAAaQEAAAAgAAAC"
41     "AAAAdQEAAAAQAAABAAAAjAEAAA==";
42 
43 static const char kDexFileLayoutInputProfile[] =
44     "cHJvADAwNQABCwABAAAAAAD1KW3+Y2xhc3Nlcy5kZXgBAA==";
45 
46 // Dex file with catch handler unreferenced by try blocks.
47 // Constructed by building a dex file with try/catch blocks and hex editing.
48 static const char kUnreferencedCatchHandlerInputDex[] =
49     "ZGV4CjAzNQD+exd52Y0f9nY5x5GmInXq5nXrO6Kl2RV4AwAAcAAAAHhWNBIAAAAAAAAAANgCAAAS"
50     "AAAAcAAAAAgAAAC4AAAAAwAAANgAAAABAAAA/AAAAAQAAAAEAQAAAQAAACQBAAA0AgAARAEAANYB"
51     "AADeAQAA5gEAAO4BAAAAAgAADwIAACYCAAA9AgAAUQIAAGUCAAB5AgAAfwIAAIUCAACIAgAAjAIA"
52     "AKECAACnAgAArAIAAAQAAAAFAAAABgAAAAcAAAAIAAAACQAAAAwAAAAOAAAADAAAAAYAAAAAAAAA"
53     "DQAAAAYAAADIAQAADQAAAAYAAADQAQAABQABABAAAAAAAAAAAAAAAAAAAgAPAAAAAQABABEAAAAD"
54     "AAAAAAAAAAAAAAABAAAAAwAAAAAAAAADAAAAAAAAAMgCAAAAAAAAAQABAAEAAAC1AgAABAAAAHAQ"
55     "AwAAAA4AAwABAAIAAgC6AgAAIQAAAGIAAAAaAQoAbiACABAAYgAAABoBCwBuIAIAEAAOAA0AYgAA"
56     "ABoBAQBuIAIAEAAo8A0AYgAAABoBAgBuIAIAEAAo7gAAAAAAAAcAAQAHAAAABwABAAIBAg8BAhgA"
57     "AQAAAAQAAAABAAAABwAGPGluaXQ+AAZDYXRjaDEABkNhdGNoMgAQSGFuZGxlclRlc3QuamF2YQAN"
58     "TEhhbmRsZXJUZXN0OwAVTGphdmEvaW8vUHJpbnRTdHJlYW07ABVMamF2YS9sYW5nL0V4Y2VwdGlv"
59     "bjsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABJMamF2YS9sYW5nL1N5"
60     "c3RlbTsABFRyeTEABFRyeTIAAVYAAlZMABNbTGphdmEvbGFuZy9TdHJpbmc7AARtYWluAANvdXQA"
61     "B3ByaW50bG4AAQAHDgAEAQAHDn17AncdHoseAAAAAgAAgYAExAIBCdwCAAANAAAAAAAAAAEAAAAA"
62     "AAAAAQAAABIAAABwAAAAAgAAAAgAAAC4AAAAAwAAAAMAAADYAAAABAAAAAEAAAD8AAAABQAAAAQA"
63     "AAAEAQAABgAAAAEAAAAkAQAAASAAAAIAAABEAQAAARAAAAIAAADIAQAAAiAAABIAAADWAQAAAyAA"
64     "AAIAAAC1AgAAACAAAAEAAADIAgAAABAAAAEAAADYAgAA";
65 
66 // Dex file with 0-size (catch all only) catch handler unreferenced by try blocks.
67 // Constructed by building a dex file with try/catch blocks and hex editing.
68 static const char kUnreferenced0SizeCatchHandlerInputDex[] =
69     "ZGV4CjAzNQCEbEEvMstSNpQpjPdfMEfUBS48cis2QRJoAwAAcAAAAHhWNBIAAAAAAAAAAMgCAAAR"
70     "AAAAcAAAAAcAAAC0AAAAAwAAANAAAAABAAAA9AAAAAQAAAD8AAAAAQAAABwBAAAsAgAAPAEAAOoB"
71     "AADyAQAABAIAABMCAAAqAgAAPgIAAFICAABmAgAAaQIAAG0CAACCAgAAhgIAAIoCAACQAgAAlQIA"
72     "AJ4CAACiAgAAAgAAAAMAAAAEAAAABQAAAAYAAAAHAAAACQAAAAcAAAAFAAAAAAAAAAgAAAAFAAAA"
73     "3AEAAAgAAAAFAAAA5AEAAAQAAQANAAAAAAAAAAAAAAAAAAIADAAAAAEAAQAOAAAAAgAAAAAAAAAA"
74     "AAAAAQAAAAIAAAAAAAAAAQAAAAAAAAC5AgAAAAAAAAEAAQABAAAApgIAAAQAAABwEAMAAAAOAAQA"
75     "AQACAAIAqwIAAC8AAABiAAAAGgEPAG4gAgAQAGIAAAAaAQoAbiACABAAYgAAABoBEABuIAIAEABi"
76     "AAAAGgELAG4gAgAQAA4ADQBiAQAAGgIKAG4gAgAhACcADQBiAQAAGgILAG4gAgAhACcAAAAAAAAA"
77     "BwABAA4AAAAHAAEAAgAdACYAAAABAAAAAwAAAAEAAAAGAAY8aW5pdD4AEEhhbmRsZXJUZXN0Lmph"
78     "dmEADUxIYW5kbGVyVGVzdDsAFUxqYXZhL2lvL1ByaW50U3RyZWFtOwASTGphdmEvbGFuZy9PYmpl"
79     "Y3Q7ABJMamF2YS9sYW5nL1N0cmluZzsAEkxqYXZhL2xhbmcvU3lzdGVtOwABVgACVkwAE1tMamF2"
80     "YS9sYW5nL1N0cmluZzsAAmYxAAJmMgAEbWFpbgADb3V0AAdwcmludGxuAAJ0MQACdDIAAQAHDgAE"
81     "AQAHDnl7eXkCeB2bAAAAAgAAgYAEvAIBCdQCAA0AAAAAAAAAAQAAAAAAAAABAAAAEQAAAHAAAAAC"
82     "AAAABwAAALQAAAADAAAAAwAAANAAAAAEAAAAAQAAAPQAAAAFAAAABAAAAPwAAAAGAAAAAQAAABwB"
83     "AAABIAAAAgAAADwBAAABEAAAAgAAANwBAAACIAAAEQAAAOoBAAADIAAAAgAAAKYCAAAAIAAAAQAA"
84     "ALkCAAAAEAAAAQAAAMgCAAA=";
85 
86 // Dex file with an unreferenced catch handler at end of code item.
87 // Constructed by building a dex file with try/catch blocks and hex editing.
88 static const char kUnreferencedEndingCatchHandlerInputDex[] =
89     "ZGV4CjAzNQCEflufI6xGTDDRmLpbfYi6ujPrDLIwvYcEBAAAcAAAAHhWNBIAAAAAAAAAAGQDAAAT"
90     "AAAAcAAAAAgAAAC8AAAAAwAAANwAAAABAAAAAAEAAAUAAAAIAQAAAQAAADABAAC0AgAAUAEAAE4C"
91     "AABWAgAAXgIAAGYCAAB4AgAAhwIAAJ4CAAC1AgAAyQIAAN0CAADxAgAA9wIAAP0CAAAAAwAABAMA"
92     "ABkDAAAcAwAAIgMAACcDAAAEAAAABQAAAAYAAAAHAAAACAAAAAkAAAAMAAAADgAAAAwAAAAGAAAA"
93     "AAAAAA0AAAAGAAAAQAIAAA0AAAAGAAAASAIAAAUAAQARAAAAAAAAAAAAAAAAAAAADwAAAAAAAgAQ"
94     "AAAAAQABABIAAAADAAAAAAAAAAAAAAABAAAAAwAAAAAAAAADAAAAAAAAAFADAAAAAAAAAQABAAEA"
95     "AAAwAwAABAAAAHAQBAAAAA4AAgAAAAIAAgA1AwAAIQAAAGIAAAAaAQoAbiADABAAYgAAABoBCwBu"
96     "IAMAEAAOAA0AYgAAABoBAQBuIAMAEAAo8A0AYgAAABoBAgBuIAMAEAAo7gAAAAAAAAcAAQAHAAAA"
97     "BwABAAIBAg8BAhgAAwABAAIAAgBCAwAAIQAAAGIAAAAaAQoAbiADABAAYgAAABoBCwBuIAMAEAAO"
98     "AA0AYgAAABoBAQBuIAMAEAAo8A0AYgAAABoBAgBuIAMAEAAo7gAAAAAAAAcAAQAHAAAABwABAAIB"
99     "Ag8BAhgAAQAAAAQAAAABAAAABwAGPGluaXQ+AAZDYXRjaDEABkNhdGNoMgAQSGFuZGxlclRlc3Qu"
100     "amF2YQANTEhhbmRsZXJUZXN0OwAVTGphdmEvaW8vUHJpbnRTdHJlYW07ABVMamF2YS9sYW5nL0V4"
101     "Y2VwdGlvbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABJMamF2YS9s"
102     "YW5nL1N5c3RlbTsABFRyeTEABFRyeTIAAVYAAlZMABNbTGphdmEvbGFuZy9TdHJpbmc7AAFhAARt"
103     "YWluAANvdXQAB3ByaW50bG4AAQAHDgAEAAcOfHsCeB0eih4AEQEABw59ewJ3HR6LHgAAAAMAAIGA"
104     "BNACAQnoAgEJ1AMAAA0AAAAAAAAAAQAAAAAAAAABAAAAEwAAAHAAAAACAAAACAAAALwAAAADAAAA"
105     "AwAAANwAAAAEAAAAAQAAAAABAAAFAAAABQAAAAgBAAAGAAAAAQAAADABAAABIAAAAwAAAFABAAAB"
106     "EAAAAgAAAEACAAACIAAAEwAAAE4CAAADIAAAAwAAADADAAAAIAAAAQAAAFADAAAAEAAAAQAAAGQD"
107     "AAA=";
108 
109 // Dex file with multiple code items that have the same debug_info_off_. Constructed by a modified
110 // dexlayout on XandY.
111 static const char kDexFileDuplicateOffset[] =
112     "ZGV4CjAzNwAQfXfPCB8qCxo7MqdFhmHZQwCv8+udHD8MBAAAcAAAAHhWNBIAAAAAAAAAAFQDAAAT"
113     "AAAAcAAAAAgAAAC8AAAAAQAAANwAAAABAAAA6AAAAAUAAADwAAAAAwAAABgBAACUAgAAeAEAABQC"
114     "AAAeAgAAJgIAACsCAAAyAgAANwIAAFsCAAB7AgAAngIAALICAAC1AgAAvQIAAMUCAADIAgAA1QIA"
115     "AOkCAADvAgAA9QIAAPwCAAACAAAAAwAAAAQAAAAFAAAABgAAAAcAAAAIAAAACQAAAAkAAAAHAAAA"
116     "AAAAAAIAAQASAAAAAAAAAAEAAAABAAAAAQAAAAIAAAAAAAAAAgAAAAEAAAAGAAAAAQAAAAAAAAAA"
117     "AAAABgAAAAAAAAAKAAAAAAAAACsDAAAAAAAAAQAAAAAAAAAGAAAAAAAAAAsAAAD0AQAANQMAAAAA"
118     "AAACAAAAAAAAAAAAAAAAAAAACwAAAAQCAAA/AwAAAAAAAAIAAAAUAwAAGgMAAAEAAAAjAwAAAQAB"
119     "AAEAAAAFAAAABAAAAHAQBAAAAA4AAQABAAEAAAAFAAAABAAAAHAQBAAAAA4AAQAAAAEAAAAFAAAA"
120     "CAAAACIAAQBwEAEAAABpAAAADgABAAEAAQAAAAUAAAAEAAAAcBAAAAAADgB4AQAAAAAAAAAAAAAA"
121     "AAAAhAEAAAAAAAAAAAAAAAAAAAg8Y2xpbml0PgAGPGluaXQ+AANMWDsABUxZJFo7AANMWTsAIkxk"
122     "YWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2aWsvYW5ub3RhdGlvbi9Jbm5l"
123     "ckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNzZXM7ABJMamF2YS9sYW5nL09i"
124     "amVjdDsAAVYABlguamF2YQAGWS5qYXZhAAFaAAthY2Nlc3NGbGFncwASZW1pdHRlcjogamFjay00"
125     "LjI1AARuYW1lAAR0aGlzAAV2YWx1ZQABegARAAcOABMABw4AEgAHDnYAEQAHDgACAwERGAICBAIN"
126     "BAgPFwwCBQERHAEYAQAAAQAAgIAEjAMAAAEAAYCABKQDAQACAAAIAoiABLwDAYCABNwDAAAADwAA"
127     "AAAAAAABAAAAAAAAAAEAAAATAAAAcAAAAAIAAAAIAAAAvAAAAAMAAAABAAAA3AAAAAQAAAABAAAA"
128     "6AAAAAUAAAAFAAAA8AAAAAYAAAADAAAAGAEAAAMQAAACAAAAeAEAAAEgAAAEAAAAjAEAAAYgAAAC"
129     "AAAA9AEAAAIgAAATAAAAFAIAAAMgAAAEAAAA/wIAAAQgAAADAAAAFAMAAAAgAAADAAAAKwMAAAAQ"
130     "AAABAAAAVAMAAA==";
131 
132 // Dex file with null value for annotations_off in the annotation_set_ref_list.
133 // Constructed by building a dex file with annotations and hex editing.
134 static const char kNullSetRefListElementInputDex[] =
135     "ZGV4CjAzNQB1iA+7ZwgkF+7E6ZesYFc2lRAR3qnRAanwAwAAcAAAAHhWNBIAAAAAAAAAACADAAAS"
136     "AAAAcAAAAAgAAAC4AAAAAwAAANgAAAABAAAA/AAAAAQAAAAEAQAAAgAAACQBAACMAgAAZAEAAOgB"
137     "AADwAQAAAAIAAAMCAAAQAgAAIAIAADQCAABIAgAAawIAAI0CAAC1AgAAyAIAANECAADUAgAA2QIA"
138     "ANwCAADjAgAA6QIAAAMAAAAEAAAABQAAAAYAAAAHAAAACAAAAAkAAAAMAAAAAgAAAAMAAAAAAAAA"
139     "DAAAAAcAAAAAAAAADQAAAAcAAADgAQAABgAGAAsAAAAAAAEAAAAAAAAAAgAOAAAAAQAAABAAAAAC"
140     "AAEAAAAAAAAAAAAAAAAAAgAAAAAAAAABAAAAsAEAAAgDAAAAAAAAAQAAAAEmAAACAAAA2AEAAAoA"
141     "AADIAQAAFgMAAAAAAAACAAAAAAAAAHwBAAABAAAA/AIAAAAAAAABAAAAAgMAAAEAAQABAAAA8AIA"
142     "AAQAAABwEAMAAAAOAAIAAgAAAAAA9QIAAAEAAAAOAAAAAAAAAAAAAAAAAAAAAQAAAAEAAABkAQAA"
143     "cAEAAAAAAAAAAAAAAAAAAAEAAAAEAAAAAgAAAAMAAwAGPGluaXQ+AA5Bbm5vQ2xhc3MuamF2YQAB"
144     "TAALTEFubm9DbGFzczsADkxNeUFubm90YXRpb247ABJMamF2YS9sYW5nL09iamVjdDsAEkxqYXZh"
145     "L2xhbmcvU3RyaW5nOwAhTGphdmEvbGFuZy9hbm5vdGF0aW9uL0Fubm90YXRpb247ACBMamF2YS9s"
146     "YW5nL2Fubm90YXRpb24vUmV0ZW50aW9uOwAmTGphdmEvbGFuZy9hbm5vdGF0aW9uL1JldGVudGlv"
147     "blBvbGljeTsAEU15QW5ub3RhdGlvbi5qYXZhAAdSVU5USU1FAAFWAANWTEwAAWEABWFOYW1lAARu"
148     "YW1lAAV2YWx1ZQABAAcOAAICAAAHDgABBQERGwABAQEQFw8AAAIAAICABIQDAQmcAwAAAAECgQgA"
149     "AAARAAAAAAAAAAEAAAAAAAAAAQAAABIAAABwAAAAAgAAAAgAAAC4AAAAAwAAAAMAAADYAAAABAAA"
150     "AAEAAAD8AAAABQAAAAQAAAAEAQAABgAAAAIAAAAkAQAAAhAAAAEAAABkAQAAAxAAAAMAAABwAQAA"
151     "ASAAAAIAAACEAQAABiAAAAIAAACwAQAAARAAAAIAAADYAQAAAiAAABIAAADoAQAAAyAAAAIAAADw"
152     "AgAABCAAAAIAAAD8AgAAACAAAAIAAAAIAwAAABAAAAEAAAAgAwAA";
153 
154 // Dex file with shared empty class data item for multiple class defs.
155 // Constructing by building a dex file with multiple classes and hex editing.
156 static const char kMultiClassDataInputDex[] =
157     "ZGV4CjAzNQALJgF9TtnLq748xVe/+wyxETrT9lTEiW6YAQAAcAAAAHhWNBIAAAAAAAAAADQBAAAI"
158     "AAAAcAAAAAQAAACQAAAAAAAAAAAAAAACAAAAoAAAAAAAAAAAAAAAAgAAALAAAACoAAAA8AAAAPAA"
159     "AAD4AAAAAAEAAAMBAAAIAQAADQEAACEBAAAkAQAAAgAAAAMAAAAEAAAABQAAAAEAAAAGAAAAAgAA"
160     "AAcAAAABAAAAAQYAAAMAAAAAAAAAAAAAAAAAAAAnAQAAAAAAAAIAAAABBgAAAwAAAAAAAAABAAAA"
161     "AAAAACcBAAAAAAAABkEuamF2YQAGQi5qYXZhAAFJAANMQTsAA0xCOwASTGphdmEvbGFuZy9PYmpl"
162     "Y3Q7AAFhAAFiAAAAAAABAAAAARkAAAAIAAAAAAAAAAEAAAAAAAAAAQAAAAgAAABwAAAAAgAAAAQA"
163     "AACQAAAABAAAAAIAAACgAAAABgAAAAIAAACwAAAAAiAAAAgAAADwAAAAACAAAAIAAAAnAQAAABAA"
164     "AAEAAAA0AQAA";
165 
166 // Dex file with code info followed by non 4-byte aligned section.
167 // Constructed a dex file with code info followed by string data and hex edited.
168 static const char kUnalignedCodeInfoInputDex[] =
169     "ZGV4CjAzNQDXJzXNb4iWn2SLhmLydW/8h1K9moERIw7UAQAAcAAAAHhWNBIAAAAAAAAAAEwBAAAG"
170     "AAAAcAAAAAMAAACIAAAAAQAAAJQAAAAAAAAAAAAAAAMAAACgAAAAAQAAALgAAAD8AAAA2AAAAAIB"
171     "AAAKAQAAEgEAABcBAAArAQAALgEAAAIAAAADAAAABAAAAAQAAAACAAAAAAAAAAAAAAAAAAAAAAAA"
172     "AAUAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAABAAAAAAAAADsBAAAAAAAAAQABAAEAAAAxAQAA"
173     "BAAAAHAQAgAAAA4AAQABAAAAAAA2AQAAAQAAAA4ABjxpbml0PgAGQS5qYXZhAANMQTsAEkxqYXZh"
174     "L2xhbmcvT2JqZWN0OwABVgABYQABAAcOAAMABw4AAAABAQCBgATYAQEB8AEAAAALAAAAAAAAAAEA"
175     "AAAAAAAAAQAAAAYAAABwAAAAAgAAAAMAAACIAAAAAwAAAAEAAACUAAAABQAAAAMAAACgAAAABgAA"
176     "AAEAAAC4AAAAASAAAAIAAADYAAAAAiAAAAYAAAACAQAAAyAAAAIAAAAxAQAAACAAAAEAAAA7AQAA"
177     "ABAAAAEAAABMAQAA";
178 
179 // Dex file with class data section preceding code items.
180 // Constructed by passing dex file through dexmerger tool and hex editing.
181 static const char kClassDataBeforeCodeInputDex[] =
182     "ZGV4CjAzNQCZKmCu3XXn4zvxCh5VH0gZNNobEAcsc49EAgAAcAAAAHhWNBIAAAAAAAAAAAQBAAAJ"
183     "AAAAcAAAAAQAAACUAAAAAgAAAKQAAAAAAAAAAAAAAAUAAAC8AAAAAQAAAOQAAABAAQAABAEAAPgB"
184     "AAAAAgAACAIAAAsCAAAQAgAAJAIAACcCAAAqAgAALQIAAAIAAAADAAAABAAAAAUAAAACAAAAAAAA"
185     "AAAAAAAFAAAAAwAAAAAAAAABAAEAAAAAAAEAAAAGAAAAAQAAAAcAAAABAAAACAAAAAIAAQAAAAAA"
186     "AQAAAAEAAAACAAAAAAAAAAEAAAAAAAAAjAEAAAAAAAALAAAAAAAAAAEAAAAAAAAAAQAAAAkAAABw"
187     "AAAAAgAAAAQAAACUAAAAAwAAAAIAAACkAAAABQAAAAUAAAC8AAAABgAAAAEAAADkAAAAABAAAAEA"
188     "AAAEAQAAACAAAAEAAACMAQAAASAAAAQAAACkAQAAAiAAAAkAAAD4AQAAAyAAAAQAAAAwAgAAAAAB"
189     "AwCBgASkAwEBvAMBAdADAQHkAwAAAQABAAEAAAAwAgAABAAAAHAQBAAAAA4AAgABAAAAAAA1AgAA"
190     "AgAAABIQDwACAAEAAAAAADoCAAACAAAAEiAPAAIAAQAAAAAAPwIAAAIAAAASMA8ABjxpbml0PgAG"
191     "QS5qYXZhAAFJAANMQTsAEkxqYXZhL2xhbmcvT2JqZWN0OwABVgABYQABYgABYwABAAcOAAMABw4A"
192     "BgAHDgAJAAcOAA==";
193 
194 // Dex file with local info containing a null type descriptor.
195 // Constructed a dex file with debug info sequence containing DBG_RESTART_LOCAL without any
196 // DBG_START_LOCAL to give it a declared type.
197 static const char kUnknownTypeDebugInfoInputDex[] =
198     "ZGV4CjAzNQBtKqZfzjHLNSNwW2A6Bz9FuCEX0sL+FF38AQAAcAAAAHhWNBIAAAAAAAAAAHQBAAAI"
199     "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAMAQAA8AAAABwB"
200     "AAAkAQAALAEAAC8BAAA0AQAASAEAAEsBAABOAQAAAgAAAAMAAAAEAAAABQAAAAIAAAAAAAAAAAAA"
201     "AAUAAAADAAAAAAAAAAEAAQAAAAAAAQAAAAYAAAACAAEAAAAAAAEAAAABAAAAAgAAAAAAAAABAAAA"
202     "AAAAAGMBAAAAAAAAAQABAAEAAABUAQAABAAAAHAQAgAAAA4AAgABAAAAAABZAQAAAgAAABIQDwAG"
203     "PGluaXQ+AAZBLmphdmEAAUkAA0xBOwASTGphdmEvbGFuZy9PYmplY3Q7AAFWAAFhAAR0aGlzAAEA"
204     "Bw4AAwAHDh4GAAYAAAAAAQEAgYAE8AEBAYgCAAAACwAAAAAAAAABAAAAAAAAAAEAAAAIAAAAcAAA"
205     "AAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAAuAAAAAYAAAABAAAA0AAAAAEgAAACAAAA"
206     "8AAAAAIgAAAIAAAAHAEAAAMgAAACAAAAVAEAAAAgAAABAAAAYwEAAAAQAAABAAAAdAEAAA==";
207 
208 // Dex file with multiple class data items pointing to the same code item.
209 // Constructed by hex editing.
210 static const char kDuplicateCodeItemInputDex[] =
211     "ZGV4CjAzNQCwKtVglQOmLWuHwldN5jkBOInC7mTMhJMAAgAAcAAAAHhWNBIAAAAAAAAAAHgBAAAH"
212     "AAAAcAAAAAMAAACMAAAAAQAAAJgAAAAAAAAAAAAAAAQAAACkAAAAAQAAAMQAAAAcAQAA5AAAACQB"
213     "AAAsAQAANAEAADkBAABNAQAAUAEAAFMBAAACAAAAAwAAAAQAAAAEAAAAAgAAAAAAAAAAAAAAAAAA"
214     "AAAAAAAFAAAAAAAAAAYAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAABAAAAAAAAAGUBAAAAAAAA"
215     "AQABAAEAAABWAQAABAAAAHAQAwAAAA4AAQABAAAAAABbAQAAAQAAAA4AAAABAAEAAAAAAGABAAAB"
216     "AAAADgAAAAY8aW5pdD4ABkEuamF2YQADTEE7ABJMamF2YS9sYW5nL09iamVjdDsAAVYAAWEAAWIA"
217     "AQAHDgADAAcOAAUABw4AAAABAgCBgATkAQEA/AEBAPwBAAsAAAAAAAAAAQAAAAAAAAABAAAABwAA"
218     "AHAAAAACAAAAAwAAAIwAAAADAAAAAQAAAJgAAAAFAAAABAAAAKQAAAAGAAAAAQAAAMQAAAABIAAA"
219     "AwAAAOQAAAACIAAABwAAACQBAAADIAAAAwAAAFYBAAAAIAAAAQAAAGUBAAAAEAAAAQAAAHgBAAA=";
220 
WriteBase64ToFile(const char * base64,File * file)221 static void WriteBase64ToFile(const char* base64, File* file) {
222   // Decode base64.
223   CHECK(base64 != nullptr);
224   size_t length;
225   std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length));
226   CHECK(bytes != nullptr);
227   if (!file->WriteFully(bytes.get(), length)) {
228     PLOG(FATAL) << "Failed to write base64 as file";
229   }
230 }
231 
WriteFileBase64(const char * base64,const char * location)232 static void WriteFileBase64(const char* base64, const char* location) {
233   // Write to provided file.
234   std::unique_ptr<File> file(OS::CreateEmptyFile(location));
235   CHECK(file != nullptr);
236   WriteBase64ToFile(base64, file.get());
237   if (file->FlushCloseOrErase() != 0) {
238     PLOG(FATAL) << "Could not flush and close test file.";
239   }
240 }
241 
242 class DexLayoutTest : public CommonRuntimeTest {
243  protected:
SetUp()244   virtual void SetUp() {
245     CommonRuntimeTest::SetUp();
246   }
247 
248   // Runs FullPlainOutput test.
FullPlainOutputExec(std::string * error_msg)249   bool FullPlainOutputExec(std::string* error_msg) {
250     // TODO: dexdump2 -> dexdump ?
251     ScratchFile dexdump_output;
252     const std::string& dexdump_filename = dexdump_output.GetFilename();
253     std::string dexdump = GetTestAndroidRoot() + "/bin/dexdump2";
254     EXPECT_TRUE(OS::FileExists(dexdump.c_str())) << dexdump << " should be a valid file path";
255 
256     ScratchFile dexlayout_output;
257     const std::string& dexlayout_filename = dexlayout_output.GetFilename();
258     std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
259     EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
260 
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 };
264       std::vector<std::string> dexlayout_exec_argv =
265           { dexlayout, "-d", "-f", "-h", "-l", "plain", "-o", dexlayout_filename, dex_file };
266       if (!::art::Exec(dexdump_exec_argv, error_msg)) {
267         return false;
268       }
269       if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
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       }
277     }
278     return true;
279   }
280 
281   // Runs DexFileOutput test.
DexFileOutputExec(std::string * error_msg)282   bool DexFileOutputExec(std::string* error_msg) {
283     ScratchFile tmp_file;
284     const std::string& tmp_name = tmp_file.GetFilename();
285     size_t tmp_last_slash = tmp_name.rfind('/');
286     std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
287     std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
288     EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
289 
290     for (const std::string &dex_file : GetLibCoreDexFileNames()) {
291       std::vector<std::string> dexlayout_exec_argv =
292           { dexlayout, "-w", tmp_dir, "-o", tmp_name, dex_file };
293       if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
294         return false;
295       }
296       size_t dex_file_last_slash = dex_file.rfind("/");
297       std::string dex_file_name = dex_file.substr(dex_file_last_slash + 1);
298       std::vector<std::string> unzip_exec_argv =
299           { "/usr/bin/unzip", dex_file, "classes.dex", "-d", tmp_dir};
300       if (!::art::Exec(unzip_exec_argv, error_msg)) {
301         return false;
302       }
303       std::vector<std::string> diff_exec_argv =
304           { "/usr/bin/diff", tmp_dir + "classes.dex" , tmp_dir + dex_file_name };
305       if (!::art::Exec(diff_exec_argv, error_msg)) {
306         return false;
307       }
308       std::vector<std::string> rm_zip_exec_argv = { "/bin/rm", tmp_dir + "classes.dex" };
309       if (!::art::Exec(rm_zip_exec_argv, error_msg)) {
310         return false;
311       }
312       std::vector<std::string> rm_out_exec_argv = { "/bin/rm", tmp_dir + dex_file_name };
313       if (!::art::Exec(rm_out_exec_argv, error_msg)) {
314         return false;
315       }
316     }
317     return true;
318   }
319 
320   // Runs DexFileLayout test.
DexFileLayoutExec(std::string * error_msg)321   bool DexFileLayoutExec(std::string* error_msg) {
322     ScratchFile tmp_file;
323     std::string tmp_name = tmp_file.GetFilename();
324     size_t tmp_last_slash = tmp_name.rfind("/");
325     std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
326 
327     // Write inputs and expected outputs.
328     std::string dex_file = tmp_dir + "classes.dex";
329     WriteFileBase64(kDexFileLayoutInputDex, dex_file.c_str());
330     std::string profile_file = tmp_dir + "primary.prof";
331     WriteFileBase64(kDexFileLayoutInputProfile, profile_file.c_str());
332     std::string output_dex = tmp_dir + "classes.dex.new";
333 
334     std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
335     EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
336 
337     std::vector<std::string> dexlayout_exec_argv =
338         { dexlayout, "-v", "-w", tmp_dir, "-o", tmp_name, "-p", profile_file, dex_file };
339     if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
340       return false;
341     }
342 
343     // -v makes sure that the layout did not corrupt the dex file.
344 
345     std::vector<std::string> rm_exec_argv =
346         { "/bin/rm", dex_file, profile_file, output_dex };
347     if (!::art::Exec(rm_exec_argv, error_msg)) {
348       return false;
349     }
350     return true;
351   }
352 
353   // Runs UnreferencedCatchHandlerTest & Unreferenced0SizeCatchHandlerTest.
UnreferencedCatchHandlerExec(std::string * error_msg,const char * filename)354   bool UnreferencedCatchHandlerExec(std::string* error_msg, const char* filename) {
355     ScratchFile tmp_file;
356     std::string tmp_name = tmp_file.GetFilename();
357     size_t tmp_last_slash = tmp_name.rfind("/");
358     std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
359 
360     // Write inputs and expected outputs.
361     std::string input_dex = tmp_dir + "classes.dex";
362     WriteFileBase64(filename, input_dex.c_str());
363     std::string output_dex = tmp_dir + "classes.dex.new";
364 
365     std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
366     EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
367 
368     std::vector<std::string> dexlayout_exec_argv =
369         { dexlayout, "-w", tmp_dir, "-o", "/dev/null", input_dex };
370     if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
371       return false;
372     }
373 
374     // Diff input and output. They should be the same.
375     std::vector<std::string> diff_exec_argv = { "/usr/bin/diff", input_dex, output_dex };
376     if (!::art::Exec(diff_exec_argv, error_msg)) {
377       return false;
378     }
379 
380     std::vector<std::string> rm_exec_argv = { "/bin/rm", input_dex, output_dex };
381     if (!::art::Exec(rm_exec_argv, error_msg)) {
382       return false;
383     }
384     return true;
385   }
386 
DexLayoutExec(ScratchFile * dex_file,const char * dex_filename,ScratchFile * profile_file,const char * profile_filename,std::vector<std::string> & dexlayout_exec_argv)387   bool DexLayoutExec(ScratchFile* dex_file,
388                      const char* dex_filename,
389                      ScratchFile* profile_file,
390                      const char* profile_filename,
391                      std::vector<std::string>& dexlayout_exec_argv) {
392     WriteBase64ToFile(dex_filename, dex_file->GetFile());
393     EXPECT_EQ(dex_file->GetFile()->Flush(), 0);
394     if (profile_file != nullptr) {
395       WriteBase64ToFile(profile_filename, profile_file->GetFile());
396       EXPECT_EQ(profile_file->GetFile()->Flush(), 0);
397     }
398     std::string error_msg;
399     const bool result = ::art::Exec(dexlayout_exec_argv, &error_msg);
400     if (!result) {
401       LOG(ERROR) << "Error: " << error_msg;
402       return false;
403     }
404     return true;
405   }
406 };
407 
408 
TEST_F(DexLayoutTest,FullPlainOutput)409 TEST_F(DexLayoutTest, FullPlainOutput) {
410   // Disable test on target.
411   TEST_DISABLED_FOR_TARGET();
412   std::string error_msg;
413   ASSERT_TRUE(FullPlainOutputExec(&error_msg)) << error_msg;
414 }
415 
TEST_F(DexLayoutTest,DexFileOutput)416 TEST_F(DexLayoutTest, DexFileOutput) {
417   // Disable test on target.
418   TEST_DISABLED_FOR_TARGET();
419   std::string error_msg;
420   ASSERT_TRUE(DexFileOutputExec(&error_msg)) << error_msg;
421 }
422 
TEST_F(DexLayoutTest,DexFileLayout)423 TEST_F(DexLayoutTest, DexFileLayout) {
424   // Disable test on target.
425   TEST_DISABLED_FOR_TARGET();
426   std::string error_msg;
427   ASSERT_TRUE(DexFileLayoutExec(&error_msg)) << error_msg;
428 }
429 
TEST_F(DexLayoutTest,UnreferencedCatchHandler)430 TEST_F(DexLayoutTest, UnreferencedCatchHandler) {
431   // Disable test on target.
432   TEST_DISABLED_FOR_TARGET();
433   std::string error_msg;
434   ASSERT_TRUE(UnreferencedCatchHandlerExec(&error_msg,
435                                            kUnreferencedCatchHandlerInputDex)) << error_msg;
436 }
437 
TEST_F(DexLayoutTest,Unreferenced0SizeCatchHandler)438 TEST_F(DexLayoutTest, Unreferenced0SizeCatchHandler) {
439   // Disable test on target.
440   TEST_DISABLED_FOR_TARGET();
441   std::string error_msg;
442   ASSERT_TRUE(UnreferencedCatchHandlerExec(&error_msg,
443                                            kUnreferenced0SizeCatchHandlerInputDex)) << error_msg;
444 }
445 
TEST_F(DexLayoutTest,UnreferencedEndingCatchHandler)446 TEST_F(DexLayoutTest, UnreferencedEndingCatchHandler) {
447   // Disable test on target.
448   TEST_DISABLED_FOR_TARGET();
449   std::string error_msg;
450   ASSERT_TRUE(UnreferencedCatchHandlerExec(&error_msg,
451                                            kUnreferencedEndingCatchHandlerInputDex)) << error_msg;
452 }
453 
TEST_F(DexLayoutTest,DuplicateOffset)454 TEST_F(DexLayoutTest, DuplicateOffset) {
455   ScratchFile temp_dex;
456   std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
457   EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
458   std::vector<std::string> dexlayout_exec_argv =
459       { dexlayout, "-a", "-i", "-o", "/dev/null", temp_dex.GetFilename() };
460   ASSERT_TRUE(DexLayoutExec(&temp_dex,
461                             kDexFileDuplicateOffset,
462                             nullptr /* profile_file */,
463                             nullptr /* profile_filename */,
464                             dexlayout_exec_argv));
465 }
466 
TEST_F(DexLayoutTest,NullSetRefListElement)467 TEST_F(DexLayoutTest, NullSetRefListElement) {
468   ScratchFile temp_dex;
469   std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
470   EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
471   std::vector<std::string> dexlayout_exec_argv =
472       { dexlayout, "-o", "/dev/null", temp_dex.GetFilename() };
473   ASSERT_TRUE(DexLayoutExec(&temp_dex,
474                             kNullSetRefListElementInputDex,
475                             nullptr /* profile_file */,
476                             nullptr /* profile_filename */,
477                             dexlayout_exec_argv));
478 }
479 
TEST_F(DexLayoutTest,MultiClassData)480 TEST_F(DexLayoutTest, MultiClassData) {
481   ScratchFile temp_dex;
482   ScratchFile temp_profile;
483   std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
484   EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
485   std::vector<std::string> dexlayout_exec_argv =
486       { dexlayout, "-p", temp_profile.GetFilename(), "-o", "/dev/null", temp_dex.GetFilename() };
487   ASSERT_TRUE(DexLayoutExec(&temp_dex,
488                             kMultiClassDataInputDex,
489                             &temp_profile,
490                             kDexFileLayoutInputProfile,
491                             dexlayout_exec_argv));
492 }
493 
TEST_F(DexLayoutTest,UnalignedCodeInfo)494 TEST_F(DexLayoutTest, UnalignedCodeInfo) {
495   ScratchFile temp_dex;
496   ScratchFile temp_profile;
497   std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
498   EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
499   std::vector<std::string> dexlayout_exec_argv =
500       { dexlayout, "-p", temp_profile.GetFilename(), "-o", "/dev/null", temp_dex.GetFilename() };
501   ASSERT_TRUE(DexLayoutExec(&temp_dex,
502                             kUnalignedCodeInfoInputDex,
503                             &temp_profile,
504                             kDexFileLayoutInputProfile,
505                             dexlayout_exec_argv));
506 }
507 
TEST_F(DexLayoutTest,ClassDataBeforeCode)508 TEST_F(DexLayoutTest, ClassDataBeforeCode) {
509   ScratchFile temp_dex;
510   ScratchFile temp_profile;
511   std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
512   EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
513   std::vector<std::string> dexlayout_exec_argv =
514       { dexlayout, "-p", temp_profile.GetFilename(), "-o", "/dev/null", temp_dex.GetFilename() };
515   ASSERT_TRUE(DexLayoutExec(&temp_dex,
516                             kClassDataBeforeCodeInputDex,
517                             &temp_profile,
518                             kDexFileLayoutInputProfile,
519                             dexlayout_exec_argv));
520 }
521 
TEST_F(DexLayoutTest,UnknownTypeDebugInfo)522 TEST_F(DexLayoutTest, UnknownTypeDebugInfo) {
523   ScratchFile temp_dex;
524   std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
525   EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
526   std::vector<std::string> dexlayout_exec_argv =
527       { dexlayout, "-o", "/dev/null", temp_dex.GetFilename() };
528   ASSERT_TRUE(DexLayoutExec(&temp_dex,
529                             kUnknownTypeDebugInfoInputDex,
530                             nullptr /* profile_file */,
531                             nullptr /* profile_filename */,
532                             dexlayout_exec_argv));
533 }
534 
TEST_F(DexLayoutTest,DuplicateCodeItem)535 TEST_F(DexLayoutTest, DuplicateCodeItem) {
536   ScratchFile temp_dex;
537   std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
538   EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
539   std::vector<std::string> dexlayout_exec_argv =
540       { dexlayout, "-o", "/dev/null", temp_dex.GetFilename() };
541   ASSERT_TRUE(DexLayoutExec(&temp_dex,
542                             kDuplicateCodeItemInputDex,
543                             nullptr /* profile_file */,
544                             nullptr /* profile_filename */,
545                             dexlayout_exec_argv));
546 }
547 
548 }  // namespace art
549