1 /*
2  * Copyright (C) 2011 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 "dex_file.h"
18 
19 #include <memory>
20 
21 #include "base64_test_util.h"
22 #include "code_item_accessors-inl.h"
23 #include "descriptors_names.h"
24 #include "dex_file-inl.h"
25 #include "dex_file_loader.h"
26 #include "gtest/gtest.h"
27 
28 namespace art {
29 
30 class DexFileLoaderTest : public testing::Test {};
31 
32 static constexpr char kLocationString[] = "/a/dex/file/location";
33 
DecodeBase64Vec(const char * src)34 static inline std::vector<uint8_t> DecodeBase64Vec(const char* src) {
35   std::vector<uint8_t> res;
36   size_t size;
37   std::unique_ptr<uint8_t[]> data(DecodeBase64(src, &size));
38   res.resize(size);
39   memcpy(res.data(), data.get(), size);
40   return res;
41 }
42 
43 // Although this is the same content logically as the Nested test dex,
44 // the DexFileHeader test is sensitive to subtle changes in the
45 // contents due to the checksum etc, so we embed the exact input here.
46 //
47 // class Nested {
48 //     class Inner {
49 //     }
50 // }
51 static const char kRawDex[] =
52   "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP"
53   "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B"
54   "AAC2AQAAvQEAAM0BAADXAQAA+wEAABsCAAA+AgAAUgIAAF8CAABiAgAAZgIAAHMCAAB5AgAAgQIA"
55   "AAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABgAAAAAAAAAKAAAABgAAAKgBAAAAAAEA"
56   "DQAAAAAAAQAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAIAAAAiAEAAKsCAAAA"
57   "AAAAAQAAAAAAAAAFAAAAAAAAAAgAAACYAQAAuAIAAAAAAAACAAAAlAIAAJoCAAABAAAAowIAAAIA"
58   "AgABAAAAiAIAAAYAAABbAQAAcBACAAAADgABAAEAAQAAAI4CAAAEAAAAcBACAAAADgBAAQAAAAAA"
59   "AAAAAAAAAAAATAEAAAAAAAAAAAAAAAAAAAEAAAABAAY8aW5pdD4ABUlubmVyAA5MTmVzdGVkJElu"
60   "bmVyOwAITE5lc3RlZDsAIkxkYWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2"
61   "aWsvYW5ub3RhdGlvbi9Jbm5lckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNz"
62   "ZXM7ABJMamF2YS9sYW5nL09iamVjdDsAC05lc3RlZC5qYXZhAAFWAAJWTAALYWNjZXNzRmxhZ3MA"
63   "BG5hbWUABnRoaXMkMAAFdmFsdWUAAgEABw4AAQAHDjwAAgIBDhgBAgMCCwQADBcBAgQBDhwBGAAA"
64   "AQEAAJAgAICABNQCAAABAAGAgATwAgAAEAAAAAAAAAABAAAAAAAAAAEAAAAPAAAAcAAAAAIAAAAH"
65   "AAAArAAAAAMAAAACAAAAyAAAAAQAAAABAAAA4AAAAAUAAAADAAAA6AAAAAYAAAACAAAAAAEAAAMQ"
66   "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA"
67   "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA==";
68 
69 // kRawDex{38,39,40,41} are dex'ed versions of the following Java source :
70 //
71 // public class Main {
72 //     public static void main(String[] foo) {
73 //     }
74 // }
75 //
76 // The dex file was manually edited to change its dex version code to 38
77 // or 39, respectively.
78 static const char kRawDex38[] =
79   "ZGV4CjAzOAC4OovJlJ1089ikzK6asMf/f8qp3Kve5VsgAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAI"
80   "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAwAQAA8AAAACIB"
81   "AAAqAQAAMgEAAEYBAABRAQAAVAEAAFgBAABtAQAAAQAAAAIAAAAEAAAABgAAAAQAAAACAAAAAAAA"
82   "AAUAAAACAAAAHAEAAAAAAAAAAAAAAAABAAcAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAADAAAA"
83   "AAAAAH4BAAAAAAAAAQABAAEAAABzAQAABAAAAHAQAgAAAA4AAQABAAAAAAB4AQAAAQAAAA4AAAAB"
84   "AAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJW"
85   "TAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEbWFpbgABAAcOAAMBAAcOAAAAAgAAgYAE8AEBCYgCDAAA"
86   "AAAAAAABAAAAAAAAAAEAAAAIAAAAcAAAAAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAA"
87   "uAAAAAYAAAABAAAA0AAAAAEgAAACAAAA8AAAAAEQAAABAAAAHAEAAAIgAAAIAAAAIgEAAAMgAAAC"
88   "AAAAcwEAAAAgAAABAAAAfgEAAAAQAAABAAAAjAEAAA==";
89 
90 static const char kRawDex39[] =
91   "ZGV4CjAzOQC4OovJlJ1089ikzK6asMf/f8qp3Kve5VsgAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAI"
92   "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAwAQAA8AAAACIB"
93   "AAAqAQAAMgEAAEYBAABRAQAAVAEAAFgBAABtAQAAAQAAAAIAAAAEAAAABgAAAAQAAAACAAAAAAAA"
94   "AAUAAAACAAAAHAEAAAAAAAAAAAAAAAABAAcAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAADAAAA"
95   "AAAAAH4BAAAAAAAAAQABAAEAAABzAQAABAAAAHAQAgAAAA4AAQABAAAAAAB4AQAAAQAAAA4AAAAB"
96   "AAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJW"
97   "TAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEbWFpbgABAAcOAAMBAAcOAAAAAgAAgYAE8AEBCYgCDAAA"
98   "AAAAAAABAAAAAAAAAAEAAAAIAAAAcAAAAAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAA"
99   "uAAAAAYAAAABAAAA0AAAAAEgAAACAAAA8AAAAAEQAAABAAAAHAEAAAIgAAAIAAAAIgEAAAMgAAAC"
100   "AAAAcwEAAAAgAAABAAAAfgEAAAAQAAABAAAAjAEAAA==";
101 
102 static const char kRawDex40[] =
103   "ZGV4CjA0MAC4OovJlJ1089ikzK6asMf/f8qp3Kve5VsgAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAI"
104   "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAwAQAA8AAAACIB"
105   "AAAqAQAAMgEAAEYBAABRAQAAVAEAAFgBAABtAQAAAQAAAAIAAAAEAAAABgAAAAQAAAACAAAAAAAA"
106   "AAUAAAACAAAAHAEAAAAAAAAAAAAAAAABAAcAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAADAAAA"
107   "AAAAAH4BAAAAAAAAAQABAAEAAABzAQAABAAAAHAQAgAAAA4AAQABAAAAAAB4AQAAAQAAAA4AAAAB"
108   "AAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJW"
109   "TAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEbWFpbgABAAcOAAMBAAcOAAAAAgAAgYAE8AEBCYgCDAAA"
110   "AAAAAAABAAAAAAAAAAEAAAAIAAAAcAAAAAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAA"
111   "uAAAAAYAAAABAAAA0AAAAAEgAAACAAAA8AAAAAEQAAABAAAAHAEAAAIgAAAIAAAAIgEAAAMgAAAC"
112   "AAAAcwEAAAAgAAABAAAAfgEAAAAQAAABAAAAjAEAAA==";
113 
114 // Taken from 001-Main.
115 static const char kRawDex41[] =
116   "ZGV4CjA0MQBBaEGw/8clTiOn3IafJ++m20gViy5Peh7UAgAAeAAAAHhWNBIAAAAAAAAAAEACAAAK"
117   "AAAAeAAAAAQAAACgAAAAAgAAALAAAAAAAAAAAAAAAAMAAADIAAAAAQAAAOAAAAAAAAAAAAAAANQC"
118   "AAAAAAAAOgEAAEIBAABKAQAAXgEAAGkBAABsAQAAcAEAAIUBAACLAQAAkQEAAAEAAAACAAAABAAA"
119   "AAYAAAAEAAAAAgAAAAAAAAAFAAAAAgAAADQBAAAAAAAAAAAAAAAAAQAIAAAAAQAAAAAAAAAAAAAA"
120   "AQAAAAEAAAAAAAAAAwAAAAAAAAAxAgAAAAAAAAEAAQABAAAAKgEAAAQAAABwEAIAAAAOAAEAAQAA"
121   "AAAALgEAAAEAAAAOABEADgATAQgOAAABAAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcv"
122   "T2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJWTAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEYXJncwAEbWFp"
123   "bgCdAX5+RDh7ImJhY2tlbmQiOiJkZXgiLCJjb21waWxhdGlvbi1tb2RlIjoiZGVidWciLCJoYXMt"
124   "Y2hlY2tzdW1zIjpmYWxzZSwibWluLWFwaSI6MjYsInNoYS0xIjoiNTRjYmIzMTZlNGI3OWFhMDM1"
125   "ZDUwMTM4ZTI3NjY4OGJiOTM5ZGIwNCIsInZlcnNpb24iOiI4LjMuMTQtZGV2In0AAAACAACBgASA"
126   "AgEJmAIADAAAAAAAAAABAAAAAAAAAAEAAAAKAAAAeAAAAAIAAAAEAAAAoAAAAAMAAAACAAAAsAAA"
127   "AAUAAAADAAAAyAAAAAYAAAABAAAA4AAAAAEgAAACAAAAAAEAAAMgAAACAAAAKgEAAAEQAAABAAAA"
128   "NAEAAAIgAAAKAAAAOgEAAAAgAAABAAAAMQIAAAAQAAABAAAAQAIAAA==";
129 
130 // Taken from 001-Main and modified.
131 static const char kRawDex42[] =
132   "ZGV4CjA0MgBBaEGw/8clTiOn3IafJ++m20gViy5Peh7UAgAAeAAAAHhWNBIAAAAAAAAAAEACAAAK"
133   "AAAAeAAAAAQAAACgAAAAAgAAALAAAAAAAAAAAAAAAAMAAADIAAAAAQAAAOAAAAAAAAAAAAAAANQC"
134   "AAAAAAAAOgEAAEIBAABKAQAAXgEAAGkBAABsAQAAcAEAAIUBAACLAQAAkQEAAAEAAAACAAAABAAA"
135   "AAYAAAAEAAAAAgAAAAAAAAAFAAAAAgAAADQBAAAAAAAAAAAAAAAAAQAIAAAAAQAAAAAAAAAAAAAA"
136   "AQAAAAEAAAAAAAAAAwAAAAAAAAAxAgAAAAAAAAEAAQABAAAAKgEAAAQAAABwEAIAAAAOAAEAAQAA"
137   "AAAALgEAAAEAAAAOABEADgATAQgOAAABAAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcv"
138   "T2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJWTAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEYXJncwAEbWFp"
139   "bgCdAX5+RDh7ImJhY2tlbmQiOiJkZXgiLCJjb21waWxhdGlvbi1tb2RlIjoiZGVidWciLCJoYXMt"
140   "Y2hlY2tzdW1zIjpmYWxzZSwibWluLWFwaSI6MjYsInNoYS0xIjoiNTRjYmIzMTZlNGI3OWFhMDM1"
141   "ZDUwMTM4ZTI3NjY4OGJiOTM5ZGIwNCIsInZlcnNpb24iOiI4LjMuMTQtZGV2In0AAAACAACBgASA"
142   "AgEJmAIADAAAAAAAAAABAAAAAAAAAAEAAAAKAAAAeAAAAAIAAAAEAAAAoAAAAAMAAAACAAAAsAAA"
143   "AAUAAAADAAAAyAAAAAYAAAABAAAA4AAAAAEgAAACAAAAAAEAAAMgAAACAAAAKgEAAAEQAAABAAAA"
144   "NAEAAAIgAAAKAAAAOgEAAAAgAAABAAAAMQIAAAAQAAABAAAAQAIAAA==";
145 
146 // Taken from art-gtest-jars-MultiDex.jar
147 static const char kRawContainerDex[] =
148   "ZGV4CjA0MQAAIUzJT/jrhaH3BHocZOpqBIO1QRkfBPE0AgAAeAAAAHhWNBIAAAAAAAAAAJQBAAAV"
149   "AAAArAIAAAgAAAB4AAAABAAAAJgAAAABAAAAyAAAAAYAAADQAAAAAQAAAAABAAAAAAAAAAAAALwF"
150   "AAAAAAAAAgAAAAMAAAAEAAAABQAAAAYAAAAHAAAACwAAAA0AAAABAAAABAAAAAAAAAALAAAABgAA"
151   "AAAAAAAMAAAABgAAAHgBAAAMAAAABgAAAIABAAAFAAIAEQAAAAAAAQAAAAAAAAADABAAAAABAAEA"
152   "AAAAAAEAAAAPAAAAAgACABIAAAADAAEAAAAAAAAAAAAAAAAAAwAAAAAAAAAIAAAAAAAAAIYBAAAA"
153   "AAAAAQABAAEAAABmAQAABAAAAHAQBQAAAA4ABAABAAIAAABqAQAADwAAACIAAQBwEAIAAABiAQAA"
154   "bhADAAAADAJuIAQAIQAOABEADgATAQ8OWgMAFAKWAAAAAAEAAAAEAAAAAQAAAAcAAAACAACAgASg"
155   "AgEJuAINAAAAAAAAAAEAAAAAAAAAAgAAAAgAAAB4AAAAAwAAAAQAAACYAAAABAAAAAEAAADIAAAA"
156   "BQAAAAYAAADQAAAABgAAAAEAAAAAAQAAASAAAAIAAAAgAQAAAyAAAAIAAABmAQAAARAAAAIAAAB4"
157   "AQAAACAAAAEAAACGAQAAABAAAAEAAACUAQAAAQAAABUAAACsAgAAAiAAABUAAACYAwAAZGV4CjA0"
158   "MQAxmn5fJHSijXMoNjKkUwU/LqsrYEld5QiIAwAAeAAAAHhWNBIAAAAAAAAAADQFAAAVAAAArAIA"
159   "AAQAAAAAAwAAAgAAABADAAAAAAAAAAAAAAMAAAAoAwAAAQAAAEADAAAAAAAAAAAAALwFAAA0AgAA"
160   "mAMAAKADAACjAwAAqwMAALUDAADMAwAA4AMAAPQDAAAIBAAAEwQAAB0EAAAqBAAALQQAADEEAABG"
161   "BAAATAQAAFcEAABdBAAAYgQAAGsEAABzBAAAAwAAAAUAAAAGAAAACwAAAAEAAAACAAAAAAAAAAsA"
162   "AAADAAAAAAAAAAAAAQAAAAAAAAAAAA8AAAABAAEAAAAAAAAAAAAAAAAAAQAAAAAAAAAKAAAAAAAA"
163   "ACMFAAAAAAAAAgABAAAAAACQAwAAAwAAABoACQARAAAAAQABAAEAAACUAwAABAAAAHAQAgAAAA4A"
164   "EwAOABEADgAGPGluaXQ+AAFMAAZMTWFpbjsACExTZWNvbmQ7ABVMamF2YS9pby9QcmludFN0cmVh"
165   "bTsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABJMamF2YS9sYW5nL1N5"
166   "c3RlbTsACU1haW4uamF2YQAIT3JpZ2luYWwAC1NlY29uZC5qYXZhAAFWAAJWTAATW0xqYXZhL2xh"
167   "bmcvU3RyaW5nOwAEYXJncwAJZ2V0U2Vjb25kAARtYWluAANvdXQAB3ByaW50bG4ABnNlY29uZACt"
168   "AX5+RDh7ImJhY2tlbmQiOiJkZXgiLCJjb21waWxhdGlvbi1tb2RlIjoiZGVidWciLCJoYXMtY2hl"
169   "Y2tzdW1zIjpmYWxzZSwibWluLWFwaSI6MTksInBsYXRmb3JtIjp0cnVlLCJzaGEtMSI6IjA0ZmZl"
170   "ODMwMGM0MjlmMWFkMTZmM2E1Y2I0ZWQ2OTkyMTNlNGYyY2QiLCJ2ZXJzaW9uIjoiOC4yLjIxLWRl"
171   "diJ9AAAAAQEAgIAE+AYBAeAGAAAACwAAAAAAAAABAAAANAIAAAEAAAAVAAAArAIAAAIAAAAEAAAA"
172   "AAMAAAMAAAACAAAAEAMAAAUAAAADAAAAKAMAAAYAAAABAAAAQAMAAAEgAAACAAAAYAMAAAMgAAAC"
173   "AAAAkAMAAAIgAAAVAAAAmAMAAAAgAAABAAAAIwUAAAAQAAABAAAANAUAAA==";
174 
175 static const char kRawDexZeroLength[] =
176   "UEsDBAoAAAAAAOhxAkkAAAAAAAAAAAAAAAALABwAY2xhc3Nlcy5kZXhVVAkAA2QNoVdnDaFXdXgL"
177   "AAEE5AMBAASIEwAAUEsBAh4DCgAAAAAA6HECSQAAAAAAAAAAAAAAAAsAGAAAAAAAAAAAAKCBAAAA"
178   "AGNsYXNzZXMuZGV4VVQFAANkDaFXdXgLAAEE5AMBAASIEwAAUEsFBgAAAAABAAEAUQAAAEUAAAAA"
179   "AA==";
180 
181 static const char kRawZipClassesDexPresent[] =
182   "UEsDBBQAAAAIANVRN0ms99lIMQEAACACAAALABwAY2xhc3Nlcy5kZXhVVAkAAwFj5VcUY+VXdXgL"
183   "AAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQCEwNDAQMDQ0WY"
184   "iRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGxGxAHAnEIEEcA"
185   "cS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8UFGgP6Fu4IOa"
186   "wczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYxMIX5MAhHIykL"
187   "LinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHCmg0pvBkVIGpA"
188   "Yc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACADVUTdJrPfZSDEBAAAg"
189   "AgAACwAYAAAAAAAAAAAAoIEAAAAAY2xhc3Nlcy5kZXhVVAUAAwFj5Vd1eAsAAQTkAwEABIgTAABQ"
190   "SwUGAAAAAAEAAQBRAAAAdgEAAAAA";
191 
192 static const char kRawZipClassesDexAbsent[] =
193   "UEsDBBQAAAAIANVRN0ms99lIMQEAACACAAAOABwAbm90Y2xhc3Nlcy5kZXhVVAkAAwFj5VcUY+VX"
194   "dXgLAAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQCEwNDAQMD"
195   "Q0WYiRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGxGxAHAnEI"
196   "EEcAcS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8UFGgP6Fu"
197   "4IOawczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYxMIX5MAhH"
198   "IykLLinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHCmg0pvBkV"
199   "IGpAYc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACADVUTdJrPfZSDEB"
200   "AAAgAgAADgAYAAAAAAAAAAAAoIEAAAAAbm90Y2xhc3Nlcy5kZXhVVAUAAwFj5Vd1eAsAAQTkAwEA"
201   "BIgTAABQSwUGAAAAAAEAAQBUAAAAeQEAAAAA";
202 
203 static const char kRawZipThreeDexFiles[] =
204   "UEsDBBQAAAAIAP1WN0ms99lIMQEAACACAAAMABwAY2xhc3NlczIuZGV4VVQJAAOtbOVXrWzlV3V4"
205   "CwABBOQDAQAEiBMAAEtJreAyMLZg2GHVfXLK3JLPN5acWTdrw/H/9adW3ll972m0AhMDQwEDA0NF"
206   "mIkQAxT0MDIwcDBAxFmAeAIQA5UxLGBAAGYg3gHEQKUMF4DYAMj4AKSVgLQWEBsBsRsQBwJxCBBH"
207   "AHEuI0Q9E9RcNijNBDWTFcqWYUSyCKiDHWoPQgTBZ4bSdYwwOUawXDEjxOwCAZCJfFBRoD+hbuCD"
208   "msHMwGaTmZdZYsfA5uObmJlnzSDkk5VYlqifk5iXru+flJWaXGLNwAmS0gOJMzCGMTCF+TAIRyMp"
209   "Cy4pysxLt2ZgyQUqAzmYj4EZTIL909jA8oGRkbODiQfJ/TAaFs6wMJkA9RMsvFmRwpoNKbwZFSBq"
210   "QGHOKAARB4UbkwLETFA8MEPVgMKCQQGiBhxOUPWgeAYAUEsDBBQAAAAIAABXN0ms99lIMQEAACAC"
211   "AAAMABwAY2xhc3NlczMuZGV4VVQJAAOvbOVXr2zlV3V4CwABBOQDAQAEiBMAAEtJreAyMLZg2GHV"
212   "fXLK3JLPN5acWTdrw/H/9adW3ll972m0AhMDQwEDA0NFmIkQAxT0MDIwcDBAxFmAeAIQA5UxLGBA"
213   "AGYg3gHEQKUMF4DYAMj4AKSVgLQWEBsBsRsQBwJxCBBHAHEuI0Q9E9RcNijNBDWTFcqWYUSyCKiD"
214   "HWoPQgTBZ4bSdYwwOUawXDEjxOwCAZCJfFBRoD+hbuCDmsHMwGaTmZdZYsfA5uObmJlnzSDkk5VY"
215   "lqifk5iXru+flJWaXGLNwAmS0gOJMzCGMTCF+TAIRyMpCy4pysxLt2ZgyQUqAzmYj4EZTIL909jA"
216   "8oGRkbODiQfJ/TAaFs6wMJkA9RMsvFmRwpoNKbwZFSBqQGHOKAARB4UbkwLETFA8MEPVgMKCQQGi"
217   "BhxOUPWgeAYAUEsDBBQAAAAIANVRN0ms99lIMQEAACACAAALABwAY2xhc3Nlcy5kZXhVVAkAAwFj"
218   "5VetbOVXdXgLAAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQC"
219   "EwNDAQMDQ0WYiRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGx"
220   "GxAHAnEIEEcAcS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8"
221   "UFGgP6Fu4IOawczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYx"
222   "MIX5MAhHIykLLinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHC"
223   "mg0pvBkVIGpAYc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACAD9VjdJ"
224   "rPfZSDEBAAAgAgAADAAYAAAAAAAAAAAAoIEAAAAAY2xhc3NlczIuZGV4VVQFAAOtbOVXdXgLAAEE"
225   "5AMBAASIEwAAUEsBAh4DFAAAAAgAAFc3Saz32UgxAQAAIAIAAAwAGAAAAAAAAAAAAKCBdwEAAGNs"
226   "YXNzZXMzLmRleFVUBQADr2zlV3V4CwABBOQDAQAEiBMAAFBLAQIeAxQAAAAIANVRN0ms99lIMQEA"
227   "ACACAAALABgAAAAAAAAAAACgge4CAABjbGFzc2VzLmRleFVUBQADAWPlV3V4CwABBOQDAQAEiBMA"
228   "AFBLBQYAAAAAAwADAPUAAABkBAAAAAA=";
229 
230 static const char kRawDexBadMapOffset[] =
231   "ZGV4CjAzNQAZKGSz85r+tXJ1I24FYi+FpQtWbXtelAmoAQAAcAAAAHhWNBIAAAAAAAAAAEAwIBAF"
232   "AAAAcAAAAAMAAACEAAAAAQAAAJAAAAAAAAAAAAAAAAIAAACcAAAAAQAAAKwAAADcAAAAzAAAAOQA"
233   "AADsAAAA9AAAAPkAAAANAQAAAgAAAAMAAAAEAAAABAAAAAIAAAAAAAAAAAAAAAAAAAABAAAAAAAA"
234   "AAAAAAABAAAAAQAAAAAAAAABAAAAAAAAABUBAAAAAAAAAQABAAEAAAAQAQAABAAAAHAQAQAAAA4A"
235   "Bjxpbml0PgAGQS5qYXZhAANMQTsAEkxqYXZhL2xhbmcvT2JqZWN0OwABVgABAAcOAAAAAQAAgYAE"
236   "zAEACwAAAAAAAAABAAAAAAAAAAEAAAAFAAAAcAAAAAIAAAADAAAAhAAAAAMAAAABAAAAkAAAAAUA"
237   "AAACAAAAnAAAAAYAAAABAAAArAAAAAEgAAABAAAAzAAAAAIgAAAFAAAA5AAAAAMgAAABAAAAEAEA"
238   "AAAgAAABAAAAFQEAAAAQAAABAAAAIAEAAA==";
239 
240 static const char kRawDexDebugInfoLocalNullType[] =
241     "ZGV4CjAzNQA+Kwj2g6OZMH88OvK9Ey6ycdIsFCt18ED8AQAAcAAAAHhWNBIAAAAAAAAAAHQBAAAI"
242     "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAMAQAA8AAAABwB"
243     "AAAkAQAALAEAAC8BAAA0AQAASAEAAEsBAABOAQAAAgAAAAMAAAAEAAAABQAAAAIAAAAAAAAAAAAA"
244     "AAUAAAADAAAAAAAAAAEAAQAAAAAAAQAAAAYAAAACAAEAAAAAAAEAAAABAAAAAgAAAAAAAAABAAAA"
245     "AAAAAGMBAAAAAAAAAQABAAEAAABUAQAABAAAAHAQAgAAAA4AAgABAAAAAABZAQAAAgAAABIQDwAG"
246     "PGluaXQ+AAZBLmphdmEAAUkAA0xBOwASTGphdmEvbGFuZy9PYmplY3Q7AAFWAAFhAAR0aGlzAAEA"
247     "Bw4AAwAHDh4DAAcAAAAAAQEAgYAE8AEBAIgCAAAACwAAAAAAAAABAAAAAAAAAAEAAAAIAAAAcAAA"
248     "AAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAAuAAAAAYAAAABAAAA0AAAAAEgAAACAAAA"
249     "8AAAAAIgAAAIAAAAHAEAAAMgAAACAAAAVAEAAAAgAAABAAAAYwEAAAAQAAABAAAAdAEAAA==";
250 
251 // Created from kRawDex38 by changing version to 35 and appending three entries
252 // to the map list, namely `kDexTypeMethodHandleItem`, `kDexTypeCallSiteIdItem`
253 // and `kDexTypeHiddenapiClassData`, each with size one and invalid offset 0xffff.
254 static const char kRawDexBadMapOffsets[] =
255     "ZGV4CjAzNQC4OovJlJ1089ikzK6asMf/f8qp3Kve5VsgAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAI"
256     "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAwAQAA8AAAACIB"
257     "AAAqAQAAMgEAAEYBAABRAQAAVAEAAFgBAABtAQAAAQAAAAIAAAAEAAAABgAAAAQAAAACAAAAAAAA"
258     "AAUAAAACAAAAHAEAAAAAAAAAAAAAAAABAAcAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAADAAAA"
259     "AAAAAH4BAAAAAAAAAQABAAEAAABzAQAABAAAAHAQAgAAAA4AAQABAAAAAAB4AQAAAQAAAA4AAAAB"
260     "AAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJW"
261     "TAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEbWFpbgABAAcOAAMBAAcOAAAAAgAAgYAE8AEBCYgCDwAA"
262     "AAAAAAABAAAAAAAAAAEAAAAIAAAAcAAAAAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAA"
263     "uAAAAAYAAAABAAAA0AAAAAEgAAACAAAA8AAAAAEQAAABAAAAHAEAAAIgAAAIAAAAIgEAAAMgAAAC"
264     "AAAAcwEAAAAgAAABAAAAfgEAAAAQAAABAAAAjAEAAAgAAAABAAAA//8AAAcAAAABAAAA//8AAADw"
265     "AAABAAAA//8AAA==";
266 
267 static const char kRawDexStringDataOOB[] =
268     "ZGV4CjAzNQCeYAY06q0ySzKz8hklA3wUmxR8x10yt8X0AgAAcAAAAHhWNBIAAAAAAAAAAFQCAAAQAAAAcAAAAAcAAACw"
269     "AAAAAwAAAMwAAAABAAAA8AAAAAQAAAD4AAAAAQAAABgBAAC8AQAAOAEAAH4BAACGAQABAAEAlQAAnQC0AQAAyAEAANwB"
270     "AADwAQAA+"
271     "wEAAP4BAAACAgAAFwIAAB0CAAAjAgAAKAIAADECAAACAAAAAwAAAAQAAAAFAAAABgAAAAgAAAAKAAAACAAAAAUAAAAAA"
272     "AAACQAAAAUAAABwAQAACQAAAAUAAAB4AQAABAABAA0AAAAAAAAAAAAAAAAAAgAMAAAAAQABAA4AAAACAAAAAAAAAAAAA"
273     "AABAAAAAgAAAAAAAAAHAAAAAAAAAEMCAAAAAAAAAQABAAEAAAA3AgAABAAAAHAQAwAAAA4AAwABAAIAAAA8AgAACAAAA"
274     "GIAAAAaAQEAbiACABAADgABAAAAAwAAAAEAAAAGAAY8aW5pdD4ADUhlbGxvLCB3b3JsZCEABkxNYWluOwAVTGphdmEva"
275     "W8vUHJpbnRTdHJlYW07ABJMamF2YS9sYW5nL09iamVjdDsAEkxqYXZhL2xhbmcvU3RyaW5nOwASTGphdmEvbGFuZy9Te"
276     "XN0ZW07AAlNYWluLmphdmEAAVYAAlZMABNbTGphK2EvbGFuZy9TdHJpbmc7AARhcmdzAARtYWluAANvdXQAB3ByaW50b"
277     "G4ABHRoaXMAEQAHDgATAQwHDngAAAACAACBgAS4AgEJ0AIAAAANAAAAAAAAAAEAAAAAAAAAAQAAABAAAABwAAAAAgAAA"
278     "AcAAACwAAAAAwAAAAMAAADMAAAABAAAAAEAAADwAAAABQAAAAQAAAD4AAAABgAAAAEAAAAYAQAAASAAAAIAAAA4AQAAA"
279     "RAAAAIAAABwAQAAAiAAABAAAAB+AQAAAyAAAAIAAAA3AgAAACAAAAEAAABDAgAAABAAAAEAAABUAgAA";
280 
281 static const char kRawDexCodeItemOOB[] =
282     "ZGV4CjAzNQBNRhvKLnmGPLR973zkwLwGomvp/qfZL080AgAAcAAAAHhWNBIAAAAA"
283     "AAAAAKABAAAKAAAAcAAAAAQAAACYAAAAAgAAAKgAAAAAAAAAAAAAAAMAAADAAAAA"
284     "AQAAANgAAAA8AQAACAAAACoBAAAxAQAA2gEAAE4BAABZAQAAXAEAAGABAAB1AQAA"
285     "ewEAAIEBAAABAAAAAgAAAAQAAAAGAAAABAAAAAIAAAAAAAAABQAAAAIAAAAkAQAA"
286     "AAAAAAAAAAAAAAEACAAAAAEAAAAAAAAAAAAAAAEAAAABAAAAAAAAAAMAAAAAAAAA"
287     "kgEAAAAAAAABAAEAAQAAAIcBAAKSAAAAcBACAAAADgABAAEAAAAAAIwBAAABAAAA"
288     "DgAAAAEAAAADAAY8aW5pdD4ABkxNYWluOwASTGphdmEvbGFuZy9PYmplY3Q7AAlN"
289     "YWluLmphdmEAAVYAAlZMABNbTGphdmEvbGFuZy9TdHJpbmc7AARhcmdzAARtYWlu"
290     "AAR0aGlzABEABw4A6QH4+PH////9+gAlgAT4AQEJkAIMAAAAAAAAAAEAAAAAAAAA"
291     "AQAAAAoAAABwAAAAAgAAAAQAAACYAAAAAwAAAAIAAACoAAAABQAAAAMAAADAAAAA"
292     "BgAAAAEAAADYAAAAASAAAAIAAAD4AAAAARAAAAEAAAAkAQAAAiAAAAoAAAAqAQAA"
293     "AyAAAAIAAACHAQAAACAAAAEAAACSAQAAABAAAAEAAACgAQAA";
294 
295 static const char kHiddenAPIClassDataBadOffset[] =
296     "ZGV4CjAzNQA+Lt8iLnmGPLR973zkwLwGomvp/qfZL080AgAAcAAAAHhWNBIAAAAA"
297     "AAAAAKABAAAKAAAAcAAAAAQAAACYAAAAAgAAAKgAAAAAAAAAAAAAAAMAAADAAAAA"
298     "AQAAANgAAAA8AfoA+AAAACoBAAAyAQAAOgEAAE4BAABZAQAAXAEAAGABAAB1AQAA"
299     "ewEAAIEBAAABAAAAAgAAAAQAAAAGAAAABAAAAAIAAAAAAAAABQAAAAIAAABHAQBP"
300     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/"
301     "//////8DRwAAAAAAAAAAAAAAAAAIAAAAAAAAAPIAAAAIAAAAAAAAAAAAAAAAAAAA"
302     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
303     "ACAAAAACAAAABAAAAJgAAAADAAAAAgAAAKgAAAAFAAAAAwAAAMAAAAAGAAAAAQAA"
304     "AAR0aGlzABEAAA4AEwEIBw4AAAACAAAlgPsH/gEJkAIMAAAAAAAAAAEAAAAAAAAA"
305     "AQAAAAoAAABwAAAAAgAAAAQAAACYAAAAAwAAAAIAAACoAAAABQAAAAMAAADAAAAA"
306     "BgAAAAEAAADYAAAABwAAAAIAAAD4AAAAAPAAAAAAAQAEAQAAAiAAAAoAAAAqAQAA"
307     "AyAAAC0AAACHAQAAACAAAAEAAACSAQAAABAAAAEAAACgAQAA";
308 
309 static const char kRawBadDebugInfoItem[] =
310     "ZGV4CjAzOAAaShJb6q0xSzOzJXwUA/IZmxR8x10yt8X0AgAAcAAAAHhWNBIwQB8z"
311     "AAAAAFQCAAAQAAAIcAAAAAcAAACwAAAAAwAAAMwAAAABAAAA8AAAAPz/9wD4AAAA"
312     "AQAAOhgBAAC8AQA5AQAAAH4BAACAgAAAAAAAAAAAAAAEAAABAAn///kACAAAAAAA"
313     "BgAAAAgACgIAABcCAAClAAIAIwIAACgCAAAxAgDeAgAAAEAAAAAEAAAABQBQAAYA"
314     "AAAIAAAAAAAAAAAAAAcAAAEACf8Y+QAIAAAAAAAGAAAACAAAAAwAAICAgIAAAAIA"
315     "IwL5ACgCAAAwAgACAQAAAAMAAAAEAAAABQBQAAYAAAAIAAAAAAAAAAADAAEACf//"
316     "+QAIAAAAAAAGAAAACAAKAgAAFwMAAKUAAAMAAAAEAAAABQAAAAYAAAAEAAAAAgAA"
317     "AAAAgIAACQAFEAAAAIAABACAgIAAAAAsCH4AAAAAAAYAAAAIAAAADAAAAAUAAAAC"
318     "AAAAAICABICAgAAAAICAAAUAAAAGAAAACAAAAwAAAAAABAAAAAIAAAAM+f8ABQAA"
319     "AAIAAACAgIAAAAQAgACAAIAALAB+AACAAAAGAAAACAAAAAwAAAAFAACAAgAAAACA"
320     "gIAAAAAAAIAAAAAAAAAAAAAAAAQAAAACAAAAAACAAATmgICAAAAAABMAAAAAAACA"
321     "AAAAAAgAAAAMAAAABQAAAAIAAAAAgIAEgICAAAAAgIAABQAAAAYAAAAIAAADAAAA"
322     "AAAEAAAAAgAAAAwAAAAFAAAAAgANAAAAAAAjAAEAAAAAAAAABwAAABAAAABwAAAA"
323     "AgAAAAcAAACwAAAAAyAAAEEAAADMAAAABAAAAAEnAADwAAAABQAAAAQAA/j4AAAA"
324     "BgAAAFv///8bAQAAAQAAAAAAAG81AQAAARAAAAIAAABwAQAAAiAAABAAAAB+AQAA"
325     "AwAAKQIAAAA3AgAAACAAAAEAAABDAgAAABAAAAEAAABUAgAA";
326 
327 static const char kIncorrectSectionSizeInHeader[] =
328     "ZGV4CjAzOACmfCim6q0xSzKzJXwUA/IZmxR8x10yt8X0AgAAcAAAAHhWNBIwQB8z"
329     "AAAAAFQCAAABAT//cAAAAAcAAACwAAAAAwAAAMwAAAABAACA8AAAAPz/9wD4AAAA"
330     "AQAAOhgBAAC8AzI0AQAAAH4BgAAAAQAAANgAAAApAQAAYQAAACoJWwAxAQAAOgAA"
331     "zAAAAP////8QCQAABAAAADEBAAA6AADMAAAA/////xAJAAAEAAAAAAAAAIAAAAAA"
332     "gAAAAAAAAAEAAADmAAAABAAAAAoAAACAAAAAAIAAAAAAgAAAAICAgAAAAAAAAAAA"
333     "gIAAAAAAgAAAAICAgAAAAAAAAAAAgIAAAAAAAAAAAACAAAAAAHoAAACGgu//Nzk3"
334     "QPUhAEEAAP//dgACAAAADQAAAOYAAAAAAAAAAAAATWFpbm7qYXZhEAD8//cA+AAA"
335     "AAEAADoYAQAAvAMyNAEAAAB+AQAAgIAAAAAAAAAAAICAAAAAAIAAAACAgIAAAAAA"
336     "AAAAAICAAAAAAAAAAAAAgAAAAAB6AAAAhoLv/zc5N0D1IQBBAAD//3YAAgAAAA0A"
337     "AADmAAAAAAAAAAAAAE1haW5u6mF2YRAA/P/3APgAAADYAAAAKQEAAGEAAAAqCVsA"
338     "MQEAADoAAMwAAAD/////EAkAAAQAAAAxAQAAOgAAzAAAAP////8QCQAATWFpbm7q"
339     "YQB2YQFWAAJWGAATizUBAQmQBgwAAAAAAAAAAACHAQAAACAAAAEA+P9t+gAAABAA"
340     "AAEABAAAAICAgACAAAAAAIAABQANAAAAAAAjAAEAAAAAAAAABwAABxAAAABwAAAA"
341     "AgAAAAcAAACwAAAAACAABEEAAADMAAAABAAAAAEnAADwAAAABQAAAwAAAPj4AAAA"
342     "BgAwqDYA//8YAQAAAQAAAAAAAG8zAQAAARAAAAIAAABwAQAAAiAAABAAAAB+AQAA"
343     "AwAAKQIAAAAzAgAABiAAAAEAAABDAgAAABAAAAEAAABUAgAA";
344 
345 static const char kFileSizeTooSmallInHeader[] =
346     "ZGV4CjAzOADm+mgA5vpofOqtMUsBCAAAJAEAAAJ3AAABAAAAcQAA/////////0ES"
347     "+//4mrr////u/wAAAAAAADv//0X/ZAEAAFwBAABgY2Q6JAEAAHsBAACBAQAAAQAA"
348     "AAIAAAAFAAAEAAAAAAAAAA==";
349 
DecodeDexFile(const char * base64,std::vector<uint8_t> * dex_bytes)350 static void DecodeDexFile(const char* base64, std::vector<uint8_t>* dex_bytes) {
351   // decode base64
352   CHECK(base64 != nullptr);
353   *dex_bytes = DecodeBase64Vec(base64);
354   CHECK_NE(dex_bytes->size(), 0u);
355 }
356 
OpenDexFilesBase64(const char * base64,const char * location,std::vector<uint8_t> * dex_bytes,std::vector<std::unique_ptr<const DexFile>> * dex_files,DexFileLoaderErrorCode * error_code,std::string * error_msg)357 static bool OpenDexFilesBase64(const char* base64,
358                                const char* location,
359                                std::vector<uint8_t>* dex_bytes,
360                                std::vector<std::unique_ptr<const DexFile>>* dex_files,
361                                DexFileLoaderErrorCode* error_code,
362                                std::string* error_msg) {
363   DecodeDexFile(base64, dex_bytes);
364 
365   // read dex file(s)
366   static constexpr bool kVerifyChecksum = true;
367   std::vector<std::unique_ptr<const DexFile>> tmp;
368   DexFileLoader dex_file_loader(dex_bytes->data(), dex_bytes->size(), location);
369   bool success =
370       dex_file_loader.Open(/* verify= */ true, kVerifyChecksum, error_code, error_msg, dex_files);
371   return success;
372 }
373 
OpenDexFileBase64(const char * base64,const char * location,std::vector<uint8_t> * dex_bytes,size_t expected_dex_files=1)374 static std::unique_ptr<const DexFile> OpenDexFileBase64(const char* base64,
375                                                         const char* location,
376                                                         std::vector<uint8_t>* dex_bytes,
377                                                         size_t expected_dex_files = 1) {
378   // read dex files.
379   DexFileLoaderErrorCode error_code;
380   std::string error_msg;
381   std::vector<std::unique_ptr<const DexFile>> dex_files;
382   bool success = OpenDexFilesBase64(base64, location, dex_bytes, &dex_files, &error_code,
383                                     &error_msg);
384   CHECK(success) << error_msg;
385   EXPECT_EQ(expected_dex_files, dex_files.size());
386   return std::move(dex_files[0]);
387 }
388 
OpenDexFileInMemoryBase64(const char * base64,const char * location,uint32_t location_checksum,bool expect_success,std::vector<uint8_t> * dex_bytes)389 static std::unique_ptr<const DexFile> OpenDexFileInMemoryBase64(const char* base64,
390                                                                 const char* location,
391                                                                 uint32_t location_checksum,
392                                                                 bool expect_success,
393                                                                 std::vector<uint8_t>* dex_bytes) {
394   DecodeDexFile(base64, dex_bytes);
395 
396   std::string error_message;
397   DexFileLoader dex_file_loader(dex_bytes->data(), dex_bytes->size(), location);
398   std::unique_ptr<const DexFile> dex_file(dex_file_loader.Open(location_checksum,
399                                                                /* oat_dex_file= */ nullptr,
400                                                                /* verify= */ true,
401                                                                /* verify_checksum= */ true,
402                                                                &error_message));
403   if (expect_success) {
404     CHECK(dex_file != nullptr) << error_message;
405   } else {
406     CHECK(dex_file == nullptr) << "Expected dex file open to fail.";
407   }
408   return dex_file;
409 }
410 
ValidateDexFileHeader(std::unique_ptr<const DexFile> dex_file)411 static void ValidateDexFileHeader(std::unique_ptr<const DexFile> dex_file) {
412   static const DexFile::Magic kExpectedDexFileMagic = {
413       0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x35, 0x00,  // "dex\n035\0".
414   };
415   static const DexFile::Sha1 kExpectedSha1 = {
416       0x7b, 0xb8, 0x0c, 0xd4, 0x1f, 0xd6, 0x1e, 0xc5, 0x89, 0xe8,
417       0xbe, 0xe5, 0x18, 0x02, 0x12, 0x18, 0x2e, 0xf2, 0x8c, 0x3d,
418   };
419 
420   const DexFile::Header& header = dex_file->GetHeader();
421   EXPECT_EQ(kExpectedDexFileMagic, header.magic_);
422   EXPECT_EQ(0x00d87910U, header.checksum_);
423   EXPECT_EQ(kExpectedSha1, header.signature_);
424   EXPECT_EQ(904U, header.file_size_);
425   EXPECT_EQ(112U, header.header_size_);
426   EXPECT_EQ(0U, header.link_size_);
427   EXPECT_EQ(0U, header.link_off_);
428   EXPECT_EQ(15U, header.string_ids_size_);
429   EXPECT_EQ(112U, header.string_ids_off_);
430   EXPECT_EQ(7U, header.type_ids_size_);
431   EXPECT_EQ(172U, header.type_ids_off_);
432   EXPECT_EQ(2U, header.proto_ids_size_);
433   EXPECT_EQ(200U, header.proto_ids_off_);
434   EXPECT_EQ(1U, header.field_ids_size_);
435   EXPECT_EQ(224U, header.field_ids_off_);
436   EXPECT_EQ(3U, header.method_ids_size_);
437   EXPECT_EQ(232U, header.method_ids_off_);
438   EXPECT_EQ(2U, header.class_defs_size_);
439   EXPECT_EQ(256U, header.class_defs_off_);
440   EXPECT_EQ(584U, header.data_size_);
441   EXPECT_EQ(320U, header.data_off_);
442 
443   EXPECT_EQ(header.checksum_, dex_file->GetLocationChecksum());
444 }
445 
TEST_F(DexFileLoaderTest,Header)446 TEST_F(DexFileLoaderTest, Header) {
447   std::vector<uint8_t> dex_bytes;
448   std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, kLocationString, &dex_bytes));
449   ValidateDexFileHeader(std::move(raw));
450 }
451 
TEST_F(DexFileLoaderTest,HeaderInMemory)452 TEST_F(DexFileLoaderTest, HeaderInMemory) {
453   std::vector<uint8_t> dex_bytes;
454   std::unique_ptr<const DexFile> raw =
455       OpenDexFileInMemoryBase64(kRawDex, kLocationString, 0x00d87910U, true, &dex_bytes);
456   ValidateDexFileHeader(std::move(raw));
457 }
458 
TEST_F(DexFileLoaderTest,Version38Accepted)459 TEST_F(DexFileLoaderTest, Version38Accepted) {
460   std::vector<uint8_t> dex_bytes;
461   std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex38, kLocationString, &dex_bytes));
462   ASSERT_TRUE(raw.get() != nullptr);
463 
464   const DexFile::Header& header = raw->GetHeader();
465   EXPECT_EQ(38u, header.GetVersion());
466 }
467 
TEST_F(DexFileLoaderTest,Version39Accepted)468 TEST_F(DexFileLoaderTest, Version39Accepted) {
469   std::vector<uint8_t> dex_bytes;
470   std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex39, kLocationString, &dex_bytes));
471   ASSERT_TRUE(raw.get() != nullptr);
472 
473   const DexFile::Header& header = raw->GetHeader();
474   EXPECT_EQ(39u, header.GetVersion());
475 }
476 
TEST_F(DexFileLoaderTest,Version40Accepted)477 TEST_F(DexFileLoaderTest, Version40Accepted) {
478   std::vector<uint8_t> dex_bytes;
479   std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex40, kLocationString, &dex_bytes));
480   ASSERT_TRUE(raw.get() != nullptr);
481 
482   const DexFile::Header& header = raw->GetHeader();
483   EXPECT_EQ(40u, header.GetVersion());
484 }
485 
TEST_F(DexFileLoaderTest,Version41Accepted)486 TEST_F(DexFileLoaderTest, Version41Accepted) {
487   std::vector<uint8_t> dex_bytes;
488   std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex41, kLocationString, &dex_bytes, 1));
489   ASSERT_TRUE(raw.get() != nullptr);
490 
491   const DexFile::Header& header = raw->GetHeader();
492   EXPECT_EQ(41u, header.GetVersion());
493 }
494 
TEST_F(DexFileLoaderTest,Version42Rejected)495 TEST_F(DexFileLoaderTest, Version42Rejected) {
496   std::vector<uint8_t> dex_bytes;
497   DecodeDexFile(kRawDex42, &dex_bytes);
498 
499   static constexpr bool kVerifyChecksum = true;
500   DexFileLoaderErrorCode error_code;
501   std::string error_msg;
502   std::vector<std::unique_ptr<const DexFile>> dex_files;
503   DexFileLoader dex_file_loader(dex_bytes.data(), dex_bytes.size(), kLocationString);
504   ASSERT_FALSE(dex_file_loader.Open(
505       /* verify= */ true, kVerifyChecksum, &error_code, &error_msg, &dex_files));
506 }
507 
TEST_F(DexFileLoaderTest,ContainerDex)508 TEST_F(DexFileLoaderTest, ContainerDex) {
509   std::vector<uint8_t> dex_bytes;
510   std::unique_ptr<const DexFile> raw(
511       OpenDexFileBase64(kRawContainerDex, kLocationString, &dex_bytes, 2));
512   ASSERT_TRUE(raw.get() != nullptr);
513 
514   const DexFile::Header& header = raw->GetHeader();
515   EXPECT_EQ(41u, header.GetVersion());
516 }
517 
TEST_F(DexFileLoaderTest,ZeroLengthDexRejected)518 TEST_F(DexFileLoaderTest, ZeroLengthDexRejected) {
519   std::vector<uint8_t> dex_bytes;
520   DecodeDexFile(kRawDexZeroLength, &dex_bytes);
521 
522   static constexpr bool kVerifyChecksum = true;
523   DexFileLoaderErrorCode error_code;
524   std::string error_msg;
525   std::vector<std::unique_ptr<const DexFile>> dex_files;
526   DexFileLoader dex_file_loader(dex_bytes.data(), dex_bytes.size(), kLocationString);
527   ASSERT_FALSE(dex_file_loader.Open(
528       /* verify= */ true, kVerifyChecksum, &error_code, &error_msg, &dex_files));
529 }
530 
TEST_F(DexFileLoaderTest,GetMultiDexClassesDexName)531 TEST_F(DexFileLoaderTest, GetMultiDexClassesDexName) {
532   ASSERT_EQ("classes.dex", DexFileLoader::GetMultiDexClassesDexName(0));
533   ASSERT_EQ("classes2.dex", DexFileLoader::GetMultiDexClassesDexName(1));
534   ASSERT_EQ("classes3.dex", DexFileLoader::GetMultiDexClassesDexName(2));
535   ASSERT_EQ("classes100.dex", DexFileLoader::GetMultiDexClassesDexName(99));
536 }
537 
TEST_F(DexFileLoaderTest,GetMultiDexLocation)538 TEST_F(DexFileLoaderTest, GetMultiDexLocation) {
539   std::string dex_location_str = "/system/app/framework.jar";
540   const char* dex_location = dex_location_str.c_str();
541   ASSERT_EQ("/system/app/framework.jar", DexFileLoader::GetMultiDexLocation(0, dex_location));
542   ASSERT_EQ("/system/app/framework.jar!classes2.dex",
543             DexFileLoader::GetMultiDexLocation(1, dex_location));
544   ASSERT_EQ("/system/app/framework.jar!classes101.dex",
545             DexFileLoader::GetMultiDexLocation(100, dex_location));
546 }
547 
TEST(DexFileUtilsTest,GetBaseLocationAndMultiDexSuffix)548 TEST(DexFileUtilsTest, GetBaseLocationAndMultiDexSuffix) {
549   EXPECT_EQ("/foo/bar/baz.jar", DexFileLoader::GetBaseLocation("/foo/bar/baz.jar"));
550   EXPECT_EQ("/foo/bar/baz.jar", DexFileLoader::GetBaseLocation("/foo/bar/baz.jar!classes2.dex"));
551   EXPECT_EQ("/foo/bar/baz.jar", DexFileLoader::GetBaseLocation("/foo/bar/baz.jar!classes8.dex"));
552   EXPECT_EQ("", DexFileLoader::GetMultiDexSuffix("/foo/bar/baz.jar"));
553   EXPECT_EQ("!classes2.dex", DexFileLoader::GetMultiDexSuffix("/foo/bar/baz.jar!classes2.dex"));
554   EXPECT_EQ("!classes8.dex", DexFileLoader::GetMultiDexSuffix("/foo/bar/baz.jar!classes8.dex"));
555 }
556 
TEST_F(DexFileLoaderTest,ZipOpenClassesPresent)557 TEST_F(DexFileLoaderTest, ZipOpenClassesPresent) {
558   std::vector<uint8_t> dex_bytes;
559   std::vector<std::unique_ptr<const DexFile>> dex_files;
560   DexFileLoaderErrorCode error_code;
561   std::string error_msg;
562   ASSERT_TRUE(OpenDexFilesBase64(kRawZipClassesDexPresent,
563                                  kLocationString,
564                                  &dex_bytes,
565                                  &dex_files,
566                                  &error_code,
567                                  &error_msg));
568   EXPECT_EQ(dex_files.size(), 1u);
569 }
570 
TEST_F(DexFileLoaderTest,ZipOpenClassesAbsent)571 TEST_F(DexFileLoaderTest, ZipOpenClassesAbsent) {
572   std::vector<uint8_t> dex_bytes;
573   std::vector<std::unique_ptr<const DexFile>> dex_files;
574   DexFileLoaderErrorCode error_code;
575   std::string error_msg;
576   ASSERT_FALSE(OpenDexFilesBase64(kRawZipClassesDexAbsent,
577                                   kLocationString,
578                                   &dex_bytes,
579                                   &dex_files,
580                                   &error_code,
581                                   &error_msg));
582   EXPECT_EQ(error_code, DexFileLoaderErrorCode::kEntryNotFound);
583   EXPECT_EQ(dex_files.size(), 0u);
584 }
585 
TEST_F(DexFileLoaderTest,ZipOpenThreeDexFiles)586 TEST_F(DexFileLoaderTest, ZipOpenThreeDexFiles) {
587   std::vector<uint8_t> dex_bytes;
588   std::vector<std::unique_ptr<const DexFile>> dex_files;
589   DexFileLoaderErrorCode error_code;
590   std::string error_msg;
591   ASSERT_TRUE(OpenDexFilesBase64(kRawZipThreeDexFiles,
592                                  kLocationString,
593                                  &dex_bytes,
594                                  &dex_files,
595                                  &error_code,
596                                  &error_msg));
597   EXPECT_EQ(dex_files.size(), 3u);
598 }
599 
TEST_F(DexFileLoaderTest,OpenDexBadMapOffset)600 TEST_F(DexFileLoaderTest, OpenDexBadMapOffset) {
601   std::vector<uint8_t> dex_bytes;
602   std::unique_ptr<const DexFile> raw =
603       OpenDexFileInMemoryBase64(kRawDexBadMapOffset,
604                                 kLocationString,
605                                 0xb3642819U,
606                                 false,
607                                 &dex_bytes);
608   EXPECT_EQ(raw, nullptr);
609 }
610 
TEST_F(DexFileLoaderTest,OpenDexDebugInfoLocalNullType)611 TEST_F(DexFileLoaderTest, OpenDexDebugInfoLocalNullType) {
612   std::vector<uint8_t> dex_bytes;
613   std::unique_ptr<const DexFile> raw = OpenDexFileInMemoryBase64(kRawDexDebugInfoLocalNullType,
614                                                                  kLocationString,
615                                                                  0xf25f2b38U,
616                                                                  true,
617                                                                  &dex_bytes);
618   const dex::ClassDef& class_def = raw->GetClassDef(0);
619   constexpr uint32_t kMethodIdx = 1;
620   const dex::CodeItem* code_item = raw->GetCodeItem(raw->FindCodeItemOffset(class_def, kMethodIdx));
621   CodeItemDebugInfoAccessor accessor(*raw, code_item, kMethodIdx);
622   ASSERT_TRUE(accessor.DecodeDebugLocalInfo(true, 1, VoidFunctor()));
623 }
624 
625 // Helper for tests that open and verify raw dex files to avoid boilerplate.
OpenAndVerify(const char * dex_file_base64,bool expected_success)626 void OpenAndVerify(const char* dex_file_base64, bool expected_success) {
627   std::vector<uint8_t> dex_bytes;
628   std::vector<std::unique_ptr<const DexFile>> dex_files;
629   DexFileLoaderErrorCode error_code;
630   std::string error_msg;
631   bool success = OpenDexFilesBase64(
632       dex_file_base64, kLocationString, &dex_bytes, &dex_files, &error_code, &error_msg);
633   ASSERT_EQ(success, expected_success);
634 }
635 
636 // Bad offset for `kDexTypeHiddenapiClassData` previously triggered a `DCHECK()`
637 // before verifying the dex file. We want to reject dex files with bad offsets
638 // without crashing, even in debug builds. b/281960267
TEST_F(DexFileLoaderTest,BadMapOffsets)639 TEST_F(DexFileLoaderTest, BadMapOffsets) {
640   OpenAndVerify(kRawDexBadMapOffsets, /*expected_success=*/false);
641 }
642 
643 // Generated dex file with a string data offset out of bounds. It should fail verification without
644 // crashing. b/280066537
TEST_F(DexFileLoaderTest,StringDataOffsetOutOfBounds)645 TEST_F(DexFileLoaderTest, StringDataOffsetOutOfBounds) {
646   OpenAndVerify(kRawDexStringDataOOB, /*expected_success=*/false);
647 }
648 
TEST_F(DexFileLoaderTest,CodeItemOutOfBounds)649 TEST_F(DexFileLoaderTest, CodeItemOutOfBounds) {
650   OpenAndVerify(kRawDexCodeItemOOB, /*expected_success=*/false);
651 }
652 
TEST_F(DexFileLoaderTest,HiddenAPIClassDataBadOffset)653 TEST_F(DexFileLoaderTest, HiddenAPIClassDataBadOffset) {
654   OpenAndVerify(kHiddenAPIClassDataBadOffset, /*expected_success=*/false);
655 }
656 
TEST_F(DexFileLoaderTest,BadDebugInfoItem)657 TEST_F(DexFileLoaderTest, BadDebugInfoItem) {
658   OpenAndVerify(kRawBadDebugInfoItem, /*expected_success=*/false);
659 }
660 
TEST_F(DexFileLoaderTest,IncorrectSectionSizeInHeader)661 TEST_F(DexFileLoaderTest, IncorrectSectionSizeInHeader) {
662   OpenAndVerify(kIncorrectSectionSizeInHeader, /*expected_success=*/false);
663 }
664 
TEST_F(DexFileLoaderTest,FileSizeTooSmallInHeader)665 TEST_F(DexFileLoaderTest, FileSizeTooSmallInHeader) {
666   OpenAndVerify(kFileSizeTooSmallInHeader, /*expected_success=*/false);
667 }
668 
669 }  // namespace art
670