Skip to content

fix(gnovm): reclaim stored key object on delete() of object-keyed map#5882

Open
omarsy wants to merge 1 commit into
gnolang:masterfrom
omarsy:fix/delete-objkey-leak
Open

fix(gnovm): reclaim stored key object on delete() of object-keyed map#5882
omarsy wants to merge 1 commit into
gnolang:masterfrom
omarsy:fix/delete-objkey-leak

Conversation

@omarsy

@omarsy omarsy commented Jul 2, 2026

Copy link
Copy Markdown
Member

Fixes #5881.

Problem

delete(m, key) on a realm map with array/struct keys detached the argument key (arg1.Deref()) from the realm via DidUpdate, not the distinct stored key object the map holds (an iv.Copy child). The stored key kept RefCount == 1, was never marked deleted, and was never removed from the store — realm storage grows across add/delete cycles and the key's storage deposit is never refunded. Deterministic (no consensus impact); the value side was already correct.

Fix

MapValue.DeleteForKey now returns the stored key TypedValue it removed; the delete builtin marks that stored key (not the argument key) in DidUpdate. DeleteForKey has a single caller, so the signature change is contained. Value handling unchanged.

Test

gnovm/tests/files/zrealm_map5.gno: init() persists a map[[1]int]int, main() deletes one entry; the finalize golden now emits d[keyOID](-213) for the reclaimed key — before the fix the map entry was dropped (u[map]) but the key object was never deleted (no d[...]). Verified red→green (the golden mismatches on unfixed code). Existing zrealm_map* / map / delete filetests pass; build + vet clean.

🤖 Generated with Claude Code

delete(m, key) on a realm map with array/struct keys detached the ARGUMENT key from the realm instead of the STORED key object the map held (an iv.Copy child). The stored key was therefore never DecRef'd, never marked deleted, and never removed from the store — a deterministic, unbounded on-chain storage leak (and unrefunded storage deposit) for any realm that add/deletes an array- or struct-keyed persistent map.

DeleteForKey now returns the stored key TypedValue it removed; the delete builtin marks that stored key (not the argument key) in DidUpdate. Single caller; value path unchanged.

Regression zrealm_map5.gno: init() persists a map[[1]int]int, main() deletes one entry; the finalize golden now emits d[keyOID](-213) for the reclaimed key. Verified red->green: buggy code omits the d[...] deletion, so the golden mismatches.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions github-actions Bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Jul 2, 2026
@Gno2D2 Gno2D2 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Jul 2, 2026
@Gno2D2

Gno2D2 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

🛠 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)
🟢 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: omarsy/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
    │       ├── 🟢 User davd-gzl already reviewed PR 5882 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

@omarsy omarsy requested review from a team, ltzmaxwell and thehowl July 2, 2026 17:33

@davd-gzl davd-gzl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good. Verified on e980213: reverting to mark the argument key instead of the stored key drops the d[...:8](-213) reclaim from the finalize golden, so this frees the leaked key.

Full review: https://github.com/samouraiworld/gno-agent-workspace/blob/main/reviews/pr/5xxx/5882-reclaim-stored-map-key/1-e98021315/review_claude-opus-4-8_davd-gzl.md

Comment on lines +828 to 840
func (mv *MapValue) DeleteForKey(m *Machine, store Store, key *TypedValue) (deletedKey *TypedValue) {
// if key is NaN, do nothing.
kmk, isNaN := key.ComputeMapKey(m, store, false)
if isNaN {
return
return nil
}
if mli, ok := mv.vmap[kmk]; ok {
mv.List.Remove(mli)
delete(mv.vmap, kmk)
return &mli.Key
}
return nil
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

DeleteForKey has the machine and could mark the removed key itself instead of returning it for the builtin. Is that deliberate, to keep DidUpdate out of MapValue?

@Gno2D2 Gno2D2 removed the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Jul 6, 2026
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

Development

Successfully merging this pull request may close these issues.

delete() on an array/struct-keyed realm map leaks the stored key object (storage not reclaimed)

3 participants