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): correct map key persistence #3127

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

Conversation

ltzmaxwell
Copy link
Contributor

@ltzmaxwell ltzmaxwell commented Nov 14, 2024

update:

closes: #2060 and some other issues, by:

The main issue is that after the pointer value is retrieved from the store, the TV field is not correctly filled, it leads to inconsistent map key calculation. this PR solves this problem.

===========================================================
closes: #2060 and some other issues, by:

- correct map key computation when it's a pointer;
- make key object owned by map value.

The root cause of the issue lies in how the map key is calculated when using a pointer type. Currently, the memory address of the pointer value is used to generate the map key. However, this approach is inconsistent between the VM space and storage space. As a result, if a map key derived from a pointer is persisted and later restored, the two keys do not match.

The proposed solution is to derive an immutable identifier for pointer values. This involves calculating an absolute path for the variables that the pointer value references, ensuring a corresponding and consistent origin.

The absolute path consists of pkgId:blockId:[index], providing a globally unique identifier for the pointer.

For example: In the code below, the map key i, which is a pointer value, has an origin represented as: 15bc39c5756bbda67fd0d56bc9e86b29d7b444fc:1:[1]

// PKGPATH: gno.land/r/ptr_map
package ptr_map

var (
m = map[*int]int{}
i = new(int)
)

func AddToMap(value int) {
m[i] = value
}

func GetFromMap() int {
return m[i]
}

func init() {
*i = 1
AddToMap(5)
}

// ----above is initialized and persisted before main is executed.

func main() {
r := GetFromMap()
println(r == 5)

*i = 2 // this changes TV, also Base of a pointer value
r = GetFromMap()
println(r == 5)

}~~

// Output:
// true
// true

@github-actions github-actions bot added 📦 🤖 gnovm Issues or PRs gnovm related 📦 ⛰️ gno.land Issues or PRs gno.land package related labels Nov 14, 2024
@ltzmaxwell
Copy link
Contributor Author

ltzmaxwell commented Nov 15, 2024

another bug identified:

// PKGPATH: gno.land/r/ptr_map
package ptr_map

import "fmt"

type MyStruct struct {
	Name string
}

var (
	m = map[[1]*MyStruct]string{}

	i1   = &MyStruct{Name: "alice"}
	key1 = [1]*MyStruct{i1}
)

func init() {
	m[key1] = "first key"
}

func main() {
	fmt.Println("Value for key1:", m[key1])
}

// Error:
// interface conversion: gnolang.Value is gnolang.RefValue, not *gnolang.ArrayValue

this if fixed.

@ltzmaxwell
Copy link
Contributor Author

ltzmaxwell commented Nov 15, 2024

also this, is it counterintuitive?

// PKGPATH: gno.land/r/ptr_map
package ptr_map

type MyStruct struct {
	Name string
}

var (
	m = map[[1]*MyStruct]string{}

	i1 = &MyStruct{Name: "alice"}
)

func main() {
	// Create an array of pointers to MyStruct
	key1 := [1]*MyStruct{i1}
	m[key1] = "first key"
}
// Error:
// unexpected unreal object

this is fixed but should consider the side effects on storage.

XXX, the right way to fix it should me make map key object owned by map.

Copy link

codecov bot commented Nov 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@Kouteki Kouteki added the in focus Core team is prioritizing this work label Nov 15, 2024
@Kouteki Kouteki added this to the 🚀 Mainnet launch milestone Nov 15, 2024
@ltzmaxwell ltzmaxwell marked this pull request as draft November 18, 2024 03:00
@ltzmaxwell
Copy link
Contributor Author

ltzmaxwell commented Nov 27, 2024

also this, is it counterintuitive?

// PKGPATH: gno.land/r/ptr_map
package ptr_map

type MyStruct struct {
	Name string
}

var (
	m = map[[1]*MyStruct]string{}

	i1 = &MyStruct{Name: "alice"}
)

func main() {
	// Create an array of pointers to MyStruct
	key1 := [1]*MyStruct{i1}
	m[key1] = "first key"
}
// Error:
// unexpected unreal object

this is fixed but should consider the side effects on storage.

XXX, the right way to fix it should me make map key object owned by map.

This one should be fixed in another scope.

// update:
It's fixed in this PR.

@ltzmaxwell ltzmaxwell changed the title WIP fix(gnovm): correct map key persistence fix(gnovm): correct map key persistence Nov 28, 2024
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Nov 30, 2024

🛠 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
└── 🟢 The pull request was created from a fork (head branch repo: ltzmaxwell/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

@ltzmaxwell ltzmaxwell changed the title fix(gnovm): correct map key persistence WIP fix(gnovm): correct map key persistence Dec 4, 2024
@ltzmaxwell ltzmaxwell changed the title WIP fix(gnovm): correct map key persistence fix(gnovm): correct map key persistence Dec 19, 2024
@ltzmaxwell ltzmaxwell marked this pull request as ready for review December 19, 2024 14:12
@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Dec 19, 2024
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.

Still not fully clear, but I'm very tired, so will continue when I can

gnovm/pkg/gnolang/values.go Outdated Show resolved Hide resolved
gnovm/pkg/gnolang/values.go Outdated Show resolved Hide resolved
gnovm/pkg/gnolang/values.go Outdated Show resolved Hide resolved
gnovm/pkg/gnolang/op_expressions.go Outdated Show resolved Hide resolved
@ltzmaxwell
Copy link
Contributor Author

working on an alternative solution, make this draft now.

@ltzmaxwell ltzmaxwell marked this pull request as draft January 3, 2025 09:33
@ltzmaxwell ltzmaxwell changed the title fix(gnovm): correct map key persistence WIP fix(gnovm): correct map key persistence Jan 3, 2025
@ltzmaxwell ltzmaxwell force-pushed the fix/maxwell/mapkey-persistence branch from 50948d0 to 217dfc3 Compare January 6, 2025 13:47
@ltzmaxwell ltzmaxwell changed the title WIP fix(gnovm): correct map key persistence fix(gnovm): correct map key persistence Jan 7, 2025
@ltzmaxwell ltzmaxwell marked this pull request as ready for review January 7, 2025 06:47
@ltzmaxwell
Copy link
Contributor Author

the previous solution was based on an incorrect assumption. The current solution is more accurate. could you please review it again? 🙏 @thehowl @mvertes

@ltzmaxwell ltzmaxwell requested a review from thehowl January 7, 2025 06:51
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 📦 ⛰️ gno.land Issues or PRs gno.land package related 📦 🤖 gnovm Issues or PRs gnovm related 🧾 package/realm Tag used for new Realms or Packages.
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

[GnoVM] bug: inconsistent map key
4 participants