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: don't pass value types by reference #1263

Merged
merged 6 commits into from
Nov 7, 2023
Merged

fix: don't pass value types by reference #1263

merged 6 commits into from
Nov 7, 2023

Conversation

deelawn
Copy link
Contributor

@deelawn deelawn commented Oct 19, 2023

Addresses #1096

These changes ensure that passing non-primitive value types to a function do not result in the modification of the original value, even if the value type is represented as a pointer inside the VM.

@github-actions github-actions bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Oct 19, 2023
@codecov
Copy link

codecov bot commented Oct 19, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (a88e3e3) 55.70% compared to head (3da4ef1) 55.71%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1263      +/-   ##
==========================================
+ Coverage   55.70%   55.71%   +0.01%     
==========================================
  Files         421      421              
  Lines       65555    65562       +7     
==========================================
+ Hits        36515    36528      +13     
  Misses      26177    26177              
+ Partials     2863     2857       -6     
Files Coverage Δ
gnovm/pkg/gnolang/op_call.go 58.55% <100.00%> (ø)
gnovm/pkg/gnolang/values.go 42.56% <100.00%> (+0.23%) ⬆️

... and 8 files with indirect coverage changes

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

@deelawn deelawn changed the title fixed issue where values were being changed as if by reference fix: don't pass value types by reference Oct 20, 2023
@deelawn deelawn marked this pull request as ready for review October 20, 2023 14:17
@deelawn deelawn requested review from jaekwon, moul and a team as code owners October 20, 2023 14:17
@mvertes
Copy link
Contributor

mvertes commented Oct 21, 2023

The solution you provide is correct to me, and I approve it 👏 .

Regarding the test case, it would be better if the code was compatible with pure Go, which is not at the moment due to the use of println which is more limited in pure Go. It is certainly fixable easily.

Copy link
Member

@zivkovicmilos zivkovicmilos left a comment

Choose a reason for hiding this comment

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

Looks great 💯

Thank you for the fix 🙏

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.

LGTM.

requesting review by @piux2 or @jaekwon

@thehowl
Copy link
Member

thehowl commented Oct 23, 2023

Regarding the test case, it would be better if the code was compatible with pure Go, which is not at the moment due to the use of println which is more limited in pure Go. It is certainly fixable easily.

the "weird printing" of println is present in a lot of filetests (rg -A5 '// Output:$' gnovm/tests) and I think it's a bit out of scope for this PR, so I'd say to do that elsewhere.

you're welcome to create an issue :)

@piux2
Copy link
Contributor

piux2 commented Oct 24, 2023

The fix works well. Since the fix is in op_call during VM execution and involves large number copying, maybe @jaekwon can help identify if it's the best place for the fix and any other related areas that need attention.

@jaekwon
Copy link
Contributor

jaekwon commented Nov 7, 2023

I think it's the right place.
Stating the obvious, but .Assign() calls Copy() too, so args for function calls should also.
On the other hand I return values don't need to be Copy()'d separately unless they are likewise assigned.
So this means function chaining doesn't result in unnecessary copies for throwaway values.

op_eval.go for *CallExpr which invokes OpPrecall doesn't copy, it just evaluates.
OpPrecall doesn't do any copying, it just digs into the value stack to fetch the function.
OpCall seems like the right place to do it; after that is OpBody, and by that point the input args should have been fully prepared.

Copy link
Contributor

@jaekwon jaekwon left a comment

Choose a reason for hiding this comment

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

LGTM

@deelawn deelawn merged commit 1732214 into master Nov 7, 2023
185 checks passed
@deelawn deelawn deleted the bug/1096 branch November 7, 2023 16:17
gfanton pushed a commit to gfanton/gno that referenced this pull request Nov 9, 2023
Addresses gnolang#1096

These changes ensure that passing non-primitive value types to a
function do not result in the modification of the original value, even
if the value type is represented as a pointer inside the VM.
moul pushed a commit to moul/gno that referenced this pull request Nov 14, 2023
Addresses gnolang#1096

These changes ensure that passing non-primitive value types to a
function do not result in the modification of the original value, even
if the value type is represented as a pointer inside the VM.
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: 🚀 Needed for Launch
Archived in project
7 participants