From 7146958a3cc0212b2c2c35917c10adde0aa57d30 Mon Sep 17 00:00:00 2001 From: Viktor Dukhovni Date: Tue, 27 Apr 2021 13:33:26 -0400 Subject: Tighten scope of non-POSIX visibility macros The __BSD_VISIBLE and _DARWIN_C_SOURCE macros expose non-POSIX prototypes in system header files. We should scope these to just the ".c" modules that actually need them, and avoid defining them in header files used in other C modules. --- includes/Rts.h | 6 ------ rts/LinkerInternals.h | 5 ----- rts/posix/Itimer.c | 21 +++++++++++++++++++++ rts/posix/OSThreads.c | 17 +++++++++++++---- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/includes/Rts.h b/includes/Rts.h index e7fb8561f6..0f96ba2eca 100644 --- a/includes/Rts.h +++ b/includes/Rts.h @@ -29,12 +29,6 @@ extern "C" { #include #endif -#if defined(ios_HOST_OS) || defined(darwin_HOST_OS) -/* Inclusion of system headers usually requires _DARWIN_C_SOURCE on Mac OS X - * because of some specific defines like MMAP_ANON, MMAP_ANONYMOUS. */ -#define _DARWIN_C_SOURCE 1 -#endif - #if !defined(IN_STG_CODE) #define IN_STG_CODE 0 #endif diff --git a/rts/LinkerInternals.h b/rts/LinkerInternals.h index e1da899c89..167a14202d 100644 --- a/rts/LinkerInternals.h +++ b/rts/LinkerInternals.h @@ -13,11 +13,6 @@ #include "linker/M32Alloc.h" #if RTS_LINKER_USE_MMAP -#if defined(ios_HOST_OS) || defined(darwin_HOST_OS) -/* Inclusion of system headers usually requires _DARWIN_C_SOURCE on Mac OS X - * because of some specific defines like MMAP_ANON, MMAP_ANONYMOUS. */ -#define _DARWIN_C_SOURCE 1 -#endif #include #endif diff --git a/rts/posix/Itimer.c b/rts/posix/Itimer.c index 80925df703..98b23f623e 100644 --- a/rts/posix/Itimer.c +++ b/rts/posix/Itimer.c @@ -18,6 +18,27 @@ */ #include "PosixSource.h" + +/* We've defined _POSIX_SOURCE via "PosixSource.h", and yet still use + some non-POSIX features. With _POSIX_SOURCE defined, visibility of + non-POSIX extension prototypes requires _DARWIN_C_SOURCE on Mac OS X and + __BSD_VISIBLE on FreeBSD and DragonflyBSD. Otherwise, for example, code + using pthread_setname_np(3) and variants will not compile. We must + therefore define the additional macros that expose non-POSIX APIs early, + before any of the relevant system headers are included via "Rts.h". + + An alternative approach could be to write portable wrappers or stubs for all + the non-posix functions in a C-module that does not include "PosixSource.h", + and then use only POSIX features and the portable wrapper functions in all + other C-modules. */ +#include "ghcconfig.h" +#if defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS) +#define __BSD_VISIBLE 1 +#endif +#if defined(darwin_HOST_OS) +#define _DARWIN_C_SOURCE 1 +#endif + #include "Rts.h" /* diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c index b815894a9f..e039b43573 100644 --- a/rts/posix/OSThreads.c +++ b/rts/posix/OSThreads.c @@ -9,14 +9,23 @@ #include "PosixSource.h" +/* We've defined _POSIX_SOURCE via "PosixSource.h", and yet still use + some non-POSIX features. With _POSIX_SOURCE defined, visibility of + non-POSIX extension prototypes requires _DARWIN_C_SOURCE on Mac OS X and + __BSD_VISIBLE on FreeBSD and DragonflyBSD. Otherwise, for example, code + using pthread_setname_np(3) and variants will not compile. We must + therefore define the additional macros that expose non-POSIX APIs early, + before any of the relevant system headers are included via "Rts.h". + + An alternative approach could be to write portable wrappers or stubs for all + the non-posix functions in a C-module that does not include "PosixSource.h", + and then use only POSIX features and the portable wrapper functions in all + other C-modules. */ +#include "ghcconfig.h" #if defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS) -/* Inclusion of system headers usually requires __BSD_VISIBLE on FreeBSD and - * DragonflyBSD, because of some specific types, like u_char, u_int, etc. */ #define __BSD_VISIBLE 1 #endif #if defined(darwin_HOST_OS) -/* Inclusion of system headers usually requires _DARWIN_C_SOURCE on Mac OS X - * because of some specific types like u_char, u_int, etc. */ #define _DARWIN_C_SOURCE 1 #endif -- cgit v1.2.1