Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "dex_file.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 19 | #include <sys/mman.h> |
| 20 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
| 22 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include "base/stl_util.h" |
| 24 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 25 | #include "common_runtime_test.h" |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 26 | #include "dex_file-inl.h" |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 27 | #include "mem_map.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 28 | #include "os.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 29 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 30 | #include "thread-current-inl.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 31 | #include "utils.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 32 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 33 | namespace art { |
| 34 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 35 | class DexFileTest : public CommonRuntimeTest {}; |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 36 | |
| 37 | TEST_F(DexFileTest, Open) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 38 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 39 | std::unique_ptr<const DexFile> dex(OpenTestDexFile("Nested")); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 40 | ASSERT_TRUE(dex.get() != nullptr); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 41 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 42 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 43 | static inline std::vector<uint8_t> DecodeBase64Vec(const char* src) { |
| 44 | std::vector<uint8_t> res; |
| 45 | size_t size; |
| 46 | std::unique_ptr<uint8_t[]> data(DecodeBase64(src, &size)); |
| 47 | res.resize(size); |
| 48 | memcpy(res.data(), data.get(), size); |
| 49 | return res; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 52 | // Although this is the same content logically as the Nested test dex, |
| 53 | // the DexFileHeader test is sensitive to subtle changes in the |
| 54 | // contents due to the checksum etc, so we embed the exact input here. |
| 55 | // |
| 56 | // class Nested { |
| 57 | // class Inner { |
| 58 | // } |
| 59 | // } |
| 60 | static const char kRawDex[] = |
| 61 | "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP" |
| 62 | "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B" |
| 63 | "AAC2AQAAvQEAAM0BAADXAQAA+wEAABsCAAA+AgAAUgIAAF8CAABiAgAAZgIAAHMCAAB5AgAAgQIA" |
| 64 | "AAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABgAAAAAAAAAKAAAABgAAAKgBAAAAAAEA" |
| 65 | "DQAAAAAAAQAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAIAAAAiAEAAKsCAAAA" |
| 66 | "AAAAAQAAAAAAAAAFAAAAAAAAAAgAAACYAQAAuAIAAAAAAAACAAAAlAIAAJoCAAABAAAAowIAAAIA" |
| 67 | "AgABAAAAiAIAAAYAAABbAQAAcBACAAAADgABAAEAAQAAAI4CAAAEAAAAcBACAAAADgBAAQAAAAAA" |
| 68 | "AAAAAAAAAAAATAEAAAAAAAAAAAAAAAAAAAEAAAABAAY8aW5pdD4ABUlubmVyAA5MTmVzdGVkJElu" |
| 69 | "bmVyOwAITE5lc3RlZDsAIkxkYWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2" |
| 70 | "aWsvYW5ub3RhdGlvbi9Jbm5lckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNz" |
| 71 | "ZXM7ABJMamF2YS9sYW5nL09iamVjdDsAC05lc3RlZC5qYXZhAAFWAAJWTAALYWNjZXNzRmxhZ3MA" |
| 72 | "BG5hbWUABnRoaXMkMAAFdmFsdWUAAgEABw4AAQAHDjwAAgIBDhgBAgMCCwQADBcBAgQBDhwBGAAA" |
| 73 | "AQEAAJAgAICABNQCAAABAAGAgATwAgAAEAAAAAAAAAABAAAAAAAAAAEAAAAPAAAAcAAAAAIAAAAH" |
| 74 | "AAAArAAAAAMAAAACAAAAyAAAAAQAAAABAAAA4AAAAAUAAAADAAAA6AAAAAYAAAACAAAAAAEAAAMQ" |
| 75 | "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA" |
| 76 | "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA=="; |
| 77 | |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 78 | // kRawDex38 and 39 are dex'ed versions of the following Java source : |
| 79 | // |
| 80 | // public class Main { |
| 81 | // public static void main(String[] foo) { |
| 82 | // } |
| 83 | // } |
| 84 | // |
| 85 | // The dex file was manually edited to change its dex version code to 38 |
| 86 | // or 39, respectively. |
| 87 | static const char kRawDex38[] = |
| 88 | "ZGV4CjAzOAC4OovJlJ1089ikzK6asMf/f8qp3Kve5VsgAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAI" |
| 89 | "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAwAQAA8AAAACIB" |
| 90 | "AAAqAQAAMgEAAEYBAABRAQAAVAEAAFgBAABtAQAAAQAAAAIAAAAEAAAABgAAAAQAAAACAAAAAAAA" |
| 91 | "AAUAAAACAAAAHAEAAAAAAAAAAAAAAAABAAcAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAADAAAA" |
| 92 | "AAAAAH4BAAAAAAAAAQABAAEAAABzAQAABAAAAHAQAgAAAA4AAQABAAAAAAB4AQAAAQAAAA4AAAAB" |
| 93 | "AAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJW" |
| 94 | "TAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEbWFpbgABAAcOAAMBAAcOAAAAAgAAgYAE8AEBCYgCDAAA" |
| 95 | "AAAAAAABAAAAAAAAAAEAAAAIAAAAcAAAAAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAA" |
| 96 | "uAAAAAYAAAABAAAA0AAAAAEgAAACAAAA8AAAAAEQAAABAAAAHAEAAAIgAAAIAAAAIgEAAAMgAAAC" |
| 97 | "AAAAcwEAAAAgAAABAAAAfgEAAAAQAAABAAAAjAEAAA=="; |
| 98 | |
| 99 | static const char kRawDex39[] = |
| 100 | "ZGV4CjAzOQC4OovJlJ1089ikzK6asMf/f8qp3Kve5VsgAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAI" |
| 101 | "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAwAQAA8AAAACIB" |
| 102 | "AAAqAQAAMgEAAEYBAABRAQAAVAEAAFgBAABtAQAAAQAAAAIAAAAEAAAABgAAAAQAAAACAAAAAAAA" |
| 103 | "AAUAAAACAAAAHAEAAAAAAAAAAAAAAAABAAcAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAADAAAA" |
| 104 | "AAAAAH4BAAAAAAAAAQABAAEAAABzAQAABAAAAHAQAgAAAA4AAQABAAAAAAB4AQAAAQAAAA4AAAAB" |
| 105 | "AAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJW" |
| 106 | "TAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEbWFpbgABAAcOAAMBAAcOAAAAAgAAgYAE8AEBCYgCDAAA" |
| 107 | "AAAAAAABAAAAAAAAAAEAAAAIAAAAcAAAAAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAA" |
| 108 | "uAAAAAYAAAABAAAA0AAAAAEgAAACAAAA8AAAAAEQAAABAAAAHAEAAAIgAAAIAAAAIgEAAAMgAAAC" |
| 109 | "AAAAcwEAAAAgAAABAAAAfgEAAAAQAAABAAAAjAEAAA=="; |
| 110 | |
ganxiaolin | cd16d0a | 2016-07-18 11:21:44 +0800 | [diff] [blame] | 111 | static const char kRawDexZeroLength[] = |
| 112 | "UEsDBAoAAAAAAOhxAkkAAAAAAAAAAAAAAAALABwAY2xhc3Nlcy5kZXhVVAkAA2QNoVdnDaFXdXgL" |
| 113 | "AAEE5AMBAASIEwAAUEsBAh4DCgAAAAAA6HECSQAAAAAAAAAAAAAAAAsAGAAAAAAAAAAAAKCBAAAA" |
| 114 | "AGNsYXNzZXMuZGV4VVQFAANkDaFXdXgLAAEE5AMBAASIEwAAUEsFBgAAAAABAAEAUQAAAEUAAAAA" |
| 115 | "AA=="; |
| 116 | |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 117 | static const char kRawZipClassesDexPresent[] = |
| 118 | "UEsDBBQAAAAIANVRN0ms99lIMQEAACACAAALABwAY2xhc3Nlcy5kZXhVVAkAAwFj5VcUY+VXdXgL" |
| 119 | "AAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQCEwNDAQMDQ0WY" |
| 120 | "iRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGxGxAHAnEIEEcA" |
| 121 | "cS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8UFGgP6Fu4IOa" |
| 122 | "wczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYxMIX5MAhHIykL" |
| 123 | "LinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHCmg0pvBkVIGpA" |
| 124 | "Yc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACADVUTdJrPfZSDEBAAAg" |
| 125 | "AgAACwAYAAAAAAAAAAAAoIEAAAAAY2xhc3Nlcy5kZXhVVAUAAwFj5Vd1eAsAAQTkAwEABIgTAABQ" |
| 126 | "SwUGAAAAAAEAAQBRAAAAdgEAAAAA"; |
| 127 | |
| 128 | static const char kRawZipClassesDexAbsent[] = |
| 129 | "UEsDBBQAAAAIANVRN0ms99lIMQEAACACAAAOABwAbm90Y2xhc3Nlcy5kZXhVVAkAAwFj5VcUY+VX" |
| 130 | "dXgLAAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQCEwNDAQMD" |
| 131 | "Q0WYiRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGxGxAHAnEI" |
| 132 | "EEcAcS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8UFGgP6Fu" |
| 133 | "4IOawczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYxMIX5MAhH" |
| 134 | "IykLLinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHCmg0pvBkV" |
| 135 | "IGpAYc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACADVUTdJrPfZSDEB" |
| 136 | "AAAgAgAADgAYAAAAAAAAAAAAoIEAAAAAbm90Y2xhc3Nlcy5kZXhVVAUAAwFj5Vd1eAsAAQTkAwEA" |
| 137 | "BIgTAABQSwUGAAAAAAEAAQBUAAAAeQEAAAAA"; |
| 138 | |
| 139 | static const char kRawZipThreeDexFiles[] = |
| 140 | "UEsDBBQAAAAIAP1WN0ms99lIMQEAACACAAAMABwAY2xhc3NlczIuZGV4VVQJAAOtbOVXrWzlV3V4" |
| 141 | "CwABBOQDAQAEiBMAAEtJreAyMLZg2GHVfXLK3JLPN5acWTdrw/H/9adW3ll972m0AhMDQwEDA0NF" |
| 142 | "mIkQAxT0MDIwcDBAxFmAeAIQA5UxLGBAAGYg3gHEQKUMF4DYAMj4AKSVgLQWEBsBsRsQBwJxCBBH" |
| 143 | "AHEuI0Q9E9RcNijNBDWTFcqWYUSyCKiDHWoPQgTBZ4bSdYwwOUawXDEjxOwCAZCJfFBRoD+hbuCD" |
| 144 | "msHMwGaTmZdZYsfA5uObmJlnzSDkk5VYlqifk5iXru+flJWaXGLNwAmS0gOJMzCGMTCF+TAIRyMp" |
| 145 | "Cy4pysxLt2ZgyQUqAzmYj4EZTIL909jA8oGRkbODiQfJ/TAaFs6wMJkA9RMsvFmRwpoNKbwZFSBq" |
| 146 | "QGHOKAARB4UbkwLETFA8MEPVgMKCQQGiBhxOUPWgeAYAUEsDBBQAAAAIAABXN0ms99lIMQEAACAC" |
| 147 | "AAAMABwAY2xhc3NlczMuZGV4VVQJAAOvbOVXr2zlV3V4CwABBOQDAQAEiBMAAEtJreAyMLZg2GHV" |
| 148 | "fXLK3JLPN5acWTdrw/H/9adW3ll972m0AhMDQwEDA0NFmIkQAxT0MDIwcDBAxFmAeAIQA5UxLGBA" |
| 149 | "AGYg3gHEQKUMF4DYAMj4AKSVgLQWEBsBsRsQBwJxCBBHAHEuI0Q9E9RcNijNBDWTFcqWYUSyCKiD" |
| 150 | "HWoPQgTBZ4bSdYwwOUawXDEjxOwCAZCJfFBRoD+hbuCDmsHMwGaTmZdZYsfA5uObmJlnzSDkk5VY" |
| 151 | "lqifk5iXru+flJWaXGLNwAmS0gOJMzCGMTCF+TAIRyMpCy4pysxLt2ZgyQUqAzmYj4EZTIL909jA" |
| 152 | "8oGRkbODiQfJ/TAaFs6wMJkA9RMsvFmRwpoNKbwZFSBqQGHOKAARB4UbkwLETFA8MEPVgMKCQQGi" |
| 153 | "BhxOUPWgeAYAUEsDBBQAAAAIANVRN0ms99lIMQEAACACAAALABwAY2xhc3Nlcy5kZXhVVAkAAwFj" |
| 154 | "5VetbOVXdXgLAAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQC" |
| 155 | "EwNDAQMDQ0WYiRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGx" |
| 156 | "GxAHAnEIEEcAcS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8" |
| 157 | "UFGgP6Fu4IOawczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYx" |
| 158 | "MIX5MAhHIykLLinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHC" |
| 159 | "mg0pvBkVIGpAYc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACAD9VjdJ" |
| 160 | "rPfZSDEBAAAgAgAADAAYAAAAAAAAAAAAoIEAAAAAY2xhc3NlczIuZGV4VVQFAAOtbOVXdXgLAAEE" |
| 161 | "5AMBAASIEwAAUEsBAh4DFAAAAAgAAFc3Saz32UgxAQAAIAIAAAwAGAAAAAAAAAAAAKCBdwEAAGNs" |
| 162 | "YXNzZXMzLmRleFVUBQADr2zlV3V4CwABBOQDAQAEiBMAAFBLAQIeAxQAAAAIANVRN0ms99lIMQEA" |
| 163 | "ACACAAALABgAAAAAAAAAAACgge4CAABjbGFzc2VzLmRleFVUBQADAWPlV3V4CwABBOQDAQAEiBMA" |
| 164 | "AFBLBQYAAAAAAwADAPUAAABkBAAAAAA="; |
| 165 | |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 166 | static const char kRawDexBadMapOffset[] = |
| 167 | "ZGV4CjAzNQAZKGSz85r+tXJ1I24FYi+FpQtWbXtelAmoAQAAcAAAAHhWNBIAAAAAAAAAAEAwIBAF" |
| 168 | "AAAAcAAAAAMAAACEAAAAAQAAAJAAAAAAAAAAAAAAAAIAAACcAAAAAQAAAKwAAADcAAAAzAAAAOQA" |
| 169 | "AADsAAAA9AAAAPkAAAANAQAAAgAAAAMAAAAEAAAABAAAAAIAAAAAAAAAAAAAAAAAAAABAAAAAAAA" |
| 170 | "AAAAAAABAAAAAQAAAAAAAAABAAAAAAAAABUBAAAAAAAAAQABAAEAAAAQAQAABAAAAHAQAQAAAA4A" |
| 171 | "Bjxpbml0PgAGQS5qYXZhAANMQTsAEkxqYXZhL2xhbmcvT2JqZWN0OwABVgABAAcOAAAAAQAAgYAE" |
| 172 | "zAEACwAAAAAAAAABAAAAAAAAAAEAAAAFAAAAcAAAAAIAAAADAAAAhAAAAAMAAAABAAAAkAAAAAUA" |
| 173 | "AAACAAAAnAAAAAYAAAABAAAArAAAAAEgAAABAAAAzAAAAAIgAAAFAAAA5AAAAAMgAAABAAAAEAEA" |
| 174 | "AAAgAAABAAAAFQEAAAAQAAABAAAAIAEAAA=="; |
| 175 | |
Jeff Hao | c122536 | 2017-05-01 17:29:35 -0700 | [diff] [blame] | 176 | static const char kRawDexDebugInfoLocalNullType[] = |
| 177 | "ZGV4CjAzNQA+Kwj2g6OZMH88OvK9Ey6ycdIsFCt18ED8AQAAcAAAAHhWNBIAAAAAAAAAAHQBAAAI" |
| 178 | "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAMAQAA8AAAABwB" |
| 179 | "AAAkAQAALAEAAC8BAAA0AQAASAEAAEsBAABOAQAAAgAAAAMAAAAEAAAABQAAAAIAAAAAAAAAAAAA" |
| 180 | "AAUAAAADAAAAAAAAAAEAAQAAAAAAAQAAAAYAAAACAAEAAAAAAAEAAAABAAAAAgAAAAAAAAABAAAA" |
| 181 | "AAAAAGMBAAAAAAAAAQABAAEAAABUAQAABAAAAHAQAgAAAA4AAgABAAAAAABZAQAAAgAAABIQDwAG" |
| 182 | "PGluaXQ+AAZBLmphdmEAAUkAA0xBOwASTGphdmEvbGFuZy9PYmplY3Q7AAFWAAFhAAR0aGlzAAEA" |
| 183 | "Bw4AAwAHDh4DAAcAAAAAAQEAgYAE8AEBAIgCAAAACwAAAAAAAAABAAAAAAAAAAEAAAAIAAAAcAAA" |
| 184 | "AAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAAuAAAAAYAAAABAAAA0AAAAAEgAAACAAAA" |
| 185 | "8AAAAAIgAAAIAAAAHAEAAAMgAAACAAAAVAEAAAAgAAABAAAAYwEAAAAQAAABAAAAdAEAAA=="; |
| 186 | |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 187 | static void DecodeAndWriteDexFile(const char* base64, const char* location) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 188 | // decode base64 |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 189 | CHECK(base64 != nullptr); |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 190 | std::vector<uint8_t> dex_bytes = DecodeBase64Vec(base64); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 191 | CHECK_NE(dex_bytes.size(), 0u); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 192 | |
| 193 | // write to provided file |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 194 | std::unique_ptr<File> file(OS::CreateEmptyFile(location)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 195 | CHECK(file.get() != nullptr); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 196 | if (!file->WriteFully(dex_bytes.data(), dex_bytes.size())) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 197 | PLOG(FATAL) << "Failed to write base64 as dex file"; |
| 198 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 199 | if (file->FlushCloseOrErase() != 0) { |
| 200 | PLOG(FATAL) << "Could not flush and close test file."; |
| 201 | } |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 202 | } |
| 203 | |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 204 | static bool OpenDexFilesBase64(const char* base64, |
| 205 | const char* location, |
| 206 | std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 207 | std::string* error_msg) { |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 208 | DecodeAndWriteDexFile(base64, location); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 209 | |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 210 | // read dex file(s) |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 211 | ScopedObjectAccess soa(Thread::Current()); |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 212 | static constexpr bool kVerifyChecksum = true; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 213 | std::vector<std::unique_ptr<const DexFile>> tmp; |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 214 | bool success = DexFile::Open(location, location, kVerifyChecksum, error_msg, &tmp); |
| 215 | if (success) { |
| 216 | for (std::unique_ptr<const DexFile>& dex_file : tmp) { |
| 217 | EXPECT_EQ(PROT_READ, dex_file->GetPermissions()); |
| 218 | EXPECT_TRUE(dex_file->IsReadOnly()); |
| 219 | } |
| 220 | *dex_files = std::move(tmp); |
| 221 | } |
| 222 | return success; |
| 223 | } |
| 224 | |
| 225 | static std::unique_ptr<const DexFile> OpenDexFileBase64(const char* base64, |
| 226 | const char* location) { |
| 227 | // read dex files. |
| 228 | std::string error_msg; |
| 229 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 230 | bool success = OpenDexFilesBase64(base64, location, &dex_files, &error_msg); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 231 | CHECK(success) << error_msg; |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 232 | EXPECT_EQ(1U, dex_files.size()); |
| 233 | return std::move(dex_files[0]); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 236 | static std::unique_ptr<const DexFile> OpenDexFileInMemoryBase64(const char* base64, |
| 237 | const char* location, |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 238 | uint32_t location_checksum, |
| 239 | bool expect_success) { |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 240 | CHECK(base64 != nullptr); |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 241 | std::vector<uint8_t> dex_bytes = DecodeBase64Vec(base64); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 242 | CHECK_NE(dex_bytes.size(), 0u); |
| 243 | |
| 244 | std::string error_message; |
| 245 | std::unique_ptr<MemMap> region(MemMap::MapAnonymous("test-region", |
| 246 | nullptr, |
| 247 | dex_bytes.size(), |
| 248 | PROT_READ | PROT_WRITE, |
| 249 | /* low_4gb */ false, |
| 250 | /* reuse */ false, |
| 251 | &error_message)); |
| 252 | memcpy(region->Begin(), dex_bytes.data(), dex_bytes.size()); |
| 253 | std::unique_ptr<const DexFile> dex_file(DexFile::Open(location, |
| 254 | location_checksum, |
| 255 | std::move(region), |
| 256 | /* verify */ true, |
| 257 | /* verify_checksum */ true, |
| 258 | &error_message)); |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 259 | if (expect_success) { |
| 260 | CHECK(dex_file != nullptr) << error_message; |
| 261 | } else { |
| 262 | CHECK(dex_file == nullptr) << "Expected dex file open to fail."; |
| 263 | } |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 264 | return dex_file; |
| 265 | } |
| 266 | |
Orion Hodson | cffc529 | 2016-09-05 13:43:11 +0100 | [diff] [blame] | 267 | static void ValidateDexFileHeader(std::unique_ptr<const DexFile> dex_file) { |
| 268 | static const uint8_t kExpectedDexFileMagic[8] = { |
| 269 | /* d */ 0x64, /* e */ 0x64, /* x */ 0x78, /* \n */ 0x0d, |
| 270 | /* 0 */ 0x30, /* 3 */ 0x33, /* 5 */ 0x35, /* \0 */ 0x00 |
| 271 | }; |
| 272 | static const uint8_t kExpectedSha1[DexFile::kSha1DigestSize] = { |
| 273 | 0x7b, 0xb8, 0x0c, 0xd4, 0x1f, 0xd6, 0x1e, 0xc5, |
| 274 | 0x89, 0xe8, 0xbe, 0xe5, 0x18, 0x02, 0x12, 0x18, |
| 275 | 0x2e, 0xf2, 0x8c, 0x3d, |
| 276 | }; |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 277 | |
Orion Hodson | cffc529 | 2016-09-05 13:43:11 +0100 | [diff] [blame] | 278 | const DexFile::Header& header = dex_file->GetHeader(); |
| 279 | EXPECT_EQ(*kExpectedDexFileMagic, *header.magic_); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 280 | EXPECT_EQ(0x00d87910U, header.checksum_); |
Orion Hodson | cffc529 | 2016-09-05 13:43:11 +0100 | [diff] [blame] | 281 | EXPECT_EQ(*kExpectedSha1, *header.signature_); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 282 | EXPECT_EQ(904U, header.file_size_); |
| 283 | EXPECT_EQ(112U, header.header_size_); |
| 284 | EXPECT_EQ(0U, header.link_size_); |
| 285 | EXPECT_EQ(0U, header.link_off_); |
| 286 | EXPECT_EQ(15U, header.string_ids_size_); |
| 287 | EXPECT_EQ(112U, header.string_ids_off_); |
| 288 | EXPECT_EQ(7U, header.type_ids_size_); |
| 289 | EXPECT_EQ(172U, header.type_ids_off_); |
| 290 | EXPECT_EQ(2U, header.proto_ids_size_); |
| 291 | EXPECT_EQ(200U, header.proto_ids_off_); |
| 292 | EXPECT_EQ(1U, header.field_ids_size_); |
| 293 | EXPECT_EQ(224U, header.field_ids_off_); |
| 294 | EXPECT_EQ(3U, header.method_ids_size_); |
| 295 | EXPECT_EQ(232U, header.method_ids_off_); |
| 296 | EXPECT_EQ(2U, header.class_defs_size_); |
| 297 | EXPECT_EQ(256U, header.class_defs_off_); |
| 298 | EXPECT_EQ(584U, header.data_size_); |
| 299 | EXPECT_EQ(320U, header.data_off_); |
| 300 | |
Orion Hodson | cffc529 | 2016-09-05 13:43:11 +0100 | [diff] [blame] | 301 | EXPECT_EQ(header.checksum_, dex_file->GetLocationChecksum()); |
| 302 | } |
| 303 | |
| 304 | TEST_F(DexFileTest, Header) { |
| 305 | ScratchFile tmp; |
| 306 | std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename().c_str())); |
| 307 | ValidateDexFileHeader(std::move(raw)); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | TEST_F(DexFileTest, HeaderInMemory) { |
| 311 | ScratchFile tmp; |
| 312 | std::unique_ptr<const DexFile> raw = |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 313 | OpenDexFileInMemoryBase64(kRawDex, tmp.GetFilename().c_str(), 0x00d87910U, true); |
Orion Hodson | cffc529 | 2016-09-05 13:43:11 +0100 | [diff] [blame] | 314 | ValidateDexFileHeader(std::move(raw)); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 317 | TEST_F(DexFileTest, Version38Accepted) { |
| 318 | ScratchFile tmp; |
| 319 | std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex38, tmp.GetFilename().c_str())); |
| 320 | ASSERT_TRUE(raw.get() != nullptr); |
| 321 | |
| 322 | const DexFile::Header& header = raw->GetHeader(); |
| 323 | EXPECT_EQ(38u, header.GetVersion()); |
| 324 | } |
| 325 | |
| 326 | TEST_F(DexFileTest, Version39Rejected) { |
| 327 | ScratchFile tmp; |
| 328 | const char* location = tmp.GetFilename().c_str(); |
| 329 | DecodeAndWriteDexFile(kRawDex39, location); |
| 330 | |
| 331 | ScopedObjectAccess soa(Thread::Current()); |
| 332 | static constexpr bool kVerifyChecksum = true; |
| 333 | std::string error_msg; |
| 334 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 335 | ASSERT_FALSE(DexFile::Open(location, location, kVerifyChecksum, &error_msg, &dex_files)); |
| 336 | } |
| 337 | |
ganxiaolin | cd16d0a | 2016-07-18 11:21:44 +0800 | [diff] [blame] | 338 | TEST_F(DexFileTest, ZeroLengthDexRejected) { |
| 339 | ScratchFile tmp; |
| 340 | const char* location = tmp.GetFilename().c_str(); |
| 341 | DecodeAndWriteDexFile(kRawDexZeroLength, location); |
| 342 | |
| 343 | ScopedObjectAccess soa(Thread::Current()); |
| 344 | static constexpr bool kVerifyChecksum = true; |
| 345 | std::string error_msg; |
| 346 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 347 | ASSERT_FALSE(DexFile::Open(location, location, kVerifyChecksum, &error_msg, &dex_files)); |
| 348 | } |
| 349 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 350 | TEST_F(DexFileTest, GetLocationChecksum) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 351 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 352 | std::unique_ptr<const DexFile> raw(OpenTestDexFile("Main")); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 353 | EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum()); |
| 354 | } |
| 355 | |
| 356 | TEST_F(DexFileTest, GetChecksum) { |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 357 | std::vector<uint32_t> checksums; |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 358 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 359 | std::string error_msg; |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 360 | EXPECT_TRUE(DexFile::GetMultiDexChecksums(GetLibCoreDexFileNames()[0].c_str(), &checksums, &error_msg)) |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 361 | << error_msg; |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 362 | ASSERT_EQ(1U, checksums.size()); |
| 363 | EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksums[0]); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 366 | TEST_F(DexFileTest, GetMultiDexChecksums) { |
| 367 | std::string error_msg; |
| 368 | std::vector<uint32_t> checksums; |
| 369 | std::string multidex_file = GetTestDexFileName("MultiDex"); |
| 370 | EXPECT_TRUE(DexFile::GetMultiDexChecksums(multidex_file.c_str(), |
| 371 | &checksums, |
| 372 | &error_msg)) << error_msg; |
| 373 | |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 374 | std::vector<std::unique_ptr<const DexFile>> dexes = OpenTestDexFiles("MultiDex"); |
| 375 | ASSERT_EQ(2U, dexes.size()); |
| 376 | ASSERT_EQ(2U, checksums.size()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 377 | |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 378 | EXPECT_EQ(dexes[0]->GetLocation(), DexFile::GetMultiDexLocation(0, multidex_file.c_str())); |
| 379 | EXPECT_EQ(dexes[0]->GetLocationChecksum(), checksums[0]); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 380 | |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 381 | EXPECT_EQ(dexes[1]->GetLocation(), DexFile::GetMultiDexLocation(1, multidex_file.c_str())); |
| 382 | EXPECT_EQ(dexes[1]->GetLocationChecksum(), checksums[1]); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 385 | TEST_F(DexFileTest, ClassDefs) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 386 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 387 | std::unique_ptr<const DexFile> raw(OpenTestDexFile("Nested")); |
| 388 | ASSERT_TRUE(raw.get() != nullptr); |
Andreas Gampe | ae78c26 | 2017-02-01 20:40:44 -0800 | [diff] [blame] | 389 | EXPECT_EQ(3U, raw->NumClassDefs()); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 390 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 391 | const DexFile::ClassDef& c0 = raw->GetClassDef(0); |
Andreas Gampe | ae78c26 | 2017-02-01 20:40:44 -0800 | [diff] [blame] | 392 | EXPECT_STREQ("LNested$1;", raw->GetClassDescriptor(c0)); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 393 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 394 | const DexFile::ClassDef& c1 = raw->GetClassDef(1); |
Andreas Gampe | ae78c26 | 2017-02-01 20:40:44 -0800 | [diff] [blame] | 395 | EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c1)); |
| 396 | |
| 397 | const DexFile::ClassDef& c2 = raw->GetClassDef(2); |
| 398 | EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c2)); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 401 | TEST_F(DexFileTest, GetMethodSignature) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 402 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 403 | std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature")); |
| 404 | ASSERT_TRUE(raw.get() != nullptr); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 405 | EXPECT_EQ(1U, raw->NumClassDefs()); |
| 406 | |
| 407 | const DexFile::ClassDef& class_def = raw->GetClassDef(0); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 408 | ASSERT_STREQ("LGetMethodSignature;", raw->GetClassDescriptor(class_def)); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 409 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 410 | const uint8_t* class_data = raw->GetClassData(class_def); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 411 | ASSERT_TRUE(class_data != nullptr); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 412 | ClassDataItemIterator it(*raw, class_data); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 413 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 414 | EXPECT_EQ(1u, it.NumDirectMethods()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 415 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 416 | // Check the signature for the static initializer. |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 417 | { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 418 | ASSERT_EQ(1U, it.NumDirectMethods()); |
| 419 | const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 420 | const char* name = raw->StringDataByIdx(method_id.name_idx_); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 421 | ASSERT_STREQ("<init>", name); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 422 | std::string signature(raw->GetMethodSignature(method_id).ToString()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 423 | ASSERT_EQ("()V", signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // Check both virtual methods. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 427 | ASSERT_EQ(2U, it.NumVirtualMethods()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 428 | { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 429 | it.Next(); |
| 430 | const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 431 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 432 | const char* name = raw->StringDataByIdx(method_id.name_idx_); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 433 | ASSERT_STREQ("m1", name); |
| 434 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 435 | std::string signature(raw->GetMethodSignature(method_id).ToString()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 436 | ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 440 | it.Next(); |
| 441 | const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 442 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 443 | const char* name = raw->StringDataByIdx(method_id.name_idx_); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 444 | ASSERT_STREQ("m2", name); |
| 445 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 446 | std::string signature(raw->GetMethodSignature(method_id).ToString()); |
| 447 | ASSERT_EQ("(ZSC)LGetMethodSignature;", signature); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | TEST_F(DexFileTest, FindStringId) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 452 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 453 | std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature")); |
| 454 | ASSERT_TRUE(raw.get() != nullptr); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 455 | EXPECT_EQ(1U, raw->NumClassDefs()); |
| 456 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 457 | const char* strings[] = { "LGetMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;", |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 458 | "D", "I", "J", nullptr }; |
| 459 | for (size_t i = 0; strings[i] != nullptr; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 460 | const char* str = strings[i]; |
| 461 | const DexFile::StringId* str_id = raw->FindStringId(str); |
| 462 | const char* dex_str = raw->GetStringData(*str_id); |
| 463 | EXPECT_STREQ(dex_str, str); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | TEST_F(DexFileTest, FindTypeId) { |
| 468 | for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 469 | const char* type_str = java_lang_dex_file_->StringByTypeIdx(dex::TypeIndex(i)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 470 | const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 471 | ASSERT_TRUE(type_str_id != nullptr); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 472 | dex::StringIndex type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 473 | const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx); |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 474 | ASSERT_EQ(type_id, java_lang_dex_file_->FindTypeId(type_str)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 475 | ASSERT_TRUE(type_id != nullptr); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 476 | EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id).index_, i); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | TEST_F(DexFileTest, FindProtoId) { |
| 481 | for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) { |
| 482 | const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i); |
| 483 | const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 484 | std::vector<dex::TypeIndex> to_find_types; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 485 | if (to_find_tl != nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 486 | for (size_t j = 0; j < to_find_tl->Size(); j++) { |
| 487 | to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_); |
| 488 | } |
| 489 | } |
| 490 | const DexFile::ProtoId* found = |
| 491 | java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 492 | ASSERT_TRUE(found != nullptr); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 493 | EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | TEST_F(DexFileTest, FindMethodId) { |
| 498 | for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) { |
| 499 | const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i); |
| 500 | const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); |
| 501 | const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); |
| 502 | const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_); |
| 503 | const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 504 | ASSERT_TRUE(found != nullptr) << "Didn't find method " << i << ": " |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 505 | << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." |
| 506 | << java_lang_dex_file_->GetStringData(name) |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 507 | << java_lang_dex_file_->GetMethodSignature(to_find); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 508 | EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 509 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 512 | TEST_F(DexFileTest, FindFieldId) { |
| 513 | for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) { |
| 514 | const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i); |
| 515 | const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); |
| 516 | const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); |
| 517 | const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_); |
| 518 | const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 519 | ASSERT_TRUE(found != nullptr) << "Didn't find field " << i << ": " |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 520 | << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " " |
| 521 | << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." |
| 522 | << java_lang_dex_file_->GetStringData(name); |
| 523 | EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i); |
| 524 | } |
| 525 | } |
| 526 | |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 527 | TEST_F(DexFileTest, GetMultiDexClassesDexName) { |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 528 | ASSERT_EQ("classes.dex", DexFile::GetMultiDexClassesDexName(0)); |
| 529 | ASSERT_EQ("classes2.dex", DexFile::GetMultiDexClassesDexName(1)); |
| 530 | ASSERT_EQ("classes3.dex", DexFile::GetMultiDexClassesDexName(2)); |
| 531 | ASSERT_EQ("classes100.dex", DexFile::GetMultiDexClassesDexName(99)); |
| 532 | } |
| 533 | |
| 534 | TEST_F(DexFileTest, GetMultiDexLocation) { |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 535 | std::string dex_location_str = "/system/app/framework.jar"; |
| 536 | const char* dex_location = dex_location_str.c_str(); |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 537 | ASSERT_EQ("/system/app/framework.jar", DexFile::GetMultiDexLocation(0, dex_location)); |
| 538 | ASSERT_EQ("/system/app/framework.jar:classes2.dex", |
| 539 | DexFile::GetMultiDexLocation(1, dex_location)); |
| 540 | ASSERT_EQ("/system/app/framework.jar:classes101.dex", |
| 541 | DexFile::GetMultiDexLocation(100, dex_location)); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | TEST_F(DexFileTest, GetDexCanonicalLocation) { |
| 545 | ScratchFile file; |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 546 | UniqueCPtr<const char[]> dex_location_real(realpath(file.GetFilename().c_str(), nullptr)); |
| 547 | std::string dex_location(dex_location_real.get()); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 548 | |
Calin Juravle | 61281dc | 2014-08-07 11:54:36 +0100 | [diff] [blame] | 549 | ASSERT_EQ(dex_location, DexFile::GetDexCanonicalLocation(dex_location.c_str())); |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 550 | std::string multidex_location = DexFile::GetMultiDexLocation(1, dex_location.c_str()); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 551 | ASSERT_EQ(multidex_location, DexFile::GetDexCanonicalLocation(multidex_location.c_str())); |
| 552 | |
| 553 | std::string dex_location_sym = dex_location + "symlink"; |
| 554 | ASSERT_EQ(0, symlink(dex_location.c_str(), dex_location_sym.c_str())); |
| 555 | |
| 556 | ASSERT_EQ(dex_location, DexFile::GetDexCanonicalLocation(dex_location_sym.c_str())); |
| 557 | |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 558 | std::string multidex_location_sym = DexFile::GetMultiDexLocation(1, dex_location_sym.c_str()); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 559 | ASSERT_EQ(multidex_location, DexFile::GetDexCanonicalLocation(multidex_location_sym.c_str())); |
| 560 | |
| 561 | ASSERT_EQ(0, unlink(dex_location_sym.c_str())); |
| 562 | } |
| 563 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 564 | TEST(DexFileUtilsTest, GetBaseLocationAndMultiDexSuffix) { |
| 565 | EXPECT_EQ("/foo/bar/baz.jar", DexFile::GetBaseLocation("/foo/bar/baz.jar")); |
| 566 | EXPECT_EQ("/foo/bar/baz.jar", DexFile::GetBaseLocation("/foo/bar/baz.jar:classes2.dex")); |
| 567 | EXPECT_EQ("/foo/bar/baz.jar", DexFile::GetBaseLocation("/foo/bar/baz.jar:classes8.dex")); |
| 568 | EXPECT_EQ("", DexFile::GetMultiDexSuffix("/foo/bar/baz.jar")); |
| 569 | EXPECT_EQ(":classes2.dex", DexFile::GetMultiDexSuffix("/foo/bar/baz.jar:classes2.dex")); |
| 570 | EXPECT_EQ(":classes8.dex", DexFile::GetMultiDexSuffix("/foo/bar/baz.jar:classes8.dex")); |
| 571 | } |
| 572 | |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 573 | TEST_F(DexFileTest, ZipOpenClassesPresent) { |
| 574 | ScratchFile tmp; |
| 575 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 576 | std::string error_msg; |
| 577 | ASSERT_TRUE(OpenDexFilesBase64(kRawZipClassesDexPresent, tmp.GetFilename().c_str(), &dex_files, |
| 578 | &error_msg)); |
| 579 | EXPECT_EQ(dex_files.size(), 1u); |
| 580 | } |
| 581 | |
| 582 | TEST_F(DexFileTest, ZipOpenClassesAbsent) { |
| 583 | ScratchFile tmp; |
| 584 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 585 | std::string error_msg; |
| 586 | ASSERT_FALSE(OpenDexFilesBase64(kRawZipClassesDexAbsent, tmp.GetFilename().c_str(), &dex_files, |
| 587 | &error_msg)); |
| 588 | EXPECT_EQ(dex_files.size(), 0u); |
| 589 | } |
| 590 | |
| 591 | TEST_F(DexFileTest, ZipOpenThreeDexFiles) { |
| 592 | ScratchFile tmp; |
| 593 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 594 | std::string error_msg; |
| 595 | ASSERT_TRUE(OpenDexFilesBase64(kRawZipThreeDexFiles, tmp.GetFilename().c_str(), &dex_files, |
| 596 | &error_msg)); |
| 597 | EXPECT_EQ(dex_files.size(), 3u); |
| 598 | } |
| 599 | |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 600 | TEST_F(DexFileTest, OpenDexBadMapOffset) { |
| 601 | ScratchFile tmp; |
| 602 | std::unique_ptr<const DexFile> raw = |
| 603 | OpenDexFileInMemoryBase64(kRawDexBadMapOffset, tmp.GetFilename().c_str(), 0xb3642819U, false); |
| 604 | EXPECT_EQ(raw, nullptr); |
| 605 | } |
| 606 | |
Jeff Hao | 120504f | 2017-04-14 14:33:52 -0700 | [diff] [blame] | 607 | TEST_F(DexFileTest, GetStringWithNoIndex) { |
| 608 | ScratchFile tmp; |
| 609 | std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename().c_str())); |
| 610 | dex::TypeIndex idx; |
| 611 | EXPECT_EQ(raw->StringByTypeIdx(idx), nullptr); |
| 612 | } |
| 613 | |
Jeff Hao | c122536 | 2017-05-01 17:29:35 -0700 | [diff] [blame] | 614 | static void Callback(void* context ATTRIBUTE_UNUSED, |
| 615 | const DexFile::LocalInfo& entry ATTRIBUTE_UNUSED) { |
| 616 | } |
| 617 | |
| 618 | TEST_F(DexFileTest, OpenDexDebugInfoLocalNullType) { |
| 619 | ScratchFile tmp; |
| 620 | std::unique_ptr<const DexFile> raw = OpenDexFileInMemoryBase64( |
| 621 | kRawDexDebugInfoLocalNullType, tmp.GetFilename().c_str(), 0xf25f2b38U, true); |
| 622 | const DexFile::ClassDef& class_def = raw->GetClassDef(0); |
| 623 | const DexFile::CodeItem* code_item = raw->GetCodeItem(raw->FindCodeItemOffset(class_def, 1)); |
| 624 | ASSERT_TRUE(raw->DecodeDebugLocalInfo(code_item, true, 1, Callback, nullptr)); |
| 625 | } |
| 626 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 627 | } // namespace art |