|
1 /* |
|
2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 package jdk.vm.ci.hotspot.test; |
|
24 |
|
25 import java.util.ArrayList; |
|
26 import java.util.Arrays; |
|
27 import java.util.List; |
|
28 import java.util.function.Predicate; |
|
29 |
|
30 import org.junit.Assert; |
|
31 import org.junit.Test; |
|
32 |
|
33 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; |
|
34 import jdk.vm.ci.meta.MetaAccessProvider; |
|
35 import jdk.vm.ci.meta.ResolvedJavaType; |
|
36 |
|
37 public class TestHotSpotJVMCIRuntime { |
|
38 |
|
39 @Test |
|
40 public void writeDebugOutputTest() { |
|
41 HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime(); |
|
42 |
|
43 expectWriteDebugOutputFailure(runtime, null, 0, 0, true, true, NullPointerException.class); |
|
44 expectWriteDebugOutputFailure(runtime, null, 0, 0, true, false, -1); |
|
45 |
|
46 byte[] emptyOutput = {}; |
|
47 byte[] nonEmptyOutput = String.format("non-empty output%n").getBytes(); |
|
48 |
|
49 for (boolean canThrow : new boolean[]{true, false}) { |
|
50 for (byte[] output : new byte[][]{emptyOutput, nonEmptyOutput}) { |
|
51 for (int offset = 0; offset < output.length; offset++) { |
|
52 int length = output.length - offset; |
|
53 runtime.writeDebugOutput(output, offset, length, true, canThrow); |
|
54 } |
|
55 |
|
56 Object expect = canThrow ? IndexOutOfBoundsException.class : -2; |
|
57 expectWriteDebugOutputFailure(runtime, output, output.length + 1, 0, true, canThrow, expect); |
|
58 expectWriteDebugOutputFailure(runtime, output, 0, output.length + 1, true, canThrow, expect); |
|
59 expectWriteDebugOutputFailure(runtime, output, -1, 0, true, canThrow, expect); |
|
60 expectWriteDebugOutputFailure(runtime, output, 0, -1, true, canThrow, expect); |
|
61 } |
|
62 } |
|
63 } |
|
64 |
|
65 private static void expectWriteDebugOutputFailure(HotSpotJVMCIRuntime runtime, byte[] bytes, int offset, int length, boolean flush, boolean canThrow, Object expect) { |
|
66 try { |
|
67 int result = runtime.writeDebugOutput(bytes, offset, length, flush, canThrow); |
|
68 if (expect instanceof Integer) { |
|
69 Assert.assertEquals((int) expect, result); |
|
70 } else { |
|
71 Assert.fail("expected " + expect + ", got " + result + " for bytes == " + Arrays.toString(bytes)); |
|
72 } |
|
73 } catch (Exception e) { |
|
74 if (expect instanceof Integer) { |
|
75 Assert.fail("expected " + expect + ", got " + e + " for bytes == " + Arrays.toString(bytes)); |
|
76 } else { |
|
77 Assert.assertTrue(e.toString(), ((Class<?>) expect).isInstance(e)); |
|
78 } |
|
79 } |
|
80 } |
|
81 |
|
82 @Test |
|
83 public void getIntrinsificationTrustPredicateTest() throws Exception { |
|
84 HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime(); |
|
85 MetaAccessProvider metaAccess = runtime.getHostJVMCIBackend().getMetaAccess(); |
|
86 Predicate<ResolvedJavaType> predicate = runtime.getIntrinsificationTrustPredicate(HotSpotJVMCIRuntime.class); |
|
87 List<Class<?>> classes = new ArrayList<>(Arrays.asList( |
|
88 Object.class, |
|
89 String.class, |
|
90 Class.class, |
|
91 HotSpotJVMCIRuntime.class, |
|
92 VirtualObjectLayoutTest.class, |
|
93 TestHotSpotJVMCIRuntime.class)); |
|
94 try { |
|
95 classes.add(Class.forName("com.sun.crypto.provider.AESCrypt")); |
|
96 classes.add(Class.forName("com.sun.crypto.provider.CipherBlockChaining")); |
|
97 } catch (ClassNotFoundException e) { |
|
98 // Extension classes not available |
|
99 } |
|
100 ClassLoader jvmciLoader = HotSpotJVMCIRuntime.class.getClassLoader(); |
|
101 ClassLoader extLoader = getExtensionLoader(); |
|
102 for (Class<?> c : classes) { |
|
103 ClassLoader cl = c.getClassLoader(); |
|
104 boolean expected = cl == null || cl == jvmciLoader || cl == extLoader; |
|
105 boolean actual = predicate.test(metaAccess.lookupJavaType(c)); |
|
106 Assert.assertEquals(c + ": cl=" + cl, expected, actual); |
|
107 } |
|
108 } |
|
109 |
|
110 private static ClassLoader getExtensionLoader() throws Exception { |
|
111 Object launcher = Class.forName("sun.misc.Launcher").getMethod("getLauncher").invoke(null); |
|
112 ClassLoader appLoader = (ClassLoader) launcher.getClass().getMethod("getClassLoader").invoke(launcher); |
|
113 ClassLoader extLoader = appLoader.getParent(); |
|
114 assert extLoader.getClass().getName().equals("sun.misc.Launcher$ExtClassLoader") : extLoader; |
|
115 return extLoader; |
|
116 } |
|
117 } |