Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unique variable name for yul variable #648

Merged
merged 2 commits into from
Sep 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions slither/solc_parsing/yul/parse_yul.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ def link_underlying_nodes(node1: YulNode, node2: YulNode):
link_nodes(node1.underlying_node, node2.underlying_node)


def _name_to_yul_name(variable_name: str, yul_id: List[str]) -> str:
"""
Translate the variable name to a unique yul name
Within the same function, yul blocks can declare
different variables with the same name
We need to create unique name per variable
to prevent collision during the SSA generation

:param var:
:param yul_id:
:return:
"""
return variable_name + f"_{'_'.join(yul_id)}"


class YulScope(metaclass=abc.ABCMeta):
__slots__ = [
"_contract",
Expand Down Expand Up @@ -135,7 +150,11 @@ def add_yul_local_variable(self, var):

def get_yul_local_variable_from_name(self, variable_name):
return next(
(v for v in self._yul_local_variables if v.underlying.name == variable_name),
(
v
for v in self._yul_local_variables
if v.underlying.name == _name_to_yul_name(variable_name, self.id)
),
None,
)

Expand All @@ -162,7 +181,7 @@ def __init__(self, var: LocalVariable, root: YulScope, ast: Dict):
var.set_function(root.function)
var.set_offset(ast["src"], root.slither)

var.name = ast["name"]
var.name = _name_to_yul_name(ast["name"], root.id)
var.set_type(ElementaryType("uint256"))
var.set_location("memory")

Expand Down Expand Up @@ -516,7 +535,6 @@ def convert_yul_typed_name(root: YulScope, parent: YulNode, ast: Dict) -> YulNod
local_var = LocalVariable()

var = YulLocalVariable(local_var, root, ast)

root.add_yul_local_variable(var)

node = root.new_node(NodeType.VARIABLE, ast["src"])
Expand Down