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

fix[codegen]: fix non-memory reason strings #3877

Merged
merged 8 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 22 additions & 0 deletions tests/functional/codegen/features/test_assert.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def foo():

def test_assert_reason(w3, get_contract_with_gas_estimation, tx_failed, memory_mocker):
code = """
err: String[32]

@external
def test(a: int128) -> int128:
assert a > 1, "larger than one please"
Expand All @@ -43,6 +45,17 @@ def test2(a: int128, b: int128, extra_reason: String[32]) -> int128:
@external
def test3(reason_str: String[32]):
raise reason_str

@external
def test4(a: int128, reason_str: String[32]) -> int128:
self.err = reason_str
assert a > 1, self.err
return 1 + a

@external
def test5(reason_str: String[32]):
self.err = reason_str
raise self.err
"""
c = get_contract_with_gas_estimation(code)

Expand All @@ -66,6 +79,15 @@ def test3(reason_str: String[32]):
c.test3("An exception")
assert _fixup_err_str(e_info.value.args[0]) == "An exception"

assert c.test4(2, "msg") == 3
with pytest.raises(TransactionFailed) as e_info:
c.test4(0, "larger than one again please")
assert _fixup_err_str(e_info.value.args[0]) == "larger than one again please"

with pytest.raises(TransactionFailed) as e_info:
c.test5("A storage exception")
assert _fixup_err_str(e_info.value.args[0]) == "A storage exception"


invalid_code = [
"""
Expand Down
58 changes: 20 additions & 38 deletions vyper/codegen/stmt.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import vyper.codegen.events as events
import vyper.utils as util
from vyper import ast as vy_ast
from vyper.codegen.abi_encoder import abi_encode

Check notice

Code scanning / CodeQL

Cyclic import Note

Import of module
vyper.codegen.abi_encoder
begins an import cycle.
from vyper.codegen.context import Constancy, Context
from vyper.codegen.core import (
LOAD,
STORE,
IRnode,
add_ofst,
clamp_le,
get_dyn_array_count,
get_element_ptr,
make_byte_array_copier,
get_type_for_exact_size,
make_setter,
zero_pad,
)
from vyper.codegen.expr import Expr
from vyper.codegen.return_ import make_return_stmt
from vyper.evm.address_space import MEMORY, STORAGE
from vyper.exceptions import (
CodegenPanic,
CompilerPanic,
StructureException,
TypeCheckFailure,
tag_exceptions,
)
from vyper.exceptions import CodegenPanic, StructureException, TypeCheckFailure, tag_exceptions
from vyper.semantics.types import DArrayT
from vyper.semantics.types.shortcuts import UINT256_T

Expand Down Expand Up @@ -132,39 +127,26 @@
finally:
self.context.constancy = tmp

# TODO this is probably useful in codegen.core
# compare with eval_seq.
def _get_last(ir):
if len(ir.args) == 0:
return ir.value
return _get_last(ir.args[-1])

# TODO maybe use ensure_in_memory
if msg_ir.location != MEMORY:
buf = self.context.new_internal_variable(msg_ir.typ)
instantiate_msg = make_byte_array_copier(buf, msg_ir)
else:
buf = _get_last(msg_ir)
if not isinstance(buf, int): # pragma: nocover
raise CompilerPanic(f"invalid bytestring {buf}\n{self}")
instantiate_msg = msg_ir
bufsz = msg_ir.typ.memory_bytes_required + 64
buf = self.context.new_internal_variable(get_type_for_exact_size(bufsz))

# offset of bytes in (bytes,)
method_id = util.method_id_int("Error(string)")

# abi encode method_id + bytestring
assert buf >= 36, "invalid buffer"
# we don't mind overwriting other memory because we are
# getting out of here anyway.
_runtime_length = ["mload", buf]
revert_seq = [
"seq",
instantiate_msg,
zero_pad(buf),
["mstore", buf - 64, method_id],
["mstore", buf - 32, 0x20],
["revert", buf - 36, ["add", 4 + 32 + 32, ["ceil32", _runtime_length]]],
]
# abi encode method_id + bytestring to `buf+32`, then
# write method_id to `buf` and get out of here
bufsz -= 32 # reduce buffer by size of `method_id` slot
encoded_length = abi_encode(
add_ofst(buf, 32), msg_ir, self.context, bufsz, returns_len=True
)
with encoded_length.cache_when_complex("encoded_len") as (b1, encoded_length):
revert_seq = [
"seq",
["mstore", buf, method_id],
["revert", add_ofst(buf, 28), ["add", 4 + 32, encoded_length]],
]
revert_seq = b1.resolve(revert_seq)

if is_raise:
ir_node = revert_seq
else:
Expand Down
Loading