Skip to content

Commit

Permalink
8295044: Implementation of Foreign Function and Memory API (Second Pr…
Browse files Browse the repository at this point in the history
…eview)

Co-authored-by: Jorn Vernee <jvernee@openjdk.org>
Co-authored-by: Per Minborg <pminborg@openjdk.org>
Co-authored-by: Maurizio Cimadamore <mcimadamore@openjdk.org>
Reviewed-by: jvernee, pminborg, psandoz, alanb, sundar
  • Loading branch information
3 people committed Dec 5, 2022
1 parent bd38188 commit 73baadc
Show file tree
Hide file tree
Showing 252 changed files with 9,209 additions and 7,877 deletions.
18 changes: 1 addition & 17 deletions src/hotspot/cpu/aarch64/downcallLinker_aarch64.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
}

// --