Skip to content

runtime: detect infinite recursion during stack growth #2556

@i3d

Description

@i3d
Contributor
What steps will reproduce the problem?
This simple code reveals an infinite print loop: 
package main 
import "fmt" 
import "log" 
import "runtime/debug" 
var count = 0 
type foo struct { 
  i int 
} 

func (f foo) String() string { 
  count++ 
  if count > 10 { 
    debug.PrintStack() 
    panic("call stack too large") 
  } 
  return fmt.Sprintf("foo@%p value: %d", f, f.i) 
} 

func main() { 
  f := foo{i: 3} 
  // ok 
  log.Println(fmt.Sprintf("foo@%p value: %d", &f, f.i)) 
  // inf loop 
  log.Printf("foo@%p, value: %d\n", f, f.i) 
} 

If you take out the loop protection in the code, you will end up crashing with no system
memory left.

What is the expected output?
There should be a callstack protection so that the program wouldn't chew up all the
system memories.

What do you see instead?
hang and eat up all the memories.

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g, 8g

Which operating system are you using?
Linux 386/amd64

Which revision are you using?  (hg identify)
tip

Please provide any additional information below.

Ian's comment:

I think we should use the depth we are already passing down to catch 
this kind of loop before we eat all of memory.

Activity

robpike

robpike commented on Dec 12, 2011

@robpike
Contributor

Comment 1:

The actual bug in fmt is in issue #2555; once it's fixed (soon) the example will no
longer trigger the fault.
This bug is about general protection against infinite recursion and should have a
simpler example attached, perhaps something like
func main() {
  var a [1e6]int64
  _ = a
  main()
}

Status changed to Accepted.

robpike

robpike commented on Dec 20, 2011

@robpike
Contributor

Comment 2:

as advertised, the original program now works fine, but the related problem of detecting
infinite recursion is worth thinking about.
rsc

rsc commented on Sep 12, 2012

@rsc
Contributor

Comment 5:

Labels changed: added go1.1maybe.

robpike

robpike commented on Mar 7, 2013

@robpike
Contributor

Comment 6:

Labels changed: removed go1.1maybe.

rsc

rsc commented on Mar 12, 2013

@rsc
Contributor

Comment 7:

[The time for maybe has passed.]
rsc

rsc commented on May 29, 2013

@rsc
Contributor

Comment 8:

Issue #5587 has been merged into this issue.

rsc

rsc commented on Jul 30, 2013

@rsc
Contributor

Comment 9:

Status changed to Duplicate.

Merged into issue #4692.

rsc

rsc commented on Aug 16, 2013

@rsc
Contributor

Comment 10:

This issue was closed by revision 757e0de.

Status changed to Fixed.

locked and limited conversation to collaborators on Jun 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rsc@i3d@robpike@gopherbot

        Issue actions

          runtime: detect infinite recursion during stack growth · Issue #2556 · golang/go