Subject: Re: bug#46824: 28.0.50; nativecomp crash with cl-block/cl-defun

From: Pip Cet <[email-masked]>
Date: Mon, 1 Mar 2021 13:02:03 +0000

On Mon, Mar 1, 2021 at 9:02 AM Andrea Corallo <[email-masked]> wrote:
> Pip Cet <[email-masked]> writes:
> > On Sun, Feb 28, 2021 at 6:13 AM Pip Cet <[email-masked]> wrote:
> >> Andrea, is it possible to modify the code so _setjmp is called
> >> directly rather than through a function pointer?
> >
> > This patch, which I hasten to add is not for inclusion and hasn't been
> > tested against the test suite, might fix the problem (on non-Windows
> > systems. I'm afraid this problem is something that Windows actually
> > got right and Unix got wrong...) It does here, experimentally.
> >
> > I'd appreciate it if someone could test further.
>
> I haven't looked into the patch but the approach sounds like a good idea
> to me. Can we have the version of the patch you'd like to have included?

Here's the version I'd like to include. This should not cause any new
test suite failures, just the ones I've been seeing all along. Apart
from the ABI bump, it should not change anything on WINDOWSNT, which I
can't test.

Pip
From 4bf47460dc4df61c79737964ff11fa9f5bf1102f Mon Sep 17 00:00:00 2001
From: Pip Cet <[email-masked]>
Date: Sun, 28 Feb 2021 06:31:00 +0000
Subject: [PATCH] Don't call _setjmp through a function pointer (Bug#46824)

* src/comp.c (helper_link_table): Don't include SETJMP except on Windows.
(emit_setjmp): Don't use function pointers except on Windows.
(declare_runtime_imported_funcs): Don't import SETJMP at runtime.
(ABI_VERSION): Bump.
---
 src/comp.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/src/comp.c b/src/comp.c
index 21d1c1a23cf0e..3b002535768b4 100644
--- a/src/comp.c
+++ b/src/comp.c
@@ -416,7 +416,7 @@ load_gccjit_if_necessary (bool mandatory)
 
 
 /* Increase this number to force a new Vcomp_abi_hash to be generated.  */
-#define ABI_VERSION "1"
+#define ABI_VERSION "2"
 
 /* Length of the hashes used for eln file naming.  */
 #define HASH_LENGTH 8
@@ -640,7 +640,9 @@ #define NUM_CAST_TYPES 15
     helper_PSEUDOVECTOR_TYPEP_XUNTAG,
     pure_write_error,
     push_handler,
+#ifdef WINDOWSNT
     SETJMP_NAME,
+#endif
     record_unwind_protect_excursion,
     helper_unbind_n,
     helper_save_restriction,
@@ -1929,8 +1931,20 @@ emit_setjmp (gcc_jit_rvalue *buf)
 {
 #ifndef WINDOWSNT
   gcc_jit_rvalue *args[] = {buf};
-  return emit_call (intern_c_string (STR (SETJMP_NAME)), comp.int_type, 1, args,
-                   false);
+  gcc_jit_param *params[] =
+    {
+      gcc_jit_context_new_param (comp.ctxt, NULL, comp.void_ptr_type, "buf"),
+    };
+  /* Don't call setjmp through a function pointer (Bug#46824) */
+  return gcc_jit_context_new_call (comp.ctxt,
+				   NULL,
+				   gcc_jit_context_new_function
+				   (comp.ctxt, NULL, GCC_JIT_FUNCTION_IMPORTED,
+				    comp.int_type, STR (SETJMP_NAME),
+				    ARRAYELTS (params), params,
+				    false),
+				   1,
+				   args);
 #else
   /* _setjmp (buf, __builtin_frame_address (0)) */
   gcc_jit_rvalue *args[2];
@@ -2662,10 +2676,7 @@ #define ADD_IMPORTED(f_name, ret_type, nargs, args)			       \
   args[1] = comp.int_type;
   ADD_IMPORTED (push_handler, comp.handler_ptr_type, 2, args);
 
-#ifndef WINDOWSNT
-  args[0] = gcc_jit_type_get_pointer (gcc_jit_struct_as_type (comp.jmp_buf_s));
-  ADD_IMPORTED (SETJMP_NAME, comp.int_type, 1, args);
-#else
+#ifdef WINDOWSNT
   args[0] = gcc_jit_type_get_pointer (gcc_jit_struct_as_type (comp.jmp_buf_s));
   args[1] = comp.void_ptr_type;
   ADD_IMPORTED (SETJMP_NAME, comp.int_type, 2, args);
-- 
2.30.1