10000 Indicate that _PyGC_Head is only 8-byte aligned. (closes bpo-33374) · python/cpython@0b91f8a · GitHub
[go: up one dir, main page]

Skip to content

Commit 0b91f8a

Browse files
fweimerbenjaminp
authored andcommitted
Indicate that _PyGC_Head is only 8-byte aligned. (closes bpo-33374)
By spec, the "long double" in _PyGC_Head requires the union to always be 16-byte aligned. However, obmalloc only yields 8-byte alignment. Compilers including GCC 8 are starting to use alignment information to do store-merging. So, the "long double" needs to be changed to a simple "double" as was long ago done in Python 3 by e348c8d. For 2.7, we need to add some dummy padding to make sure _PyGC_Head stays the same size.
1 parent bad9a58 commit 0b91f8a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Include/objimpl.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,29 @@ PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
248248
/* for source compatibility with 2.2 */
249249
#define _PyObject_GC_Del PyObject_GC_Del
250250

251+
/*
252+
* Former over-aligned definition of PyGC_Head, used to compute the size of the
253+
* padding for the new version below.
254+
*/
255+
union _gc_head;
256+
union _gc_head_old {
257+
struct {
258+
union _gc_head_old *gc_next;
259+
union _gc_head_old *gc_prev;
260+
Py_ssize_t gc_refs;
261+
} gc;
262+
long double dummy;
263+
};
264+
251265
/* GC information is stored BEFORE the object structure. */
252266
typedef union _gc_head {
253267
struct {
254268
union _gc_head *gc_next;
255269
union _gc_head *gc_prev;
256270
Py_ssize_t gc_refs;
257271
} gc;
258-
long double dummy; /* force worst-case alignment */
272+
double dummy; /* Force at least 8-byte alignment. */
273+
char dummy_padding[sizeof(union _gc_head_old)];
259274
} PyGC_Head;
260275

261276
extern PyGC_Head *_PyGC_generation0;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Tweak the definition of PyGC_Head, so compilers do not believe it is always
2+
16-byte aligned on x86. This prevents crashes with more aggressive
3+
optimizations present in GCC 8.

0 commit comments

Comments
 (0)
0