Skip to content

Commit 9552ea2

Browse files
authored
[Entitlements] Add native checks support for preview (pre-22 Java versions) (elastic#121798)
1 parent be85bfb commit 9552ea2

File tree

25 files changed

+981
-227
lines changed

25 files changed

+981
-227
lines changed

libs/entitlement/bridge/build.gradle

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@ apply plugin: 'elasticsearch.mrjar'
1414

1515
tasks.named('jar').configure {
1616
// guarding for intellij
17-
if (sourceSets.findByName("main23")) {
18-
from sourceSets.main23.output
17+
if (sourceSets.findByName("main19")) {
18+
from sourceSets.main19.output
19+
}
20+
if (sourceSets.findByName("main20")) {
21+
from sourceSets.main20.output
1922
}
2023
if (sourceSets.findByName("main21")) {
2124
from sourceSets.main21.output
2225
}
2326
if (sourceSets.findByName("main22")) {
2427
from sourceSets.main22.output
2528
}
29+
if (sourceSets.findByName("main23")) {
30+
from sourceSets.main23.output
31+
}
2632
}
2733

2834
// The bridge only uses things within the jdk, but the checker
@@ -31,3 +37,9 @@ tasks.named('jar').configure {
3137
tasks.withType(CheckForbiddenApisTask).configureEach {
3238
enabled = false
3339
}
40+
41+
// EntitlementChecker interfaces may contain long URLs pointing to JDK code references on GH, or to JDK documentation,
42+
// and @SuppressWarnings for checkstyle does not work for mrjar projects
43+
tasks.withType(Checkstyle).configureEach {
44+
exclude "**/*EntitlementChecker.java"
45+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.entitlement.bridge;
11+
12+
public interface Java19EntitlementChecker extends Java19PreviewEntitlementChecker, EntitlementChecker {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.entitlement.bridge;
11+
12+
/**
13+
* Java19 variant of {@link EntitlementChecker} handle holder.
14+
*/
15+
public class Java19EntitlementCheckerHandle {
16+
17+
public static Java19EntitlementChecker instance() {
18+
return Holder.instance;
19+
}
20+
21+
private static class Holder {
22+
private static final Java19EntitlementChecker instance = HandleLoader.load(Java19EntitlementChecker.class);
23+
}
24+
25+
// no construction
26+
private Java19EntitlementCheckerHandle() {}
27+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.entitlement.bridge;
11+
12+
import java.lang.foreign.Addressable;
13+
import java.lang.foreign.FunctionDescriptor;
14+
import java.lang.foreign.Linker;
15+
import java.lang.foreign.MemoryAddress;
16+
import java.lang.foreign.MemorySession;
17+
import java.lang.invoke.MethodHandle;
18+
import java.nio.file.Path;
19+
20+
/**
21+
* Interface with Java19 Preview specific functions and types.
22+
* This interface must be kept isolated, as we cannot inherit from it in subsequent Java-specific versions as it contains types that
23+
* were removed in the following previews/in final code (like MemorySession or MemoryAddress)
24+
*/
25+
public interface Java19PreviewEntitlementChecker {
26+
27+
/**
28+
* downcallHandle has a different signature in Java 19.
29+
* See docs: https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/foreign/Linker.html#downcallHandle(java.lang.foreign.FunctionDescriptor)
30+
*
31+
* Its only allowed implementation is in AbstractLinker:
32+
* https://github.com/openjdk/jdk19u/blob/677bec11078ff41c21821fec46590752e0fc5128/src/java.base/share/classes/jdk/internal/foreign/abi/AbstractLinker.java#L47
33+
*/
34+
void check$jdk_internal_foreign_abi_AbstractLinker$downcallHandle(Class<?> callerClass, Linker that, FunctionDescriptor function);
35+
36+
/**
37+
* downcallHandle has a different signature in Java 19, and it is a default interface method. Later implementations (Java 21+)
38+
* use an implementation class for this overload too.
39+
* See docs: https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/foreign/Linker.html#downcallHandle(java.lang.foreign.Addressable,java.lang.foreign.FunctionDescriptor)
40+
*/
41+
void check$java_lang_foreign_Linker$downcallHandle(Class<?> callerClass, Linker that, Addressable address, FunctionDescriptor function);
42+
43+
/**
44+
* upcallStub has a different signature in Java 19,
45+
* Its only allowed implementation is in AbstractLinker:
46+
* https://github.com/openjdk/jdk19u/blob/677bec11078ff41c21821fec46590752e0fc5128/src/java.base/share/classes/jdk/internal/foreign/abi/AbstractLinker.java#L60
47+
*/
48+
void check$jdk_internal_foreign_abi_AbstractLinker$upcallStub(
49+
Class<?> callerClass,
50+
Linker that,
51+
MethodHandle target,
52+
FunctionDescriptor function,
53+
MemorySession scope
54+
);
55+
56+
/**
57+
* This function has a different signature in Java 20.
58+
* See docs: https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/foreign/MemorySegment.html#ofAddress(java.lang.foreign.MemoryAddress,long,java.lang.foreign.MemorySession)
59+
*
60+
* It is superseded by {@code MemorySegment.reinterpret} in the final
61+
* implementation (Java 21+)
62+
*/
63+
void check$java_lang_foreign_MemorySegment$$ofAddress(
64+
Class<?> callerClass,
65+
MemoryAddress address,
66+
long byteSize,
67+
MemorySession session
68+
);
69+
70+
/**
71+
* This function signature changes from Java 19 to Java 20 (MemorySession parameter).
72+
*/
73+
void check$java_lang_foreign_SymbolLookup$$libraryLookup(Class<?> callerClass, String name, MemorySession session);
74+
75+
/**
76+
* This function signature changes from Java 19 to Java 20 (MemorySession parameter).
77+
*/
78+
void check$java_lang_foreign_SymbolLookup$$libraryLookup(Class<?> callerClass, Path path, MemorySession session);
79+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.entitlement.bridge;
11+
12+
public interface Java20EntitlementChecker extends Java20StableEntitlementChecker, Java20PreviewEntitlementChecker {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.entitlement.bridge;
11+
12+
/**
13+
* Java20 variant of {@link EntitlementChecker} handle holder.
14+
*/
15+
public class Java20EntitlementCheckerHandle {
16+
17+
public static Java20EntitlementChecker instance() {
18+
return Holder.instance;
19+
}
20+
21+
private static class Holder {
22+
private static final Java20EntitlementChecker instance = HandleLoader.load(Java20EntitlementChecker.class);
23+
}
24+
25+
// no construction
26+
private Java20EntitlementCheckerHandle() {}
27+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.entitlement.bridge;
11+
12+
import java.lang.foreign.FunctionDescriptor;
13+
import java.lang.foreign.Linker;
14+
import java.lang.foreign.MemorySegment;
15+
import java.lang.foreign.SegmentScope;
16+
import java.lang.foreign.ValueLayout;
17+
import java.lang.invoke.MethodHandle;
18+
import java.nio.file.Path;
19+
20+
/**
21+
* Interface with Java20 Preview specific functions and types.
22+
* This interface must be kept isolated, as we cannot inherit from it in subsequent Java-specific versions as it contains types that
23+
* were removed in the following previews/in final code (like MemorySession or MemoryAddress)
24+
*/
25+
public interface Java20PreviewEntitlementChecker {
26+
27+
/**
28+
* This downcallHandle overload has its final signature in Java 20.
29+
* See docs: https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/foreign/Linker.html#downcallHandle(java.lang.foreign.MemorySegment,java.lang.foreign.FunctionDescriptor,java.lang.foreign.Linker.Option...)
30+
*
31+
* However in Java 20 it is implemented as a default interface method.
32+
* Later implementations (Java 21+) use an implementation class for this overload too, so we need a specific check method for this.
33+
* See https://github.com/openjdk/jdk20u/blob/9ced461a4d8cb2ecfe2d6a74ec218ec589dcd617/src/java.base/share/classes/java/lang/foreign/Linker.java#L211
34+
*/
35+
void check$java_lang_foreign_Linker$downcallHandle(
36+
Class<?> callerClass,
37+
Linker that,
38+
MemorySegment address,
39+
FunctionDescriptor function,
40+
Linker.Option... options
41+
);
42+
43+
/**
44+
* upcallStub has a different signature in Java 20 (SegmentScope parameter),
45+
* Its only allowed implementation is in AbstractLinker:
46+
* https://github.com/openjdk/jdk20u/blob/9ced461a4d8cb2ecfe2d6a74ec218ec589dcd617/src/java.base/share/classes/jdk/internal/foreign/abi/AbstractLinker.java#L69
47+
*/
48+
void check$jdk_internal_foreign_abi_AbstractLinker$upcallStub(
49+
Class<?> callerClass,
50+
Linker that,
51+
MethodHandle target,
52+
FunctionDescriptor function,
53+
SegmentScope scope
54+
);
55+
56+
/**
57+
* This function signature changes from Java 19 to Java 20.
58+
* It is superseded by {@code MemorySegment.reinterpret} in the final implementation (Java 21+)
59+
*/
60+
void check$java_lang_foreign_MemorySegment$$ofAddress(Class<?> callerClass, long address);
61+
62+
/**
63+
* This function signature changes from Java 19 to Java 20.
64+
* See docs: https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/foreign/MemorySegment.html#ofAddress(long,long,java.lang.foreign.SegmentScope)
65+
*
66+
* It is superseded by {@code MemorySegment.reinterpret} in the final implementation (Java 21+)
67+
* See https://github.com/openjdk/jdk20u/blob/9ced461a4d8cb2ecfe2d6a74ec218ec589dcd617/src/java.base/share/classes/java/lang/foreign/MemorySegment.java#L1071C5-L1071C64
68+
*/
69+
void check$java_lang_foreign_MemorySegment$$ofAddress(Class<?> callerClass, long address, long byteSize);
70+
71+
/**
72+
* This function overload is new to Java 20.
73+
* It is superseded by {@code MemorySegment.reinterpret} in the final implementation (Java 21+)
74+
*/
75+
void check$java_lang_foreign_MemorySegment$$ofAddress(Class<?> callerClass, long address, long byteSize, SegmentScope scope);
76+
77+
/**
78+
* This function overload is new to Java 20.
79+
* It is superseded by {@code MemorySegment.reinterpret} in the final implementation (Java 21+)
80+
*/
81+
void check$java_lang_foreign_MemorySegment$$ofAddress(
82+
Class<?> callerClass,
83+
long address,
84+
long byteSize,
85+
SegmentScope scope,
86+
Runnable cleanupAction
87+
);
88+
89+
/**
90+
* This function is specific to Java 20.
91+
* It is superseded by {@code MemorySegment.reinterpret} in the final implementation (Java 21+)
92+
* See https://github.com/openjdk/jdk20u/blob/9ced461a4d8cb2ecfe2d6a74ec218ec589dcd617/src/java.base/share/classes/jdk/internal/foreign/layout/ValueLayouts.java#L442
93+
*/
94+
void check$jdk_internal_foreign_layout_ValueLayouts$OfAddressImpl$asUnbounded(Class<?> callerClass, ValueLayout.OfAddress that);
95+
96+
/**
97+
* This function signature changes from Java 20 to Java 21 (SegmentScope parameter).
98+
*/
99+
void check$java_lang_foreign_SymbolLookup$$libraryLookup(Class<?> callerClass, String name, SegmentScope scope);
100+
101+
/**
102+
* This function signature changes from Java 20 to Java 21 (SegmentScope parameter).
103+
*/
104+
void check$java_lang_foreign_SymbolLookup$$libraryLookup(Class<?> callerClass, Path path, SegmentScope scope);
105+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.entitlement.bridge;
11+
12+
import java.lang.foreign.FunctionDescriptor;
13+
import java.lang.foreign.Linker;
14+
15+
/**
16+
* Interface with Java20 "stable" functions and types.
17+
* It inherits from the previous "stable" interface, in this case {@link EntitlementChecker} as there is no Java19-specific stable
18+
* API to instrument.
19+
*/
20+
public interface Java20StableEntitlementChecker extends EntitlementChecker {
21+
22+
/**
23+
* This overload of downcallHandle has its final form starting from Java 20
24+
* See docs: https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/foreign/Linker.html#downcallHandle(java.lang.foreign.FunctionDescriptor,java.lang.foreign.Linker.Option...)
25+
*
26+
* Its only allowed implementation is in AbstractLinker:
27+
* https://github.com/openjdk/jdk20u/blob/9ced461a4d8cb2ecfe2d6a74ec218ec589dcd617/src/java.base/share/classes/jdk/internal/foreign/abi/AbstractLinker.java#L52
28+
*/
29+
void check$jdk_internal_foreign_abi_AbstractLinker$downcallHandle(
30+
Class<?> callerClass,
31+
Linker that,
32+
FunctionDescriptor function,
33+
Linker.Option... options
34+
);
35+
}

0 commit comments

Comments
 (0)