Source file src/cmd/internal/objabi/pkgspecial.go

     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  package objabi
     6  
     7  import "sync"
     8  
     9  // PkgSpecial indicates special build properties of a given runtime-related
    10  // package.
    11  type PkgSpecial struct {
    12  	// Runtime indicates that this package is "runtime" or imported by
    13  	// "runtime". This has several effects (which maybe should be split out):
    14  	//
    15  	// - Implicit allocation is disallowed.
    16  	//
    17  	// - Various runtime pragmas are enabled.
    18  	//
    19  	// - Optimizations are always enabled.
    20  	//
    21  	// This should be set for runtime and all packages it imports, and may be
    22  	// set for additional packages.
    23  	Runtime bool
    24  
    25  	// NoInstrument indicates this package should not receive sanitizer
    26  	// instrumentation. In many of these, instrumentation could cause infinite
    27  	// recursion. This is all runtime packages, plus those that support the
    28  	// sanitizers.
    29  	NoInstrument bool
    30  
    31  	// NoRaceFunc indicates functions in this package should not get
    32  	// racefuncenter/racefuncexit instrumentation Memory accesses in these
    33  	// packages are either uninteresting or will cause false positives.
    34  	NoRaceFunc bool
    35  
    36  	// AllowAsmABI indicates that assembly in this package is allowed to use ABI
    37  	// selectors in symbol names. Generally this is needed for packages that
    38  	// interact closely with the runtime package or have performance-critical
    39  	// assembly.
    40  	AllowAsmABI bool
    41  }
    42  
    43  var runtimePkgs = []string{
    44  	"runtime",
    45  
    46  	"runtime/internal/atomic",
    47  	"runtime/internal/math",
    48  	"runtime/internal/sys",
    49  	"runtime/internal/syscall",
    50  
    51  	"internal/abi",
    52  	"internal/bytealg",
    53  	"internal/chacha8rand",
    54  	"internal/coverage/rtcov",
    55  	"internal/cpu",
    56  	"internal/goarch",
    57  	"internal/godebugs",
    58  	"internal/goexperiment",
    59  	"internal/goos",
    60  }
    61  
    62  // extraNoInstrumentPkgs is the set of packages in addition to runtimePkgs that
    63  // should have NoInstrument set.
    64  var extraNoInstrumentPkgs = []string{
    65  	"runtime/race",
    66  	"runtime/msan",
    67  	"runtime/asan",
    68  	// We omit bytealg even though it's imported by runtime because it also
    69  	// backs a lot of package bytes. Currently we don't have a way to omit race
    70  	// instrumentation when used from the runtime while keeping race
    71  	// instrumentation when used from user code. Somehow this doesn't seem to
    72  	// cause problems, though we may be skating on thin ice. See #61204.
    73  	"-internal/bytealg",
    74  }
    75  
    76  var noRaceFuncPkgs = []string{"sync", "sync/atomic"}
    77  
    78  var allowAsmABIPkgs = []string{
    79  	"runtime",
    80  	"reflect",
    81  	"syscall",
    82  	"internal/bytealg",
    83  	"internal/chacha8rand",
    84  	"runtime/internal/syscall",
    85  	"runtime/internal/startlinetest",
    86  }
    87  
    88  var (
    89  	pkgSpecials     map[string]PkgSpecial
    90  	pkgSpecialsOnce sync.Once
    91  )
    92  
    93  // LookupPkgSpecial returns special build properties for the given package path.
    94  func LookupPkgSpecial(pkgPath string) PkgSpecial {
    95  	pkgSpecialsOnce.Do(func() {
    96  		// Construct pkgSpecials from various package lists. This lets us use
    97  		// more flexible logic, while keeping the final map simple, and avoids
    98  		// the init-time cost of a map.
    99  		pkgSpecials = make(map[string]PkgSpecial)
   100  		set := func(elt string, f func(*PkgSpecial)) {
   101  			s := pkgSpecials[elt]
   102  			f(&s)
   103  			pkgSpecials[elt] = s
   104  		}
   105  		for _, pkg := range runtimePkgs {
   106  			set(pkg, func(ps *PkgSpecial) { ps.Runtime = true; ps.NoInstrument = true })
   107  		}
   108  		for _, pkg := range extraNoInstrumentPkgs {
   109  			if pkg[0] == '-' {
   110  				set(pkg[1:], func(ps *PkgSpecial) { ps.NoInstrument = false })
   111  			} else {
   112  				set(pkg, func(ps *PkgSpecial) { ps.NoInstrument = true })
   113  			}
   114  		}
   115  		for _, pkg := range noRaceFuncPkgs {
   116  			set(pkg, func(ps *PkgSpecial) { ps.NoRaceFunc = true })
   117  		}
   118  		for _, pkg := range allowAsmABIPkgs {
   119  			set(pkg, func(ps *PkgSpecial) { ps.AllowAsmABI = true })
   120  		}
   121  	})
   122  	return pkgSpecials[pkgPath]
   123  }
   124  

View as plain text