-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
457 lines (384 loc) · 13.9 KB
/
Copy pathmain.py
File metadata and controls
457 lines (384 loc) · 13.9 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import dis
import builtins
import sys
import types
import inspect
import operator
class Frame(object):
def __init__(self):
self.data_stack = []
self.block_stack = []
self.locals = dict()
self.noname_locals = dict()
class Function(object): # INCOMPLETE
def __init__(self, qualname, codeobject, vm, defaults):
self.__qualname__ = qualname
self.__name__ = qualname
self.codeobject = codeobject
self.vm = vm
self.func_ = types.FunctionType(self.codeobject, globals(),
self.__qualname__, defaults)
def __repr__(self):
return '<Function %s at 0x{:08d}>'.format(self.__name__, id(self))
def __call__(self, *args, **kwargs):
self.frame = Frame()
prepared_args = inspect.getcallargs(self.func_, *args, **kwargs)
self.frame.locals = dict(prepared_args)
return self.vm.run_function(self)
def clean_up(self):
pass
class Executor(object):
def __init__(self, instruction, frame, instructions, vm):
self.stack = frame.data_stack
self.frame = frame
self.argval = instruction.argval
self.locals_ = frame.locals
self.block_stack = frame.block_stack
self.instructions = instructions
self.vm_ = vm
def NOP(self): # No Instruction mehtods
return
def POP_TOP(self):
self.stack.pop()
def ROT_TWO(self):
self.stack[-2], self.stack[-1] = self.stack[-1], self.stack[-2]
def ROT_THREE(self):
self.stack[-3], self.stack[-2], self.stack[-1] = self.stack[-1],\
self.stack[-3],\
self.stack[-2]
def DUP_TOP(self):
self.stack.append(self.stack[-1])
def DUP_TOP_TWO(self):
self.stack.append(self.stack[-2])
self.stack.append(self.stack[-2])
def STORE_SUBSCR(self): # MAYBE WRONG
a = self.stack[-1]
self.stack.pop()
b = self.stack[-1]
self.stack.pop()
c = self.stack[-1]
self.stack.pop()
b[a] = c
def DELETE_SUBSCR(self): # MAYBE WRONG
a = self.stack[-1]
self.stack.pop()
b = self.stack[-1]
self.stack.pop()
del b[a]
def PRINT_EXPR(self): # MAYBE WRONG
print(self.stack.pop())
def LOAD_BUILD_CLASS(self):
self.stack.append(builtins.__build_class__())
def UNPACK_SEQUENCE(self):
TOS = self.stack[-1]
self.stack.pop()
for x in reversed(TOS):
self.stack.append(x)
def STORE_MAP(self):
map_, val, key = self.stack[-3:]
self.stack[-2:] = []
map_[key] = val
def LOAD_GLOBAL(self):
if self.argval in globals():
self.stack.append(globals()[self.argval])
else:
self.stack.append(getattr(builtins, self.argval))
def LOAD_NAME(self): # INCOMPLETE
if self.argval in self.locals_:
self.stack.append(self.locals_[self.argval])
elif self.argval in globals():
self.stack.append(globals()[self.argval])
elif self.argval in dir(builtins):
self.stack.append(getattr(builtins, self.argval))
else:
raise NameError()
def LOAD_FAST(self):
if self.argval in self.locals_:
self.stack.append(self.locals_[self.argval])
else:
raise NameError()
def STORE_GLOBAL(self):
globals()[self.argval] = self.stack.pop()
def STORE_NAME(self): # MAYBE WRONG
self.locals_[self.argval] = self.stack[-1]
self.stack.pop()
def STORE_FAST(self):
self.locals_[self.argval] = self.stack[-1]
self.stack.pop()
def DELETE_NAME(self):
if instruction.argval in self.locals_:
del locals_[self.argval]
else:
del globals()[self.argval]
def DELETE_FAST(self):
del self.locals_[self.argval]
def DELETE_GLOBAL(self):
del globals()[self.argval]
def CONTINUE_LOOP(self):
return self.instructions[self.argval]
def SET_ADD(self):
a = self.stack[-1]
self.stack.pop()
self.stack[-self.argval].add(a)
def LIST_APPEND(self):
value = self.stack[-1]
self.stack.pop()
key = self.stack[-1]
self.stack.pop()
self.stack[-self.argval][key] = value
def MAP_ADD(self):
a = self.stack[-1]
self.stack[-self.argval] = a
def RETURN_VALUE(self):
a = self.stack[-1]
self.stack.pop()
return (a,)
def POP_BLOCK(self):
self.block_stack.pop()
def STORE_ATTR(self): # need tests
a = self.stack[-1]
self.stack.pop()
b = self.stack[-1]
self.stack.pop()
setattr(a, self.argval, b)
def DELETE_ATTR(self): # need tests
a = self.stack[-1]
self.stack.pop()
delattr(a, self.argval)
def JUMP_FORWARD(self): # MAYBE WRONG NEW!!!!!!!!!!!!!!!!
return self.instructions[self.argval]
def POP_JUMP_IF_TRUE(self):
a = self.stack.pop()
if a:
return instructions[self.argval]
def POP_JUMP_IF_FALSE(self):
a = self.stack.pop()
if not a:
return self.instructions[self.argval]
def JUMP_IF_TRUE_OR_POP(self):
a = self.stack[-1]
if a:
return self.instructions[self.argval]
else:
self.stack.pop()
def JUMP_IF_FALSE_OR_POP(self):
a = self.stack[-1]
if not a:
return self.instructions[self.argval]
else:
self.stack.pop()
def JUMP_ABSOLUTE(self):
return self.instructions[self.argval]
def FOR_ITER(self):
TOS = self.stack[-1]
try:
a = next(TOS)
self.stack.append(a)
except StopIteration:
self.stack.pop()
return self.instructions[self.argval]
def SETUP_LOOP(self):
self.block_stack.append(('loop', self.argval))
def BREAK_LOOP(self):
index = self.instructions[self.block_stack[-1][1]]
self.block_stack.pop()
return index
def LOAD_CONST(self):
self.stack.append(self.argval)
def BUILD_TUPLE(self):
args = []
for i in range(self.argval):
args.append(self.stack.pop())
args = reversed(args)
self.stack.append(tuple(args))
def BUILD_LIST(self):
args = []
for i in range(self.argval):
args.append(self.stack[-1].pop())
args = reversed(args)
self.stack.append(list(args))
def BUILD_SET(self):
args = []
for i in range(self.argval):
args.append(self.stack[-1])
self.stack.pop()
args = reversed(args)
stack.append(set(args))
def BUILD_MAP(self):
args = []
for i in range(self.argval):
args.append((self.stack[-2], self.stack[-1]))
self.stack.pop()
self.stack.pop()
self.stack.append(dict(args))
def LOAD_ATTR(self):
self.stack[-1] = getattr(self.stack[-1], self.argval)
def FOR_ITER(self):
TOS = self.stack[-1]
try:
a = next(TOS)
self.stack.append(a)
except StopIteration:
self.stack.pop()
return self.instructions[self.argval]
def CALL_FUNCTION(self):
pos_args_number = self.argval % 256
keyword_args_number = self.argval // 256
keyword_args = {}
for i in range(keyword_args_number):
keyword_args[self.stack[-2]] = self.stack[-1]
self.stack.pop()
self.stack.pop()
pos_args = []
for i in range(pos_args_number):
pos_args.append(self.stack[-1])
self.stack.pop()
pos_args = reversed(pos_args)
function = self.stack[-1]
self.stack.pop()
self.stack.append(function(*pos_args, **keyword_args))
def MAKE_FUNCTION(self): # INCOMPLETE
qualname = self.stack[-1]
self.stack.pop()
codeobject = self.stack[-1]
self.stack.pop()
defaults_num = self.argval & 0xFF
named_defaults_num = (self.argval >> 8) & 0xFF
annotation_num = (self.argval >> 16) & 0x7FFF
defaults = []
for i in range(defaults_num):
defaults.append(self.stack[-1])
self.stack.pop()
self.stack.append(Function(qualname, codeobject,
self.vm_, tuple(defaults)))
def BUILD_SLICE(self):
if self.argval == 2:
b, a = self.stack[-2:]
self.stack[-2:] = []
self.stack.append(slice(b, a))
else:
c, b, a = self.stack[-3:]
self.stack[-3:] = []
self.stack.append(slice(c, b, a))
def UNARY_POSITIVE(self):
self.stack[-1] = +self.stack[-1]
def UNARY_NEGATIVE(self):
self.stack[-1] = -self.stack[-1]
def UNARY_NOT(self):
self.stack[-1] = not self.stack[-1]
def UNARY_INVERT(self):
self.stack[-1] = ~self.stack[-1]
def GET_ITER(self):
self.stack[-1] = iter(self.stack[-1])
def COMPARE_OP(self): # exception match and BAD?
a = self.stack[-1]
self.stack.pop()
b = self.stack[-1]
self.stack.pop()
operators = {
'<': operator.lt,
'<=': operator.le,
'>': operator.gt,
'>=': operator.ge,
'==': operator.eq,
'!=': operator.ne,
'is': operator.is_,
'is not': operator.is_not,
'in': lambda x, y: x in y,
'not in': lambda x, y: x not in y
}
self.stack.append(operators[self.argval](b, a))
class VirtualMachine(object):
def __init__(self, verbose=False):
self.verbose = verbose
self.call_stack = [Frame()]
def prepare_codeobject_(self, codeobject):
instructions = dict()
instructions_list = []
for instruction in dis.get_instructions(codeobject):
instructions[instruction.offset] = len(instructions_list)
instructions_list.append(instruction)
return instructions, instructions_list
def execute(self, instruction, instructions):
stack = self.call_stack[-1].data_stack
executor = Executor(instruction, self.call_stack[-1],
instructions, self)
if 'BINARY' in instruction.opname:
a = stack.pop()
b = stack.pop()
operations = {
'BINARY_POWER': operator.pow,
'BINARY_MULTIPLY': operator.mul,
'BINARY_FLOOR_DIVIDE': operator.floordiv,
'BINARY_TRUE_DIVIDE': operator.truediv,
'BINARY_MODULO': operator.mod,
'BINARY_ADD': operator.add,
'BINARY_SUBSTRACT': operator.sub,
'BINARY_SUBSCR': operator.getitem,
'BINARY_LSHIFT': operator.lshift,
'BINARY_RSHIFT': operator.rshift,
'BINARY_AND': operator.and_,
'BINARY_XOR': operator.xor,
'BINARY_OR': operator.or_,
}
stack.append(operations[instruction.opname](b, a))
elif 'INPLACE' in instruction.opname:
a = stack[-1]
stack.pop()
operations = {
'INPLACE_POWER': operator.ipow,
'INPLACE_MULTIPLY': operator.imul,
'INPLACE_FLOOR_DIVIDE': operator.ifloordiv,
'INPLACE_TRUE_DIVIDE': operator.itruediv,
'INPLACE_MODULO': operator.imod,
'INPLACE_ADD': operator.iadd,
'INPLACE_SUBTRACT': operator.isub,
'INPLACE_LSHIFT': operator.ilshift,
'INPLACE_RSHIFT': operator.irshift,
'INPLACE_AND': operator.iand,
'INPLACE_XOR': operator.ixor,
'INPLACE_OR': operator.ior,
}
stack[-1] = operations[instruction.opname](stack[-1], a)
elif instruction.opname in dir(Executor):
return getattr(executor, instruction.opname)()
def run_code(self, codeobject):
if type(codeobject) == str:
codeobject = compile(codeobject, '', 'exec')
self.__init__(self.verbose)
instructions, instructions_list = self.prepare_codeobject_(codeobject)
index = 0
while index != len(instructions_list):
execute_return = self.execute(instructions_list[index],
instructions)
while type(execute_return) is int:
index = execute_return
execute_return = self.execute(instructions_list[index],
instructions)
index += 1
if self.verbose:
print('END EXECUTION.\t\t STACK IS',
self.call_stack[-1].data_stack)
def run_function(self, function):
self.call_stack.append(function.frame)
instructions, instructions_list = self.prepare_codeobject_(
function.codeobject)
index = 0
while index != len(instructions_list):
execute_return = self.execute(instructions_list[index],
instructions)
while type(execute_return) is int:
index = execute_return
execute_return = self.execute(instructions_list[index],
instructions)
if type(execute_return) is tuple:
function.clean_up()
self.call_stack.pop()
return execute_return[0]
index += 1
if self.verbose:
print('END EXECUTION.\t\t STACK IS',
self.call_stack[-1].data_stack)
if __name__ == "__main__":
compiled = compile(sys.stdin.read(), "<stdin>", 'exec')
VirtualMachine().run_code(compiled)