This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Dev shadow #39
Open
ghost
wants to merge
6
commits into
execution
Choose a base branch
from
dev_shadow
base: execution
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Dev shadow #39
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
422f50f
refactor files in directory executor/
tc20042008 89e75a1
shadow.SymbolicTranslator/shadow.SymbolicExecutor
tc20042008 2bb4f53
refactor signature of *SymbolicExecutor.__init__
tc20042008 5b66d24
SymbolicExecutor.pre_RETURN_VALUE
tc20042008 f8eae4c
merge branch execution
tc20042008 804d1d8
refine debug code
tc20042008 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ user_tag | |
|
||
# Editor config | ||
.vscode | ||
core.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from .symbolic_executor import SymbolicExecutor | ||
from .symbolic_frame_mgr import SymbolicFrameMgr | ||
from ..utils import no_eval_frame | ||
import types | ||
|
||
class InitialSymbolicExecutor(SymbolicExecutor): | ||
@no_eval_frame | ||
def __init__(self, code_obj: types.CodeType): | ||
frame = SymbolicFrameMgr.current_frame(code_obj) | ||
super().__init__(frame) | ||
|
||
def pre_RETURN_VALUE(self, instruction): | ||
assert len(self.frame.stack) == 1, "Stack must have one element." | ||
ret_val = self.pop() | ||
new_code, guard_fn = self.frame.function_graph.start_compile(ret_val) | ||
from .symbolic_translator_cache import SymbolicTranslatorCache | ||
SymbolicTranslatorCache().update_executed_code_obj(self.frame.f_code, new_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from .symbolic_executor import SymbolicExecutor | ||
from .symbolic_frame_mgr import SymbolicFrameMgr | ||
from ..utils import no_eval_frame | ||
import types | ||
|
||
class NormalSymbolicExecutor(SymbolicExecutor): | ||
@no_eval_frame | ||
def __init__(self, code_obj: types.CodeType): | ||
frame = SymbolicFrameMgr.create_frame(code_obj) | ||
super().__init__(frame) | ||
|
||
def pre_RETURN_VALUE(self, instruction): | ||
# Do nothing | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
class SymbolicDict: | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from .symbolic_frame import SymbolicFrame | ||
from ..opcode_translator.executor.source import LocalSource | ||
from ..opcode_translator.executor.variables import ( | ||
ConstantVariable, | ||
) | ||
from ..utils import no_eval_frame | ||
import types | ||
import dis | ||
import sys | ||
|
||
class SymbolicExecutor: | ||
frame: SymbolicFrame | ||
# next instruction to be executed. | ||
next_instruction_index: int | ||
|
||
def __init__(self, frame: SymbolicFrame): | ||
self.frame = frame | ||
self.next_instruction_index = 0 | ||
|
||
@no_eval_frame | ||
def __call__(self, instruction_index): | ||
instruction = self.frame.instructions[instruction_index] | ||
if self.next_instruction_index != instruction_index: | ||
self._run_post_jump_instruction(self.next_instruction_index, instruction_index) | ||
self._run_post_instruction(instruction_index) | ||
self.next_instruction_index = instruction_index + 1 | ||
|
||
@no_eval_frame | ||
def pre_action(self, instruction_index): | ||
instruction = self.frame.instructions[instruction_index] | ||
method_name = f"pre_{instruction.opname}" | ||
assert hasattr(self, method_name) | ||
getattr(self, method_name)(instruction) | ||
|
||
def pre_RETURN_VALUE(self, instruction): | ||
raise NotImplementedError("Derived class should override prev_RETURN_VALUE() method") | ||
|
||
def _run_post_jump_instruction(self, jump_instruction_index, target_instruction_index): | ||
jump_instruction = self.get_instruction(jump_instruction_index) | ||
assert self._is_jump_instruction(jump_instruction) | ||
is_jump = self._is_jump(jump_instruction, target_instruction_index) | ||
TODO | ||
|
||
def _run_post_instruction(self, instruction_index): | ||
assert instruction_index >= 0 | ||
instruction = self.frame.instructions[instruction_index] | ||
opname = instruction.opname | ||
assert hasattr(self, opname), f"{opname} not supported" | ||
method = getattr(self, opname) | ||
method(instruction) | ||
|
||
def push(self, value): | ||
self.frame.stack.append(value) | ||
|
||
def pop(self): | ||
return self.frame.stack.pop() | ||
|
||
def LOAD_FAST(self, instr): | ||
varname = instr.argval | ||
var = self.frame.f_locals[varname] | ||
var.try_set_source(LocalSource(instr.arg, varname)) | ||
self.push(var) | ||
|
||
def STORE_FAST(self, instr): | ||
""" | ||
TODO: side effect may happen | ||
""" | ||
var = self.pop() | ||
self.frame.f_locals[instr.argval] = var | ||
|
||
def LOAD_CONST(self, instr): | ||
var = ConstantVariable(instr.argval) | ||
self.push(var) | ||
|
||
def BINARY_ADD(self, instr): | ||
b = self.pop() | ||
a = self.pop() | ||
self.push(a + b) | ||
|
||
def BINARY_MULTIPLY(self, instr): | ||
b = self.pop() | ||
a = self.pop() | ||
self.push(a * b) | ||
|
||
def RETURN_VALUE(self, instr): | ||
raise NotImplementedError("dead code never to be executed.") | ||
|
||
def __del__(self): | ||
# Do nothing. | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import List,Dict, Optional | ||
from . import symbolic_frame_stack as symbolic_frame_stack | ||
import types | ||
import dis | ||
|
||
class SymbolicFrame: | ||
f_locals: Dict[str, "VariableTracker"] | ||
function_graph: "FunctionGraph" | ||
f_code: types.CodeType | ||
stack: List["VariableTracker"] | ||
instructions: List[dis.Instruction] | ||
f_back: "SymbolicFrame" | ||
|
||
def __init__(self, f_locals, function_graph, code_obj, instructions): | ||
self.f_locals = f_locals | ||
self.function_graph = function_graph | ||
self.f_code = code_obj | ||
self.instructions = instructions | ||
self.stack = [] | ||
self.f_back = symbolic_frame_stack.top() | ||
symbolic_frame_stack.push(self) | ||
|
||
def __del__(self): | ||
symbolic_frame_stack.pop(self.f_back) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里实现了pre_RETURN_VALUE,是否所有的opcode类型都需要实现 pre_XXX 函数逻辑?还是按需的?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所有的控制类包括跳转指令、RETURN_VALUE、YIELD_VALUE等都需要实现pre_XXX。其他实现post action就行。