Skip to content

Possible cause of segfault in subclassing #680

@kngwyu

Description

@kngwyu

Related to #220, #407
cc: @konstin @joar @davidhewitt

So as I said now I'm investigating safer layout than our current pyclass, which is constructed (roughly) by

let ptr = alloc(...);  // *mut PyObject
let align = std::mem::align_of::<Self>();  // Self = pyclass
// Suppose 
// struct PyClassLayout { |\
//       base: Self::BASE,      | offset
//       self_: PyClass   |/
// }
let offset = (self::BASE::SIZE + align - 1) / align * align;  
let self_ptr = (ptr as *mut u8).offset(Self::OFFSET) as *mut T;

And what I found problematic is <Py~ as PyTypeInfo>::SIZE is std::mem::size_of::<ffi::PyObject> for all Py~ types, which is not true.
Actually, PyDictObject is defined as (roughly)

typedef struct {
    PyObject ob_base;
    Py_ssize_t ma_used;
    uint64_t ma_version_tag;
    PyDictKeysObject *ma_keys;
    PyObject **ma_values;
} PyDictObject;

So it's size 64bit * 4 = 32bytes larger than PyObject.
Is it the cause of segfault?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions