-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecorder.cpp
More file actions
296 lines (260 loc) · 10.5 KB
/
execorder.cpp
File metadata and controls
296 lines (260 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// DEBUG stuff ==================================================
#define PRNT(obj) PyObject_Print(obj, stdout, 0);
#define PRNTn(obj) PRNT(obj); printf("\n");
//===============================================================
#include "Python.h"
#include "ceval.h"
#include "frameobject.h"
#include "opcode.h"
#include "recording.h"
#include <atomic>
#define TOP() (frame->f_stacktop[-1])
#define SECOND() (frame->f_stacktop[-2])
#define THIRD() (frame->f_stacktop[-3])
#define NAME() (PyTuple_GET_ITEM(((PyTupleObject*)frame->f_code->co_names), (oparg)))
Py_ssize_t recording_i, my_code_i;
std::atomic<int> running_execs = 0;
bool get_recording(PyFrameObject* frame, RecordingObject** recording){
PyObject* in_my_code = NULL;
_PyCode_GetExtra((PyObject*)frame->f_code, recording_i, (void**)recording);
_PyCode_GetExtra((PyObject*)frame->f_code, my_code_i, (void**)&in_my_code);
return in_my_code != NULL;
}
void mutation(PyFrameObject* frame, int opcode, int i, PyObject* a, PyObject* b, PyObject* c){
// A mutation occurred, see whether we need to record it...
RecordingObject* recording = NULL;
bool in_my_code = get_recording(frame, &recording);
auto f = (PyObject*)frame;
if(in_my_code){
PyObject *name = NULL;
switch(opcode){
// Name bind
case STORE_NAME: // a[b] = c (b = c in namespace a)
case DELETE_NAME:
name = b;
break;
case STORE_GLOBAL: // a = c
case DELETE_GLOBAL:
name = a;
break;
case STORE_FAST: // FASTS[i] = c
case DELETE_FAST:
name = PyTuple_GetItem(frame->f_code->co_varnames, i);
break;
case STORE_DEREF:
case DELETE_DEREF:
// TODO: logic about i >? co_varnames etc.
break;
}
if(name != NULL){
Recording_record(recording, opcode, f, name, c);
}
}
if(recording != NULL && Recording_object_tracked(recording, a)){
// We aren't in my code, but we are tracking this object's state
// e.g. my code called random.shuffle(X) and we are in shuffle's implementation
switch(opcode){
case STORE_SUBSCR: // a[b] = c
case DELETE_SUBSCR: // del a[b]
case STORE_ATTR: // a.b = c
case DELETE_ATTR: // del a.b
case INPLACE_POWER: // a **= b
case INPLACE_MULTIPLY: // a *= b
case INPLACE_MATRIX_MULTIPLY: // a @= b
case INPLACE_TRUE_DIVIDE: // a /= b
case INPLACE_FLOOR_DIVIDE: // a //= b
case INPLACE_MODULO: // a %= b
case INPLACE_ADD: // a += b
case INPLACE_SUBTRACT: // a -= b
case INPLACE_LSHIFT: // a <<= b
case INPLACE_RSHIFT: // a >>= b
case INPLACE_AND: // a &= b
case INPLACE_XOR: // a ^= b
case INPLACE_OR: // a |= b
Recording_record(recording, opcode, a, b, c);
break;
}
}
}
int trace_opcode(PyFrameObject* frame){
// Check whether the next opcode can potentially mutate state...
auto instructions = PyBytes_AS_STRING(frame->f_code->co_code);
auto opcode = instructions[frame->f_lasti];
auto oparg = instructions[frame->f_lasti + 1];
switch(opcode){
case STORE_FAST:
mutation(frame, STORE_FAST, oparg, NULL, NULL, TOP());
break;
case DELETE_FAST:
mutation(frame, DELETE_FAST, oparg, NULL, NULL, NULL);
break;
case STORE_SUBSCR:
mutation(frame, STORE_SUBSCR, 0, SECOND(), TOP(), THIRD());
break;
case DELETE_SUBSCR:
mutation(frame, DELETE_SUBSCR, 0, SECOND(), TOP(), NULL);
break;
case STORE_NAME:
mutation(frame, STORE_NAME, 0, frame->f_locals, NAME(), TOP());
break;
case DELETE_NAME:
mutation(frame, STORE_NAME, 0, frame->f_locals, NAME(), NULL);
break;
case STORE_ATTR:
mutation(frame, STORE_ATTR, 0, TOP(), NAME(), SECOND());
break;
case DELETE_ATTR:
mutation(frame, DELETE_ATTR, 0, TOP(), NAME(), NULL);
break;
case STORE_GLOBAL:
mutation(frame, STORE_GLOBAL, 0, NAME(), NULL, TOP());
break;
case DELETE_GLOBAL:
mutation(frame, DELETE_GLOBAL, 0, NAME(), NULL, NULL);
break;
case INPLACE_ADD: // TODO: deal with unicode
case INPLACE_POWER:
case INPLACE_MULTIPLY:
case INPLACE_MATRIX_MULTIPLY:
case INPLACE_TRUE_DIVIDE:
case INPLACE_FLOOR_DIVIDE:
case INPLACE_MODULO:
case INPLACE_SUBTRACT:
case INPLACE_LSHIFT:
case INPLACE_RSHIFT:
case INPLACE_AND:
case INPLACE_XOR:
case INPLACE_OR:
mutation(frame, opcode, 0, SECOND(), TOP(), NULL);
break;
}
return 0;
}
int trace_step(PyFrameObject *frame, RecordingObject* recording, int what){
if(recording->record_state){
if(recording->global_frame == NULL){
recording->global_frame = (PyObject*)frame; // First trace step, save the frame
}
// Deal with 'hidden' name bindings when entering new frame
if(recording->fresh_milestone || what == PyTrace_CALL){
PyFrame_FastToLocals(frame); // TODO: this is slow, refactor it out?
std::vector<PyObject*> dicts; int opcode;
if(recording->fresh_milestone){
dicts = {frame->f_globals, frame->f_locals};
opcode = STORE_GLOBAL;
recording->fresh_milestone = false;
} else {
dicts = {frame->f_locals};
opcode = STORE_NAME;
}
PyObject *key, *value; Py_ssize_t pos = 0;
for(auto& dict : dicts){
while(PyDict_Next(dict, &pos, &key, &value)) {
Recording_record(recording, opcode, (PyObject*)frame, key, value);
opcode = STORE_NAME;
}
}
}
}
int err = Recording_record(recording, what, (PyObject*)frame, NULL, NULL);
return err;
}
int trace(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg){
int err = 0;
if(what == PyTrace_OPCODE){
err = trace_opcode(frame);
} else {
RecordingObject* recording = NULL;
bool in_my_code = get_recording(frame, &recording);
if(recording == NULL && what == PyTrace_CALL && frame->f_back != NULL){
// Newly called frame - copy possible Recording from parent
_PyCode_GetExtra((PyObject*)frame->f_back->f_code, recording_i, (void**)&recording);
_PyCode_SetExtra((PyObject*)frame->f_code, recording_i, (void*)recording);
}
if(recording != NULL){
if(recording->record_state){
frame->f_trace_opcodes = 1;
}
if(in_my_code){
err = trace_step(frame, recording, what);
}
}
}
return err;
}
void mark_code_with_recording(PyObject* code, RecordingObject* recording){
_PyCode_SetExtra(code, my_code_i, (void*)true);
_PyCode_SetExtra(code, recording_i, (void*)recording);
auto co_consts = ((PyCodeObject*)code)->co_consts;
auto n = PyTuple_Size(co_consts);
for(Py_ssize_t i = 0; i < n; i++){
auto co_const = PyTuple_GetItem(co_consts, i);
if(PyCode_Check(co_const)){
// Mark any sub-code objects (e.g. functions)
mark_code_with_recording(co_const, recording);
}
}
}
static PyObject* exec(PyObject *self, PyObject *args, PyObject *kwargs){
PyObject *code_str, *globals, *callback = NULL;
long max_steps = 0, record_state = 1;
char *keywords[] = {"", "", "callback", "max_steps", "record_state", NULL};
if(PyArg_ParseTupleAndKeywords(args, kwargs, "O|O$Olp:exec", keywords, &code_str, &globals,
&callback, &max_steps, &record_state)){
auto code_utf8 = PyUnicode_AsUTF8(code_str);
auto code = Py_CompileStringExFlags(code_utf8, "<execorder>", Py_file_input, NULL, -1);
if(PyErr_Occurred()){
return NULL; // Compile failure
}
auto recording = Recording_New(code);
recording->record_state = (bool)record_state;
recording->callback = callback;
recording->max_steps = max_steps;
mark_code_with_recording(code, recording); // Attached recoding to code object
globals = PyDict_New();
auto builtins = PyDict_Copy(PyEval_GetBuiltins());
PyDict_SetItemString(builtins, "__cffi_backend_extern_py", Py_None); // Clear gevent nonsense
PyDict_SetItemString(globals, "__builtins__", builtins);
Py_DECREF(builtins);
running_execs++;
PyEval_SetTrace((Py_tracefunc)trace, NULL); // Turn on tracing
Recording_make_callback(recording); // Starting callback
PyEval_EvalCode(code, globals, NULL); // Run the code
running_execs--;
if(running_execs == 0){
// No other threads are running - safe to turn off tracing
PyEval_SetTrace(NULL, NULL);
}
Py_DECREF(globals);
if(PyErr_Occurred()){
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
Recording_make_callback(recording);
PyErr_Restore(type, value, traceback);
recording = NULL;
}
return (PyObject*)recording;
}
return NULL;
}
static PyMethodDef
methods[] = {
{"exec", (PyCFunction)exec, METH_VARARGS | METH_KEYWORDS, "Execute code object"},
{NULL}
};
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"execorder", NULL, -1, methods,
NULL, NULL, NULL, NULL,
};
extern "C"
PyObject* PyInit_execorder(void) {
auto module = PyModule_Create(&moduledef);
my_code_i = _PyEval_RequestCodeExtraIndex(NULL);
recording_i = _PyEval_RequestCodeExtraIndex(NULL);
auto recording_type = Recording_Type();
PyType_Ready(recording_type);
Py_INCREF(recording_type);
PyModule_AddObject(module, "Recording", (PyObject*)recording_type);
return module;
}