Debian Codegraph @

src/runtime/stack.go

raw
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package runtime
6
7 import (
8 "internal/abi"
9 "internal/cpu"
10 "internal/goarch"
11 "internal/goos"
12 "internal/runtime/atomic"
13 "internal/runtime/gc"
14 "internal/runtime/sys"
15 "unsafe"
16 )
17
18 /*
19 Stack layout parameters.
20 Included both by runtime (compiled via 6c) and linkers (compiled via gcc).
21
22 The per-goroutine g->stackguard is set to point StackGuard bytes
23 above the bottom of the stack. Each function compares its stack
24 pointer against g->stackguard to check for overflow. To cut one
25 instruction from the check sequence for functions with tiny frames,
26 the stack is allowed to protrude StackSmall bytes below the stack
27 guard. Functions with large frames don't bother with the check and
28 always call morestack. The sequences are (for amd64, others are
29 similar):
30
31 guard = g->stackguard
32 frame = function's stack frame size
33 argsize = size of function arguments (call + return)
34
35 stack frame size <= StackSmall:
36 CMPQ guard, SP
37 JHI 3(PC)
38 MOVQ m->morearg, $(argsize << 32)
39 CALL morestack(SB)
40
41 stack frame size > StackSmall but < StackBig
42 LEAQ (frame-StackSmall)(SP), R0
43 CMPQ guard, R0
44 JHI 3(PC)
45 MOVQ m->morearg, $(argsize << 32)
46 CALL morestack(SB)
47
48 stack frame size >= StackBig:
49 MOVQ m->morearg, $((argsize << 32) | frame)
50 CALL morestack(SB)
51
52 The bottom StackGuard - StackSmall bytes are important: there has
53 to be enough room to execute functions that refuse to check for
54 stack overflow, either because they need to be adjacent to the
55 actual caller's frame (deferproc) or because they handle the imminent
56 stack overflow (morestack).
57
58 For example, deferproc might call malloc, which does one of the
59 above checks (without allocating a full frame), which might trigger
60 a call to morestack. This sequence needs to fit in the bottom
61 section of the stack. On amd64, morestack's frame is 40 bytes, and
62 deferproc's frame is 56 bytes. That fits well within the
63 StackGuard - StackSmall bytes at the bottom.
64 The linkers explore all possible call traces involving non-splitting
65 functions to make sure that this limit cannot be violated.
66 */
67
68 const (
69 // stackSystem is a number of additional bytes to add
70 // to each stack below the usual guard area for OS-specific
71 // purposes like signal handling. Used on Windows, Plan 9,
72 // and iOS because they do not use a separate stack.
73 stackSystem = goos.IsWindows*4096 + goos.IsPlan9*512 + goos.IsIos*goarch.IsArm64*1024
74
75 // The minimum size of stack used by Go code
76 stackMin = 2048
77
78 // The minimum stack size to allocate.
79 // The hackery here rounds fixedStack0 up to a power of 2.
80 fixedStack0 = stackMin + stackSystem
81 fixedStack1 = fixedStack0 - 1
82 fixedStack2 = fixedStack1 | (fixedStack1 >> 1)
83 fixedStack3 = fixedStack2 | (fixedStack2 >> 2)
84 fixedStack4 = fixedStack3 | (fixedStack3 >> 4)
85 fixedStack5 = fixedStack4 | (fixedStack4 >> 8)
86 fixedStack6 = fixedStack5 | (fixedStack5 >> 16)
87 fixedStack = fixedStack6 + 1
88
89 // stackNosplit is the maximum number of bytes that a chain of NOSPLIT
90 // functions can use.
91 // This arithmetic must match that in cmd/internal/objabi/stack.go:StackNosplit.
92 stackNosplit = abi.StackNosplitBase * sys.StackGuardMultiplier
93
94 // The stack guard is a pointer this many bytes above the
95 // bottom of the stack.
96 //
97 // The guard leaves enough room for a stackNosplit chain of NOSPLIT calls
98 // plus one stackSmall frame plus stackSystem bytes for the OS.
99 // This arithmetic must match that in cmd/internal/objabi/stack.go:StackLimit.
100 stackGuard = stackNosplit + stackSystem + abi.StackSmall
101 )
102
103 const (
104 // stackDebug == 0: no logging
105 // == 1: logging of per-stack operations
106 // == 2: logging of per-frame operations
107 // == 3: logging of per-word updates
108 // == 4: logging of per-word reads
109 stackDebug = 0
110 stackFromSystem = 0 // allocate stacks from system memory instead of the heap
111 stackFaultOnFree = 0 // old stacks are mapped noaccess to detect use after free
112 stackNoCache = 0 // disable per-P small stack caches
113
114 // check the BP links during traceback.
115 debugCheckBP = false
116 )
117
118 var (
119 stackPoisonCopy = 0 // fill stack that should not be accessed with garbage, to detect bad dereferences during copy
120 )
121
122 const (
123 uintptrMask = 1<<(8*goarch.PtrSize) - 1
124
125 // The values below can be stored to g.stackguard0 to force
126 // the next stack check to fail.
127 // These are all larger than any real SP.
128
129 // Goroutine preemption request.
130 // 0xfffffade in hex.
131 stackPreempt = uintptrMask & -1314
132
133 // Thread is forking. Causes a split stack check failure.
134 // 0xfffffb2e in hex.
135 stackFork = uintptrMask & -1234
136
137 // Force a stack movement. Used for debugging.
138 // 0xfffffeed in hex.
139 stackForceMove = uintptrMask & -275
140
141 // stackPoisonMin is the lowest allowed stack poison value.
142 stackPoisonMin = uintptrMask & -4096
143 )
144
145 // Global pool of spans that have free stacks.
146 // Stacks are assigned an order according to size.
147 //
148 // order = log_2(size/FixedStack)
149 //
150 // There is a free list for each order.
151 var stackpool [_NumStackOrders]struct {
152 item stackpoolItem
153 _ [(cpu.CacheLinePadSize - unsafe.Sizeof(stackpoolItem{})%cpu.CacheLinePadSize) % cpu.CacheLinePadSize]byte
154 }
155
156 type stackpoolItem struct {
157 _ sys.NotInHeap
158 mu mutex
159 span mSpanList
160 }
161
162 // Global pool of large stack spans.
163 var stackLarge struct {
164 lock mutex
165 free [heapAddrBits - gc.PageShift]mSpanList // free lists by log_2(s.npages)
166 }
167
168 func stackinit() {
169 if _StackCacheSize&pageMask != 0 {
170 throw("cache size must be a multiple of page size")
171 }
172 for i := range stackpool {
173 stackpool[i].item.span.init()
174 lockInit(&stackpool[i].item.mu, lockRankStackpool)
175 }
176 for i := range stackLarge.free {
177 stackLarge.free[i].init()
178 lockInit(&stackLarge.lock, lockRankStackLarge)
179 }
180 }
181
182 // stacklog2 returns ⌊log_2(n)⌋.
183 func stacklog2(n uintptr) int {
184 log2 := 0
185 for n > 1 {
186 n >>= 1
187 log2++
188 }
189 return log2
190 }
191
192 // Allocates a stack from the free pool. Must be called with
193 // stackpool[order].item.mu held.
194 func stackpoolalloc(order uint8) gclinkptr {
195 list := &stackpool[order].item.span
196 s := list.first
197 lockWithRankMayAcquire(&mheap_.lock, lockRankMheap)
198 if s == nil {
199 // no free stacks. Allocate another span worth.
200 s = mheap_.allocManual(_StackCacheSize>>gc.PageShift, spanAllocStack)
201 if s == nil {
202 throw("out of memory")
203 }
204 if s.allocCount != 0 {
205 throw("bad allocCount")
206 }
207 if s.manualFreeList.ptr() != nil {
208 throw("bad manualFreeList")
209 }
210 osStackAlloc(s)
211 s.elemsize = fixedStack << order
212 for i := uintptr(0); i < _StackCacheSize; i += s.elemsize {
213 x := gclinkptr(s.base() + i)
214 if valgrindenabled {
215 // The address of x.ptr() becomes the base of stacks. We need to
216 // mark it allocated here and in stackfree and stackpoolfree, and free'd in
217 // stackalloc in order to avoid overlapping allocations and
218 // uninitialized memory errors in valgrind.
219 valgrindMalloc(unsafe.Pointer(x.ptr()), unsafe.Sizeof(x.ptr()))
220 }
221 x.ptr().next = s.manualFreeList
222 s.manualFreeList = x
223 }
224 list.insert(s)
225 }
226 x := s.manualFreeList
227 if x.ptr() == nil {
228 throw("span has no free stacks")
229 }
230 s.manualFreeList = x.ptr().next
231 s.allocCount++
232 if s.manualFreeList.ptr() == nil {
233 // all stacks in s are allocated.
234 list.remove(s)
235 }
236 return x
237 }
238
239 // Adds stack x to the free pool. Must be called with stackpool[order].item.mu held.
240 func stackpoolfree(x gclinkptr, order uint8) {
241 s := spanOfUnchecked(uintptr(x))
242 if s.state.get() != mSpanManual {
243 throw("freeing stack not in a stack span")
244 }
245 if s.manualFreeList.ptr() == nil {
246 // s will now have a free stack
247 stackpool[order].item.span.insert(s)
248 }
249 x.ptr().next = s.manualFreeList
250 s.manualFreeList = x
251 s.allocCount--
252 if gcphase == _GCoff && s.allocCount == 0 {
253 // Span is completely free. Return it to the heap
254 // immediately if we're sweeping.
255 //
256 // If GC is active, we delay the free until the end of
257 // GC to avoid the following type of situation:
258 //
259 // 1) GC starts, scans a SudoG but does not yet mark the SudoG.elem pointer
260 // 2) The stack that pointer points to is copied
261 // 3) The old stack is freed
262 // 4) The containing span is marked free
263 // 5) GC attempts to mark the SudoG.elem pointer. The
264 // marking fails because the pointer looks like a
265 // pointer into a free span.
266 //
267 // By not freeing, we prevent step #4 until GC is done.
268 stackpool[order].item.span.remove(s)
269 s.manualFreeList = 0
270 osStackFree(s)
271 mheap_.freeManual(s, spanAllocStack)
272 }
273 }
274
275 // stackcacherefill/stackcacherelease implement a global pool of stack segments.
276 // The pool is required to prevent unlimited growth of per-thread caches.
277 //
278 //go:systemstack
279 func stackcacherefill(c *mcache, order uint8) {
280 if stackDebug >= 1 {
281 print("stackcacherefill order=", order, "\n")
282 }
283
284 // Grab some stacks from the global cache.
285 // Grab half of the allowed capacity (to prevent thrashing).
286 var list gclinkptr
287 var size uintptr
288 lock(&stackpool[order].item.mu)
289 for size < _StackCacheSize/2 {
290 x := stackpoolalloc(order)
291 x.ptr().next = list
292 list = x
293 size += fixedStack << order
294 }
295 unlock(&stackpool[order].item.mu)
296 c.stackcache[order].list = list
297 c.stackcache[order].size = size
298 }
299
300 //go:systemstack
301 func stackcacherelease(c *mcache, order uint8) {
302 if stackDebug >= 1 {
303 print("stackcacherelease order=", order, "\n")
304 }
305 x := c.stackcache[order].list
306 size := c.stackcache[order].size
307 lock(&stackpool[order].item.mu)
308 for size > _StackCacheSize/2 {
309 y := x.ptr().next
310 stackpoolfree(x, order)
311 x = y
312 size -= fixedStack << order
313 }
314 unlock(&stackpool[order].item.mu)
315 c.stackcache[order].list = x
316 c.stackcache[order].size = size
317 }
318
319 //go:systemstack
320 func stackcache_clear(c *mcache) {
321 if stackDebug >= 1 {
322 print("stackcache clear\n")
323 }
324 for order := uint8(0); order < _NumStackOrders; order++ {
325 lock(&stackpool[order].item.mu)
326 x := c.stackcache[order].list
327 for x.ptr() != nil {
328 y := x.ptr().next
329 stackpoolfree(x, order)
330 x = y
331 }
332 c.stackcache[order].list = 0
333 c.stackcache[order].size = 0
334 unlock(&stackpool[order].item.mu)
335 }
336 }
337
338 // stackalloc allocates an n byte stack.
339 //
340 // stackalloc must run on the system stack because it uses per-P
341 // resources and must not split the stack.
342 //
343 //go:systemstack
344 func stackalloc(n uint32) stack {
345 // Stackalloc must be called on scheduler stack, so that we
346 // never try to grow the stack during the code that stackalloc runs.
347 // Doing so would cause a deadlock (issue 1547).
348 thisg := getg()
349 if thisg != thisg.m.g0 {
350 throw("stackalloc not on scheduler stack")
351 }
352 if n&(n-1) != 0 {
353 throw("stack size not a power of 2")
354 }
355 if stackDebug >= 1 {
356 print("stackalloc ", n, "\n")
357 }
358
359 if debug.efence != 0 || stackFromSystem != 0 {
360 n = uint32(alignUp(uintptr(n), physPageSize))
361 v := sysAlloc(uintptr(n), &memstats.stacks_sys, "goroutine stack (system)")
362 if v == nil {
363 throw("out of memory (stackalloc)")
364 }
365 return stack{uintptr(v), uintptr(v) + uintptr(n)}
366 }
367
368 // Small stacks are allocated with a fixed-size free-list allocator.
369 // If we need a stack of a bigger size, we fall back on allocating
370 // a dedicated span.
371 var v unsafe.Pointer
372 if n < fixedStack<<_NumStackOrders && n < _StackCacheSize {
373 order := uint8(0)
374 n2 := n
375 for n2 > fixedStack {
376 order++
377 n2 >>= 1
378 }
379 var x gclinkptr
380 if stackNoCache != 0 || thisg.m.p == 0 || thisg.m.preemptoff != "" {
381 // thisg.m.p == 0 can happen in the guts of exitsyscall
382 // or procresize. Just get a stack from the global pool.
383 // Also don't touch stackcache during gc
384 // as it's flushed concurrently.
385 lock(&stackpool[order].item.mu)
386 x = stackpoolalloc(order)
387 unlock(&stackpool[order].item.mu)
388 } else {
389 c := thisg.m.p.ptr().mcache
390 x = c.stackcache[order].list
391 if x.ptr() == nil {
392 stackcacherefill(c, order)
393 x = c.stackcache[order].list
394 }
395 c.stackcache[order].list = x.ptr().next
396 c.stackcache[order].size -= uintptr(n)
397 }
398 if valgrindenabled {
399 // We're about to allocate the stack region starting at x.ptr().
400 // To prevent valgrind from complaining about overlapping allocations,
401 // we need to mark the (previously allocated) memory as free'd.
402 valgrindFree(unsafe.Pointer(x.ptr()))
403 }
404 v = unsafe.Pointer(x)
405 } else {
406 var s *mspan
407 npage := uintptr(n) >> gc.PageShift
408 log2npage := stacklog2(npage)
409
410 // Try to get a stack from the large stack cache.
411 lock(&stackLarge.lock)
412 if !stackLarge.free[log2npage].isEmpty() {
413 s = stackLarge.free[log2npage].first
414 stackLarge.free[log2npage].remove(s)
415 }
416 unlock(&stackLarge.lock)
417
418 lockWithRankMayAcquire(&mheap_.lock, lockRankMheap)
419
420 if s == nil {
421 // Allocate a new stack from the heap.
422 s = mheap_.allocManual(npage, spanAllocStack)
423 if s == nil {
424 throw("out of memory")
425 }
426 osStackAlloc(s)
427 s.elemsize = uintptr(n)
428 }
429 v = unsafe.Pointer(s.base())
430 }
431
432 if traceAllocFreeEnabled() {
433 trace := traceAcquire()
434 if trace.ok() {
435 trace.GoroutineStackAlloc(uintptr(v), uintptr(n))
436 traceRelease(trace)
437 }
438 }
439 if raceenabled {
440 racemalloc(v, uintptr(n))
441 }
442 if msanenabled {
443 msanmalloc(v, uintptr(n))
444 }
445 if asanenabled {
446 asanunpoison(v, uintptr(n))
447 }
448 if valgrindenabled {
449 valgrindMalloc(v, uintptr(n))
450 }
451 if stackDebug >= 1 {
452 print(" allocated ", v, "\n")
453 }
454 return stack{uintptr(v), uintptr(v) + uintptr(n)}
455 }
456
457 // stackfree frees an n byte stack allocation at stk.
458 //
459 // stackfree must run on the system stack because it uses per-P
460 // resources and must not split the stack.
461 //
462 //go:systemstack
463 func stackfree(stk stack) {
464 gp := getg()
465 v := unsafe.Pointer(stk.lo)
466 n := stk.hi - stk.lo
467 if n&(n-1) != 0 {
468 throw("stack not a power of 2")
469 }
470 if stk.lo+n < stk.hi {
471 throw("bad stack size")
472 }
473 if stackDebug >= 1 {
474 println("stackfree", v, n)
475 memclrNoHeapPointers(v, n) // for testing, clobber stack data
476 }
477 if debug.efence != 0 || stackFromSystem != 0 {
478 if debug.efence != 0 || stackFaultOnFree != 0 {
479 sysFault(v, n)
480 } else {
481 sysFree(v, n, &memstats.stacks_sys)
482 }
483 return
484 }
485 if traceAllocFreeEnabled() {
486 trace := traceAcquire()
487 if trace.ok() {
488 trace.GoroutineStackFree(uintptr(v))
489 traceRelease(trace)
490 }
491 }
492 if msanenabled {
493 msanfree(v, n)
494 }
495 if asanenabled {
496 asanpoison(v, n)
497 }
498 if valgrindenabled {
499 valgrindFree(v)
500 }
501 if n < fixedStack<<_NumStackOrders && n < _StackCacheSize {
502 order := uint8(0)
503 n2 := n
504 for n2 > fixedStack {
505 order++
506 n2 >>= 1
507 }
508 x := gclinkptr(v)
509 if stackNoCache != 0 || gp.m.p == 0 || gp.m.preemptoff != "" {
510 lock(&stackpool[order].item.mu)
511 if valgrindenabled {
512 // x.ptr() is the head of the list of free stacks, and will be used
513 // when allocating a new stack, so it has to be marked allocated.
514 valgrindMalloc(unsafe.Pointer(x.ptr()), unsafe.Sizeof(x.ptr()))
515 }
516 stackpoolfree(x, order)
517 unlock(&stackpool[order].item.mu)
518 } else {
519 c := gp.m.p.ptr().mcache
520 if c.stackcache[order].size >= _StackCacheSize {
521 stackcacherelease(c, order)
522 }
523 if valgrindenabled {
524 // x.ptr() is the head of the list of free stacks, and will
525 // be used when allocating a new stack, so it has to be
526 // marked allocated.
527 valgrindMalloc(unsafe.Pointer(x.ptr()), unsafe.Sizeof(x.ptr()))
528 }
529 x.ptr().next = c.stackcache[order].list
530 c.stackcache[order].list = x
531 c.stackcache[order].size += n
532 }
533 } else {
534 s := spanOfUnchecked(uintptr(v))
535 if s.state.get() != mSpanManual {
536 println(hex(s.base()), v)
537 throw("bad span state")
538 }
539 if gcphase == _GCoff {
540 // Free the stack immediately if we're
541 // sweeping.
542 osStackFree(s)
543 mheap_.freeManual(s, spanAllocStack)
544 } else {
545 // If the GC is running, we can't return a
546 // stack span to the heap because it could be
547 // reused as a heap span, and this state
548 // change would race with GC. Add it to the
549 // large stack cache instead.
550 log2npage := stacklog2(s.npages)
551 lock(&stackLarge.lock)
552 stackLarge.free[log2npage].insert(s)
553 unlock(&stackLarge.lock)
554 }
555 }
556 }
557
558 var maxstacksize uintptr = 1 << 20 // enough until runtime.main sets it for real
559
560 var maxstackceiling = maxstacksize
561
562 var ptrnames = []string{
563 0: "scalar",
564 1: "ptr",
565 }
566
567 // Stack frame layout
568 //
569 // (x86)
570 // +------------------+
571 // | args from caller |
572 // +------------------+ <- frame->argp
573 // | return address |
574 // +------------------+
575 // | caller's BP (*) | (*) if framepointer_enabled && varp > sp
576 // +------------------+ <- frame->varp
577 // | locals |
578 // +------------------+
579 // | args to callee |
580 // +------------------+ <- frame->sp
581 //
582 // (arm)
583 // +------------------+
584 // | args from caller |
585 // +------------------+ <- frame->argp
586 // | caller's retaddr |
587 // +------------------+
588 // | caller's FP (*) | (*) on ARM64, if framepointer_enabled && varp > sp
589 // +------------------+ <- frame->varp
590 // | locals |
591 // +------------------+
592 // | args to callee |
593 // +------------------+
594 // | return address |
595 // +------------------+ <- frame->sp
596 //
597 // varp > sp means that the function has a frame;
598 // varp == sp means frameless function.
599
600 type adjustinfo struct {
601 old stack
602 delta uintptr // ptr distance from old to new stack (newbase - oldbase)
603
604 // sghi is the highest sudog.elem on the stack.
605 sghi uintptr
606 }
607
608 // adjustpointer checks whether *vpp is in the old stack described by adjinfo.
609 // If so, it rewrites *vpp to point into the new stack.
610 func adjustpointer(adjinfo *adjustinfo, vpp unsafe.Pointer) {
611 pp := (*uintptr)(vpp)
612 p := *pp
613 if stackDebug >= 4 {
614 print(" ", pp, ":", hex(p), "\n")
615 }
616 if valgrindenabled {
617 // p is a pointer on a stack, it is inherently initialized, as
618 // everything on the stack is, but valgrind for _some unknown reason_
619 // sometimes thinks it's uninitialized, and flags operations on p below
620 // as uninitialized. We just initialize it if valgrind thinks its
621 // uninitialized.
622 //
623 // See go.dev/issues/73801.
624 valgrindMakeMemDefined(unsafe.Pointer(&p), unsafe.Sizeof(&p))
625 }
626 if adjinfo.old.lo <= p && p < adjinfo.old.hi {
627 *pp = p + adjinfo.delta
628 if stackDebug >= 3 {
629 print(" adjust ptr ", pp, ":", hex(p), " -> ", hex(*pp), "\n")
630 }
631 }
632 }
633
634 // Information from the compiler about the layout of stack frames.
635 // Note: this type must agree with reflect.bitVector.
636 type bitvector struct {
637 n int32 // # of bits
638 bytedata *uint8
639 }
640
641 // ptrbit returns the i'th bit in bv.
642 // ptrbit is less efficient than iterating directly over bitvector bits,
643 // and should only be used in non-performance-critical code.
644 // See adjustpointers for an example of a high-efficiency walk of a bitvector.
645 func (bv *bitvector) ptrbit(i uintptr) uint8 {
646 b := *(addb(bv.bytedata, i/8))
647 return (b >> (i % 8)) & 1
648 }
649
650 // bv describes the memory starting at address scanp.
651 // Adjust any pointers contained therein.
652 func adjustpointers(scanp unsafe.Pointer, bv *bitvector, adjinfo *adjustinfo, f funcInfo) {
653 minp := adjinfo.old.lo
654 maxp := adjinfo.old.hi
655 delta := adjinfo.delta
656 num := uintptr(bv.n)
657 // If this frame might contain channel receive slots, use CAS
658 // to adjust pointers. If the slot hasn't been received into
659 // yet, it may contain stack pointers and a concurrent send
660 // could race with adjusting those pointers. (The sent value
661 // itself can never contain stack pointers.)
662 useCAS := uintptr(scanp) < adjinfo.sghi
663 for i := uintptr(0); i < num; i += 8 {
664 if stackDebug >= 4 {
665 for j := uintptr(0); j < 8; j++ {
666 print(" ", add(scanp, (i+j)*goarch.PtrSize), ":", ptrnames[bv.ptrbit(i+j)], ":", hex(*(*uintptr)(add(scanp, (i+j)*goarch.PtrSize))), " # ", i, " ", *addb(bv.bytedata, i/8), "\n")
667 }
668 }
669 b := *(addb(bv.bytedata, i/8))
670 for b != 0 {
671 j := uintptr(sys.TrailingZeros8(b))
672 b &= b - 1
673 pp := (*uintptr)(add(scanp, (i+j)*goarch.PtrSize))
674 retry:
675 p := *pp
676 if f.valid() && 0 < p && p < minLegalPointer && debug.invalidptr != 0 {
677 // Looks like a junk value in a pointer slot.
678 // Live analysis wrong?
679 getg().m.traceback = 2
680 print("runtime: bad pointer in frame ", funcname(f), " at ", pp, ": ", hex(p), "\n")
681 throw("invalid pointer found on stack")
682 }
683 if minp <= p && p < maxp {
684 if stackDebug >= 3 {
685 print("adjust ptr ", hex(p), " ", funcname(f), "\n")
686 }
687 if useCAS {
688 ppu := (*unsafe.Pointer)(unsafe.Pointer(pp))
689 if !atomic.Casp1(ppu, unsafe.Pointer(p), unsafe.Pointer(p+delta)) {
690 goto retry
691 }
692 } else {
693 *pp = p + delta
694 }
695 }
696 }
697 }
698 }
699
700 // Note: the argument/return area is adjusted by the callee.
701 func adjustframe(frame *stkframe, adjinfo *adjustinfo) {
702 // Adjust saved frame pointer if there is one.
703 if (goarch.ArchFamily == goarch.AMD64 || goarch.ArchFamily == goarch.ARM64) && frame.argp-frame.varp == 2*goarch.PtrSize {
704 if stackDebug >= 3 {
705 print(" saved bp\n")
706 }
707 if debugCheckBP {
708 // Frame pointers should always point to the next higher frame on
709 // the Go stack (or be nil, for the top frame on the stack).
710 bp := *(*uintptr)(unsafe.Pointer(frame.varp))
711 if bp != 0 && (bp < adjinfo.old.lo || bp >= adjinfo.old.hi) {
712 println("runtime: found invalid frame pointer")
713 print("bp=", hex(bp), " min=", hex(adjinfo.old.lo), " max=", hex(adjinfo.old.hi), "\n")
714 throw("bad frame pointer")
715 }
716 }
717 // On AMD64, this is the caller's frame pointer saved in the current
718 // frame.
719 // On ARM64, this is the frame pointer of the caller's caller saved
720 // by the caller in its frame (one word below its SP).
721 adjustpointer(adjinfo, unsafe.Pointer(frame.varp))
722 }
723 if goarch.ArchFamily == goarch.ARM64 && isInjectedCall(frame.fn.funcID) {
724 // If this is an injected call on arm64, then we need to adjust
725 // the frame pointer saved by the original function into which
726 // the call was injected. Normally this would be handled when
727 // adjusting the callee's frame or in adjustctxt. But when a
728 // call is injected, the frame is placed 16 bytes below the
729 // original stack pointer to make room to save the link
730 // register, and the frame pointer saved by the original
731 // function isn't inside any call frame. We can adjust that
732 // saved frame pointer here by looking just above frame.fp.
733 //
734 // ^ original call ^
735 // | frame above... |
736 // +-------------------+ <- stack pointer at the time of injection
737 // : FP saved by :
738 // : original func :
739 // :···················: <- frame pointer register from original function
740 // : LR saved during :
741 // : injection :
742 // +-------------------+ <- frame.fp (injection decrements SP by 16 bytes)
743 // | FP saved |
744 // | during injection |
745 // +-------------------+
746 // | injected call |
747 // V frame below... V
748 adjustpointer(adjinfo, unsafe.Pointer(frame.fp+goarch.PtrSize))
749 }
750
751 if frame.continpc == 0 {
752 // Frame is dead. The program might still see the frame pointer
753 // saved in the frame, adjusted above, but we don't need to
754 // adjust the rest of the frame.
755 return
756 }
757 f := frame.fn
758 if stackDebug >= 2 {
759 print(" adjusting ", funcname(f), " frame=[", hex(frame.sp), ",", hex(frame.fp), "] pc=", hex(frame.pc), " continpc=", hex(frame.continpc), "\n")
760 }
761
762 locals, args, objs := frame.getStackMap(true)
763
764 // Adjust local variables if stack frame has been allocated.
765 if locals.n > 0 {
766 size := uintptr(locals.n) * goarch.PtrSize
767 adjustpointers(unsafe.Pointer(frame.varp-size), &locals, adjinfo, f)
768 }
769
770 // Adjust arguments.
771 if args.n > 0 {
772 if stackDebug >= 3 {
773 print(" args\n")
774 }
775 adjustpointers(unsafe.Pointer(frame.argp), &args, adjinfo, funcInfo{})
776 }
777
778 // Adjust pointers in all stack objects (whether they are live or not).
779 // See comments in mgcmark.go:scanframeworker.
780 if frame.varp != 0 {
781 for i := range objs {
782 obj := &objs[i]
783 off := obj.off
784 base := frame.varp // locals base pointer
785 if off >= 0 {
786 base = frame.argp // arguments and return values base pointer
787 }
788 p := base + uintptr(off)
789 if p < frame.sp {
790 // Object hasn't been allocated in the frame yet.
791 // (Happens when the stack bounds check fails and
792 // we call into morestack.)
793 continue
794 }
795 ptrBytes, gcData := obj.gcdata()
796 for i := uintptr(0); i < ptrBytes; i += goarch.PtrSize {
797 if *addb(gcData, i/(8*goarch.PtrSize))>>(i/goarch.PtrSize&7)&1 != 0 {
798 adjustpointer(adjinfo, unsafe.Pointer(p+i))
799 }
800 }
801 }
802 }
803 }
804
805 func adjustctxt(gp *g, adjinfo *adjustinfo) {
806 adjustpointer(adjinfo, unsafe.Pointer(&gp.sched.ctxt))
807 if !framepointer_enabled {
808 return
809 }
810 if debugCheckBP {
811 bp := gp.sched.bp
812 if bp != 0 && (bp < adjinfo.old.lo || bp >= adjinfo.old.hi) {
813 println("runtime: found invalid top frame pointer")
814 print("bp=", hex(bp), " min=", hex(adjinfo.old.lo), " max=", hex(adjinfo.old.hi), "\n")
815 throw("bad top frame pointer")
816 }
817 }
818 oldfp := gp.sched.bp
819 adjustpointer(adjinfo, unsafe.Pointer(&gp.sched.bp))
820 if GOARCH == "arm64" {
821 // On ARM64, the frame pointer is saved one word *below* the SP,
822 // which is not copied or adjusted in any frame. Do it explicitly
823 // here.
824 if oldfp == gp.sched.sp-goarch.PtrSize {
825 memmove(unsafe.Pointer(gp.sched.bp), unsafe.Pointer(oldfp), goarch.PtrSize)
826 adjustpointer(adjinfo, unsafe.Pointer(gp.sched.bp))
827 }
828 }
829 }
830
831 func adjustdefers(gp *g, adjinfo *adjustinfo) {
832 // Adjust pointers in the Defer structs.
833 // We need to do this first because we need to adjust the
834 // defer.link fields so we always work on the new stack.
835 adjustpointer(adjinfo, unsafe.Pointer(&gp._defer))
836 for d := gp._defer; d != nil; d = d.link {
837 adjustpointer(adjinfo, unsafe.Pointer(&d.fn))
838 adjustpointer(adjinfo, unsafe.Pointer(&d.sp))
839 adjustpointer(adjinfo, unsafe.Pointer(&d.link))
840 }
841 }
842
843 func adjustpanics(gp *g, adjinfo *adjustinfo) {
844 // Panics are on stack and already adjusted.
845 // Update pointer to head of list in G.
846 adjustpointer(adjinfo, unsafe.Pointer(&gp._panic))
847 }
848
849 func adjustsudogs(gp *g, adjinfo *adjustinfo) {
850 // the data elements pointed to by a SudoG structure
851 // might be in the stack.
852 for s := gp.waiting; s != nil; s = s.waitlink {
853 adjustpointer(adjinfo, unsafe.Pointer(&s.elem))
854 }
855 }
856
857 func fillstack(stk stack, b byte) {
858 for p := stk.lo; p < stk.hi; p++ {
859 *(*byte)(unsafe.Pointer(p)) = b
860 }
861 }
862
863 func findsghi(gp *g, stk stack) uintptr {
864 var sghi uintptr
865 for sg := gp.waiting; sg != nil; sg = sg.waitlink {
866 p := uintptr(sg.elem) + uintptr(sg.c.elemsize)
867 if stk.lo <= p && p < stk.hi && p > sghi {
868 sghi = p
869 }
870 }
871 return sghi
872 }
873
874 // syncadjustsudogs adjusts gp's sudogs and copies the part of gp's
875 // stack they refer to while synchronizing with concurrent channel
876 // operations. It returns the number of bytes of stack copied.
877 func syncadjustsudogs(gp *g, used uintptr, adjinfo *adjustinfo) uintptr {
878 if gp.waiting == nil {
879 return 0
880 }
881
882 // Lock channels to prevent concurrent send/receive.
883 var lastc *hchan
884 for sg := gp.waiting; sg != nil; sg = sg.waitlink {
885 if sg.c != lastc {
886 // There is a ranking cycle here between gscan bit and
887 // hchan locks. Normally, we only allow acquiring hchan
888 // locks and then getting a gscan bit. In this case, we
889 // already have the gscan bit. We allow acquiring hchan
890 // locks here as a special case, since a deadlock can't
891 // happen because the G involved must already be
892 // suspended. So, we get a special hchan lock rank here
893 // that is lower than gscan, but doesn't allow acquiring
894 // any other locks other than hchan.
895 lockWithRank(&sg.c.lock, lockRankHchanLeaf)
896 }
897 lastc = sg.c
898 }
899
900 // Adjust sudogs.
901 adjustsudogs(gp, adjinfo)
902
903 // Copy the part of the stack the sudogs point in to
904 // while holding the lock to prevent races on
905 // send/receive slots.
906 var sgsize uintptr
907 if adjinfo.sghi != 0 {
908 oldBot := adjinfo.old.hi - used
909 newBot := oldBot + adjinfo.delta
910 sgsize = adjinfo.sghi - oldBot
911 memmove(unsafe.Pointer(newBot), unsafe.Pointer(oldBot), sgsize)
912 }
913
914 // Unlock channels.
915 lastc = nil
916 for sg := gp.waiting; sg != nil; sg = sg.waitlink {
917 if sg.c != lastc {
918 unlock(&sg.c.lock)
919 }
920 lastc = sg.c
921 }
922
923 return sgsize
924 }
925
926 // Copies gp's stack to a new stack of a different size.
927 // Caller must have changed gp status to Gcopystack.
928 func copystack(gp *g, newsize uintptr) {
929 if gp.syscallsp != 0 {
930 throw("stack growth not allowed in system call")
931 }
932 old := gp.stack
933 if old.lo == 0 {
934 throw("nil stackbase")
935 }
936 used := old.hi - gp.sched.sp
937 // Add just the difference to gcController.addScannableStack.
938 // g0 stacks never move, so this will never account for them.
939 // It's also fine if we have no P, addScannableStack can deal with
940 // that case.
941 gcController.addScannableStack(getg().m.p.ptr(), int64(newsize)-int64(old.hi-old.lo))
942
943 // allocate new stack
944 new := stackalloc(uint32(newsize))
945 if stackPoisonCopy != 0 {
946 fillstack(new, 0xfd)
947 }
948 if stackDebug >= 1 {
949 print("copystack gp=", gp, " [", hex(old.lo), " ", hex(old.hi-used), " ", hex(old.hi), "]", " -> [", hex(new.lo), " ", hex(new.hi-used), " ", hex(new.hi), "]/", newsize, "\n")
950 }
951
952 // Compute adjustment.
953 var adjinfo adjustinfo
954 adjinfo.old = old
955 adjinfo.delta = new.hi - old.hi
956
957 // Adjust sudogs, synchronizing with channel ops if necessary.
958 ncopy := used
959 if !gp.activeStackChans {
960 if newsize < old.hi-old.lo && gp.parkingOnChan.Load() {
961 // It's not safe for someone to shrink this stack while we're actively
962 // parking on a channel, but it is safe to grow since we do that
963 // ourselves and explicitly don't want to synchronize with channels
964 // since we could self-deadlock.
965 throw("racy sudog adjustment due to parking on channel")
966 }
967 adjustsudogs(gp, &adjinfo)
968 } else {
969 // sudogs may be pointing in to the stack and gp has
970 // released channel locks, so other goroutines could
971 // be writing to gp's stack. Find the highest such
972 // pointer so we can handle everything there and below
973 // carefully. (This shouldn't be far from the bottom
974 // of the stack, so there's little cost in handling
975 // everything below it carefully.)
976 adjinfo.sghi = findsghi(gp, old)
977
978 // Synchronize with channel ops and copy the part of
979 // the stack they may interact with.
980 ncopy -= syncadjustsudogs(gp, used, &adjinfo)
981 }
982
983 // Copy the stack (or the rest of it) to the new location
984 memmove(unsafe.Pointer(new.hi-ncopy), unsafe.Pointer(old.hi-ncopy), ncopy)
985
986 // Adjust remaining structures that have pointers into stacks.
987 // We have to do most of these before we traceback the new
988 // stack because gentraceback uses them.
989 adjustctxt(gp, &adjinfo)
990 adjustdefers(gp, &adjinfo)
991 adjustpanics(gp, &adjinfo)
992 if adjinfo.sghi != 0 {
993 adjinfo.sghi += adjinfo.delta
994 }
995
996 // Swap out old stack for new one
997 gp.stack = new
998 gp.stackguard0 = new.lo + stackGuard // NOTE: might clobber a preempt request
999 gp.sched.sp = new.hi - used
1000 gp.stktopsp += adjinfo.delta
1001
1002 // Adjust pointers in the new stack.
1003 var u unwinder
1004 for u.init(gp, 0); u.valid(); u.next() {
1005 adjustframe(&u.frame, &adjinfo)
1006 }
1007
1008 if valgrindenabled {
1009 if gp.valgrindStackID == 0 {
1010 gp.valgrindStackID = valgrindRegisterStack(unsafe.Pointer(new.lo), unsafe.Pointer(new.hi))
1011 } else {
1012 valgrindChangeStack(gp.valgrindStackID, unsafe.Pointer(new.lo), unsafe.Pointer(new.hi))
1013 }
1014 }
1015
1016 // free old stack
1017 if stackPoisonCopy != 0 {
1018 fillstack(old, 0xfc)
1019 }
1020 stackfree(old)
1021 }
1022
1023 // round x up to a power of 2.
1024 func round2(x int32) int32 {
1025 s := uint(0)
1026 for 1<<s < x {
1027 s++
1028 }
1029 return 1 << s
1030 }
1031
1032 // Called from runtime·morestack when more stack is needed.
1033 // Allocate larger stack and relocate to new stack.
1034 // Stack growth is multiplicative, for constant amortized cost.
1035 //
1036 // g->atomicstatus will be Grunning or Gscanrunning upon entry.
1037 // If the scheduler is trying to stop this g, then it will set preemptStop.
1038 //
1039 // This must be nowritebarrierrec because it can be called as part of
1040 // stack growth from other nowritebarrierrec functions, but the
1041 // compiler doesn't check this.
1042 //
1043 //go:nowritebarrierrec
1044 func newstack() {
1045 thisg := getg()
1046 // TODO: double check all gp. shouldn't be getg().
1047 if thisg.m.morebuf.g.ptr().stackguard0 == stackFork {
1048 throw("stack growth after fork")
1049 }
1050 if thisg.m.morebuf.g.ptr() != thisg.m.curg {
1051 print("runtime: newstack called from g=", hex(thisg.m.morebuf.g), "\n"+"\tm=", thisg.m, " m->curg=", thisg.m.curg, " m->g0=", thisg.m.g0, " m->gsignal=", thisg.m.gsignal, "\n")
1052 morebuf := thisg.m.morebuf
1053 traceback(morebuf.pc, morebuf.sp, morebuf.lr, morebuf.g.ptr())
1054 throw("runtime: wrong goroutine in newstack")
1055 }
1056
1057 gp := thisg.m.curg
1058
1059 if thisg.m.curg.throwsplit {
1060 // Update syscallsp, syscallpc in case traceback uses them.
1061 morebuf := thisg.m.morebuf
1062 gp.syscallsp = morebuf.sp
1063 gp.syscallpc = morebuf.pc
1064 pcname, pcoff := "(unknown)", uintptr(0)
1065 f := findfunc(gp.sched.pc)
1066 if f.valid() {
1067 pcname = funcname(f)
1068 pcoff = gp.sched.pc - f.entry()
1069 }
1070 print("runtime: newstack at ", pcname, "+", hex(pcoff),
1071 " sp=", hex(gp.sched.sp), " stack=[", hex(gp.stack.lo), ", ", hex(gp.stack.hi), "]\n",
1072 "\tmorebuf={pc:", hex(morebuf.pc), " sp:", hex(morebuf.sp), " lr:", hex(morebuf.lr), "}\n",
1073 "\tsched={pc:", hex(gp.sched.pc), " sp:", hex(gp.sched.sp), " lr:", hex(gp.sched.lr), " ctxt:", gp.sched.ctxt, "}\n")
1074
1075 thisg.m.traceback = 2 // Include runtime frames
1076 traceback(morebuf.pc, morebuf.sp, morebuf.lr, gp)
1077 throw("runtime: stack split at bad time")
1078 }
1079
1080 morebuf := thisg.m.morebuf
1081 thisg.m.morebuf.pc = 0
1082 thisg.m.morebuf.lr = 0
1083 thisg.m.morebuf.sp = 0
1084 thisg.m.morebuf.g = 0
1085
1086 // NOTE: stackguard0 may change underfoot, if another thread
1087 // is about to try to preempt gp. Read it just once and use that same
1088 // value now and below.
1089 stackguard0 := atomic.Loaduintptr(&gp.stackguard0)
1090
1091 // Be conservative about where we preempt.
1092 // We are interested in preempting user Go code, not runtime code.
1093 // If we're holding locks, mallocing, or preemption is disabled, don't
1094 // preempt.
1095 // This check is very early in newstack so that even the status change
1096 // from Grunning to Gwaiting and back doesn't happen in this case.
1097 // That status change by itself can be viewed as a small preemption,
1098 // because the GC might change Gwaiting to Gscanwaiting, and then
1099 // this goroutine has to wait for the GC to finish before continuing.
1100 // If the GC is in some way dependent on this goroutine (for example,
1101 // it needs a lock held by the goroutine), that small preemption turns
1102 // into a real deadlock.
1103 preempt := stackguard0 == stackPreempt
1104 if preempt {
1105 if !canPreemptM(thisg.m) {
1106 // Let the goroutine keep running for now.
1107 // gp->preempt is set, so it will be preempted next time.
1108 gp.stackguard0 = gp.stack.lo + stackGuard
1109 gogo(&gp.sched) // never return
1110 }
1111 }
1112
1113 if gp.stack.lo == 0 {
1114 throw("missing stack in newstack")
1115 }
1116 sp := gp.sched.sp
1117 if goarch.ArchFamily == goarch.AMD64 || goarch.ArchFamily == goarch.I386 || goarch.ArchFamily == goarch.WASM {
1118 // The call to morestack cost a word.
1119 sp -= goarch.PtrSize
1120 }
1121 if stackDebug >= 1 || sp < gp.stack.lo {
1122 print("runtime: newstack sp=", hex(sp), " stack=[", hex(gp.stack.lo), ", ", hex(gp.stack.hi), "]\n",
1123 "\tmorebuf={pc:", hex(morebuf.pc), " sp:", hex(morebuf.sp), " lr:", hex(morebuf.lr), "}\n",
1124 "\tsched={pc:", hex(gp.sched.pc), " sp:", hex(gp.sched.sp), " lr:", hex(gp.sched.lr), " ctxt:", gp.sched.ctxt, "}\n")
1125 }
1126 if sp < gp.stack.lo {
1127 print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->status=", hex(readgstatus(gp)), "\n ")
1128 print("runtime: split stack overflow: ", hex(sp), " < ", hex(gp.stack.lo), "\n")
1129 throw("runtime: split stack overflow")
1130 }
1131
1132 if preempt {
1133 if gp == thisg.m.g0 {
1134 throw("runtime: preempt g0")
1135 }
1136 if thisg.m.p == 0 && thisg.m.locks == 0 {
1137 throw("runtime: g is running but p is not")
1138 }
1139
1140 if gp.preemptShrink {
1141 // We're at a synchronous safe point now, so
1142 // do the pending stack shrink.
1143 gp.preemptShrink = false
1144 shrinkstack(gp)
1145 }
1146
1147 // Set a flag indicated that we've been synchronously preempted.
1148 gp.syncSafePoint = true
1149
1150 if gp.preemptStop {
1151 preemptPark(gp) // never returns
1152 }
1153
1154 // Act like goroutine called runtime.Gosched.
1155 gopreempt_m(gp) // never return
1156 }
1157
1158 // Allocate a bigger segment and move the stack.
1159 oldsize := gp.stack.hi - gp.stack.lo
1160 newsize := oldsize * 2
1161
1162 // Make sure we grow at least as much as needed to fit the new frame.
1163 // (This is just an optimization - the caller of morestack will
1164 // recheck the bounds on return.)
1165 if f := findfunc(gp.sched.pc); f.valid() {
1166 max := uintptr(funcMaxSPDelta(f))
1167 needed := max + stackGuard
1168 used := gp.stack.hi - gp.sched.sp
1169 for newsize-used < needed {
1170 newsize *= 2
1171 }
1172 }
1173
1174 if stackguard0 == stackForceMove {
1175 // Forced stack movement used for debugging.
1176 // Don't double the stack (or we may quickly run out
1177 // if this is done repeatedly).
1178 newsize = oldsize
1179 }
1180
1181 if newsize > maxstacksize || newsize > maxstackceiling {
1182 if maxstacksize < maxstackceiling {
1183 print("runtime: goroutine stack exceeds ", maxstacksize, "-byte limit\n")
1184 } else {
1185 print("runtime: goroutine stack exceeds ", maxstackceiling, "-byte limit\n")
1186 }
1187 print("runtime: sp=", hex(sp), " stack=[", hex(gp.stack.lo), ", ", hex(gp.stack.hi), "]\n")
1188 throw("stack overflow")
1189 }
1190
1191 // The goroutine must be executing in order to call newstack,
1192 // so it must be Grunning (or Gscanrunning).
1193 casgstatus(gp, _Grunning, _Gcopystack)
1194
1195 // The concurrent GC will not scan the stack while we are doing the copy since
1196 // the gp is in a Gcopystack status.
1197 copystack(gp, newsize)
1198 if stackDebug >= 1 {
1199 print("stack grow done\n")
1200 }
1201 casgstatus(gp, _Gcopystack, _Grunning)
1202 gogo(&gp.sched)
1203 }
1204
1205 //go:nosplit
1206 func nilfunc() {
1207 *(*uint8)(nil) = 0
1208 }
1209
1210 // adjust Gobuf as if it executed a call to fn
1211 // and then stopped before the first instruction in fn.
1212 func gostartcallfn(gobuf *gobuf, fv *funcval) {
1213 var fn unsafe.Pointer
1214 if fv != nil {
1215 fn = unsafe.Pointer(fv.fn)
1216 } else {
1217 fn = unsafe.Pointer(abi.FuncPCABIInternal(nilfunc))
1218 }
1219 gostartcall(gobuf, fn, unsafe.Pointer(fv))
1220 }
1221
1222 // isShrinkStackSafe returns whether it's safe to attempt to shrink
1223 // gp's stack. Shrinking the stack is only safe when we have precise
1224 // pointer maps for all frames on the stack. The caller must hold the
1225 // _Gscan bit for gp or must be running gp itself.
1226 func isShrinkStackSafe(gp *g) bool {
1227 // We can't copy the stack if we're in a syscall.
1228 // The syscall might have pointers into the stack and
1229 // often we don't have precise pointer maps for the innermost
1230 // frames.
1231 if gp.syscallsp != 0 {
1232 return false
1233 }
1234 // We also can't copy the stack if we're at an asynchronous
1235 // safe-point because we don't have precise pointer maps for
1236 // all frames.
1237 if gp.asyncSafePoint {
1238 return false
1239 }
1240 // We also can't *shrink* the stack in the window between the
1241 // goroutine calling gopark to park on a channel and
1242 // gp.activeStackChans being set.
1243 if gp.parkingOnChan.Load() {
1244 return false
1245 }
1246 // We also can't copy the stack while tracing is enabled, and
1247 // gp is in _Gwaiting solely to make itself available to suspendG.
1248 // In these cases, the G is actually executing on the system
1249 // stack, and the execution tracer may want to take a stack trace
1250 // of the G's stack. Note: it's safe to access gp.waitreason here.
1251 // We're only checking if this is true if we took ownership of the
1252 // G with the _Gscan bit. This prevents the goroutine from transitioning,
1253 // which prevents gp.waitreason from changing.
1254 if traceEnabled() && readgstatus(gp)&^_Gscan == _Gwaiting && gp.waitreason.isWaitingForSuspendG() {
1255 return false
1256 }
1257 return true
1258 }
1259
1260 // Maybe shrink the stack being used by gp.
1261 //
1262 // gp must be stopped and we must own its stack. It may be in
1263 // _Grunning, but only if this is our own user G.
1264 func shrinkstack(gp *g) {
1265 if gp.stack.lo == 0 {
1266 throw("missing stack in shrinkstack")
1267 }
1268 if s := readgstatus(gp); s&_Gscan == 0 {
1269 // We don't own the stack via _Gscan. We could still
1270 // own it if this is our own user G and we're on the
1271 // system stack.
1272 if !(gp == getg().m.curg && getg() != getg().m.curg && s == _Grunning) {
1273 // We don't own the stack.
1274 throw("bad status in shrinkstack")
1275 }
1276 }
1277 if !isShrinkStackSafe(gp) {
1278 throw("shrinkstack at bad time")
1279 }
1280 // Check for self-shrinks while in a libcall. These may have
1281 // pointers into the stack disguised as uintptrs, but these
1282 // code paths should all be nosplit.
1283 if gp == getg().m.curg && gp.m.libcallsp != 0 {
1284 throw("shrinking stack in libcall")
1285 }
1286
1287 if debug.gcshrinkstackoff > 0 {
1288 return
1289 }
1290 f := findfunc(gp.startpc)
1291 if f.valid() && f.funcID == abi.FuncID_gcBgMarkWorker {
1292 // We're not allowed to shrink the gcBgMarkWorker
1293 // stack (see gcBgMarkWorker for explanation).
1294 return
1295 }
1296
1297 oldsize := gp.stack.hi - gp.stack.lo
1298 newsize := oldsize / 2
1299 // Don't shrink the allocation below the minimum-sized stack
1300 // allocation.
1301 if newsize < fixedStack {
1302 return
1303 }
1304 // Compute how much of the stack is currently in use and only
1305 // shrink the stack if gp is using less than a quarter of its
1306 // current stack. The currently used stack includes everything
1307 // down to the SP plus the stack guard space that ensures
1308 // there's room for nosplit functions.
1309 avail := gp.stack.hi - gp.stack.lo
1310 if used := gp.stack.hi - gp.sched.sp + stackNosplit; used >= avail/4 {
1311 return
1312 }
1313
1314 if stackDebug > 0 {
1315 print("shrinking stack ", oldsize, "->", newsize, "\n")
1316 }
1317
1318 copystack(gp, newsize)
1319 }
1320
1321 // freeStackSpans frees unused stack spans at the end of GC.
1322 func freeStackSpans() {
1323 // Scan stack pools for empty stack spans.
1324 for order := range stackpool {
1325 lock(&stackpool[order].item.mu)
1326 list := &stackpool[order].item.span
1327 for s := list.first; s != nil; {
1328 next := s.next
1329 if s.allocCount == 0 {
1330 list.remove(s)
1331 s.manualFreeList = 0
1332 osStackFree(s)
1333 mheap_.freeManual(s, spanAllocStack)
1334 }
1335 s = next
1336 }
1337 unlock(&stackpool[order].item.mu)
1338 }
1339
1340 // Free large stack spans.
1341 lock(&stackLarge.lock)
1342 for i := range stackLarge.free {
1343 for s := stackLarge.free[i].first; s != nil; {
1344 next := s.next
1345 stackLarge.free[i].remove(s)
1346 osStackFree(s)
1347 mheap_.freeManual(s, spanAllocStack)
1348 s = next
1349 }
1350 }
1351 unlock(&stackLarge.lock)
1352 }
1353
1354 // A stackObjectRecord is generated by the compiler for each stack object in a stack frame.
1355 // This record must match the generator code in cmd/compile/internal/liveness/plive.go:emitStackObjects.
1356 type stackObjectRecord struct {
1357 // offset in frame
1358 // if negative, offset from varp
1359 // if non-negative, offset from argp
1360 off int32
1361 size int32
1362 ptrBytes int32
1363 gcdataoff uint32 // offset to gcdata from moduledata.rodata
1364 }
1365
1366 // gcdata returns the number of bytes that contain pointers, and
1367 // a ptr/nonptr bitmask covering those bytes.
1368 // Note that this bitmask might be larger than internal/abi.MaxPtrmaskBytes.
1369 func (r *stackObjectRecord) gcdata() (uintptr, *byte) {
1370 ptr := uintptr(unsafe.Pointer(r))
1371 var mod *moduledata
1372 for datap := &firstmoduledata; datap != nil; datap = datap.next {
1373 if datap.gofunc <= ptr && ptr < datap.end {
1374 mod = datap
1375 break
1376 }
1377 }
1378 // If you get a panic here due to a nil mod,
1379 // you may have made a copy of a stackObjectRecord.
1380 // You must use the original pointer.
1381 res := mod.rodata + uintptr(r.gcdataoff)
1382 return uintptr(r.ptrBytes), (*byte)(unsafe.Pointer(res))
1383 }
1384
1385 // This is exported as ABI0 via linkname so obj can call it.
1386 //
1387 //go:nosplit
1388 //go:linkname morestackc
1389 func morestackc() {
1390 throw("attempt to execute system stack code on user stack")
1391 }
1392
1393 // startingStackSize is the amount of stack that new goroutines start with.
1394 // It is a power of 2, and between fixedStack and maxstacksize, inclusive.
1395 // startingStackSize is updated every GC by tracking the average size of
1396 // stacks scanned during the GC.
1397 var startingStackSize uint32 = fixedStack
1398
1399 func gcComputeStartingStackSize() {
1400 if debug.adaptivestackstart == 0 {
1401 return
1402 }
1403 // For details, see the design doc at
1404 // https://docs.google.com/document/d/1YDlGIdVTPnmUiTAavlZxBI1d9pwGQgZT7IKFKlIXohQ/edit?usp=sharing
1405 // The basic algorithm is to track the average size of stacks
1406 // and start goroutines with stack equal to that average size.
1407 // Starting at the average size uses at most 2x the space that
1408 // an ideal algorithm would have used.
1409 // This is just a heuristic to avoid excessive stack growth work
1410 // early in a goroutine's lifetime. See issue 18138. Stacks that
1411 // are allocated too small can still grow, and stacks allocated
1412 // too large can still shrink.
1413 var scannedStackSize uint64
1414 var scannedStacks uint64
1415 for _, p := range allp {
1416 scannedStackSize += p.scannedStackSize
1417 scannedStacks += p.scannedStacks
1418 // Reset for next time
1419 p.scannedStackSize = 0
1420 p.scannedStacks = 0
1421 }
1422 if scannedStacks == 0 {
1423 startingStackSize = fixedStack
1424 return
1425 }
1426 avg := scannedStackSize/scannedStacks + stackGuard
1427 // Note: we add stackGuard to ensure that a goroutine that
1428 // uses the average space will not trigger a growth.
1429 if avg > uint64(maxstacksize) {
1430 avg = uint64(maxstacksize)
1431 }
1432 if avg < fixedStack {
1433 avg = fixedStack
1434 }
1435 // Note: maxstacksize fits in 30 bits, so avg also does.
1436 startingStackSize = uint32(round2(int32(avg)))
1437 }