David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | import dalvik.system.InMemoryDexClassLoader; |
| 18 | import java.lang.reflect.Method; |
| 19 | import java.io.File; |
| 20 | import java.nio.ByteBuffer; |
| 21 | import java.util.Base64; |
| 22 | |
| 23 | public class Main { |
| 24 | private static void check(boolean expected, boolean actual, String message) { |
| 25 | if (expected != actual) { |
| 26 | System.err.println( |
| 27 | "ERROR: " + message + " (expected=" + expected + ", actual=" + actual + ")"); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | private static ClassLoader singleLoader() { |
| 32 | return new InMemoryDexClassLoader( |
| 33 | new ByteBuffer[] { ByteBuffer.wrap(DEX_BYTES_A), ByteBuffer.wrap(DEX_BYTES_B) }, |
| 34 | /*parent*/null); |
| 35 | } |
| 36 | |
| 37 | private static ClassLoader[] multiLoader() { |
| 38 | ClassLoader clA = new InMemoryDexClassLoader(ByteBuffer.wrap(DEX_BYTES_A), /*parent*/ null); |
| 39 | ClassLoader clB = new InMemoryDexClassLoader(ByteBuffer.wrap(DEX_BYTES_B), /*parent*/ clA); |
| 40 | return new ClassLoader[] { clA, clB }; |
| 41 | } |
| 42 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 43 | private static void test(ClassLoader loader, |
| 44 | boolean expectedHasVdexFile, |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 45 | boolean expectedBackedByOat, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 46 | boolean invokeMethod) throws Exception { |
| 47 | // If ART created a vdex file, it must have verified all the classes. |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 48 | // That happens if and only if we expect a vdex at the end of the test but |
| 49 | // do not expect it to have been loaded. |
| 50 | boolean expectedClassesVerified = expectedHasVdexFile && !expectedBackedByOat; |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 51 | |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 52 | waitForVerifier(); |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 53 | check(expectedClassesVerified, areClassesVerified(loader), "areClassesVerified"); |
| 54 | check(expectedHasVdexFile, hasVdexFile(loader), "areClassesVerified"); |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 55 | check(expectedBackedByOat, isBackedByOatFile(loader), "isBackedByOatFile"); |
| 56 | check(expectedBackedByOat, areClassesPreverified(loader), "areClassesPreverified"); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 57 | |
| 58 | if (invokeMethod) { |
| 59 | loader.loadClass("art.ClassB").getDeclaredMethod("printHello").invoke(null); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public static void main(String[] args) throws Exception { |
| 64 | System.loadLibrary(args[0]); |
| 65 | ClassLoader[] loaders = null; |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 66 | |
| 67 | // Feature is disabled in debuggable mode because runtime threads are not |
| 68 | // allowed to load classes. |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 69 | boolean featureEnabled = !isDebuggable(); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 70 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 71 | // Data directory not set. Background verification job should not have run |
| 72 | // and vdex should not have been created. |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 73 | test(singleLoader(), /*hasVdex*/ false, /*backedByOat*/ false, /*invokeMethod*/ true); |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 74 | |
| 75 | // Set data directory for this process. |
| 76 | setProcessDataDir(DEX_LOCATION); |
| 77 | |
| 78 | // Data directory is now set. Background verification job should have run, |
| 79 | // should have verified classes and written results to a vdex. |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 80 | test(singleLoader(), /*hasVdex*/ featureEnabled, /*backedByOat*/ false, /*invokeMethod*/ true); |
| 81 | test(singleLoader(), /*hasVdex*/ featureEnabled, /*backedByOat*/ true, /*invokeMethod*/ true); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 82 | |
| 83 | // Test loading the two dex files with separate class loaders. |
| 84 | // Background verification task should still verify all classes. |
| 85 | loaders = multiLoader(); |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 86 | test(loaders[0], /*hasVdex*/ featureEnabled, /*backedByOat*/ false, /*invokeMethod*/ false); |
| 87 | test(loaders[1], /*hasVdex*/ featureEnabled, /*backedByOat*/ false, /*invokeMethod*/ true); |
| 88 | |
| 89 | loaders = multiLoader(); |
| 90 | test(loaders[0], /*hasVdex*/ featureEnabled, /*backedByOat*/ featureEnabled, |
| 91 | /*invokeMethod*/ false); |
| 92 | test(loaders[1], /*hasVdex*/ featureEnabled, /*backedByOat*/ featureEnabled, |
| 93 | /*invokeMethod*/ true); |
| 94 | |
| 95 | // Change boot classpath checksum. |
| 96 | appendToBootClassLoader(DEX_EXTRA, /*isCorePlatform*/ false); |
| 97 | |
| 98 | loaders = multiLoader(); |
| 99 | test(loaders[0], /*hasVdex*/ featureEnabled, /*backedByOat*/ false, /*invokeMethod*/ false); |
| 100 | test(loaders[1], /*hasVdex*/ featureEnabled, /*backedByOat*/ false, /*invokeMethod*/ true); |
| 101 | |
| 102 | loaders = multiLoader(); |
| 103 | test(loaders[0], /*hasVdex*/ featureEnabled, /*backedByOat*/ featureEnabled, |
| 104 | /*invokeMethod*/ false); |
| 105 | test(loaders[1], /*hasVdex*/ featureEnabled, /*backedByOat*/ featureEnabled, |
| 106 | /*invokeMethod*/ true); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private static native boolean isDebuggable(); |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 110 | private static native void setProcessDataDir(String path); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 111 | private static native void waitForVerifier(); |
| 112 | private static native boolean areClassesVerified(ClassLoader loader); |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 113 | private static native boolean hasVdexFile(ClassLoader loader); |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 114 | private static native boolean isBackedByOatFile(ClassLoader loader); |
| 115 | private static native boolean areClassesPreverified(ClassLoader loader); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 116 | |
| 117 | // Defined in 674-hiddenapi. |
| 118 | private static native void appendToBootClassLoader(String dexPath, boolean isCorePlatform); |
| 119 | |
| 120 | private static final String DEX_LOCATION = System.getenv("DEX_LOCATION"); |
| 121 | private static final String DEX_EXTRA = |
| 122 | new File(DEX_LOCATION, "692-vdex-inmem-loader-ex.jar").getAbsolutePath(); |
| 123 | |
| 124 | private static final byte[] DEX_BYTES_A = Base64.getDecoder().decode( |
| 125 | "ZGV4CjAzNQBxYu/tdPfiHaRPYr5yaT6ko9V/xMinr1OwAgAAcAAAAHhWNBIAAAAAAAAAABwCAAAK" + |
| 126 | "AAAAcAAAAAQAAACYAAAAAgAAAKgAAAAAAAAAAAAAAAMAAADAAAAAAQAAANgAAAC4AQAA+AAAADAB" + |
| 127 | "AAA4AQAARQEAAEwBAABPAQAAXQEAAHEBAACFAQAAiAEAAJIBAAAEAAAABQAAAAYAAAAHAAAAAwAA" + |
| 128 | "AAIAAAAAAAAABwAAAAMAAAAAAAAAAAABAAAAAAAAAAAACAAAAAEAAQAAAAAAAAAAAAEAAAABAAAA" + |
| 129 | "AAAAAAEAAAAAAAAACQIAAAAAAAABAAAAAAAAACwBAAADAAAAGgACABEAAAABAAEAAQAAACgBAAAE" + |
| 130 | "AAAAcBACAAAADgATAA4AFQAOAAY8aW5pdD4AC0NsYXNzQS5qYXZhAAVIZWxsbwABTAAMTGFydC9D" + |
| 131 | "bGFzc0E7ABJMamF2YS9sYW5nL09iamVjdDsAEkxqYXZhL2xhbmcvU3RyaW5nOwABVgAIZ2V0SGVs" + |
| 132 | "bG8AdX5+RDh7ImNvbXBpbGF0aW9uLW1vZGUiOiJkZWJ1ZyIsIm1pbi1hcGkiOjEsInNoYS0xIjoi" + |
| 133 | "OTY2MDhmZDdiYmNjZGQyMjc2Y2Y4OTI4M2QyYjgwY2JmYzRmYzgxYyIsInZlcnNpb24iOiIxLjUu" + |
| 134 | "NC1kZXYifQAAAAIAAIGABJACAQn4AQAAAAAADAAAAAAAAAABAAAAAAAAAAEAAAAKAAAAcAAAAAIA" + |
| 135 | "AAAEAAAAmAAAAAMAAAACAAAAqAAAAAUAAAADAAAAwAAAAAYAAAABAAAA2AAAAAEgAAACAAAA+AAA" + |
| 136 | "AAMgAAACAAAAKAEAAAIgAAAKAAAAMAEAAAAgAAABAAAACQIAAAMQAAABAAAAGAIAAAAQAAABAAAA" + |
| 137 | "HAIAAA=="); |
| 138 | private static final byte[] DEX_BYTES_B = Base64.getDecoder().decode( |
| 139 | "ZGV4CjAzNQB+hWvce73hXt7ZVNgp9RAyMLSwQzsWUjV4AwAAcAAAAHhWNBIAAAAAAAAAAMwCAAAQ" + |
| 140 | "AAAAcAAAAAcAAACwAAAAAwAAAMwAAAABAAAA8AAAAAUAAAD4AAAAAQAAACABAAA4AgAAQAEAAI4B" + |
| 141 | "AACWAQAAowEAAKYBAAC0AQAAwgEAANkBAADtAQAAAQIAABUCAAAYAgAAHAIAACYCAAArAgAANwIA" + |
| 142 | "AEACAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAAAgAAAAQAAAAAAAAACQAAAAYAAAAAAAAA" + |
| 143 | "CgAAAAYAAACIAQAABQACAAwAAAAAAAAACwAAAAEAAQAAAAAAAQABAA0AAAACAAIADgAAAAMAAQAA" + |
| 144 | "AAAAAQAAAAEAAAADAAAAAAAAAAEAAAAAAAAAtwIAAAAAAAABAAEAAQAAAHwBAAAEAAAAcBAEAAAA" + |
| 145 | "DgACAAAAAgAAAIABAAAKAAAAYgAAAHEAAAAAAAwBbiADABAADgATAA4AFQAOlgAAAAABAAAABAAG" + |
| 146 | "PGluaXQ+AAtDbGFzc0IuamF2YQABTAAMTGFydC9DbGFzc0E7AAxMYXJ0L0NsYXNzQjsAFUxqYXZh" + |
| 147 | "L2lvL1ByaW50U3RyZWFtOwASTGphdmEvbGFuZy9PYmplY3Q7ABJMamF2YS9sYW5nL1N0cmluZzsA" + |
| 148 | "EkxqYXZhL2xhbmcvU3lzdGVtOwABVgACVkwACGdldEhlbGxvAANvdXQACnByaW50SGVsbG8AB3By" + |
| 149 | "aW50bG4AdX5+RDh7ImNvbXBpbGF0aW9uLW1vZGUiOiJkZWJ1ZyIsIm1pbi1hcGkiOjEsInNoYS0x" + |
| 150 | "IjoiOTY2MDhmZDdiYmNjZGQyMjc2Y2Y4OTI4M2QyYjgwY2JmYzRmYzgxYyIsInZlcnNpb24iOiIx" + |
| 151 | "LjUuNC1kZXYifQAAAAIAAYGABMACAQnYAgAAAAAAAAAOAAAAAAAAAAEAAAAAAAAAAQAAABAAAABw" + |
| 152 | "AAAAAgAAAAcAAACwAAAAAwAAAAMAAADMAAAABAAAAAEAAADwAAAABQAAAAUAAAD4AAAABgAAAAEA" + |
| 153 | "AAAgAQAAASAAAAIAAABAAQAAAyAAAAIAAAB8AQAAARAAAAEAAACIAQAAAiAAABAAAACOAQAAACAA" + |
| 154 | "AAEAAAC3AgAAAxAAAAEAAADIAgAAABAAAAEAAADMAgAA"); |
| 155 | } |