github.com/lzhfromustc/gofuzz@v0.0.0-20211116160056-151b3108bbd1/runtime/os_openbsd_libc.go (about)

     1  // Copyright 2020 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  // +build openbsd,amd64 openbsd,arm64
     6  
     7  package runtime
     8  
     9  import (
    10  	"unsafe"
    11  )
    12  
    13  var failThreadCreate = []byte("runtime: failed to create new OS thread\n")
    14  
    15  // mstart_stub provides glue code to call mstart from pthread_create.
    16  func mstart_stub()
    17  
    18  // May run with m.p==nil, so write barriers are not allowed.
    19  //go:nowritebarrierrec
    20  func newosproc(mp *m) {
    21  	if false {
    22  		print("newosproc m=", mp, " g=", mp.g0, " id=", mp.id, " ostk=", &mp, "\n")
    23  	}
    24  
    25  	// Initialize an attribute object.
    26  	var attr pthreadattr
    27  	if err := pthread_attr_init(&attr); err != 0 {
    28  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    29  		exit(1)
    30  	}
    31  
    32  	// Find out OS stack size for our own stack guard.
    33  	var stacksize uintptr
    34  	if pthread_attr_getstacksize(&attr, &stacksize) != 0 {
    35  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    36  		exit(1)
    37  	}
    38  	mp.g0.stack.hi = stacksize // for mstart
    39  
    40  	// Tell the pthread library we won't join with this thread.
    41  	if pthread_attr_setdetachstate(&attr, _PTHREAD_CREATE_DETACHED) != 0 {
    42  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    43  		exit(1)
    44  	}
    45  
    46  	// Finally, create the thread. It starts at mstart_stub, which does some low-level
    47  	// setup and then calls mstart.
    48  	var oset sigset
    49  	sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
    50  	err := pthread_create(&attr, funcPC(mstart_stub), unsafe.Pointer(mp))
    51  	sigprocmask(_SIG_SETMASK, &oset, nil)
    52  	if err != 0 {
    53  		write(2, unsafe.Pointer(&failThreadCreate[0]), int32(len(failThreadCreate)))
    54  		exit(1)
    55  	}
    56  
    57  	pthread_attr_destroy(&attr)
    58  }