1 /* 2 * Copyright (C) 2017 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 public class Main { 18 public static void main(String[] args) { 19 System.loadLibrary(args[0]); 20 if (!hasJit()) { 21 // Make the test pass if not using JIT. 22 return; 23 } 24 if (hasImage()) { 25 throw new Error("The `run` script should prevent this test from running with an image!"); 26 } 27 if (!isClassMoveable(String.class)) { 28 throw new Error("String.class not moveable despite running without image!"); 29 } 30 31 // Make sure the Main.test() is JIT-compiled and then call it. 32 ensureJitCompiled(Main.class, "test"); 33 test(); 34 } 35 36 public static void test() { 37 int length = 5; 38 39 // Hide the type of these strings in an Object array, 40 // so that we treat them as Object for the String.equals() below. 41 Object[] array = new Object[length]; 42 for (int i = 0; i != length; ++i) { 43 array[i] = "V" + i; 44 } 45 46 // Continually check string equality between a newly allocated String and an 47 // already allocated String with the same contents while allocating over 128MiB 48 // memory (with heap size limited to 16MiB), ensuring we run GC and stress the 49 // instanceof check in the String.equals() implementation. 50 for (int count = 0; count != 128 * 1024; ++count) { 51 for (int i = 0; i != length; ++i) { 52 allocateAtLeast1KiB(); 53 assertTrue(("V" + i).equals(array[i])); 54 } 55 } 56 } 57 58 public static void allocateAtLeast1KiB() { 59 // Give GC more work by allocating Object arrays. 60 memory[allocationIndex] = new Object[1024 / 4]; 61 ++allocationIndex; 62 if (allocationIndex == memory.length) { 63 allocationIndex = 0; 64 } 65 } 66 67 public static void assertTrue(boolean value) { 68 if (!value) { 69 throw new Error("Assertion failed!"); 70 } 71 } 72 73 private native static boolean hasJit(); 74 private native static boolean hasImage(); 75 private native static boolean isClassMoveable(Class<?> cls); 76 private static native void ensureJitCompiled(Class<?> itf, String method_name); 77 78 // We shall retain some allocated memory and release old allocations 79 // so that the GC has something to do. 80 public static Object[] memory = new Object[4096]; 81 public static int allocationIndex = 0; 82 } 83