From ee70f952d1e33055d71d2cc4702394383daa79fb Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Mon, 28 Mar 2022 11:53:40 +0100 Subject: [PATCH] Allow POSIX PALs to overwrite writev and fsync. (#495) On FreeBSD (possibly elsewhere) the normal versions of these go via an indirection layer because they are pthread cancellation points. This indirection layer does not work correctly if pthreads can't allocate memory and so we can't get debug output until malloc is working, at least a little bit. With this version, we can call the __sys_ variants, which skip any libc / libthr interposition. --- src/pal/pal_bsd.h | 4 ++-- src/pal/pal_bsd_aligned.h | 4 ++-- src/pal/pal_freebsd.h | 12 +++++++++++- src/pal/pal_posix.h | 10 ++++++---- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/pal/pal_bsd.h b/src/pal/pal_bsd.h index a07b7c5..4689b43 100644 --- a/src/pal/pal_bsd.h +++ b/src/pal/pal_bsd.h @@ -10,8 +10,8 @@ namespace snmalloc * Generic *BSD PAL mixin. This provides features that are common to the BSD * family. */ - template - class PALBSD : public PALPOSIX + template + class PALBSD : public PALPOSIX { public: /** diff --git a/src/pal/pal_bsd_aligned.h b/src/pal/pal_bsd_aligned.h index c770a52..4c88287 100644 --- a/src/pal/pal_bsd_aligned.h +++ b/src/pal/pal_bsd_aligned.h @@ -10,8 +10,8 @@ namespace snmalloc * This adds aligned allocation using `MAP_ALIGNED` to the generic BSD * implementation. This flag is supported by NetBSD and FreeBSD. */ - template - class PALBSD_Aligned : public PALBSD + template + class PALBSD_Aligned : public PALBSD { public: /** diff --git a/src/pal/pal_freebsd.h b/src/pal/pal_freebsd.h index 916fc70..d24f4b7 100644 --- a/src/pal/pal_freebsd.h +++ b/src/pal/pal_freebsd.h @@ -13,6 +13,15 @@ # endif # endif +/** + * Direct system-call wrappers so that we can skip libthr interception, which + * won't work if malloc is broken. + * @{ + */ +extern "C" ssize_t __sys_writev(int fd, const struct iovec* iov, int iovcnt); +extern "C" int __sys_fsync(int fd); +/// @} + namespace snmalloc { /** @@ -21,7 +30,8 @@ namespace snmalloc * This adds FreeBSD-specific aligned allocation to the generic BSD * implementation. */ - class PALFreeBSD : public PALBSD_Aligned + class PALFreeBSD + : public PALBSD_Aligned { public: /** diff --git a/src/pal/pal_posix.h b/src/pal/pal_posix.h index 16c5ef7..add7712 100644 --- a/src/pal/pal_posix.h +++ b/src/pal/pal_posix.h @@ -34,11 +34,13 @@ namespace snmalloc * efficient implementation. Subclasses should provide more efficient * implementations using platform-specific functionality. * - * The template parameter for this is the subclass and is used for explicit - * up casts to allow this class to call non-virtual methods on the templated - * version. + * The first template parameter for this is the subclass and is used for + * explicit up casts to allow this class to call non-virtual methods on the + * templated version. The next two allow subclasses to provide `writev` and + * `fsync` implementations that bypass any libc machinery that might not be + * working when an early-malloc error appears. */ - template + template class PALPOSIX : public PalTimerDefaultImpl> { /**