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(gnovm): Prevent use of blank identifier as Value or Type #2699

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

Conversation

notJoon
Copy link
Member

@notJoon notJoon commented Aug 14, 2024

Description

Closes #1946

The isNamedConversion function now includes a safety check to prevent the use of blank identifiers ("_") as values or types. If both xt and t are nil, the function assumes that a blank identifier is being used inappropriately and panics with an error message that includes the location of the issue.

Variable Explanations

  • xt (Expression Type): Represents the type of the right-hand side of an assignment or expression. It's the type resulting from evaluating an expression.
  • t (Target Type): Represents the type of the left-hand side of an assignment. It's the variable or field that will receive the value.

Checks if a named conversion is needed when assigning a value of type xt to a variable of type t.

Preprocess

Added some checks to prevent the disallowd usage of blank identifiers in Preprocess function level. Theses checks are performed at different stages of the preprocessing:

  1. TRANS_ENTER for AssignStmt:
    • Checks if both LHS and RHS are blank identifiers in a DEFINE statement.
  2. TRANS_LEAVE for NameExpr:
    • Checks if blank identifier is used as a value in disallowed contexts (excluding TRANS_ASSIGN_LHS, TRANS_RANGE_KEY and TRANS_RANGE_VALUE).
  3. TRANS_LEAVE for AssignStmt:
    • Checks if RHS is a blank identifier when LHS is not, in a DEFINE statement.

When any of these conditions are met, the function throws an panics like go message.

Contributors' checklist...
  • Added new tests, or not needed, or not feasible
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • No breaking changes were made, or a BREAKING CHANGE: xxx message was included in the description
  • Added references to related issues and PRs
  • Provided any useful hints for running manual tests
  • Added new benchmarks to generated graphs, if any. More info here.

@github-actions github-actions bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Aug 14, 2024
@notJoon notJoon changed the title Fix(gnovm): Prevent use of blank identifier in named conversion fix(gnovm): Prevent use of blank identifier in named conversion Aug 14, 2024
Copy link

codecov bot commented Aug 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 63.38%. Comparing base (5c876f3) to head (5d4a2b8).
Report is 59 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2699      +/-   ##
==========================================
+ Coverage   63.18%   63.38%   +0.19%     
==========================================
  Files         561      564       +3     
  Lines       78636    78786     +150     
==========================================
+ Hits        49690    49940     +250     
+ Misses      25569    25469     -100     
  Partials     3377     3377              
Flag Coverage Δ
contribs/gnofaucet 14.82% <ø> (ø)
misc/genstd 79.72% <ø> (ø)
tm2 62.39% <ø> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@deelawn deelawn left a comment

Choose a reason for hiding this comment

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

Thanks Joon. Looks mostly good as it fixes the example in the issue. Can you also make a change that panics if the RHS identifier is _ during a DEFINE assignment? Maybe assert it is a named expression and compare its name to blankIdentifier if it is.

package main

func main() {
	a := _
}

And while you are making changes regarding the blank identifier, maybe we want to handle this case as well. It is technically a valid expression, though I can't think of a use case, so I'm not sure if we should fix it for any reason other than to more strictly adhere to the go spec.

package main

type zilch interface{}

func main() {
	_ = zilch(nil)
}

It would be good to get @ltzmaxwell 's review on this as well.

@notJoon notJoon requested a review from deelawn August 28, 2024 14:16
@notJoon
Copy link
Member Author

notJoon commented Aug 28, 2024

@deelawn I've just finished up due to working on other tasks. Now I've modified the code based on your review.

This cases seemed to require handling at the Preprocess function level, so i update that function. Additionally, for the last, case, I also couldn't think of a use case ither, but I thought it would be better to follow the go spec. So I fixed that as well.

@ltzmaxwell
Copy link
Contributor

consider this one:

package main

func main() {
	var a = _
}

gnovm/pkg/gnolang/preprocess.go Outdated Show resolved Hide resolved
@ltzmaxwell
Copy link
Contributor

there should be a better PR title too.

@notJoon
Copy link
Member Author

notJoon commented Sep 2, 2024

@ltzmaxwell I apologize for the delay. I've made some changes to reflect your comments. Still, I can't think of a good idea for the PR title for now. Thanks!

@zivkovicmilos
Copy link
Member

Pinging @deelawn and @ltzmaxwell for visibility

Comment on lines +2905 to +2906
if xt == nil && t == nil {
panic("cannot use _ as value or type")
Copy link
Contributor

@ltzmaxwell ltzmaxwell Oct 2, 2024

Choose a reason for hiding this comment

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

is there any test targeting this?

Copy link
Member Author

Choose a reason for hiding this comment

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

d8d53a1

I wrote new test for isNamedConversion function to replace the tests that were previously run through the machine. I checked that this cover everything.

Copy link
Contributor

@ltzmaxwell ltzmaxwell Oct 30, 2024

Choose a reason for hiding this comment

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

can you provide a file test like this:

package main

type (
	nat  []word
	word int
)

func main() {
	var a nat
	b := []word{0}
	a = b

	println(a)
}

to better illustrate why are these logic(added) needed?
like when xt is nil? when xt and t are both nil?

Copy link
Contributor

Choose a reason for hiding this comment

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

what I mean here is that I did not find a situation that xt and t are both nil in the context of isNamedConversion.

@Kouteki Kouteki added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Oct 3, 2024
@jefft0 jefft0 removed the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Oct 4, 2024
@jefft0
Copy link
Contributor

jefft0 commented Oct 4, 2024

Removed the "review team" label because this is already reviewed by core devs.

Copy link
Member

@omarsy omarsy left a comment

Choose a reason for hiding this comment

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

Maybe you should handle the type case:

package main

func main() {
	var i _
	println(i)
}

@omarsy
Copy link
Member

omarsy commented Oct 18, 2024

I think your PR will fix this issue #1461 too

@Kouteki Kouteki added this to the 🚀 Mainnet launch milestone Oct 21, 2024
@@ -0,0 +1,8 @@
package main
Copy link
Contributor

@ltzmaxwell ltzmaxwell Oct 30, 2024

Choose a reason for hiding this comment

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

the name of test files seems to be inaccurate , "typeassert"?

}

// Error:
// main/files/typeassert12.gno:4:6: cannot use _ as value or type
Copy link
Contributor

Choose a reason for hiding this comment

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

please consider this also:

package main

type S _

func main() {
	println("hey")
}

@@ -3689,6 +3703,11 @@ func tryPredefine(store Store, last BlockNode, d Decl) (un Name) {
})
d.Path = last.GetPathForName(store, d.Name)
case *ValueDecl:
if d.Type != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

if cd, ok := d.(*ValueDecl); ok {
checkValDefineMismatch(cd)

do you think these logics can be integrated to avoid excessive fragmentation?

Comment on lines +2905 to +2906
if xt == nil && t == nil {
panic("cannot use _ as value or type")
Copy link
Contributor

@ltzmaxwell ltzmaxwell Oct 30, 2024

Choose a reason for hiding this comment

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

can you provide a file test like this:

package main

type (
	nat  []word
	word int
)

func main() {
	var a nat
	b := []word{0}
	a = b

	println(a)
}

to better illustrate why are these logic(added) needed?
like when xt is nil? when xt and t are both nil?

@Kouteki Kouteki added the in focus Core team is prioritizing this work label Nov 15, 2024
@Kouteki Kouteki requested review from ltzmaxwell and petar-dambovaliev and removed request for jaekwon, moul, deelawn, thehowl and piux2 November 15, 2024 09:40
@ltzmaxwell
Copy link
Contributor

hey @notJoon, do you have time to take a look at the comments, so we can push this forward? thank you.

if t == nil {
t = xt
}
if xt == nil {
xt = t
}
Copy link
Contributor

Choose a reason for hiding this comment

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

also this.

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

Successfully merging this pull request may close these issues.

blank identifier "_" should not be used as value or type
8 participants