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/bytealg"
     9  	"internal/goarch"
    10  	"internal/runtime/atomic"
    11  	"unsafe"
    12  )
    13  
    14  // Keep a cached value to make gotraceback fast,
    15  // since we call it on every call to gentraceback.
    16  // The cached value is a uint32 in which the low bits
    17  // are the "crash" and "all" settings and the remaining
    18  // bits are the traceback value (0 off, 1 on, 2 include system).
    19  const (
    20  	tracebackCrash = 1 << iota
    21  	tracebackAll
    22  	tracebackShift = iota
    23  )
    24  
    25  var traceback_cache uint32 = 2 << tracebackShift
    26  var traceback_env uint32
    27  
    28  // gotraceback returns the current traceback settings.
    29  //
    30  // If level is 0, suppress all tracebacks.
    31  // If level is 1, show tracebacks, but exclude runtime frames.
    32  // If level is 2, show tracebacks including runtime frames.
    33  // If all is set, print all goroutine stacks. Otherwise, print just the current goroutine.
    34  // If crash is set, crash (core dump, etc) after tracebacking.
    35  //
    36  //go:nosplit
    37  func gotraceback() (level int32, all, crash bool) {
    38  	gp := getg()
    39  	t := atomic.Load(&traceback_cache)
    40  	crash = t&tracebackCrash != 0
    41  	all = gp.m.throwing >= throwTypeUser || t&tracebackAll != 0
    42  	if gp.m.traceback != 0 {
    43  		level = int32(gp.m.traceback)
    44  	} else if gp.m.throwing >= throwTypeRuntime {
    45  		// Always include runtime frames in runtime throws unless
    46  		// otherwise overridden by m.traceback.
    47  		level = 2
    48  	} else {
    49  		level = int32(t >> tracebackShift)
    50  	}
    51  	return
    52  }
    53  
    54  var (
    55  	argc int32
    56  	argv **byte
    57  )
    58  
    59  // nosplit for use in linux startup sysargs.
    60  //
    61  //go:nosplit
    62  func argv_index(argv **byte, i int32) *byte {
    63  	return *(**byte)(add(unsafe.Pointer(argv), uintptr(i)*goarch.PtrSize))
    64  }
    65  
    66  func args(c int32, v **byte) {
    67  	argc = c
    68  	argv = v
    69  	sysargs(c, v)
    70  }
    71  
    72  func goargs() {
    73  	if GOOS == "windows" {
    74  		return
    75  	}
    76  	argslice = make([]string, argc)
    77  	for i := int32(0); i < argc; i++ {
    78  		argslice[i] = gostringnocopy(argv_index(argv, i))
    79  	}
    80  }
    81  
    82  func goenvs_unix() {
    83  	// TODO(austin): ppc64 in dynamic linking mode doesn't
    84  	// guarantee env[] will immediately follow argv. Might cause
    85  	// problems.
    86  	n := int32(0)
    87  	for argv_index(argv, argc+1+n) != nil {
    88  		n++
    89  	}
    90  
    91  	envs = make([]string, n)
    92  	for i := int32(0); i < n; i++ {
    93  		envs[i] = gostring(argv_index(argv, argc+1+i))
    94  	}
    95  }
    96  
    97  func environ() []string {
    98  	return envs
    99  }
   100  
   101  // TODO: These should be locals in testAtomic64, but we don't 8-byte
   102  // align stack variables on 386.
   103  var test_z64, test_x64 uint64
   104  
   105  func testAtomic64() {
   106  	test_z64 = 42
   107  	test_x64 = 0
   108  	if atomic.Cas64(&test_z64, test_x64, 1) {
   109  		throw("cas64 failed")
   110  	}
   111  	if test_x64 != 0 {
   112  		throw("cas64 failed")
   113  	}
   114  	test_x64 = 42
   115  	if !atomic.Cas64(&test_z64, test_x64, 1) {
   116  		throw("cas64 failed")
   117  	}
   118  	if test_x64 != 42 || test_z64 != 1 {
   119  		throw("cas64 failed")
   120  	}
   121  	if atomic.Load64(&test_z64) != 1 {
   122  		throw("load64 failed")
   123  	}
   124  	atomic.Store64(&test_z64, (1<<40)+1)
   125  	if atomic.Load64(&test_z64) != (1<<40)+1 {
   126  		throw("store64 failed")
   127  	}
   128  	if atomic.Xadd64(&test_z64, (1<<40)+1) != (2<<40)+2 {
   129  		throw("xadd64 failed")
   130  	}
   131  	if atomic.Load64(&test_z64) != (2<<40)+2 {
   132  		throw("xadd64 failed")
   133  	}
   134  	if atomic.Xchg64(&test_z64, (3<<40)+3) != (2<<40)+2 {
   135  		throw("xchg64 failed")
   136  	}
   137  	if atomic.Load64(&test_z64) != (3<<40)+3 {
   138  		throw("xchg64 failed")
   139  	}
   140  }
   141  
   142  func check() {
   143  	var (
   144  		a     int8
   145  		b     uint8
   146  		c     int16
   147  		d     uint16
   148  		e     int32
   149  		f     uint32
   150  		g     int64
   151  		h     uint64
   152  		i, i1 float32
   153  		j, j1 float64
   154  		k     unsafe.Pointer
   155  		l     *uint16
   156  		m     [4]byte
   157  	)
   158  	type x1t struct {
   159  		x uint8
   160  	}
   161  	type y1t struct {
   162  		x1 x1t
   163  		y  uint8
   164  	}
   165  	var x1 x1t
   166  	var y1 y1t
   167  
   168  	if unsafe.Sizeof(a) != 1 {
   169  		throw("bad a")
   170  	}
   171  	if unsafe.Sizeof(b) != 1 {
   172  		throw("bad b")
   173  	}
   174  	if unsafe.Sizeof(c) != 2 {
   175  		throw("bad c")
   176  	}
   177  	if unsafe.Sizeof(d) != 2 {
   178  		throw("bad d")
   179  	}
   180  	if unsafe.Sizeof(e) != 4 {
   181  		throw("bad e")
   182  	}
   183  	if unsafe.Sizeof(f) != 4 {
   184  		throw("bad f")
   185  	}
   186  	if unsafe.Sizeof(g) != 8 {
   187  		throw("bad g")
   188  	}
   189  	if unsafe.Sizeof(h) != 8 {
   190  		throw("bad h")
   191  	}
   192  	if unsafe.Sizeof(i) != 4 {
   193  		throw("bad i")
   194  	}
   195  	if unsafe.Sizeof(j) != 8 {
   196  		throw("bad j")
   197  	}
   198  	if unsafe.Sizeof(k) != goarch.PtrSize {
   199  		throw("bad k")
   200  	}
   201  	if unsafe.Sizeof(l) != goarch.PtrSize {
   202  		throw("bad l")
   203  	}
   204  	if unsafe.Sizeof(x1) != 1 {
   205  		throw("bad unsafe.Sizeof x1")
   206  	}
   207  	if unsafe.Offsetof(y1.y) != 1 {
   208  		throw("bad offsetof y1.y")
   209  	}
   210  	if unsafe.Sizeof(y1) != 2 {
   211  		throw("bad unsafe.Sizeof y1")
   212  	}
   213  
   214  	if timediv(12345*1000000000+54321, 1000000000, &e) != 12345 || e != 54321 {
   215  		throw("bad timediv")
   216  	}
   217  
   218  	var z uint32
   219  	z = 1
   220  	if !atomic.Cas(&z, 1, 2) {
   221  		throw("cas1")
   222  	}
   223  	if z != 2 {
   224  		throw("cas2")
   225  	}
   226  
   227  	z = 4
   228  	if atomic.Cas(&z, 5, 6) {
   229  		throw("cas3")
   230  	}
   231  	if z != 4 {
   232  		throw("cas4")
   233  	}
   234  
   235  	z = 0xffffffff
   236  	if !atomic.Cas(&z, 0xffffffff, 0xfffffffe) {
   237  		throw("cas5")
   238  	}
   239  	if z != 0xfffffffe {
   240  		throw("cas6")
   241  	}
   242  
   243  	m = [4]byte{1, 1, 1, 1}
   244  	atomic.Or8(&m[1], 0xf0)
   245  	if m[0] != 1 || m[1] != 0xf1 || m[2] != 1 || m[3] != 1 {
   246  		throw("atomicor8")
   247  	}
   248  
   249  	m = [4]byte{0xff, 0xff, 0xff, 0xff}
   250  	atomic.And8(&m[1], 0x1)
   251  	if m[0] != 0xff || m[1] != 0x1 || m[2] != 0xff || m[3] != 0xff {
   252  		throw("atomicand8")
   253  	}
   254  
   255  	*(*uint64)(unsafe.Pointer(&j)) = ^uint64(0)
   256  	if j == j {
   257  		throw("float64nan")
   258  	}
   259  	if !(j != j) {
   260  		throw("float64nan1")
   261  	}
   262  
   263  	*(*uint64)(unsafe.Pointer(&j1)) = ^uint64(1)
   264  	if j == j1 {
   265  		throw("float64nan2")
   266  	}
   267  	if !(j != j1) {
   268  		throw("float64nan3")
   269  	}
   270  
   271  	*(*uint32)(unsafe.Pointer(&i)) = ^uint32(0)
   272  	if i == i {
   273  		throw("float32nan")
   274  	}
   275  	if i == i {
   276  		throw("float32nan1")
   277  	}
   278  
   279  	*(*uint32)(unsafe.Pointer(&i1)) = ^uint32(1)
   280  	if i == i1 {
   281  		throw("float32nan2")
   282  	}
   283  	if i == i1 {
   284  		throw("float32nan3")
   285  	}
   286  
   287  	testAtomic64()
   288  
   289  	if fixedStack != round2(fixedStack) {
   290  		throw("FixedStack is not power-of-2")
   291  	}
   292  
   293  	if !checkASM() {
   294  		throw("assembly checks failed")
   295  	}
   296  }
   297  
   298  type dbgVar struct {
   299  	name   string
   300  	value  *int32        // for variables that can only be set at startup
   301  	atomic *atomic.Int32 // for variables that can be changed during execution
   302  	def    int32         // default value (ideally zero)
   303  }
   304  
   305  // Holds variables parsed from GODEBUG env var,
   306  // except for "memprofilerate" since there is an
   307  // existing int var for that value, which may
   308  // already have an initial value.
   309  var debug struct {
   310  	cgocheck                 int32
   311  	clobberfree              int32
   312  	disablethp               int32
   313  	dontfreezetheworld       int32
   314  	efence                   int32
   315  	gccheckmark              int32
   316  	gcpacertrace             int32
   317  	gcshrinkstackoff         int32
   318  	gcstoptheworld           int32
   319  	gctrace                  int32
   320  	invalidptr               int32
   321  	madvdontneed             int32 // for Linux; issue 28466
   322  	runtimeContentionStacks  atomic.Int32
   323  	scavtrace                int32
   324  	scheddetail              int32
   325  	schedtrace               int32
   326  	tracebackancestors       int32
   327  	asyncpreemptoff          int32
   328  	harddecommit             int32
   329  	adaptivestackstart       int32
   330  	tracefpunwindoff         int32
   331  	traceadvanceperiod       int32
   332  	traceCheckStackOwnership int32
   333  
   334  	// debug.malloc is used as a combined debug check
   335  	// in the malloc function and should be set
   336  	// if any of the below debug options is != 0.
   337  	malloc         bool
   338  	allocfreetrace int32
   339  	inittrace      int32
   340  	sbrk           int32
   341  
   342  	panicnil atomic.Int32
   343  
   344  	// asynctimerchan controls whether timer channels
   345  	// behave asynchronously (as in Go 1.22 and earlier)
   346  	// instead of their Go 1.23+ synchronous behavior.
   347  	// The value can change at any time (in response to os.Setenv("GODEBUG"))
   348  	// and affects all extant timer channels immediately.
   349  	// Programs wouldn't normally change over an execution,
   350  	// but allowing it is convenient for testing and for programs
   351  	// that do an os.Setenv in main.init or main.main.
   352  	asynctimerchan atomic.Int32
   353  }
   354  
   355  var dbgvars = []*dbgVar{
   356  	{name: "adaptivestackstart", value: &debug.adaptivestackstart},
   357  	{name: "allocfreetrace", value: &debug.allocfreetrace},
   358  	{name: "asyncpreemptoff", value: &debug.asyncpreemptoff},
   359  	{name: "asynctimerchan", atomic: &debug.asynctimerchan},
   360  	{name: "cgocheck", value: &debug.cgocheck},
   361  	{name: "clobberfree", value: &debug.clobberfree},
   362  	{name: "disablethp", value: &debug.disablethp},
   363  	{name: "dontfreezetheworld", value: &debug.dontfreezetheworld},
   364  	{name: "efence", value: &debug.efence},
   365  	{name: "gccheckmark", value: &debug.gccheckmark},
   366  	{name: "gcpacertrace", value: &debug.gcpacertrace},
   367  	{name: "gcshrinkstackoff", value: &debug.gcshrinkstackoff},
   368  	{name: "gcstoptheworld", value: &debug.gcstoptheworld},
   369  	{name: "gctrace", value: &debug.gctrace},
   370  	{name: "harddecommit", value: &debug.harddecommit},
   371  	{name: "inittrace", value: &debug.inittrace},
   372  	{name: "invalidptr", value: &debug.invalidptr},
   373  	{name: "madvdontneed", value: &debug.madvdontneed},
   374  	{name: "panicnil", atomic: &debug.panicnil},
   375  	{name: "runtimecontentionstacks", atomic: &debug.runtimeContentionStacks},
   376  	{name: "sbrk", value: &debug.sbrk},
   377  	{name: "scavtrace", value: &debug.scavtrace},
   378  	{name: "scheddetail", value: &debug.scheddetail},
   379  	{name: "schedtrace", value: &debug.schedtrace},
   380  	{name: "traceadvanceperiod", value: &debug.traceadvanceperiod},
   381  	{name: "tracecheckstackownership", value: &debug.traceCheckStackOwnership},
   382  	{name: "tracebackancestors", value: &debug.tracebackancestors},
   383  	{name: "tracefpunwindoff", value: &debug.tracefpunwindoff},
   384  }
   385  
   386  func parsedebugvars() {
   387  	// defaults
   388  	debug.cgocheck = 1
   389  	debug.invalidptr = 1
   390  	debug.adaptivestackstart = 1 // set this to 0 to turn larger initial goroutine stacks off
   391  	if GOOS == "linux" {
   392  		// On Linux, MADV_FREE is faster than MADV_DONTNEED,
   393  		// but doesn't affect many of the statistics that
   394  		// MADV_DONTNEED does until the memory is actually
   395  		// reclaimed. This generally leads to poor user
   396  		// experience, like confusing stats in top and other
   397  		// monitoring tools; and bad integration with
   398  		// management systems that respond to memory usage.
   399  		// Hence, default to MADV_DONTNEED.
   400  		debug.madvdontneed = 1
   401  	}
   402  	debug.traceadvanceperiod = defaultTraceAdvancePeriod
   403  
   404  	godebug := gogetenv("GODEBUG")
   405  
   406  	p := new(string)
   407  	*p = godebug
   408  	godebugEnv.Store(p)
   409  
   410  	// apply runtime defaults, if any
   411  	for _, v := range dbgvars {
   412  		if v.def != 0 {
   413  			// Every var should have either v.value or v.atomic set.
   414  			if v.value != nil {
   415  				*v.value = v.def
   416  			} else if v.atomic != nil {
   417  				v.atomic.Store(v.def)
   418  			}
   419  		}
   420  	}
   421  
   422  	// apply compile-time GODEBUG settings
   423  	parsegodebug(godebugDefault, nil)
   424  
   425  	// apply environment settings
   426  	parsegodebug(godebug, nil)
   427  
   428  	debug.malloc = (debug.allocfreetrace | debug.inittrace | debug.sbrk) != 0
   429  
   430  	setTraceback(gogetenv("GOTRACEBACK"))
   431  	traceback_env = traceback_cache
   432  }
   433  
   434  // reparsedebugvars reparses the runtime's debug variables
   435  // because the environment variable has been changed to env.
   436  func reparsedebugvars(env string) {
   437  	seen := make(map[string]bool)
   438  	// apply environment settings
   439  	parsegodebug(env, seen)
   440  	// apply compile-time GODEBUG settings for as-yet-unseen variables
   441  	parsegodebug(godebugDefault, seen)
   442  	// apply defaults for as-yet-unseen variables
   443  	for _, v := range dbgvars {
   444  		if v.atomic != nil && !seen[v.name] {
   445  			v.atomic.Store(0)
   446  		}
   447  	}
   448  }
   449  
   450  // parsegodebug parses the godebug string, updating variables listed in dbgvars.
   451  // If seen == nil, this is startup time and we process the string left to right
   452  // overwriting older settings with newer ones.
   453  // If seen != nil, $GODEBUG has changed and we are doing an
   454  // incremental update. To avoid flapping in the case where a value is
   455  // set multiple times (perhaps in the default and the environment,
   456  // or perhaps twice in the environment), we process the string right-to-left
   457  // and only change values not already seen. After doing this for both
   458  // the environment and the default settings, the caller must also call
   459  // cleargodebug(seen) to reset any now-unset values back to their defaults.
   460  func parsegodebug(godebug string, seen map[string]bool) {
   461  	for p := godebug; p != ""; {
   462  		var field string
   463  		if seen == nil {
   464  			// startup: process left to right, overwriting older settings with newer
   465  			i := bytealg.IndexByteString(p, ',')
   466  			if i < 0 {
   467  				field, p = p, ""
   468  			} else {
   469  				field, p = p[:i], p[i+1:]
   470  			}
   471  		} else {
   472  			// incremental update: process right to left, updating and skipping seen
   473  			i := len(p) - 1
   474  			for i >= 0 && p[i] != ',' {
   475  				i--
   476  			}
   477  			if i < 0 {
   478  				p, field = "", p
   479  			} else {
   480  				p, field = p[:i], p[i+1:]
   481  			}
   482  		}
   483  		i := bytealg.IndexByteString(field, '=')
   484  		if i < 0 {
   485  			continue
   486  		}
   487  		key, value := field[:i], field[i+1:]
   488  		if seen[key] {
   489  			continue
   490  		}
   491  		if seen != nil {
   492  			seen[key] = true
   493  		}
   494  
   495  		// Update MemProfileRate directly here since it
   496  		// is int, not int32, and should only be updated
   497  		// if specified in GODEBUG.
   498  		if seen == nil && key == "memprofilerate" {
   499  			if n, ok := atoi(value); ok {
   500  				MemProfileRate = n
   501  			}
   502  		} else {
   503  			for _, v := range dbgvars {
   504  				if v.name == key {
   505  					if n, ok := atoi32(value); ok {
   506  						if seen == nil && v.value != nil {
   507  							*v.value = n
   508  						} else if v.atomic != nil {
   509  							v.atomic.Store(n)
   510  						}
   511  					}
   512  				}
   513  			}
   514  		}
   515  	}
   516  
   517  	if debug.cgocheck > 1 {
   518  		throw("cgocheck > 1 mode is no longer supported at runtime. Use GOEXPERIMENT=cgocheck2 at build time instead.")
   519  	}
   520  }
   521  
   522  //go:linkname setTraceback runtime/debug.SetTraceback
   523  func setTraceback(level string) {
   524  	var t uint32
   525  	switch level {
   526  	case "none":
   527  		t = 0
   528  	case "single", "":
   529  		t = 1 << tracebackShift
   530  	case "all":
   531  		t = 1<<tracebackShift | tracebackAll
   532  	case "system":
   533  		t = 2<<tracebackShift | tracebackAll
   534  	case "crash":
   535  		t = 2<<tracebackShift | tracebackAll | tracebackCrash
   536  	case "wer":
   537  		if GOOS == "windows" {
   538  			t = 2<<tracebackShift | tracebackAll | tracebackCrash
   539  			enableWER()
   540  			break
   541  		}
   542  		fallthrough
   543  	default:
   544  		t = tracebackAll
   545  		if n, ok := atoi(level); ok && n == int(uint32(n)) {
   546  			t |= uint32(n) << tracebackShift
   547  		}
   548  	}
   549  	// when C owns the process, simply exit'ing the process on fatal errors
   550  	// and panics is surprising. Be louder and abort instead.
   551  	if islibrary || isarchive {
   552  		t |= tracebackCrash
   553  	}
   554  
   555  	t |= traceback_env
   556  
   557  	atomic.Store(&traceback_cache, t)
   558  }
   559  
   560  // Poor mans 64-bit division.
   561  // This is a very special function, do not use it if you are not sure what you are doing.
   562  // int64 division is lowered into _divv() call on 386, which does not fit into nosplit functions.
   563  // Handles overflow in a time-specific manner.
   564  // This keeps us within no-split stack limits on 32-bit processors.
   565  //
   566  //go:nosplit
   567  func timediv(v int64, div int32, rem *int32) int32 {
   568  	res := int32(0)
   569  	for bit := 30; bit >= 0; bit-- {
   570  		if v >= int64(div)<<uint(bit) {
   571  			v = v - (int64(div) << uint(bit))
   572  			// Before this for loop, res was 0, thus all these
   573  			// power of 2 increments are now just bitsets.
   574  			res |= 1 << uint(bit)
   575  		}
   576  	}
   577  	if v >= int64(div) {
   578  		if rem != nil {
   579  			*rem = 0
   580  		}
   581  		return 0x7fffffff
   582  	}
   583  	if rem != nil {
   584  		*rem = int32(v)
   585  	}
   586  	return res
   587  }
   588  
   589  // Helpers for Go. Must be NOSPLIT, must only call NOSPLIT functions, and must not block.
   590  
   591  //go:nosplit
   592  func acquirem() *m {
   593  	gp := getg()
   594  	gp.m.locks++
   595  	return gp.m
   596  }
   597  
   598  //go:nosplit
   599  func releasem(mp *m) {
   600  	gp := getg()
   601  	mp.locks--
   602  	if mp.locks == 0 && gp.preempt {
   603  		// restore the preemption request in case we've cleared it in newstack
   604  		gp.stackguard0 = stackPreempt
   605  	}
   606  }
   607  
   608  //go:linkname reflect_typelinks reflect.typelinks
   609  func reflect_typelinks() ([]unsafe.Pointer, [][]int32) {
   610  	modules := activeModules()
   611  	sections := []unsafe.Pointer{unsafe.Pointer(modules[0].types)}
   612  	ret := [][]int32{modules[0].typelinks}
   613  	for _, md := range modules[1:] {
   614  		sections = append(sections, unsafe.Pointer(md.types))
   615  		ret = append(ret, md.typelinks)
   616  	}
   617  	return sections, ret
   618  }
   619  
   620  // reflect_resolveNameOff resolves a name offset from a base pointer.
   621  //
   622  //go:linkname reflect_resolveNameOff reflect.resolveNameOff
   623  func reflect_resolveNameOff(ptrInModule unsafe.Pointer, off int32) unsafe.Pointer {
   624  	return unsafe.Pointer(resolveNameOff(ptrInModule, nameOff(off)).Bytes)
   625  }
   626  
   627  // reflect_resolveTypeOff resolves an *rtype offset from a base type.
   628  //
   629  //go:linkname reflect_resolveTypeOff reflect.resolveTypeOff
   630  func reflect_resolveTypeOff(rtype unsafe.Pointer, off int32) unsafe.Pointer {
   631  	return unsafe.Pointer(toRType((*_type)(rtype)).typeOff(typeOff(off)))
   632  }
   633  
   634  // reflect_resolveTextOff resolves a function pointer offset from a base type.
   635  //
   636  //go:linkname reflect_resolveTextOff reflect.resolveTextOff
   637  func reflect_resolveTextOff(rtype unsafe.Pointer, off int32) unsafe.Pointer {
   638  	return toRType((*_type)(rtype)).textOff(textOff(off))
   639  }
   640  
   641  // reflectlite_resolveNameOff resolves a name offset from a base pointer.
   642  //
   643  //go:linkname reflectlite_resolveNameOff internal/reflectlite.resolveNameOff
   644  func reflectlite_resolveNameOff(ptrInModule unsafe.Pointer, off int32) unsafe.Pointer {
   645  	return unsafe.Pointer(resolveNameOff(ptrInModule, nameOff(off)).Bytes)
   646  }
   647  
   648  // reflectlite_resolveTypeOff resolves an *rtype offset from a base type.
   649  //
   650  //go:linkname reflectlite_resolveTypeOff internal/reflectlite.resolveTypeOff
   651  func reflectlite_resolveTypeOff(rtype unsafe.Pointer, off int32) unsafe.Pointer {
   652  	return unsafe.Pointer(toRType((*_type)(rtype)).typeOff(typeOff(off)))
   653  }
   654  
   655  // reflect_addReflectOff adds a pointer to the reflection offset lookup map.
   656  //
   657  //go:linkname reflect_addReflectOff reflect.addReflectOff
   658  func reflect_addReflectOff(ptr unsafe.Pointer) int32 {
   659  	reflectOffsLock()
   660  	if reflectOffs.m == nil {
   661  		reflectOffs.m = make(map[int32]unsafe.Pointer)
   662  		reflectOffs.minv = make(map[unsafe.Pointer]int32)
   663  		reflectOffs.next = -1
   664  	}
   665  	id, found := reflectOffs.minv[ptr]
   666  	if !found {
   667  		id = reflectOffs.next
   668  		reflectOffs.next-- // use negative offsets as IDs to aid debugging
   669  		reflectOffs.m[id] = ptr
   670  		reflectOffs.minv[ptr] = id
   671  	}
   672  	reflectOffsUnlock()
   673  	return id
   674  }
   675  

View as plain text