Skip to content

Commit bf4a1cb

Browse files
committed
Skip unreliable assertions
Only check immortality in GIL builds, and only check static immortality if it's flag-based.
1 parent 56094d5 commit bf4a1cb

23 files changed

Lines changed: 114 additions & 69 deletions

‎Include/internal/pycore_global_objects_fini.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ extern "C" {
1212

1313
#include "pycore_bytesobject.h" // _PyBytes_CheckOverflow()
1414
#include "pycore_long.h" // TAG_FROM_SIGN_AND_SIZE()
15+
#include "pycore_object.h" // _Py_IMMORTALITY_IS_RELIABLE
1516

1617
static inline void
1718
_PyStaticObject_CheckSingleton(PyObject *obj, PyTypeObject *type)
1819
{
19-
// Check PyObject.ob_refcnt
20-
_PyObject_ASSERT(obj, _Py_IsImmortal(obj));
20+
// Check immortality
21+
_PyObject_ASSERT(obj, _Py_IsImmortal(obj) || !_Py_IMMORTALITY_IS_RELIABLE);
2122

2223
// Check PyObject.ob_type
2324
_PyObject_ASSERT(obj, Py_TYPE(obj) == type);

‎Include/internal/pycore_long.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C" {
1010

1111
#include "pycore_bytesobject.h" // _PyBytesWriter
1212
#include "pycore_runtime.h" // _Py_SINGLETON()
13+
#include "pycore_object.h" // _Py_ASSERT_STATIC_IMMORTAL
1314

1415
/*
1516
* Default int base conversion size limitation: Denial of Service prevention.
@@ -240,7 +241,7 @@ _PyLong_IsSmallInt(const PyLongObject *op)
240241
bool is_small_int = (op->long_value.lv_tag & IMMORTALITY_BIT_MASK) != 0;
241242
if (is_small_int) {
242243
assert(PyLong_CheckExact(op));
243-
assert(_Py_IsImmortal(op));
244+
_Py_ASSERT_STATIC_IMMORTAL(op);
244245
assert((_PyLong_IsCompact(op)
245246
&& _PY_IS_SMALL_INT(_PyLong_CompactValue(op))));
246247
}

‎Include/internal/pycore_object.h‎

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,41 @@ extern "C" {
1818

1919
#include <stdbool.h> // bool
2020

21+
// Stable ABI extensions compiled for Python 3.11 and lower can make
22+
// immortal objects, including statically allocated ones, mortal.
23+
// free-threading and 64-bit-GIL builds use flags to track immortality,
24+
// so _Py_IsImmortal and _Py_IsStaticImmortal are trustworthy.
25+
// On 32-bit-GIL builds, they are not.
26+
#ifndef _Py_IMMORTALITY_IS_RELIABLE
27+
# if defined(Py_GIL_DISABLED)
28+
# define _Py_IMMORTALITY_IS_RELIABLE 1
29+
# else
30+
# define _Py_IMMORTALITY_IS_RELIABLE 0
31+
# endif
32+
#endif
33+
#ifndef _Py_STATIC_IMMORTALITY_IS_RELIABLE
34+
# if defined(Py_GIL_DISABLED) || (SIZEOF_VOID_P > 4)
35+
# define _Py_STATIC_IMMORTALITY_IS_RELIABLE 1
36+
# else
37+
# define _Py_STATIC_IMMORTALITY_IS_RELIABLE 0
38+
# endif
39+
#endif
40+
// Define the most common assertion macros.
41+
#if _Py_IMMORTALITY_IS_RELIABLE
42+
# define _Py_ASSERT_IMMORTAL(op) assert(_Py_IsImmortal(op))
43+
# define _Py_ASSERT_MORTAL(op) assert(!_Py_IsImmortal(op))
44+
#else
45+
# define _Py_ASSERT_IMMORTAL(op) ((void)(op))
46+
# define _Py_ASSERT_MORTAL(op) ((void)(op))
47+
#endif
48+
#if _Py_STATIC_IMMORTALITY_IS_RELIABLE
49+
# define _Py_ASSERT_STATIC_IMMORTAL(op) assert(_Py_IsStaticImmortal(op))
50+
# define _Py_ASSERT_NOT_STATIC_IMMORTAL(op) assert(!_Py_IsStaticImmortal(op))
51+
#else
52+
# define _Py_ASSERT_STATIC_IMMORTAL(op) ((void)(op))
53+
# define _Py_ASSERT_NOT_STATIC_IMMORTAL(op) ((void)(op))
54+
#endif
55+
2156

2257
// This value is added to `ob_ref_shared` for objects that use deferred
2358
// reference counting so that they are not immediately deallocated when the
@@ -200,7 +235,7 @@ PyAPI_FUNC(void) _Py_SetImmortalUntracked(PyObject *op);
200235
static inline void _Py_SetMortal(PyObject *op, short refcnt)
201236
{
202237
if (op) {
203-
assert(_Py_IsImmortal(op));
238+
_Py_ASSERT_IMMORTAL(op);
204239
#ifdef Py_GIL_DISABLED
205240
op->ob_tid = _Py_UNOWNED_TID;
206241
op->ob_ref_local = 0;
@@ -406,7 +441,7 @@ static inline void Py_DECREF_MORTAL(const char *filename, int lineno, PyObject *
406441
_Py_NegativeRefcount(filename, lineno, op);
407442
}
408443
_Py_DECREF_STAT_INC();
409-
assert(!_Py_IsStaticImmortal(op));
444+
_Py_ASSERT_NOT_STATIC_IMMORTAL(op);
410445
if (!_Py_IsImmortal(op)) {
411446
_Py_DECREF_DecRefTotal();
412447
}
@@ -422,7 +457,7 @@ static inline void _Py_DECREF_MORTAL_SPECIALIZED(const char *filename, int linen
422457
_Py_NegativeRefcount(filename, lineno, op);
423458
}
424459
_Py_DECREF_STAT_INC();
425-
assert(!_Py_IsStaticImmortal(op));
460+
_Py_ASSERT_NOT_STATIC_IMMORTAL(op);
426461
if (!_Py_IsImmortal(op)) {
427462
_Py_DECREF_DecRefTotal();
428463
}
@@ -440,7 +475,7 @@ static inline void _Py_DECREF_MORTAL_SPECIALIZED(const char *filename, int linen
440475

441476
static inline void Py_DECREF_MORTAL(PyObject *op)
442477
{
443-
assert(!_Py_IsStaticImmortal(op));
478+
_Py_ASSERT_NOT_STATIC_IMMORTAL(op);
444479
_Py_DECREF_STAT_INC();
445480
if (--op->ob_refcnt == 0) {
446481
_Py_Dealloc(op);
@@ -450,7 +485,7 @@ static inline void Py_DECREF_MORTAL(PyObject *op)
450485

451486
static inline void Py_DECREF_MORTAL_SPECIALIZED(PyObject *op, destructor destruct)
452487
{
453-
assert(!_Py_IsStaticImmortal(op));
488+
_Py_ASSERT_NOT_STATIC_IMMORTAL(op);
454489
_Py_DECREF_STAT_INC();
455490
if (--op->ob_refcnt == 0) {
456491
_PyReftracerTrack(op, PyRefTracer_DESTROY);
@@ -475,7 +510,11 @@ _PyObject_Init(PyObject *op, PyTypeObject *typeobj)
475510
{
476511
assert(op != NULL);
477512
Py_SET_TYPE(op, typeobj);
478-
assert(_PyType_HasFeature(typeobj, Py_TPFLAGS_HEAPTYPE) || _Py_IsImmortal(typeobj));
513+
#ifndef NDEBUG
514+
if (!_PyType_HasFeature(typeobj, Py_TPFLAGS_HEAPTYPE)) {
515+
_Py_ASSERT_IMMORTAL(typeobj);
516+
}
517+
#endif
479518
_Py_INCREF_TYPE(typeobj);
480519
_Py_NewReference(op);
481520
}
@@ -987,7 +1026,7 @@ extern int _PyObject_SetManagedDict(PyObject *obj, PyObject *new_dict);
9871026
#ifndef Py_GIL_DISABLED
9881027
static inline Py_ALWAYS_INLINE void _Py_INCREF_MORTAL(PyObject *op)
9891028
{
990-
assert(!_Py_IsStaticImmortal(op));
1029+
_Py_ASSERT_MORTAL(op);
9911030
op->ob_refcnt++;
9921031
_Py_INCREF_STAT_INC();
9931032
#if defined(Py_REF_DEBUG) && !defined(Py_LIMITED_API)

‎Include/internal/pycore_stackref.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ _PyStackRef_DupImmortal(_PyStackRef ref, const char *filename, int linenumber)
270270
assert(!PyStackRef_IsTaggedInt(ref));
271271
assert(!PyStackRef_RefcountOnObject(ref));
272272
PyObject *obj = _Py_stackref_get_object(ref);
273-
assert(_Py_IsImmortal(obj));
273+
_Py_ASSERT_IMMORTAL(obj);
274274
return _Py_stackref_create(obj, Py_TAG_REFCNT, filename, linenumber);
275275
}
276276
#define PyStackRef_DupImmortal(REF) _PyStackRef_DupImmortal((REF), __FILE__, __LINE__)
@@ -361,7 +361,7 @@ static inline _PyStackRef
361361
_PyStackRef_FromPyObjectNewMortal(PyObject *obj, const char *filename, int linenumber)
362362
{
363363
assert(obj != NULL);
364-
assert(!_Py_IsStaticImmortal(obj));
364+
_Py_ASSERT_NOT_STATIC_IMMORTAL(obj);
365365
Py_INCREF(obj);
366366
return _Py_stackref_create(obj, 0, filename, linenumber);
367367
}
@@ -502,7 +502,7 @@ PyStackRef_CheckValid(_PyStackRef ref)
502502
switch (tag) {
503503
case 0:
504504
/* Can be immortal if object was made immortal after reference came into existence */
505-
assert(!_Py_IsStaticImmortal(obj));
505+
_Py_ASSERT_NOT_STATIC_IMMORTAL(obj);
506506
break;
507507
case Py_TAG_REFCNT:
508508
break;
@@ -644,7 +644,7 @@ PyStackRef_DupImmortal(_PyStackRef ref)
644644
{
645645
assert(!PyStackRef_IsNull(ref));
646646
assert(!PyStackRef_RefcountOnObject(ref));
647-
assert(_Py_IsImmortal(BITS_TO_PTR_MASKED(ref)));
647+
_Py_ASSERT_IMMORTAL(BITS_TO_PTR_MASKED(ref));
648648
return ref;
649649
}
650650

‎Modules/_asynciomodule.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,7 @@ _asyncio_Future__state_get_impl(FutureObj *self)
16001600
default:
16011601
assert (0);
16021602
}
1603-
assert(_Py_IsImmortal(ret));
1603+
_Py_ASSERT_IMMORTAL(ret);
16041604
return ret;
16051605
}
16061606

‎Modules/_testinternalcapi/test_cases.c.h‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Modules/_typesmodule.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ _types_exec(PyObject *m)
1212
{
1313
#define EXPORT_STATIC_TYPE(NAME, TYPE) \
1414
do { \
15-
assert(PyUnstable_IsImmortal((PyObject *)&(TYPE))); \
15+
_Py_ASSERT_STATIC_IMMORTAL((PyObject *)&(TYPE)); \
1616
if (PyModule_AddObjectRef(m, (NAME), (PyObject *)&(TYPE)) < 0) { \
1717
return -1; \
1818
} \

‎Objects/bytearrayobject.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ bytearray_reinit_from_bytes(PyByteArrayObject *self, Py_ssize_t size)
6363
}
6464
else {
6565
// Use the empty bytes string singleton for an empty bytearray
66-
assert(_Py_IsImmortal(self->ob_bytes_object));
66+
_Py_ASSERT_IMMORTAL(self->ob_bytes_object);
6767
}
6868

6969
self->ob_bytes = self->ob_start = PyBytes_AS_STRING(self->ob_bytes_object);

‎Objects/bytesobject.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static inline PyObject*
4646
bytes_get_empty(void)
4747
{
4848
PyObject *empty = &EMPTY->ob_base.ob_base;
49-
assert(_Py_IsImmortal(empty));
49+
_Py_ASSERT_STATIC_IMMORTAL(empty);
5050
return empty;
5151
}
5252

@@ -56,7 +56,7 @@ static inline PyObject*
5656
bytes_get_char(uint8_t ch)
5757
{
5858
PyObject *obj = (PyObject*)CHARACTER(ch);
59-
assert(_Py_IsImmortal(obj));
59+
_Py_ASSERT_STATIC_IMMORTAL(obj);
6060
return obj;
6161
}
6262

‎Objects/codeobject.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3127,7 +3127,7 @@ intern_one_constant(PyObject *op)
31273127
return op;
31283128
}
31293129

3130-
assert(_Py_IsImmortal(entry->value));
3130+
_Py_ASSERT_IMMORTAL(entry->value);
31313131
return (PyObject *)entry->value;
31323132
}
31333133

0 commit comments

Comments
 (0)