Skip to content

Commit

Permalink
chore: improve some error messages (#3524)
Browse files Browse the repository at this point in the history
fix array bounds check and `create_*` builtin error messages

- array bounds checks, previously were something like

`clamp lt [mload, 640 <result>]`

- codesize check error message was missing for create builtins
- create failure error message was also missing
  • Loading branch information
charles-cooper authored Jul 24, 2023
1 parent 299352e commit 4ca1c81
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
16 changes: 13 additions & 3 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,9 @@ def _create_ir(value, buf, length, salt=None, checked=True):
if not checked:
return ret

return clamp_nonzero(ret)
ret = clamp_nonzero(ret)
ret.set_error_msg(f"{create_op} failed")
return ret


# calculate the gas used by create for a given number of bytes
Expand Down Expand Up @@ -1830,7 +1832,10 @@ def _build_create_IR(self, expr, args, context, value, salt):
ir = ["seq"]

# make sure there is actually code at the target
ir.append(["assert", codesize])
check_codesize = ["assert", codesize]
ir.append(
IRnode.from_list(check_codesize, error_msg="empty target (create_copy_of)")
)

# store the preamble at msize + 22 (zero padding)
preamble, preamble_len = _create_preamble(codesize)
Expand Down Expand Up @@ -1920,7 +1925,12 @@ def _build_create_IR(self, expr, args, context, value, salt, code_offset, raw_ar
# (code_ofst == (extcodesize target) would be empty
# initcode, which we disallow for hygiene reasons -
# same as `create_copy_of` on an empty target).
ir.append(["assert", ["sgt", codesize, 0]])
check_codesize = ["assert", ["sgt", codesize, 0]]
ir.append(
IRnode.from_list(
check_codesize, error_msg="empty target (create_from_blueprint)"
)
)

# copy the target code into memory.
# layout starting from mem_ofst:
Expand Down
1 change: 1 addition & 0 deletions vyper/codegen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ def _get_element_ptr_array(parent, key, array_bounds_check):
# an array index, and the clamp will throw an error.
# NOTE: there are optimization rules for this when ix or bound is literal
ix = clamp("lt", ix, bound)
ix.set_error_msg(f"{parent.typ} bounds check")

if parent.encoding == Encoding.ABI:
if parent.location == STORAGE:
Expand Down
8 changes: 8 additions & 0 deletions vyper/codegen/ir_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,14 @@ def is_complex_ir(self):
and self.value.lower() not in do_not_cache
)

# set an error message and push down into all children.
# useful for overriding an error message generated by a helper
# function with a more specific error message.
def set_error_msg(self, error_msg: str) -> None:
self.error_msg = error_msg
for arg in self.args:
arg.set_error_msg(error_msg)

# get the unique symbols contained in this node, which provides
# sanity check invariants for the optimizer.
# cache because it's a perf hotspot. note that this (and other cached
Expand Down

0 comments on commit 4ca1c81

Please sign in to comment.