src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/CustomizedBytecodePatternTest.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58299 6df94ce3ab2f
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50609
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
     1
/*
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55621
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
50609
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
     4
 *
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
     7
 * published by the Free Software Foundation.
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
     8
 *
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    13
 * accompanied this code).
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    14
 *
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    18
 *
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    21
 * questions.
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    22
 */
50858
2d3e99a72541 8205824: Update Graal
never
parents: 50609
diff changeset
    23
2d3e99a72541 8205824: Update Graal
never
parents: 50609
diff changeset
    24
50609
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    25
package org.graalvm.compiler.core.test;
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    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
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 50858
diff changeset
    31
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
50609
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    32
import org.objectweb.asm.Opcodes;
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    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
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    36
public abstract class CustomizedBytecodePatternTest extends GraalCompilerTest implements Opcodes {
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    37
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    38
    protected Class<?> getClass(String className) throws ClassNotFoundException {
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    39
        return new CachedLoader(CustomizedBytecodePatternTest.class.getClassLoader(), className).findClass(className);
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    40
    }
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    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
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 50858
diff changeset
    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
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55621
diff changeset
    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
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    66
    private class CachedLoader extends ClassLoader {
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    67
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    68
        final String className;
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    69
        Class<?> loaded;
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    70
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    71
        CachedLoader(ClassLoader parent, String className) {
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    72
            super(parent);
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    73
            this.className = className;
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    74
        }
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    75
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    76
        @Override
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    77
        protected Class<?> findClass(String name) throws ClassNotFoundException {
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    78
            if (name.equals(className)) {
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    79
                if (loaded == null) {
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    80
                    byte[] gen = generateClass(name.replace('.', '/'));
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    81
                    loaded = defineClass(name, gen, 0, gen.length);
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    82
                }
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    83
                return loaded;
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    84
            } else {
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    85
                return super.findClass(name);
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    86
            }
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    87
        }
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    88
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    89
    }
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    90
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    91
    protected abstract byte[] generateClass(String internalClassName);
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    92
bf414874c28f 8204231: Update Graal
dlong
parents:
diff changeset
    93
}