Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MemFDUnixWriter implements ForeignMemoryWriter {
private static final Logger log = LoggerFactory.getLogger(MemFDUnixWriter.class);

private interface LibC extends Library {
int memfd_create(String name, int flags);
int syscall(int number, Object... args);

NativeLong write(int fd, Pointer buf, NativeLong count);

Expand All @@ -36,7 +36,13 @@ private interface LibC extends Library {
public void write(String fileName, byte[] payload) {
final LibC libc = Native.load("c", LibC.class);

int memFd = libc.memfd_create(fileName, MFD_CLOEXEC | MFD_ALLOW_SEALING);
String arch = System.getProperty("os.arch");
int memfdSyscall = getMemfdSyscall(arch);
if (memfdSyscall <= 0) {
log.debug("service discovery not supported for arch={}", arch);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you interested in sending those to the telemetry? Otherwise you won't know unless the customer activates the debug logging and send a flares. In this case you have a SEND_TELEMETRY marker

return;
}
int memFd = libc.syscall(memfdSyscall, fileName, MFD_CLOEXEC | MFD_ALLOW_SEALING);
if (memFd < 0) {
log.warn("{} memfd create failed, errno={}", fileName, Native.getLastError());
return;
Expand All @@ -60,4 +66,36 @@ public void write(String fileName, byte[] payload) {
}
// memfd is not closed to keep it readable for the lifetime of the process.
}

private static int getMemfdSyscall(String arch) {
switch (arch.toLowerCase()) {
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/x86_64/bits/syscall.h.in#L320
case "x86_64":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could consolidate this by allowing the case statements to fallthrough.
case "x86_64":
case "x64":
case "amd64":
return 319;

Just a suggestion, I don't feel strongly about it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at these "magic numbers" makes every bone in my body shake, but because I can't think of a better idea that doesnt require a C shim 😅 or preprocessing of the source code 🫣 I'll stay quiet and say this lgtm 😜

return 319;
case "x64":
return 319;
case "amd64":
return 319;
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/i386/bits/syscall.h.in#L356
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: same as above.

Also, does makes sense to have a x86 case ?

case "x386":
return 356;
case "86":
return 356;
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/aarch64/bits/syscall.h.in#L264
case "aarch64":
return 279;
case "arm64":
return 279;
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/arm/bits/syscall.h.in#L343
case "arm":
return 385;
case "arm32":
return 385;
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/powerpc64/bits/syscall.h.in#L350
case "ppc64":
return 360;
default:
return -1;
}
}
}
Loading