Skip to content

Commit 865effe

Browse files
committed
Add native check support and tests for preview version (21)
1 parent d7db1f5 commit 865effe

File tree

6 files changed

+113
-165
lines changed

6 files changed

+113
-165
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,22 @@ static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
181181
entry("runtime_load_library", forPlugins(LoadNativeLibrariesCheckActions::runtimeLoadLibrary)),
182182
entry("system_load", forPlugins(LoadNativeLibrariesCheckActions::systemLoad)),
183183
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)),
184+
entry("enable_native_access", deniedToPlugins(VersionSpecificNativeChecks::enableNativeAccess)),
185+
entry("address_target_layout", new CheckAction(VersionSpecificNativeChecks::addressLayoutWithTargetLayout, false, 21)),
186+
entry("donwncall_handle", new CheckAction(VersionSpecificNativeChecks::linkerDowncallHandle, false, 21)),
187187
entry(
188188
"donwncall_handle_with_address",
189-
new CheckAction(VersionSpecificNativeChecks::linkerDowncallHandleWithAddress, false, 22)
189+
new CheckAction(VersionSpecificNativeChecks::linkerDowncallHandleWithAddress, false, 21)
190190
),
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)),
191+
entry("upcall_stub", new CheckAction(VersionSpecificNativeChecks::linkerUpcallStub, false, 21)),
192+
entry("reinterpret", new CheckAction(VersionSpecificNativeChecks::memorySegmentReinterpret, false, 21)),
193+
entry("reinterpret_cleanup", new CheckAction(VersionSpecificNativeChecks::memorySegmentReinterpretWithCleanup, false, 21)),
194194
entry(
195195
"reinterpret_size_cleanup",
196-
new CheckAction(VersionSpecificNativeChecks::memorySegmentReinterpretWithSizeAndCleanup, false, 22)
196+
new CheckAction(VersionSpecificNativeChecks::memorySegmentReinterpretWithSizeAndCleanup, false, 21)
197197
),
198-
entry("symbol_lookup_name", new CheckAction(VersionSpecificNativeChecks::symbolLookupWithName, false, 22)),
199-
entry("symbol_lookup_path", new CheckAction(VersionSpecificNativeChecks::symbolLookupWithPath, false, 22))
198+
entry("symbol_lookup_name", new CheckAction(VersionSpecificNativeChecks::symbolLookupWithName, false, 21)),
199+
entry("symbol_lookup_path", new CheckAction(VersionSpecificNativeChecks::symbolLookupWithPath, false, 21))
200200
),
201201
getTestEntries(FileCheckActions.class),
202202
getTestEntries(SpiActions.class),

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

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,110 @@
99

1010
package org.elasticsearch.entitlement.qa.test;
1111

12+
import org.elasticsearch.entitlement.qa.entitled.EntitledPlugin;
13+
14+
import java.lang.foreign.AddressLayout;
15+
import java.lang.foreign.Arena;
16+
import java.lang.foreign.FunctionDescriptor;
17+
import java.lang.foreign.Linker;
18+
import java.lang.foreign.MemoryLayout;
19+
import java.lang.foreign.MemorySegment;
20+
import java.lang.foreign.SymbolLookup;
21+
import java.lang.foreign.ValueLayout;
22+
import java.lang.invoke.MethodHandle;
23+
import java.lang.invoke.MethodHandles;
24+
import java.lang.invoke.MethodType;
25+
import java.lang.module.Configuration;
26+
import java.lang.module.ModuleFinder;
27+
import java.nio.file.Path;
28+
import java.util.List;
29+
import java.util.Set;
30+
31+
import static java.lang.foreign.ValueLayout.ADDRESS;
32+
import static java.lang.foreign.ValueLayout.JAVA_LONG;
33+
1234
class VersionSpecificNativeChecks {
1335

14-
static void enableNativeAccess() throws Exception {}
36+
static void enableNativeAccess() throws Exception {
37+
ModuleLayer parent = ModuleLayer.boot();
38+
39+
var location = EntitledPlugin.class.getProtectionDomain().getCodeSource().getLocation();
40+
41+
// We create a layer for our own module, so we have a controller to try and call enableNativeAccess on it.
42+
// This works in both the modular and non-modular case: the target module has to be present in the new layer, but its entitlements
43+
// and policies do not matter to us: we are checking that the caller is (or isn't) entitled to use enableNativeAccess
44+
Configuration cf = parent.configuration()
45+
.resolve(ModuleFinder.of(Path.of(location.toURI())), ModuleFinder.of(), Set.of("org.elasticsearch.entitlement.qa.entitled"));
46+
var controller = ModuleLayer.defineModulesWithOneLoader(cf, List.of(parent), ClassLoader.getSystemClassLoader());
47+
var targetModule = controller.layer().findModule("org.elasticsearch.entitlement.qa.entitled");
48+
49+
controller.enableNativeAccess(targetModule.get());
50+
}
51+
52+
static void addressLayoutWithTargetLayout() {
53+
AddressLayout addressLayout = ADDRESS.withoutTargetLayout();
54+
addressLayout.withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, ValueLayout.JAVA_BYTE));
55+
}
56+
57+
static void linkerDowncallHandle() {
58+
Linker linker = Linker.nativeLinker();
59+
linker.downcallHandle(FunctionDescriptor.of(JAVA_LONG, ADDRESS));
60+
}
61+
62+
static void linkerDowncallHandleWithAddress() {
63+
Linker linker = Linker.nativeLinker();
64+
linker.downcallHandle(linker.defaultLookup().find("strlen").get(), FunctionDescriptor.of(JAVA_LONG, ADDRESS));
65+
}
1566

16-
static void addressLayoutWithTargetLayout() {}
67+
static int callback() {
68+
return 0;
69+
}
1770

18-
static void linkerDowncallHandle() {}
71+
static void linkerUpcallStub() throws NoSuchMethodException {
72+
Linker linker = Linker.nativeLinker();
1973

20-
static void linkerDowncallHandleWithAddress() {}
74+
MethodHandle mh = null;
75+
try {
76+
mh = MethodHandles.lookup().findStatic(VersionSpecificNativeChecks.class, "callback", MethodType.methodType(int.class));
77+
} catch (IllegalAccessException e) {
78+
assert false;
79+
}
2180

22-
static void linkerUpcallStub() throws NoSuchMethodException {}
81+
FunctionDescriptor callbackDescriptor = FunctionDescriptor.of(ValueLayout.JAVA_INT);
82+
linker.upcallStub(mh, callbackDescriptor, Arena.ofAuto());
83+
}
2384

24-
static void memorySegmentReinterpret() {}
85+
static void memorySegmentReinterpret() {
86+
Arena arena = Arena.ofAuto();
87+
MemorySegment segment = arena.allocate(100);
88+
segment.reinterpret(50);
89+
}
2590

26-
static void memorySegmentReinterpretWithCleanup() {}
91+
static void memorySegmentReinterpretWithCleanup() {
92+
Arena arena = Arena.ofAuto();
93+
MemorySegment segment = arena.allocate(100);
94+
segment.reinterpret(Arena.ofAuto(), s -> {});
95+
}
2796

28-
static void memorySegmentReinterpretWithSizeAndCleanup() {}
97+
static void memorySegmentReinterpretWithSizeAndCleanup() {
98+
Arena arena = Arena.ofAuto();
99+
MemorySegment segment = arena.allocate(100);
100+
segment.reinterpret(50, Arena.ofAuto(), s -> {});
101+
}
29102

30-
static void symbolLookupWithPath() {}
103+
static void symbolLookupWithPath() {
104+
try {
105+
SymbolLookup.libraryLookup(Path.of("/foo/bar/libFoo.so"), Arena.ofAuto());
106+
} catch (IllegalArgumentException e) {
107+
// IllegalArgumentException is thrown if path does not point to a valid library (and it does not)
108+
}
109+
}
31110

32-
static void symbolLookupWithName() {}
111+
static void symbolLookupWithName() {
112+
try {
113+
SymbolLookup.libraryLookup("foo", Arena.ofAuto());
114+
} catch (IllegalArgumentException e) {
115+
// IllegalArgumentException is thrown if path does not point to a valid library (and it does not)
116+
}
117+
}
33118
}

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

Lines changed: 0 additions & 118 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)