blob: 54157d9e3a5f57a75228b3bdffc736701cfb090f [file] [log] [blame]
Jeff Haoc3acfc52016-08-29 14:18:26 -07001/*
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 Gampe8cf9cb32017-07-19 09:28:38 -070017#include <sstream>
Jeff Haoc3acfc52016-08-29 14:18:26 -070018#include <string>
19#include <vector>
Jeff Haoc3acfc52016-08-29 14:18:26 -070020
21#include <sys/types.h>
22#include <unistd.h>
23
Jeff Hao042e8982016-10-19 11:17:11 -070024#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080025#include "base/utils.h"
Jeff Haoc3acfc52016-08-29 14:18:26 -070026#include "common_runtime_test.h"
David Sehr013fd802018-01-11 22:55:24 -080027#include "dex/art_dex_file_loader.h"
David Sehre1123402018-02-01 02:46:18 -080028#include "dex/base64_test_util.h"
Mathieu Chartiera34812c2018-08-01 17:10:43 -070029#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080030#include "dex/code_item_accessors-inl.h"
31#include "dex/dex_file-inl.h"
32#include "dex/dex_file_loader.h"
Mathieu Chartier75175552018-01-25 11:23:01 -080033#include "dexlayout.h"
David Sehr97c381e2017-02-01 15:09:58 -080034#include "exec_utils.h"
David Sehr82d046e2018-04-23 08:14:19 -070035#include "profile/profile_compilation_info.h"
Jeff Haoc3acfc52016-08-29 14:18:26 -070036
37namespace art {
38
Jeff Hao042e8982016-10-19 11:17:11 -070039static 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
Jeff Haoe17f5892017-02-23 16:14:04 -080051// Dex file with catch handler unreferenced by try blocks.
52// Constructed by building a dex file with try/catch blocks and hex editing.
53static 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
Jeff Haoac462712017-03-02 10:59:43 -080071// 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.
73static 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
Jeff Haoa64a64c2017-03-06 15:05:38 -080091// 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.
93static 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
Mathieu Chartier6b0dbef2017-02-21 12:45:30 -0800114// Dex file with multiple code items that have the same debug_info_off_. Constructed by a modified
115// dexlayout on XandY.
116static 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
Jeff Haof914f8e2017-02-21 16:14:23 -0800137// 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.
139static 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
Jeff Haoe17f5892017-02-23 16:14:04 -0800159// 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.
161static 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.
173static const char kUnalignedCodeInfoInputDex[] =
174 "ZGV4CjAzNQDXJzXNb4iWn2SLhmLydW/8h1K9moERIw7UAQAAcAAAAHhWNBIAAAAAAAAAAEwBAAAG"
175 "AAAAcAAAAAMAAACIAAAAAQAAAJQAAAAAAAAAAAAAAAMAAACgAAAAAQAAALgAAAD8AAAA2AAAAAIB"
176 "AAAKAQAAEgEAABcBAAArAQAALgEAAAIAAAADAAAABAAAAAQAAAACAAAAAAAAAAAAAAAAAAAAAAAA"
177 "AAUAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAABAAAAAAAAADsBAAAAAAAAAQABAAEAAAAxAQAA"
178 "BAAAAHAQAgAAAA4AAQABAAAAAAA2AQAAAQAAAA4ABjxpbml0PgAGQS5qYXZhAANMQTsAEkxqYXZh"
179 "L2xhbmcvT2JqZWN0OwABVgABYQABAAcOAAMABw4AAAABAQCBgATYAQEB8AEAAAALAAAAAAAAAAEA"
180 "AAAAAAAAAQAAAAYAAABwAAAAAgAAAAMAAACIAAAAAwAAAAEAAACUAAAABQAAAAMAAACgAAAABgAA"
181 "AAEAAAC4AAAAASAAAAIAAADYAAAAAiAAAAYAAAACAQAAAyAAAAIAAAAxAQAAACAAAAEAAAA7AQAA"
182 "ABAAAAEAAABMAQAA";
Jeff Hao44652a32017-02-22 14:20:41 -0800183
Jeff Hao32a60ab2017-03-01 16:44:50 -0800184// Dex file with class data section preceding code items.
185// Constructed by passing dex file through dexmerger tool and hex editing.
186static 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
Jeff Haod9be7682017-04-05 09:29:43 -0700199// 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.
202static const char kUnknownTypeDebugInfoInputDex[] =
203 "ZGV4CjAzNQBtKqZfzjHLNSNwW2A6Bz9FuCEX0sL+FF38AQAAcAAAAHhWNBIAAAAAAAAAAHQBAAAI"
204 "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAMAQAA8AAAABwB"
205 "AAAkAQAALAEAAC8BAAA0AQAASAEAAEsBAABOAQAAAgAAAAMAAAAEAAAABQAAAAIAAAAAAAAAAAAA"
206 "AAUAAAADAAAAAAAAAAEAAQAAAAAAAQAAAAYAAAACAAEAAAAAAAEAAAABAAAAAgAAAAAAAAABAAAA"
207 "AAAAAGMBAAAAAAAAAQABAAEAAABUAQAABAAAAHAQAgAAAA4AAgABAAAAAABZAQAAAgAAABIQDwAG"
208 "PGluaXQ+AAZBLmphdmEAAUkAA0xBOwASTGphdmEvbGFuZy9PYmplY3Q7AAFWAAFhAAR0aGlzAAEA"
209 "Bw4AAwAHDh4GAAYAAAAAAQEAgYAE8AEBAYgCAAAACwAAAAAAAAABAAAAAAAAAAEAAAAIAAAAcAAA"
210 "AAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAAuAAAAAYAAAABAAAA0AAAAAEgAAACAAAA"
211 "8AAAAAIgAAAIAAAAHAEAAAMgAAACAAAAVAEAAAAgAAABAAAAYwEAAAAQAAABAAAAdAEAAA==";
212
Jeff Haod212d5b2017-04-26 12:09:06 -0700213// Dex file with multiple class data items pointing to the same code item.
214// Constructed by hex editing.
215static const char kDuplicateCodeItemInputDex[] =
216 "ZGV4CjAzNQCwKtVglQOmLWuHwldN5jkBOInC7mTMhJMAAgAAcAAAAHhWNBIAAAAAAAAAAHgBAAAH"
217 "AAAAcAAAAAMAAACMAAAAAQAAAJgAAAAAAAAAAAAAAAQAAACkAAAAAQAAAMQAAAAcAQAA5AAAACQB"
218 "AAAsAQAANAEAADkBAABNAQAAUAEAAFMBAAACAAAAAwAAAAQAAAAEAAAAAgAAAAAAAAAAAAAAAAAA"
219 "AAAAAAAFAAAAAAAAAAYAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAABAAAAAAAAAGUBAAAAAAAA"
220 "AQABAAEAAABWAQAABAAAAHAQAwAAAA4AAQABAAAAAABbAQAAAQAAAA4AAAABAAEAAAAAAGABAAAB"
221 "AAAADgAAAAY8aW5pdD4ABkEuamF2YQADTEE7ABJMamF2YS9sYW5nL09iamVjdDsAAVYAAWEAAWIA"
222 "AQAHDgADAAcOAAUABw4AAAABAgCBgATkAQEA/AEBAPwBAAsAAAAAAAAAAQAAAAAAAAABAAAABwAA"
223 "AHAAAAACAAAAAwAAAIwAAAADAAAAAQAAAJgAAAAFAAAABAAAAKQAAAAGAAAAAQAAAMQAAAABIAAA"
224 "AwAAAOQAAAACIAAABwAAACQBAAADIAAAAwAAAFYBAAAAIAAAAQAAAGUBAAAAEAAAAQAAAHgBAAA=";
225
Mathieu Chartiera27af082018-02-03 16:12:23 -0800226// Returns the default compact dex option for dexlayout based on kDefaultCompactDexLevel.
227static 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
Mathieu Chartier6b0dbef2017-02-21 12:45:30 -0800232static void WriteBase64ToFile(const char* base64, File* file) {
Jeff Hao042e8982016-10-19 11:17:11 -0700233 // Decode base64.
234 CHECK(base64 != nullptr);
235 size_t length;
236 std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length));
Mathieu Chartier6b0dbef2017-02-21 12:45:30 -0800237 CHECK(bytes != nullptr);
Jeff Hao042e8982016-10-19 11:17:11 -0700238 if (!file->WriteFully(bytes.get(), length)) {
239 PLOG(FATAL) << "Failed to write base64 as file";
240 }
Mathieu Chartier6b0dbef2017-02-21 12:45:30 -0800241}
242
243static 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());
Jeff Hao042e8982016-10-19 11:17:11 -0700248 if (file->FlushCloseOrErase() != 0) {
249 PLOG(FATAL) << "Could not flush and close test file.";
250 }
251}
252
Jeff Haoc3acfc52016-08-29 14:18:26 -0700253class DexLayoutTest : public CommonRuntimeTest {
254 protected:
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800255 std::string GetDexLayoutPath() {
256 return GetTestAndroidRoot() + "/bin/dexlayoutd";
Jeff Haoc3acfc52016-08-29 14:18:26 -0700257 }
258
Jeff Haoa8621002016-10-04 18:13:44 +0000259 // Runs FullPlainOutput test.
260 bool FullPlainOutputExec(std::string* error_msg) {
Jeff Haoc3acfc52016-08-29 14:18:26 -0700261 // TODO: dexdump2 -> dexdump ?
262 ScratchFile dexdump_output;
Andreas Gampeca620d72016-11-08 08:09:33 -0800263 const std::string& dexdump_filename = dexdump_output.GetFilename();
Jeff Haoc3acfc52016-08-29 14:18:26 -0700264 std::string dexdump = GetTestAndroidRoot() + "/bin/dexdump2";
265 EXPECT_TRUE(OS::FileExists(dexdump.c_str())) << dexdump << " should be a valid file path";
Jeff Haoc3acfc52016-08-29 14:18:26 -0700266
267 ScratchFile dexlayout_output;
Andreas Gampeca620d72016-11-08 08:09:33 -0800268 const std::string& dexlayout_filename = dexlayout_output.GetFilename();
Jeff Haoc3acfc52016-08-29 14:18:26 -0700269
Mathieu Chartier41468402018-08-29 11:39:00 -0700270 for (const std::string& dex_file : GetLibCoreDexFileNames()) {
Jeff Haoa8621002016-10-04 18:13:44 +0000271 std::vector<std::string> dexdump_exec_argv =
272 { dexdump, "-d", "-f", "-h", "-l", "plain", "-o", dexdump_filename, dex_file };
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800273 std::vector<std::string> dexlayout_args =
274 { "-d", "-f", "-h", "-l", "plain", "-o", dexlayout_filename, dex_file };
Jeff Haoa8621002016-10-04 18:13:44 +0000275 if (!::art::Exec(dexdump_exec_argv, error_msg)) {
276 return false;
277 }
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800278 if (!DexLayoutExec(dexlayout_args, error_msg)) {
Jeff Haoa8621002016-10-04 18:13:44 +0000279 return false;
280 }
281 std::vector<std::string> diff_exec_argv =
282 { "/usr/bin/diff", dexdump_filename, dexlayout_filename };
283 if (!::art::Exec(diff_exec_argv, error_msg)) {
284 return false;
285 }
Jeff Haoc3acfc52016-08-29 14:18:26 -0700286 }
287 return true;
288 }
289
Jeff Haoa8621002016-10-04 18:13:44 +0000290 // Runs DexFileOutput test.
291 bool DexFileOutputExec(std::string* error_msg) {
292 ScratchFile tmp_file;
Andreas Gampeca620d72016-11-08 08:09:33 -0800293 const std::string& tmp_name = tmp_file.GetFilename();
294 size_t tmp_last_slash = tmp_name.rfind('/');
Jeff Haoa8621002016-10-04 18:13:44 +0000295 std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
Mathieu Chartier41468402018-08-29 11:39:00 -0700296 std::string unzip_dir = tmp_dir + "unzip/";
Jeff Haoa8621002016-10-04 18:13:44 +0000297
Mathieu Chartier41468402018-08-29 11:39:00 -0700298 for (const std::string& dex_file : GetLibCoreDexFileNames()) {
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800299 std::vector<std::string> dexlayout_args =
300 { "-w", tmp_dir, "-o", tmp_name, dex_file };
Andreas Gampe9b031f72018-10-04 11:03:34 -0700301 if (!DexLayoutExec(dexlayout_args, error_msg, /*pass_default_cdex_option=*/ false)) {
Jeff Haoa8621002016-10-04 18:13:44 +0000302 return false;
303 }
Mathieu Chartier41468402018-08-29 11:39:00 -0700304 std::string dex_file_name = "classes.dex";
Jeff Haoa8621002016-10-04 18:13:44 +0000305 std::vector<std::string> unzip_exec_argv =
Mathieu Chartier41468402018-08-29 11:39:00 -0700306 { "/usr/bin/unzip", dex_file, "classes.dex", "-d", unzip_dir};
Jeff Haoa8621002016-10-04 18:13:44 +0000307 if (!::art::Exec(unzip_exec_argv, error_msg)) {
308 return false;
309 }
310 std::vector<std::string> diff_exec_argv =
Mathieu Chartier41468402018-08-29 11:39:00 -0700311 { "/usr/bin/diff", tmp_dir + "classes.dex" , unzip_dir + dex_file_name };
Jeff Haoa8621002016-10-04 18:13:44 +0000312 if (!::art::Exec(diff_exec_argv, error_msg)) {
313 return false;
314 }
Mathieu Chartier41468402018-08-29 11:39:00 -0700315 if (!UnlinkFile(unzip_dir + "classes.dex")) {
Jeff Haoa8621002016-10-04 18:13:44 +0000316 return false;
317 }
Orion Hodson98273672018-02-06 11:50:27 +0000318 if (!UnlinkFile(tmp_dir + dex_file_name)) {
Jeff Haoa8621002016-10-04 18:13:44 +0000319 return false;
320 }
Mathieu Chartier41468402018-08-29 11:39:00 -0700321 // Remove the unzip temp directory so that unlinking android_data doesn't fail.
322 EXPECT_EQ(rmdir(unzip_dir.c_str()), 0);
Jeff Haoa8621002016-10-04 18:13:44 +0000323 }
Jeff Hao042e8982016-10-19 11:17:11 -0700324 return true;
325 }
Jeff Haoa8621002016-10-04 18:13:44 +0000326
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700327 // Create a profile with some subset of methods and classes.
328 void CreateProfile(const std::string& input_dex,
329 const std::string& out_profile,
330 const std::string& dex_location) {
331 std::vector<std::unique_ptr<const DexFile>> dex_files;
332 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -0800333 const ArtDexFileLoader dex_file_loader;
334 bool result = dex_file_loader.Open(input_dex.c_str(),
335 input_dex,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700336 /*verify=*/ true,
337 /*verify_checksum=*/ false,
David Sehr013fd802018-01-11 22:55:24 -0800338 &error_msg,
339 &dex_files);
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700340
341 ASSERT_TRUE(result) << error_msg;
342 ASSERT_GE(dex_files.size(), 1u);
343
344 size_t profile_methods = 0;
345 size_t profile_classes = 0;
346 ProfileCompilationInfo pfi;
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700347 std::set<DexCacheResolvedClasses> classes;
348 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
349 for (uint32_t i = 0; i < dex_file->NumMethodIds(); i += 2) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700350 uint8_t flags = 0u;
351
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700352 if ((i & 3) != 0) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700353 flags |= ProfileCompilationInfo::MethodHotness::kFlagHot;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700354 ++profile_methods;
355 } else if ((i & 2) != 0) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700356 flags |= ProfileCompilationInfo::MethodHotness::kFlagStartup;
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700357 ++profile_methods;
358 }
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700359 pfi.AddMethodIndex(static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags),
360 dex_location,
361 dex_file->GetLocationChecksum(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700362 /*method_idx=*/i,
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700363 dex_file->NumMethodIds());
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700364 }
365 DexCacheResolvedClasses cur_classes(dex_location,
366 dex_location,
Mathieu Chartierea650f32017-05-24 12:04:13 -0700367 dex_file->GetLocationChecksum(),
368 dex_file->NumMethodIds());
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700369 // Add every even class too.
370 for (uint32_t i = 0; i < dex_file->NumClassDefs(); i += 1) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700371 if ((i & 2) == 0) {
372 cur_classes.AddClass(dex_file->GetClassDef(i).class_idx_);
373 ++profile_classes;
374 }
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700375 }
Mathieu Chartierea650f32017-05-24 12:04:13 -0700376 classes.insert(cur_classes);
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700377 }
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700378 pfi.AddClasses(classes);
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700379 // Write to provided file.
380 std::unique_ptr<File> file(OS::CreateEmptyFile(out_profile.c_str()));
381 ASSERT_TRUE(file != nullptr);
382 pfi.Save(file->Fd());
383 if (file->FlushCloseOrErase() != 0) {
384 PLOG(FATAL) << "Could not flush and close test file.";
385 }
386 EXPECT_GE(profile_methods, 0u);
387 EXPECT_GE(profile_classes, 0u);
388 }
389
Jeff Haod9be7682017-04-05 09:29:43 -0700390 // Runs DexFileLayout test.
Jeff Hao042e8982016-10-19 11:17:11 -0700391 bool DexFileLayoutExec(std::string* error_msg) {
392 ScratchFile tmp_file;
Andreas Gampe641a4732017-08-24 13:21:35 -0700393 const std::string& tmp_name = tmp_file.GetFilename();
Andreas Gampe5555dd12017-08-24 13:50:21 -0700394 size_t tmp_last_slash = tmp_name.rfind('/');
Jeff Hao042e8982016-10-19 11:17:11 -0700395 std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
396
397 // Write inputs and expected outputs.
398 std::string dex_file = tmp_dir + "classes.dex";
399 WriteFileBase64(kDexFileLayoutInputDex, dex_file.c_str());
400 std::string profile_file = tmp_dir + "primary.prof";
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700401 CreateProfile(dex_file, profile_file, dex_file);
402 // WriteFileBase64(kDexFileLayoutInputProfile, profile_file.c_str());
Jeff Hao042e8982016-10-19 11:17:11 -0700403 std::string output_dex = tmp_dir + "classes.dex.new";
404
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800405 std::vector<std::string> dexlayout_args =
406 { "-v", "-w", tmp_dir, "-o", tmp_name, "-p", profile_file, dex_file };
407 if (!DexLayoutExec(dexlayout_args, error_msg)) {
Jeff Hao042e8982016-10-19 11:17:11 -0700408 return false;
409 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -0700410
411 // -v makes sure that the layout did not corrupt the dex file.
Orion Hodson98273672018-02-06 11:50:27 +0000412 if (!UnlinkFile(dex_file) || !UnlinkFile(profile_file) || !UnlinkFile(output_dex)) {
Jeff Hao042e8982016-10-19 11:17:11 -0700413 return false;
414 }
Jeff Haoa8621002016-10-04 18:13:44 +0000415 return true;
416 }
Jeff Hao44652a32017-02-22 14:20:41 -0800417
Bharadwaj Kalandhabhatta02ef6402017-05-17 17:13:39 -0700418 // Runs DexFileLayout test twice (second time is run on output of first time)
419 // for behavior consistency.
420 bool DexFileLayoutFixedPointExec(std::string* error_msg) {
421 ScratchFile tmp_file;
Andreas Gampe641a4732017-08-24 13:21:35 -0700422 const std::string& tmp_name = tmp_file.GetFilename();
Andreas Gampe5555dd12017-08-24 13:50:21 -0700423 size_t tmp_last_slash = tmp_name.rfind('/');
Bharadwaj Kalandhabhatta02ef6402017-05-17 17:13:39 -0700424 std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
425
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700426 // Unzip the test dex file to the classes.dex destination. It is required to unzip since
427 // opening from jar recalculates the dex location checksum.
Bharadwaj Kalandhabhatta02ef6402017-05-17 17:13:39 -0700428 std::string dex_file = tmp_dir + "classes.dex";
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700429
430 std::vector<std::string> unzip_args = {
431 "/usr/bin/unzip",
432 GetTestDexFileName("ManyMethods"),
433 "classes.dex",
434 "-d",
435 tmp_dir,
436 };
437 if (!art::Exec(unzip_args, error_msg)) {
438 LOG(ERROR) << "Failed to unzip dex";
439 return false;
440 }
441
Bharadwaj Kalandhabhatta02ef6402017-05-17 17:13:39 -0700442 std::string profile_file = tmp_dir + "primary.prof";
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700443 CreateProfile(dex_file, profile_file, dex_file);
Bharadwaj Kalandhabhatta02ef6402017-05-17 17:13:39 -0700444 std::string output_dex = tmp_dir + "classes.dex.new";
445 std::string second_output_dex = tmp_dir + "classes.dex.new.new";
446
Bharadwaj Kalandhabhatta02ef6402017-05-17 17:13:39 -0700447 // -v makes sure that the layout did not corrupt the dex file.
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800448 std::vector<std::string> dexlayout_args =
449 { "-i", "-v", "-w", tmp_dir, "-o", tmp_name, "-p", profile_file, dex_file };
Andreas Gampe9b031f72018-10-04 11:03:34 -0700450 if (!DexLayoutExec(dexlayout_args, error_msg, /*pass_default_cdex_option=*/ false)) {
Bharadwaj Kalandhabhatta02ef6402017-05-17 17:13:39 -0700451 return false;
452 }
453
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700454 // Recreate the profile with the new dex location. This is required so that the profile dex
455 // location matches.
456 CreateProfile(dex_file, profile_file, output_dex);
457
Bharadwaj Kalandhabhatta02ef6402017-05-17 17:13:39 -0700458 // -v makes sure that the layout did not corrupt the dex file.
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700459 // -i since the checksum won't match from the first layout.
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800460 std::vector<std::string> second_dexlayout_args =
461 { "-i", "-v", "-w", tmp_dir, "-o", tmp_name, "-p", profile_file, output_dex };
Andreas Gampe9b031f72018-10-04 11:03:34 -0700462 if (!DexLayoutExec(second_dexlayout_args, error_msg, /*pass_default_cdex_option=*/ false)) {
Bharadwaj Kalandhabhatta02ef6402017-05-17 17:13:39 -0700463 return false;
464 }
465
466 bool diff_result = true;
467 std::vector<std::string> diff_exec_argv =
468 { "/usr/bin/diff", output_dex, second_output_dex };
469 if (!::art::Exec(diff_exec_argv, error_msg)) {
470 diff_result = false;
471 }
472
Orion Hodson98273672018-02-06 11:50:27 +0000473 std::vector<std::string> test_files = { dex_file, profile_file, output_dex, second_output_dex };
Andreas Gampebc802de2018-06-20 17:24:11 -0700474 for (const std::string& test_file : test_files) {
Orion Hodson98273672018-02-06 11:50:27 +0000475 if (!UnlinkFile(test_file)) {
476 return false;
477 }
Bharadwaj Kalandhabhatta02ef6402017-05-17 17:13:39 -0700478 }
479
480 return diff_result;
481 }
482
Jeff Haoac462712017-03-02 10:59:43 -0800483 // Runs UnreferencedCatchHandlerTest & Unreferenced0SizeCatchHandlerTest.
484 bool UnreferencedCatchHandlerExec(std::string* error_msg, const char* filename) {
Jeff Hao44652a32017-02-22 14:20:41 -0800485 ScratchFile tmp_file;
Andreas Gampe641a4732017-08-24 13:21:35 -0700486 const std::string& tmp_name = tmp_file.GetFilename();
Andreas Gampe5555dd12017-08-24 13:50:21 -0700487 size_t tmp_last_slash = tmp_name.rfind('/');
Jeff Hao44652a32017-02-22 14:20:41 -0800488 std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
489
490 // Write inputs and expected outputs.
491 std::string input_dex = tmp_dir + "classes.dex";
Jeff Haoac462712017-03-02 10:59:43 -0800492 WriteFileBase64(filename, input_dex.c_str());
Jeff Hao44652a32017-02-22 14:20:41 -0800493 std::string output_dex = tmp_dir + "classes.dex.new";
494
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800495 std::vector<std::string> dexlayout_args = { "-w", tmp_dir, "-o", "/dev/null", input_dex };
Andreas Gampe9b031f72018-10-04 11:03:34 -0700496 if (!DexLayoutExec(dexlayout_args, error_msg, /*pass_default_cdex_option=*/ false)) {
Jeff Hao44652a32017-02-22 14:20:41 -0800497 return false;
498 }
499
500 // Diff input and output. They should be the same.
501 std::vector<std::string> diff_exec_argv = { "/usr/bin/diff", input_dex, output_dex };
502 if (!::art::Exec(diff_exec_argv, error_msg)) {
503 return false;
504 }
505
Orion Hodson98273672018-02-06 11:50:27 +0000506 std::vector<std::string> dex_files = { input_dex, output_dex };
Andreas Gampebc802de2018-06-20 17:24:11 -0700507 for (const std::string& dex_file : dex_files) {
Orion Hodson98273672018-02-06 11:50:27 +0000508 if (!UnlinkFile(dex_file)) {
509 return false;
510 }
Jeff Hao44652a32017-02-22 14:20:41 -0800511 }
512 return true;
513 }
Jeff Haod9be7682017-04-05 09:29:43 -0700514
515 bool DexLayoutExec(ScratchFile* dex_file,
516 const char* dex_filename,
517 ScratchFile* profile_file,
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800518 const std::vector<std::string>& dexlayout_args) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700519 if (dex_filename != nullptr) {
520 WriteBase64ToFile(dex_filename, dex_file->GetFile());
521 EXPECT_EQ(dex_file->GetFile()->Flush(), 0);
522 }
Jeff Haod9be7682017-04-05 09:29:43 -0700523 if (profile_file != nullptr) {
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700524 CreateProfile(dex_file->GetFilename(), profile_file->GetFilename(), dex_file->GetFilename());
Jeff Haod9be7682017-04-05 09:29:43 -0700525 }
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800526
Jeff Haod9be7682017-04-05 09:29:43 -0700527 std::string error_msg;
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800528 const bool result = DexLayoutExec(dexlayout_args, &error_msg);
Jeff Haod9be7682017-04-05 09:29:43 -0700529 if (!result) {
530 LOG(ERROR) << "Error: " << error_msg;
531 return false;
532 }
533 return true;
534 }
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800535
Mathieu Chartiera27af082018-02-03 16:12:23 -0800536 bool DexLayoutExec(const std::vector<std::string>& dexlayout_args,
537 std::string* error_msg,
538 bool pass_default_cdex_option = true) {
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800539 std::vector<std::string> argv;
540
541 std::string dexlayout = GetDexLayoutPath();
542 CHECK(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
543 argv.push_back(dexlayout);
Mathieu Chartiera27af082018-02-03 16:12:23 -0800544 if (pass_default_cdex_option) {
545 std::vector<std::string> cdex_level = DefaultCompactDexOption();
546 argv.insert(argv.end(), cdex_level.begin(), cdex_level.end());
547 }
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800548
549 argv.insert(argv.end(), dexlayout_args.begin(), dexlayout_args.end());
550
551 return ::art::Exec(argv, error_msg);
552 }
Orion Hodson98273672018-02-06 11:50:27 +0000553
554 bool UnlinkFile(const std::string& file_path) {
555 return unix_file::FdFile(file_path, 0, false).Unlink();
556 }
Jeff Haoc3acfc52016-08-29 14:18:26 -0700557};
558
559
560TEST_F(DexLayoutTest, FullPlainOutput) {
Jeff Hao0f7eaeb2016-08-31 17:56:13 -0700561 // Disable test on target.
562 TEST_DISABLED_FOR_TARGET();
Jeff Haoc3acfc52016-08-29 14:18:26 -0700563 std::string error_msg;
Jeff Haoa8621002016-10-04 18:13:44 +0000564 ASSERT_TRUE(FullPlainOutputExec(&error_msg)) << error_msg;
565}
566
567TEST_F(DexLayoutTest, DexFileOutput) {
568 // Disable test on target.
569 TEST_DISABLED_FOR_TARGET();
570 std::string error_msg;
571 ASSERT_TRUE(DexFileOutputExec(&error_msg)) << error_msg;
Jeff Haoc3acfc52016-08-29 14:18:26 -0700572}
573
Jeff Hao042e8982016-10-19 11:17:11 -0700574TEST_F(DexLayoutTest, DexFileLayout) {
575 // Disable test on target.
576 TEST_DISABLED_FOR_TARGET();
577 std::string error_msg;
578 ASSERT_TRUE(DexFileLayoutExec(&error_msg)) << error_msg;
579}
580
Bharadwaj Kalandhabhatta02ef6402017-05-17 17:13:39 -0700581TEST_F(DexLayoutTest, DexFileLayoutFixedPoint) {
582 // Disable test on target.
583 TEST_DISABLED_FOR_TARGET();
584 std::string error_msg;
585 ASSERT_TRUE(DexFileLayoutFixedPointExec(&error_msg)) << error_msg;
586}
587
Jeff Haoe17f5892017-02-23 16:14:04 -0800588TEST_F(DexLayoutTest, UnreferencedCatchHandler) {
589 // Disable test on target.
590 TEST_DISABLED_FOR_TARGET();
591 std::string error_msg;
Jeff Haoac462712017-03-02 10:59:43 -0800592 ASSERT_TRUE(UnreferencedCatchHandlerExec(&error_msg,
593 kUnreferencedCatchHandlerInputDex)) << error_msg;
Jeff Haoe17f5892017-02-23 16:14:04 -0800594}
Jeff Haoac462712017-03-02 10:59:43 -0800595
596TEST_F(DexLayoutTest, Unreferenced0SizeCatchHandler) {
597 // Disable test on target.
598 TEST_DISABLED_FOR_TARGET();
599 std::string error_msg;
600 ASSERT_TRUE(UnreferencedCatchHandlerExec(&error_msg,
601 kUnreferenced0SizeCatchHandlerInputDex)) << error_msg;
602}
603
Jeff Haoa64a64c2017-03-06 15:05:38 -0800604TEST_F(DexLayoutTest, UnreferencedEndingCatchHandler) {
605 // Disable test on target.
606 TEST_DISABLED_FOR_TARGET();
607 std::string error_msg;
608 ASSERT_TRUE(UnreferencedCatchHandlerExec(&error_msg,
609 kUnreferencedEndingCatchHandlerInputDex)) << error_msg;
610}
611
Mathieu Chartier6b0dbef2017-02-21 12:45:30 -0800612TEST_F(DexLayoutTest, DuplicateOffset) {
Jeff Haod9be7682017-04-05 09:29:43 -0700613 ScratchFile temp_dex;
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800614 std::vector<std::string> dexlayout_args =
615 { "-a", "-i", "-o", "/dev/null", temp_dex.GetFilename() };
Jeff Haod9be7682017-04-05 09:29:43 -0700616 ASSERT_TRUE(DexLayoutExec(&temp_dex,
617 kDexFileDuplicateOffset,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700618 /* profile_file= */ nullptr,
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800619 dexlayout_args));
Mathieu Chartier6b0dbef2017-02-21 12:45:30 -0800620}
621
Jeff Haof914f8e2017-02-21 16:14:23 -0800622TEST_F(DexLayoutTest, NullSetRefListElement) {
Jeff Haod9be7682017-04-05 09:29:43 -0700623 ScratchFile temp_dex;
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800624 std::vector<std::string> dexlayout_args = { "-o", "/dev/null", temp_dex.GetFilename() };
Jeff Haod9be7682017-04-05 09:29:43 -0700625 ASSERT_TRUE(DexLayoutExec(&temp_dex,
626 kNullSetRefListElementInputDex,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700627 /* profile_file= */ nullptr,
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800628 dexlayout_args));
Jeff Haof914f8e2017-02-21 16:14:23 -0800629}
630
Jeff Haoe17f5892017-02-23 16:14:04 -0800631TEST_F(DexLayoutTest, MultiClassData) {
Jeff Haod9be7682017-04-05 09:29:43 -0700632 ScratchFile temp_dex;
633 ScratchFile temp_profile;
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800634 std::vector<std::string> dexlayout_args =
635 { "-p", temp_profile.GetFilename(), "-o", "/dev/null", temp_dex.GetFilename() };
Jeff Haod9be7682017-04-05 09:29:43 -0700636 ASSERT_TRUE(DexLayoutExec(&temp_dex,
637 kMultiClassDataInputDex,
638 &temp_profile,
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800639 dexlayout_args));
Jeff Haoe17f5892017-02-23 16:14:04 -0800640}
641
642TEST_F(DexLayoutTest, UnalignedCodeInfo) {
Jeff Haod9be7682017-04-05 09:29:43 -0700643 ScratchFile temp_dex;
644 ScratchFile temp_profile;
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800645 std::vector<std::string> dexlayout_args =
646 { "-p", temp_profile.GetFilename(), "-o", "/dev/null", temp_dex.GetFilename() };
Jeff Haod9be7682017-04-05 09:29:43 -0700647 ASSERT_TRUE(DexLayoutExec(&temp_dex,
648 kUnalignedCodeInfoInputDex,
649 &temp_profile,
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800650 dexlayout_args));
Jeff Hao44652a32017-02-22 14:20:41 -0800651}
652
Jeff Hao32a60ab2017-03-01 16:44:50 -0800653TEST_F(DexLayoutTest, ClassDataBeforeCode) {
Jeff Haod9be7682017-04-05 09:29:43 -0700654 ScratchFile temp_dex;
655 ScratchFile temp_profile;
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800656 std::vector<std::string> dexlayout_args =
657 { "-p", temp_profile.GetFilename(), "-o", "/dev/null", temp_dex.GetFilename() };
Jeff Haod9be7682017-04-05 09:29:43 -0700658 ASSERT_TRUE(DexLayoutExec(&temp_dex,
659 kClassDataBeforeCodeInputDex,
660 &temp_profile,
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800661 dexlayout_args));
Jeff Haod9be7682017-04-05 09:29:43 -0700662}
663
664TEST_F(DexLayoutTest, UnknownTypeDebugInfo) {
665 ScratchFile temp_dex;
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800666 std::vector<std::string> dexlayout_args = { "-o", "/dev/null", temp_dex.GetFilename() };
Jeff Haod9be7682017-04-05 09:29:43 -0700667 ASSERT_TRUE(DexLayoutExec(&temp_dex,
668 kUnknownTypeDebugInfoInputDex,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700669 /* profile_file= */ nullptr,
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800670 dexlayout_args));
Jeff Hao32a60ab2017-03-01 16:44:50 -0800671}
672
Jeff Haod212d5b2017-04-26 12:09:06 -0700673TEST_F(DexLayoutTest, DuplicateCodeItem) {
674 ScratchFile temp_dex;
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800675 std::vector<std::string> dexlayout_args = { "-o", "/dev/null", temp_dex.GetFilename() };
Jeff Haod212d5b2017-04-26 12:09:06 -0700676 ASSERT_TRUE(DexLayoutExec(&temp_dex,
677 kDuplicateCodeItemInputDex,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700678 /* profile_file= */ nullptr,
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800679 dexlayout_args));
Jeff Haod212d5b2017-04-26 12:09:06 -0700680}
681
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700682// Test that instructions that go past the end of the code items don't cause crashes.
683TEST_F(DexLayoutTest, CodeItemOverrun) {
684 ScratchFile temp_dex;
685 MutateDexFile(temp_dex.GetFile(), GetTestDexFileName("ManyMethods"), [] (DexFile* dex) {
686 bool mutated_successfully = false;
687 // Change the dex instructions to make an opcode that spans past the end of the code item.
Mathieu Chartiera34812c2018-08-01 17:10:43 -0700688 for (ClassAccessor accessor : dex->GetClasses()) {
689 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
690 DexFile::CodeItem* item = const_cast<DexFile::CodeItem*>(method.GetCodeItem());
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700691 if (item != nullptr) {
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800692 CodeItemInstructionAccessor instructions(*dex, item);
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700693 if (instructions.begin() != instructions.end()) {
694 DexInstructionIterator last_instruction = instructions.begin();
695 for (auto dex_it = instructions.begin(); dex_it != instructions.end(); ++dex_it) {
696 last_instruction = dex_it;
697 }
698 if (last_instruction->SizeInCodeUnits() == 1) {
699 // Set the opcode to something that will go past the end of the code item.
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700700 const_cast<Instruction&>(last_instruction.Inst()).SetOpcode(
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700701 Instruction::CONST_STRING_JUMBO);
702 mutated_successfully = true;
Mathieu Chartier176190c2017-10-31 09:58:07 -0700703 // Test that the safe iterator doesn't go past the end.
704 SafeDexInstructionIterator it2(instructions.begin(), instructions.end());
705 while (!it2.IsErrorState()) {
706 ++it2;
707 }
708 EXPECT_TRUE(it2 == last_instruction);
709 EXPECT_TRUE(it2 < instructions.end());
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700710 }
711 }
712 }
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700713 }
714 }
715 CHECK(mutated_successfully)
716 << "Failed to find candidate code item with only one code unit in last instruction.";
717 });
Mathieu Chartiera27af082018-02-03 16:12:23 -0800718
719 std::string error_msg;
720
721 ScratchFile tmp_file;
722 const std::string& tmp_name = tmp_file.GetFilename();
723 size_t tmp_last_slash = tmp_name.rfind('/');
724 std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
725 ScratchFile profile_file;
726
727 std::vector<std::string> dexlayout_args =
728 { "-i",
729 "-v",
730 "-w", tmp_dir,
731 "-o", tmp_name,
732 "-p", profile_file.GetFilename(),
733 temp_dex.GetFilename()
734 };
735 // -v makes sure that the layout did not corrupt the dex file.
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700736 ASSERT_TRUE(DexLayoutExec(&temp_dex,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700737 /*dex_filename=*/ nullptr,
Mathieu Chartiera27af082018-02-03 16:12:23 -0800738 &profile_file,
Andreas Gampe693bfbf2017-11-10 12:23:31 -0800739 dexlayout_args));
Orion Hodson98273672018-02-06 11:50:27 +0000740 ASSERT_TRUE(UnlinkFile(temp_dex.GetFilename() + ".new"));
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700741}
742
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -0800743// Test that link data is written out (or at least the header is updated).
744TEST_F(DexLayoutTest, LinkData) {
Mathieu Chartierfe9185a2017-11-21 19:01:10 -0800745 TEST_DISABLED_FOR_TARGET();
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -0800746 ScratchFile temp_dex;
747 size_t file_size = 0;
748 MutateDexFile(temp_dex.GetFile(), GetTestDexFileName("ManyMethods"), [&] (DexFile* dex) {
749 DexFile::Header& header = const_cast<DexFile::Header&>(dex->GetHeader());
750 header.link_off_ = header.file_size_;
751 header.link_size_ = 16 * KB;
752 header.file_size_ += header.link_size_;
753 file_size = header.file_size_;
754 });
755 TEMP_FAILURE_RETRY(temp_dex.GetFile()->SetLength(file_size));
756
757 std::string error_msg;
758
759 ScratchFile tmp_file;
760 const std::string& tmp_name = tmp_file.GetFilename();
761 size_t tmp_last_slash = tmp_name.rfind('/');
762 std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
763 ScratchFile profile_file;
764
765 std::vector<std::string> dexlayout_args =
766 { "-i",
767 "-v",
768 "-w", tmp_dir,
769 "-o", tmp_name,
770 "-p", profile_file.GetFilename(),
771 temp_dex.GetFilename()
772 };
773 // -v makes sure that the layout did not corrupt the dex file.
774 ASSERT_TRUE(DexLayoutExec(&temp_dex,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700775 /*dex_filename=*/ nullptr,
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -0800776 &profile_file,
777 dexlayout_args));
Orion Hodson98273672018-02-06 11:50:27 +0000778 ASSERT_TRUE(UnlinkFile(temp_dex.GetFilename() + ".new"));
Mathieu Chartier2f36d2f2017-11-20 15:45:25 -0800779}
780
Mathieu Chartier75175552018-01-25 11:23:01 -0800781TEST_F(DexLayoutTest, ClassFilter) {
782 std::vector<std::unique_ptr<const DexFile>> dex_files;
783 std::string error_msg;
784 const ArtDexFileLoader dex_file_loader;
785 const std::string input_jar = GetTestDexFileName("ManyMethods");
786 CHECK(dex_file_loader.Open(input_jar.c_str(),
787 input_jar.c_str(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700788 /*verify=*/ true,
789 /*verify_checksum=*/ true,
Mathieu Chartier75175552018-01-25 11:23:01 -0800790 &error_msg,
791 &dex_files)) << error_msg;
792 ASSERT_EQ(dex_files.size(), 1u);
793 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
794 EXPECT_GT(dex_file->NumClassDefs(), 1u);
795 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
796 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
797 LOG(INFO) << dex_file->GetClassDescriptor(class_def);
798 }
799 Options options;
800 // Filter out all the classes other than the one below based on class descriptor.
801 options.class_filter_.insert("LManyMethods$Strings;");
802 DexLayout dexlayout(options,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700803 /*info=*/ nullptr,
804 /*out_file=*/ nullptr,
805 /*header=*/ nullptr);
Mathieu Chartier75175552018-01-25 11:23:01 -0800806 std::unique_ptr<DexContainer> out;
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800807 bool result = dexlayout.ProcessDexFile(
808 dex_file->GetLocation().c_str(),
809 dex_file.get(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700810 /*dex_file_index=*/ 0,
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800811 &out,
812 &error_msg);
813 ASSERT_TRUE(result) << "Failed to run dexlayout " << error_msg;
Mathieu Chartier75175552018-01-25 11:23:01 -0800814 std::unique_ptr<const DexFile> output_dex_file(
815 dex_file_loader.OpenWithDataSection(
816 out->GetMainSection()->Begin(),
817 out->GetMainSection()->Size(),
818 out->GetDataSection()->Begin(),
819 out->GetDataSection()->Size(),
820 dex_file->GetLocation().c_str(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700821 /* location_checksum= */ 0,
822 /*oat_dex_file=*/ nullptr,
823 /* verify= */ true,
824 /*verify_checksum=*/ false,
Mathieu Chartier75175552018-01-25 11:23:01 -0800825 &error_msg));
826 ASSERT_TRUE(output_dex_file != nullptr);
827
Mathieu Chartier807b21b2018-01-29 05:18:24 -0800828 ASSERT_EQ(output_dex_file->NumClassDefs(), options.class_filter_.size());
Mathieu Chartier75175552018-01-25 11:23:01 -0800829 for (uint32_t i = 0; i < output_dex_file->NumClassDefs(); ++i) {
Mathieu Chartier807b21b2018-01-29 05:18:24 -0800830 // Check that every class in the output dex file is in the filter.
Mathieu Chartier75175552018-01-25 11:23:01 -0800831 const DexFile::ClassDef& class_def = output_dex_file->GetClassDef(i);
832 ASSERT_TRUE(options.class_filter_.find(output_dex_file->GetClassDescriptor(class_def)) !=
833 options.class_filter_.end());
834 }
835 }
836}
837
Jeff Haoc3acfc52016-08-29 14:18:26 -0700838} // namespace art