author | jlaskey |
Thu, 14 Nov 2019 12:50:08 -0400 | |
branch | JDK-8193209-branch |
changeset 59088 | da026c172c1e |
parent 58299 | 6df94ce3ab2f |
child 58679 | 9c3209ff7550 |
permissions | -rw-r--r-- |
50609 | 1 |
/* |
58299 | 2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
50609 | 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 |
*/ |
|
50858 | 23 |
|
24 |
||
50609 | 25 |
package org.graalvm.compiler.core.test; |
26 |
||
55619
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
27 |
import java.lang.invoke.MethodHandles; |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
28 |
import java.lang.reflect.Method; |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
29 |
import java.security.ProtectionDomain; |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
30 |
|
55509 | 31 |
import org.graalvm.compiler.serviceprovider.JavaVersionUtil; |
50609 | 32 |
import org.objectweb.asm.Opcodes; |
33 |
||
55619
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
34 |
import sun.misc.Unsafe; |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
35 |
|
50609 | 36 |
public abstract class CustomizedBytecodePatternTest extends GraalCompilerTest implements Opcodes { |
37 |
||
38 |
protected Class<?> getClass(String className) throws ClassNotFoundException { |
|
39 |
return new CachedLoader(CustomizedBytecodePatternTest.class.getClassLoader(), className).findClass(className); |
|
40 |
} |
|
41 |
||
55619
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
42 |
/** |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
43 |
* @param className |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
44 |
* @param lookUp lookup object with boot class load capability (required for jdk 9 and above) |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
45 |
* @return loaded class |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
46 |
* @throws ClassNotFoundException |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
47 |
*/ |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
48 |
protected Class<?> getClassBL(String className, MethodHandles.Lookup lookUp) throws ClassNotFoundException { |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
49 |
byte[] gen = generateClass(className.replace('.', '/')); |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
50 |
Method defineClass = null; |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
51 |
Class<?> loadedClass = null; |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
52 |
try { |
55509 | 53 |
if (JavaVersionUtil.JAVA_SPEC <= 8) { |
55619
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
54 |
defineClass = Unsafe.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class); |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
55 |
loadedClass = (Class<?>) defineClass.invoke(UNSAFE, className, gen, 0, gen.length, null, null); |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
56 |
} else { |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
57 |
defineClass = MethodHandles.lookup().getClass().getDeclaredMethod("defineClass", byte[].class); |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
58 |
loadedClass = (Class<?>) defineClass.invoke(lookUp, gen); |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
59 |
} |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
60 |
} catch (Exception e) { |
58299 | 61 |
throw new ClassNotFoundException(className, e); |
55619
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
62 |
} |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
63 |
return loadedClass; |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
64 |
} |
3595fb8794c5
8221577: [Graal] Implement basic type consistency checks for Low level MH intrinsics
jcm
parents:
50858
diff
changeset
|
65 |
|
50609 | 66 |
private class CachedLoader extends ClassLoader { |
67 |
||
68 |
final String className; |
|
69 |
Class<?> loaded; |
|
70 |
||
71 |
CachedLoader(ClassLoader parent, String className) { |
|
72 |
super(parent); |
|
73 |
this.className = className; |
|
74 |
} |
|
75 |
||
76 |
@Override |
|
77 |
protected Class<?> findClass(String name) throws ClassNotFoundException { |
|
78 |
if (name.equals(className)) { |
|
79 |
if (loaded == null) { |
|
80 |
byte[] gen = generateClass(name.replace('.', '/')); |
|
81 |
loaded = defineClass(name, gen, 0, gen.length); |
|
82 |
} |
|
83 |
return loaded; |
|
84 |
} else { |
|
85 |
return super.findClass(name); |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
} |
|
90 |
||
91 |
protected abstract byte[] generateClass(String internalClassName); |
|
92 |
||
93 |
} |