Skip to content

Commit 6ecf553

Browse files
committed
Specify minimum PyGC_Head alignment to fix build failure
As documented in InternalDocs/garbage_collector.md, the garbage collector stores flags in the least significant two bits of the _gc_prev pointer in struct PyGC_Head. Consequently, this pointer is only capable of storing a location that's aligned to a 4-byte boundary. This alignment requirement is documented but it's not actually encoded. The code works when python happens to run on a platform that has a large minimum alignment, but fails otherwise. Since we know that 2 bits are needed, we also know the minimum alignment that's needed. Let's make that explicit, so the compiler can then make sure those 2 bits are available. This patch fixes a segfault in _bootstrap_python. It also clarifies the code by making the actual requirement explicit. This bug was investigated by Adrian Glaubitz here: https://lists.debian.org/debian-68k/2024/11/msg00020.html https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1087600 Although Adrian's patch isn't really correct (because natural alignment is not needed), he deserves full credit for finding the root cause.
1 parent 1f8267b commit 6ecf553

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Include/internal/pycore_gc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ typedef struct {
1616

1717
// Pointer to previous object in the list.
1818
// Lowest two bits are used for flags documented later.
19+
// Those two bits are made available by the struct's minimum alignment.
1920
uintptr_t _gc_prev;
20-
} PyGC_Head;
21+
} PyGC_Head Py_ALIGNED(4);
2122

2223
#define _PyGC_Head_UNUSED PyGC_Head
2324

0 commit comments

Comments
 (0)