Skip to content

Commit 164d562

Browse files
committed
[Entitlements] Add native checks support and tests for Java 21 (preview) (elastic#121881)
1 parent f5d0a85 commit 164d562

File tree

6 files changed

+25
-80
lines changed

6 files changed

+25
-80
lines changed
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030

3131
import static java.lang.foreign.ValueLayout.ADDRESS;
3232
import static java.lang.foreign.ValueLayout.JAVA_LONG;
33+
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS;
34+
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.SERVER_ONLY;
3335

34-
class VersionSpecificNativeChecks {
36+
class NativeActions {
3537

38+
@EntitlementTest(expectedAccess = SERVER_ONLY)
3639
static void enableNativeAccess() throws Exception {
3740
ModuleLayer parent = ModuleLayer.boot();
3841

@@ -49,16 +52,19 @@ static void enableNativeAccess() throws Exception {
4952
controller.enableNativeAccess(targetModule.get());
5053
}
5154

55+
@EntitlementTest(expectedAccess = PLUGINS)
5256
static void addressLayoutWithTargetLayout() {
5357
AddressLayout addressLayout = ADDRESS.withoutTargetLayout();
5458
addressLayout.withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, ValueLayout.JAVA_BYTE));
5559
}
5660

61+
@EntitlementTest(expectedAccess = PLUGINS)
5762
static void linkerDowncallHandle() {
5863
Linker linker = Linker.nativeLinker();
5964
linker.downcallHandle(FunctionDescriptor.of(JAVA_LONG, ADDRESS));
6065
}
6166

67+
@EntitlementTest(expectedAccess = PLUGINS)
6268
static void linkerDowncallHandleWithAddress() {
6369
Linker linker = Linker.nativeLinker();
6470
linker.downcallHandle(linker.defaultLookup().find("strlen").get(), FunctionDescriptor.of(JAVA_LONG, ADDRESS));
@@ -68,12 +74,13 @@ static int callback() {
6874
return 0;
6975
}
7076

77+
@EntitlementTest(expectedAccess = PLUGINS)
7178
static void linkerUpcallStub() throws NoSuchMethodException {
7279
Linker linker = Linker.nativeLinker();
7380

7481
MethodHandle mh = null;
7582
try {
76-
mh = MethodHandles.lookup().findStatic(VersionSpecificNativeChecks.class, "callback", MethodType.methodType(int.class));
83+
mh = MethodHandles.lookup().findStatic(NativeActions.class, "callback", MethodType.methodType(int.class));
7784
} catch (IllegalAccessException e) {
7885
assert false;
7986
}
@@ -82,24 +89,28 @@ static void linkerUpcallStub() throws NoSuchMethodException {
8289
linker.upcallStub(mh, callbackDescriptor, Arena.ofAuto());
8390
}
8491

92+
@EntitlementTest(expectedAccess = PLUGINS)
8593
static void memorySegmentReinterpret() {
8694
Arena arena = Arena.ofAuto();
8795
MemorySegment segment = arena.allocate(100);
8896
segment.reinterpret(50);
8997
}
9098

99+
@EntitlementTest(expectedAccess = PLUGINS)
91100
static void memorySegmentReinterpretWithCleanup() {
92101
Arena arena = Arena.ofAuto();
93102
MemorySegment segment = arena.allocate(100);
94103
segment.reinterpret(Arena.ofAuto(), s -> {});
95104
}
96105

106+
@EntitlementTest(expectedAccess = PLUGINS)
97107
static void memorySegmentReinterpretWithSizeAndCleanup() {
98108
Arena arena = Arena.ofAuto();
99109
MemorySegment segment = arena.allocate(100);
100110
segment.reinterpret(50, Arena.ofAuto(), s -> {});
101111
}
102112

113+
@EntitlementTest(expectedAccess = PLUGINS)
103114
static void symbolLookupWithPath() {
104115
try {
105116
SymbolLookup.libraryLookup(Path.of("/foo/bar/libFoo.so"), Arena.ofAuto());
@@ -108,6 +119,7 @@ static void symbolLookupWithPath() {
108119
}
109120
}
110121

122+
@EntitlementTest(expectedAccess = PLUGINS)
111123
static void symbolLookupWithName() {
112124
try {
113125
SymbolLookup.libraryLookup("foo", Arena.ofAuto());

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/RestEntitlementsCheckAction.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,12 @@ static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
180180
entry("runtime_load", forPlugins(LoadNativeLibrariesCheckActions::runtimeLoad)),
181181
entry("runtime_load_library", forPlugins(LoadNativeLibrariesCheckActions::runtimeLoadLibrary)),
182182
entry("system_load", forPlugins(LoadNativeLibrariesCheckActions::systemLoad)),
183-
entry("system_load_library", forPlugins(LoadNativeLibrariesCheckActions::systemLoadLibrary)),
184-
entry("enable_native_access", new CheckAction(VersionSpecificNativeChecks::enableNativeAccess, false, 22)),
185-
entry("address_target_layout", new CheckAction(VersionSpecificNativeChecks::addressLayoutWithTargetLayout, false, 22)),
186-
entry("donwncall_handle", new CheckAction(VersionSpecificNativeChecks::linkerDowncallHandle, false, 22)),
187-
entry(
188-
"donwncall_handle_with_address",
189-
new CheckAction(VersionSpecificNativeChecks::linkerDowncallHandleWithAddress, false, 22)
190-
),
191-
entry("upcall_stub", new CheckAction(VersionSpecificNativeChecks::linkerUpcallStub, false, 22)),
192-
entry("reinterpret", new CheckAction(VersionSpecificNativeChecks::memorySegmentReinterpret, false, 22)),
193-
entry("reinterpret_cleanup", new CheckAction(VersionSpecificNativeChecks::memorySegmentReinterpretWithCleanup, false, 22)),
194-
entry(
195-
"reinterpret_size_cleanup",
196-
new CheckAction(VersionSpecificNativeChecks::memorySegmentReinterpretWithSizeAndCleanup, false, 22)
197-
),
198-
entry("symbol_lookup_name", new CheckAction(VersionSpecificNativeChecks::symbolLookupWithName, false, 22)),
199-
entry("symbol_lookup_path", new CheckAction(VersionSpecificNativeChecks::symbolLookupWithPath, false, 22))
183+
entry("system_load_library", forPlugins(LoadNativeLibrariesCheckActions::systemLoadLibrary))
200184
),
201185
getTestEntries(FileCheckActions.class),
202186
getTestEntries(SpiActions.class),
203-
getTestEntries(SystemActions.class)
187+
getTestEntries(SystemActions.class),
188+
getTestEntries(NativeActions.class)
204189
)
205190
.flatMap(Function.identity())
206191
.filter(entry -> entry.getValue().fromJavaVersion() == null || Runtime.version().feature() >= entry.getValue().fromJavaVersion())

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/VersionSpecificNativeChecks.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/api/ElasticsearchEntitlementChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ public ElasticsearchEntitlementChecker(PolicyManager policyManager) {
908908
ModuleLayer.Controller that,
909909
Module target
910910
) {
911-
policyManager.checkLoadingNativeLibraries(callerClass);
911+
policyManager.checkChangeJVMGlobalState(callerClass);
912912
}
913913

914914
/// /////////////////

libs/native/src/main/java/org/elasticsearch/nativeaccess/NativeAccessUtil.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111

1212
public class NativeAccessUtil {
1313
/**
14-
* Enables native access for the provided module. No-op for JDK 21 or before.
14+
* Enables native access for the provided module.
15+
* We need to have this adapter even if the method is available in JDK 21, as it was in preview.
16+
* Available to JDK 22+, required for JDK 24+ when using --illegal-native-access=deny
1517
*/
16-
public static void enableNativeAccess(ModuleLayer.Controller controller, Module module) {}
18+
public static void enableNativeAccess(ModuleLayer.Controller controller, Module module) {
19+
controller.enableNativeAccess(module);
20+
}
1721

1822
public static boolean isNativeAccessEnabled(Module module) {
19-
return true;
23+
return module.isNativeAccessEnabled();
2024
}
2125
}

libs/native/src/main22/java/org/elasticsearch/nativeaccess/NativeAccessUtil.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)