Blame SOURCES/bugfix-fixed-a-segfault-when-unsinking-64-bit-pointers.patch

006bc1
From a6a2720ddc22f9f62f119325881d05722c4f392e Mon Sep 17 00:00:00 2001
006bc1
From: Thibault Charbonnier <thibaultcha@me.com>
006bc1
Date: Tue, 19 Mar 2019 13:52:51 -0700
006bc1
Subject: [PATCH 1/3] bugfix: fixed a segfault when unsinking 64-bit pointers.
006bc1
006bc1
The unsinking code was not using the correct layout for GC64 IR
006bc1
constants (value in adjacent slot) for this case.
006bc1
006bc1
This patch is a derivative of
006bc1
https://github.com/raptorjit/raptorjit/pull/246 ported for LuaJIT
006bc1
itself.
006bc1
006bc1
Fixed after an intense debugging session with @lukego.
006bc1
006bc1
Co-authored-by: Luke Gorrie <lukego@gmail.com>
006bc1
---
006bc1
 src/lj_ir.h   | 12 ++++++------
006bc1
 src/lj_snap.c |  2 +-
006bc1
 2 files changed, 7 insertions(+), 7 deletions(-)
006bc1
006bc1
diff --git a/src/lj_ir.h b/src/lj_ir.h
006bc1
index 8057a750..a46b561f 100644
006bc1
--- a/src/lj_ir.h
006bc1
+++ b/src/lj_ir.h
006bc1
@@ -562,6 +562,11 @@ typedef union IRIns {
006bc1
   TValue tv;		/* TValue constant (overlaps entire slot). */
006bc1
 } IRIns;
006bc1
 
006bc1
+#define ir_isk64(ir) ((ir)->o == IR_KNUM || (ir)->o == IR_KINT64 || \
006bc1
+                      (LJ_GC64 && \
006bc1
+                       ((ir)->o == IR_KGC || \
006bc1
+                       (ir)->o == IR_KPTR || (ir)->o == IR_KKPTR)))
006bc1
+
006bc1
 #define ir_kgc(ir)	check_exp((ir)->o == IR_KGC, gcref((ir)[LJ_GC64].gcr))
006bc1
 #define ir_kstr(ir)	(gco2str(ir_kgc((ir))))
006bc1
 #define ir_ktab(ir)	(gco2tab(ir_kgc((ir))))
006bc1
@@ -569,12 +574,7 @@ typedef union IRIns {
006bc1
 #define ir_kcdata(ir)	(gco2cd(ir_kgc((ir))))
006bc1
 #define ir_knum(ir)	check_exp((ir)->o == IR_KNUM, &(ir)[1].tv)
006bc1
 #define ir_kint64(ir)	check_exp((ir)->o == IR_KINT64, &(ir)[1].tv)
006bc1
-#define ir_k64(ir) \
006bc1
-  check_exp((ir)->o == IR_KNUM || (ir)->o == IR_KINT64 || \
006bc1
-	    (LJ_GC64 && \
006bc1
-	     ((ir)->o == IR_KGC || \
006bc1
-	      (ir)->o == IR_KPTR || (ir)->o == IR_KKPTR)), \
006bc1
-	    &(ir)[1].tv)
006bc1
+#define ir_k64(ir)	check_exp(ir_isk64(ir), &(ir)[1].tv)
006bc1
 #define ir_kptr(ir) \
006bc1
   check_exp((ir)->o == IR_KPTR || (ir)->o == IR_KKPTR, \
006bc1
     mref((ir)[LJ_GC64].ptr, void))
006bc1
diff --git a/src/lj_snap.c b/src/lj_snap.c
006bc1
index ceaf2ca5..75888d80 100644
006bc1
--- a/src/lj_snap.c
006bc1
+++ b/src/lj_snap.c
006bc1
@@ -688,7 +688,7 @@ static void snap_restoredata(GCtrace *T, ExitState *ex,
006bc1
   int32_t *src;
006bc1
   uint64_t tmp;
006bc1
   if (irref_isk(ref)) {
006bc1
-    if (ir->o == IR_KNUM || ir->o == IR_KINT64) {
006bc1
+    if (ir_isk64(ir)) {
006bc1
       src = (int32_t *)&ir[1];
006bc1
     } else if (sz == 8) {
006bc1
       tmp = (uint64_t)(uint32_t)ir->i;
006bc1
-- 
006bc1
2.21.0
006bc1
006bc1
006bc1
From f36cddf49b664d713bfa7c332673bdc66861d2ad Mon Sep 17 00:00:00 2001
006bc1
From: Thibault Charbonnier <thibaultcha@me.com>
006bc1
Date: Tue, 19 Mar 2019 13:49:18 -0700
006bc1
Subject: [PATCH 2/3] tests: ffi: added a test case unsinking a 64-bit pointer
006bc1
 from a constant.
006bc1
006bc1
This test case reproduces the issue observed at:
006bc1
https://github.com/openresty/lua-resty-core/issues/232 and was
006bc1
contributed by @lukego and myself.
006bc1
006bc1
Co-authored-by: Luke Gorrie <lukego@gmail.com>
006bc1
---
006bc1
 test/ffi/unsink_64_kptr.lua | 26 ++++++++++++++++++++++++++
006bc1
 1 file changed, 26 insertions(+)
006bc1
 create mode 100644 test/ffi/unsink_64_kptr.lua
006bc1
006bc1
diff --git a/test/ffi/unsink_64_kptr.lua b/test/ffi/unsink_64_kptr.lua
006bc1
new file mode 100644
006bc1
index 00000000..7fab0e89
006bc1
--- /dev/null
006bc1
+++ b/test/ffi/unsink_64_kptr.lua
006bc1
@@ -0,0 +1,26 @@
006bc1
+local ffi = require("ffi")
006bc1
+
006bc1
+local array = ffi.new("struct { int x; } [1]")
006bc1
+
006bc1
+-- This test forces the VM to unsink a pointer that was constructed
006bc1
+-- from a constant. The IR will include a 'cnewi' instruction to
006bc1
+-- allocate an FFI pointer object, the pointer value will be an IR
006bc1
+-- constant, the allocation will be sunk, and the allocation will
006bc1
+-- at some point be "unsunk" due to a reference in the snapshot for
006bc1
+-- a taken exit.
006bc1
+
006bc1
+-- Note: JIT will recognize <array> as a "singleton" and allow its
006bc1
+-- address to be inlined ("constified") instead of looking up the
006bc1
+-- upvalue at runtime.
006bc1
+
006bc1
+local function fn(i)
006bc1
+  local struct = array[0]   -- Load pointer that the JIT will constify.
006bc1
+  if i == 1000 then end     -- Force trace exit when i==1000.
006bc1
+  struct.x = 0              -- Ensure that 'struct' is live after exit.
006bc1
+end
006bc1
+
006bc1
+-- Loop over the function to make it compile and take a trace exit
006bc1
+-- during the final iteration.
006bc1
+for i = 1, 1000 do
006bc1
+  fn(i)
006bc1
+end
006bc1
-- 
006bc1
2.21.0
006bc1
006bc1
006bc1
From 7b2f874b8061f206b22c04aee336b15030213637 Mon Sep 17 00:00:00 2001
006bc1
From: Siddhesh Poyarekar <siddhesh@sourceware.org>
006bc1
Date: Tue, 14 May 2019 22:01:37 +0530
006bc1
Subject: [PATCH 3/3] Make unsink_64_kptr usable in the testsuite
006bc1
006bc1
---
006bc1
 test/lib/ffi/index                    | 1 +
006bc1
 test/{ => lib}/ffi/unsink_64_kptr.lua | 6 ++++--
006bc1
 2 files changed, 5 insertions(+), 2 deletions(-)
006bc1
 rename test/{ => lib}/ffi/unsink_64_kptr.lua (93%)
006bc1
006bc1
diff --git a/test/lib/ffi/index b/test/lib/ffi/index
006bc1
index 59e36dd8..7933c5a7 100644
006bc1
--- a/test/lib/ffi/index
006bc1
+++ b/test/lib/ffi/index
006bc1
@@ -10,3 +10,4 @@ jit_struct.lua
006bc1
 meta_tostring.lua
006bc1
 redir.lua
006bc1
 type_punning.lua
006bc1
+unsink_64_kptr.lua
006bc1
diff --git a/test/ffi/unsink_64_kptr.lua b/test/lib/ffi/unsink_64_kptr.lua
006bc1
similarity index 93%
006bc1
rename from test/ffi/unsink_64_kptr.lua
006bc1
rename to test/lib/ffi/unsink_64_kptr.lua
006bc1
index 7fab0e89..f285d9ff 100644
006bc1
--- a/test/ffi/unsink_64_kptr.lua
006bc1
+++ b/test/lib/ffi/unsink_64_kptr.lua
006bc1
@@ -21,6 +21,8 @@ end
006bc1
 
006bc1
 -- Loop over the function to make it compile and take a trace exit
006bc1
 -- during the final iteration.
006bc1
-for i = 1, 1000 do
006bc1
-  fn(i)
006bc1
+do --- unsink 64-bit pointers
006bc1
+  for i = 1, 1000 do
006bc1
+    fn(i)
006bc1
+  end
006bc1
 end
006bc1
-- 
006bc1
2.21.0
006bc1