For #59670.
Change-Id: I04a17079b351b9b4999ca252825373c17afb8a88
Reviewed-on: https://go-review.googlesource.com/c/go/+/486379
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
        // unnecessarily. See issue #35470.
        p = c.ctxt.StartUnsafePoint(p, c.newprog)
 
-       if framesize <= objabi.StackSmall {
+       if framesize <= abi.StackSmall {
                // small stack: SP < stackguard
                //      CMP     stackguard, SP
                p = obj.Appendp(p, c.newprog)
                p.From.Type = obj.TYPE_REG
                p.From.Reg = REG_R1
                p.Reg = REGSP
-       } else if framesize <= objabi.StackBig {
+       } else if framesize <= abi.StackBig {
                // large stack: SP-framesize < stackguard-StackSmall
                //      MOVW $-(framesize-StackSmall)(SP), R2
                //      CMP stackguard, R2
                p.As = AMOVW
                p.From.Type = obj.TYPE_ADDR
                p.From.Reg = REGSP
-               p.From.Offset = -(int64(framesize) - objabi.StackSmall)
+               p.From.Offset = -(int64(framesize) - abi.StackSmall)
                p.To.Type = obj.TYPE_REG
                p.To.Reg = REG_R2
 
                p.As = ASUB
                p.Scond = C_SBIT
                p.From.Type = obj.TYPE_CONST
-               p.From.Offset = int64(framesize) - objabi.StackSmall
+               p.From.Offset = int64(framesize) - abi.StackSmall
                p.Reg = REGSP
                p.To.Type = obj.TYPE_REG
                p.To.Reg = REG_R2
 
        p = c.ctxt.StartUnsafePoint(p, c.newprog)
 
        q := (*obj.Prog)(nil)
-       if framesize <= objabi.StackSmall {
+       if framesize <= abi.StackSmall {
                // small stack: SP < stackguard
                //      CMP     stackguard, SP
 
                p.From.Type = obj.TYPE_REG
                p.From.Reg = REGRT1
                p.Reg = REGSP
-       } else if framesize <= objabi.StackBig {
+       } else if framesize <= abi.StackBig {
                // large stack: SP-framesize < stackguard-StackSmall
                //      SUB     $(framesize-StackSmall), SP, RT2
                //      CMP     stackguard, RT2
 
                p.As = ASUB
                p.From.Type = obj.TYPE_CONST
-               p.From.Offset = int64(framesize) - objabi.StackSmall
+               p.From.Offset = int64(framesize) - abi.StackSmall
                p.Reg = REGSP
                p.To.Type = obj.TYPE_REG
                p.To.Reg = REGRT2
                p = obj.Appendp(p, c.newprog)
                p.As = ASUBS
                p.From.Type = obj.TYPE_CONST
-               p.From.Offset = int64(framesize) - objabi.StackSmall
+               p.From.Offset = int64(framesize) - abi.StackSmall
                p.Reg = REGSP
                p.To.Type = obj.TYPE_REG
                p.To.Reg = REGRT2
                                }
                        }
 
-                       if p.Mark&LEAF != 0 && c.autosize < objabi.StackSmall {
+                       if p.Mark&LEAF != 0 && c.autosize < abi.StackSmall {
                                // A leaf function with a small stack can be marked
                                // NOSPLIT, avoiding a stack check.
                                p.From.Sym.Set(obj.AttrNoSplit, true)
 
 
 import (
        "cmd/internal/obj"
-       "cmd/internal/objabi"
        "cmd/internal/sys"
        "log"
        "math"
        p = c.ctxt.StartUnsafePoint(p, c.newprog)
 
        var q *obj.Prog
-       if framesize <= objabi.StackSmall {
+       if framesize <= abi.StackSmall {
                // small stack: SP < stackguard
                //      AGTU    SP, stackguard, R19
                p = obj.Appendp(p, c.newprog)
                p.To.Reg = REG_R19
        } else {
                // large stack: SP-framesize < stackguard-StackSmall
-               offset := int64(framesize) - objabi.StackSmall
-               if framesize > objabi.StackBig {
+               offset := int64(framesize) - abi.StackSmall
+               if framesize > abi.StackBig {
                        // Such a large stack we need to protect against underflow.
                        // The runtime guarantees SP > objabi.StackBig, but
                        // framesize is large enough that SP-framesize may
 
 
 import (
        "cmd/internal/obj"
-       "cmd/internal/objabi"
        "cmd/internal/sys"
        "encoding/binary"
        "fmt"
        p = c.ctxt.StartUnsafePoint(p, c.newprog)
 
        var q *obj.Prog
-       if framesize <= objabi.StackSmall {
+       if framesize <= abi.StackSmall {
                // small stack: SP < stackguard
                //      AGTU    SP, stackguard, R1
                p = obj.Appendp(p, c.newprog)
                p.To.Reg = REG_R1
        } else {
                // large stack: SP-framesize < stackguard-StackSmall
-               offset := int64(framesize) - objabi.StackSmall
-               if framesize > objabi.StackBig {
+               offset := int64(framesize) - abi.StackSmall
+               if framesize > abi.StackBig {
                        // Such a large stack we need to protect against underflow.
                        // The runtime guarantees SP > objabi.StackBig, but
                        // framesize is large enough that SP-framesize may
 
                                autosize += int32(c.ctxt.Arch.FixedFrameSize)
                        }
 
-                       if p.Mark&LEAF != 0 && autosize < objabi.StackSmall {
+                       if p.Mark&LEAF != 0 && autosize < abi.StackSmall {
                                // A leaf function with a small stack can be marked
                                // NOSPLIT, avoiding a stack check.
                                p.From.Sym.Set(obj.AttrNoSplit, true)
        p = c.ctxt.StartUnsafePoint(p, c.newprog)
 
        var q *obj.Prog
-       if framesize <= objabi.StackSmall {
+       if framesize <= abi.StackSmall {
                // small stack: SP < stackguard
                //      CMP     stackguard, SP
                p = obj.Appendp(p, c.newprog)
                p.To.Reg = REGSP
        } else {
                // large stack: SP-framesize < stackguard-StackSmall
-               offset := int64(framesize) - objabi.StackSmall
-               if framesize > objabi.StackBig {
+               offset := int64(framesize) - abi.StackSmall
+               if framesize > abi.StackBig {
                        // Such a large stack we need to protect against underflow.
                        // The runtime guarantees SP > objabi.StackBig, but
                        // framesize is large enough that SP-framesize may
 
 
        var to_done, to_more *obj.Prog
 
-       if framesize <= objabi.StackSmall {
+       if framesize <= abi.StackSmall {
                // small stack
                //      // if SP > stackguard { goto done }
                //      BLTU    stackguard, SP, done
                to_done = p
        } else {
                // large stack: SP-framesize < stackguard-StackSmall
-               offset := int64(framesize) - objabi.StackSmall
-               if framesize > objabi.StackBig {
+               offset := int64(framesize) - abi.StackSmall
+               if framesize > abi.StackBig {
                        // Such a large stack we need to protect against underflow.
                        // The runtime guarantees SP > objabi.StackBig, but
                        // framesize is large enough that SP-framesize may
 
                                autosize += int32(c.ctxt.Arch.FixedFrameSize)
                        }
 
-                       if p.Mark&LEAF != 0 && autosize < objabi.StackSmall {
+                       if p.Mark&LEAF != 0 && autosize < abi.StackSmall {
                                // A leaf function with a small stack can be marked
                                // NOSPLIT, avoiding a stack check.
                                p.From.Sym.Set(obj.AttrNoSplit, true)
        // unnecessarily. See issue #35470.
        p = c.ctxt.StartUnsafePoint(p, c.newprog)
 
-       if framesize <= objabi.StackSmall {
+       if framesize <= abi.StackSmall {
                // small stack: SP < stackguard
                //      CMPUBGE stackguard, SP, label-of-call-to-morestack
 
 
        // large stack: SP-framesize < stackguard-StackSmall
 
-       offset := int64(framesize) - objabi.StackSmall
-       if framesize > objabi.StackBig {
+       offset := int64(framesize) - abi.StackSmall
+       if framesize > abi.StackBig {
                // Such a large stack we need to protect against underflow.
                // The runtime guarantees SP > objabi.StackBig, but
                // framesize is large enough that SP-framesize may
 
        "cmd/internal/sys"
        "encoding/binary"
        "fmt"
+       "internal/abi"
        "io"
        "math"
 )
        if needMoreStack {
                p := pMorestack
 
-               if framesize <= objabi.StackSmall {
+               if framesize <= abi.StackSmall {
                        // small stack: SP <= stackguard
                        // Get SP
                        // Get g
                        p = appendp(p, AGet, regAddr(REGG))
                        p = appendp(p, AI32WrapI64)
                        p = appendp(p, AI32Load, constAddr(2*int64(ctxt.Arch.PtrSize))) // G.stackguard0
-                       p = appendp(p, AI32Const, constAddr(framesize-objabi.StackSmall))
+                       p = appendp(p, AI32Const, constAddr(framesize-abi.StackSmall))
                        p = appendp(p, AI32Add)
                        p = appendp(p, AI32LeU)
                }
 
        }
 
        // TODO(rsc): Remove 'ctxt.Arch.Family == sys.AMD64 &&'.
-       if ctxt.Arch.Family == sys.AMD64 && autoffset < objabi.StackSmall && !p.From.Sym.NoSplit() {
+       if ctxt.Arch.Family == sys.AMD64 && autoffset < abi.StackSmall && !p.From.Sym.NoSplit() {
                leaf := true
        LeafSearch:
                for q := p; q != nil; q = q.Link {
                                }
                                fallthrough
                        case obj.ADUFFCOPY, obj.ADUFFZERO:
-                               if autoffset >= objabi.StackSmall-8 {
+                               if autoffset >= abi.StackSmall-8 {
                                        leaf = false
                                        break LeafSearch
                                }
        p, rg = loadG(ctxt, cursym, p, newprog)
 
        var q1 *obj.Prog
-       if framesize <= objabi.StackSmall {
+       if framesize <= abi.StackSmall {
                // small stack: SP <= stackguard
                //      CMPQ SP, stackguard
                p = obj.Appendp(p, newprog)
                // cleared, but we'll still call morestack, which will double the stack
                // unnecessarily. See issue #35470.
                p = ctxt.StartUnsafePoint(p, newprog)
-       } else if framesize <= objabi.StackBig {
+       } else if framesize <= abi.StackBig {
                // large stack: SP-framesize <= stackguard-StackSmall
                //      LEAQ -xxx(SP), tmp
                //      CMPQ tmp, stackguard
                p.As = lea
                p.From.Type = obj.TYPE_MEM
                p.From.Reg = REG_SP
-               p.From.Offset = -(int64(framesize) - objabi.StackSmall)
+               p.From.Offset = -(int64(framesize) - abi.StackSmall)
                p.To.Type = obj.TYPE_REG
                p.To.Reg = tmp
 
                p = obj.Appendp(p, newprog)
                p.As = sub
                p.From.Type = obj.TYPE_CONST
-               p.From.Offset = int64(framesize) - objabi.StackSmall
+               p.From.Offset = int64(framesize) - abi.StackSmall
                p.To.Type = obj.TYPE_REG
                p.To.Reg = tmp
 
 
 
 package objabi
 
-import "internal/buildcfg"
+import (
+       "internal/abi"
+       "internal/buildcfg"
+)
 
 // For the linkers. Must match Go definitions.
 
 const (
        STACKSYSTEM = 0
        StackSystem = STACKSYSTEM
-       StackBig    = 4096
-       StackSmall  = 128
 )
 
 func StackLimit(race bool) int {
        // This arithmetic must match that in runtime/stack.go:{_StackGuard,_StackLimit}.
        stackGuard := 928*stackGuardMultiplier(race) + StackSystem
-       stackLimit := stackGuard - StackSystem - StackSmall
+       stackLimit := stackGuard - StackSystem - abi.StackSmall
        return stackLimit
 }
 
 
--- /dev/null
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package abi
+
+const (
+       // We have three different sequences for stack bounds checks, depending on
+       // whether the stack frame of a function is small, big, or huge.
+
+       // After a stack split check the SP is allowed to be StackSmall bytes below
+       // the stack guard.
+       //
+       // Functions that need frames <= StackSmall can perform the stack check
+       // using a single comparison directly between the stack guard and the SP
+       // because we ensure that StackSmall bytes of stack space are available
+       // beyond the stack guard.
+       StackSmall = 128
+
+       // Functions that need frames <= StackBig can assume that neither
+       // SP-framesize nor stackGuard-StackSmall will underflow, and thus use a
+       // more efficient check. In order to ensure this, StackBig must be <= the
+       // size of the unmapped space at zero.
+       StackBig = 4096
+)
 
        _FixedStack6 = _FixedStack5 | (_FixedStack5 >> 16)
        _FixedStack  = _FixedStack6 + 1
 
-       // Functions that need frames bigger than this use an extra
-       // instruction to do the stack split check, to avoid overflow
-       // in case SP - framesize wraps below zero.
-       // This value can be no bigger than the size of the unmapped
-       // space at zero.
-       _StackBig = 4096
-
        // The stack guard is a pointer this many bytes above the
        // bottom of the stack.
        //
        // This arithmetic must match that in cmd/internal/objabi/stack.go:StackLimit.
        _StackGuard = 928*sys.StackGuardMultiplier + _StackSystem
 
-       // After a stack split check the SP is allowed to be this
-       // many bytes below the stack guard. This saves an instruction
-       // in the checking sequence for tiny frames.
-       _StackSmall = 128
-
        // The maximum number of bytes that a chain of NOSPLIT
        // functions can use.
        // This arithmetic must match that in cmd/internal/objabi/stack.go:StackLimit.
-       _StackLimit = _StackGuard - _StackSystem - _StackSmall
+       _StackLimit = _StackGuard - _StackSystem - abi.StackSmall
 )
 
 const (