1 /* 2 * Copyright 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 class Main { 18 private static final boolean DEBUG = false; 19 main(String[] args)20 public static void main(String[] args) throws Exception { 21 System.loadLibrary(args[0]); 22 makeVisiblyInitialized(); 23 Class<?> testClass = Class.forName("TestClass"); // Request initialized class. 24 boolean is_visibly_initialized = isVisiblyInitialized(testClass); 25 if (DEBUG) { 26 System.out.println((is_visibly_initialized ? "Already" : "Not yet") + " visibly intialized"); 27 } 28 if (!is_visibly_initialized) { 29 synchronized(testClass) { 30 Thread t = new Thread() { 31 public void run() { 32 // Regression test: This would have previously deadlocked 33 // trying to lock on testClass. b/138561860 34 makeVisiblyInitialized(); 35 } 36 }; 37 t.start(); 38 t.join(); 39 } 40 if (!isVisiblyInitialized(testClass)) { 41 throw new Error("Should be visibly initialized now."); 42 } 43 } 44 } 45 makeVisiblyInitialized()46 public static native void makeVisiblyInitialized(); isVisiblyInitialized(Class<?> klass)47 public static native boolean isVisiblyInitialized(Class<?> klass); 48 } 49 50 class TestClass { 51 static { 52 // Add a static constructor that prevents initialization at compile time (app images). 53 Main.isVisiblyInitialized(TestClass.class); // Native call, discard result. 54 } 55 } 56