Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-63075] Restore public access from inherited classes when public access is enabled. #10868

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions truffle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This changelog summarizes major changes between Truffle versions relevant to lan
## Version 25.0
* GR-31495 Added ability to specify language and instrument specific options using `Source.Builder.option(String, String)`. Languages may describe available source options by implementing `TruffleLanguage.getSourceOptionDescriptors()` and `TruffleInstrument.getSourceOptionDescriptors()` respectively.
* GR-61493 Added `RootNode.prepareForCall` which allows root nodes to prepare themselves for use as a call target (or to validate whether they can be used as a call target).
* GR-63075 Java host interop again inherits public method methods from non-public base classes if public access is enabled. This was originally changed in 24.1.

## Version 24.2.0
* GR-60636 Truffle now stops compiling when the code cache fills up on HotSpot. A warning is printed when that happens.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,17 @@
"methods": [
{ "name": "getIsolate", "parameterTypes": ["java.lang.Object"] }
]
},
{
"name": "com.oracle.truffle.api.test.host.GR63075Test$BaseClass",
"allPublicMethods": true
},
{
"name": "com.oracle.truffle.api.test.host.GR63075Test$SubClass",
"allPublicMethods": true
},
{
"name": "com.oracle.truffle.api.test.host.GR63075Test$NonPublicSubClass",
"allPublicMethods": true
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.api.test.host;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.HostAccess.Export;
import org.graalvm.polyglot.Value;
import org.junit.Test;

public class GR63075Test {

static class BaseClass {

@Export
public String publicAbstractMethod() {
return "publicAbstractMethod";
}

}

public static class SubClass extends BaseClass {

@Export
public String publicMethod() {
return "publicMethod";
}

}

static class NonPublicSubClass extends BaseClass {

public String publicMethod() {
return "publicMethod";
}

}

@Test
public void testPublicAccess() {
try (Context c = Context.newBuilder().allowHostAccess(HostAccess.ALL).build()) {
SubClass b = new SubClass();
Value v = c.asValue(b);

assertEquals("publicMethod", v.invokeMember("publicMethod").asString());
assertEquals("publicAbstractMethod", v.invokeMember("publicAbstractMethod").asString());
}
}

@Test
public void testNonPublicAccess() {
try (Context c = Context.newBuilder().allowHostAccess(HostAccess.ALL).build()) {
NonPublicSubClass b = new NonPublicSubClass();
Value v = c.asValue(b);

assertNull(v.getMember("publicMethod"));
assertNull(v.getMember("publicAbstractMethod"));
}
}

@Test
public void testExplicitAccess() {
try (Context c = Context.newBuilder().allowHostAccess(HostAccess.EXPLICIT).build()) {
SubClass b = new SubClass();
Value v = c.asValue(b);

assertEquals("publicMethod", v.invokeMember("publicMethod").asString());
assertNull(v.getMember("publicAbstractMethod"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,8 @@ public void testStaticMethodNotInherited() throws InteropException {
}

@Test
public void testPublicClassBridgeMethod() {
// GR-42882: public bridge method B1.run() for A3.run() no longer exposed.
// invokeRun(new B1(), A3.class);
Assert.assertFalse(INTEROP.isMemberExisting(asTruffleObject(new B1()), "run"));
public void testPublicClassBridgeMethod() throws InteropException {
invokeRun(new B1(), A3.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.awt.*;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ private static void collectPublicMethods(HostClassCache hostAccess, Class<?> typ
Class<?> startType) {
boolean isPublicType = getLookupForPublicClass(type, hostAccess) != null && !Proxy.isProxyClass(type);
boolean includeInherited = hostAccess.allowsPublicAccess || hostAccess.allowsAccessInheritance;
List<Method> bridgeMethods = null;
/**
* If we do not allow inheriting access, we discover all accessible methods through the
* public methods of this class. That way, (possibly inaccessible) method overrides hide
Expand All @@ -231,8 +232,26 @@ private static void collectPublicMethods(HostClassCache hostAccess, Class<?> typ
} else if (getLookupForPublicClass(declaringClass, hostAccess) == null && !Proxy.isProxyClass(declaringClass)) {
// the declaring class and the method itself must be public and accessible
continue;
} else if (m.isBridge() && hostAccess.allowsPublicAccess && hostAccess.allowsAccess(m)) {
/*
* Bridge methods for varargs methods generated by javac may not have the
* varargs modifier, so we must not use the bridge method in that case since
* it would be then treated as non-varargs.
*
* As a workaround, stash away all bridge methods and only consider them at
* the end if no equivalent public non-bridge method was found.
*/
if (bridgeMethods == null) {
bridgeMethods = new ArrayList<>();
}
bridgeMethods.add(m);
continue;
} else if (m.isBridge()) {
// skip all bridge methods
/*
* GR-42882] Without public access we skip all bridge methods. This is
* needed due to inconsistent behavior between javac and JDT. JDT does not
* inherit exported methods for bridge methods.
*/
continue;
}
if (hostAccess.allowsAccess(m)) {
Expand All @@ -255,9 +274,18 @@ private static void collectPublicMethods(HostClassCache hostAccess, Class<?> typ
}
}
}
// Add bridge methods for public methods inherited from non-public superclasses.
// See https://bugs.openjdk.java.net/browse/JDK-6342411
if (bridgeMethods != null && !bridgeMethods.isEmpty()) {
for (Method m : bridgeMethods) {
assert hostAccess.allowsAccess(m); // already checked above
collectPublicMethod(hostAccess, methodMap, staticMethodMap, visited, m);
}
}
}

private static void collectPublicMethod(HostClassCache hostAccess, Map<String, HostMethodDesc> methodMap, Map<String, HostMethodDesc> staticMethodMap, Map<Object, Object> visited, Method m) {
private static void collectPublicMethod(HostClassCache hostAccess, Map<String, HostMethodDesc> methodMap, Map<String, HostMethodDesc> staticMethodMap, Map<Object, Object> visited,
Method m) {
MethodInfo methodInfo = methodInfo(m);
if (!visited.containsKey(methodInfo)) {
visited.put(methodInfo, methodInfo);
Expand Down