-
Notifications
You must be signed in to change notification settings - Fork 386
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
base: master
Are you sure you want to change the base?
fix(gnovm): correct map key persistence #3127
Conversation
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. |
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. |
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
This one should be fixed in another scope. // update: |
🛠 PR Checks SummaryAll Automated Checks passed. ✅ Manual Checks (for Reviewers):
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:
☑️ Reviewer Actions:
📚 Resources:Debug
|
There was a problem hiding this 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
working on an alternative solution, make this draft now. |
50948d0
to
217dfc3
Compare
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 consistentorigin
.The absolute path consists ofpkgId:blockId:[index]
, providing a globally unique identifier for the pointer.For example: In the code below, the map keyi
, which is a pointer value, has an origin represented as:15bc39c5756bbda67fd0d56bc9e86b29d7b444fc:1:[1]
// PKGPATH: gno.land/r/ptr_mappackage ptr_mapvar (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