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

From: Pip Cet <[email-masked]>
Date: Sun, 28 Feb 2021 06:36:06 +0000

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.

Pip
From cfde527ad69eb76204218a3193bb0c534003c7a0 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)

This is because _setjmp returns twice; any stack modifications
performed after the first return are still visible after the second
return, causing havoc. Code like this

stack_var = value1;
if (_setjmp())
  {
    assert (stack_var == value1);
  }
else
  {
    stack_var = value2;
    longjmp()
  }

will fail.

I'm not sure whether the fix will actually avoid the problem given
libgccjit's idiosyncracies.
---
 src/comp.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/comp.c b/src/comp.c
index a8b8ef95fa14d..8031f4bd67d56 100644
--- a/src/comp.c
+++ b/src/comp.c
@@ -1929,8 +1929,18 @@ 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"),
+  };
+  return gcc_jit_context_new_call (comp.ctxt,
+				   NULL,
+				   gcc_jit_context_new_function
+				   (comp.ctxt, NULL, GCC_JIT_FUNCTION_IMPORTED,
+				    comp.int_type, "_setjmp",
+				    ARRAYELTS (params), params,
+				    false),
+				   1,
+				   args);
 #else
   /* _setjmp (buf, __builtin_frame_address (0)) */
   gcc_jit_rvalue *args[2];
-- 
2.30.1