Skip to content

Commit

Permalink
Merge pull request #2402 from JasonFengJ9/reflectaccess
Browse files Browse the repository at this point in the history
Skip parameter access check for reflect objects
  • Loading branch information
gacholio committed Jul 20, 2018
2 parents 930bbbe + 7efe916 commit ccde146
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public <T extends Member> T reflectAs(Class<T> expected, MethodHandles.Lookup lo
throw new NullPointerException();
}
try {
lookup.checkAccess(mh);
lookup.checkAccess(mh, false);
} catch (IllegalAccessException e) {
/*[MSG "K0583", "The Member is not accessible to the Lookup object"]*/
IllegalArgumentException x = new IllegalArgumentException(com.ibm.oti.util.Msg.getString("K0583")); //$NON-NLS-1$
Expand Down
47 changes: 24 additions & 23 deletions jcl/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public MethodHandle bind(Object receiver, String methodName, MethodType type) th
handle = handle.asFixedArity(); // remove unnecessary varargsCollector from the middle of the MH chain.
handle = convertToVarargsIfRequired(handle.bindTo(receiver));
}
checkAccess(handle);
checkAccess(handle, false);
checkSecurity(handle.getDefc(), receiverClass, handle.getModifiers());

handle = SecurityFrameInjector.wrapHandleWithInjectedSecurityFrameIfRequired(this, handle);
Expand Down Expand Up @@ -337,7 +337,7 @@ private static boolean isSamePackage(Class<?> a, Class<?> b){
return false;
}

void checkAccess(MethodHandle handle) throws IllegalAccessException {
void checkAccess(MethodHandle handle, boolean skipAccessCheckPara) throws IllegalAccessException {
if (INTERNAL_PRIVILEGED == accessMode) {
// Full access for use by MH implementation.
return;
Expand All @@ -347,11 +347,11 @@ void checkAccess(MethodHandle handle) throws IllegalAccessException {
throw new IllegalAccessException(this.toString());
}

checkAccess(handle.getDefc(), handle.getMethodName(), handle.getModifiers(), handle);
checkAccess(handle.getDefc(), handle.getMethodName(), handle.getModifiers(), handle, skipAccessCheckPara);
}

/*[IF Sidecar19-SE]*/
void checkAccess(VarHandle handle) throws IllegalAccessException {
void checkAccess(VarHandle handle, boolean reflectiveAccess) throws IllegalAccessException {
if (INTERNAL_PRIVILEGED == accessMode) {
// Full access for use by MH implementation.
return;
Expand All @@ -361,7 +361,7 @@ void checkAccess(VarHandle handle) throws IllegalAccessException {
throw new IllegalAccessException(this.toString());
}

checkAccess(handle.getDefiningClass(), handle.getFieldName(), handle.getModifiers(), null);
checkAccess(handle.getDefiningClass(), handle.getFieldName(), handle.getModifiers(), null, reflectiveAccess);
}

void checkAccess(Class<?> clazz) throws IllegalAccessException {
Expand Down Expand Up @@ -389,14 +389,15 @@ void checkAccess(Class<?> clazz) throws IllegalAccessException {
* The object is always {@link MethodHandle} in the current implementation, so in order
* to avoid extra type checking, the parameter is {@link MethodHandle}. If we need to
* handle {@link VarHandle} in the future, the type can be {@link Object}.
* @param skipAccessCheckPara skip access check for MethodType parameters
* @throws IllegalAccessException If the member is not accessible.
* @throws IllegalAccessError If a handle argument or return type is not accessible.
*/
private void checkAccess(Class<?> targetClass, String name, int memberModifiers, MethodHandle handle) throws IllegalAccessException {
private void checkAccess(Class<?> targetClass, String name, int memberModifiers, MethodHandle handle, boolean skipAccessCheckPara) throws IllegalAccessException {
checkClassAccess(targetClass);

/*[IF Sidecar19-SE]*/
if (null != handle) {
if (null != handle && !skipAccessCheckPara) {
MethodType type = handle.type();
Module accessModule = accessClass.getModule();

Expand Down Expand Up @@ -580,7 +581,7 @@ public MethodHandle findSpecial(Class<?> clazz, String methodName, MethodType ty
/*[MSG "K0586", "Lookup class ({0}) must be the same as or subclass of the current class ({1})"]*/
throw new IllegalAccessException(com.ibm.oti.util.Msg.getString("K0586", accessClass, handleDefc)); //$NON-NLS-1$
}
checkAccess(handle);
checkAccess(handle, false);
checkSecurity(handleDefc, clazz, handle.getModifiers());
handle = SecurityFrameInjector.wrapHandleWithInjectedSecurityFrameIfRequired(this, handle);
} catch (DefaultMethodConflictException e) {
Expand Down Expand Up @@ -641,7 +642,7 @@ public MethodHandle findStatic(Class<?> clazz, String methodName, MethodType typ
handle = new DirectHandle(clazz, methodName, type, MethodHandle.KIND_STATIC, null);
handle = HandleCache.putMethodInPerClassCache(cache, methodName, type, convertToVarargsIfRequired(handle));
}
checkAccess(handle);
checkAccess(handle, false);
checkSecurity(handle.getDefc(), clazz, handle.getModifiers());
/* Static check is performed by native code */
handle = SecurityFrameInjector.wrapHandleWithInjectedSecurityFrameIfRequired(this, handle);
Expand Down Expand Up @@ -717,7 +718,7 @@ public MethodHandle findVirtual(Class<?> clazz, String methodName, MethodType ty
HandleCache.putMethodInPerClassCache(cache, methodName, type, handle);
}
handle = restrictReceiver(handle);
checkAccess(handle);
checkAccess(handle, false);
checkSecurity(handle.getDefc(), clazz, handle.getModifiers());
handle = SecurityFrameInjector.wrapHandleWithInjectedSecurityFrameIfRequired(this, handle);
return handle;
Expand Down Expand Up @@ -924,7 +925,7 @@ public MethodHandle findGetter(Class<?> clazz, String fieldName, Class<?> fieldT
handle = new FieldGetterHandle(clazz, fieldName, fieldType, accessClass);
HandleCache.putFieldInPerClassCache(cache, fieldName, fieldType, handle);
}
checkAccess(handle);
checkAccess(handle, false);
checkSecurity(handle.getDefc(), clazz, handle.getModifiers());
return handle;
}
Expand All @@ -951,7 +952,7 @@ public MethodHandle findStaticGetter(Class<?> clazz, String fieldName, Class<?>
handle = new StaticFieldGetterHandle(clazz, fieldName, fieldType, accessClass);
HandleCache.putFieldInPerClassCache(cache, fieldName, fieldType, handle);
}
checkAccess(handle);
checkAccess(handle, false);
checkSecurity(handle.getDefc(), clazz, handle.getModifiers());
return handle;
}
Expand Down Expand Up @@ -986,7 +987,7 @@ public MethodHandle findSetter(Class<?> clazz, String fieldName, Class<?> fieldT
/*[MSG "K05cf", "illegal setter on final field"]*/
throw new IllegalAccessException(Msg.getString("K05cf")); //$NON-NLS-1$
}
checkAccess(handle);
checkAccess(handle, false);
checkSecurity(handle.getDefc(), clazz, handle.getModifiers());
return handle;
}
Expand Down Expand Up @@ -1021,7 +1022,7 @@ public MethodHandle findStaticSetter(Class<?> clazz, String fieldName, Class<?>
/*[MSG "K05cf", "illegal setter on final field"]*/
throw new IllegalAccessException(Msg.getString("K05cf")); //$NON-NLS-1$
}
checkAccess(handle);
checkAccess(handle, false);
checkSecurity(handle.getDefc(), clazz, handle.getModifiers());
return handle;
}
Expand Down Expand Up @@ -1235,7 +1236,7 @@ public MethodHandle unreflect(Method method) throws IllegalAccessException{

if (!method.isAccessible()) {
handle = restrictReceiver(handle);
checkAccess(handle);
checkAccess(handle, true);
}

handle = SecurityFrameInjector.wrapHandleWithInjectedSecurityFrameIfRequired(this, handle);
Expand Down Expand Up @@ -1319,7 +1320,7 @@ public MethodHandle unreflectConstructor(Constructor<?> method) throws IllegalAc
}

if (!method.isAccessible()) {
checkAccess(handle);
checkAccess(handle, true);
}

return handle;
Expand Down Expand Up @@ -1350,7 +1351,7 @@ public MethodHandle findConstructor(Class<?> declaringClass, MethodType type) th
}
handle = HandleCache.putMethodInPerClassCache(cache, "<init>", type, convertToVarargsIfRequired(handle)); //$NON-NLS-1$
}
checkAccess(handle);
checkAccess(handle, false);
checkSecurity(handle.getDefc(), declaringClass, handle.getModifiers());
return handle;
}
Expand Down Expand Up @@ -1382,7 +1383,7 @@ public MethodHandle unreflectSpecial(Method method, Class<?> specialToken) throw
}

if (!method.isAccessible()) {
checkAccess(handle);
checkAccess(handle, true);
}

handle = SecurityFrameInjector.wrapHandleWithInjectedSecurityFrameIfRequired(this, handle);
Expand Down Expand Up @@ -1427,7 +1428,7 @@ public MethodHandle unreflectGetter(Field field) throws IllegalAccessException {
HandleCache.putFieldInPerClassCache(cache, fieldName, fieldType, handle);
}
if (!field.isAccessible()) {
checkAccess(handle);
checkAccess(handle, true);
}
return handle;
}
Expand Down Expand Up @@ -1478,7 +1479,7 @@ public MethodHandle unreflectSetter(Field field) throws IllegalAccessException {
}

if (!field.isAccessible()) {
checkAccess(handle);
checkAccess(handle, true);
}
return handle;
}
Expand All @@ -1503,7 +1504,7 @@ public MethodHandleInfo revealDirect(MethodHandle target) throws IllegalArgument
}
}
try {
checkAccess(target);
checkAccess(target, false);
checkSecurity(target.getDefc(), target.getReferenceClass(), target.getModifiers());
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
Expand Down Expand Up @@ -1674,7 +1675,7 @@ private VarHandle findVarHandleCommon(Class<?> definingClass, String name, Class
} else {
handle = new InstanceFieldVarHandle(definingClass, name, type, this.accessClass);
}
checkAccess(handle);
checkAccess(handle, false);
checkSecurity(definingClass, definingClass, handle.getModifiers());
return handle;
} catch (NoSuchFieldError e) {
Expand Down Expand Up @@ -1710,7 +1711,7 @@ public VarHandle unreflectVarHandle(Field field) throws IllegalAccessException {
handle = new InstanceFieldVarHandle(field, field.getDeclaringClass(), field.getType());
}

checkAccess(handle);
checkAccess(handle, true);
checkSecurity(handle.getDefiningClass(), handle.getDefiningClass(), handle.modifiers);

return handle;
Expand Down

0 comments on commit ccde146

Please sign in to comment.