From dab9a068a982f85169d506a35c9746a8208e5b92 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 30 Oct 2020 11:07:38 +0100 Subject: [PATCH] Fix compile error introduced by a63faa4fa1ae08efc391e9bcf461ab699a5d7efd (#10750) Motivation: a63faa4fa1ae08efc391e9bcf461ab699a5d7efd missed to update the macos specific c files for the resolver. Modifications: Fix up c files so it compiles again on macos Result: Compiles on macos again --- .../src/main/c/netty_resolver_dns_macos.c | 144 +++++------------- 1 file changed, 40 insertions(+), 104 deletions(-) diff --git a/resolver-dns-native-macos/src/main/c/netty_resolver_dns_macos.c b/resolver-dns-native-macos/src/main/c/netty_resolver_dns_macos.c index 06dd65ee9b..699ad159e0 100644 --- a/resolver-dns-native-macos/src/main/c/netty_resolver_dns_macos.c +++ b/resolver-dns-native-macos/src/main/c/netty_resolver_dns_macos.c @@ -30,6 +30,8 @@ #define NETTY_JNI_UTIL_BUILD_STATIC #endif +#define STREAM_CLASSNAME "io/netty/resolver/dns/macos/MacOSDnsServerAddressStreamProvider" + static jclass dnsResolverClass = NULL; static jclass byteArrayClass = NULL; static jclass stringClass = NULL; @@ -122,126 +124,60 @@ error: static JNINativeMethod* createDynamicMethodsTable(const char* packagePrefix) { JNINativeMethod* dynamicMethods = malloc(sizeof(JNINativeMethod) * 1); - char* dynamicTypeName = netty_unix_util_prepend(packagePrefix, "io/netty/resolver/dns/macos/DnsResolver;"); + char* dynamicTypeName = netty_jni_util_prepend(packagePrefix, "io/netty/resolver/dns/macos/DnsResolver;"); JNINativeMethod* dynamicMethod = &dynamicMethods[0]; dynamicMethod->name = "resolvers"; - dynamicMethod->signature = netty_unix_util_prepend("()[L", dynamicTypeName); + dynamicMethod->signature = netty_jni_util_prepend("()[L", dynamicTypeName); dynamicMethod->fnPtr = (void *) netty_resolver_dns_macos_resolvers; free(dynamicTypeName); return dynamicMethods; } -static void freeDynamicMethodsTable(JNINativeMethod* dynamicMethods) { - free(dynamicMethods[0].signature); - free(dynamicMethods); -} - // JNI Method Registration Table End - -static void JNI_OnUnload_netty_resolver_dns_native_macos0(JavaVM* vm, void* reserved) { - JNIEnv* env; - if ((*vm)->GetEnv(vm, (void**) &env, NETTY_JNI_VERSION) != JNI_OK) { - // Something is wrong but nothing we can do about this :( - return; - } - - if (byteArrayClass != NULL) { - (*env)->DeleteGlobalRef(env, byteArrayClass); - byteArrayClass = NULL; - } - - if (stringClass != NULL) { - (*env)->DeleteGlobalRef(env, stringClass); - stringClass = NULL; - } +static void netty_resolver_dns_native_macos_JNI_OnUnLoad(JNIEnv* env, const char* packagePrefix) { + NETTY_JNI_UTIL_UNLOAD_CLASS(env, byteArrayClass); + NETTY_JNI_UTIL_UNLOAD_CLASS(env, stringClass); + netty_jni_util_unregister_natives(env, packagePrefix, STREAM_CLASSNAME); } -static void netty_resolver_dns_native_macos0_OnUnLoad(JNIEnv* env) { - -} - -static jint JNI_OnLoad_netty_resolver_dns_native_macos0(JavaVM* vm, void* reserved) { - JNIEnv* env; - if ((*vm)->GetEnv(vm, (void**) &env, NETTY_JNI_VERSION) != JNI_OK) { - return JNI_ERR; - } - -#ifndef NETTY_BUILD_STATIC - Dl_info dlinfo; - jint status = 0; - // We need to use an address of a function that is uniquely part of this library, so choose a static - // function. See https://github.com/netty/netty/issues/4840. - if (!dladdr((void*) netty_resolver_dns_native_macos0_OnUnLoad, &dlinfo)) { - fprintf(stderr, "FATAL: resolver-dns-native-macos JNI call to dladdr failed!\n"); - return JNI_ERR; - } - char* packagePrefix = netty_unix_util_parse_package_prefix(dlinfo.dli_fname, "netty_resolver_dns_native_macos", &status); - if (status == JNI_ERR) { - fprintf(stderr, "FATAL: resolver-dns-native-macos JNI encountered unexpected dlinfo.dli_fname: %s\n", dlinfo.dli_fname); - return JNI_ERR; - } -#endif /* NETTY_BUILD_STATIC */ +static jint netty_resolver_dns_native_macos_JNI_OnLoad(JNIEnv* env, const char* packagePrefix) { + int ret = JNI_ERR; + int providerRegistered = 0; + char* nettyClassName = NULL; // Register the methods which are not referenced by static member variables JNINativeMethod* dynamicMethods = createDynamicMethodsTable(packagePrefix); - if (netty_unix_util_register_natives(env, + if (dynamicMethods == NULL) { + goto done; + } + if (netty_jni_util_register_natives(env, packagePrefix, - "io/netty/resolver/dns/macos/MacOSDnsServerAddressStreamProvider", + STREAM_CLASSNAME, dynamicMethods, 1) != 0) { - freeDynamicMethodsTable(dynamicMethods); - fprintf(stderr, "FATAL: Couldnt register natives"); - - return JNI_ERR; + goto done; } - freeDynamicMethodsTable(dynamicMethods); - dynamicMethods = NULL; + providerRegistered = 1; + nettyClassName = netty_jni_util_prepend(packagePrefix, "io/netty/resolver/dns/macos/DnsResolver"); + NETTY_JNI_UTIL_LOAD_CLASS(env, dnsResolverClass, nettyClassName, done); + netty_jni_util_free_dynamic_name(&nettyClassName); - char* nettyClassName = netty_unix_util_prepend(packagePrefix, "io/netty/resolver/dns/macos/DnsResolver"); - jclass localDnsResolverClass = (*env)->FindClass(env, nettyClassName); + NETTY_JNI_UTIL_GET_METHOD(env, dnsResolverClass, dnsResolverMethodId, "", "(Ljava/lang/String;[[BI[Ljava/lang/String;Ljava/lang/String;II)V", done); + + NETTY_JNI_UTIL_LOAD_CLASS(env, byteArrayClass, "[B", done); + NETTY_JNI_UTIL_LOAD_CLASS(env, stringClass, "java/lang/String", done); + + ret = NETTY_JNI_UTIL_JNI_VERSION; +done: + if (ret == JNI_ERR) { + if (providerRegistered == 1) { + netty_jni_util_unregister_natives(env, packagePrefix, STREAM_CLASSNAME); + } + } + netty_jni_util_free_dynamic_methods_table(dynamicMethods, 0, 1); free(nettyClassName); - nettyClassName = NULL; - if (localDnsResolverClass == NULL) { - // pending exception... - return JNI_ERR; - } - dnsResolverClass = (jclass) (*env)->NewGlobalRef(env, localDnsResolverClass); - if (dnsResolverClass == NULL) { - return JNI_ERR; - } - dnsResolverMethodId = (*env)->GetMethodID(env, dnsResolverClass, "", "(Ljava/lang/String;[[BI[Ljava/lang/String;Ljava/lang/String;II)V"); - if (dnsResolverMethodId == NULL) { - netty_unix_errors_throwRuntimeException(env, "failed to get method ID: DnsResolver.(String, byte[][], String[], String, int, int)"); - return JNI_ERR; - } - - if (packagePrefix != NULL) { - free(packagePrefix); - packagePrefix = NULL; - } - - jclass byteArrayCls = (*env)->FindClass(env, "[B"); - if (byteArrayCls == NULL) { - // pending exception... - return JNI_ERR; - } - byteArrayClass = (jclass) (*env)->NewGlobalRef(env, byteArrayCls); - if (byteArrayClass == NULL) { - return JNI_ERR; - } - - jclass stringCls = (*env)->FindClass(env, "java/lang/String"); - if (stringCls == NULL) { - // pending exception... - return JNI_ERR; - } - stringClass = (jclass) (*env)->NewGlobalRef(env, stringCls); - if (stringClass == NULL) { - return JNI_ERR; - } - - return NETTY_JNI_VERSION; + return ret; } // We build with -fvisibility=hidden so ensure we mark everything that needs to be visible with JNIEXPORT @@ -249,20 +185,20 @@ static jint JNI_OnLoad_netty_resolver_dns_native_macos0(JavaVM* vm, void* reserv // Invoked by the JVM when statically linked JNIEXPORT jint JNI_OnLoad_netty_resolver_dns_native_macos(JavaVM* vm, void* reserved) { - return JNI_OnLoad_netty_resolver_dns_native_macos0(vm, reserved); + return netty_jni_util_JNI_OnLoad(vm, reserved, "netty_resolver_dns_native_macos", netty_resolver_dns_native_macos_JNI_OnLoad); } // Invoked by the JVM when statically linked JNIEXPORT void JNI_OnUnload_netty_resolver_dns_native_macos(JavaVM* vm, void* reserved) { - JNI_OnUnload_netty_resolver_dns_native_macos0(vm, reserved); + netty_jni_util_JNI_OnUnload(vm, reserved, netty_resolver_dns_native_macos_JNI_OnUnLoad); } #ifndef NETTY_BUILD_STATIC JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { - return JNI_OnLoad_netty_resolver_dns_native_macos0(vm, reserved); + return netty_jni_util_JNI_OnLoad(vm, reserved, "netty_resolver_dns_native_macos", netty_resolver_dns_native_macos_JNI_OnLoad); } JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved) { - return JNI_OnUnload_netty_resolver_dns_native_macos0(vm, reserved); + netty_jni_util_JNI_OnUnload(vm, reserved, netty_resolver_dns_native_macos_JNI_OnUnLoad); } #endif /* NETTY_BUILD_STATIC */