Debian Codegraph @

libdrgn/python/error.c

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include "drgnpy.h"
5 #include "../error.h"
6
7 static int FaultError_init(PyObject *self, PyObject *args, PyObject *kwds)
8 {
9 static char *keywords[] = {"message", "address", NULL};
10 PyObject *address, *message;
11
12 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:FaultError", keywords,
13 &message, &address))
14 return -1;
15
16 if (PyObject_SetAttrString(self, "message", message) < 0 ||
17 PyObject_SetAttrString(self, "address", address) < 0)
18 return -1;
19 return 0;
20 }
21
22 static PyObject *FaultError_str(PyObject *self)
23 {
24 _cleanup_pydecref_ PyObject *message =
25 PyObject_GetAttrString(self, "message");
26 if (!message)
27 return NULL;
28
29 _cleanup_pydecref_ PyObject *address =
30 PyObject_GetAttrString(self, "address");
31 if (!address)
32 return NULL;
33
34 _cleanup_pydecref_ PyObject *args =
35 Py_BuildValue("OO", message, address);
36 if (!args)
37 return NULL;
38
39 _cleanup_pydecref_ PyObject *fmt = PyUnicode_FromString("%s: %#x");
40 if (!fmt)
41 return NULL;
42
43 return PyUnicode_Format(fmt, args);
44 }
45
46 PyTypeObject FaultError_type = {
47 PyVarObject_HEAD_INIT(NULL, 0)
48 .tp_name = "_drgn.FaultError",
49 .tp_basicsize = sizeof(PyBaseExceptionObject),
50 .tp_str = (reprfunc)FaultError_str,
51 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
52 .tp_doc = drgn_FaultError_DOC,
53 .tp_init = (initproc)FaultError_init,
54 };
55
56 static int ObjectNotFoundError_init(PyObject *self, PyObject *args,
57 PyObject *kwds)
58 {
59 if (((PyTypeObject *)PyExc_BaseException)->tp_init(self, args, NULL) < 0)
60 return -1;
61
62 static char *keywords[] = {"name", NULL};
63 _cleanup_pydecref_ PyObject *empty_tuple = PyTuple_New(0);
64 if (!empty_tuple)
65 return -1;
66 PyObject *name;
67 if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds,
68 "|$O:ObjectNotFoundError", keywords,
69 &name))
70 return -1;
71
72 return PyObject_SetAttrString(self, "name", name);
73 }
74
75 PyTypeObject ObjectNotFoundError_type = {
76 PyVarObject_HEAD_INIT(NULL, 0)
77 .tp_name = "_drgn.ObjectNotFoundError",
78 .tp_basicsize = sizeof(PyBaseExceptionObject),
79 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
80 .tp_doc = drgn_ObjectNotFoundError_DOC,
81 .tp_init = (initproc)ObjectNotFoundError_init,
82 };
83
84 static struct drgn_error drgn_error_python = {
85 .code = DRGN_ERROR_OTHER,
86 .message = "error in Python callback",
87 };
88
89 static _Thread_local bool drgn_in_python = false;
90
91 bool set_drgn_in_python(void)
92 {
93 if (drgn_in_python)
94 return false;
95 drgn_in_python = true;
96 return true;
97 }
98
99 void clear_drgn_in_python(void)
100 {
101 drgn_in_python = false;
102 }
103
104 static struct drgn_error *drgn_fault_error_from_python(PyObject *exc_value)
105 {
106 _cleanup_pydecref_ PyObject *py_message =
107 PyObject_GetAttrString(exc_value, "message");
108 const char *message = py_message ? PyUnicode_AsUTF8(py_message) : NULL;
109 if (!message)
110 return NULL;
111
112 _cleanup_pydecref_ PyObject *py_address =
113 PyObject_GetAttrString(exc_value, "address");
114 uint64_t address;
115 if (!py_address || PyLong_AsUInt64(py_address, &address))
116 return NULL;
117
118 return drgn_error_create_fault(message, address);
119 }
120
121 struct drgn_error *drgn_error_from_python(void)
122 {
123 _cleanup_pydecref_ PyObject *exc_type, *exc_value, *exc_traceback;
124 PyErr_Fetch(&exc_type, &exc_value, &exc_traceback);
125 if (!exc_type)
126 return NULL;
127
128 // Python FaultErrors should be translated back to drgn errors because
129 // they are frequently handled in libdrgn. They should be translated no
130 // matter how deeply nested we are, so we do this before checking
131 // drgn_in_python.
132 if ((PyTypeObject *)exc_type == &FaultError_type && exc_value) {
133 struct drgn_error *err = drgn_fault_error_from_python(exc_value);
134 if (err)
135 return err;
136 // A NULL return means that we encountered a Python error while
137 // trying to convert it. Clear the Python error and fall back to
138 // the standard code path.
139 PyErr_Clear();
140 }
141
142 if (drgn_in_python) {
143 PyErr_Restore(exc_type, exc_value, exc_traceback);
144 exc_type = exc_value = exc_traceback = NULL;
145 return &drgn_error_python;
146 }
147
148 const char *type = ((PyTypeObject *)exc_type)->tp_name;
149 _cleanup_pydecref_ PyObject *exc_message = NULL;
150 const char *message;
151 if (exc_value) {
152 exc_message = PyObject_Str(exc_value);
153 message = exc_message ? PyUnicode_AsUTF8(exc_message) : NULL;
154 if (!message) {
155 PyErr_Clear();
156 return drgn_error_format(DRGN_ERROR_OTHER,
157 "%s: <exception str() failed>", type);
158 }
159 } else {
160 message = "";
161 }
162
163 if (message[0]) {
164 return drgn_error_format(DRGN_ERROR_OTHER, "%s: %s", type,
165 message);
166 } else {
167 return drgn_error_create(DRGN_ERROR_OTHER, type);
168 }
169 }
170
171 void *set_drgn_error(struct drgn_error *err)
172 {
173 if (err == &drgn_error_python)
174 return NULL;
175
176 switch (err->code) {
177 case DRGN_ERROR_NO_MEMORY:
178 PyErr_NoMemory();
179 break;
180 case DRGN_ERROR_INVALID_ARGUMENT:
181 PyErr_SetString(PyExc_ValueError, err->message);
182 break;
183 case DRGN_ERROR_OVERFLOW:
184 PyErr_SetString(PyExc_OverflowError, err->message);
185 break;
186 case DRGN_ERROR_RECURSION:
187 PyErr_SetString(PyExc_RecursionError, err->message);
188 break;
189 case DRGN_ERROR_OS:
190 errno = err->errnum;
191 PyErr_SetFromErrnoWithFilename(PyExc_OSError, err->path);
192 break;
193 case DRGN_ERROR_MISSING_DEBUG_INFO:
194 PyErr_SetString(MissingDebugInfoError, err->message);
195 break;
196 case DRGN_ERROR_SYNTAX:
197 PyErr_SetString(PyExc_SyntaxError, err->message);
198 break;
199 case DRGN_ERROR_LOOKUP:
200 PyErr_SetString(PyExc_LookupError, err->message);
201 break;
202 case DRGN_ERROR_FAULT: {
203 _cleanup_pydecref_ PyObject *exc =
204 PyObject_CallFunction((PyObject *)&FaultError_type,
205 "sK", err->message,
206 (unsigned long long)err->address);
207 if (exc)
208 PyErr_SetObject((PyObject *)&FaultError_type, exc);
209 break;
210 }
211 case DRGN_ERROR_TYPE:
212 PyErr_SetString(PyExc_TypeError, err->message);
213 break;
214 case DRGN_ERROR_ZERO_DIVISION:
215 PyErr_SetString(PyExc_ZeroDivisionError, err->message);
216 break;
217 case DRGN_ERROR_OUT_OF_BOUNDS:
218 PyErr_SetString(OutOfBoundsError, err->message);
219 break;
220 case DRGN_ERROR_OBJECT_ABSENT:
221 PyErr_SetString(ObjectAbsentError, err->message);
222 break;
223 case DRGN_ERROR_NOT_IMPLEMENTED:
224 PyErr_SetString(PyExc_NotImplementedError, err->message);
225 break;
226 default:
227 PyErr_SetString(PyExc_Exception, err->message);
228 break;
229 }
230
231 drgn_error_destroy(err);
232 return NULL;
233 }
234
235 void *set_error_type_name(const char *format,
236 struct drgn_qualified_type qualified_type)
237 {
238 return set_drgn_error(drgn_qualified_type_error(format,
239 qualified_type));
240 }
241
242 struct drgn_error *
243 drgn_blocking_check_signals(drgn_blocking_state *statep)
244 {
245 if (!*statep)
246 return NULL;
247 PyEval_RestoreThread((PyThreadState *)*statep);
248 int r = PyErr_CheckSignals();
249 *statep = (drgn_blocking_state)PyEval_SaveThread();
250 return r ? &drgn_error_python : NULL;
251 }