git: 5842d08c97cd - 2024Q2 - security/gnutls: initialise libpthread

From: Tijl Coosemans <tijl_at_FreeBSD.org>
Date: Fri, 12 Apr 2024 20:21:42 UTC
The branch 2024Q2 has been updated by tijl:

URL: https://cgit.FreeBSD.org/ports/commit/?id=5842d08c97cdecf1e3f8e2c536adb0aac3191574

commit 5842d08c97cdecf1e3f8e2c536adb0aac3191574
Author:     Tijl Coosemans <tijl@FreeBSD.org>
AuthorDate: 2024-04-12 19:03:35 +0000
Commit:     Tijl Coosemans <tijl@FreeBSD.org>
CommitDate: 2024-04-12 20:20:49 +0000

    security/gnutls: initialise libpthread
    
    To ensure thread-safety libgnutls calls libpthread functions but to
    avoid the overhead for single-threaded programs it does not link with
    libpthread.  It only calls libpthread if the executable or another
    library links to it.
    
    Since 3.8.0 libgnutls calls pthread_key_create from its init function
    but because it does not link with libpthread libpthread might not have
    been initialised yet.  Patch the libgnutls init function so it
    initialises libpthread.
    
    PR:             278076
    (cherry picked from commit 9ffc65e659a32a4eb293afd5c4d03be553a61570)
---
 security/gnutls/Makefile                 |  1 +
 security/gnutls/files/patch-lib_global.c | 43 ++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/security/gnutls/Makefile b/security/gnutls/Makefile
index f72a03894fb0..d7d190bce6f5 100644
--- a/security/gnutls/Makefile
+++ b/security/gnutls/Makefile
@@ -1,5 +1,6 @@
 PORTNAME=	gnutls
 DISTVERSION=	3.8.5
+PORTREVISION=	1
 CATEGORIES=	security net
 MASTER_SITES=	GNUPG/${PORTNAME}/v${DISTVERSION:R}
 
diff --git a/security/gnutls/files/patch-lib_global.c b/security/gnutls/files/patch-lib_global.c
new file mode 100644
index 000000000000..967aa49a5877
--- /dev/null
+++ b/security/gnutls/files/patch-lib_global.c
@@ -0,0 +1,43 @@
+--- lib/global.c.orig	2024-04-11 15:36:24 UTC
++++ lib/global.c
+@@ -29,6 +29,13 @@
+ #include "random.h"
+ #include <gnutls/pkcs11.h>
+ 
++#if __FreeBSD__
++#include <sys/param.h>
++#if __FreeBSD_version >= 1400094
++#include <dlfcn.h>
++#endif
++#endif
++
+ #include "hello_ext.h" /* for _gnutls_hello_ext_init */
+ #include "supplemental.h" /* for _gnutls_supplemental_deinit */
+ #include "locks.h"
+@@ -520,6 +527,26 @@ static void _CONSTRUCTOR lib_init(void)
+ 		if (ret == 1)
+ 			return;
+ 	}
++
++#if __FreeBSD__
++#if __FreeBSD_version >= 1400094
++	/* This dlopen call initialises libpthread if it is present.  Normally
++	   this is handled by linking to libpthread but libgnutls does not link
++	   with libpthread to avoid the overhead for non-threaded programs.  */
++	(void) dlopen("libpthread.so", RTLD_LAZY | RTLD_GLOBAL | RTLD_NOLOAD);
++#else
++	/* The dlopen call above does not work correctly on older versions of
++	   FreeBSD.  Call pthread_mutex_timedlock instead.  It initialises
++	   libpthread and there's no libc stub that can preempt it.  */
++#pragma weak pthread_mutex_timedlock
++	if (pthread_mutex_timedlock != NULL) {
++		pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
++		pthread_mutex_timedlock(&lock, NULL);
++		pthread_mutex_unlock(&lock);
++		pthread_mutex_destroy(&lock);
++	}
++#endif
++#endif
+ 
+ 	ret = _gnutls_global_init(1);
+ 	if (ret < 0) {