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

Nonreentrant return fix #1532

Merged
merged 2 commits into from
Jul 19, 2019
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
5 changes: 4 additions & 1 deletion tests/parser/features/decorators/test_nonreentrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ def set_callback(c: address):

@public
@nonreentrant('protect_special_value')
def protected_function(val: string[100], do_callback: bool):
def protected_function(val: string[100], do_callback: bool) -> uint256:
self.special_value = val

if do_callback:
self.callback.updated_protected()
return 1
else:
return 2

@public
def unprotected_function(val: string[100], do_callback: bool):
Expand Down
5 changes: 4 additions & 1 deletion vyper/parser/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def __init__(self,
is_private=False,
is_payable=False,
origcode='',
method_id=''):
method_id='',
sig=None):
# In-memory variables, in the form (name, memory location, type)
self.vars = vars or {}
# Memory alloctor, keeps track of currently allocated memory.
Expand Down Expand Up @@ -79,6 +80,8 @@ def __init__(self,
self.method_id = method_id
# store global context
self.global_ctx = global_ctx
# full function signature
self.sig = sig

def is_constant(self):
return self.constancy is Constancy.Constant or \
Expand Down
3 changes: 2 additions & 1 deletion vyper/parser/function_definitions/parse_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def parse_function(code, sigs, origcode, global_ctx, _vars=None):
is_payable=sig.payable,
origcode=origcode,
is_private=sig.private,
method_id=sig.method_id
method_id=sig.method_id,
sig=sig
)

if sig.private:
Expand Down
12 changes: 9 additions & 3 deletions vyper/parser/parser_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,10 @@ def zero_pad(bytez_placeholder, maxlen, context):

# Generate return code for stmt
def make_return_stmt(stmt, context, begin_pos, _size, loop_memory_position=None):
from vyper.parser.function_definitions.utils import (
get_nonreentrant_lock
)
_, nonreentrant_post = get_nonreentrant_lock(context.sig, context.global_ctx)
if context.is_private:
if loop_memory_position is None:
loop_memory_position = context.new_placeholder(typ=BaseType('uint256'))
Expand All @@ -922,7 +926,8 @@ def make_return_stmt(stmt, context, begin_pos, _size, loop_memory_position=None)
mloads = [
['mload', pos] for pos in range(begin_pos, _size, 32)
]
return ['seq_unchecked'] + mloads + [['jump', ['mload', context.callback_ptr]]]
return ['seq_unchecked'] + mloads + nonreentrant_post + \
[['jump', ['mload', context.callback_ptr]]]
else:
mloads = [
'seq_unchecked',
Expand All @@ -945,9 +950,10 @@ def make_return_stmt(stmt, context, begin_pos, _size, loop_memory_position=None)
['goto', start_label],
['label', exit_label]
]
return ['seq_unchecked'] + [mloads] + [['jump', ['mload', context.callback_ptr]]]
return ['seq_unchecked'] + [mloads] + nonreentrant_post + \
[['jump', ['mload', context.callback_ptr]]]
else:
return ['return', begin_pos, _size]
return ['seq_unchecked'] + nonreentrant_post + [['return', begin_pos, _size]]


# Generate code for returning a tuple or struct.
Expand Down
2 changes: 1 addition & 1 deletion vyper/parser/self_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def call_self_private(stmt_expr, context, sig):
static_arg_size = 32 * sum(
[get_static_size_of_type(arg.typ)
for arg in expr_args])
static_pos = arg_pos + static_arg_size
static_pos = int(arg_pos + static_arg_size)
needs_dyn_section = any(
[has_dynamic_data(arg.typ)
for arg in expr_args])
Expand Down