|
| 1 | +package datadog.trace.agent.tooling.servicediscovery; |
| 2 | + |
| 3 | +import java.lang.foreign.Arena; |
| 4 | +import java.lang.foreign.FunctionDescriptor; |
| 5 | +import java.lang.foreign.Linker; |
| 6 | +import java.lang.foreign.MemoryLayout; |
| 7 | +import java.lang.foreign.MemorySegment; |
| 8 | +import java.lang.foreign.StructLayout; |
| 9 | +import java.lang.foreign.SymbolLookup; |
| 10 | +import java.lang.foreign.ValueLayout; |
| 11 | +import java.lang.invoke.MethodHandle; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +public class MemFDUnixWriterFFM extends MemFDUnixWriter { |
| 16 | + private static final Logger log = LoggerFactory.getLogger(MemFDUnixWriterFFM.class); |
| 17 | + |
| 18 | + // Captured call state layout for errno |
| 19 | + private static final StructLayout CAPTURE_STATE_LAYOUT = Linker.Option.captureStateLayout(); |
| 20 | + private static final long ERRNO_OFFSET = |
| 21 | + CAPTURE_STATE_LAYOUT.byteOffset(MemoryLayout.PathElement.groupElement("errno")); |
| 22 | + |
| 23 | + // Function handles - initialized once |
| 24 | + private final MethodHandle syscallMH; |
| 25 | + private final MethodHandle writeMH; |
| 26 | + private final MethodHandle fcntlMH; |
| 27 | + |
| 28 | + private final MemorySegment captureState; |
| 29 | + |
| 30 | + public MemFDUnixWriterFFM() { |
| 31 | + final Linker linker = Linker.nativeLinker(); |
| 32 | + final SymbolLookup LIBC = linker.defaultLookup(); |
| 33 | + |
| 34 | + // Allocate memory for capturing errno (need to be alive until the class instance is collected) |
| 35 | + this.captureState = Arena.ofAuto().allocate(CAPTURE_STATE_LAYOUT); |
| 36 | + |
| 37 | + // long syscall(long number, ...) |
| 38 | + // Note: variadic functions require special handling, we'll use a fixed signature |
| 39 | + syscallMH = |
| 40 | + linker.downcallHandle( |
| 41 | + LIBC.find("syscall").orElseThrow(), |
| 42 | + FunctionDescriptor.of( |
| 43 | + ValueLayout.JAVA_LONG, // return type: long |
| 44 | + ValueLayout.JAVA_LONG, // syscall number |
| 45 | + ValueLayout.ADDRESS, // const char* name |
| 46 | + ValueLayout.JAVA_INT // int flags |
| 47 | + ), |
| 48 | + Linker.Option.captureCallState("errno")); |
| 49 | + |
| 50 | + // ssize_t write(int fd, const void *buf, size_t count) |
| 51 | + writeMH = |
| 52 | + linker.downcallHandle( |
| 53 | + LIBC.find("write").orElseThrow(), |
| 54 | + FunctionDescriptor.of( |
| 55 | + ValueLayout.JAVA_LONG, // return type: ssize_t |
| 56 | + ValueLayout.JAVA_INT, // int fd |
| 57 | + ValueLayout.ADDRESS, // const void* buf |
| 58 | + ValueLayout.JAVA_LONG // size_t count |
| 59 | + ), |
| 60 | + Linker.Option.captureCallState("errno")); |
| 61 | + |
| 62 | + // int fcntl(int fd, int cmd, ... /* arg */) |
| 63 | + fcntlMH = |
| 64 | + linker.downcallHandle( |
| 65 | + LIBC.find("fcntl").orElseThrow(), |
| 66 | + FunctionDescriptor.of( |
| 67 | + ValueLayout.JAVA_INT, // return type: int |
| 68 | + ValueLayout.JAVA_INT, // int fd |
| 69 | + ValueLayout.JAVA_INT, // int cmd |
| 70 | + ValueLayout.JAVA_INT // int arg |
| 71 | + ), |
| 72 | + Linker.Option.captureCallState("errno")); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected long syscall(long number, String name, int flags) { |
| 77 | + try (Arena arena = Arena.ofConfined()) { |
| 78 | + // Allocate native string for file name |
| 79 | + MemorySegment fileNameSegment = arena.allocateFrom(name); |
| 80 | + // Call memfd_create via syscall, passing captureState as first arg |
| 81 | + return (long) syscallMH.invoke(captureState, (long) number, fileNameSegment, flags); |
| 82 | + } catch (Throwable t) { |
| 83 | + log.error("Unable to make a syscall through FFM", t); |
| 84 | + return -1; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected long write(int fd, byte[] payload) { |
| 90 | + try (Arena arena = Arena.ofConfined()) { |
| 91 | + // Allocate native memory for payload |
| 92 | + MemorySegment buffer = arena.allocate(payload.length); |
| 93 | + MemorySegment.copy(payload, 0, buffer, ValueLayout.JAVA_BYTE, 0, payload.length); |
| 94 | + |
| 95 | + // Write payload to memfd, passing captureState as first arg |
| 96 | + return (long) writeMH.invoke(captureState, fd, buffer, (long) payload.length); |
| 97 | + } catch (Throwable t) { |
| 98 | + log.error("Unable to make a write call through FFM", t); |
| 99 | + return -1; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected int fcntl(int fd, int cmd, int arg) { |
| 105 | + try { |
| 106 | + return (int) fcntlMH.invoke(captureState, fd, cmd, arg); |
| 107 | + } catch (Throwable t) { |
| 108 | + log.error("Unable to make a fcntl call through FFM", t); |
| 109 | + return -1; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected int getLastError() { |
| 115 | + try { |
| 116 | + // Read errno from the captured state memory segment |
| 117 | + return captureState.get(ValueLayout.JAVA_INT, ERRNO_OFFSET); |
| 118 | + } catch (Throwable t) { |
| 119 | + log.error("Unable to read errno from captured state", t); |
| 120 | + return -1; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments