Use _Noreturn (C11; GNU89) properly

A function that returns with no value is a different thing from a
function that doesn't return at all.  Those are two orthogonal
concepts, commonly confused.

pthread_create(3) expects a pointer to a start routine that has a
very precise prototype:

    void *(*start_routine)(void *);

However, other thread functions, such as kernel ones, expect:

    void (*start_routine)(void *);

Providing a different one is incorrect, and has only been working
because the ABIs happen to produce a compatible function.

We should use '_Noreturn void', since it's the natural type, and
then provide a '_Noreturn void *' wrapper for pthread functions.

For consistency, replace most cases of __NORETURN or
__attribute__((noreturn)) by _Noreturn.  _Noreturn is understood
by -std=gnu89, so it should be safe to use everywhere.

Ref: https://github.com/openzfs/zfs/pull/13110#discussion_r808450136
Ref: https://software.codidact.com/posts/285972
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Co-authored-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
Closes #13120
This commit is contained in:
Alejandro Colomar
2022-03-05 01:25:22 +01:00
committed by GitHub
parent 06b8050678
commit db7f1a91de
30 changed files with 76 additions and 90 deletions
-1
View File
@@ -28,7 +28,6 @@
#define _SYS_FEATURE_TESTS_H
#define ____cacheline_aligned
#define __NORETURN __attribute__((__noreturn__))
#if !defined(zfs_fallthrough) && !defined(_LIBCPP_VERSION)
#if defined(HAVE_IMPLICIT_FALLTHROUGH)
+3 -3
View File
@@ -40,8 +40,8 @@
static const char *pname;
static void
uu_die_internal(int status, const char *format, va_list alist) __NORETURN;
static _Noreturn void
uu_die_internal(int status, const char *format, va_list alist);
int uu_exit_ok_value = EXIT_SUCCESS;
int uu_exit_fatal_value = EXIT_FAILURE;
@@ -110,7 +110,7 @@ uu_warn(const char *format, ...)
va_end(alist);
}
static __attribute__((format(printf, 2, 0))) __NORETURN void
static __attribute__((format(printf, 2, 0))) _Noreturn void
uu_die_internal(int status, const char *format, va_list alist)
{
uu_warn_internal(errno, format, alist);
-5
View File
@@ -24,11 +24,6 @@ AM_CFLAGS += $(ZLIB_CFLAGS)
AM_CFLAGS += -DLIB_ZPOOL_BUILD
# For the void (*)(void *) -> void *(*)(void *) cast in zk_thread_create()
# See https://github.com/openzfs/zfs/pull/13110#discussion_r808450136
kernel.$(OBJEXT): CFLAGS += $(NO_CAST_FUNCTION_TYPE)
kernel.l$(OBJEXT): CFLAGS += $(NO_CAST_FUNCTION_TYPE)
lib_LTLIBRARIES = libzpool.la
USER_C = \
+20 -1
View File
@@ -75,12 +75,28 @@ struct proc p0;
#define TS_STACK_MIN MAX(PTHREAD_STACK_MIN, 32768)
#define TS_STACK_MAX (256 * 1024)
struct zk_thread_wrapper {
void (*func)(void *);
void *arg;
};
static void *
zk_thread_wrapper(void *arg)
{
struct zk_thread_wrapper ztw;
memcpy(&ztw, arg, sizeof (ztw));
free(arg);
ztw.func(ztw.arg);
return (NULL);
}
kthread_t *
zk_thread_create(void (*func)(void *), void *arg, size_t stksize, int state)
{
pthread_attr_t attr;
pthread_t tid;
char *stkstr;
struct zk_thread_wrapper *ztw;
int detachstate = PTHREAD_CREATE_DETACHED;
VERIFY0(pthread_attr_init(&attr));
@@ -117,7 +133,10 @@ zk_thread_create(void (*func)(void *), void *arg, size_t stksize, int state)
VERIFY0(pthread_attr_setstacksize(&attr, stksize));
VERIFY0(pthread_attr_setguardsize(&attr, PAGESIZE));
VERIFY0(pthread_create(&tid, &attr, (void *(*)(void *))func, arg));
VERIFY(ztw = malloc(sizeof (*ztw)));
ztw->func = func;
ztw->arg = arg;
VERIFY0(pthread_create(&tid, &attr, zk_thread_wrapper, ztw));
VERIFY0(pthread_attr_destroy(&attr));
return ((void *)(uintptr_t)tid);
+1 -1
View File
@@ -211,7 +211,7 @@ taskq_wait_outstanding(taskq_t *tq, taskqid_t id)
taskq_wait(tq);
}
static void
static _Noreturn void
taskq_thread(void *arg)
{
taskq_t *tq = arg;