Repository navigation
Expand file tree
/
Copy pathStandaloneTransportCheck.java
More file actions
72 lines (68 loc) 路 3.13 KB
/
Copy pathStandaloneTransportCheck.java
File metadata and controls
72 lines (68 loc) 路 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Copyright (c) 2024-2026 Elide Technologies, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
import dev.elide.bemo.transport.DriverSelection;
import dev.elide.bemo.transport.FfmTransportNative;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Linker;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
/** Plain JVM, no Elide bootstrap: a Netty echo through NativeIoHandler maps no Elide runtime. */
public final class StandaloneTransportCheck {
public static void main(String[] args) throws Throwable {
try {
Class.forName("dev.elide.netty.NativeTransport");
throw new AssertionError("Elide library discovery is on the classpath");
} catch (ClassNotFoundException expected) {
// The adapter and its bindings are the only Elide classes present.
}
NativeChannelTest.verify(new BackendTransport(new FfmTransportNative(Path.of(args[0]))));
DriverSelection selection = DriverSelection.observed();
if (selection == null) throw new AssertionError("event loop recorded no driver selection");
if (!mapped("bemo_ffi"))
throw new AssertionError("module enumeration cannot see the transport");
if (mapped("elideruntime")) throw new AssertionError("elideruntime was loaded");
System.out.println("Standalone transport check passed: driver " + selection.describe());
}
private static boolean mapped(String library) throws Throwable {
String file = System.mapLibraryName(library);
String os = System.getProperty("os.name");
if (os.equals("Linux"))
return Files.readAllLines(Path.of("/proc/self/maps")).stream()
.anyMatch(line -> line.endsWith("/" + file));
Linker linker = Linker.nativeLinker();
if (os.startsWith("Mac")) {
SymbolLookup system = linker.defaultLookup();
MethodHandle count =
linker.downcallHandle(
system.find("_dyld_image_count").orElseThrow(),
FunctionDescriptor.of(ValueLayout.JAVA_INT));
MethodHandle name =
linker.downcallHandle(
system.find("_dyld_get_image_name").orElseThrow(),
FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_INT));
for (int index = (int) count.invokeExact() - 1; index >= 0; index--) {
MemorySegment image = (MemorySegment) name.invokeExact(index);
if (!image.equals(MemorySegment.NULL)
&& image.reinterpret(4096).getString(0).endsWith("/" + file)) return true;
}
return false;
}
try (Arena arena = Arena.ofConfined()) {
MethodHandle handle =
linker.downcallHandle(
SymbolLookup.libraryLookup("kernel32", arena).find("GetModuleHandleW").orElseThrow(),
FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS));
MemorySegment module =
(MemorySegment) handle.invokeExact(arena.allocateFrom(file, StandardCharsets.UTF_16LE));
return !module.equals(MemorySegment.NULL);
}
}
}