From 7dc666132ae9bf8ce217b1e48bee4491bec36b56 Mon Sep 17 00:00:00 2001 From: local-dev Date: Wed, 27 May 2026 16:39:50 -0700 Subject: [PATCH] fix(libc): guard hash_page sigprocmask use behind _NO_SIGPROCMASK newlib/libc/search/hash_page.c::open_temp() unconditionally calls sigprocmask() and sigfillset() around a mkstemp + unlink window. Targets whose newlib build does not provide sigprocmask (e.g. nanvix) therefore fail to link when libc.a is pulled in with --whole-archive: undefined references to sigprocmask and sigfillset. Introduce _NO_SIGPROCMASK as a target opt-out, matching the existing _NO_SIGSET / _NO_POPEN / _NO_POSIX_SPAWN family already used by newlib/configure.host. The default code path is unchanged; targets that lack sigprocmask add -D_NO_SIGPROCMASK to newlib_cflags and the masking step is compiled out. This mirrors the in-function HAVE_FCNTL guard around fcntl(F_SETFD, ...) on the next line: an optional libc API the target may or may not provide is gated by a capability macro defined per target in configure.host. Effect of the opt-out: the mkstemp + unlink window is no longer protected by a blocked signal mask. On a target with no asynchronous signal delivery, no handler can run inside that window, so the protection was a no-op in practice. configure.host is updated only for *-nanvix*; other targets see no behavior change. Related, not fixed in this PR: newlib/libc/posix/posix_spawn.c also references sigprocmask unconditionally; same root cause, separate fix. Verification: preprocessor + unifdef gating test under ghcr.io/nanvix/toolchain-gcc:sha-34a3641 confirms that with -D_NO_SIGPROCMASK the sigprocmask/sigfillset calls are elided, and that without the macro they remain present (default behavior preserved for all other targets). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- newlib/configure.host | 2 +- newlib/libc/search/hash_page.c | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/newlib/configure.host b/newlib/configure.host index 428b673404..d3e4a8a381 100644 --- a/newlib/configure.host +++ b/newlib/configure.host @@ -801,7 +801,7 @@ newlib_cflags="${newlib_cflags} -DCLOCK_PROVIDED -DMALLOC_PROVIDED -DEXIT_PROVID *-nanvix*) default_newlib_io_long_long="yes" syscall_dir= - newlib_cflags="${newlib_cflags} -DMALLOC_PROVIDED -DREENTRANT_SYSCALLS_PROVIDED -DMISSING_SYSCALL_NAMES" + newlib_cflags="${newlib_cflags} -DMALLOC_PROVIDED -DREENTRANT_SYSCALLS_PROVIDED -DMISSING_SYSCALL_NAMES -D_NO_SIGPROCMASK" ;; nios2*) syscall_dir= diff --git a/newlib/libc/search/hash_page.c b/newlib/libc/search/hash_page.c index 9d15b0cb70..5ff70d5bb1 100644 --- a/newlib/libc/search/hash_page.c +++ b/newlib/libc/search/hash_page.c @@ -857,19 +857,41 @@ static int open_temp(hashp) HTAB *hashp; { + /* + * The sigprocmask() bracket below guards the mkstemp + unlink + * window against a signal that would otherwise leave the temp + * file behind. Targets whose newlib build does not provide + * sigprocmask() can opt out of that protection by defining + * _NO_SIGPROCMASK via newlib_cflags in configure.host, in the + * same style as the existing _NO_SIGSET / _NO_POPEN / + * _NO_POSIX_SPAWN opt-outs (see newlib/configure.host). + * + * Effect of the opt-out: the mkstemp + unlink window is no + * longer protected by a blocked signal mask. On a target with + * no asynchronous signal delivery, no handler can run in that + * window so the protection was a no-op anyway. The optional + * fcntl(F_SETFD, ...) on the next line uses the same shape via + * HAVE_FCNTL. + */ +#ifndef _NO_SIGPROCMASK sigset_t set, oset; +#endif static char namestr[] = "_hashXXXXXX"; - /* Block signals; make sure file goes away at process exit. */ +#ifndef _NO_SIGPROCMASK + /* Block signals so the file is guaranteed unlinked. */ (void)sigfillset(&set); (void)sigprocmask(SIG_BLOCK, &set, &oset); +#endif if ((hashp->fp = mkstemp(namestr)) != -1) { (void)unlink(namestr); #ifdef HAVE_FCNTL (void)fcntl(hashp->fp, F_SETFD, 1); #endif } +#ifndef _NO_SIGPROCMASK (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL); +#endif return (hashp->fp != -1 ? 0 : -1); }