bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* jit/cache tests: Fix for powerpc*, ia64, hppa* CPUs
@ 2024-01-08 21:54 Bruno Haible
  0 siblings, 0 replies; only message in thread
From: Bruno Haible @ 2024-01-08 21:54 UTC (permalink / raw)
  To: bug-gnulib

I was seeing test failures on platforms with powerpc*, ia64, hppa* CPUs.
The reason is a thinko in the test: The statements

  int (*f) (void) = return1;
  CODE (f) = start;

modify the function pointer &return1 (!), and then the memcpy invocation

  memcpy (start_rw, CODE (return1), 64);

is a no-op (since the first two arguments have the same value), thus in the line

  ASSERT (f () == 1);

we get an Illegal instruction.

This patch fixes it.


2024-01-08  Bruno Haible  <bruno@clisp.org>

	jit/cache tests: Fix for powerpc*, ia64, hppa* CPUs.
	* modules/jit/cache-tests (Depends-on): Add xalloc.
	* tests/jit/test-cache.c: Include xalloc.h.
	(FUNCPTR_POINTS_TO_CODE, COPY_FUNCPTR): New macros.
	(xcopy_funcptr): New function.
	(main): Create a copy of the function pointer return1, so as not to
	destructively modify return1. Fix memcpy argument.

diff --git a/modules/jit/cache-tests b/modules/jit/cache-tests
index d777684093..2b30831f1d 100644
--- a/modules/jit/cache-tests
+++ b/modules/jit/cache-tests
@@ -12,6 +12,7 @@ clean-temp-simple
 getpagesize
 host-cpu-c-abi
 stdint
+xalloc
 
 configure.ac:
 AC_CHECK_HEADERS_ONCE([sys/mman.h])
diff --git a/tests/jit/test-cache.c b/tests/jit/test-cache.c
index b19a25ec4f..184e157776 100644
--- a/tests/jit/test-cache.c
+++ b/tests/jit/test-cache.c
@@ -36,6 +36,7 @@
 # include "clean-temp-simple.h"
 #endif
 
+#include "xalloc.h"
 #include "macros.h"
 
 /*  On most platforms, function pointers are just a pointer to the
@@ -43,7 +44,6 @@
     however, is not universally true, see:
     https://git.savannah.gnu.org/gitweb/?p=libffcall.git;a=blob;f=porting-tools/abis/function-pointer.txt.  */
 
-#define CODE(fn) (((struct func *) (fn))->code_address)
 #if ((defined __powerpc__ || defined __powerpc64__) && defined _AIX) || (defined __powerpc64__ && defined __linux__)
 struct func
 {
@@ -84,8 +84,22 @@ struct func
 };
 # endif
 #else
-# undef CODE
-# define CODE(fn) ((*(void **) (&fn)))
+# define FUNCPTR_POINTS_TO_CODE
+#endif
+#ifdef FUNCPTR_POINTS_TO_CODE
+/* A function pointer points directly to the code.  */
+# define COPY_FUNCPTR(funcptr) funcptr
+# define CODE(funcptr) ((void*)(funcptr))
+#else
+/* A function pointer points to a 'struct func'.  */
+static struct func *xcopy_funcptr (struct func *orig_funcptr)
+{
+  struct func *copy = (struct func *) xmalloc (sizeof (struct func));
+  *copy = *orig_funcptr;
+  return copy;
+}
+# define COPY_FUNCPTR(funcptr) (void *) xcopy_funcptr ((struct func *)(funcptr))
+# define CODE(funcptr) (((struct func *)(funcptr))->code_address)
 #endif
 
 /* This test assumes that the code generated by the compiler for the
@@ -188,16 +202,16 @@ main ()
     end = start + mapping_size;
   }
 
-  int (*f) (void) = return1;
+  int (*f) (void) = COPY_FUNCPTR (return1);
   CODE (f) = 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_rw, return1, 64);
+  memcpy (start_rw, CODE (return1), 64);
   clear_cache (start, end);
   ASSERT (f () == 1);
 
-  memcpy (start_rw, return2, 64);
+  memcpy (start_rw, CODE (return2), 64);
   clear_cache (start, end);
   ASSERT (f () == 2);
 





^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-08 21:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-08 21:54 jit/cache tests: Fix for powerpc*, ia64, hppa* CPUs Bruno Haible

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).