From e36756b264000fe2deb95ec8f68d1571fd7653f9 Mon Sep 17 00:00:00 2001 From: Chen Liang Date: Wed, 30 Apr 2025 21:56:13 +0000 Subject: [PATCH] 8297727: Forcing LF interpretation lead to StackOverflowError in reflection code Reviewed-by: jvernee --- .../java/lang/invoke/DirectMethodHandle.java | 11 ++--- .../ReflectionInInterpretTest.java | 42 +++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 test/jdk/java/lang/invoke/LFInterpret/ReflectionInInterpretTest.java diff --git a/src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java b/src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java index 4eb825d13ce..ad6387e8c9e 100644 --- a/src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java +++ b/src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java @@ -214,7 +214,6 @@ sealed class DirectMethodHandle extends MethodHandle { which = LF_INVSPECIAL_IFC; } LambdaForm lform = preparedLambdaForm(mtype, which); - maybeCompile(lform, m); assert(lform.methodType().dropParameterTypes(0, 1) .equals(m.getInvocationType().basicType())) : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType()); @@ -320,12 +319,6 @@ sealed class DirectMethodHandle extends MethodHandle { return null; } - private static void maybeCompile(LambdaForm lform, MemberName m) { - if (lform.vmentry == null && VerifyAccess.isSamePackage(m.getDeclaringClass(), MethodHandle.class)) - // Help along bootstrapping... - lform.compileToBytecode(); - } - /** Static wrapper for DirectMethodHandle.internalMemberName. */ @ForceInline /*non-public*/ @@ -666,7 +659,6 @@ sealed class DirectMethodHandle extends MethodHandle { formOp += (AF_GETSTATIC_INIT - AF_GETSTATIC); } LambdaForm lform = preparedFieldLambdaForm(formOp, isVolatile, ftype); - maybeCompile(lform, m); assert(lform.methodType().dropParameterTypes(0, 1) .equals(m.getInvocationType().basicType())) : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType()); @@ -843,6 +835,9 @@ sealed class DirectMethodHandle extends MethodHandle { } LambdaForm.associateWithDebugName(form, nameBuilder.toString()); } + + // NF_UNSAFE uses field form, avoid circular dependency in interpreter + form.compileToBytecode(); return form; } diff --git a/test/jdk/java/lang/invoke/LFInterpret/ReflectionInInterpretTest.java b/test/jdk/java/lang/invoke/LFInterpret/ReflectionInInterpretTest.java new file mode 100644 index 00000000000..a7fece07e5c --- /dev/null +++ b/test/jdk/java/lang/invoke/LFInterpret/ReflectionInInterpretTest.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025, 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. + */ + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; + +/* + * @test + * @bug 8297727 + * @summary MethodHandle field access fails with bytecode compilation disabled + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+ShowHiddenFrames + * -Djava.lang.invoke.MethodHandle.COMPILE_THRESHOLD=-1 + * ReflectionInInterpretTest + */ +public class ReflectionInInterpretTest { + private static Integer f; // non-Object type is required to find a non-compiled form + + public static void main(String... args) throws Throwable { + MethodHandle mh = MethodHandles.lookup().findStaticGetter(ReflectionInInterpretTest.class, "f", Integer.class); + var _ = (Integer) mh.invokeExact(); + } +}