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: Missing Type Check for Built-in Function Arguments (len, cap) Causes Runtime Panic #3454 #4033

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

Conversation

KemalBekir
Copy link
Contributor

Invalid arguments provided to builtin functions len,cap and make would compile successfully but it was triggering a runtime panic when executed.

fixes #3454

@github-actions github-actions bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Mar 30, 2025
@Gno2D2 Gno2D2 requested a review from a team March 30, 2025 22:08
@Gno2D2 Gno2D2 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Mar 30, 2025
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Mar 30, 2025

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
  • The pull request description provides enough details
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)
🟢 Pending initial approval by a review team member, or review from tech-staff

☑️ 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: KemalBekir/gno)

Then

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

Pending initial approval by a review team member, or review from tech-staff

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 Not (🔴 Pull request author is a member of the team: tech-staff)

Then

🟢 Requirement satisfied
└── 🟢 If
    ├── 🟢 Condition
    │   └── 🟢 Or
    │       ├── 🔴 At least 1 user(s) of the organization reviewed the pull request (with state "APPROVED")
    │       ├── 🟢 At least 1 user(s) of the team tech-staff reviewed pull request
    │       └── 🔴 This pull request is a draft
    └── 🟢 Then
        └── 🟢 Not (🔴 This label is applied to pull request: review/triage-pending)

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
The pull request description provides enough details

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 Not (🔴 Pull request author is a member of the team: core-contributors)
    └── 🟢 Not (🔴 Pull request author is user: dependabot[bot])

Can be checked by

  • team core-contributors

@KemalBekir KemalBekir changed the title fix:Missing Type Check for Built-in Function Arguments (len, cap) Causes Runtime Panic #3454 fix: Missing Type Check for Built-in Function Arguments (len, cap) Causes Runtime Panic #3454 Mar 30, 2025
@zivkovicmilos zivkovicmilos requested review from thehowl, ltzmaxwell and petar-dambovaliev and removed request for a team March 31, 2025 08:43
Copy link
Member

Choose a reason for hiding this comment

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

These should be filetests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will move them there.

Copy link
Member

@thehowl thehowl left a comment

Choose a reason for hiding this comment

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

a first sanity review

Comment on lines 5506 to 5510
const (
BuiltinLen = "len"
BuiltinCap = "cap"
BuiltinMake = "make"
)
Copy link
Member

Choose a reason for hiding this comment

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

We don't use constant names, so just use the raw strings in the comparison above.

@@ -1362,6 +1363,10 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node {
ift := evalStaticTypeOf(store, last, n.Func)
switch cft := baseOf(ift).(type) {
case *FuncType:
fn := extractFunctionName(n)
if fn == BuiltinMake || fn == BuiltinCap || fn == BuiltinLen {
assertValidBuiltinArgType(store, last, n)
Copy link
Member

Choose a reason for hiding this comment

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

Just use evalStaticTypeOf on each argument.

case *ConstExpr:
assertValidBuiltinArgType(store, last, currExpr.Source)
case *NameExpr:
assertValidBuiltinArgType(store, last, currExpr)
Copy link
Member

Choose a reason for hiding this comment

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

This is an infinite loop

Copy link
Contributor

@petar-dambovaliev petar-dambovaliev left a comment

Choose a reason for hiding this comment

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

Looks okay. Just fix the requested changes.

case *BasicLitExpr:
var num int64
if e.Kind == FLOAT {
f, err := hasFractionalPart(e.Value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you change the name of this function? The name looks like it returns a boolean but actually returns a number and an error.

@jefft0 jefft0 removed the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Apr 1, 2025
Copy link
Contributor

@ltzmaxwell ltzmaxwell left a comment

Choose a reason for hiding this comment

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

a first round review.


switch at := unwrapPointerType(baseOf(at)).(type) {
case PrimitiveType:
if at.Kind() != StringKind && at != UntypedStringType {
Copy link
Contributor

Choose a reason for hiding this comment

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

seems at.Kind() != StringKind is already good.

Comment on lines +5600 to +5602
if capacity < length {
capacity = length
}
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't just panic here?

Comment on lines +5511 to +5521

// Check for invalid arguments in builtin functions
// len, cap, make
func assertValidBuiltinArgType(store Store, last BlockNode, currExpr Expr) {
switch currExpr := currExpr.(type) {
case *ConstExpr:
assertValidBuiltinArgType(store, last, currExpr.Source)
case *CallExpr:
ift := evalStaticTypeOf(store, last, currExpr.Func)
switch baseOf(ift).(type) {
case *FuncType:
Copy link
Contributor

Choose a reason for hiding this comment

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

it'll be better to put these checks in type_check.go.

@@ -6,4 +6,4 @@ func main() {
}

// Error:
// unexpected type for cap(): *int
// main/files/cap8.gno:5:17: unexpected type for cap(): int
Copy link
Contributor

Choose a reason for hiding this comment

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

looks not right, the type should be *int.

Comment on lines +5663 to +5665
if f != intPart {
return f, fmt.Errorf("invalid argument: %s has a fractional part", s)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a test cover this?

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

Successfully merging this pull request may close these issues.

Missing Type Check for Built-in Function Arguments (len, cap) Causes Runtime Panic
7 participants