From 60f7b89a1bea05e35552a3b423f1682b64311bbe Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 1 Feb 2022 15:09:23 +0100 Subject: [PATCH] Don't leak temporary file during test and inital loading of lib. (#141) Motivation: We did leak a file during our tests and also during the initial loading of the native lib Modifications: Use a tempory file / directory and so ensure the file is deleted Result: No leaked file --- .../netty/incubator/channel/uring/Native.java | 19 ++++++++++++------- .../src/main/c/netty_io_uring_native.c | 10 +++++++--- .../incubator/channel/uring/NativeTest.java | 8 ++++++-- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/transport-classes-io_uring/src/main/java/io/netty/incubator/channel/uring/Native.java b/transport-classes-io_uring/src/main/java/io/netty/incubator/channel/uring/Native.java index 12c32577..f80bbdb3 100644 --- a/transport-classes-io_uring/src/main/java/io/netty/incubator/channel/uring/Native.java +++ b/transport-classes-io_uring/src/main/java/io/netty/incubator/channel/uring/Native.java @@ -26,8 +26,10 @@ import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; +import java.io.File; import java.io.IOException; import java.nio.channels.Selector; +import java.nio.file.Path; import java.util.Arrays; import java.util.Locale; @@ -59,14 +61,17 @@ final class Native { PeerCredentials.class, java.io.FileDescriptor.class ); + File tmpDir = PlatformDependent.tmpdir(); + Path tmpFile = tmpDir.toPath().resolve("netty_io_uring.tmp"); try { // First, try calling a side-effect free JNI method to see if the library was already // loaded by the application. - Native.createFile(); + Native.createFile(tmpFile.toString()); } catch (UnsatisfiedLinkError ignore) { // The library was not previously loaded, load it now. loadNativeLibrary(); } finally { + tmpFile.toFile().delete(); try { if (selector != null) { selector.close(); @@ -256,23 +261,23 @@ private static boolean checkKernelVersion0(String kernelVersion) { private static native boolean ioUringProbe(int ringFd, int[] ios); private static native long[][] ioUringSetup(int entries); - public static native int ioUringEnter(int ringFd, int toSubmit, int minComplete, int flags); + static native int ioUringEnter(int ringFd, int toSubmit, int minComplete, int flags); - public static native void eventFdWrite(int fd, long value); + static native void eventFdWrite(int fd, long value); - public static FileDescriptor newBlockingEventFd() { + static FileDescriptor newBlockingEventFd() { return new FileDescriptor(blockingEventFd()); } - public static native void ioUringExit(long submissionQueueArrayAddress, int submissionQueueRingEntries, + static native void ioUringExit(long submissionQueueArrayAddress, int submissionQueueRingEntries, long submissionQueueRingAddress, int submissionQueueRingSize, long completionQueueRingAddress, int completionQueueRingSize, int ringFd); private static native int blockingEventFd(); - // for testing(it is only temporary) - public static native int createFile(); + // for testing only! + static native int createFile(String name); private static native int registerUnix(); diff --git a/transport-native-io_uring/src/main/c/netty_io_uring_native.c b/transport-native-io_uring/src/main/c/netty_io_uring_native.c index 5db4c64d..0da6f026 100644 --- a/transport-native-io_uring/src/main/c/netty_io_uring_native.c +++ b/transport-native-io_uring/src/main/c/netty_io_uring_native.c @@ -296,8 +296,12 @@ static jobjectArray netty_io_uring_setup(JNIEnv *env, jclass clazz, jint entries return array; } -static jint netty_create_file(JNIEnv *env, jclass class) { - return open("io-uring-test.txt", O_RDWR | O_TRUNC | O_CREAT, 0644); +static jint netty_create_file(JNIEnv *env, jclass class, jstring filename) { + const char *file = (*env)->GetStringUTFChars(env, filename, 0); + + int fd = open(file, O_RDWR | O_TRUNC | O_CREAT, 0644); + (*env)->ReleaseStringUTFChars(env, filename, file); + return fd; } @@ -592,7 +596,7 @@ static const JNINativeMethod method_table[] = { {"ioUringSetup", "(I)[[J", (void *) netty_io_uring_setup}, {"ioUringProbe", "(I[I)Z", (void *) netty_io_uring_probe}, {"ioUringExit", "(JIJIJII)V", (void *) netty_io_uring_ring_buffer_exit}, - {"createFile", "()I", (void *) netty_create_file}, + {"createFile", "(Ljava/lang/String;)I", (void *) netty_create_file}, {"ioUringEnter", "(IIII)I", (void *) netty_io_uring_enter}, {"blockingEventFd", "()I", (void *) netty_epoll_native_blocking_event_fd}, {"eventFdWrite", "(IJ)V", (void *) netty_io_uring_eventFdWrite }, diff --git a/transport-native-io_uring/src/test/java/io/netty/incubator/channel/uring/NativeTest.java b/transport-native-io_uring/src/test/java/io/netty/incubator/channel/uring/NativeTest.java index 8e55ef5d..9cbc7df3 100644 --- a/transport-native-io_uring/src/test/java/io/netty/incubator/channel/uring/NativeTest.java +++ b/transport-native-io_uring/src/test/java/io/netty/incubator/channel/uring/NativeTest.java @@ -17,7 +17,9 @@ import io.netty.channel.unix.FileDescriptor; +import java.io.File; import java.nio.charset.Charset; +import java.nio.file.Path; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicReference; @@ -37,6 +39,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.io.TempDir; public class NativeTest { @@ -46,13 +49,14 @@ public static void loadJNI() { } @Test - public void canWriteFile() throws Exception { + public void canWriteFile(@TempDir Path tmpDir) throws Exception { ByteBufAllocator allocator = new UnpooledByteBufAllocator(true); final ByteBuf writeEventByteBuf = allocator.directBuffer(100); final String inputString = "Hello World!"; writeEventByteBuf.writeCharSequence(inputString, Charset.forName("UTF-8")); - int fd = Native.createFile(); + Path file = tmpDir.resolve("io_uring.tmp"); + int fd = Native.createFile(file.toString()); RingBuffer ringBuffer = Native.createRingBuffer(32); IOUringSubmissionQueue submissionQueue = ringBuffer.ioUringSubmissionQueue();