Subject: Re: bug#32452: 26.1; gnutls_try_handshake maxes out cpu retrying when server is a bit busy

From: Paul Eggert <[email-masked]>
Date: Tue, 8 Mar 2022 19:01:25 -0800

On 3/5/22 09:11, Lars Ingebrigtsen wrote:
>   	$(AM_V_at)$(CC) -shared $(CPPFLAGS) $(MODULE_CFLAGS) $(LDFLAGS) \
>   	  -o $@ $< $(LIBGMP) \
> +	  ../lib/libgnu.a \

Unfortunately that won't work in general; the comment for mod-test.so 
says "In the compilation command, we can't use any object or archive 
file as source because those are not compiled with -fPIC" but as things 
stood mod-test.so was being linked with libgnu.a.

Anyway, mod-test.so should be decoupled from Emacs as much as possible - 
that's the whole point of modules after all - so the mod-test.so rule 
shouldn't make assumptions about how Emacs internals use Gnulib. I 
installed the attached patches to lessen the coupling between 
mod-test.so and Emacs internals and I hope this fixes the problem. It 
may need to be adjusted a tad for platforms that lack 
nanosleep/clock_gettime/libgmp whatever, but the basic idea should work 
on these platforms with only minor twiddling.

Even with these patches, mod-test.so uses "config.h"; but at least it 
doesn't use Gnulib code.

From 5b96a5136aacc82a634d41407d4473494596f196 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Tue, 8 Mar 2022 18:46:47 -0800
Subject: [PATCH 1/2] Do not include <attribute.h> from <config.h>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is because mod-test.c shouldn’t use source code from lib,
but it does need to include <config.h>.
* lib-src/ebrowse.c, lib-src/emacsclient.c, lib-src/etags.c:
* lib-src/make-docfile.c, lib-src/movemail.c:
* lib-src/seccomp-filter.c, src/dynlib.h, src/lisp.h:
* src/syssignal.h, src/sysstdio.h, src/systhread.h, src/tparam.h:
Include <attribute.h>.
* src/conf_post.h: Do not include <attribute.h>.
All uses of attribute.h macros replaced with their _GL_ equivalents.
---
 lib-src/ebrowse.c        |  1 +
 lib-src/emacsclient.c    |  1 +
 lib-src/etags.c          |  1 +
 lib-src/make-docfile.c   |  1 +
 lib-src/movemail.c       |  1 +
 lib-src/seccomp-filter.c |  3 ++-
 src/conf_post.h          | 19 +++++++++----------
 src/dynlib.h             |  1 +
 src/lisp.h               |  1 +
 src/syssignal.h          |  2 ++
 src/sysstdio.h           |  4 +++-
 src/systhread.h          |  2 ++
 src/tparam.h             |  2 ++
 13 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/lib-src/ebrowse.c b/lib-src/ebrowse.c
index ac7e790187..b77572734f 100644
--- a/lib-src/ebrowse.c
+++ b/lib-src/ebrowse.c
@@ -26,6 +26,7 @@ Copyright (C) 1992-2022 Free Software Foundation, Inc.
 #include <assert.h>
 #include <getopt.h>
 
+#include <attribute.h>
 #include <flexmember.h>
 #include <min-max.h>
 #include <unlocked-io.h>
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 365e803e1c..7406ef3490 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -80,6 +80,7 @@ Copyright (C) 1986-1987, 1994, 1999-2022 Free Software Foundation, Inc.
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include <attribute.h>
 #include <filename.h>
 #include <intprops.h>
 #include <min-max.h>
diff --git a/lib-src/etags.c b/lib-src/etags.c
index aa5bc8839d..65b9fae8d5 100644
--- a/lib-src/etags.c
+++ b/lib-src/etags.c
@@ -112,6 +112,7 @@ Copyright (C) 1984, 1987-1989, 1993-1995, 1998-2022 Free Software
 # define O_CLOEXEC O_NOINHERIT
 #endif /* WINDOWSNT */
 
+#include <attribute.h>
 #include <inttypes.h>
 #include <limits.h>
 #include <unistd.h>
diff --git a/lib-src/make-docfile.c b/lib-src/make-docfile.c
index f42b1988a2..d2d4b1d277 100644
--- a/lib-src/make-docfile.c
+++ b/lib-src/make-docfile.c
@@ -42,6 +42,7 @@ Copyright (C) 1985-1986, 1992-1994, 1997, 1999-2022 Free Software
 #include <stdlib.h>
 #include <string.h>
 
+#include <attribute.h>
 #include <binary-io.h>
 #include <c-ctype.h>
 #include <intprops.h>
diff --git a/lib-src/movemail.c b/lib-src/movemail.c
index 888688f90b..aa3c15e72e 100644
--- a/lib-src/movemail.c
+++ b/lib-src/movemail.c
@@ -69,6 +69,7 @@ Copyright (C) 1986, 1992-1994, 1996, 1999, 2001-2022 Free Software
 #include <signal.h>
 #include <string.h>
 
+#include <attribute.h>
 #include <unlocked-io.h>
 
 #include "syswait.h"
diff --git a/lib-src/seccomp-filter.c b/lib-src/seccomp-filter.c
index b515851010..9f0de7d64f 100644
--- a/lib-src/seccomp-filter.c
+++ b/lib-src/seccomp-filter.c
@@ -59,7 +59,8 @@ Copyright (C) 2020-2022 Free Software Foundation, Inc.
 #include <seccomp.h>
 #include <unistd.h>
 
-#include "verify.h"
+#include <attribute.h>
+#include <verify.h>
 
 #ifndef ARCH_CET_STATUS
 #define ARCH_CET_STATUS 0x3001
diff --git a/src/conf_post.h b/src/conf_post.h
index dd350b8dc8..5108e44efb 100644
--- a/src/conf_post.h
+++ b/src/conf_post.h
@@ -32,13 +32,11 @@ Copyright (C) 1988, 1993-1994, 1999-2002, 2004-2022 Free Software
 /* To help make dependencies clearer elsewhere, this file typically
    does not #include other files.  The exceptions are stdbool.h
    because it is unlikely to interfere with configuration and bool is
-   such a core part of the C language, attribute.h because its
-   ATTRIBUTE_* macros are used here, and ms-w32.h (DOS_NT
+   such a core part of the C language, and ms-w32.h (DOS_NT
    only) because it historically was included here and changing that
    would take some work.  */
 
 #include <stdbool.h>
-#include <attribute.h>
 
 #if defined WINDOWSNT && !defined DEFER_MS_W32_H
 # include <ms-w32.h>
@@ -279,8 +277,8 @@ #define setenv_TZ emacs_setenv_TZ
 extern char *emacs_getenv_TZ (void);
 extern int emacs_setenv_TZ (char const *);
 
-#define NO_INLINE ATTRIBUTE_NOINLINE
-#define EXTERNALLY_VISIBLE ATTRIBUTE_EXTERNALLY_VISIBLE
+#define NO_INLINE _GL_ATTRIBUTE_NOINLINE
+#define EXTERNALLY_VISIBLE _GL_ATTRIBUTE_EXTERNALLY_VISIBLE
 
 #if GNUC_PREREQ (4, 4, 0) && defined __GLIBC_MINOR__
 # define PRINTF_ARCHETYPE __gnu_printf__
@@ -310,9 +308,9 @@ #define EXTERNALLY_VISIBLE ATTRIBUTE_EXTERNALLY_VISIBLE
 # define PRINTF_ARCHETYPE __printf__
 #endif
 #define ATTRIBUTE_FORMAT_PRINTF(string_index, first_to_check) \
-  ATTRIBUTE_FORMAT ((PRINTF_ARCHETYPE, string_index, first_to_check))
+  _GL_ATTRIBUTE_FORMAT ((PRINTF_ARCHETYPE, string_index, first_to_check))
 
-#define ARG_NONNULL ATTRIBUTE_NONNULL
+#define ARG_NONNULL _GL_ATTRIBUTE_NONNULL
 
 /* Declare NAME to be a pointer to an object of type TYPE, initialized
    to the address ADDR, which may be of a different type.  Accesses
@@ -320,15 +318,16 @@ #define ARG_NONNULL ATTRIBUTE_NONNULL
    behavior, even if options like gcc -fstrict-aliasing are used.  */
 
 #define DECLARE_POINTER_ALIAS(name, type, addr) \
-  type ATTRIBUTE_MAY_ALIAS *name = (type *) (addr)
+  type _GL_ATTRIBUTE_MAY_ALIAS *name = (type *) (addr)
 
 #if 3 <= __GNUC__
 # define ATTRIBUTE_SECTION(name) __attribute__((section (name)))
 #else
-#define ATTRIBUTE_SECTION(name)
+# define ATTRIBUTE_SECTION(name)
 #endif
 
-#define ATTRIBUTE_MALLOC_SIZE(args) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE (args)
+#define ATTRIBUTE_MALLOC_SIZE(args) \
+  _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE (args)
 
 /* Work around GCC bug 59600: when a function is inlined, the inlined
    code may have its addresses sanitized even if the function has the
diff --git a/src/dynlib.h b/src/dynlib.h
index ac3d8e58ab..03b8f98356 100644
--- a/src/dynlib.h
+++ b/src/dynlib.h
@@ -20,6 +20,7 @@
 #ifndef DYNLIB_H
 #define DYNLIB_H
 
+#include <attribute.h>
 #include <stdbool.h>
 
 typedef void *dynlib_handle_ptr;
diff --git a/src/lisp.h b/src/lisp.h
index deeca9bc86..778bd1bfa5 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -31,6 +31,7 @@ #define EMACS_LISP_H
 #include <inttypes.h>
 #include <limits.h>
 
+#include <attribute.h>
 #include <intprops.h>
 #include <verify.h>
 
diff --git a/src/syssignal.h b/src/syssignal.h
index 07055c04be..02fe44a382 100644
--- a/src/syssignal.h
+++ b/src/syssignal.h
@@ -22,6 +22,8 @@ #define EMACS_SYSSIGNAL_H
 
 #include <signal.h>
 
+#include <attribute.h>
+
 extern void init_signals (void);
 extern void block_child_signal (sigset_t *);
 extern void unblock_child_signal (sigset_t const *);
diff --git a/src/sysstdio.h b/src/sysstdio.h
index 5bcfe7d8a5..727a466be5 100644
--- a/src/sysstdio.h
+++ b/src/sysstdio.h
@@ -24,7 +24,9 @@ #define EMACS_SYSSTDIO_H
 #include <fcntl.h>
 #include <limits.h>
 #include <stdio.h>
-#include "unlocked-io.h"
+
+#include <attribute.h>
+#include <unlocked-io.h>
 
 extern FILE *emacs_fopen (char const *, char const *) ATTRIBUTE_MALLOC;
 extern void errputc (int);
diff --git a/src/systhread.h b/src/systhread.h
index fb1a0a72d6..bf4e0306cd 100644
--- a/src/systhread.h
+++ b/src/systhread.h
@@ -21,6 +21,8 @@ #define SYSTHREAD_H
 
 #include <stdbool.h>
 
+#include <attribute.h>
+
 #ifdef THREADS_ENABLED
 
 #ifdef HAVE_PTHREAD
diff --git a/src/tparam.h b/src/tparam.h
index 6361f138ea..653f01bdde 100644
--- a/src/tparam.h
+++ b/src/tparam.h
@@ -20,6 +20,8 @@ Copyright (C) 2011-2022 Free Software Foundation, Inc.
 #ifndef EMACS_TPARAM_H
 #define EMACS_TPARAM_H
 
+#include <attribute.h>
+
 /* Don't try to include termcap.h.  On some systems, configure finds a
    non-standard termcap.h that the main build won't find.  */
 
-- 
2.35.1

From 3b86bc162cca1991a1ce66445e5d8f0d8f3bc7b2 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Tue, 8 Mar 2022 18:46:47 -0800
Subject: [PATCH 2/2] Decouple mod-test.c from Gnulib
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

mod-test.c should not use Gnulib code, as that creates
unnecessary coupling between Emacs internals and this test module.
Also Gnulib code is compiled without -fPIC whereas mod-test.c
needs -fPIC and recompiling Gnulib with -fPIC would be too painful.
* src/Makefile.in (LIB_NANOSLEEP): New macro.
(LIBES): Use it.
* test/Makefile.in (REPLACE_FREE, FREE_SOURCE_0, FREE_SOURCE_0):
Remove.  All uses removed.
(LIB_CLOCK_GETTIME, LIB_NANOSLEEP): New macros.
(MODULE_CFLAGS): Do not -I from lib as that would include Gnulib
modifications to standard .h files (e.g., "#define nanosleep
rpl_nanosleep") and we don’t want the Gnulib replacements.
Instead, for gmp.h (on platforms lacking <gmp.h>) simply '-I.'
with a suitable gmp.h.
(gmp.h): New rule to create a suitable gmp.h.
($(test_module)): Depend on config.h since the code uses config.h.
Depend on gmp.h if we need to create a suitable one.
If compiling mini-gmp.h, compile the original one and not
the Emacs-specific one, to lessen coupling with Emacs internals.
Link with LIB_CLOCK_GETTIME and LIB_NANOSLEEP.
(clean): Remove gmp.h.
* test/src/emacs-module-resources/mod-test.c: Don’t include timespec.h.
All uses of timespec.h APIs changed to use system-supplied APIs.
Change _Static_assert to plain assert, so that we needn’t rely
on Gnulib’s _Static_assert.
(timespec_le) [CLOCK_REALTIME]: New function.
Change use of timespec_cmp changed to use this instead.
(Fmod_test_sleep_until, Fmod_test_nanoseconds):
Define these functions and their Lisp names mod-test-sleep-until
and mod-test-nanoseconds only if CLOCK_REALTIME,
since they now won’t work on platforms lacking CLOCK_REALTIME.
(Fmod_test_nanoseconds): Just use _Static_assert since it
should work on all platforms.
* test/src/emacs-module-tests.el (mod-test-sleep-until)
(mod-test-nanoseconds, mod-test-double):
Skip test if the corresponding functione is not defined.
---
 src/Makefile.in                            |  3 +-
 test/Makefile.in                           | 22 ++++++------
 test/src/emacs-module-resources/mod-test.c | 42 ++++++++++++++--------
 test/src/emacs-module-tests.el             |  3 ++
 4 files changed, 44 insertions(+), 26 deletions(-)

diff --git a/src/Makefile.in b/src/Makefile.in
index 3353fb16d7..2b7c4bb316 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -146,6 +146,7 @@ M17N_FLT_LIBS =
 LIB_ACL=@LIB_ACL@
 LIB_CLOCK_GETTIME=@LIB_CLOCK_GETTIME@
 LIB_EACCESS=@LIB_EACCESS@
+LIB_NANOSLEEP=@LIB_NANOSLEEP@
 LIB_TIMER_TIME=@LIB_TIMER_TIME@
 
 DBUS_CFLAGS = @DBUS_CFLAGS@
@@ -548,7 +549,7 @@ lisp =
 LIBES = $(LIBS) $(W32_LIBS) $(LIBS_GNUSTEP) $(PGTK_LIBS) $(LIBX_BASE) $(LIBIMAGE) \
    $(LIBX_OTHER) $(LIBSOUND) \
    $(RSVG_LIBS) $(IMAGEMAGICK_LIBS) $(LIB_ACL) $(LIB_CLOCK_GETTIME) \
-   $(WEBKIT_LIBS) \
+   $(LIB_NANOSLEEP) $(WEBKIT_LIBS) \
    $(LIB_EACCESS) $(LIB_TIMER_TIME) $(DBUS_LIBS) \
    $(LIB_EXECINFO) $(XRANDR_LIBS) $(XINERAMA_LIBS) $(XFIXES_LIBS) \
    $(XDBE_LIBS) $(XSYNC_LIBS) \
diff --git a/test/Makefile.in b/test/Makefile.in
index 79d3dcf92f..3b6e116e65 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -49,8 +49,6 @@ SEPCHAR =
 
 HAVE_NATIVE_COMP = @HAVE_NATIVE_COMP@
 
-REPLACE_FREE = @REPLACE_FREE@
-
 -include ${top_builddir}/src/verbose.mk
 
 # We never change directory before running Emacs, so a relative file
@@ -265,27 +263,29 @@ FPIC_CFLAGS =
 
 GMP_H = @GMP_H@
 LIBGMP = @LIBGMP@
+LIB_CLOCK_GETTIME = @LIB_CLOCK_GETTIME@
+LIB_NANOSLEEP = @LIB_NANOSLEEP@
 
-MODULE_CFLAGS = -I../src -I$(srcdir)/../src -I../lib -I$(srcdir)/../lib \
+MODULE_CFLAGS = $(and $(GMP_H),-I.) -I../src -I$(srcdir)/../src \
   $(FPIC_CFLAGS) $(PROFILING_CFLAGS) \
   $(WARN_CFLAGS) $(WERROR_CFLAGS) $(CFLAGS)
 
+gmp.h:
+	echo '#include "$(srcdir)/../lib/mini-gmp.h"' >$@
+
 test_module = $(test_module_dir)/mod-test${SO}
 src/emacs-module-tests.log src/emacs-module-tests.elc: $(test_module)
 
-FREE_SOURCE_0 =
-FREE_SOURCE_1 = $(srcdir)/../lib/free.c
-
 # In the compilation command, we can't use any object or archive file
 # as source because those are not compiled with -fPIC.  Therefore we
 # use only source files.
-$(test_module): $(test_module:${SO}=.c) ../src/emacs-module.h
+$(test_module): $(test_module:${SO}=.c) ../src/emacs-module.h \
+  ../src/config.h $(and $(GMP_H),gmp.h)
 	$(AM_V_CCLD)${MKDIR_P} $(dir $@)
 	$(AM_V_at)$(CC) -shared $(CPPFLAGS) $(MODULE_CFLAGS) $(LDFLAGS) \
 	  -o $@ $< $(LIBGMP) \
-	  $(and $(GMP_H),$(srcdir)/../lib/mini-gmp-gnulib.c) \
-	  $(FREE_SOURCE_$(REPLACE_FREE)) \
-	  ../lib/libgnu.a
+	  $(and $(GMP_H),$(srcdir)/../lib/mini-gmp.c) \
+	  $(LIB_CLOCK_GETTIME) $(LIB_NANOSLEEP)
 endif
 
 src/emacs-tests.log: ../lib-src/seccomp-filter.c
@@ -345,7 +345,7 @@ clean:
 	find . '(' -name '*.xml' -a ! -path '*resources*' ')' $(FIND_DELETE)
 	rm -f ${srcdir}/lisp/gnus/mml-sec-resources/random_seed
 	rm -f $(test_module_dir)/*.o $(test_module_dir)/*.so \
-	  $(test_module_dir)/*.dll
+	  $(test_module_dir)/*.dll gmp.h
 
 bootstrap-clean: clean
 	find $(srcdir) -name '*.elc' $(FIND_DELETE)
diff --git a/test/src/emacs-module-resources/mod-test.c b/test/src/emacs-module-resources/mod-test.c
index 015c1efd97..187af821c2 100644
--- a/test/src/emacs-module-resources/mod-test.c
+++ b/test/src/emacs-module-resources/mod-test.c
@@ -47,8 +47,6 @@
 #include <gmp.h>
 #include <emacs-module.h>
 
-#include "timespec.h"
-
 int plugin_is_GPL_compatible;
 
 #if INTPTR_MAX <= 0
@@ -74,9 +72,6 @@
 # error "INTPTR_MAX too large"
 #endif
 
-/* Smoke test to verify that EMACS_LIMB_MAX is defined. */
-_Static_assert (0 < EMACS_LIMB_MAX, "EMACS_LIMB_MAX missing or incorrect");
-
 /* Always return symbol 't'.  */
 static emacs_value
 Fmod_test_return_t (emacs_env *env, ptrdiff_t nargs, emacs_value args[],
@@ -422,6 +417,16 @@ signal_errno (emacs_env *env, const char *function)
   signal_system_error (env, errno, function);
 }
 
+#ifdef CLOCK_REALTIME
+
+/* Whether A <= B.  */
+static bool
+timespec_le (struct timespec a, struct timespec b)
+{
+  return (a.tv_sec < b.tv_sec
+	  || (a.tv_sec == b.tv_sec && a.tv_nsec <= b.tv_nsec));
+}
+
 /* A long-running operation that occasionally calls `should_quit' or
    `process_input'.  */
 
@@ -434,11 +439,13 @@ Fmod_test_sleep_until (emacs_env *env, ptrdiff_t nargs, emacs_value *args,
   if (env->non_local_exit_check (env))
     return NULL;
   const bool process_input = env->is_not_nil (env, args[1]);
-  const struct timespec amount = make_timespec(0,  10000000);
+  const struct timespec amount = { .tv_nsec = 10000000 };
   while (true)
     {
-      const struct timespec now = current_timespec ();
-      if (timespec_cmp (now, until) >= 0)
+      struct timespec now;
+      if (clock_gettime (CLOCK_REALTIME, &now) != 0)
+	return NULL;
+      if (timespec_le (until, now))
         break;
       if (nanosleep (&amount, NULL) && errno != EINTR)
         {
@@ -452,6 +459,7 @@ Fmod_test_sleep_until (emacs_env *env, ptrdiff_t nargs, emacs_value *args,
     }
   return env->intern (env, "finished");
 }
+#endif
 
 static emacs_value
 Fmod_test_add_nanosecond (emacs_env *env, ptrdiff_t nargs, emacs_value *args,
@@ -553,6 +561,7 @@ make_big_integer (emacs_env *env, const mpz_t value)
   return result;
 }
 
+#ifdef CLOCK_REALTIME
 static emacs_value
 Fmod_test_nanoseconds (emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data) {
   assert (nargs == 1);
@@ -560,11 +569,6 @@ Fmod_test_nanoseconds (emacs_env *env, ptrdiff_t nargs, emacs_value *args, void
   mpz_t nanoseconds;
   assert (LONG_MIN <= time.tv_sec && time.tv_sec <= LONG_MAX);
   mpz_init_set_si (nanoseconds, time.tv_sec);
-#ifdef __MINGW32__
-  _Static_assert (1000000000 <= ULONG_MAX, "unsupported architecture");
-#else
-  static_assert (1000000000 <= ULONG_MAX, "unsupported architecture");
-#endif
   mpz_mul_ui (nanoseconds, nanoseconds, 1000000000);
   assert (0 <= time.tv_nsec && time.tv_nsec <= ULONG_MAX);
   mpz_add_ui (nanoseconds, nanoseconds, time.tv_nsec);
@@ -572,6 +576,7 @@ Fmod_test_nanoseconds (emacs_env *env, ptrdiff_t nargs, emacs_value *args, void
   mpz_clear (nanoseconds);
   return result;
 }
+#endif
 
 static emacs_value
 Fmod_test_double (emacs_env *env, ptrdiff_t nargs, emacs_value *args,
@@ -631,7 +636,7 @@ sleep_for_half_second (void)
 #ifdef WINDOWSNT
   Sleep (500);
 #else
-  const struct timespec sleep = {0, 500000000};
+  const struct timespec sleep = { .tv_nsec = 500000000 };
   if (nanosleep (&sleep, NULL) != 0)
     perror ("nanosleep");
 #endif
@@ -763,6 +768,11 @@ bind_function (emacs_env *env, const char *name, emacs_value Sfun)
 int
 emacs_module_init (struct emacs_runtime *ert)
 {
+  /* These smoke tests don't use _Static_assert because too many
+     compilers lack support for _Static_assert.  */
+  assert (0 < EMACS_LIMB_MAX);
+  assert (1000000000 <= ULONG_MAX);
+
   /* Check that EMACS_MAJOR_VERSION is defined and an integral
      constant.  */
   char dummy[EMACS_MAJOR_VERSION];
@@ -815,9 +825,13 @@ #define DEFUN(lsym, csym, amin, amax, doc, data) \
   DEFUN ("mod-test-invalid-load", Fmod_test_invalid_load, 0, 0, NULL, NULL);
   DEFUN ("mod-test-invalid-finalizer", Fmod_test_invalid_finalizer, 0, 0,
          NULL, NULL);
+#ifdef CLOCK_REALTIME
   DEFUN ("mod-test-sleep-until", Fmod_test_sleep_until, 2, 2, NULL, NULL);
+#endif
   DEFUN ("mod-test-add-nanosecond", Fmod_test_add_nanosecond, 1, 1, NULL, NULL);
+#ifdef CLOCK_REALTIME
   DEFUN ("mod-test-nanoseconds", Fmod_test_nanoseconds, 1, 1, NULL, NULL);
+#endif
   DEFUN ("mod-test-double", Fmod_test_double, 1, 1, NULL, NULL);
   DEFUN ("mod-test-make-function-with-finalizer",
          Fmod_test_make_function_with_finalizer, 0, 0, NULL, NULL);
diff --git a/test/src/emacs-module-tests.el b/test/src/emacs-module-tests.el
index ec83f91f00..1099fd0467 100644
--- a/test/src/emacs-module-tests.el
+++ b/test/src/emacs-module-tests.el
@@ -336,6 +336,7 @@ module/load-history
 (ert-deftest mod-test-sleep-until ()
   "Check that `mod-test-sleep-until' either returns normally or quits.
 Interactively, you can try hitting \\[keyboard-quit] to quit."
+  (skip-unless (fboundp 'mod-test-sleep-until))
   (dolist (arg '(nil t))
     ;; Guard against some caller setting `inhibit-quit'.
     (with-local-quit
@@ -390,6 +391,7 @@ mod-test-add-nanosecond/invalid
 
 (ert-deftest mod-test-nanoseconds ()
   "Test truncation when converting to `struct timespec'."
+  (skip-unless (fboundp 'mod-test-nanoseconds))
   (dolist (test-case '((0 . 0)
                        (-1 . -1000000000)
                        ((1 . 1000000000) . 1)
@@ -408,6 +410,7 @@ mod-test-nanoseconds
         (should (= (mod-test-nanoseconds input) expected))))))
 
 (ert-deftest mod-test-double ()
+  (skip-unless (fboundp 'mod-test-double))
   (dolist (input (list 0 1 2 -1 42 12345678901234567890
                        most-positive-fixnum (1+ most-positive-fixnum)
                        most-negative-fixnum (1- most-negative-fixnum)))
-- 
2.35.1