8020981: Update methods of java.lang.reflect.Parameter to throw correct exceptions

Fix behavior of parameter reflection API for malformed class files.

Reviewed-by: darcy
This commit is contained in:
Eric McCorkle 2013-10-02 19:13:42 -04:00
parent 47f46da3fa
commit 367fa5ac0e
4 changed files with 563 additions and 4 deletions

View File

@ -286,12 +286,14 @@ public abstract class Executable extends AccessibleObject
* this object. Returns an array of length 0 if the executable
* has no parameters.
*
* The parameters of the underlying executable do not necessarily
* <p>The parameters of the underlying executable do not necessarily
* have unique names, or names that are legal identifiers in the
* Java programming language (JLS 3.8).
*
* @throws MalformedParametersException if the class file contains
* a MethodParameters attribute that is improperly formatted.
* @return an array of {@code Parameter} objects representing all
* the parameters to the executable this object represents
* the parameters to the executable this object represents.
*/
public Parameter[] getParameters() {
// TODO: This may eventually need to be guarded by security
@ -315,6 +317,30 @@ public abstract class Executable extends AccessibleObject
return out;
}
private void verifyParameters(final Parameter[] parameters) {
final int mask = Modifier.FINAL | Modifier.SYNTHETIC | Modifier.MANDATED;
if (getParameterTypes().length != parameters.length)
throw new MalformedParametersException("Wrong number of parameters in MethodParameters attribute");
for (Parameter parameter : parameters) {
final String name = parameter.getRealName();
final int mods = parameter.getModifiers();
if (name != null) {
if (name.isEmpty() || name.indexOf('.') != -1 ||
name.indexOf(';') != -1 || name.indexOf('[') != -1 ||
name.indexOf('/') != -1) {
throw new MalformedParametersException("Invalid parameter name \"" + name + "\"");
}
}
if (mods != (mods & mask)) {
throw new MalformedParametersException("Invalid parameter modifiers");
}
}
}
private Parameter[] privateGetParameters() {
// Use tmp to avoid multiple writes to a volatile.
Parameter[] tmp = parameters;
@ -322,7 +348,12 @@ public abstract class Executable extends AccessibleObject
if (tmp == null) {
// Otherwise, go to the JVM to get them
tmp = getParameters0();
try {
tmp = getParameters0();
} catch(IllegalArgumentException e) {
// Rethrow ClassFormatErrors
throw new MalformedParametersException("Invalid constant pool index");
}
// If we get back nothing, then synthesize parameters
if (tmp == null) {
@ -330,6 +361,7 @@ public abstract class Executable extends AccessibleObject
tmp = synthesizeAllParams();
} else {
hasRealParameterData = true;
verifyParameters(tmp);
}
parameters = tmp;

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang.reflect;
/**
* Thrown when {@link java.lang.reflect.Executable#getParameters the
* java.lang.reflect package} attempts to read method parameters from
* a class file and determines that one or more parameters are
* malformed.
*
* <p>The following is a list of conditions under which this exception
* can be thrown:
* <ul>
* <li> The number of parameters (parameter_count) is wrong for the method
* <li> A constant pool index is out of bounds.
* <li> A constant pool index does not refer to a UTF-8 entry
* <li> A parameter's name is "", or contains an illegal character
* <li> The flags field contains an illegal flag (something other than
* FINAL, SYNTHETIC, or MANDATED)
* </ul>
*
* See {@link java.lang.reflect.Executable#getParameters} for more
* information.
*
* @see java.lang.reflect.Executable#getParameters
* @since 1.8
*/
public class MalformedParametersException extends RuntimeException {
private static final long serialVersionUID = 20130919L;
public MalformedParametersException() {}
public MalformedParametersException(String reason) {
super(reason);
}
}

View File

@ -104,7 +104,7 @@ public final class Parameter implements AnnotatedElement {
* to the class file.
*/
public boolean isNamePresent() {
return executable.hasRealParameterData();
return executable.hasRealParameterData() && name != null;
}
/**
@ -182,6 +182,11 @@ public final class Parameter implements AnnotatedElement {
return name;
}
// Package-private accessor to the real name field.
String getRealName() {
return name;
}
/**
* Returns a {@code Type} object that identifies the parameterized
* type for the parameter represented by this {@code Parameter}

View File

@ -0,0 +1,462 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @run main BadClassFiles
* @summary The reflection API should throw the correct exceptions.
*/
import java.lang.Class;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.MalformedParametersException;
import java.lang.ClassLoader;
import java.lang.ClassNotFoundException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class BadClassFiles {
private int errors = 0;
/* Class files were created by compiling the following source and
* then editing it:
*
* public class EmptyName {
* public void m(int a, int b) {}
* }
*
*/
private static final byte[] EmptyName_bytes = {
-54,-2,-70,-66,0,0,0,52,
0,18,10,0,3,0,15,7,
0,16,7,0,17,1,0,6,
60,105,110,105,116,62,1,0,
3,40,41,86,1,0,4,67,
111,100,101,1,0,15,76,105,
110,101,78,117,109,98,101,114,
84,97,98,108,101,1,0,1,
109,1,0,5,40,73,73,41,
86,1,0,16,77,101,116,104,
111,100,80,97,114,97,109,101,
116,101,114,115,1,0,0,1,
0,1,98,1,0,10,83,111,
117,114,99,101,70,105,108,101,
1,0,14,69,109,112,116,121,
78,97,109,101,46,106,97,118,
97,12,0,4,0,5,1,0,
9,69,109,112,116,121,78,97,
109,101,1,0,16,106,97,118,
97,47,108,97,110,103,47,79,
98,106,101,99,116,0,33,0,
2,0,3,0,0,0,0,0,
2,0,1,0,4,0,5,0,
1,0,6,0,0,0,29,0,
1,0,1,0,0,0,5,42,
-73,0,1,-79,0,0,0,1,
0,7,0,0,0,6,0,1,
0,0,0,1,0,1,0,8,
0,9,0,2,0,6,0,0,
0,25,0,0,0,3,0,0,
0,1,-79,0,0,0,1,0,
7,0,0,0,6,0,1,0,
0,0,2,0,10,0,0,0,
9,2,0,11,0,0,0,12,
0,0,0,1,0,13,0,0,
0,2,0,14
};
private static final byte[] BadModifiers_bytes = {
-54,-2,-70,-66,0,0,0,52,
0,18,10,0,3,0,15,7,
0,16,7,0,17,1,0,6,
60,105,110,105,116,62,1,0,
3,40,41,86,1,0,4,67,
111,100,101,1,0,15,76,105,
110,101,78,117,109,98,101,114,
84,97,98,108,101,1,0,1,
109,1,0,5,40,73,73,41,
86,1,0,16,77,101,116,104,
111,100,80,97,114,97,109,101,
116,101,114,115,1,0,1,97,
1,0,1,98,1,0,10,83,
111,117,114,99,101,70,105,108,
101,1,0,17,66,97,100,77,
111,100,105,102,105,101,114,115,
46,106,97,118,97,12,0,4,
0,5,1,0,12,66,97,100,
77,111,100,105,102,105,101,114,
115,1,0,16,106,97,118,97,
47,108,97,110,103,47,79,98,
106,101,99,116,0,33,0,2,
0,3,0,0,0,0,0,2,
0,1,0,4,0,5,0,1,
0,6,0,0,0,29,0,1,
0,1,0,0,0,5,42,-73,
0,1,-79,0,0,0,1,0,
7,0,0,0,6,0,1,0,
0,0,1,0,1,0,8,0,
9,0,2,0,6,0,0,0,
25,0,0,0,3,0,0,0,
1,-79,0,0,0,1,0,7,
0,0,0,6,0,1,0,0,
0,2,0,10,0,0,0,9,
2,0,11,0,51,51,12,0,
0,0,1,0,13,0,0,0,
2,0,14
};
private static final byte[] BadNameIndex_bytes = {
-54,-2,-70,-66,0,0,0,52,
0,18,10,0,3,0,15,7,
0,16,7,0,17,1,0,6,
60,105,110,105,116,62,1,0,
3,40,41,86,1,0,4,67,
111,100,101,1,0,15,76,105,
110,101,78,117,109,98,101,114,
84,97,98,108,101,1,0,1,
109,1,0,5,40,73,73,41,
86,1,0,16,77,101,116,104,
111,100,80,97,114,97,109,101,
116,101,114,115,1,0,1,97,
1,0,1,98,1,0,10,83,
111,117,114,99,101,70,105,108,
101,1,0,17,66,97,100,78,
97,109,101,73,110,100,101,120,
46,106,97,118,97,12,0,4,
0,5,1,0,12,66,97,100,
78,97,109,101,73,110,100,101,
120,1,0,16,106,97,118,97,
47,108,97,110,103,47,79,98,
106,101,99,116,0,33,0,2,
0,3,0,0,0,0,0,2,
0,1,0,4,0,5,0,1,
0,6,0,0,0,29,0,1,
0,1,0,0,0,5,42,-73,
0,1,-79,0,0,0,1,0,
7,0,0,0,6,0,1,0,
0,0,1,0,1,0,8,0,
9,0,2,0,6,0,0,0,
25,0,0,0,3,0,0,0,
1,-79,0,0,0,1,0,7,
0,0,0,6,0,1,0,0,
0,2,0,10,0,0,0,9,
2,0,1,0,0,0,12,0,
0,0,1,0,13,0,0,0,
2,0,14
};
private static final byte[] NameIndexOutOfBounds_bytes = {
-54,-2,-70,-66,0,0,0,52,
0,18,10,0,3,0,15,7,
0,16,7,0,17,1,0,6,
60,105,110,105,116,62,1,0,
3,40,41,86,1,0,4,67,
111,100,101,1,0,15,76,105,
110,101,78,117,109,98,101,114,
84,97,98,108,101,1,0,1,
109,1,0,5,40,73,73,41,
86,1,0,16,77,101,116,104,
111,100,80,97,114,97,109,101,
116,101,114,115,1,0,1,97,
1,0,1,98,1,0,10,83,
111,117,114,99,101,70,105,108,
101,1,0,25,78,97,109,101,
73,110,100,101,120,79,117,116,
79,102,66,111,117,110,100,115,
46,106,97,118,97,12,0,4,
0,5,1,0,20,78,97,109,
101,73,110,100,101,120,79,117,
116,79,102,66,111,117,110,100,
115,1,0,16,106,97,118,97,
47,108,97,110,103,47,79,98,
106,101,99,116,0,33,0,2,
0,3,0,0,0,0,0,2,
0,1,0,4,0,5,0,1,
0,6,0,0,0,29,0,1,
0,1,0,0,0,5,42,-73,
0,1,-79,0,0,0,1,0,
7,0,0,0,6,0,1,0,
0,0,1,0,1,0,8,0,
9,0,2,0,6,0,0,0,
25,0,0,0,3,0,0,0,
1,-79,0,0,0,1,0,7,
0,0,0,6,0,1,0,0,
0,2,0,10,0,0,0,9,
2,0,-1,0,0,0,12,0,
0,0,1,0,13,0,0,0,
2,0,14
};
private static final byte[] ExtraParams_bytes = {
-54,-2,-70,-66,0,0,0,52,
0,18,10,0,3,0,15,7,
0,16,7,0,17,1,0,6,
60,105,110,105,116,62,1,0,
3,40,41,86,1,0,4,67,
111,100,101,1,0,15,76,105,
110,101,78,117,109,98,101,114,
84,97,98,108,101,1,0,1,
109,1,0,5,40,73,73,41,
86,1,0,16,77,101,116,104,
111,100,80,97,114,97,109,101,
116,101,114,115,1,0,1,97,
1,0,1,98,1,0,10,83,
111,117,114,99,101,70,105,108,
101,1,0,16,69,120,116,114,
97,80,97,114,97,109,115,46,
106,97,118,97,12,0,4,0,
5,1,0,11,69,120,116,114,
97,80,97,114,97,109,115,1,
0,16,106,97,118,97,47,108,
97,110,103,47,79,98,106,101,
99,116,0,33,0,2,0,3,
0,0,0,0,0,2,0,1,
0,4,0,5,0,1,0,6,
0,0,0,29,0,1,0,1,
0,0,0,5,42,-73,0,1,
-79,0,0,0,1,0,7,0,
0,0,6,0,1,0,0,0,
1,0,1,0,8,0,9,0,
2,0,6,0,0,0,25,0,
0,0,3,0,0,0,1,-79,
0,0,0,1,0,7,0,0,
0,6,0,1,0,0,0,2,
0,10,0,0,0,13,3,0,
11,0,0,0,12,0,0,0,
11,0,0,0,1,0,13,0,
0,0,2,0,14
};
private static final byte[] BadName1_bytes = {
-54,-2,-70,-66,0,0,0,52,
0,18,10,0,3,0,15,7,
0,16,7,0,17,1,0,6,
60,105,110,105,116,62,1,0,
3,40,41,86,1,0,4,67,
111,100,101,1,0,15,76,105,
110,101,78,117,109,98,101,114,
84,97,98,108,101,1,0,1,
109,1,0,5,40,73,73,41,
86,1,0,16,77,101,116,104,
111,100,80,97,114,97,109,101,
116,101,114,115,1,0,1,46,
1,0,1,98,1,0,10,83,
111,117,114,99,101,70,105,108,
101,1,0,13,66,97,100,78,
97,109,101,49,46,106,97,118,
97,12,0,4,0,5,1,0,
8,66,97,100,78,97,109,101,
49,1,0,16,106,97,118,97,
47,108,97,110,103,47,79,98,
106,101,99,116,0,33,0,2,
0,3,0,0,0,0,0,2,
0,1,0,4,0,5,0,1,
0,6,0,0,0,29,0,1,
0,1,0,0,0,5,42,-73,
0,1,-79,0,0,0,1,0,
7,0,0,0,6,0,1,0,
0,0,1,0,1,0,8,0,
9,0,2,0,6,0,0,0,
25,0,0,0,3,0,0,0,
1,-79,0,0,0,1,0,7,
0,0,0,6,0,1,0,0,
0,2,0,10,0,0,0,9,
2,0,11,0,0,0,12,0,
0,0,1,0,13,0,0,0,
2,0,14
};
private static final byte[] BadName2_bytes = {
-54,-2,-70,-66,0,0,0,52,
0,18,10,0,3,0,15,7,
0,16,7,0,17,1,0,6,
60,105,110,105,116,62,1,0,
3,40,41,86,1,0,4,67,
111,100,101,1,0,15,76,105,
110,101,78,117,109,98,101,114,
84,97,98,108,101,1,0,1,
109,1,0,5,40,73,73,41,
86,1,0,16,77,101,116,104,
111,100,80,97,114,97,109,101,
116,101,114,115,1,0,1,91,
1,0,1,98,1,0,10,83,
111,117,114,99,101,70,105,108,
101,1,0,13,66,97,100,78,
97,109,101,50,46,106,97,118,
97,12,0,4,0,5,1,0,
8,66,97,100,78,97,109,101,
50,1,0,16,106,97,118,97,
47,108,97,110,103,47,79,98,
106,101,99,116,0,33,0,2,
0,3,0,0,0,0,0,2,
0,1,0,4,0,5,0,1,
0,6,0,0,0,29,0,1,
0,1,0,0,0,5,42,-73,
0,1,-79,0,0,0,1,0,
7,0,0,0,6,0,1,0,
0,0,1,0,1,0,8,0,
9,0,2,0,6,0,0,0,
25,0,0,0,3,0,0,0,
1,-79,0,0,0,1,0,7,
0,0,0,6,0,1,0,0,
0,2,0,10,0,0,0,9,
2,0,11,0,0,0,12,0,
0,0,1,0,13,0,0,0,
2,0,14
};
private static final byte[] BadName3_bytes = {
-54,-2,-70,-66,0,0,0,52,
0,18,10,0,3,0,15,7,
0,16,7,0,17,1,0,6,
60,105,110,105,116,62,1,0,
3,40,41,86,1,0,4,67,
111,100,101,1,0,15,76,105,
110,101,78,117,109,98,101,114,
84,97,98,108,101,1,0,1,
109,1,0,5,40,73,73,41,
86,1,0,16,77,101,116,104,
111,100,80,97,114,97,109,101,
116,101,114,115,1,0,1,59,
1,0,1,98,1,0,10,83,
111,117,114,99,101,70,105,108,
101,1,0,13,66,97,100,78,
97,109,101,51,46,106,97,118,
97,12,0,4,0,5,1,0,
8,66,97,100,78,97,109,101,
51,1,0,16,106,97,118,97,
47,108,97,110,103,47,79,98,
106,101,99,116,0,33,0,2,
0,3,0,0,0,0,0,2,
0,1,0,4,0,5,0,1,
0,6,0,0,0,29,0,1,
0,1,0,0,0,5,42,-73,
0,1,-79,0,0,0,1,0,
7,0,0,0,6,0,1,0,
0,0,1,0,1,0,8,0,
9,0,2,0,6,0,0,0,
25,0,0,0,3,0,0,0,
1,-79,0,0,0,1,0,7,
0,0,0,6,0,1,0,0,
0,2,0,10,0,0,0,9,
2,0,11,0,0,0,12,0,
0,0,1,0,13,0,0,0,
2,0,14
};
private static final byte[] BadName4_bytes = {
-54,-2,-70,-66,0,0,0,52,
0,18,10,0,3,0,15,7,
0,16,7,0,17,1,0,6,
60,105,110,105,116,62,1,0,
3,40,41,86,1,0,4,67,
111,100,101,1,0,15,76,105,
110,101,78,117,109,98,101,114,
84,97,98,108,101,1,0,1,
109,1,0,5,40,73,73,41,
86,1,0,16,77,101,116,104,
111,100,80,97,114,97,109,101,
116,101,114,115,1,0,1,47,
1,0,1,98,1,0,10,83,
111,117,114,99,101,70,105,108,
101,1,0,13,66,97,100,78,
97,109,101,52,46,106,97,118,
97,12,0,4,0,5,1,0,
8,66,97,100,78,97,109,101,
52,1,0,16,106,97,118,97,
47,108,97,110,103,47,79,98,
106,101,99,116,0,33,0,2,
0,3,0,0,0,0,0,2,
0,1,0,4,0,5,0,1,
0,6,0,0,0,29,0,1,
0,1,0,0,0,5,42,-73,
0,1,-79,0,0,0,1,0,
7,0,0,0,6,0,1,0,
0,0,1,0,1,0,8,0,
9,0,2,0,6,0,0,0,
25,0,0,0,3,0,0,0,
1,-79,0,0,0,1,0,7,
0,0,0,6,0,1,0,0,
0,2,0,10,0,0,0,9,
2,0,11,0,0,0,12,0,
0,0,1,0,13,0,0,0,
2,0,14
};
private static class InMemoryClassLoader extends ClassLoader {
public Class<?> defineClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}
};
private static final InMemoryClassLoader loader = new InMemoryClassLoader();
private final Class<?>[] classes;
private BadClassFiles() throws ClassNotFoundException {
classes = new Class<?>[] {
loader.defineClass("EmptyName", EmptyName_bytes),
loader.defineClass("BadModifiers", BadModifiers_bytes),
loader.defineClass("BadNameIndex", BadNameIndex_bytes),
loader.defineClass("NameIndexOutOfBounds", NameIndexOutOfBounds_bytes),
loader.defineClass("ExtraParams", ExtraParams_bytes),
// Name with .
loader.defineClass("BadName1", BadName1_bytes),
// Name with [
loader.defineClass("BadName2", BadName2_bytes),
// Name with ;
loader.defineClass("BadName3", BadName3_bytes),
// Name with /
loader.defineClass("BadName4", BadName4_bytes)
};
}
public static void main(String... args)
throws NoSuchMethodException, IOException, ClassNotFoundException {
new BadClassFiles().run();
}
public void assertBadParameters(Class<?> cls) throws NoSuchMethodException {
try {
System.err.println("Trying " + cls);
final Method method = cls.getMethod("m", int.class, int.class);
final Parameter[] params = method.getParameters();
System.err.println("Name " + params[0].getName());
System.err.println("Did not see expected exception");
errors++;
} catch(MalformedParametersException e) {
System.err.println("Expected exception seen");
}
}
public void run() throws NoSuchMethodException {
for (Class<?> cls : classes)
assertBadParameters(cls);
if (errors != 0)
throw new RuntimeException(errors + " errors in test");
}
}