From 58560db768e2f5a29512346bc7d36706821fe8fd Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Sun, 14 Jun 2020 23:49:07 +0200 Subject: [PATCH] Restore build without __sync_fetch_and_{add,sub} (re: 07cc71b8) The more I think about it, the more it seems obvious that commit 07cc71b8 (PR #14) is quite simply a workaround for a GCC optimiser bug, and (who knows?) possibly an old, long-fixed one, as the bug report is years old. The commit also caused ksh to fail to build on HP-UX B.11.11 with GCC 4.2.3 (hosted at polarhome.com), because it doesn't have __sync_fetch_and_add() and __sync_fetch_and_sub(). It may fail on other systems. The GCC documentation says these are legacy: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html HELP WANTED: what I would like best is if someone could come up with some way of detecting this optimiser bug and then error out with a message along the lines of "please upgrade your broken compiler". It would probably need to be a new iffe test. Meanwhile, let's try it this way for a while and see what happens: src/cmd/ksh93/include/jobs.h: - Restore original ksh version of job_lock()/job_unlock() macros. - Use the workaround version only if the compiler has the builtins __sync_fetch_and_add() and __sync_fetch_and_sub(). --- TODO | 2 +- src/cmd/ksh93/include/jobs.h | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/TODO b/TODO index 9bfc5cd35..cd5da9df8 100644 --- a/TODO +++ b/TODO @@ -9,7 +9,7 @@ Fix regression test failures: ______ Fix build system: -- ksh does not currently build on NetBSD, AIX, HP-UX, Solaris, or QNX. +- ksh does not currently build on NetBSD, AIX, Solaris, or QNX. - Reimport the removed nmake. It is necessary for changes in Makefiles to take effect. The machine-generated Mamfiles are now used as a fallback, but they are not meant to be edited by hand. diff --git a/src/cmd/ksh93/include/jobs.h b/src/cmd/ksh93/include/jobs.h index 7072a37e1..af5ea5cd1 100644 --- a/src/cmd/ksh93/include/jobs.h +++ b/src/cmd/ksh93/include/jobs.h @@ -149,9 +149,17 @@ extern struct jobs job; #define vmbusy() 0 #endif +/* + * Job locking and unlocking macros + */ +#if defined(__sync_fetch_and_add) && defined(__sync_fetch_and_sub) +/* + * This version may prevent segfaults due to a GCC optimizer bug. + * See: https://bugzilla.redhat.com/show_bug.cgi?id=1112306 + * https://bugs.launchpad.net/ubuntu/+source/ksh/+bug/1697501 + */ #define asoincint(p) __sync_fetch_and_add(p,1) #define asodecint(p) __sync_fetch_and_sub(p,1) - #define job_lock() asoincint(&job.in_critical) #define job_unlock() \ do { \ @@ -163,6 +171,22 @@ extern struct jobs job; asodecint(&job.in_critical); \ } \ } while(0) +#else +/* + * Original ksh93 version. + */ +#define job_lock() (job.in_critical++) +#define job_unlock() \ + do { \ + int sig; \ + if (!--job.in_critical && (sig = job.savesig)) \ + { \ + if (!job.in_critical++ && !vmbusy()) \ + job_reap(sig); \ + job.in_critical--; \ + } \ + } while(0) +#endif /* defined(__sync_fetch_and_add) && defined(__sync_fetch_and_sub) */ extern const char e_jobusage[]; extern const char e_done[];