--- /dev/null
+/* Test clear_cache.
+
+ Copyright 2023 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <jit/cache.h>
+
+#include <pagealign_alloc.h>
+#include <string.h>
+#include <unistd.h>
+
+#if HAVE_SYS_MMAN_H && HAVE_MPROTECT
+# include <sys/mman.h>
+#endif
+
+#include "macros.h"
+
+/* This test assumes that the code generated by the compiler for the
+ procedures `return1' and `return2' is position independent. It
+ also assumes that function pointers are compatible with data
+ pointers and that these are bit-compatible to integers. */
+
+static int
+return1 (void)
+{
+ return 1;
+}
+
+static int
+return2 (void)
+{
+ return 2;
+}
+
+int
+main ()
+{
+#if !(HAVE_SYS_MMAN_H && HAVE_SYS_MMAN_H)
+ return 77;
+#endif
+
+ int const pagesize = getpagesize ();
+ unsigned char *start = pagealign_xalloc (pagesize);
+ unsigned char *end = start + pagesize;
+
+ /* We have to call `mprotect' before the tests because on some
+ platforms `mprotect' invalidates the caches. */
+ mprotect (start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
+
+ int (*f) (void) = (void *) start;
+
+ /* We assume that the code is not longer than 64 bytes and that we
+ can access the full 64 bytes for reading. */
+ memcpy (start, return1, 64);
+ clear_cache (start, end);
+ ASSERT (f () == 1);
+
+ memcpy (start, return2, 64);
+ clear_cache (start, end);
+ ASSERT (f () == 2);
+
+ return 0;
+}