Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Alex Light | 4eec3c5 | 2017-04-14 13:17:26 -0700 | [diff] [blame] | 17 | |
| 18 | import art.Redefinition; |
| 19 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 20 | import java.util.function.Consumer; |
| 21 | import java.lang.reflect.Method; |
| 22 | import java.util.Base64; |
| 23 | |
| 24 | public class Main { |
| 25 | |
| 26 | // import java.util.function.Consumer; |
| 27 | // |
| 28 | // class Transform { |
| 29 | // private void Start(Consumer<String> reporter) { |
| 30 | // reporter.accept("Hello - private - Transformed"); |
| 31 | // } |
| 32 | // |
| 33 | // private void Finish(Consumer<String> reporter) { |
| 34 | // reporter.accept("Goodbye - private - Transformed"); |
| 35 | // } |
| 36 | // |
| 37 | // public void sayHi(Runnable r, Consumer<String> reporter) { |
| 38 | // reporter.accept("pre Start private method call - Transformed"); |
| 39 | // Start(reporter); |
| 40 | // reporter.accept("post Start private method call - Transformed"); |
| 41 | // r.run(); |
| 42 | // reporter.accept("pre Finish private method call - Transformed"); |
| 43 | // Finish(reporter); |
| 44 | // reporter.accept("post Finish private method call - Transformed"); |
| 45 | // } |
| 46 | // } |
| 47 | private static final byte[] CLASS_BYTES = Base64.getDecoder().decode( |
| 48 | "yv66vgAAADQAMAoADQAcCAAdCwAeAB8IACAIACE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QAGYWNjZXB0" + |
| 61 | "AQAVKExqYXZhL2xhbmcvT2JqZWN0OylWAQASamF2YS9sYW5nL1J1bm5hYmxlAQADcnVuACAADAAN" + |
| 62 | "AAAAAAAEAAAADgAPAAEAEAAAAB0AAQABAAAABSq3AAGxAAAAAQARAAAABgABAAAAEwACABIAEwAC" + |
| 63 | "ABAAAAAlAAIAAgAAAAkrEgK5AAMCALEAAAABABEAAAAKAAIAAAAVAAgAFgAUAAAAAgAVAAIAFgAT" + |
| 64 | "AAIAEAAAACUAAgACAAAACSsSBLkAAwIAsQAAAAEAEQAAAAoAAgAAABkACAAaABQAAAACABUAAQAX" + |
| 65 | "ABgAAgAQAAAAZQACAAMAAAAxLBIFuQADAgAqLLcABiwSB7kAAwIAK7kACAEALBIJuQADAgAqLLcA" + |
| 66 | "CiwSC7kAAwIAsQAAAAEAEQAAACIACAAAAB0ACAAeAA0AHwAVACAAGwAhACMAIgAoACMAMAAkABQA" + |
| 67 | "AAACABkAAQAaAAAAAgAb"); |
| 68 | private static final byte[] DEX_BYTES = Base64.getDecoder().decode( |
| 69 | "ZGV4CjAzNQBc8wr9PcHqnOR61m+0kimXTSddVMToJPuYBQAAcAAAAHhWNBIAAAAAAAAAAOAEAAAc" + |
| 70 | "AAAAcAAAAAYAAADgAAAABAAAAPgAAAAAAAAAAAAAAAcAAAAoAQAAAQAAAGABAAAYBAAAgAEAAHoC" + |
| 71 | "AAB9AgAAgAIAAIgCAACOAgAAlgIAALcCAADWAgAA4wIAAAIDAAAWAwAALAMAAEADAABeAwAAfQMA" + |
| 72 | "AIQDAACUAwAAlwMAAJsDAACgAwAAqAMAALwDAADrAwAAGQQAAEcEAAB0BAAAeQQAAIAEAAAHAAAA" + |
| 73 | "CAAAAAkAAAAKAAAADQAAABAAAAAQAAAABQAAAAAAAAARAAAABQAAAGQCAAASAAAABQAAAGwCAAAR" + |
| 74 | "AAAABQAAAHQCAAAAAAAAAgAAAAAAAwAEAAAAAAADAA4AAAAAAAIAGgAA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G9kIGNhbGwgLSBUcmFuc2Zvcm1lZAAscHJlIEZpbmlzaCBwcml2YXRlIG1ldGhvZCBjYWxsIC0g" + |
| 88 | "VHJhbnNmb3JtZWQAK3ByZSBTdGFydCBwcml2YXRlIG1ldGhvZCBjYWxsIC0gVHJhbnNmb3JtZWQA" + |
| 89 | "A3J1bgAFc2F5SGkABXZhbHVlABMABw4AGQEABw5pABUBAAcOaQAdAgAABw5pPGk8aTxpAAIBARsc" + |
| 90 | "BRcAFwwXARcLFwMCAQEbHAYXABcKFwwXARcLFwMAAAMBAICABJADAQKoAwECyAMDAegDDwAAAAAA" + |
| 91 | "AAABAAAAAAAAAAEAAAAcAAAAcAAAAAIAAAAGAAAA4AAAAAMAAAAEAAAA+AAAAAUAAAAHAAAAKAEA" + |
| 92 | "AAYAAAABAAAAYAEAAAMQAAACAAAAgAEAAAEgAAAEAAAAkAEAAAYgAAABAAAAPAIAAAEQAAADAAAA" + |
| 93 | "ZAIAAAIgAAAcAAAAegIAAAMgAAAEAAAAhwQAAAQgAAACAAAAqAQAAAAgAAABAAAAygQAAAAQAAAB" + |
| 94 | "AAAA4AQAAA=="); |
| 95 | |
| 96 | // A class that we can use to keep track of the output of this test. |
| 97 | private static class TestWatcher implements Consumer<String> { |
| 98 | private StringBuilder sb; |
| 99 | public TestWatcher() { |
| 100 | sb = new StringBuilder(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void accept(String s) { |
| 105 | sb.append(s); |
| 106 | sb.append('\n'); |
| 107 | } |
| 108 | |
| 109 | public String getOutput() { |
| 110 | return sb.toString(); |
| 111 | } |
| 112 | |
| 113 | public void clear() { |
| 114 | sb = new StringBuilder(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public static void main(String[] args) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 119 | doTest(new Transform(), new TestWatcher()); |
| 120 | } |
| 121 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 122 | private static boolean interpreting = true; |
| 123 | private static boolean retry = false; |
| 124 | |
| 125 | public static void doTest(Transform t, TestWatcher w) { |
| 126 | // Get the methods that need to be optimized. |
| 127 | Method say_hi_method; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 128 | // Figure out if we can even JIT at all. |
| 129 | final boolean has_jit = hasJit(); |
| 130 | try { |
| 131 | say_hi_method = Transform.class.getDeclaredMethod( |
| 132 | "sayHi", Runnable.class, Consumer.class); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 133 | } catch (Exception e) { |
| 134 | System.out.println("Unable to find methods!"); |
Kevin Brodsky | f6c66c3 | 2015-12-17 14:13:00 +0000 | [diff] [blame] | 135 | e.printStackTrace(System.out); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | // Makes sure the stack is the way we want it for the test and does the redefinition. It will |
Alex Light | 5ecd5b7 | 2017-01-30 14:15:54 -0800 | [diff] [blame] | 139 | // set the retry boolean to true if the stack does not have a JIT-compiled sayHi entry. This can |
| 140 | // only happen if the method gets GC'd. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 141 | Runnable do_redefinition = () -> { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 142 | if (has_jit && Main.isInterpretedFunction(say_hi_method, true)) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 143 | // Try again. We are not running the right jitted methods/cannot redefine them now. |
| 144 | retry = true; |
| 145 | } else { |
| 146 | // Actually do the redefinition. The stack looks good. |
| 147 | retry = false; |
| 148 | w.accept("transforming calling function"); |
Alex Light | 4eec3c5 | 2017-04-14 13:17:26 -0700 | [diff] [blame] | 149 | Redefinition.doCommonClassRedefinition(Transform.class, CLASS_BYTES, DEX_BYTES); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 150 | } |
| 151 | }; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 152 | // This just prints something out to show we are running the Runnable. |
| 153 | Runnable say_nothing = () -> { w.accept("Not doing anything here"); }; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 154 | do { |
Alex Light | 4e03c52 | 2017-01-14 01:48:01 +0000 | [diff] [blame] | 155 | // Run ensureJitCompiled here since it might get GCd |
| 156 | ensureJitCompiled(Transform.class, "sayHi"); |
Alex Light | 4e03c52 | 2017-01-14 01:48:01 +0000 | [diff] [blame] | 157 | // Clear output. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 158 | w.clear(); |
| 159 | // Try and redefine. |
| 160 | t.sayHi(say_nothing, w); |
| 161 | t.sayHi(do_redefinition, w); |
| 162 | t.sayHi(say_nothing, w); |
| 163 | } while (retry); |
| 164 | // Print output of last run. |
| 165 | System.out.print(w.getOutput()); |
| 166 | } |
| 167 | |
| 168 | private static native boolean hasJit(); |
| 169 | |
| 170 | private static native boolean isInterpretedFunction(Method m, boolean require_deoptimizable); |
| 171 | |
Alex Light | 4e03c52 | 2017-01-14 01:48:01 +0000 | [diff] [blame] | 172 | private static native void ensureJitCompiled(Class c, String name); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 173 | } |