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
8301578: Perform output outside synchronization in Module.class #12193
Closed
Closed
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cdd3ac4
Reduce contention
minborg 47d902b
Remove warning
minborg 9af658b
Update after comments
minborg 21d1122
Use another type of comment
minborg b9e42f7
Fix typo
minborg b0aeb64
Revert benchmark change
minborg 11db7d1
Use Unsafe instead of synchronized
minborg 5b78467
Undo unintended changes
minborg 344d47e
Fix things from PR comments
minborg dfa2422
Undo more unintended edits
minborg 29afac8
Remove comment and rename class
minborg File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,5 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Copyright (c) 2014, 2023, 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -113,6 +113,8 @@ public final class Module implements AnnotatedElement { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private final ModuleDescriptor descriptor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// true, if this module allows restricted native access | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Accessing this variable is mostly done under synchronization as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// constructs like VarHandle and AtomicBoolean are not available at this stage. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Stable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private boolean enableNativeAccess; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -258,8 +260,8 @@ public ModuleLayer getLayer() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Update this module to allow access to restricted methods. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
synchronized Module implAddEnableNativeAccess() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
enableNativeAccess = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Module implAddEnableNativeAccess() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
trySetEnableNativeAccess(this); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return this; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -274,6 +276,12 @@ synchronized Module implAddEnableNativeAccess() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@PreviewFeature(feature=PreviewFeature.Feature.FOREIGN) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public boolean isNativeAccessEnabled() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Module target = moduleForNativeAccess(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return isNativeAccessEnabled(target); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private static boolean isNativeAccessEnabled(Module target) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (target.enableNativeAccess) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
synchronized(target) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return target.enableNativeAccess; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -289,46 +297,43 @@ private Module moduleForNativeAccess() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!isNativeAccessEnabled(target)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (ModuleBootstrap.hasEnableNativeAccessFlag()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
throw new IllegalCallerException("Illegal native access from: " + this); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (trySetEnableNativeAccess(target)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Atomically sets enableNativeAccess if not already set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// returning if the value was updated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private static boolean trySetEnableNativeAccess(Module target) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
synchronized (target) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!target.enableNativeAccess) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
target.enableNativeAccess = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Update all unnamed modules to allow access to restricted methods. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
static void implAddEnableNativeAccessToAllUnnamed() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
synchronized (ALL_UNNAMED_MODULE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ALL_UNNAMED_MODULE.enableNativeAccess = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
trySetEnableNativeAccess(ALL_UNNAMED_MODULE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@minborg It'd seem a bit easier to implement using VarHandle and not having to use
synchronized
? Is the reason that you don't want to init VarHandle in this class? 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a bootstrap problem. We cannot use AtomicBoolean, VarHandle or Unsafe here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn’t
Unsafe
already be loaded at this point?