From aa7c02ff80dff3c9f1699e921fcf2d061250ce67 Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 16:42:29 -0800 Subject: [PATCH 01/15] os: Add an implementation of wl_os_socket_peercred() for darwin This adds an alternative implementation of wl_os_socket_peercred() using getpeereid(3) and LOCAL_PEERPID, similar to changes I recently made to xorg-server. Signed-off-by: Jeremy Huddleston Sequoia --- meson.build | 1 + src/wayland-os.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/meson.build b/meson.build index f469756f..3d94094b 100644 --- a/meson.build +++ b/meson.build @@ -40,6 +40,7 @@ endforeach have_funcs = [ 'accept4', + 'getpeereid', 'mkostemp', 'posix_fallocate', 'prctl', diff --git a/src/wayland-os.c b/src/wayland-os.c index a9066cae..6651c8ce 100644 --- a/src/wayland-os.c +++ b/src/wayland-os.c @@ -115,6 +115,22 @@ wl_os_socket_peercred(int sockfd, uid_t *uid, gid_t *gid, pid_t *pid) *pid = ucred.pid; return 0; } +#elif defined(HAVE_GETPEEREID) && defined(LOCAL_PEERPID) +int +wl_os_socket_peercred(int sockfd, uid_t *uid, gid_t *gid, pid_t *pid) +{ + socklen_t len; + + if (getpeereid(sockfd, uid, gid) != 0) { + return -1; + } + + len = sizeof(pid_t); + if (getsockopt(sockfd, SOL_LOCAL, LOCAL_PEERPID, pid, &len) != 0) { + return -1; + } + return 0; +} #else #error "Don't know how to read ucred on this platform" #endif From b5cb23d69898da70f33a61e1074f31da604b715f Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 14:52:05 -0800 Subject: [PATCH 02/15] os: Use fallback implementations when *_CLOEXEC are unavailable at build time On platforms without F_DUPFD_CLOEXEC, MSG_CMSG_CLOEXEC, or SOCK_CLOEXEC, we should skip past their usage and just use the fcntl() fallback. Signed-off-by: Jeremy Huddleston Sequoia --- src/wayland-os.c | 6 +++++- tests/os-wrappers-test.c | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/wayland-os.c b/src/wayland-os.c index 6651c8ce..86e9816d 100644 --- a/src/wayland-os.c +++ b/src/wayland-os.c @@ -69,11 +69,13 @@ wl_os_socket_cloexec(int domain, int type, int protocol) { int fd; +#ifdef SOCK_CLOEXEC fd = socket(domain, type | SOCK_CLOEXEC, protocol); if (fd >= 0) return fd; if (errno != EINVAL) return -1; +#endif fd = socket(domain, type, protocol); return set_cloexec_or_close(fd); @@ -140,11 +142,13 @@ wl_os_dupfd_cloexec(int fd, int minfd) { int newfd; +#ifdef F_DUPFD_CLOEXEC newfd = fcntl(fd, F_DUPFD_CLOEXEC, minfd); if (newfd >= 0) return newfd; if (errno != EINVAL) return -1; +#endif newfd = fcntl(fd, F_DUPFD, minfd); return set_cloexec_or_close(newfd); @@ -192,7 +196,7 @@ wl_os_recvmsg_cloexec(int sockfd, struct msghdr *msg, int flags) * fix (https://cgit.freebsd.org/src/commit/?id=6ceacebdf52211). */ #pragma message("Using fallback directly since MSG_CMSG_CLOEXEC is broken.") -#else +#elif defined(MSG_CMSG_CLOEXEC) ssize_t len; len = recvmsg(sockfd, msg, flags | MSG_CMSG_CLOEXEC); diff --git a/tests/os-wrappers-test.c b/tests/os-wrappers-test.c index 8d8c3ab9..552eddbd 100644 --- a/tests/os-wrappers-test.c +++ b/tests/os-wrappers-test.c @@ -141,10 +141,12 @@ recvmsg(int sockfd, struct msghdr *msg, int flags) { wrapped_calls_recvmsg++; +#ifdef MSG_CMSG_CLOEXEC if (fall_back && (flags & MSG_CMSG_CLOEXEC)) { errno = EINVAL; return -1; } +#endif return real_recvmsg(sockfd, msg, flags); } @@ -342,9 +344,10 @@ do_os_wrappers_recvmsg_cloexec(int n) struct marshal_data data; data.nr_fds_begin = count_open_fds(); -#if HAVE_BROKEN_MSG_CMSG_CLOEXEC +#if HAVE_BROKEN_MSG_CMSG_CLOEXEC || !defined(MSG_CMSG_CLOEXEC) /* We call the fallback directly on FreeBSD versions with a broken - * MSG_CMSG_CLOEXEC, so we don't call the local recvmsg() wrapper. */ + * MSG_CMSG_CLOEXEC or platforms without MSG_CMSG_CLOEXEC, so we + * don't call the local recvmsg() wrapper. */ data.wrapped_calls = 0; #else data.wrapped_calls = n; From e519c4318c54db205607a62c5aae29eac7847575 Mon Sep 17 00:00:00 2001 From: Weijia Wang Date: Thu, 28 Jul 2022 01:37:50 +0200 Subject: [PATCH 03/15] os: Provide wl_os_socketpair_cloexec wrapper for systems without SOCK_CLOEXEC Reviewed-by: Jeremy Huddleston Sequoia Signed-off-by: Weijia Wang --- src/wayland-os.c | 20 ++++++++++++++++++++ src/wayland-os.h | 3 +++ tests/client-test.c | 3 ++- tests/connection-test.c | 9 +++++---- tests/os-wrappers-test.c | 10 ++++++++-- tests/resources-test.c | 7 ++++--- 6 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/wayland-os.c b/src/wayland-os.c index 86e9816d..ba2c814b 100644 --- a/src/wayland-os.c +++ b/src/wayland-os.c @@ -81,6 +81,26 @@ wl_os_socket_cloexec(int domain, int type, int protocol) return set_cloexec_or_close(fd); } +int +wl_os_socketpair_cloexec(int domain, int type, int protocol, int sv[2]) +{ + int retval; + +#ifdef SOCK_CLOEXEC + retval = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv); + if (retval >= 0) + return retval; + if (errno != EINVAL) + return -1; +#endif + + retval = socketpair(domain, type, protocol, sv); + if (set_cloexec_or_close(sv[0]) == -1 || set_cloexec_or_close(sv[1]) == -1) + retval = -1; + + return retval; +} + #if defined(__FreeBSD__) int wl_os_socket_peercred(int sockfd, uid_t *uid, gid_t *gid, pid_t *pid) diff --git a/src/wayland-os.h b/src/wayland-os.h index 068fd2fe..42e28007 100644 --- a/src/wayland-os.h +++ b/src/wayland-os.h @@ -32,6 +32,9 @@ int wl_os_socket_cloexec(int domain, int type, int protocol); +int +wl_os_socketpair_cloexec(int domain, int type, int protocol, int sv[2]); + int wl_os_socket_peercred(int sockfd, uid_t *uid, gid_t *gid, pid_t *pid); diff --git a/tests/client-test.c b/tests/client-test.c index 47be83fc..d3b09443 100644 --- a/tests/client-test.c +++ b/tests/client-test.c @@ -34,6 +34,7 @@ #include #include +#include "wayland-os.h" #include "wayland-private.h" #include "wayland-server.h" #include "test-runner.h" @@ -88,7 +89,7 @@ TEST(client_destroy_listener) struct client_destroy_listener a, b; int s[2]; - assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0); + assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0); display = wl_display_create(); assert(display); client = wl_client_create(display, s[0]); diff --git a/tests/connection-test.c b/tests/connection-test.c index 9762e0da..80f4f443 100644 --- a/tests/connection-test.c +++ b/tests/connection-test.c @@ -37,6 +37,7 @@ #include #include +#include "wayland-os.h" #include "wayland-private.h" #include "test-runner.h" #include "test-compositor.h" @@ -48,7 +49,7 @@ setup(int *s) { struct wl_connection *connection; - assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0); + assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0); connection = wl_connection_create(s[0]); assert(connection); @@ -181,8 +182,8 @@ struct marshal_data { static void setup_marshal_data(struct marshal_data *data) { - assert(socketpair(AF_UNIX, - SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0); + assert(wl_os_socketpair_cloexec(AF_UNIX, + SOCK_STREAM, 0, data->s) == 0); data->read_connection = wl_connection_create(data->s[0]); assert(data->read_connection); data->write_connection = wl_connection_create(data->s[1]); @@ -824,7 +825,7 @@ TEST(request_bogus_size) for (bogus_size = 11; bogus_size >= 0; bogus_size--) { fprintf(stderr, "* bogus size %d\n", bogus_size); - assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0); + assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0); display = wl_display_create(); assert(display); client = wl_client_create(display, s[0]); diff --git a/tests/os-wrappers-test.c b/tests/os-wrappers-test.c index 552eddbd..717fc19f 100644 --- a/tests/os-wrappers-test.c +++ b/tests/os-wrappers-test.c @@ -92,10 +92,12 @@ socket(int domain, int type, int protocol) { wrapped_calls_socket++; +#ifdef SOCK_CLOEXEC if (fall_back && (type & SOCK_CLOEXEC)) { errno = EINVAL; return -1; } +#endif return real_socket(domain, type, protocol); } @@ -181,7 +183,11 @@ do_os_wrappers_socket_cloexec(int n) * Must have 2 calls if falling back, but must also allow * falling back without a forced fallback. */ +#ifdef SOCK_CLOEXEC assert(wrapped_calls_socket > n); +#else + assert(wrapped_calls_socket == 1); +#endif exec_fd_leak_check(nr_fds); } @@ -255,8 +261,8 @@ struct marshal_data { static void setup_marshal_data(struct marshal_data *data) { - assert(socketpair(AF_UNIX, - SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0); + assert(wl_os_socketpair_cloexec(AF_UNIX, + SOCK_STREAM, 0, data->s) == 0); data->read_connection = wl_connection_create(data->s[0]); assert(data->read_connection); diff --git a/tests/resources-test.c b/tests/resources-test.c index fa6ba2b2..ca4595cb 100644 --- a/tests/resources-test.c +++ b/tests/resources-test.c @@ -28,6 +28,7 @@ #include #include +#include "wayland-os.h" #include "wayland-server.h" #include "test-runner.h" @@ -40,7 +41,7 @@ TEST(create_resource_tst) int s[2]; uint32_t id; - assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0); + assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0); display = wl_display_create(); assert(display); client = wl_client_create(display, s[0]); @@ -111,7 +112,7 @@ TEST(destroy_res_tst) .notify = &destroy_notify }; - assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0); + assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0); display = wl_display_create(); assert(display); client = wl_client_create(display, s[0]); @@ -159,7 +160,7 @@ TEST(create_resource_with_same_id) int s[2]; uint32_t id; - assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0); + assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0); display = wl_display_create(); assert(display); client = wl_client_create(display, s[0]); From 2cd63a46bb8a31171c3fb8e4f7ec507e4bbaa82f Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 19:44:15 -0800 Subject: [PATCH 04/15] shm: Avoid unused variable warning !HAVE_MEMFD_CREATE case ../src/wayland-shm.c:307:14: warning: unused variable 'statbuf' [-Wunused-variable] struct stat statbuf; ^ ../src/wayland-shm.c:308:6: warning: unused variable 'seals' [-Wunused-variable] int seals; ^ 2 warnings generated. Signed-off-by: Jeremy Huddleston Sequoia --- src/wayland-shm.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wayland-shm.c b/src/wayland-shm.c index c4cd390c..4b52b0f2 100644 --- a/src/wayland-shm.c +++ b/src/wayland-shm.c @@ -304,8 +304,10 @@ shm_create_pool(struct wl_client *client, struct wl_resource *resource, uint32_t id, int fd, int32_t size) { struct wl_shm_pool *pool; +#ifdef HAVE_MEMFD_CREATE struct stat statbuf; int seals; +#endif int prot; int flags; From d414619cb3a09cf808f9a63c200257d0b04dcd1e Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 16:26:55 -0800 Subject: [PATCH 05/15] darwin: Compile with -D_DARWIN_C_SOURCE Signed-off-by: Jeremy Huddleston Sequoia --- meson.build | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/meson.build b/meson.build index 3d94094b..cd6ceb21 100644 --- a/meson.build +++ b/meson.build @@ -19,6 +19,10 @@ cc_args = [] if host_machine.system() != 'freebsd' cc_args += ['-D_POSIX_C_SOURCE=200809L'] endif +if host_machine.system() == 'darwin' + # For CMSG_LEN(), which is an RFC 2292 addition and not part of POSIX.1-2008 + cc_args += ['-D_DARWIN_C_SOURCE'] +endif add_project_arguments(cc_args, language: 'c') compiler_flags = [ From 4ee79756e0b2d0d38d978b084944847ea6a5800d Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 15:18:17 -0800 Subject: [PATCH 06/15] darwin: Set rt_dep to empty as it's part of libSystem Signed-off-by: Jeremy Huddleston Sequoia --- meson.build | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/meson.build b/meson.build index cd6ceb21..c99829fc 100644 --- a/meson.build +++ b/meson.build @@ -96,11 +96,15 @@ if get_option('libraries') endif endforeach - rt_dep = [] - if not cc.has_function('clock_gettime', prefix: '#include ') - rt_dep = cc.find_library('rt') - if not cc.has_function('clock_gettime', prefix: '#include ', dependencies: rt_dep, args: cc_args) - error('clock_gettime not found') + if host_machine.system() == 'darwin' + rt_dep = [] + else + rt_dep = [] + if cc.has_function('clock_gettime', prefix: '#include ') + rt_dep = cc.find_library('rt') + if not cc.has_function('clock_gettime', prefix: '#include ', dependencies: rt_dep, args: cc_args) + error('clock_gettime not found') + endif endif endif endif From 0b01986b0bbeed7dacbac11fdcb4e1013737afa6 Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 15:02:01 -0800 Subject: [PATCH 07/15] darwin: Locate libffi with cc.find_library() rather than dependency() Signed-off-by: Jeremy Huddleston Sequoia --- meson.build | 7 ++++++- src/connection.c | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index c99829fc..db52530d 100644 --- a/meson.build +++ b/meson.build @@ -82,7 +82,12 @@ if get_option('libraries') # Otherwise, assume that epoll(7) is supported natively. epoll_dep = [] endif - ffi_dep = dependency('libffi') + + if host_machine.system() == 'darwin' + ffi_dep = cc.find_library('ffi') + else + ffi_dep = dependency('libffi') + endif decls = [ { 'header': 'sys/signalfd.h', 'symbol': 'SFD_CLOEXEC' }, diff --git a/src/connection.c b/src/connection.c index ceaeac14..af79450e 100644 --- a/src/connection.c +++ b/src/connection.c @@ -38,7 +38,11 @@ #include #include #include +#ifdef __APPLE__ +#include +#else #include +#endif #include "wayland-util.h" #include "wayland-private.h" From f4b698c0d39ff9ac3fb4f25ea0bf5b7bca935856 Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 15:20:22 -0800 Subject: [PATCH 08/15] darwin: Locate libexpat with cc.find_library() rather than dependency() Signed-off-by: Jeremy Huddleston Sequoia --- src/meson.build | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/meson.build b/src/meson.build index a8a1d2ba..42450efa 100644 --- a/src/meson.build +++ b/src/meson.build @@ -29,7 +29,12 @@ wayland_util_dep = declare_dependency( if get_option('scanner') # wayland-scanner - scanner_deps = [ dependency('expat') ] + if host_machine.system() == 'darwin' + scanner_deps = [ cc.find_library('expat') ] + else + scanner_deps = [ dependency('expat') ] + endif + scanner_args = [ '-include', 'config.h' ] if get_option('dtd_validation') From 9a04e237196488027573539606260ec5e9b0604a Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 17:17:06 -0800 Subject: [PATCH 09/15] darwin: Use epoll-shim Signed-off-by: Jeremy Huddleston Sequoia --- meson.build | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/meson.build b/meson.build index db52530d..9e0a6c92 100644 --- a/meson.build +++ b/meson.build @@ -74,9 +74,9 @@ endif config_h.set10('HAVE_BROKEN_MSG_CMSG_CLOEXEC', have_broken_msg_cmsg_cloexec) if get_option('libraries') - if host_machine.system() == 'freebsd' - # When building for FreeBSD, epoll(7) is provided by a userspace - # wrapper around kqueue(2). + if host_machine.system() in ['darwin', 'freebsd'] + # When building for darwin or FreeBSD, epoll(7) is provided by a + # userspace wrapper around kqueue(2). epoll_dep = dependency('epoll-shim') else # Otherwise, assume that epoll(7) is supported natively. From 2a7bce324b0f55a153991a9b7260a086a213df05 Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 19:54:02 -0800 Subject: [PATCH 10/15] wayland-egl: Update wayland-egl-symbols-check for darwin darwin prefixes C symbols with an underscore character Signed-off-by: Jeremy Huddleston Sequoia --- egl/wayland-egl-symbols-check | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/egl/wayland-egl-symbols-check b/egl/wayland-egl-symbols-check index d04fd042..6adef0aa 100755 --- a/egl/wayland-egl-symbols-check +++ b/egl/wayland-egl-symbols-check @@ -14,7 +14,12 @@ if ! test -n "$NM"; then exit 99 fi -AVAIL_FUNCS="$($NM -D --format=bsd --defined-only $LIB | awk '{print $3}')" +# Assuming any system with SystemVersion.plist is a darwin-derivative and prefixes C symbols with _ +if [ -f "/System/Library/CoreServices/SystemVersion.plist" ] ; then + AVAIL_FUNCS="$($NM $LIB | awk '{print $3}' | sed 's:^_::')" +else + AVAIL_FUNCS="$($NM -D --format=bsd --defined-only $LIB | awk '{print $3}')" +fi # Official ABI, taken from the header. REQ_FUNCS="wl_egl_window_resize From a200085120f1d43da19181b1f4e56e6f9cec31f5 Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 17:27:19 -0800 Subject: [PATCH 11/15] tests: Fix a format error on darwin test-compositor.c:94:25: error: format specifies type 'long' but the argument has type '__darwin_suseconds_t' (aka 'int') [-Werror,-Wformat] getpid(), tv.tv_sec, tv.tv_usec); ^~~~~~~~~~ Signed-off-by: Jeremy Huddleston Sequoia --- tests/test-compositor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-compositor.c b/tests/test-compositor.c index 103ddc85..b866f4c5 100644 --- a/tests/test-compositor.c +++ b/tests/test-compositor.c @@ -91,7 +91,7 @@ get_socket_name(void) gettimeofday(&tv, NULL); snprintf(retval, sizeof retval, "wayland-test-%d-%ld%ld", - getpid(), tv.tv_sec, tv.tv_usec); + (int)getpid(), (long)tv.tv_sec, (long)tv.tv_usec); return retval; } From 91f428c80be36fa66c0d384c03d0956199384a55 Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 17:36:44 -0800 Subject: [PATCH 12/15] tests: Implement fallback is_debugger_attached() that returns undetermined Signed-off-by: Jeremy Huddleston Sequoia --- tests/test-runner.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test-runner.c b/tests/test-runner.c index d07dab15..868e1ff5 100644 --- a/tests/test-runner.c +++ b/tests/test-runner.c @@ -308,6 +308,12 @@ is_debugger_attached(void) return rc; } +#else +static int +is_debugger_attached(void) +{ + return 0; +} #endif int main(int argc, char *argv[]) From a8a1d3d1cf49d60354d851a201dad1755a574947 Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 19:45:12 -0800 Subject: [PATCH 13/15] tests: Silence strict prototype warning tests/test-compositor.c:460:30: warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes] struct client *client_connect() ^ void 1 warning generated. Signed-off-by: Jeremy Huddleston Sequoia --- tests/test-compositor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-compositor.c b/tests/test-compositor.c index b866f4c5..d0fa2f94 100644 --- a/tests/test-compositor.c +++ b/tests/test-compositor.c @@ -457,7 +457,7 @@ static const struct wl_registry_listener registry_listener = NULL }; -struct client *client_connect() +struct client *client_connect(void) { struct wl_registry *reg; struct client *c = calloc(1, sizeof *c); From 276a28afd188f8e0bfe321d1de4239f5189509cc Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 23:25:36 -0800 Subject: [PATCH 14/15] tests: Specify the test section as __RODATA on darwin Addresses build failures on darwin such as: tests/os-wrappers-test.c:413:1: error: argument to 'section' attribute is not valid for this target: mach-o section specifier requires a segment and section separated by a comma TEST(os_wrappers_epoll_create_cloexec_fallback) ^ ../tests/test-runner.h:44:35: note: expanded from macro 'TEST' __attribute__ ((used, section ("test_section"))) = { \ ^ Signed-off-by: Jeremy Huddleston Sequoia --- tests/test-runner.c | 5 +++++ tests/test-runner.h | 10 ++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/test-runner.c b/tests/test-runner.c index 868e1ff5..7a61ecba 100644 --- a/tests/test-runner.c +++ b/tests/test-runner.c @@ -63,7 +63,12 @@ static int timeouts_enabled = 1; /* set to one if the output goes to the terminal */ static int is_atty = 0; +#ifdef __APPLE__ +extern const struct test __start_test_section __asm("section$start$__RODATA$test_section"); +extern const struct test __stop_test_section __asm("section$end$__RODATA$test_section"); +#else extern const struct test __start_test_section, __stop_test_section; +#endif static const struct test * find_test(const char *name) diff --git a/tests/test-runner.h b/tests/test-runner.h index d0734009..2024fa13 100644 --- a/tests/test-runner.h +++ b/tests/test-runner.h @@ -31,6 +31,12 @@ #include +#ifdef __APPLE__ +#define TEST_SECTION "__RODATA,test_section" +#else +#define TEST_SECTION "test_section" +#endif + struct test { const char *name; void (*run)(void); @@ -41,7 +47,7 @@ struct test { static void name(void); \ \ const struct test test##name \ - __attribute__ ((used, section ("test_section"))) = { \ + __attribute__ ((used, section (TEST_SECTION))) = { \ #name, name, 0 \ }; \ \ @@ -51,7 +57,7 @@ struct test { static void name(void); \ \ const struct test test##name \ - __attribute__ ((used, section ("test_section"))) = { \ + __attribute__ ((used, section (TEST_SECTION))) = { \ #name, name, 1 \ }; \ \ From 04654ba7b57ccbf583db899a327f8f7000f90546 Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Fri, 27 Jan 2023 23:51:20 -0800 Subject: [PATCH 15/15] tests: Update os-wrappers-test to build on non-ELF platforms The !ELF case just avoids the weak link-fu that was added to support sanitizers. Signed-off-by: Jeremy Huddleston Sequoia --- tests/os-wrappers-test.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/os-wrappers-test.c b/tests/os-wrappers-test.c index 717fc19f..50218594 100644 --- a/tests/os-wrappers-test.c +++ b/tests/os-wrappers-test.c @@ -47,6 +47,7 @@ static int fall_back; +#ifdef __ELF__ /* Play nice with sanitizers * * Sanitizers need to intercept syscalls in the compiler run-time library. As @@ -71,6 +72,13 @@ static int fall_back; #define REAL(func) (__interceptor_ ## func) ? \ __interceptor_ ## func : \ (__typeof__(&__interceptor_ ## func))dlsym(RTLD_NEXT, #func) +#else +#define DECL(ret_type, func, ...) \ + static ret_type (*real_ ## func)(__VA_ARGS__); \ + static int wrapped_calls_ ## func; + +#define REAL(func) (__typeof__(real_ ## func)) dlsym(RTLD_NEXT, #func) +#endif DECL(int, socket, int, int, int); DECL(int, fcntl, int, int, ...);