Skip to content

Commit

Permalink
Ensure native methods for unix-native-common are only registered once. (
Browse files Browse the repository at this point in the history
netty#10932)


Motiviation:

We need to ensure we only register the methods for unix-native-common once as otherwise it may have strange side-effects.

Modifications:

- Add extra method that should be called to signal that we need to register the methods. The registration will only happen once.
- Adjust code to make use of it.

Result:

No more problems due incorrect registration of these methods.
  • Loading branch information
normanmaurer authored Jan 14, 2021
1 parent 775b36e commit 5b41f3d
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 146 deletions.
78 changes: 26 additions & 52 deletions transport-native-epoll/src/main/c/netty_epoll_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "netty_unix_limits.h"
#include "netty_unix_socket.h"
#include "netty_unix_util.h"
#include "netty_unix.h"

// Add define if NETTY_BUILD_STATIC is defined so it is picked up in netty_jni_util.c
#ifdef NETTY_BUILD_STATIC
Expand Down Expand Up @@ -107,6 +108,9 @@ static jfieldID packetPortFieldId = NULL;
static jfieldID packetMemoryAddressFieldId = NULL;
static jfieldID packetCountFieldId = NULL;

static const char* staticPackagePrefix = NULL;
static int register_unix_called = 0;

// util methods
static int getSysctlValue(const char * property, int* returnValue) {
int rc = -1;
Expand Down Expand Up @@ -523,6 +527,12 @@ static jint netty_epoll_native_tcpMd5SigMaxKeyLen(JNIEnv* env, jclass clazz) {

return TCP_MD5SIG_MAXKEYLEN;
}

static jint netty_epoll_native_registerUnix(JNIEnv* env, jclass clazz) {
register_unix_called = 1;
return netty_unix_register(env, staticPackagePrefix);
}

// JNI Registered Methods End

// JNI Method Registration Table Begin
Expand Down Expand Up @@ -556,7 +566,9 @@ static const JNINativeMethod fixed_method_table[] = {
// "sendmmsg0" has a dynamic signature
{ "sizeofEpollEvent", "()I", (void *) netty_epoll_native_sizeofEpollEvent },
{ "offsetofEpollData", "()I", (void *) netty_epoll_native_offsetofEpollData },
{ "splice0", "(IJIJJ)I", (void *) netty_epoll_native_splice0 }
{ "splice0", "(IJIJJ)I", (void *) netty_epoll_native_splice0 },
{ "registerUnix", "()I", (void *) netty_epoll_native_registerUnix },

};
static const jint fixed_method_table_size = sizeof(fixed_method_table) / sizeof(fixed_method_table[0]);

Expand Down Expand Up @@ -601,11 +613,6 @@ static jint netty_epoll_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix
int ret = JNI_ERR;
int staticallyRegistered = 0;
int nativeRegistered = 0;
int limitsOnLoadCalled = 0;
int errorsOnLoadCalled = 0;
int filedescriptorOnLoadCalled = 0;
int socketOnLoadCalled = 0;
int bufferOnLoadCalled = 0;
int linuxsocketOnLoadCalled = 0;
char* nettyClassName = NULL;
jclass nativeDatagramPacketCls = NULL;
Expand Down Expand Up @@ -636,32 +643,6 @@ static jint netty_epoll_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix
}
nativeRegistered = 1;

// Load all c modules that we depend upon
if (netty_unix_limits_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto done;
}
limitsOnLoadCalled = 1;

if (netty_unix_errors_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto done;
}
errorsOnLoadCalled = 1;

if (netty_unix_filedescriptor_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto done;
}
filedescriptorOnLoadCalled = 1;

if (netty_unix_socket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto done;
}
socketOnLoadCalled = 1;

if (netty_unix_buffer_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto done;
}
bufferOnLoadCalled = 1;

if (netty_epoll_linuxsocket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto done;
}
Expand All @@ -680,6 +661,10 @@ static jint netty_epoll_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix
NETTY_JNI_UTIL_GET_FIELD(env, nativeDatagramPacketCls, packetCountFieldId, "count", "I", done);

ret = NETTY_JNI_UTIL_JNI_VERSION;

if (packagePrefix != NULL) {
staticPackagePrefix = strdup(packagePrefix);
}
done:

netty_jni_util_free_dynamic_methods_table(dynamicMethods, fixed_method_table_size, dynamicMethodsTableSize());
Expand All @@ -692,21 +677,6 @@ static jint netty_epoll_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix
if (nativeRegistered == 1) {
netty_jni_util_unregister_natives(env, packagePrefix, NATIVE_CLASSNAME);
}
if (limitsOnLoadCalled == 1) {
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
}
if (errorsOnLoadCalled == 1) {
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
}
if (filedescriptorOnLoadCalled == 1) {
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
}
if (socketOnLoadCalled == 1) {
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
}
if (bufferOnLoadCalled == 1) {
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
}
if (linuxsocketOnLoadCalled == 1) {
netty_epoll_linuxsocket_JNI_OnUnLoad(env, packagePrefix);
}
Expand All @@ -721,13 +691,17 @@ static jint netty_epoll_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix
}

static void netty_epoll_native_JNI_OnUnload(JNIEnv* env, const char* packagePrefix) {
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
netty_epoll_linuxsocket_JNI_OnUnLoad(env, packagePrefix);

if (register_unix_called == 1) {
register_unix_called = 0;
netty_unix_unregister(env, staticPackagePrefix);
}
if (staticPackagePrefix != NULL) {
free((void *) staticPackagePrefix);
staticPackagePrefix = NULL;
}

packetAddrFieldId = NULL;
packetAddrLenFieldId = NULL;
packetScopeIdFieldId = NULL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.netty.channel.unix.FileDescriptor;
import io.netty.channel.unix.Socket;
import io.netty.channel.unix.Unix;
import io.netty.util.internal.NativeLibraryLoader;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SystemPropertyUtil;
Expand Down Expand Up @@ -76,9 +77,16 @@ public final class Native {
// Just ignore
}
}
Socket.initialize();
Unix.registerInternal(new Runnable() {
@Override
public void run() {
registerUnix();
}
});
}

private static native int registerUnix();

// EventLoop operations and constants
public static final int EPOLLIN = epollin();
public static final int EPOLLOUT = epollout();
Expand Down
73 changes: 22 additions & 51 deletions transport-native-kqueue/src/main/c/netty_kqueue_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "netty_unix_limits.h"
#include "netty_unix_socket.h"
#include "netty_unix_util.h"
#include "netty_unix.h"

// Add define if NETTY_BUILD_STATIC is defined so it is picked up in netty_jni_util.c
#ifdef NETTY_BUILD_STATIC
Expand Down Expand Up @@ -75,6 +76,8 @@
#endif /* __APPLE__ */

static clockid_t waitClockId = 0; // initialized by netty_unix_util_initialize_wait_clock
static const char* staticPackagePrefix = NULL;
static int register_unix_called = 0;

static jint netty_kqueue_native_kqueueCreate(JNIEnv* env, jclass clazz) {
jint kq = kqueue();
Expand Down Expand Up @@ -159,6 +162,11 @@ static jint netty_kqueue_native_keventWait(JNIEnv* env, jclass clazz, jint kqueu
}
}

static jint netty_kqueue_native_registerUnix(JNIEnv* env, jclass clazz) {
register_unix_called = 1;
return netty_unix_register(env, staticPackagePrefix);
}

static jint netty_kqueue_native_sizeofKEvent(JNIEnv* env, jclass clazz) {
return sizeof(struct kevent);
}
Expand Down Expand Up @@ -267,19 +275,15 @@ static const JNINativeMethod fixed_method_table[] = {
{ "offsetofKEventFlags", "()I", (void *) netty_kqueue_native_offsetofKEventFlags },
{ "offsetofKEventFFlags", "()I", (void *) netty_kqueue_native_offsetofKEventFFlags },
{ "offsetofKEventFilter", "()I", (void *) netty_kqueue_native_offsetofKEventFilter },
{ "offsetofKeventData", "()I", (void *) netty_kqueue_native_offsetofKeventData }
{ "offsetofKeventData", "()I", (void *) netty_kqueue_native_offsetofKeventData },
{ "registerUnix", "()I", (void *) netty_kqueue_native_registerUnix }
};
static const jint fixed_method_table_size = sizeof(fixed_method_table) / sizeof(fixed_method_table[0]);
// JNI Method Registration Table End

static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix) {
int staticallyRegistered = 0;
int nativeRegistered = 0;
int limitsOnLoadCalled = 0;
int errorsOnLoadCalled = 0;
int filedescriptorOnLoadCalled = 0;
int socketOnLoadCalled = 0;
int bufferOnLoadCalled = 0;
int bsdsocketOnLoadCalled = 0;
int eventarrayOnLoadCalled = 0;

Expand All @@ -298,31 +302,6 @@ static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefi
goto error;
}
nativeRegistered = 1;
// Load all c modules that we depend upon
if (netty_unix_limits_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto error;
}
limitsOnLoadCalled = 1;

if (netty_unix_errors_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto error;
}
errorsOnLoadCalled = 1;

if (netty_unix_filedescriptor_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto error;
}
filedescriptorOnLoadCalled = 1;

if (netty_unix_socket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto error;
}
socketOnLoadCalled = 1;

if (netty_unix_buffer_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto error;
}
bufferOnLoadCalled = 1;

if (netty_kqueue_bsdsocket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) {
goto error;
Expand All @@ -342,6 +321,9 @@ static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefi
goto error;
}

if (packagePrefix != NULL) {
staticPackagePrefix = strdup(packagePrefix);
}
return NETTY_JNI_UTIL_JNI_VERSION;
error:
if (staticallyRegistered == 1) {
Expand All @@ -350,21 +332,6 @@ static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefi
if (nativeRegistered == 1) {
netty_jni_util_unregister_natives(env, packagePrefix, NATIVE_CLASSNAME);
}
if (limitsOnLoadCalled == 1) {
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
}
if (errorsOnLoadCalled == 1) {
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
}
if (filedescriptorOnLoadCalled == 1) {
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
}
if (socketOnLoadCalled == 1) {
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
}
if (bufferOnLoadCalled == 1) {
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
}
if (bsdsocketOnLoadCalled == 1) {
netty_kqueue_bsdsocket_JNI_OnUnLoad(env, packagePrefix);
}
Expand All @@ -375,14 +342,18 @@ static jint netty_kqueue_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefi
}

static void netty_kqueue_native_JNI_OnUnload(JNIEnv* env, const char* packagePrefix) {
netty_unix_limits_JNI_OnUnLoad(env, packagePrefix);
netty_unix_errors_JNI_OnUnLoad(env, packagePrefix);
netty_unix_filedescriptor_JNI_OnUnLoad(env, packagePrefix);
netty_unix_socket_JNI_OnUnLoad(env, packagePrefix);
netty_unix_buffer_JNI_OnUnLoad(env, packagePrefix);
netty_kqueue_bsdsocket_JNI_OnUnLoad(env, packagePrefix);
netty_kqueue_eventarray_JNI_OnUnLoad(env, packagePrefix);

if (register_unix_called == 1) {
register_unix_called = 0;
netty_unix_unregister(env, staticPackagePrefix);
}
if (staticPackagePrefix != NULL) {
free((void *) staticPackagePrefix);
staticPackagePrefix = NULL;
}

netty_jni_util_unregister_natives(env, packagePrefix, STATICALLY_CLASSNAME);
netty_jni_util_unregister_natives(env, packagePrefix, NATIVE_CLASSNAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package io.netty.channel.kqueue;

import io.netty.channel.unix.FileDescriptor;
import io.netty.channel.unix.Socket;
import io.netty.channel.unix.Unix;
import io.netty.util.internal.NativeLibraryLoader;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SystemPropertyUtil;
Expand Down Expand Up @@ -59,9 +59,16 @@ final class Native {
// The library was not previously loaded, load it now.
loadNativeLibrary();
}
Socket.initialize();
Unix.registerInternal(new Runnable() {
@Override
public void run() {
registerUnix();
}
});
}

private static native int registerUnix();

static final short EV_ADD = evAdd();
static final short EV_ENABLE = evEnable();
static final short EV_DISABLE = evDisable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static DomainSocketAddress newSocketAddress() {
if (!file.delete()) {
throw new IOException("failed to delete: " + file);
}
} while (file.getAbsolutePath().length() > Socket.UDS_SUN_PATH_SIZE);
} while (file.getAbsolutePath().length() > 128);
return new DomainSocketAddress(file);
} catch (IOException e) {
throw new IllegalStateException(e);
Expand Down
Loading

0 comments on commit 5b41f3d

Please sign in to comment.