[PATCH] JDK-8159797 Throw a right IllegalArgumentException from the bytecode generated by Method/ConstructorAccessor (original) (raw)

Jaikiran Pai jai.forums2013 at gmail.com
Thu May 3 12:53:52 UTC 2018


Hi,

This mail contains a potential patch to the issue[1] that I had reported a couple of years back. The issue is still valid and reproducible with latest upstream JDK.

In brief, if a certain code has:

public void doSomething(String foo) {...}

and then it gets invoked through reflection:

thatMethod = // get hold of that method through reflection

thatMethod.invoke(validObjectInstance, null);

then as per the javadoc of the Method#invoke[2] you would expect an IllegalArgumentException (since the method expects 1 parameter and we are sending none). This does throw an IllegalArgumentException, but when the invocation happens through a (dynamically generated) MethodAccessor, instead of a native MethodAccessor, the IllegalArgumentException that gets thrown is due to a NPE that happens and the NPE's toString() output is contained as a message of the IllegalArgumentException, as noted in the JIRA. This happens even for Constructor invocations, through ConstructorAccessor.

The commit in the attached patch, adds bytecode instructions to check if the method arguments being passed is null and if so, doesn't attempt an arraylength instruction and instead just stores 0 on to the stack. This prevents the NullPointerException being reported, enclosed as a message within the IllegalArgumentException and instead throws back a proper IllegalArgumentException (as you would expect if the invocation had happened over a native MethodAccessor).

The goal of the patch isn't to match the exception message with what the native MethodAccessor throws but just prevent the NPE from being playing a role in the IllegalArgumentException. i.e. this change doesn't throw a new IllegalArgumentException("wrong number of arguments").

The patch also contains a new testcase which reproduces this against the current upstream and verifies this patch works.

This is the first time I'm fiddling with bytecode generation, so I would appreciate some feedback on the changed bytecode and if there's a different/better way to do it. Furthermore, although I do have a signed and approved OCA, I will need a sponsor for this patch. Anyone willing to review and sponsor, please?

[1] https://bugs.openjdk.java.net/browse/JDK-8159797

[2] https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Method.html#invoke(java.lang.Object,java.lang.Object...)

-Jaikiran

-------------- next part --------------

HG changeset patch

User Jaikiran Pai <jaikiran.pai at gmail.com>

Date 1525350350 -19800

Thu May 03 17:55:50 2018 +0530

Node ID 2b535248cf02d90f6fea1c89150a277f16954c04

Parent 7379e6f906aeb6e69ed8eccda9de285e606ab1ff

JDK-8159797 Prevent NPE from the generated Method/ConstructorAccessor when invoking with incorrect number of arguments

diff --git a/src/java.base/share/classes/jdk/internal/reflect/ClassFileConstants.java b/src/java.base/share/classes/jdk/internal/reflect/ClassFileConstants.java --- a/src/java.base/share/classes/jdk/internal/reflect/ClassFileConstants.java +++ b/src/java.base/share/classes/jdk/internal/reflect/ClassFileConstants.java @@ -137,4 +137,7 @@

 // Access flags
 public static final short ACC_PUBLIC = (short) 0x0001;

} diff --git a/src/java.base/share/classes/jdk/internal/reflect/MethodAccessorGenerator.java b/src/java.base/share/classes/jdk/internal/reflect/MethodAccessorGenerator.java --- a/src/java.base/share/classes/jdk/internal/reflect/MethodAccessorGenerator.java +++ b/src/java.base/share/classes/jdk/internal/reflect/MethodAccessorGenerator.java @@ -474,7 +474,13 @@ // aload_2 | aload_1 (Method | Constructor) // ifnull // aload_2 | aload_1

@@ -496,7 +502,22 @@ } else { cb.opc_aload_2(); }

diff --git a/test/jdk/jdk/internal/reflect/Reflection/MethodAccessGeneratorTest.java b/test/jdk/jdk/internal/reflect/Reflection/MethodAccessGeneratorTest.java new file mode 100644 --- /dev/null +++ b/test/jdk/jdk/internal/reflect/Reflection/MethodAccessGeneratorTest.java @@ -0,0 +1,106 @@ +/*



More information about the core-libs-dev mailing list