Text file src/runtime/cgo/gcc_stack_unix.c

     1  // Copyright 2023 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  //go:build unix && !darwin
     6  
     7  #ifndef _GNU_SOURCE // pthread_getattr_np
     8  #define _GNU_SOURCE
     9  #endif
    10  
    11  #include <pthread.h>
    12  #include "libcgo.h"
    13  
    14  void
    15  x_cgo_getstackbound(uintptr bounds[2])
    16  {
    17  	pthread_attr_t attr;
    18  	void *addr;
    19  	size_t size;
    20  
    21  #if defined(__GLIBC__) || (defined(__sun) && !defined(__illumos__))
    22  	// pthread_getattr_np is a GNU extension supported in glibc.
    23  	// Solaris is not glibc but does support pthread_getattr_np
    24  	// (and the fallback doesn't work...). Illumos does not.
    25  	pthread_getattr_np(pthread_self(), &attr);  // GNU extension
    26  	pthread_attr_getstack(&attr, &addr, &size); // low address
    27  #elif defined(__illumos__)
    28  	pthread_attr_init(&attr);
    29  	pthread_attr_get_np(pthread_self(), &attr);
    30  	pthread_attr_getstack(&attr, &addr, &size); // low address
    31  #else
    32  	// We don't know how to get the current stacks, so assume they are the
    33  	// same as the default stack bounds.
    34  	pthread_attr_init(&attr);
    35  	pthread_attr_getstacksize(&attr, &size);
    36  	addr = __builtin_frame_address(0) + 4096 - size;
    37  #endif
    38  	pthread_attr_destroy(&attr);
    39  
    40  	bounds[0] = (uintptr)addr;
    41  	bounds[1] = (uintptr)addr + size;
    42  }
    43  

View as plain text