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

8295044: Implementation of Foreign Function and Memory API (Second Preview) #10872

Closed
wants to merge 71 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
7d8f4bd
Initial push
mcimadamore Oct 11, 2022
4f3767d
Add missing layout classes
mcimadamore Oct 11, 2022
c207b8e
Drop memory inspection utils
mcimadamore Oct 11, 2022
2bfb7d4
Add missing preview annotation on Linker.Option
mcimadamore Oct 11, 2022
688c9d3
Add preview annotation to Struct/UnionLayout
mcimadamore Oct 11, 2022
7299827
Fix call to SymbolLookup::find
minborg Oct 12, 2022
f54bfe3
Merge pull request #12 from minborg/fix-lookup-find
mcimadamore Oct 12, 2022
ebb5b30
Revert MemorySegment factories
minborg Oct 17, 2022
c7d09ad
Update javadocs after comments
minborg Oct 19, 2022
4fa32dd
Merge pull request #13 from minborg/revert-factories
mcimadamore Oct 19, 2022
341281d
Fix benchmark and test failure
mcimadamore Oct 19, 2022
5143d1d
Merge branch 'master' into PR_20
mcimadamore Oct 19, 2022
0d256f3
Revert some javadoc changes
mcimadamore Oct 19, 2022
83a86be
Update some javadocs
minborg Oct 26, 2022
34122b2
Merge pull request #14 from minborg/small-javadoc
mcimadamore Oct 26, 2022
ac7733d
Merge branch 'master' into PR_20
mcimadamore Oct 26, 2022
3d93302
Merge branch 'master' into PR_20
mcimadamore Nov 4, 2022
89f03a6
Remove VM dead code after implementation of Binding.Cast
mcimadamore Nov 7, 2022
76e9f01
Fix MemorySegment.ofBuffer when applied to StringCharBuffer
mcimadamore Nov 7, 2022
e8b95f8
Fix mismmatched acquire/release in BindingSpecializer
mcimadamore Nov 7, 2022
0c70da2
Add missing tests
mcimadamore Nov 7, 2022
b98febf
Bring windows CallArranger in sync with panama repo
mcimadamore Nov 7, 2022
f04be0d
Make memory session a pure lifetime abstraction
mcimadamore Nov 7, 2022
f73effc
Bring windows CallArranger in sync with panama repo (again)
mcimadamore Nov 8, 2022
2ac0cc1
Update src/java.base/share/classes/java/lang/foreign/Arena.java
mcimadamore Nov 8, 2022
f8858af
Update src/java.base/share/classes/java/lang/foreign/Arena.java
mcimadamore Nov 8, 2022
87d94f3
Update src/java.base/share/classes/java/lang/foreign/MemoryLayout.java
mcimadamore Nov 8, 2022
6b35c69
Update src/java.base/share/classes/java/lang/foreign/MemorySegment.java
mcimadamore Nov 8, 2022
441b4a0
Update src/java.base/share/classes/java/lang/foreign/MemorySegment.java
mcimadamore Nov 8, 2022
cc4ff58
Update src/java.base/share/classes/java/lang/foreign/MemorySession.java
mcimadamore Nov 8, 2022
0f8ca36
Update src/java.base/share/classes/java/lang/foreign/ValueLayout.java
mcimadamore Nov 8, 2022
3ff8210
Update src/java.base/share/classes/java/lang/foreign/package-info.java
mcimadamore Nov 8, 2022
afb36a9
Fix bad indent on ModuleLayer.Controller
mcimadamore Nov 8, 2022
e284023
Rework package-level javadoc for restricted methods
mcimadamore Nov 8, 2022
39099f4
Javadoc tweaks in MemorySession/Arena
mcimadamore Nov 8, 2022
fd36710
Revamp javadoc of Arena/MemorySession
mcimadamore Nov 8, 2022
01b0973
More javadoc tweaks
mcimadamore Nov 8, 2022
bb39bef
Address review comments
mcimadamore Nov 8, 2022
fff83ca
Update src/java.base/share/classes/java/lang/foreign/MemorySession.java
mcimadamore Nov 8, 2022
9be0c97
More javadoc fixes
mcimadamore Nov 8, 2022
df29e6a
Fix typo
mcimadamore Nov 8, 2022
64f4d55
Add @apiNote to package-info
minborg Nov 9, 2022
d7a1c7e
Fix failing tests
minborg Nov 9, 2022
2d75f95
Rename isOwnedBy -> isCloseableBy
mcimadamore Nov 9, 2022
c6cd176
Merge pull request #16 from minborg/fix-tests2
mcimadamore Nov 9, 2022
3952134
Merge pull request #15 from minborg/test
mcimadamore Nov 9, 2022
cd3fbe7
Tweak Arena::close javadoc
mcimadamore Nov 9, 2022
9b97bad
Tweak preview feature description for JEP 434
mcimadamore Nov 10, 2022
20ee6e8
Merge branch 'master' into PR_20
mcimadamore Nov 15, 2022
5ae5864
Rename MemorySession -> SegmentScope
mcimadamore Nov 15, 2022
3d9cebd
Fix whitespace
mcimadamore Nov 15, 2022
b2dd892
Add `since` tag in Module/ModuleLayer preview methods
mcimadamore Nov 15, 2022
19e0f6d
Address review comments
mcimadamore Nov 15, 2022
54fb485
Fix tests broken by MemorySession rename
mcimadamore Nov 15, 2022
b331a4f
Fix MapToMemorySegmentTest
mcimadamore Nov 15, 2022
5f60d05
Fix @since tag in SegmentScope
mcimadamore Nov 15, 2022
876587c
Fix typo in SegmentScope javadoc
mcimadamore Nov 15, 2022
15f86b5
Address review comments
mcimadamore Nov 17, 2022
ce41598
Fix bad @throws in MemorySegment::copy methods
mcimadamore Nov 17, 2022
a0cee7b
Address more review comments
mcimadamore Nov 21, 2022
66dd888
Fix wrong check in MemorySegment::spliterator/elements
mcimadamore Nov 22, 2022
3c75e09
Fix bit vs. byte mismatch in test
mcimadamore Nov 23, 2022
9716815
* remove unused Scoped interface
mcimadamore Nov 23, 2022
6699ad9
Address review comments
mcimadamore Nov 28, 2022
5a75118
Polish javadoc:
mcimadamore Nov 30, 2022
ce85d18
Address review comment
mcimadamore Nov 30, 2022
8668fb3
Merge branch 'master' into PR_20
mcimadamore Nov 30, 2022
df8a4a6
Address review comments
mcimadamore Nov 30, 2022
198f30c
Address review comments
mcimadamore Nov 30, 2022
8b5dc0f
Merge branch 'master' into PR_20
mcimadamore Nov 30, 2022
33b834c
Fix Preview annotation for JEP 434
mcimadamore Dec 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 1 addition & 17 deletions src/hotspot/cpu/aarch64/downcallLinker_aarch64.cpp
Expand Up @@ -210,23 +210,7 @@ void DowncallStubGenerator::generate() {
__ blr(_abi._target_addr_reg);
// this call is assumed not to have killed rthread

if (!_needs_return_buffer) {
// Unpack native results.
switch (_ret_bt) {
case T_BOOLEAN: __ c2bool(r0); break;
case T_CHAR : __ ubfx(r0, r0, 0, 16); break;
case T_BYTE : __ sbfx(r0, r0, 0, 8); break;
case T_SHORT : __ sbfx(r0, r0, 0, 16); break;
case T_INT : __ sbfx(r0, r0, 0, 32); break;
case T_DOUBLE :
case T_FLOAT :
// Result is in v0 we'll save as needed
break;
case T_VOID: break;
case T_LONG: break;
default : ShouldNotReachHere();
}
} else {
if (_needs_return_buffer) {
assert(ret_buf_addr_sp_offset != -1, "no return buffer addr spill");
__ ldr(tmp1, Address(sp, ret_buf_addr_sp_offset));
int offset = 0;
Expand Down
19 changes: 1 addition & 18 deletions src/hotspot/cpu/x86/downcallLinker_x86_64.cpp
Expand Up @@ -211,24 +211,7 @@ void DowncallStubGenerator::generate() {
__ call(_abi._target_addr_reg);
// this call is assumed not to have killed r15_thread

if (!_needs_return_buffer) {
// FIXME: this assumes we return in rax/xmm0, which might not be the case
// Unpack native results.
switch (_ret_bt) {
case T_BOOLEAN: __ c2bool(rax); break;
case T_CHAR : __ movzwl(rax, rax); break;
case T_BYTE : __ sign_extend_byte (rax); break;
case T_SHORT : __ sign_extend_short(rax); break;
case T_INT : /* nothing to do */ break;
case T_DOUBLE :
case T_FLOAT :
// Result is in xmm0 we'll save as needed
break;
case T_VOID: break;
case T_LONG: break;
default : ShouldNotReachHere();
}
} else {
if (_needs_return_buffer) {
assert(ret_buf_addr_rsp_offset != -1, "no return buffer addr spill");
__ movptr(rscratch1, Address(rsp, ret_buf_addr_rsp_offset));
int offset = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/ci/ciField.cpp
Expand Up @@ -225,7 +225,7 @@ static bool trust_final_non_static_fields(ciInstanceKlass* holder) {
// Even if general trusting is disabled, trust system-built closures in these packages.
if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke") ||
holder->is_in_package("java/lang/reflect") || holder->is_in_package("jdk/internal/reflect") ||
holder->is_in_package("jdk/internal/foreign") || holder->is_in_package("java/lang/foreign") ||
holder->is_in_package("jdk/internal/foreign/layout") || holder->is_in_package("jdk/internal/foreign") ||
holder->is_in_package("jdk/internal/vm/vector") || holder->is_in_package("jdk/incubator/vector") ||
holder->is_in_package("java/lang"))
return true;
Expand Down
72 changes: 63 additions & 9 deletions src/java.base/share/classes/java/lang/Module.java
Expand Up @@ -52,10 +52,12 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jdk.internal.javac.PreviewFeature;
import jdk.internal.loader.BuiltinClassLoader;
import jdk.internal.loader.BootLoader;
import jdk.internal.loader.ClassLoaders;
import jdk.internal.misc.CDS;
import jdk.internal.module.ModuleBootstrap;
import jdk.internal.module.ModuleLoaderMap;
import jdk.internal.module.ServicesCatalog;
import jdk.internal.module.Resources;
Expand Down Expand Up @@ -256,25 +258,77 @@ public ModuleLayer getLayer() {
/**
* Update this module to allow access to restricted methods.
*/
Module implAddEnableNativeAccess() {
synchronized Module implAddEnableNativeAccess() {
enableNativeAccess = true;
return this;
}

/**
* Update all unnamed modules to allow access to restricted methods.
* Returns {@code true} if this module can access
* <a href="foreign/package-summary.html#restricted"><em>restricted</em></a> methods.
*
* @since 20
*
* @return {@code true} if this module can access <em>restricted</em> methods.
*/
static void implAddEnableNativeAccessAllUnnamed() {
ALL_UNNAMED_MODULE.enableNativeAccess = true;
@PreviewFeature(feature=PreviewFeature.Feature.FOREIGN)
public boolean isNativeAccessEnabled() {
Module target = moduleForNativeAccess();
synchronized(target) {
return target.enableNativeAccess;
}
}

// Returns the Module object that holds the enableNativeAccess
// flag for this module.
private Module moduleForNativeAccess() {
return isNamed() ? this : ALL_UNNAMED_MODULE;
}

// This is invoked from Reflection.ensureNativeAccess
void ensureNativeAccess(Class<?> owner, String methodName) {
// The target module whose enableNativeAccess flag is ensured
Module target = moduleForNativeAccess();
// racy read of the enable native access flag
boolean isNativeAccessEnabled = target.enableNativeAccess;
if (!isNativeAccessEnabled) {
synchronized (target) {
// safe read of the enableNativeAccess of the target module
isNativeAccessEnabled = target.enableNativeAccess;

// check again with the safely read flag
if (isNativeAccessEnabled) {
// another thread beat us to it - nothing to do
return;
} else if (ModuleBootstrap.hasEnableNativeAccessFlag()) {
throw new IllegalCallerException("Illegal native access from: " + this);
} else {
// warn and set flag, so that only one warning is reported per module
String cls = owner.getName();
String mtd = cls + "::" + methodName;
String mod = isNamed() ? "module " + getName() : "the unnamed module";
String modflag = isNamed() ? getName() : "ALL-UNNAMED";
System.err.printf("""
WARNING: A restricted method in %s has been called
WARNING: %s has been called by %s
WARNING: Use --enable-native-access=%s to avoid a warning for this module
%n""", cls, mtd, mod, modflag);

// set the flag
target.enableNativeAccess = true;
}
}
}
}


/**
* Returns true if module m can access restricted methods.
* Update all unnamed modules to allow access to restricted methods.
*/
boolean implIsEnableNativeAccess() {
return isNamed() ?
enableNativeAccess :
ALL_UNNAMED_MODULE.enableNativeAccess;
static void implAddEnableNativeAccessToAllUnnamed() {
synchronized (ALL_UNNAMED_MODULE) {
ALL_UNNAMED_MODULE.enableNativeAccess = true;
}
}

// --
Expand Down
37 changes: 36 additions & 1 deletion src/java.base/share/classes/java/lang/ModuleLayer.java
Expand Up @@ -44,15 +44,17 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jdk.internal.javac.PreviewFeature;
import jdk.internal.loader.ClassLoaderValue;
import jdk.internal.loader.Loader;
import jdk.internal.loader.LoaderPool;
import jdk.internal.module.ServicesCatalog;
import jdk.internal.misc.CDS;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
import jdk.internal.vm.annotation.Stable;
import sun.security.util.SecurityConstants;


/**
* A layer of modules in the Java virtual machine.
*
Expand Down Expand Up @@ -297,6 +299,39 @@ public Controller addOpens(Module source, String pn, Module target) {
source.implAddOpens(pn, target);
return this;
}

/**
* Enables native access for a module in the layer if the caller's module
* has native access.
*
* <p> This method is <a href="foreign/package-summary.html#restricted"><em>restricted</em></a>.
* Restricted methods are unsafe, and, if used incorrectly, their use might crash
* the JVM or, worse, silently result in memory corruption. Thus, clients should refrain
* from depending on restricted methods, and use safe and supported functionalities,
* where possible.
*
* @param target
* The module to update
*
* @return This controller
*
* @throws IllegalArgumentException
* If {@code target} is not in the module layer
*
* @throws IllegalCallerException
* If the caller is in a module that does not have native access enabled
*
* @since 20
*/
@PreviewFeature(feature=PreviewFeature.Feature.FOREIGN)
@CallerSensitive
public Controller enableNativeAccess(Module target) {
ensureInLayer(target);
Reflection.ensureNativeAccess(Reflection.getCallerClass(), Module.class,
"enableNativeAccess");
target.implAddEnableNativeAccess();
return this;
}
}


Expand Down
8 changes: 4 additions & 4 deletions src/java.base/share/classes/java/lang/System.java
Expand Up @@ -2437,11 +2437,11 @@ public boolean isReflectivelyOpened(Module m, String pn, Module other) {
public Module addEnableNativeAccess(Module m) {
return m.implAddEnableNativeAccess();
}
public void addEnableNativeAccessAllUnnamed() {
Module.implAddEnableNativeAccessAllUnnamed();
public void addEnableNativeAccessToAllUnnamed() {
Module.implAddEnableNativeAccessToAllUnnamed();
}
public boolean isEnableNativeAccess(Module m) {
return m.implIsEnableNativeAccess();
public void ensureNativeAccess(Module m, Class<?> owner, String methodName) {
m.ensureNativeAccess(owner, methodName);
}
public ServicesCatalog getServicesCatalog(ModuleLayer layer) {
return layer.getServicesCatalog();
Expand Down