Skip to content

Commit

Permalink
Add offsets for bytes objects
Browse files Browse the repository at this point in the history
This continues laying the groundwork for supporting a free-threading
interpreter.

Signed-off-by: Matt Wozniski <[email protected]>
  • Loading branch information
godlygeek authored and pablogsal committed Aug 13, 2024
1 parent 4b4e0bd commit d6b1102
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/pystack/_pystack/cpython/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ typedef uint8_t Py_UCS1;
typedef Py_UCS4 Py_UNICODE;
typedef Py_ssize_t Py_hash_t;

namespace Python3 {
typedef struct
{
PyObject_VAR_HEAD Py_hash_t ob_shash;
char ob_sval[1];
} PyBytesObject;
} // namespace Python3

namespace Python2 {
typedef struct
Expand Down Expand Up @@ -107,6 +109,10 @@ typedef struct

} // namespace Python3_12

typedef union {
Python3::PyBytesObject v3;
} PyBytesObject;

typedef union {
Python2::PyUnicodeObject v2;
Python3::PyUnicodeObject v3;
Expand Down
11 changes: 6 additions & 5 deletions src/pystack/_pystack/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,14 @@ AbstractProcessManager::getBytesFromAddress(remote_addr_t addr) const
<< addr;
PyBytesObject bytes;

copyObjectFromProcess(addr, &bytes);

if ((len = bytes.ob_base.ob_size + 1) < 1) {
throw std::runtime_error("Incorrect size of the fetches bytes object");
copyMemoryFromProcess(addr, offsets().py_bytes.size, &bytes);
len = getField(bytes, &py_bytes_v::o_ob_size) + 1;
if (len < 1) {
throw std::runtime_error("Incorrect size of the fetched bytes object");
}
buffer.resize(len);
data_addr = (remote_addr_t)((char*)addr + offsetof(PyBytesObject, ob_sval));
data_addr = addr + getFieldOffset(&py_bytes_v::o_ob_sval);

LOG(DEBUG) << std::hex << std::showbase << "Copying data for bytes object from address "
<< data_addr;
copyMemoryFromProcess(data_addr, len, buffer.data());
Expand Down
22 changes: 22 additions & 0 deletions src/pystack/_pystack/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ py_object()
};
}

template<class T>
constexpr py_bytes_v
py_bytes()
{
return {
sizeof(T),
offsetof(T, ob_base.ob_size),
offsetof(T, ob_sval),
};
}

template<class T>
constexpr py_unicode_v
py_unicode()
Expand Down Expand Up @@ -410,6 +421,7 @@ python_v python_v2 = {
py_float<PyFloatObject>(),
py_long<_PyLongObject>(),
{},
{},
py_object<PyObject>(),
py_type<Python2::PyTypeObject>(),
py_code<Python2::PyCodeObject>(),
Expand All @@ -428,6 +440,7 @@ python_v python_v3_3 = {
py_dictvalues<Python3::PyDictValuesObject>(),
py_float<PyFloatObject>(),
py_long<_PyLongObject>(),
py_bytes<Python3::PyBytesObject>(),
py_unicode<Python3::PyUnicodeObject>(),
py_object<PyObject>(),
py_type<Python3_3::PyTypeObject>(),
Expand All @@ -447,6 +460,7 @@ python_v python_v3_4 = {
py_dictvalues<Python3::PyDictValuesObject>(),
py_float<PyFloatObject>(),
py_long<_PyLongObject>(),
py_bytes<Python3::PyBytesObject>(),
py_unicode<Python3::PyUnicodeObject>(),
py_object<PyObject>(),
py_type<Python3_3::PyTypeObject>(),
Expand All @@ -466,6 +480,7 @@ python_v python_v3_6 = {
py_dictvalues<Python3::PyDictValuesObject>(),
py_float<PyFloatObject>(),
py_long<_PyLongObject>(),
py_bytes<Python3::PyBytesObject>(),
py_unicode<Python3::PyUnicodeObject>(),
py_object<PyObject>(),
py_type<Python3_3::PyTypeObject>(),
Expand All @@ -485,6 +500,7 @@ python_v python_v3_7 = {
py_dictvalues<Python3::PyDictValuesObject>(),
py_float<PyFloatObject>(),
py_long<_PyLongObject>(),
py_bytes<Python3::PyBytesObject>(),
py_unicode<Python3::PyUnicodeObject>(),
py_object<PyObject>(),
py_type<Python3_3::PyTypeObject>(),
Expand All @@ -506,6 +522,7 @@ python_v python_v3_8 = {
py_dictvalues<Python3::PyDictValuesObject>(),
py_float<PyFloatObject>(),
py_long<_PyLongObject>(),
py_bytes<Python3::PyBytesObject>(),
py_unicode<Python3::PyUnicodeObject>(),
py_object<PyObject>(),
py_type<Python3_8::PyTypeObject>(),
Expand All @@ -527,6 +544,7 @@ python_v python_v3_9 = {
py_dictvalues<Python3::PyDictValuesObject>(),
py_float<PyFloatObject>(),
py_long<_PyLongObject>(),
py_bytes<Python3::PyBytesObject>(),
py_unicode<Python3::PyUnicodeObject>(),
py_object<PyObject>(),
py_type<Python3_8::PyTypeObject>(),
Expand All @@ -548,6 +566,7 @@ python_v python_v3_10 = {
py_dictvalues<Python3::PyDictValuesObject>(),
py_float<PyFloatObject>(),
py_long<_PyLongObject>(),
py_bytes<Python3::PyBytesObject>(),
py_unicode<Python3::PyUnicodeObject>(),
py_object<PyObject>(),
py_type<Python3_8::PyTypeObject>(),
Expand All @@ -569,6 +588,7 @@ python_v python_v3_11 = {
py_dictvalues<Python3::PyDictValuesObject>(),
py_float<PyFloatObject>(),
py_long<_PyLongObject>(),
py_bytes<Python3::PyBytesObject>(),
py_unicode<Python3::PyUnicodeObject>(),
py_object<PyObject>(),
py_type<Python3_8::PyTypeObject>(),
Expand All @@ -591,6 +611,7 @@ python_v python_v3_12 = {
py_dictvalues<Python3::PyDictValuesObject>(),
py_float<PyFloatObject>(),
py_long<_PyLongObject>(),
py_bytes<Python3::PyBytesObject>(),
py_unicode<Python3_12::PyUnicodeObject>(),
py_object<PyObject>(),
py_type<Python3_8::PyTypeObject>(),
Expand All @@ -613,6 +634,7 @@ python_v python_v3_13 = {
py_dictvalues<Python3_13::PyDictValuesObject>(),
py_float<PyFloatObject>(),
py_long<_PyLongObject>(),
py_bytes<Python3::PyBytesObject>(),
py_unicode<Python3_12::PyUnicodeObject>(),
py_object<PyObject>(),
py_type<Python3_8::PyTypeObject>(),
Expand Down
10 changes: 10 additions & 0 deletions src/pystack/_pystack/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ struct py_long_v
FieldOffset<digit[1]> o_ob_digit;
};

struct py_bytes_v
{
typedef PyBytesObject Structure;
ssize_t size;
FieldOffset<Py_ssize_t> o_ob_size;
FieldOffset<char[1]> o_ob_sval;
};

struct py_unicode_v
{
typedef PyUnicodeObject Structure;
Expand Down Expand Up @@ -244,6 +252,7 @@ struct python_v
py_dictvalues_v py_dictvalues;
py_float_v py_float;
py_long_v py_long;
py_bytes_v py_bytes;
py_unicode_v py_unicode;
py_object_v py_object;
py_type_v py_type;
Expand Down Expand Up @@ -273,6 +282,7 @@ define_python_v_get_specialization(py_dictkeys);
define_python_v_get_specialization(py_dictvalues);
define_python_v_get_specialization(py_float);
define_python_v_get_specialization(py_long);
define_python_v_get_specialization(py_bytes);
define_python_v_get_specialization(py_unicode);
define_python_v_get_specialization(py_object);
define_python_v_get_specialization(py_type);
Expand Down

0 comments on commit d6b1102

Please sign in to comment.