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

Infinite Loop Caused by Self-Referencing Struct in Map #2035

Closed
omarsy opened this issue May 4, 2024 · 0 comments · Fixed by #2036
Closed

Infinite Loop Caused by Self-Referencing Struct in Map #2035

omarsy opened this issue May 4, 2024 · 0 comments · Fixed by #2036

Comments

@omarsy
Copy link
Member

omarsy commented May 4, 2024

Infinite Loop Caused by Self-Referencing Struct in Map

Description

During panic situations in our application realm, we've encountered an issue related to a self-referencing struct within a map. When attempting to print the state of the machine during a panic using machine.String(), the application enters an infinite loop.

Steps to reproduce

  1. Go to https://play.gno.land/
  2. Paste this code and run
package main

type Test struct {
    next map[string]*Test
}

func getNewTest() *Test {
    curr := Test{
        next: map[string]*Test{
            "test": {},
        },
    }

    // Assigning a reference to the struct itself within the map
    curr.next["test"] = &curr

    return &curr
}

func main() {
    test := getNewTest()
    panic("here")
}

Logs

runtime: goroutine stack exceeds 1000000000-byte limit
runtime: sp=0x1402c708380 stack=[0x1402c708000, 0x1404c708000]
fatal error: stack overflow

runtime stack:
runtime.throw({0x10300471d?, 0x51001?})
        /usr/local/go/src/runtime/panic.go:1023 +0x40 fp=0x16d56ed70 sp=0x16d56ed40 pc=0x1028cc2d0
runtime.newstack()
        /usr/local/go/src/runtime/stack.go:1103 +0x460 fp=0x16d56ef20 sp=0x16d56ed70 pc=0x1028e9730
runtime.morestack()
        /usr/local/go/src/runtime/asm_arm64.s:341 +0x70 fp=0x16d56ef20 sp=0x16d56ef20 pc=0x102906220

goroutine 1 gp=0x140000021c0 m=0 mp=0x103951d40 [running]:
fmt.(*fmt).padString(0x14014feda40?, {0x140000362e8, 0x14})
        /usr/local/go/src/fmt/format.go:108 +0x29c fp=0x1402c708380 sp=0x1402c708380 pc=0x102988f3c
fmt.(*fmt).fmtS(0x0?, {0x140000362e8?, 0xd0?})
        /usr/local/go/src/fmt/format.go:359 +0x40 fp=0x1402c7083c0 sp=0x1402c708380 pc=0x102989a20
fmt.(*pp).fmtString(0x1402c708448?, {0x140000362e8?, 0x0?}, 0x0?)
        /usr/local/go/src/fmt/print.go:497 +0xe4 fp=0x1402c708410 sp=0x1402c7083c0 pc=0x10298c6f4
fmt.(*pp).printArg(0x14014feda00, {0x103268da0, 0x14014ff5910}, 0x73)
        /usr/local/go/src/fmt/print.go:741 +0x2fc fp=0x1402c7084b0 sp=0x1402c708410 pc=0x10298e1cc
fmt.(*pp).doPrintf(0x14014feda00, {0x102ffbb98, 0x5}, {0x1402c708648, 0x2, 0x2})
        /usr/local/go/src/fmt/print.go:1075 +0x2d8 fp=0x1402c7085b0 sp=0x1402c7084b0 pc=0x102990af8
fmt.Sprintf({0x102ffbb98, 0x5}, {0x1402c708648, 0x2, 0x2})
        /usr/local/go/src/fmt/print.go:239 +0x4c fp=0x1402c708610 sp=0x1402c7085b0 pc=0x10298b00c
github.com/gnolang/gno/gnovm/pkg/gnolang.(*DeclaredType).String(0x1400038ecc0)
        /Users/ghost/Documents/projects/teritori/gno/gnovm/pkg/gnolang/types.go:1442 +0x80 fp=0x1402c708670 sp=0x1402c708610 pc=0x102f50ab0
fmt.(*pp).handleMethods(0x14014fed930, 0x0?)
        /usr/local/go/src/fmt/print.go:673 +0x29c fp=0x1402c708840 sp=0x1402c708670 pc=0x10298da1c
fmt.(*pp).printArg(0x14014fed930, {0x10336fd60, 0x1400038ecc0}, 0x76)
        /usr/local/go/src/fmt/print.go:756 +0x3e0 fp=0x1402c7088e0 sp=0x1402c708840 pc=0x10298e2b0
fmt.(*pp).doPrintf(0x14014fed930, {0x102ffac14, 0x3}, {0x1402c708a78, 0x1, 0x1})
        /usr/local/go/src/fmt/print.go:1075 +0x2d8 fp=0x1402c7089e0 sp=0x1402c7088e0 pc=0x102990af8
fmt.Sprintf({0x102ffac14, 0x3}, {0x1402c708a78, 0x1, 0x1})
        /usr/local/go/src/fmt/print.go:239 +0x4c fp=0x1402c708a40 sp=0x1402c7089e0 pc=0x10298b00c
github.com/gnolang/gno/gnovm/pkg/gnolang.(*PointerType).String(0x1402c708ad8?)
        /Users/ghost/Documents/projects/teritori/gno/gnovm/pkg/gnolang/types.go:603 +0x5c fp=0x14

Proposed solution

I will open a PR with a solution

omarsy added a commit to TERITORI/gno that referenced this issue May 4, 2024
omarsy added a commit to TERITORI/gno that referenced this issue May 4, 2024
omarsy added a commit to TERITORI/gno that referenced this issue May 4, 2024
omarsy added a commit to TERITORI/gno that referenced this issue May 5, 2024
deelawn pushed a commit that referenced this issue May 6, 2024
…edValue (#2036)

Closes #2035. 

<!-- please provide a detailed description of the changes made in this
pull request. -->

<details><summary>Contributors' checklist...</summary>

- [ ] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging a pull request may close this issue.

1 participant