| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include "drgnpy.h" |
| 5 |
#include "../cleanup.h" |
| 6 |
#include "../util.h" |
| 7 |
|
| 8 |
PyObject *ModuleSectionAddresses_class; |
| 9 |
|
| 10 |
static ModuleSectionAddresses *ModuleSectionAddresses_new(PyTypeObject *subtype, |
| 11 |
PyObject *args, |
| 12 |
PyObject *kwds) |
| 13 |
{ |
| 14 |
static char *keywords[] = {"module", NULL}; |
| 15 |
Module *module; |
| 16 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 17 |
"O!:_ModuleSectionAddresses", keywords, |
| 18 |
&Module_type, &module)) |
| 19 |
return NULL; |
| 20 |
ModuleSectionAddresses *ret = |
| 21 |
(ModuleSectionAddresses *)subtype->tp_alloc(subtype, 0); |
| 22 |
if (ret) { |
| 23 |
Py_INCREF(Module_prog(module)); |
| 24 |
ret->module = module->module; |
| 25 |
} |
| 26 |
return ret; |
| 27 |
} |
| 28 |
|
| 29 |
static void ModuleSectionAddresses_dealloc(ModuleSectionAddresses *self) |
| 30 |
{ |
| 31 |
PyObject_GC_UnTrack(self); |
| 32 |
if (self->module) { |
| 33 |
struct drgn_program *prog = drgn_module_program(self->module); |
| 34 |
Py_DECREF(container_of(prog, Program, prog)); |
| 35 |
} |
| 36 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 37 |
} |
| 38 |
|
| 39 |
static int ModuleSectionAddresses_traverse(ModuleSectionAddresses *self, |
| 40 |
visitproc visit, void *arg) |
| 41 |
{ |
| 42 |
if (self->module) { |
| 43 |
struct drgn_program *prog = drgn_module_program(self->module); |
| 44 |
Py_VISIT(container_of(prog, Program, prog)); |
| 45 |
} |
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
static inline void |
| 50 |
drgn_module_section_address_iterator_destroyp(struct drgn_module_section_address_iterator **itp) |
| 51 |
{ |
| 52 |
drgn_module_section_address_iterator_destroy(*itp); |
| 53 |
} |
| 54 |
|
| 55 |
static PyObject *ModuleSectionAddresses_repr(ModuleSectionAddresses *self) |
| 56 |
{ |
| 57 |
struct drgn_error *err; |
| 58 |
|
| 59 |
_cleanup_(drgn_module_section_address_iterator_destroyp) |
| 60 |
struct drgn_module_section_address_iterator *it = NULL; |
| 61 |
err = drgn_module_section_address_iterator_create(self->module, &it); |
| 62 |
if (err) |
| 63 |
return set_drgn_error(err); |
| 64 |
|
| 65 |
_cleanup_pydecref_ PyObject *parts = PyList_New(0); |
| 66 |
if (!parts) |
| 67 |
return NULL; |
| 68 |
if (append_string(parts, "ModuleSectionAddresses(")) |
| 69 |
return NULL; |
| 70 |
bool first = true; |
| 71 |
for (;;) { |
| 72 |
const char *name; |
| 73 |
uint64_t address; |
| 74 |
err = drgn_module_section_address_iterator_next(it, &name, |
| 75 |
&address); |
| 76 |
if (err) |
| 77 |
return set_drgn_error(err); |
| 78 |
if (!name) |
| 79 |
break; |
| 80 |
|
| 81 |
_cleanup_pydecref_ PyObject *name_obj = |
| 82 |
PyUnicode_FromString(name); |
| 83 |
if (!name_obj) |
| 84 |
return NULL; |
| 85 |
if (append_format(parts, "%s%R: ", first ? "{" : ", ", name_obj) |
| 86 |
|| append_u64_hex(parts, address)) |
| 87 |
return NULL; |
| 88 |
first = false; |
| 89 |
} |
| 90 |
if (append_string(parts, first ? ")" : "})")) |
| 91 |
return NULL; |
| 92 |
return join_strings(parts); |
| 93 |
} |
| 94 |
|
| 95 |
static Py_ssize_t ModuleSectionAddresses_length(ModuleSectionAddresses *self) |
| 96 |
{ |
| 97 |
size_t ret; |
| 98 |
struct drgn_error *err = |
| 99 |
drgn_module_num_section_addresses(self->module, &ret); |
| 100 |
if (err) { |
| 101 |
set_drgn_error(err); |
| 102 |
return -1; |
| 103 |
} |
| 104 |
return ret; |
| 105 |
} |
| 106 |
|
| 107 |
static PyObject *ModuleSectionAddresses_subscript(ModuleSectionAddresses *self, |
| 108 |
PyObject *key) |
| 109 |
{ |
| 110 |
if (!PyUnicode_Check(key)) { |
| 111 |
PyErr_SetObject(PyExc_KeyError, key); |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
const char *name = PyUnicode_AsUTF8(key); |
| 115 |
if (!name) |
| 116 |
return NULL; |
| 117 |
uint64_t address; |
| 118 |
struct drgn_error *err = drgn_module_get_section_address(self->module, |
| 119 |
name, |
| 120 |
&address); |
| 121 |
if (err && err->code == DRGN_ERROR_LOOKUP) { |
| 122 |
drgn_error_destroy(err); |
| 123 |
PyErr_SetObject(PyExc_KeyError, key); |
| 124 |
return NULL; |
| 125 |
} else if (err) { |
| 126 |
return set_drgn_error(err); |
| 127 |
} |
| 128 |
return PyLong_FromUInt64(address); |
| 129 |
} |
| 130 |
|
| 131 |
static int ModuleSectionAddresses_ass_subscript(ModuleSectionAddresses *self, |
| 132 |
PyObject *key, |
| 133 |
PyObject *value) |
| 134 |
{ |
| 135 |
struct drgn_error *err; |
| 136 |
if (value) { |
| 137 |
if (!PyUnicode_Check(key)) { |
| 138 |
PyErr_SetString(PyExc_TypeError, |
| 139 |
"section_addresses key must be str"); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
const char *name = PyUnicode_AsUTF8(key); |
| 143 |
if (!name) |
| 144 |
return -1; |
| 145 |
uint64_t address; |
| 146 |
if (PyLong_AsUInt64(value, &address)) |
| 147 |
return -1; |
| 148 |
err = drgn_module_set_section_address(self->module, name, |
| 149 |
address); |
| 150 |
} else { |
| 151 |
if (!PyUnicode_Check(key)) { |
| 152 |
PyErr_SetObject(PyExc_KeyError, key); |
| 153 |
return -1; |
| 154 |
} |
| 155 |
const char *name = PyUnicode_AsUTF8(key); |
| 156 |
if (!name) |
| 157 |
return -1; |
| 158 |
err = drgn_module_delete_section_address(self->module, name); |
| 159 |
if (err && err->code == DRGN_ERROR_LOOKUP) { |
| 160 |
drgn_error_destroy(err); |
| 161 |
PyErr_SetObject(PyExc_KeyError, key); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
} |
| 165 |
if (err) { |
| 166 |
set_drgn_error(err); |
| 167 |
return -1; |
| 168 |
} |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
static ModuleSectionAddressesIterator * |
| 173 |
ModuleSectionAddresses_iter(ModuleSectionAddresses *self) |
| 174 |
{ |
| 175 |
struct drgn_error *err; |
| 176 |
_cleanup_pydecref_ ModuleSectionAddressesIterator *it = |
| 177 |
call_tp_alloc(ModuleSectionAddressesIterator); |
| 178 |
if (!it) |
| 179 |
return NULL; |
| 180 |
err = drgn_module_section_address_iterator_create(self->module, |
| 181 |
&it->it); |
| 182 |
if (err) |
| 183 |
return set_drgn_error(err); |
| 184 |
struct drgn_program *prog = drgn_module_program(self->module); |
| 185 |
Py_INCREF(container_of(prog, Program, prog)); |
| 186 |
return_ptr(it); |
| 187 |
} |
| 188 |
|
| 189 |
// We only define the bare minimum for collections.abc.MutableMapping, |
| 190 |
// which gives us naive implementations of the remaining methods. We can |
| 191 |
// define performance-sensitive ones as needed. |
| 192 |
static PyMappingMethods ModuleSectionAddressesMixin_as_mapping = { |
| 193 |
.mp_length = (lenfunc)ModuleSectionAddresses_length, |
| 194 |
.mp_subscript = (binaryfunc)ModuleSectionAddresses_subscript, |
| 195 |
.mp_ass_subscript = (objobjargproc)ModuleSectionAddresses_ass_subscript, |
| 196 |
}; |
| 197 |
|
| 198 |
static PyTypeObject ModuleSectionAddressesMixin_type = { |
| 199 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 200 |
.tp_name = "_drgn.ModuleSectionAddressesMixin", |
| 201 |
.tp_dealloc = (destructor)ModuleSectionAddresses_dealloc, |
| 202 |
.tp_basicsize = sizeof(ModuleSectionAddresses), |
| 203 |
.tp_repr = (reprfunc)ModuleSectionAddresses_repr, |
| 204 |
.tp_as_mapping = &ModuleSectionAddressesMixin_as_mapping, |
| 205 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, |
| 206 |
.tp_traverse = (traverseproc)ModuleSectionAddresses_traverse, |
| 207 |
.tp_iter = (getiterfunc)ModuleSectionAddresses_iter, |
| 208 |
.tp_new = (newfunc)ModuleSectionAddresses_new, |
| 209 |
}; |
| 210 |
|
| 211 |
static void |
| 212 |
ModuleSectionAddressesIterator_dealloc(ModuleSectionAddressesIterator *self) |
| 213 |
{ |
| 214 |
PyObject_GC_UnTrack(self); |
| 215 |
if (self->it) { |
| 216 |
struct drgn_module *module = |
| 217 |
drgn_module_section_address_iterator_module(self->it); |
| 218 |
struct drgn_program *prog = drgn_module_program(module); |
| 219 |
Py_DECREF(container_of(prog, Program, prog)); |
| 220 |
drgn_module_section_address_iterator_destroy(self->it); |
| 221 |
} |
| 222 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 223 |
} |
| 224 |
|
| 225 |
static int |
| 226 |
ModuleSectionAddressesIterator_traverse(ModuleSectionAddressesIterator *self, |
| 227 |
visitproc visit, void *arg) |
| 228 |
{ |
| 229 |
if (self->it) { |
| 230 |
struct drgn_module *module = |
| 231 |
drgn_module_section_address_iterator_module(self->it); |
| 232 |
struct drgn_program *prog = drgn_module_program(module); |
| 233 |
Py_VISIT(container_of(prog, Program, prog)); |
| 234 |
} |
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
static PyObject * |
| 239 |
ModuleSectionAddressesIterator_next(ModuleSectionAddressesIterator *self) |
| 240 |
{ |
| 241 |
struct drgn_error *err; |
| 242 |
const char *name; |
| 243 |
err = drgn_module_section_address_iterator_next(self->it, &name, NULL); |
| 244 |
if (err) |
| 245 |
return set_drgn_error(err); |
| 246 |
if (!name) |
| 247 |
return NULL; |
| 248 |
return PyUnicode_FromString(name); |
| 249 |
} |
| 250 |
|
| 251 |
PyTypeObject ModuleSectionAddressesIterator_type = { |
| 252 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 253 |
.tp_name = "_drgn._ModuleSectionAddressesIterator", |
| 254 |
.tp_basicsize = sizeof(ModuleSectionAddressesIterator), |
| 255 |
.tp_dealloc = (destructor)ModuleSectionAddressesIterator_dealloc, |
| 256 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 257 |
.tp_traverse = (traverseproc)ModuleSectionAddressesIterator_traverse, |
| 258 |
.tp_iter = PyObject_SelfIter, |
| 259 |
.tp_iternext = (iternextfunc)ModuleSectionAddressesIterator_next, |
| 260 |
}; |
| 261 |
|
| 262 |
int init_module_section_addresses(void) |
| 263 |
{ |
| 264 |
if (PyType_Ready(&ModuleSectionAddressesMixin_type)) |
| 265 |
return -1; |
| 266 |
_cleanup_pydecref_ PyObject *collections_abc = |
| 267 |
PyImport_ImportModule("collections.abc"); |
| 268 |
if (!collections_abc) |
| 269 |
return -1; |
| 270 |
_cleanup_pydecref_ PyObject *MutableMapping = |
| 271 |
PyObject_GetAttrString(collections_abc, "MutableMapping"); |
| 272 |
if (!MutableMapping) |
| 273 |
return -1; |
| 274 |
// We can't create a direct subclass of MutableMapping from C (see |
| 275 |
// https://github.com/python/cpython/issues/103968). Use this multiple |
| 276 |
// inheritance trick taken from cpython/Modules/_decimal/_decimal.c |
| 277 |
// instead. |
| 278 |
ModuleSectionAddresses_class = |
| 279 |
PyObject_CallFunction((PyObject *)&PyType_Type, "s(OO){}", |
| 280 |
"ModuleSectionAddresses", |
| 281 |
&ModuleSectionAddressesMixin_type, |
| 282 |
MutableMapping); |
| 283 |
if (!ModuleSectionAddresses_class) |
| 284 |
return -1; |
| 285 |
return 0; |
| 286 |
} |