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

feat(gnovm): custom go parser for gas counting and nesting limitation #4027

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

mvertes
Copy link
Contributor

@mvertes mvertes commented Mar 28, 2025

The Go parser from the standard library has been imported in gnovm/pkg/parser, with a Makefile to generate and apply custom patch.

The patch consist to add a new function ParseFileStats which performs accounting to enable gas computation and set the maximum nesting level as an input parameter to instead of a constant.

Parsing is instrumented to measure and control the nesting level and have a first cost estimate of parsing to compute gas, before type checking itself.

The advantages are that there is almost no overhead, early catch of problems (even before the AST memory is allocated), and a simple gas formula (no distinction between tokens except the nesting level). No extra pass is added, we just exploit some internal parser metadata for prevention of too deep nesting and gas counting.

The drawback is to fork and maintain a different version of the Go parser, but most of it is automated.

Note: the Go parser imported through the makefile corresponds to the go version installed on host (currently go1.23.7).

The maximum parsing nesting limit has been fixed to 10000, instead of previous 100000. It correspond to a maximum AST depth of approximately half of it (ie now 5000 instead of 50000). The limit is now a parameter of ParseFileStats() instead of a private constant.

The original behaviour and limits are preserved for existing functions, as ParseFile.

Some cosmetic patches to parser are added, to comply with gno lint and format rules.

This is a possible alternative to #3974

The Go parser from the standard library has been imported in
gnovm/pkg/parser, with a Makefile to generate and apply custom
patch.

The patch consist to add a new function ParseFileStats which performs
accounting to enable gas computation and set the maximum nesting level
as an input parameter to instead of a constant.

Parsing is instrumented to measure and control the nesting level and
have a first cost estimate of parsing to compute gas, before type
checking itself.

The advantages are that there is almost no overhead, early catch of
problems (even before the AST memory is allocated), and a simple gas
formula (no distinction between tokens except the nesting level). No
extra pass is added, we just exploit some internal parser metadata for
prevention of too deep nesting and gas counting.

The drawback is to fork and maintain a different version of the Go
parser, but most of it is automated.

Note: the Go parser has been imported from go1.24.1, as it has been
simplified and has no internal dependencies, as opposite to previous
versions.
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Mar 28, 2025

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
Read More

🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers.

✅ Automated Checks (for Contributors):

🟢 Maintainers must be able to edit this pull request (more info)

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Maintainers must be able to edit this pull request (more info)

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 The pull request was created from a fork (head branch repo: mvertes/gno)

Then

🟢 Requirement satisfied
└── 🟢 Maintainer can modify this pull request

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission

@Kouteki Kouteki moved this from Triage to In Progress in 🧙‍♂️gno.land core team Mar 31, 2025
@github-actions github-actions bot added the 📦 ⛰️ gno.land Issues or PRs gno.land package related label Apr 7, 2025
@mvertes mvertes marked this pull request as ready for review April 8, 2025 07:46
// nesting level reached during the parsing, which reflect both the size
// and the complexity of the AST.
return types.Gas(stats.NumTok * math.Ilogb(float64(1+stats.TopNest)))
}
Copy link
Contributor

@piux2 piux2 Apr 9, 2025

Choose a reason for hiding this comment

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

Nice! Gas is a measure of computation time, benchmarked in nanoseconds. Is it possible to benchmark this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 ⛰️ gno.land Issues or PRs gno.land package related 📦 🤖 gnovm Issues or PRs gnovm related
Projects
Status: No status
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants