jdk.internal.dynalink.support.CallSiteDescriptorFactory Java Examples

The following examples show how to use jdk.internal.dynalink.support.CallSiteDescriptorFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSObjectLinker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            if (c > 2) {
                return findGetMethod(desc);
            }
        // For indexed get, we want get GuardedInvocation beans linker and pass it.
        // JSObjectLinker.get uses this fallback getter for explicit signature method access.
        return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices));
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example #2
Source File: BrowserJSObjectLinker.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();
    GuardedInvocation inv;
    try {
        inv = nashornBeansLinker.getGuardedInvocation(request, linkerServices);
    } catch (Throwable th) {
        inv = null;
    }

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            return c > 2? findGetMethod(desc, inv) : findGetIndexMethod(inv);
        case "setProp":
        case "setElem":
            return c > 2? findSetMethod(desc, inv) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        default:
            return null;
    }
}
 
Example #3
Source File: JSObjectLinker.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static GuardedInvocation lookup(final CallSiteDescriptor desc) {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();
    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            return c > 2 ? findGetMethod(desc) : findGetIndexMethod();
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example #4
Source File: DynamicMethodLinker.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) {
    final Object receiver = linkRequest.getReceiver();
    if(!(receiver instanceof DynamicMethod)) {
        return null;
    }
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") {
        return null;
    }
    final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
    if(operator == "call") {
        final MethodHandle invocation = ((DynamicMethod)receiver).getInvocation(
                CallSiteDescriptorFactory.dropParameterTypes(desc, 0, 1), linkerServices);
        if(invocation == null) {
            return null;
        }
        return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
                desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
    }
    return null;
}
 
Example #5
Source File: ReflectionCheckLinker.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void checkLinkRequest(final LinkRequest origRequest) {
    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        final LinkRequest requestWithoutContext = origRequest.withoutRuntimeContext(); // Nashorn has no runtime context
        final Object self = requestWithoutContext.getReceiver();
        // allow 'static' access on Class objects representing public classes of non-restricted packages
        if ((self instanceof Class) && Modifier.isPublic(((Class<?>)self).getModifiers())) {
            final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();
            if(CallSiteDescriptorFactory.tokenizeOperators(desc).contains("getProp")) {
                if ("static".equals(desc.getNameToken(CallSiteDescriptor.NAME_OPERAND))) {
                    if (Context.isAccessibleClass((Class<?>)self) && !isReflectionClass((Class<?>)self)) {

                        // If "getProp:static" passes access checks, allow access.
                        return;
                    }
                }
            }
        }
        checkReflectionPermission(sm);
    }
}
 
Example #6
Source File: AbstractJavaLinker.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)
        throws Exception {
    final LinkRequest ncrequest = request.withoutRuntimeContext();
    // BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn".
    final CallSiteDescriptor callSiteDescriptor = ncrequest.getCallSiteDescriptor();
    final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]).
    if("callMethod" == op) {
        return getCallPropWithThis(callSiteDescriptor, linkerServices);
    }
    List<String> operations = CallSiteDescriptorFactory.tokenizeOperators(callSiteDescriptor);
    while(!operations.isEmpty()) {
        final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices,
                operations);
        if(gic != null) {
            return gic.getGuardedInvocation();
        }
        operations = pop(operations);
    }
    return null;
}
 
Example #7
Source File: JSObjectLinker.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
private static GuardedInvocation lookup(final CallSiteDescriptor desc) {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();
    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            return c > 2 ? findGetMethod(desc) : findGetIndexMethod();
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc, operator);
        case "callMethod":
            return findCallMethodMethod(desc, operator);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example #8
Source File: AbstractJavaLinker.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest request, final LinkerServices linkerServices)
        throws Exception {
    final LinkRequest ncrequest = request.withoutRuntimeContext();
    // BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn".
    final CallSiteDescriptor callSiteDescriptor = ncrequest.getCallSiteDescriptor();
    final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]).
    if("callMethod" == op) {
        return getCallPropWithThis(callSiteDescriptor, linkerServices);
    }
    List<String> operations = CallSiteDescriptorFactory.tokenizeOperators(callSiteDescriptor);
    while(!operations.isEmpty()) {
        final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices,
                operations);
        if(gic != null) {
            return gic.getGuardedInvocation();
        }
        operations = pop(operations);
    }
    return null;
}
 
Example #9
Source File: DynamicMethodLinker.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) {
    final Object receiver = linkRequest.getReceiver();
    if(!(receiver instanceof DynamicMethod)) {
        return null;
    }
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") {
        return null;
    }
    final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
    if(operator == "call") {
        final MethodHandle invocation = ((DynamicMethod)receiver).getInvocation(
                CallSiteDescriptorFactory.dropParameterTypes(desc, 0, 1), linkerServices);
        if(invocation == null) {
            return null;
        }
        return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
                desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
    }
    return null;
}
 
Example #10
Source File: AbstractJavaLinker.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest request, final LinkerServices linkerServices)
        throws Exception {
    final LinkRequest ncrequest = request.withoutRuntimeContext();
    // BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn".
    final CallSiteDescriptor callSiteDescriptor = ncrequest.getCallSiteDescriptor();
    final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]).
    if("callMethod" == op) {
        return getCallPropWithThis(callSiteDescriptor, linkerServices);
    }
    List<String> operations = CallSiteDescriptorFactory.tokenizeOperators(callSiteDescriptor);
    while(!operations.isEmpty()) {
        final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices,
                operations);
        if(gic != null) {
            return gic.getGuardedInvocation();
        }
        operations = pop(operations);
    }
    return null;
}
 
Example #11
Source File: JSObjectLinker.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            if (c > 2) {
                return findGetMethod(desc);
            }
        // For indexed get, we want get GuardedInvocation beans linker and pass it.
        // JSObjectLinker.get uses this fallback getter for explicit signature method access.
        return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices));
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example #12
Source File: BrowserJSObjectLinker.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();
    GuardedInvocation inv;
    try {
        inv = nashornBeansLinker.getGuardedInvocation(request, linkerServices);
    } catch (Throwable th) {
        inv = null;
    }

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            return c > 2? findGetMethod(desc, inv) : findGetIndexMethod(inv);
        case "setProp":
        case "setElem":
            return c > 2? findSetMethod(desc, inv) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        default:
            return null;
    }
}
 
Example #13
Source File: JSObjectLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            if (c > 2) {
                return findGetMethod(desc);
            }
        // For indexed get, we want get GuardedInvocation beans linker and pass it.
        // JSObjectLinker.get uses this fallback getter for explicit signature method access.
        return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices));
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example #14
Source File: JSObjectLinker.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static GuardedInvocation lookup(final CallSiteDescriptor desc) {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();
    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            return c > 2 ? findGetMethod(desc) : findGetIndexMethod();
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example #15
Source File: ReflectionCheckLinker.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static void checkLinkRequest(final LinkRequest origRequest) {
    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        final LinkRequest requestWithoutContext = origRequest.withoutRuntimeContext(); // Nashorn has no runtime context
        final Object self = requestWithoutContext.getReceiver();
        // allow 'static' access on Class objects representing public classes of non-restricted packages
        if ((self instanceof Class) && Modifier.isPublic(((Class<?>)self).getModifiers())) {
            final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();
            if(CallSiteDescriptorFactory.tokenizeOperators(desc).contains("getProp")) {
                if ("static".equals(desc.getNameToken(CallSiteDescriptor.NAME_OPERAND))) {
                    if (Context.isAccessibleClass((Class<?>)self) && !isReflectionClass((Class<?>)self)) {

                        // If "getProp:static" passes access checks, allow access.
                        return;
                    }
                }
            }
        }
        checkReflectionPermission(sm);
    }
}
 
Example #16
Source File: AbstractJavaLinker.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)
        throws Exception {
    final LinkRequest ncrequest = request.withoutRuntimeContext();
    // BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn".
    final CallSiteDescriptor callSiteDescriptor = ncrequest.getCallSiteDescriptor();
    final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]).
    if("callMethod" == op) {
        return getCallPropWithThis(callSiteDescriptor, linkerServices);
    }
    List<String> operations = CallSiteDescriptorFactory.tokenizeOperators(callSiteDescriptor);
    while(!operations.isEmpty()) {
        final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices,
                operations);
        if(gic != null) {
            return gic.getGuardedInvocation();
        }
        operations = pop(operations);
    }
    return null;
}
 
Example #17
Source File: DynamicMethodLinker.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) {
    final Object receiver = linkRequest.getReceiver();
    if(!(receiver instanceof DynamicMethod)) {
        return null;
    }
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") {
        return null;
    }
    final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
    if(operator == "call") {
        final MethodHandle invocation = ((DynamicMethod)receiver).getInvocation(
                CallSiteDescriptorFactory.dropParameterTypes(desc, 0, 1), linkerServices);
        if(invocation == null) {
            return null;
        }
        return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
                desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
    }
    return null;
}
 
Example #18
Source File: JSObjectLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            if (c > 2) {
                return findGetMethod(desc);
            }
        // For indexed get, we want get GuardedInvocation beans linker and pass it.
        // JSObjectLinker.get uses this fallback getter for explicit signature method access.
        return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices));
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example #19
Source File: AbstractJavaLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)
        throws Exception {
    final LinkRequest ncrequest = request.withoutRuntimeContext();
    // BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn".
    final CallSiteDescriptor callSiteDescriptor = ncrequest.getCallSiteDescriptor();
    final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]).
    if("callMethod" == op) {
        return getCallPropWithThis(callSiteDescriptor, linkerServices);
    }
    List<String> operations = CallSiteDescriptorFactory.tokenizeOperators(callSiteDescriptor);
    while(!operations.isEmpty()) {
        final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices,
                operations);
        if(gic != null) {
            return gic.getGuardedInvocation();
        }
        operations = pop(operations);
    }
    return null;
}
 
Example #20
Source File: JSObjectLinker.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            if (c > 2) {
                return findGetMethod(desc);
            }
        // For indexed get, we want get GuardedInvocation beans linker and pass it.
        // JSObjectLinker.get uses this fallback getter for explicit signature method access.
        return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices));
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example #21
Source File: AbstractJavaLinker.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest request, final LinkerServices linkerServices)
        throws Exception {
    final LinkRequest ncrequest = request.withoutRuntimeContext();
    // BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn".
    final CallSiteDescriptor callSiteDescriptor = ncrequest.getCallSiteDescriptor();
    final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]).
    if("callMethod" == op) {
        return getCallPropWithThis(callSiteDescriptor, linkerServices);
    }
    List<String> operations = CallSiteDescriptorFactory.tokenizeOperators(callSiteDescriptor);
    while(!operations.isEmpty()) {
        final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices,
                operations);
        if(gic != null) {
            return gic.getGuardedInvocation();
        }
        operations = pop(operations);
    }
    return null;
}
 
Example #22
Source File: BrowserJSObjectLinker.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();
    GuardedInvocation inv;
    try {
        inv = nashornBeansLinker.getGuardedInvocation(request, linkerServices);
    } catch (Throwable th) {
        inv = null;
    }

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            return c > 2? findGetMethod(desc, inv) : findGetIndexMethod(inv);
        case "setProp":
        case "setElem":
            return c > 2? findSetMethod(desc, inv) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        default:
            return null;
    }
}
 
Example #23
Source File: AbstractJavaLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)
        throws Exception {
    final LinkRequest ncrequest = request.withoutRuntimeContext();
    // BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn".
    final CallSiteDescriptor callSiteDescriptor = ncrequest.getCallSiteDescriptor();
    final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]).
    if("callMethod" == op) {
        return getCallPropWithThis(callSiteDescriptor, linkerServices);
    }
    List<String> operations = CallSiteDescriptorFactory.tokenizeOperators(callSiteDescriptor);
    while(!operations.isEmpty()) {
        final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices,
                operations);
        if(gic != null) {
            return gic.getGuardedInvocation();
        }
        operations = pop(operations);
    }
    return null;
}
 
Example #24
Source File: ReflectionCheckLinker.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
private static void checkLinkRequest(final LinkRequest origRequest) {
    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        final LinkRequest requestWithoutContext = origRequest.withoutRuntimeContext(); // Nashorn has no runtime context
        final Object self = requestWithoutContext.getReceiver();
        // allow 'static' access on Class objects representing public classes of non-restricted packages
        if ((self instanceof Class) && Modifier.isPublic(((Class<?>)self).getModifiers())) {
            final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();
            if(CallSiteDescriptorFactory.tokenizeOperators(desc).contains("getProp")) {
                if ("static".equals(desc.getNameToken(CallSiteDescriptor.NAME_OPERAND))) {
                    if (Context.isAccessibleClass((Class<?>)self) && !isReflectionClass((Class<?>)self)) {
                        // If "getProp:static" passes access checks, allow access.
                        return;
                    }
                }
            }
        }
        checkReflectionPermission(sm);
    }
}
 
Example #25
Source File: BrowserJSObjectLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();
    GuardedInvocation inv;
    try {
        inv = nashornBeansLinker.getGuardedInvocation(request, linkerServices);
    } catch (Throwable th) {
        inv = null;
    }

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            return c > 2? findGetMethod(desc, inv) : findGetIndexMethod(inv);
        case "setProp":
        case "setElem":
            return c > 2? findSetMethod(desc, inv) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        default:
            return null;
    }
}
 
Example #26
Source File: DynamicMethodLinker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) {
    final Object receiver = linkRequest.getReceiver();
    if(!(receiver instanceof DynamicMethod)) {
        return null;
    }
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") {
        return null;
    }
    final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
    final DynamicMethod dynMethod = (DynamicMethod)receiver;
    final boolean constructor = dynMethod.isConstructor();
    final MethodHandle invocation;

    if (operator == "call" && !constructor) {
        invocation = dynMethod.getInvocation(
                CallSiteDescriptorFactory.dropParameterTypes(desc, 0, 1), linkerServices);
    } else if (operator == "new" && constructor) {
        final MethodHandle ctorInvocation = dynMethod.getInvocation(desc, linkerServices);
        if(ctorInvocation == null) {
            return null;
        }

        // Insert null for StaticClass parameter
        invocation = MethodHandles.insertArguments(ctorInvocation, 0, (Object)null);
    } else {
        return null;
    }

    if (invocation != null) {
        return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
            desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
    }

    return null;
}
 
Example #27
Source File: DynamicMethodLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) {
    final Object receiver = linkRequest.getReceiver();
    if(!(receiver instanceof DynamicMethod)) {
        return null;
    }
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") {
        return null;
    }
    final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
    final DynamicMethod dynMethod = (DynamicMethod)receiver;
    final boolean constructor = dynMethod.isConstructor();
    final MethodHandle invocation;

    if (operator == "call" && !constructor) {
        invocation = dynMethod.getInvocation(
                CallSiteDescriptorFactory.dropParameterTypes(desc, 0, 1), linkerServices);
    } else if (operator == "new" && constructor) {
        final MethodHandle ctorInvocation = dynMethod.getInvocation(desc, linkerServices);
        if(ctorInvocation == null) {
            return null;
        }

        // Insert null for StaticClass parameter
        invocation = MethodHandles.insertArguments(ctorInvocation, 0, (Object)null);
    } else {
        return null;
    }

    if (invocation != null) {
        return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
            desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
    }

    return null;
}
 
Example #28
Source File: ScriptObject.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup method that, given a CallSiteDescriptor, looks up the target
 * MethodHandle and creates a GuardedInvocation
 * with the appropriate guard(s).
 *
 * @param desc call site descriptor
 * @param request the link request
 *
 * @return GuardedInvocation for the callsite
 */
public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) {
    final int c = desc.getNameTokenCount();
    // JavaScript is "immune" to all currently defined Dynalink composite operation - getProp is the same as getElem
    // is the same as getMethod as JavaScript objects have a single namespace for all three. Therefore, we don't
    // care about them, and just link to whatever is the first operation.
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    // NOTE: we support getElem and setItem as JavaScript doesn't distinguish items from properties. Nashorn itself
    // emits "dyn:getProp:identifier" for "<expr>.<identifier>" and "dyn:getElem" for "<expr>[<expr>]", but we are
    // more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the
    // operation has an associated name or not.
    switch (operator) {
    case "getProp":
    case "getElem":
    case "getMethod":
        return c > 2 ? findGetMethod(desc, request, operator) : findGetIndexMethod(desc, request);
    case "setProp":
    case "setElem":
        return c > 2 ? findSetMethod(desc, request) : findSetIndexMethod(desc, request);
    case "call":
        return findCallMethod(desc, request);
    case "new":
        return findNewMethod(desc, request);
    case "callMethod":
        return findCallMethodMethod(desc, request);
    default:
        return null;
    }
}
 
Example #29
Source File: Undefined.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup the appropriate method for an invoke dynamic call.
 * @param desc The invoke dynamic callsite descriptor.
 * @return GuardedInvocation to be invoked at call site.
 */
public static GuardedInvocation lookup(final CallSiteDescriptor desc) {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);

    switch (operator) {
    case "new":
    case "call":
        throw lookupTypeError("cant.call.undefined", desc);
    case "callMethod":
        throw lookupTypeError("cant.read.property.of.undefined", desc);
    // NOTE: we support getElem and setItem as JavaScript doesn't distinguish items from properties. Nashorn itself
    // emits "dyn:getProp:identifier" for "<expr>.<identifier>" and "dyn:getElem" for "<expr>[<expr>]", but we are
    // more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the
    // operation has an associated name or not.
    case "getProp":
    case "getElem":
    case "getMethod":
        if (desc.getNameTokenCount() < 3) {
            return findGetIndexMethod(desc);
        }
        throw lookupTypeError("cant.read.property.of.undefined", desc);
    case "setProp":
    case "setElem":
        if (desc.getNameTokenCount() < 3) {
            return findSetIndexMethod(desc);
        }
        throw lookupTypeError("cant.set.property.of.undefined", desc);
    default:
        break;
    }

    return null;
}
 
Example #30
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup method that, given a CallSiteDescriptor, looks up the target
 * MethodHandle and creates a GuardedInvocation
 * with the appropriate guard(s).
 *
 * @param desc call site descriptor
 * @param request the link request
 *
 * @return GuardedInvocation for the callsite
 */
public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) {
    final int c = desc.getNameTokenCount();
    // JavaScript is "immune" to all currently defined Dynalink composite operation - getProp is the same as getElem
    // is the same as getMethod as JavaScript objects have a single namespace for all three. Therefore, we don't
    // care about them, and just link to whatever is the first operation.
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    // NOTE: we support getElem and setItem as JavaScript doesn't distinguish items from properties. Nashorn itself
    // emits "dyn:getProp:identifier" for "<expr>.<identifier>" and "dyn:getElem" for "<expr>[<expr>]", but we are
    // more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the
    // operation has an associated name or not.
    switch (operator) {
    case "getProp":
    case "getElem":
    case "getMethod":
        return c > 2 ? findGetMethod(desc, request, operator) : findGetIndexMethod(desc, request);
    case "setProp":
    case "setElem":
        return c > 2 ? findSetMethod(desc, request) : findSetIndexMethod(desc);
    case "call":
        return findCallMethod(desc, request);
    case "new":
        return findNewMethod(desc);
    case "callMethod":
        return findCallMethodMethod(desc, request);
    default:
        return null;
    }
}