Skip to content
Permalink
Browse files
8274848: LambdaMetaFactory::metafactory on REF_invokeSpecial impl met…
…hod has incorrect behavior

Reviewed-by: psandoz, dlsmith
  • Loading branch information
Mandy Chung committed Oct 28, 2021
1 parent 48f3fca commit 21da2183875feca3dbf4f1bd875b268a7fc8d560
Showing 2 changed files with 136 additions and 1 deletion.
@@ -26,6 +26,8 @@

import sun.invoke.util.Wrapper;

import java.lang.reflect.Modifier;

import static java.lang.invoke.MethodHandleInfo.*;
import static sun.invoke.util.Wrapper.forPrimitiveType;
import static sun.invoke.util.Wrapper.forWrapperType;
@@ -157,7 +159,7 @@
// Classes compiled prior to dynamic nestmate support invoke a private instance
// method with REF_invokeSpecial. Newer classes use REF_invokeVirtual or
// REF_invokeInterface, and we can use that instruction in the lambda class.
if (targetClass == implClass) {
if (targetClass == implClass && Modifier.isPrivate(implInfo.getModifiers())) {
this.implKind = implClass.isInterface() ? REF_invokeInterface : REF_invokeVirtual;
} else {
this.implKind = REF_invokeSpecial;
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2021, 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
* @bug 8274848
* @run testng InvokeSpecialMethodTest
* @summary ensure REF_invokeSpecial on a non-private implementation method
* behaves as if `super::m` is invoked regardless of its access flag
*/

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.invoke.CallSite;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;

import static java.lang.invoke.MethodType.methodType;

public class InvokeSpecialMethodTest {
static class MethodTest {
static final Lookup LOOKUP = MethodHandles.lookup();

public String m_public() {
return "test_public";
}

protected String m_protected() {
return "test_protected";
}

private String m_private() {
return "test_private";
}

public static class SubClass extends MethodTest {
@Override
public String m_public() {
return "subclass_public";
}

@Override
public String m_protected() {
return "subclass_protected";
}

public String m_private() {
return "subclass_private";
}
}

/*
* findSpecial with Test class as the special caller matching
* the factory type `StringFactory(Test)`
*/
static MethodHandle mh(String name) {
try {
return LOOKUP.findSpecial(MethodTest.class, name, methodType(String.class), MethodTest.class);
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

static final MethodHandle M_PUBLIC = mh("m_public");
static final MethodHandle M_PROTECTED = mh("m_protected");
static final MethodHandle M_PRIVATE = mh("m_private");
}

@FunctionalInterface
interface StringFactory {
String get();
}

@DataProvider
public Object[][] methodProvider() {
return new Object[][]{
{MethodTest.M_PUBLIC, "test_public"},
{MethodTest.M_PROTECTED, "test_protected"},
{MethodTest.M_PRIVATE, "test_private"}
};
}

@Test(dataProvider = "methodProvider")
void test(MethodHandle implMethod, String expected) throws Throwable {
testMetafactory(implMethod, expected);
testAltMetafactory(implMethod, expected);
}

static void testMetafactory(MethodHandle implMethod, String expected) throws Throwable {
CallSite cs = LambdaMetafactory.metafactory(MethodTest.LOOKUP, "get",
methodType(StringFactory.class, MethodTest.class),
methodType(String.class), implMethod, methodType(String.class));
MethodTest o = new MethodTest.SubClass();
StringFactory factory = (StringFactory) cs.dynamicInvoker().invokeExact(o);
String actual = factory.get();
Assert.assertEquals(actual, expected);
}

static void testAltMetafactory(MethodHandle implMethod, String expected) throws Throwable {
CallSite cs = LambdaMetafactory.altMetafactory(MethodTest.LOOKUP, "get",
methodType(StringFactory.class, MethodTest.class),
methodType(String.class), implMethod, methodType(String.class),
LambdaMetafactory.FLAG_SERIALIZABLE);
MethodTest o = new MethodTest.SubClass();
StringFactory factory = (StringFactory) cs.dynamicInvoker().invokeExact(o);
String actual = factory.get();
Assert.assertEquals(actual, expected);
}
}

4 comments on commit 21da218

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 21da218 Oct 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@openjdk
Copy link

@openjdk openjdk bot commented on 21da218 Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mlchung @mlchung the backport was successfully created on the branch mlchung-backport-21da2183 in my personal fork of openjdk/jdk17u. To create a pull request with this backport targeting openjdk/jdk17u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

this pull request contains a backport of commit 21da2183 from the openjdk/jdk repository.

The commit being backported was authored by Mandy Chung on 28 Oct 2021 and was reviewed by Paul Sandoz and Dan Smith.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u:

$ git fetch https://github.com/openjdk-bots/jdk17u mlchung-backport-21da2183:mlchung-backport-21da2183
$ git checkout mlchung-backport-21da2183
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u mlchung-backport-21da2183

@mlchung
Copy link
Member

@mlchung mlchung commented on 21da218 Nov 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u

@openjdk
Copy link

@openjdk openjdk bot commented on 21da218 Nov 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mlchung @mlchung the backport was successfully created on the branch mlchung-backport-21da2183 in my personal fork of openjdk/jdk17u. To create a pull request with this backport targeting openjdk/jdk17u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

this pull request contains a backport of commit 21da2183 from the openjdk/jdk repository.

The commit being backported was authored by Mandy Chung on 28 Oct 2021 and was reviewed by Paul Sandoz and Dan Smith.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u:

$ git fetch https://github.com/openjdk-bots/jdk17u mlchung-backport-21da2183:mlchung-backport-21da2183
$ git checkout mlchung-backport-21da2183
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u mlchung-backport-21da2183

Please sign in to comment.