Skip to content

Commit f4dccfd

Browse files
mcimadamorehns
andcommitted
8338596: Clarify handling of restricted and caller-sensitive methods
Co-authored-by: Hannes Wallnöfer <[email protected]> Reviewed-by: alanb, jvernee
1 parent 3b8a2f8 commit f4dccfd

File tree

5 files changed

+78
-49
lines changed

5 files changed

+78
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!doctype html>
2+
<!--
3+
Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
4+
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
6+
This code is free software; you can redistribute it and/or modify it
7+
under the terms of the GNU General Public License version 2 only, as
8+
published by the Free Software Foundation. Oracle designates this
9+
particular file as subject to the "Classpath" exception as provided
10+
by Oracle in the LICENSE file that accompanied this code.
11+
12+
This code is distributed in the hope that it will be useful, but WITHOUT
13+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
version 2 for more details (a copy is included in the LICENSE file that
16+
accompanied this code).
17+
18+
You should have received a copy of the GNU General Public License version
19+
2 along with this work; if not, write to the Free Software Foundation,
20+
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21+
22+
Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23+
or visit www.oracle.com if you need additional information or have any
24+
questions.
25+
-->
26+
<html lang="en">
27+
<head>
28+
<title>Restricted methods</title>
29+
</head>
30+
<body>
31+
<h1 id="restricted">Restricted methods</h1>
32+
<p>Various methods in the Java SE API allow Java code to interoperate with resources outside the Java runtime
33+
in such a way that the runtime cannot prove correct or safe use of the resources. These methods can,
34+
when used incorrectly, violate the integrity of the Java Virtual Machine, but are conditionally made available
35+
to users, as they provide essential functionality. They are known as <em>restricted methods</em>.</p>
36+
<p>Given the potential danger of restricted methods, the Java runtime issues a warning on
37+
the standard error stream every time a restricted method is invoked. Such warnings can
38+
be disabled by granting access to restricted methods to selected modules. This can be
39+
done either via implementation-specific command line options or programmatically, e.g.
40+
by calling <a href="{@docRoot}/java.base/java/lang/ModuleLayer.Controller.html#enableNativeAccess(java.lang.Module)"><code>ModuleLayer.Controller.enableNativeAccess(java.lang.Module)</code></a>.</p>
41+
<p>When a restricted method is invoked by <a href="{@docRoot}/../specs/jni/index.html">JNI code</a>,
42+
or from an <a href="{@docRoot}/java.base/java/lang/foreign/Linker.html#upcallStub(java.lang.invoke.MethodHandle,java.lang.foreign.FunctionDescriptor,java.lang.foreign.Arena,java.lang.foreign.Linker.Option...)">upcall stub</a>
43+
and there is no caller class on the stack, it is as if the restricted method call occurred in an <em>unnamed module</em>.</p>
44+
<p>In the reference implementation, access to restricted methods can be granted to
45+
specific modules using the command line option <code>--enable-native-access=M1,M2, ... Mn</code>,
46+
where <code>M1</code>, <code>M2</code>, <code>... Mn</code> are module names (for the unnamed module,
47+
the special value <code>ALL-UNNAMED</code> can be used). Access to restricted methods
48+
from modules not listed by that option is deemed <em>illegal</em>. Clients can
49+
control how access to restricted methods is handled, using the command line
50+
option <code>--illegal-native-access</code>. If this option is not specified,
51+
illegal access to restricted methods will result in runtime warnings.</p>
52+
</body>
53+
</html>

src/java.base/share/classes/java/lang/foreign/package-info.java

-40
Original file line numberDiff line numberDiff line change
@@ -128,49 +128,9 @@
128128
* {@linkplain java.lang.foreign.SegmentAllocator#allocateFrom(java.lang.String) converting}
129129
* Java strings into zero-terminated, UTF-8 strings, as demonstrated in the above example.
130130
*
131-
* <h2 id="restricted">Restricted methods</h2>
132-
*
133-
* Some methods in this package are considered <em>restricted</em>. Restricted methods
134-
* are typically used to bind native foreign data and/or functions to first-class
135-
* Java API elements which can then be used directly by clients. For instance the
136-
* restricted method {@link java.lang.foreign.MemorySegment#reinterpret(long)} can be
137-
* used to create a fresh segment with the same address and temporal bounds, but with
138-
* the provided size. This can be useful to resize memory segments obtained when
139-
* interacting with native functions.
140-
* <p>
141-
* Binding foreign data and/or functions is generally unsafe and, if done incorrectly,
142-
* can result in VM crashes, or memory corruption when the bound Java API element
143-
* is accessed. For instance, incorrectly resizing a native memory segment using
144-
* {@link java.lang.foreign.MemorySegment#reinterpret(long)} can lead to a JVM crash, or,
145-
* worse, lead to silent memory corruption when attempting to access the resized segment.
146-
* For these reasons, it is crucial for code that calls a restricted method to never pass
147-
* arguments that might cause incorrect binding of foreign data and/or functions to
148-
* a Java API.
149-
* <p>
150-
* Given the potential danger of restricted methods, the Java runtime issues a warning on
151-
* the standard error stream every time a restricted method is invoked. Such warnings can
152-
* be disabled by granting access to restricted methods to selected modules. This can be
153-
* done either via implementation-specific command line options or programmatically, e.g.
154-
* by calling {@link java.lang.ModuleLayer.Controller#enableNativeAccess(java.lang.Module)}.
155-
* <p>
156-
* For every class in this package, unless specified otherwise, any method arguments of
157-
* reference type must not be {@code null}, and any null argument will elicit a
158-
* {@code NullPointerException}. This fact is not individually documented for methods of
159-
* this API.
160-
*
161131
* @apiNote Usual memory model guarantees (see {@jls 17.4}) do not apply when accessing
162132
* native memory segments as these segments are backed by off-heap regions of memory.
163133
*
164-
* @implNote
165-
* In the reference implementation, access to restricted methods can be granted to
166-
* specific modules using the command line option {@code --enable-native-access=M1,M2, ... Mn},
167-
* where {@code M1}, {@code M2}, {@code ... Mn} are module names (for the unnamed module,
168-
* the special value {@code ALL-UNNAMED} can be used). Access to restricted methods
169-
* from modules not listed by that option is deemed <em>illegal</em>. Clients can
170-
* control how access to restricted methods is handled, using the command line
171-
* option {@code --illegal-native-access}. If this option is not specified,
172-
* illegal access to restricted methods will result in runtime warnings.
173-
*
174134
* @spec jni/index.html Java Native Interface Specification
175135
*
176136
* @since 22

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java

+20-8
Original file line numberDiff line numberDiff line change
@@ -2642,19 +2642,31 @@ public void addRestrictedInfo(ExecutableElement forWhat, Content target) {
26422642
//in Java platform:
26432643
var restrictedDiv = HtmlTree.DIV(HtmlStyles.restrictedBlock);
26442644
restrictedDiv.setId(htmlIds.forRestrictedSection(forWhat));
2645-
String name = forWhat.getSimpleName().toString();
2645+
var name = forWhat.getSimpleName().toString();
26462646
var nameCode = HtmlTree.CODE(Text.of(name));
2647-
String leadingNoteKey = "doclet.RestrictedLeadingNote";
2648-
Content leadingNote =
2649-
contents.getContent(leadingNoteKey, nameCode);
2650-
restrictedDiv.add(HtmlTree.SPAN(HtmlStyles.restrictedLabel,
2651-
leadingNote));
2652-
Content note1 = contents.getContent("doclet.RestrictedTrailingNote1", nameCode);
2647+
var restrictedMethodLink = getRestrictedMethodDocLink();
2648+
var leadingNoteKey = "doclet.RestrictedLeadingNote";
2649+
var leadingNote = contents.getContent(leadingNoteKey, nameCode, restrictedMethodLink);
2650+
restrictedDiv.add(HtmlTree.SPAN(HtmlStyles.restrictedLabel, leadingNote));
2651+
var note1 = contents.getContent("doclet.RestrictedTrailingNote1", nameCode);
26532652
restrictedDiv.add(HtmlTree.DIV(HtmlStyles.restrictedComment, note1));
2654-
Content note2 = contents.getContent("doclet.RestrictedTrailingNote2", nameCode);
2653+
var note2 = contents.getContent("doclet.RestrictedTrailingNote2", nameCode);
26552654
restrictedDiv.add(HtmlTree.DIV(HtmlStyles.restrictedComment, note2));
26562655
target.add(restrictedDiv);
26572656
}
26582657
}
26592658

2659+
private Content getRestrictedMethodDocLink() {
2660+
var restrictedMethodLabel = contents.getContent("doclet.RestrictedMethod");
2661+
var javaLang = utils.elementUtils.getPackageElement("java.lang");
2662+
if (utils.isIncluded(javaLang)) {
2663+
var restrictedDocPath = pathToRoot
2664+
.resolve(docPaths.forPackage(javaLang))
2665+
.resolve(DocPaths.DOC_FILES)
2666+
.resolve(DocPaths.RESTRICTED_DOC);
2667+
return links.createLink(restrictedDocPath, restrictedMethodLabel);
2668+
}
2669+
return restrictedMethodLabel;
2670+
}
2671+
26602672
}

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/resources/standard.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ doclet.ReflectivePreviewAPI={0} refers to one or more reflective preview APIs:
424424
doclet.UsesDeclaredUsingPreview={0} refers to one or more types which are declared using a preview feature of the Java language: {1}.
425425
doclet.PreviewTrailingNote1=Programs can only use {0} when preview features are enabled.
426426
doclet.PreviewTrailingNote2=Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
427-
doclet.RestrictedLeadingNote={0} is a restricted method of the Java platform.
427+
doclet.RestrictedMethod=restricted method
428+
doclet.RestrictedLeadingNote={0} is a {1} of the Java platform.
428429
doclet.RestrictedTrailingNote1=Programs can only use {0} when access to restricted methods is enabled.
429430
doclet.RestrictedTrailingNote2=Restricted methods are unsafe, and, if used incorrectly, might crash \
430431
the JVM or result in memory corruption.

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/DocPaths.java

+3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ public static DocPath indexN(int n) {
163163
/** The name of the file for restricted methods. */
164164
public static final DocPath RESTRICTED_LIST = DocPath.create("restricted-list.html");
165165

166+
/** The name of the doc-file for restricted methods. */
167+
public static final DocPath RESTRICTED_DOC = DocPath.create("RestrictedMethods.html");
168+
166169
/** The name of the directory for the resource files. */
167170
public static final DocPath RESOURCE_FILES = DocPath.create("resource-files");
168171

0 commit comments

Comments
 (0)