Skip to content

fix[tool]: fix layout export with nonreentrancy pragma on #4621

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

Merged
merged 2 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
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: 24 additions & 0 deletions tests/unit/cli/storage_layout/test_storage_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,27 @@ def test_multiple_compile_codes(make_input_bundle):

assert layout1 == {"x": {"slot": start_slot, "type": "uint256", "n_slots": 1}}
assert layout2 == {"x": {"slot": start_slot + 1, "type": "uint256", "n_slots": 1}}


# test that the nonreentrancy lock gets excported when the nonreentrant pragma
# is on and the public getters are nonreentrant
def test_lock_export_with_nonreentrant_pragma(make_input_bundle):
main = """
# pragma nonreentrancy on
a: public(uint256)
"""
out = compile_code(main, output_formats=["layout"])["layout"]

if version_check(begin="cancun"):
storage_layout = {"a": {"type": "uint256", "n_slots": 1, "slot": 0}}
transient_layout = {
"$.nonreentrant_key": {"type": "nonreentrant lock", "slot": 0, "n_slots": 1}
}
assert transient_layout == out["transient_storage_layout"]
else:
storage_layout = {
"a": {"type": "uint256", "n_slots": 1, "slot": 1},
"$.nonreentrant_key": {"type": "nonreentrant lock", "slot": 0, "n_slots": 1},
}

assert storage_layout == out["storage_layout"]
18 changes: 18 additions & 0 deletions tests/unit/cli/storage_layout/test_storage_layout_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,21 @@ def test_module_overlap(make_input_bundle):
input_bundle=input_bundle,
storage_layout_override=override,
)


def test_override_with_nonreentrant_pragma(make_input_bundle):
code = """
# pragma nonreentrancy on
a: public(uint256)
"""

if version_check(begin="cancun"):
override = {"a": {"type": "uint256", "n_slots": 1, "slot": 0}}
else:
override = {
"a": {"type": "uint256", "n_slots": 1, "slot": 0},
"$.nonreentrant_key": {"type": "nonreentrant lock", "n_slots": 1, "slot": 20},
}

# note: compile_code checks roundtrip of the override
compile_code(code, storage_layout_override=override)
21 changes: 14 additions & 7 deletions vyper/semantics/analysis/data_positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ def _allocate_with_overrides(vyper_module: vy_ast.Module, layout: StorageLayout)
_allocate_with_overrides_r(vyper_module, layout, allocator, nonreentrant_slot, [])


def _get_func_defs(vyper_module: vy_ast.Module):
funcdefs = vyper_module.get_children(vy_ast.FunctionDef)
for vardecl in vyper_module.get_children(vy_ast.VariableDecl):
if not vardecl.is_public:
# no getter
continue
funcdefs.append(vardecl._expanded_getter)

return funcdefs


def _allocate_with_overrides_r(
vyper_module: vy_ast.Module,
layout: StorageLayout,
Expand All @@ -190,12 +201,7 @@ def _allocate_with_overrides_r(
path: list[str],
):
# Search through function definitions to find non-reentrant functions
funcdefs = vyper_module.get_children(vy_ast.FunctionDef)
for vardecl in vyper_module.get_children(vy_ast.VariableDecl):
if not vardecl.is_public:
# no getter
continue
funcdefs.append(vardecl._expanded_getter)
funcdefs = _get_func_defs(vyper_module)

for node in funcdefs:
fn_t = node._metadata["func_type"]
Expand Down Expand Up @@ -391,7 +397,8 @@ def _generate_layout_export_r(vyper_module):
raise CompilerPanic("unreachable")
ret[layout_key][node.target.id] = item

for fn in vyper_module.get_children(vy_ast.FunctionDef):
funcdefs = _get_func_defs(vyper_module)
for fn in funcdefs:
fn_t = fn._metadata["func_type"]
if not fn_t.nonreentrant:
continue
Expand Down