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

ARC-64: AVM Run Time Errors based on program source map #305

Closed
wants to merge 8 commits into from
Closed
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
103 changes: 103 additions & 0 deletions ARCs/arc-0064.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
arc: 64
title: AVM Run Time Errors Source Map
description: Informative AVM run time errors based on program counter source map
author: Cosimo Bassi (@cusma), Tasos Bitsios (@tasosbit), Steve Ferrigno (@nullun)
discussions-to: https://github.com/algorandfoundation/ARCs/issues/305
status: Draft
type: Standards Track
category: ARC
created: 2024-08-05
---

## Abstract

This document introduces a convention for rising informative run time error on the
Algorand Virtual Machine (AVM) based on a program counter *source map*.

## Motivation

The AVM does not offer native opcodes to catch and raise run time errors.

The lack of native error handling semantics could lead to fragmentation of tooling
and frictions for AVM clients, who are unable to retrieve informative and useful
hints about the occurred run time failures.

This ARC formalizes conventions to raise AVM run time errors based on a program
counter source map.

## Specification

The keywords "**MUST**", "**MUST NOT**", "**REQUIRED**", "**SHALL**", "**SHALL NOT**",
"**SHOULD**", "**SHOULD NOT**", "**RECOMMENDED**", "**MAY**", and "**OPTIONAL**"
in this document are to be interpreted as described in <a href="https://datatracker.ietf.org/doc/html/rfc2119">RFC 2119</a>.

> Notes like this are non-normative.

### Program Counter Error Source Map

> TEAL source code instructions are uniquely identified by a program counter, returned
> in the Algod API response upon AVM run time failure.

> A program counter source map maps program counters back to the original TEAL
> source code lines.

> The program counter error source map convention is based on error messages written
> as comments in the TEAL source code.

When a program wants to emit a run time error, it **MUST**:

1. Precede the failing TEAL instruction with a comment (error message);
1. Produce a program counter source map for the client.

Upon a program run time failure, the Algod API returns the failed *program counter*
(`pc`).

Using the program counter, the client **MUST**:

1. Retrieve the error message preceding the failed TEAL instruction from the program
counter source map;
1. Return the error message.

### Example

```
int 0 [pc=0]
int 1 [pc=1]
&& [pc=2]
// ERR_LOGIC_FALSE
assert [pc=3]
```

> The evaluation of the example program would result in a run time error on the
> `assert` instruction, returning the failed program counter (`[pc=3]`) as part
> of the Algod API response. Using a program counter source map, the client is
> then able to retrieve and return the corresponding error message `ERR_LOGIC_FALSE`
> (i.e. the comment preceding the failed program counter).

## Rationale

The program counter error source map convention for AVM run time error messages
presents the following PROS and CONS.

### PROS

- No program size consumed by error messages.

### CONS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another con here is that using the PC will be unreliable for contracts that have template variables. Integer or dynamic byteslice template variable values will change the PC. This is why ARC56 proposes using disassembled TEAL lines in the event template variables. Regardless of the width of the template variable values, the disassembled TEAL will always be the same.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our side conversation about this you said the steps required to make use of this (from front-end perspective) are:

  1. Get pc=XXX error
  2. Pass bytecode to /dissassemble to get TEAL
  3. Pass TEAL from 2 to /compile?sourcemap=true
  4. Lookup the PC XXX and see what line it corresponds with
  5. Find the object in the source map with disassembledTeal that matches the line given in step 4

Given a front-end will just get a pc=xxx error (that it has to parse out), for it to have to do all that (assuming the node its connecting to even allows the disassemble/compile endpoints!) just to get the error message associated with that PC is extremely onerous. I think it kills the whole thing imo.

Short of dynamic strings, I think the template variables being variable sized is a fixable issue. Instead of having templates do pushint, pushbytes etc at every reference, perhaps have them initialized at start into scratch storage from fixed size values, ie: btoi(0x0000000000000001) for a templated int 1 - and then reference that scratch slot late for the constant reference?


- Standard program source maps required for clients to map errors to failed program
counter;
- Developers or compilers need to create an extra “errors source map” to map program
assertion locations to specific error codes;
- Portability: errors source map needs to be kept up to date through different
iterations of a program;
- Error message not returned directly in the Algod API response.

## Security Considerations

> Not applicable.

## Copyright

Copyright and related rights waived via <a href="https://creativecommons.org/publicdomain/zero/1.0/">CCO</a>.
Loading