From b57cba7cf25e5400c59834ddf2fa8531a104f5cf Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 1 Feb 2022 11:09:57 +0100 Subject: [PATCH 1/3] Don't leak temporary file during test and inital loading of lib. 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 | 11 +++++++++-- 3 files changed, 28 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..9f54f2e6 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(filename, 0); + + int fd = open(file, O_RDWR | O_TRUNC | O_CREAT, 0644); + env->ReleaseStringUTFChars(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..c1759782 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 { @@ -45,14 +48,18 @@ public static void loadJNI() { assumeTrue(IOUring.isAvailable()); } + + + @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(); From 0261d796e9d81c596258f29f1440cb1cb06f1acd Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 1 Feb 2022 11:45:14 +0100 Subject: [PATCH 2/3] Fix compile error --- transport-native-io_uring/src/main/c/netty_io_uring_native.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 9f54f2e6..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 @@ -297,10 +297,10 @@ static jobjectArray netty_io_uring_setup(JNIEnv *env, jclass clazz, jint entries } static jint netty_create_file(JNIEnv *env, jclass class, jstring filename) { - const char *file = env->GetStringUTFChars(filename, 0); + const char *file = (*env)->GetStringUTFChars(env, filename, 0); int fd = open(file, O_RDWR | O_TRUNC | O_CREAT, 0644); - env->ReleaseStringUTFChars(filename, file); + (*env)->ReleaseStringUTFChars(env, filename, file); return fd; } From fab4597958cd2759fe54d0286ad8d899c2428dee Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 1 Feb 2022 13:45:32 +0100 Subject: [PATCH 3/3] Update transport-native-io_uring/src/test/java/io/netty/incubator/channel/uring/NativeTest.java --- .../test/java/io/netty/incubator/channel/uring/NativeTest.java | 3 --- 1 file changed, 3 deletions(-) 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 c1759782..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 @@ -48,9 +48,6 @@ public static void loadJNI() { assumeTrue(IOUring.isAvailable()); } - - - @Test public void canWriteFile(@TempDir Path tmpDir) throws Exception { ByteBufAllocator allocator = new UnpooledByteBufAllocator(true);