Source file src/internal/coverage/pkid.go
1 // Copyright 2022 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 coverage 6 7 // Building the runtime package with coverage instrumentation enabled 8 // is tricky. For all other packages, you can be guaranteed that 9 // the package init function is run before any functions are executed, 10 // but this invariant is not maintained for packages such as "runtime", 11 // "internal/cpu", etc. To handle this, hard-code the package ID for 12 // the set of packages whose functions may be running before the 13 // init function of the package is complete. 14 // 15 // Hardcoding is unfortunate because it means that the tool that does 16 // coverage instrumentation has to keep a list of runtime packages, 17 // meaning that if someone makes changes to the pkg "runtime" 18 // dependencies, unexpected behavior will result for coverage builds. 19 // The coverage runtime will detect and report the unexpected 20 // behavior; look for an error of this form: 21 // 22 // internal error in coverage meta-data tracking: 23 // list of hard-coded runtime package IDs needs revising. 24 // registered list: 25 // slot: 0 path='internal/cpu' hard-coded id: 1 26 // slot: 1 path='internal/goarch' hard-coded id: 2 27 // slot: 2 path='internal/runtime/atomic' hard-coded id: 3 28 // slot: 3 path='internal/goos' 29 // slot: 4 path='internal/runtime/sys' hard-coded id: 5 30 // slot: 5 path='internal/abi' hard-coded id: 4 31 // slot: 6 path='internal/runtime/math' hard-coded id: 6 32 // slot: 7 path='internal/bytealg' hard-coded id: 7 33 // slot: 8 path='internal/goexperiment' 34 // slot: 9 path='internal/runtime/syscall' hard-coded id: 8 35 // slot: 10 path='runtime' hard-coded id: 9 36 // fatal error: runtime.addCovMeta 37 // 38 // For the error above, the hard-coded list is missing "internal/goos" 39 // and "internal/goexperiment" ; the developer in question will need 40 // to copy the list above into "rtPkgs" below. 41 // 42 // Note: this strategy assumes that the list of dependencies of 43 // package runtime is fixed, and doesn't vary depending on OS/arch. If 44 // this were to be the case, we would need a table of some sort below 45 // as opposed to a fixed list. 46 47 var rtPkgs = [...]string{ 48 "internal/cpu", 49 "internal/goarch", 50 "internal/runtime/atomic", 51 "internal/goos", 52 "internal/chacha8rand", 53 "internal/runtime/sys", 54 "internal/abi", 55 "internal/runtime/maps", 56 "internal/runtime/math", 57 "internal/bytealg", 58 "internal/goexperiment", 59 "internal/runtime/syscall", 60 "internal/stringslite", 61 "runtime", 62 } 63 64 // Scoping note: the constants and apis in this file are internal 65 // only, not expected to ever be exposed outside of the runtime (unlike 66 // other coverage file formats and APIs, which will likely be shared 67 // at some point). 68 69 // NotHardCoded is a package pseudo-ID indicating that a given package 70 // is not part of the runtime and doesn't require a hard-coded ID. 71 const NotHardCoded = -1 72 73 // HardCodedPkgID returns the hard-coded ID for the specified package 74 // path, or -1 if we don't use a hard-coded ID. Hard-coded IDs start 75 // at -2 and decrease as we go down the list. 76 func HardCodedPkgID(pkgpath string) int { 77 for k, p := range rtPkgs { 78 if p == pkgpath { 79 return (0 - k) - 2 80 } 81 } 82 return NotHardCoded 83 } 84