Source file src/runtime/runtime1.go

     1  // Copyright 2009 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/bytealg"
    10  	"internal/goarch"
    11  	"internal/godebugs"
    12  	"internal/runtime/atomic"
    13  	"internal/strconv"
    14  	"unsafe"
    15  )
    16  
    17  // Keep a cached value to make gotraceback fast,
    18  // since we call it on every call to gentraceback.
    19  // The cached value is a uint32 in which the low bits
    20  // are the "crash" and "all" settings and the remaining
    21  // bits are the traceback value (0 off, 1 on, 2 include system).
    22  const (
    23  	tracebackCrash = 1 << iota
    24  	tracebackAll
    25  	tracebackShift = iota
    26  )
    27  
    28  var traceback_cache uint32 = 2 << tracebackShift
    29  var traceback_env uint32
    30  
    31  // gotraceback returns the current traceback settings.
    32  //
    33  // If level is 0, suppress all tracebacks.
    34  // If level is 1, show tracebacks, but exclude runtime frames.
    35  // If level is 2, show tracebacks including runtime frames.
    36  // If all is set, print all goroutine stacks. Otherwise, print just the current goroutine.
    37  // If crash is set, crash (core dump, etc) after tracebacking.
    38  //
    39  //go:nosplit
    40  func gotraceback() (level int32, all, crash bool) {
    41  	gp := getg()
    42  	t := atomic.Load(&traceback_cache)
    43  	crash = t&tracebackCrash != 0
    44  	all = gp.m.throwing > throwTypeUser || t&tracebackAll != 0
    45  	if gp.m.traceback != 0 {
    46  		level = int32(gp.m.traceback)
    47  	} else if gp.m.throwing >= throwTypeRuntime {
    48  		// Always include runtime frames in runtime throws unless
    49  		// otherwise overridden by m.traceback.
    50  		level = 2
    51  	} else {
    52  		level = int32(t >> tracebackShift)
    53  	}
    54  	return
    55  }
    56  
    57  var (
    58  	argc int32
    59  	argv **byte
    60  )
    61  
    62  // nosplit for use in linux startup sysargs.
    63  //
    64  //go:nosplit
    65  func argv_index(argv **byte, i int32) *byte {
    66  	return *(**byte)(add(unsafe.Pointer(argv), uintptr(i)*goarch.PtrSize))
    67  }
    68  
    69  func args(c int32, v **byte) {
    70  	argc = c
    71  	argv = v
    72  	sysargs(c, v)
    73  }
    74  
    75  func goargs() {
    76  	if GOOS == "windows" {
    77  		return
    78  	}
    79  	argslice = make([]string, argc)
    80  	for i := int32(0); i < argc; i++ {
    81  		argslice[i] = gostringnocopy(argv_index(argv, i))
    82  	}
    83  }
    84  
    85  func goenvs_unix() {
    86  	// TODO(austin): ppc64 in dynamic linking mode doesn't
    87  	// guarantee env[] will immediately follow argv. Might cause
    88  	// problems.
    89  	n := int32(0)
    90  	for argv_index(argv, argc+1+n) != nil {
    91  		n++
    92  	}
    93  
    94  	envs = make([]string, n)
    95  	for i := int32(0); i < n; i++ {
    96  		envs[i] = gostring(argv_index(argv, argc+1+i))
    97  	}
    98  }
    99  
   100  func environ() []string {
   101  	return envs
   102  }
   103  
   104  // TODO: These should be locals in testAtomic64, but we don't 8-byte
   105  // align stack variables on 386.
   106  var test_z64, test_x64 uint64
   107  
   108  func testAtomic64() {
   109  	test_z64 = 42
   110  	test_x64 = 0
   111  	if atomic.Cas64(&test_z64, test_x64, 1) {
   112  		throw("cas64 failed")
   113  	}
   114  	if test_x64 != 0 {
   115  		throw("cas64 failed")
   116  	}
   117  	test_x64 = 42
   118  	if !atomic.Cas64(&test_z64, test_x64, 1) {
   119  		throw("cas64 failed")
   120  	}
   121  	if test_x64 != 42 || test_z64 != 1 {
   122  		throw("cas64 failed")
   123  	}
   124  	if atomic.Load64(&test_z64) != 1 {
   125  		throw("load64 failed")
   126  	}
   127  	atomic.Store64(&test_z64, (1<<40)+1)
   128  	if atomic.Load64(&test_z64) != (1<<40)+1 {
   129  		throw("store64 failed")
   130  	}
   131  	if atomic.Xadd64(&test_z64, (1<<40)+1) != (2<<40)+2 {
   132  		throw("xadd64 failed")
   133  	}
   134  	if atomic.Load64(&test_z64) != (2<<40)+2 {
   135  		throw("xadd64 failed")
   136  	}
   137  	if atomic.Xchg64(&test_z64, (3<<40)+3) != (2<<40)+2 {
   138  		throw("xchg64 failed")
   139  	}
   140  	if atomic.Load64(&test_z64) != (3<<40)+3 {
   141  		throw("xchg64 failed")
   142  	}
   143  }
   144  
   145  func check() {
   146  	var (
   147  		a     int8
   148  		b     uint8
   149  		c     int16
   150  		d     uint16
   151  		e     int32
   152  		f     uint32
   153  		g     int64
   154  		h     uint64
   155  		i, i1 float32
   156  		j, j1 float64
   157  		k     unsafe.Pointer
   158  		l     *uint16
   159  		m     [4]byte
   160  	)
   161  	type x1t struct {
   162  		x uint8
   163  	}
   164  	type y1t struct {
   165  		x1 x1t
   166  		y  uint8
   167  	}
   168  	var x1 x1t
   169  	var y1 y1t
   170  
   171  	if unsafe.Sizeof(a) != 1 {
   172  		throw("bad a")
   173  	}
   174  	if unsafe.Sizeof(b) != 1 {
   175  		throw("bad b")
   176  	}
   177  	if unsafe.Sizeof(c) != 2 {
   178  		throw("bad c")
   179  	}
   180  	if unsafe.Sizeof(d) != 2 {
   181  		throw("bad d")
   182  	}
   183  	if unsafe.Sizeof(e) != 4 {
   184  		throw("bad e")
   185  	}
   186  	if unsafe.Sizeof(f) != 4 {
   187  		throw("bad f")
   188  	}
   189  	if unsafe.Sizeof(g) != 8 {
   190  		throw("bad g")
   191  	}
   192  	if unsafe.Sizeof(h) != 8 {
   193  		throw("bad h")
   194  	}
   195  	if unsafe.Sizeof(i) != 4 {
   196  		throw("bad i")
   197  	}
   198  	if unsafe.Sizeof(j) != 8 {
   199  		throw("bad j")
   200  	}
   201  	if unsafe.Sizeof(k) != goarch.PtrSize {
   202  		throw("bad k")
   203  	}
   204  	if unsafe.Sizeof(l) != goarch.PtrSize {
   205  		throw("bad l")
   206  	}
   207  	if unsafe.Sizeof(x1) != 1 {
   208  		throw("bad unsafe.Sizeof x1")
   209  	}
   210  	if unsafe.Offsetof(y1.y) != 1 {
   211  		throw("bad offsetof y1.y")
   212  	}
   213  	if unsafe.Sizeof(y1) != 2 {
   214  		throw("bad unsafe.Sizeof y1")
   215  	}
   216  
   217  	var z uint32
   218  	z = 1
   219  	if !atomic.Cas(&z, 1, 2) {
   220  		throw("cas1")
   221  	}
   222  	if z != 2 {
   223  		throw("cas2")
   224  	}
   225  
   226  	z = 4
   227  	if atomic.Cas(&z, 5, 6) {
   228  		throw("cas3")
   229  	}
   230  	if z != 4 {
   231  		throw("cas4")
   232  	}
   233  
   234  	z = 0xffffffff
   235  	if !atomic.Cas(&z, 0xffffffff, 0xfffffffe) {
   236  		throw("cas5")
   237  	}
   238  	if z != 0xfffffffe {
   239  		throw("cas6")
   240  	}
   241  
   242  	m = [4]byte{1, 1, 1, 1}
   243  	atomic.Or8(&m[1], 0xf0)
   244  	if m[0] != 1 || m[1] != 0xf1 || m[2] != 1 || m[3] != 1 {
   245  		throw("atomicor8")
   246  	}
   247  
   248  	m = [4]byte{0xff, 0xff, 0xff, 0xff}
   249  	atomic.And8(&m[1], 0x1)
   250  	if m[0] != 0xff || m[1] != 0x1 || m[2] != 0xff || m[3] != 0xff {
   251  		throw("atomicand8")
   252  	}
   253  
   254  	*(*uint64)(unsafe.Pointer(&j)) = ^uint64(0)
   255  	if j == j {
   256  		throw("float64nan")
   257  	}
   258  	if !(j != j) {
   259  		throw("float64nan1")
   260  	}
   261  
   262  	*(*uint64)(unsafe.Pointer(&j1)) = ^uint64(1)
   263  	if j == j1 {
   264  		throw("float64nan2")
   265  	}
   266  	if !(j != j1) {
   267  		throw("float64nan3")
   268  	}
   269  
   270  	*(*uint32)(unsafe.Pointer(&i)) = ^uint32(0)
   271  	if i == i {
   272  		throw("float32nan")
   273  	}
   274  	if i == i {
   275  		throw("float32nan1")
   276  	}
   277  
   278  	*(*uint32)(unsafe.Pointer(&i1)) = ^uint32(1)
   279  	if i == i1 {
   280  		throw("float32nan2")
   281  	}
   282  	if i == i1 {
   283  		throw("float32nan3")
   284  	}
   285  
   286  	testAtomic64()
   287  
   288  	if fixedStack != round2(fixedStack) {
   289  		throw("FixedStack is not power-of-2")
   290  	}
   291  }
   292  
   293  type dbgVar struct {
   294  	name   string
   295  	value  *int32        // for variables that can only be set at startup
   296  	atomic *atomic.Int32 // for variables that can be changed during execution
   297  	def    int32         // default value (ideally zero)
   298  }
   299  
   300  // Holds variables parsed from GODEBUG env var,
   301  // except for "memprofilerate" since there is an
   302  // existing int var for that value, which may
   303  // already have an initial value.
   304  var debug struct {
   305  	cgocheck                 int32
   306  	clobberfree              int32
   307  	containermaxprocs        int32
   308  	decoratemappings         int32
   309  	disablethp               int32
   310  	dontfreezetheworld       int32
   311  	efence                   int32
   312  	gccheckmark              int32
   313  	gcpacertrace             int32
   314  	gcshrinkstackoff         int32
   315  	gcstoptheworld           int32
   316  	gctrace                  int32
   317  	invalidptr               int32
   318  	madvdontneed             int32 // for Linux; issue 28466
   319  	memoryprofile            int32 // set to 0 to disable the memory profile's GC work
   320  	scavtrace                int32
   321  	scheddetail              int32
   322  	schedtrace               int32
   323  	tracebackancestors       int32
   324  	tracebackcrash           int32
   325  	updatemaxprocs           int32
   326  	asyncpreemptoff          int32
   327  	harddecommit             int32
   328  	adaptivestackstart       int32
   329  	tracefpunwindoff         int32
   330  	traceadvanceperiod       int32
   331  	traceCheckStackOwnership int32
   332  	profstackdepth           int32
   333  	dataindependenttiming    int32
   334  
   335  	// debug.malloc is used as a combined debug check
   336  	// in the malloc function and should be set
   337  	// if any of the below debug options is != 0.
   338  	malloc          bool
   339  	inittrace       int32
   340  	sbrk            int32
   341  	checkfinalizers int32
   342  	// traceallocfree controls whether execution traces contain
   343  	// detailed trace data about memory allocation. This value
   344  	// affects debug.malloc only if it is != 0 and the execution
   345  	// tracer is enabled, in which case debug.malloc will be
   346  	// set to "true" if it isn't already while tracing is enabled.
   347  	// It will be set while the world is stopped, so it's safe.
   348  	// The value of traceallocfree can be changed any time in response
   349  	// to os.Setenv("GODEBUG").
   350  	traceallocfree atomic.Int32
   351  
   352  	panicnil atomic.Int32
   353  
   354  	// tracebacklabels controls the inclusion of goroutine labels in the
   355  	// goroutine status header line.
   356  	tracebacklabels atomic.Int32
   357  }
   358  
   359  var dbgvars = []*dbgVar{
   360  	{name: "adaptivestackstart", value: &debug.adaptivestackstart},
   361  	{name: "asyncpreemptoff", value: &debug.asyncpreemptoff},
   362  	{name: "cgocheck", value: &debug.cgocheck},
   363  	{name: "clobberfree", value: &debug.clobberfree},
   364  	{name: "containermaxprocs", value: &debug.containermaxprocs, def: 1},
   365  	{name: "dataindependenttiming", value: &debug.dataindependenttiming},
   366  	{name: "decoratemappings", value: &debug.decoratemappings, def: 1},
   367  	{name: "disablethp", value: &debug.disablethp},
   368  	{name: "dontfreezetheworld", value: &debug.dontfreezetheworld},
   369  	{name: "checkfinalizers", value: &debug.checkfinalizers},
   370  	{name: "efence", value: &debug.efence},
   371  	{name: "gccheckmark", value: &debug.gccheckmark},
   372  	{name: "gcpacertrace", value: &debug.gcpacertrace},
   373  	{name: "gcshrinkstackoff", value: &debug.gcshrinkstackoff},
   374  	{name: "gcstoptheworld", value: &debug.gcstoptheworld},
   375  	{name: "gctrace", value: &debug.gctrace},
   376  	{name: "harddecommit", value: &debug.harddecommit},
   377  	{name: "inittrace", value: &debug.inittrace},
   378  	{name: "invalidptr", value: &debug.invalidptr},
   379  	{name: "madvdontneed", value: &debug.madvdontneed},
   380  	{name: "memoryprofile", value: &debug.memoryprofile, def: 1},
   381  	{name: "panicnil", atomic: &debug.panicnil},
   382  	{name: "profstackdepth", value: &debug.profstackdepth, def: 128},
   383  	{name: "sbrk", value: &debug.sbrk},
   384  	{name: "scavtrace", value: &debug.scavtrace},
   385  	{name: "scheddetail", value: &debug.scheddetail},
   386  	{name: "schedtrace", value: &debug.schedtrace},
   387  	{name: "traceadvanceperiod", value: &debug.traceadvanceperiod},
   388  	{name: "traceallocfree", atomic: &debug.traceallocfree},
   389  	{name: "tracecheckstackownership", value: &debug.traceCheckStackOwnership},
   390  	{name: "tracebackancestors", value: &debug.tracebackancestors},
   391  	{name: "tracebackcrash", value: &debug.tracebackcrash},
   392  	{name: "tracebacklabels", atomic: &debug.tracebacklabels, def: 1},
   393  	{name: "tracefpunwindoff", value: &debug.tracefpunwindoff},
   394  	{name: "updatemaxprocs", value: &debug.updatemaxprocs, def: 1},
   395  }
   396  
   397  func parseRuntimeDebugVars(godebug string) {
   398  	// defaults
   399  	debug.cgocheck = 1
   400  	debug.invalidptr = 1
   401  	debug.adaptivestackstart = 1 // set this to 0 to turn larger initial goroutine stacks off
   402  	if GOOS == "linux" {
   403  		// On Linux, MADV_FREE is faster than MADV_DONTNEED,
   404  		// but doesn't affect many of the statistics that
   405  		// MADV_DONTNEED does until the memory is actually
   406  		// reclaimed. This generally leads to poor user
   407  		// experience, like confusing stats in top and other
   408  		// monitoring tools; and bad integration with
   409  		// management systems that respond to memory usage.
   410  		// Hence, default to MADV_DONTNEED.
   411  		debug.madvdontneed = 1
   412  	}
   413  	debug.traceadvanceperiod = defaultTraceAdvancePeriod
   414  
   415  	// apply runtime defaults, if any
   416  	for _, v := range dbgvars {
   417  		if v.def != 0 {
   418  			// Every var should have either v.value or v.atomic set.
   419  			if v.value != nil {
   420  				*v.value = v.def
   421  			} else if v.atomic != nil {
   422  				v.atomic.Store(v.def)
   423  			}
   424  		}
   425  	}
   426  	// apply compile-time GODEBUG settings
   427  	parsegodebug(godebugDefault, nil)
   428  
   429  	// apply environment settings
   430  	parsegodebug(godebug, nil)
   431  
   432  	debug.malloc = (debug.inittrace | debug.sbrk | debug.checkfinalizers) != 0
   433  	debug.profstackdepth = min(debug.profstackdepth, maxProfStackDepth)
   434  
   435  	// Disable async preemption in checkmark mode. The following situation is
   436  	// problematic with checkmark mode:
   437  	//
   438  	// - The GC doesn't mark object A because it is truly dead.
   439  	// - The GC stops the world, asynchronously preempting G1 which has a reference
   440  	//   to A in its top stack frame
   441  	// - During the stop the world, we run the second checkmark GC. It marks the roots
   442  	//   and discovers A through G1.
   443  	// - Checkmark mode reports a failure since there's a discrepancy in mark metadata.
   444  	//
   445  	// We could disable just conservative scanning during the checkmark scan, which is
   446  	// safe but makes checkmark slightly less powerful, but that's a lot more invasive
   447  	// than just disabling async preemption altogether.
   448  	if debug.gccheckmark > 0 {
   449  		debug.asyncpreemptoff = 1
   450  	}
   451  }
   452  
   453  func finishDebugVarsSetup() {
   454  	p := new(string)
   455  	*p = gogetenv("GODEBUG")
   456  	godebugEnv.Store(p)
   457  
   458  	setTraceback(gogetenv("GOTRACEBACK"))
   459  	traceback_env = traceback_cache
   460  }
   461  
   462  // reparsedebugvars reparses the runtime's debug variables
   463  // because the environment variable has been changed to env.
   464  func reparsedebugvars(env string) {
   465  	seen := make(map[string]bool)
   466  	// apply environment settings
   467  	parsegodebug(env, seen)
   468  	// apply compile-time GODEBUG settings for as-yet-unseen variables
   469  	parsegodebug(godebugDefault, seen)
   470  	// apply defaults for as-yet-unseen variables
   471  	for _, v := range dbgvars {
   472  		if v.atomic != nil && !seen[v.name] {
   473  			v.atomic.Store(0)
   474  		}
   475  	}
   476  }
   477  
   478  // If an invalid GODEBUG setting is found during startup time,
   479  // invalidGODEBUG is set to that setting so it can be reported
   480  // when initialization has progressed sufficiently.
   481  var invalidGODEBUG struct {
   482  	key, value string
   483  	removed    int
   484  }
   485  
   486  // parsegodebug parses the godebug string, updating variables listed in dbgvars.
   487  // If seen == nil, this is startup time and we process the string left to right
   488  // overwriting older settings with newer ones.
   489  // If seen != nil, $GODEBUG has changed and we are doing an
   490  // incremental update. To avoid flapping in the case where a value is
   491  // set multiple times (perhaps in the default and the environment,
   492  // or perhaps twice in the environment), we process the string right-to-left
   493  // and only change values not already seen. After doing this for both
   494  // the environment and the default settings, the caller must also call
   495  // cleargodebug(seen) to reset any now-unset values back to their defaults.
   496  func parsegodebug(godebug string, seen map[string]bool) {
   497  	for p := godebug; p != ""; {
   498  		var field string
   499  		if seen == nil {
   500  			// startup: process left to right, overwriting older settings with newer
   501  			i := bytealg.IndexByteString(p, ',')
   502  			if i < 0 {
   503  				field, p = p, ""
   504  			} else {
   505  				field, p = p[:i], p[i+1:]
   506  			}
   507  		} else {
   508  			// incremental update: process right to left, updating and skipping seen
   509  			i := len(p) - 1
   510  			for i >= 0 && p[i] != ',' {
   511  				i--
   512  			}
   513  			if i < 0 {
   514  				p, field = "", p
   515  			} else {
   516  				p, field = p[:i], p[i+1:]
   517  			}
   518  		}
   519  		i := bytealg.IndexByteString(field, '=')
   520  		if i < 0 {
   521  			continue
   522  		}
   523  		key, value := field[:i], field[i+1:]
   524  
   525  		// Setting a removed GODEBUG is ok unless it's set to an old value.
   526  		// We only check at startup time per go.dev/issue/76163.
   527  		if seen == nil {
   528  			for _, info := range godebugs.Removed {
   529  				if info.Name == key {
   530  					if info.Old(value) {
   531  						invalidGODEBUG.key = key
   532  						invalidGODEBUG.value = value
   533  						invalidGODEBUG.removed = info.Removed
   534  						return // this skips the cgocheck below but we're about to fatal anyway
   535  					}
   536  					break
   537  				}
   538  			}
   539  		}
   540  
   541  		if seen[key] {
   542  			continue
   543  		}
   544  		if seen != nil {
   545  			seen[key] = true
   546  		}
   547  
   548  		// Update MemProfileRate directly here since it
   549  		// is int, not int32, and should only be updated
   550  		// if specified in GODEBUG.
   551  		if seen == nil && key == "memprofilerate" {
   552  			if n, err := strconv.Atoi(value); err == nil {
   553  				MemProfileRate = n
   554  			}
   555  		} else {
   556  			for _, v := range dbgvars {
   557  				if v.name == key {
   558  					if n, err := strconv.ParseInt(value, 10, 32); err == nil {
   559  						if seen == nil && v.value != nil {
   560  							*v.value = int32(n)
   561  						} else if v.atomic != nil {
   562  							v.atomic.Store(int32(n))
   563  						}
   564  					}
   565  				}
   566  			}
   567  		}
   568  	}
   569  
   570  	if debug.cgocheck > 1 {
   571  		throw("cgocheck > 1 mode is no longer supported at runtime. Use GOEXPERIMENT=cgocheck2 at build time instead.")
   572  	}
   573  }
   574  
   575  //go:linkname setTraceback runtime/debug.SetTraceback
   576  func setTraceback(level string) {
   577  	var t uint32
   578  	switch level {
   579  	case "none":
   580  		t = 0
   581  	case "single", "":
   582  		t = 1 << tracebackShift
   583  	case "all":
   584  		t = 1<<tracebackShift | tracebackAll
   585  	case "system":
   586  		t = 2<<tracebackShift | tracebackAll
   587  	case "crash":
   588  		t = 2<<tracebackShift | tracebackAll | tracebackCrash
   589  	case "wer":
   590  		if GOOS == "windows" {
   591  			t = 2<<tracebackShift | tracebackAll | tracebackCrash
   592  			enableWER()
   593  			break
   594  		}
   595  		fallthrough
   596  	default:
   597  		t = tracebackAll
   598  		if n, err := strconv.Atoi(level); err == nil && n == int(uint32(n)) {
   599  			t |= uint32(n) << tracebackShift
   600  		}
   601  	}
   602  	// when C owns the process, simply exit'ing the process on fatal errors
   603  	// and panics is surprising. Be louder and abort instead.
   604  	if islibrary || isarchive {
   605  		t |= tracebackCrash
   606  	}
   607  
   608  	t |= traceback_env
   609  
   610  	atomic.Store(&traceback_cache, t)
   611  }
   612  
   613  // Helpers for Go. Must be NOSPLIT, must only call NOSPLIT functions, and must not block.
   614  
   615  //go:nosplit
   616  func acquirem() *m {
   617  	gp := getg()
   618  	gp.m.locks++
   619  	return gp.m
   620  }
   621  
   622  //go:nosplit
   623  func releasem(mp *m) {
   624  	gp := getg()
   625  	mp.locks--
   626  	if mp.locks == 0 && gp.preempt {
   627  		// restore the preemption request in case we've cleared it in newstack
   628  		gp.stackguard0 = stackPreempt
   629  	}
   630  }
   631  
   632  // reflect_typelinks is meant for package reflect,
   633  // but widely used packages access it using linkname.
   634  // Notable members of the hall of shame include:
   635  //   - gitee.com/quant1x/gox
   636  //   - github.com/goccy/json
   637  //   - github.com/modern-go/reflect2
   638  //   - github.com/vmware/govmomi
   639  //   - github.com/pinpoint-apm/pinpoint-go-agent
   640  //   - github.com/timandy/routine
   641  //   - github.com/v2pro/plz
   642  //
   643  // Do not remove or change the type signature.
   644  // See go.dev/issue/67401.
   645  //
   646  // This is obsolete and only remains for external packages.
   647  // New code should use reflect_compiledTypelinks.
   648  //
   649  //go:linkname reflect_typelinks reflect.typelinks
   650  func reflect_typelinks() ([]unsafe.Pointer, [][]int32) {
   651  	modules := activeModules()
   652  
   653  	typesToOffsets := func(md *moduledata) []int32 {
   654  		types := moduleTypelinks(md)
   655  		ret := make([]int32, 0, len(types))
   656  		for _, typ := range types {
   657  			ret = append(ret, int32(uintptr(unsafe.Pointer(typ))-md.types))
   658  		}
   659  		return ret
   660  	}
   661  
   662  	sections := []unsafe.Pointer{unsafe.Pointer(modules[0].types)}
   663  	ret := [][]int32{typesToOffsets(modules[0])}
   664  	for _, md := range modules[1:] {
   665  		sections = append(sections, unsafe.Pointer(md.types))
   666  		ret = append(ret, typesToOffsets(md))
   667  	}
   668  	return sections, ret
   669  }
   670  
   671  // reflect_compiledTypelinks returns the typelink types
   672  // generated by the compiler for all current modules.
   673  // The normal case is a single module, so this returns one
   674  // slice for the main module, and a slice of slices, normally nil,
   675  // for other modules.
   676  //
   677  //go:linknamestd reflect_compiledTypelinks reflect.compiledTypelinks
   678  func reflect_compiledTypelinks() ([]*abi.Type, [][]*abi.Type) {
   679  	modules := activeModules()
   680  	firstTypes := moduleTypelinks(modules[0])
   681  	var rest [][]*abi.Type
   682  	for _, md := range modules[1:] {
   683  		rest = append(rest, moduleTypelinks(md))
   684  	}
   685  	return firstTypes, rest
   686  }
   687  
   688  // reflect_resolveNameOff resolves a name offset from a base pointer.
   689  //
   690  // reflect_resolveNameOff is for package reflect,
   691  // but widely used packages access it using linkname.
   692  // Notable members of the hall of shame include:
   693  //   - github.com/agiledragon/gomonkey/v2
   694  //
   695  // Do not remove or change the type signature.
   696  // See go.dev/issue/67401.
   697  //
   698  //go:linkname reflect_resolveNameOff reflect.resolveNameOff
   699  func reflect_resolveNameOff(ptrInModule unsafe.Pointer, off int32) unsafe.Pointer {
   700  	return unsafe.Pointer(resolveNameOff(ptrInModule, nameOff(off)).Bytes)
   701  }
   702  
   703  // reflect_resolveTypeOff resolves an *rtype offset from a base type.
   704  //
   705  // reflect_resolveTypeOff is meant for package reflect,
   706  // but widely used packages access it using linkname.
   707  // Notable members of the hall of shame include:
   708  //   - gitee.com/quant1x/gox
   709  //   - github.com/modern-go/reflect2
   710  //   - github.com/v2pro/plz
   711  //   - github.com/timandy/routine
   712  //
   713  // Do not remove or change the type signature.
   714  // See go.dev/issue/67401.
   715  //
   716  //go:linkname reflect_resolveTypeOff reflect.resolveTypeOff
   717  func reflect_resolveTypeOff(rtype unsafe.Pointer, off int32) unsafe.Pointer {
   718  	return unsafe.Pointer(toRType((*_type)(rtype)).typeOff(typeOff(off)))
   719  }
   720  
   721  // reflect_resolveTextOff resolves a function pointer offset from a base type.
   722  //
   723  // reflect_resolveTextOff is for package reflect,
   724  // but widely used packages access it using linkname.
   725  // Notable members of the hall of shame include:
   726  //   - github.com/agiledragon/gomonkey/v2
   727  //
   728  // Do not remove or change the type signature.
   729  // See go.dev/issue/67401.
   730  //
   731  //go:linkname reflect_resolveTextOff reflect.resolveTextOff
   732  func reflect_resolveTextOff(rtype unsafe.Pointer, off int32) unsafe.Pointer {
   733  	return toRType((*_type)(rtype)).textOff(textOff(off))
   734  }
   735  
   736  // reflectlite_resolveNameOff resolves a name offset from a base pointer.
   737  //
   738  //go:linkname reflectlite_resolveNameOff internal/reflectlite.resolveNameOff
   739  func reflectlite_resolveNameOff(ptrInModule unsafe.Pointer, off int32) unsafe.Pointer {
   740  	return unsafe.Pointer(resolveNameOff(ptrInModule, nameOff(off)).Bytes)
   741  }
   742  
   743  // reflectlite_resolveTypeOff resolves an *rtype offset from a base type.
   744  //
   745  //go:linkname reflectlite_resolveTypeOff internal/reflectlite.resolveTypeOff
   746  func reflectlite_resolveTypeOff(rtype unsafe.Pointer, off int32) unsafe.Pointer {
   747  	return unsafe.Pointer(toRType((*_type)(rtype)).typeOff(typeOff(off)))
   748  }
   749  
   750  // reflect_addReflectOff adds a pointer to the reflection offset lookup map.
   751  //
   752  //go:linkname reflect_addReflectOff reflect.addReflectOff
   753  func reflect_addReflectOff(ptr unsafe.Pointer) int32 {
   754  	reflectOffsLock()
   755  	if reflectOffs.m == nil {
   756  		reflectOffs.m = make(map[int32]unsafe.Pointer)
   757  		reflectOffs.minv = make(map[unsafe.Pointer]int32)
   758  		reflectOffs.next = -1
   759  	}
   760  	id, found := reflectOffs.minv[ptr]
   761  	if !found {
   762  		id = reflectOffs.next
   763  		reflectOffs.next-- // use negative offsets as IDs to aid debugging
   764  		reflectOffs.m[id] = ptr
   765  		reflectOffs.minv[ptr] = id
   766  	}
   767  	reflectOffsUnlock()
   768  	return id
   769  }
   770  
   771  // reflect_adjustAIXGCDataForRuntime takes a type.GCData address and returns
   772  // the new address to use. This is only called on AIX.
   773  // See getGCMaskOnDemand.
   774  //
   775  //go:linknamestd reflect_adjustAIXGCDataForRuntime reflect.adjustAIXGCDataForRuntime
   776  func reflect_adjustAIXGCDataForRuntime(addr *byte) *byte {
   777  	return (*byte)(add(unsafe.Pointer(addr), aixStaticDataBase-firstmoduledata.data))
   778  }
   779  
   780  //go:linkname fips_getIndicator crypto/internal/fips140.getIndicator
   781  func fips_getIndicator() uint8 {
   782  	return getg().fipsIndicator
   783  }
   784  
   785  //go:linkname fips_setIndicator crypto/internal/fips140.setIndicator
   786  func fips_setIndicator(indicator uint8) {
   787  	getg().fipsIndicator = indicator
   788  }
   789  

View as plain text