From adfdbd4553d9f885ebc6fc15adb5e4dd9ca6d352 Mon Sep 17 00:00:00 2001 From: Nathan Henderson Date: Fri, 17 Nov 2023 08:16:28 -0800 Subject: [PATCH] Conform to spec for interface resolution for OJDK MHs This patch fixes eclipse-openj9/openj9#14984. For Java 10-, private interface lookups should fail with a IncompatibleClassChangeError that is caught and wrapped in an IllegalAccessException. For OJ9 MHs, the IllegalAccessException is thrown in findInterface based on Java-code guards on method modifiers. For OJDK MHs, the IncompatibleClassChangeError is expected from a call to MethodHandleNatives.resolve and wrapped. This patch ensures that the correct error is thrown for private interfaces during method lookup for Java 11- with OJDK MHs. Issues: eclipse-openj9/openj9#14984 Signed-off-by: Nathan Henderson --- .../common/java_lang_invoke_MethodHandleNatives.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/runtime/jcl/common/java_lang_invoke_MethodHandleNatives.cpp b/runtime/jcl/common/java_lang_invoke_MethodHandleNatives.cpp index f9e4daa749d..577ba5c64db 100644 --- a/runtime/jcl/common/java_lang_invoke_MethodHandleNatives.cpp +++ b/runtime/jcl/common/java_lang_invoke_MethodHandleNatives.cpp @@ -926,6 +926,17 @@ Java_java_lang_invoke_MethodHandleNatives_resolve( new_flags |= MN_IS_METHOD; if (MH_REF_INVOKEINTERFACE == ref_kind) { Assert_JCL_true(J9_ARE_NO_BITS_SET(methodModifiers, J9AccStatic)); +#if JAVA_SPEC_VERSION < 11 + /* Ensure findVirtual throws an IllegalAccessException (by wrapping this IncompatibleClassChangeError) + * when trying to access a private interface method for Java 10- with OpenJDK MHs. + */ + if (J9_ARE_NO_BITS_SET(lookupOptions, J9_LOOK_STATIC) + && J9_ARE_ALL_BITS_SET(methodModifiers, J9AccPrivate) + ) { + vmFuncs->setCurrentExceptionUTF(currentThread, J9VMCONSTANTPOOL_JAVALANGINCOMPATIBLECLASSCHANGEERROR, NULL); + goto done; + } +#endif /* JAVA_SPEC_VERSION < 11 */ if (J9_ARE_ALL_BITS_SET(methodID->vTableIndex, J9_JNI_MID_INTERFACE)) { new_flags |= MH_REF_INVOKEINTERFACE << MN_REFERENCE_KIND_SHIFT; } else if (!J9ROMMETHOD_HAS_VTABLE(romMethod)) {