Blame 00358-align-allocations-and-pygc_head-to-16-bytes-on-64-bit-platforms.patch

c490957
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
c490957
From: Inada Naoki <songofacandy@gmail.com>
c490957
Date: Mon, 3 Jun 2019 10:51:32 +0900
c490957
Subject: [PATCH] 00358: align allocations and PyGC_Head to 16 bytes on 64-bit
c490957
 platforms
c490957
c490957
Upstream bug: https://bugs.python.org/issue27987
c490957
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1923658
c490957
c490957
Combination of two upstream commits:
c490957
- 1b85f4ec45a5d63188ee3866bd55eb29fdec7fbf
c490957
- 8766cb74e186d3820db0a855ccd780d6d84461f7
c490957
---
c490957
 Include/objimpl.h                                           | 6 +++++-
c490957
 .../2019-05-15-18-28-43.bpo-27987.FaxuLy.rst                | 2 ++
c490957
 Objects/obmalloc.c                                          | 6 ++++++
c490957
 3 files changed, 13 insertions(+), 1 deletion(-)
c490957
 create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-05-15-18-28-43.bpo-27987.FaxuLy.rst
c490957
c490957
diff --git a/Include/objimpl.h b/Include/objimpl.h
c490957
index e7a3696d7a..90d71b87ea 100644
c490957
--- a/Include/objimpl.h
c490957
+++ b/Include/objimpl.h
c490957
@@ -255,7 +255,11 @@ typedef union _gc_head {
c490957
         union _gc_head *gc_prev;
c490957
         Py_ssize_t gc_refs;
c490957
     } gc;
c490957
-    double dummy;  /* force worst-case alignment */
c490957
+    long double dummy;  /* force worst-case alignment */
c490957
+    // malloc returns memory block aligned for any built-in types and
c490957
+    // long double is the largest standard C type.
c490957
+    // On amd64 linux, long double requires 16 byte alignment.
c490957
+    // See bpo-27987 for more discussion.
c490957
 } PyGC_Head;
c490957
 
c490957
 extern PyGC_Head *_PyGC_generation0;
c490957
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-05-15-18-28-43.bpo-27987.FaxuLy.rst b/Misc/NEWS.d/next/Core and Builtins/2019-05-15-18-28-43.bpo-27987.FaxuLy.rst
c490957
new file mode 100644
c490957
index 0000000000..98073471ca
c490957
--- /dev/null
c490957
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-05-15-18-28-43.bpo-27987.FaxuLy.rst	
c490957
@@ -0,0 +1,2 @@
c490957
+``PyGC_Head`` structure is aligned to ``long double``.  This is needed to
c490957
+ensure GC-ed objects are aligned properly.  Patch by Inada Naoki.
c490957
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
c490957
index d46d149311..47283354f4 100644
c490957
--- a/Objects/obmalloc.c
c490957
+++ b/Objects/obmalloc.c
c490957
@@ -650,8 +650,14 @@ static int running_on_valgrind = -1;
c490957
  *
c490957
  * You shouldn't change this unless you know what you are doing.
c490957
  */
c490957
+
c490957
+#if SIZEOF_VOID_P > 4
c490957
+#define ALIGNMENT              16               /* must be 2^N */
c490957
+#define ALIGNMENT_SHIFT         4
c490957
+#else
c490957
 #define ALIGNMENT               8               /* must be 2^N */
c490957
 #define ALIGNMENT_SHIFT         3
c490957
+#endif
c490957
 
c490957
 /* Return the number of bytes in size class I, as a uint. */
c490957
 #define INDEX2SIZE(I) (((uint)(I) + 1) << ALIGNMENT_SHIFT)