Skip to content

fix(gnovm): deep-copy array elements in ArrayValue.Copy#5445

Open
thehowl wants to merge 2 commits intognolang:masterfrom
thehowl:dev/morgan/fix-array-shallow-copy
Open

fix(gnovm): deep-copy array elements in ArrayValue.Copy#5445
thehowl wants to merge 2 commits intognolang:masterfrom
thehowl:dev/morgan/fix-array-shallow-copy

Conversation

@thehowl
Copy link
Copy Markdown
Member

@thehowl thehowl commented Apr 7, 2026

Closes #5332

ArrayValue.Copy used Go's builtin copy() to duplicate its []TypedValue slice, which copies TypedValue structs by value. For composite element types (structs, nested arrays), this meant both the original and the copy shared the same *StructValue/*ArrayValue pointers, silently corrupting persistent realm state.

The fix mirrors StructValue.Copy, which already calls field.Copy(alloc) per element for exactly this reason. TypedValue.Copy dispatches correctly on the dynamic type: *StructValue and *ArrayValue are deep-copied, while reference-like values (slices, maps, pointers, functions) fall through to the shallow default — so interface-held reference types are handled correctly.

The gas expectation for gnovm/tests/files/gas/nested_alloc.gno is updated: building a 10000-deep [1]interface{}{x} chain now incurs O(n²) copy cost because each assignment deep-copies the entire chain, which is the correct cost under Gno's value semantics for interface-held arrays.

…ayValue.Copy

ArrayValue.Copy used Go's builtin copy() to duplicate its []TypedValue
slice, which copies TypedValue structs bitwise. For composite element
types (structs, nested arrays), this meant both the original and the
copy shared the same *StructValue/*ArrayValue pointers, silently
corrupting persistent realm state.

The fix mirrors StructValue.Copy, which already calls field.Copy(alloc)
per element for exactly this reason. TypedValue.Copy dispatches
correctly on the dynamic type: *StructValue and *ArrayValue are deep-
copied, while reference-like values (slices, maps, pointers, functions)
fall through to the shallow default — so interface-held reference types
are handled correctly.

The gas expectation for gnovm/tests/files/gas/nested_alloc.gno is
updated: building a 10000-deep [1]interface{}{x} chain now incurs O(n²)
copy cost because each assignment deep-copies the entire chain, which is
the correct cost under Gno's value semantics for interface-held arrays.

Fixes NEWTENDG-105.
@Gno2D2
Copy link
Copy Markdown
Collaborator

Gno2D2 commented Apr 7, 2026

🛠 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: thehowl/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

@omarsy
Copy link
Copy Markdown
Member

omarsy commented Apr 7, 2026

I was working on a fix for that #5341

@omarsy
Copy link
Copy Markdown
Member

omarsy commented Apr 7, 2026

I was working on a fix for that #5341

See It was in draft :/

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
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.

If we merge this PR should clause this #5341 and the issue #5332

Co-authored-by: 6h057 <15034695+omarsy@users.noreply.github.com>
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: No status

Development

Successfully merging this pull request may close these issues.

bug: Nested Array Deep-Copy Semantics Broken (Deterministic Shallow Aliasing) in GnoVM

4 participants