Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spurious -Warray-bounds warning on GCC 11 in free-threaded build #122974

Closed
colesbury opened this issue Aug 13, 2024 · 0 comments
Closed

Spurious -Warray-bounds warning on GCC 11 in free-threaded build #122974

colesbury opened this issue Aug 13, 2024 · 0 comments

Comments

@colesbury
Copy link
Contributor

colesbury commented Aug 13, 2024

After #122418, GCC 11+ now emits a (spurious) warning in optimized builds, but not debug builds. Clang does not issue a warning

Objects/longobject.c: In function ‘_PyLong_FromMedium’:
./Include/internal/pycore_object.h:310:19: warning: array subscript ‘PyHeapTypeObject {aka struct _heaptypeobject}[0]’ is partly outside array bounds of ‘PyTypeObject[1]’ {aka ‘struct _typeobject[1]’} [-Warray-bounds]
  310 |     if ((size_t)ht->unique_id < (size_t)tstate->types.size) {
      |                 ~~^~~~~~~~~~~
Ojects/longobject.c:6596:14: note: while referencing ‘PyLong_Type’
 6596 | PyTypeObject PyLong_Type = {
      |              ^~~~~~~~~~~

GCC is clever enough to inline the _Py_INCREF_TYPE call on PyLong_Type, but not clever enough to understand the _PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE) guard.

static inline void
_Py_INCREF_TYPE(PyTypeObject *type)
{
if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
assert(_Py_IsImmortal(type));
return;
}
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
PyHeapTypeObject *ht = (PyHeapTypeObject *)type;
// Unsigned comparison so that `unique_id=-1`, which indicates that
// per-thread refcounting has been disabled on this type, is handled by
// the "else".
if ((size_t)ht->unique_id < (size_t)tstate->types.size) {
# ifdef Py_REF_DEBUG
_Py_INCREF_IncRefTotal();
# endif
_Py_INCREF_STAT_INC();
tstate->types.refcounts[ht->unique_id]++;
}
else {
// The slow path resizes the thread-local refcount array if necessary.
// It handles the unique_id=-1 case to keep the inlinable function smaller.
_PyType_IncrefSlow(ht);
}
}

I don't see a great way to rewrite _Py_INCREF_TYPE to avoid the warning, so maybe we should just use a #pragma to suppress the Warray-bounds warnings in that function.

Linked PRs

colesbury added a commit to colesbury/cpython that referenced this issue Aug 16, 2024
…uild

GCC 11 and newerwarn about the access to `unique_id` in non-debug builds
due to inlining the call on static non-heap types.
colesbury added a commit to colesbury/cpython that referenced this issue Aug 16, 2024
…uild

GCC 11 and newer warn about the access to `unique_id` in non-debug builds
due to inlining the call on static non-heap types.
colesbury added a commit that referenced this issue Aug 17, 2024
…123071)

GCC 11 and newer warn about the access to `unique_id` in non-debug builds
due to inlining the call on static non-heap types.
jeremyhylton pushed a commit to jeremyhylton/cpython that referenced this issue Aug 19, 2024
…uild (python#123071)

GCC 11 and newer warn about the access to `unique_id` in non-debug builds
due to inlining the call on static non-heap types.
blhsing pushed a commit to blhsing/cpython that referenced this issue Aug 22, 2024
…uild (python#123071)

GCC 11 and newer warn about the access to `unique_id` in non-debug builds
due to inlining the call on static non-heap types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants