Source file src/runtime/covermeta.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 runtime
     6  
     7  import (
     8  	"internal/coverage/rtcov"
     9  	"unsafe"
    10  )
    11  
    12  // covMeta is the top-level container for bits of state related to
    13  // code coverage meta-data in the runtime.
    14  var covMeta struct {
    15  	// metaList contains the list of currently registered meta-data
    16  	// blobs for the running program.
    17  	metaList []rtcov.CovMetaBlob
    18  
    19  	// pkgMap records mappings from hard-coded package IDs to
    20  	// slots in the covMetaList above.
    21  	pkgMap map[int]int
    22  
    23  	// Set to true if we discover a package mapping glitch.
    24  	hardCodedListNeedsUpdating bool
    25  }
    26  
    27  // addCovMeta is invoked during package "init" functions by the
    28  // compiler when compiling for coverage instrumentation; here 'p' is a
    29  // meta-data blob of length 'dlen' for the package in question, 'hash'
    30  // is a compiler-computed md5.sum for the blob, 'pkpath' is the
    31  // package path, 'pkid' is the hard-coded ID that the compiler is
    32  // using for the package (or -1 if the compiler doesn't think a
    33  // hard-coded ID is needed), and 'cmode'/'cgran' are the coverage
    34  // counter mode and granularity requested by the user. Return value is
    35  // the ID for the package for use by the package code itself.
    36  func addCovMeta(p unsafe.Pointer, dlen uint32, hash [16]byte, pkpath string, pkid int, cmode uint8, cgran uint8) uint32 {
    37  	slot := len(covMeta.metaList)
    38  	covMeta.metaList = append(covMeta.metaList,
    39  		rtcov.CovMetaBlob{
    40  			P:                  (*byte)(p),
    41  			Len:                dlen,
    42  			Hash:               hash,
    43  			PkgPath:            pkpath,
    44  			PkgID:              pkid,
    45  			CounterMode:        cmode,
    46  			CounterGranularity: cgran,
    47  		})
    48  	if pkid != -1 {
    49  		if covMeta.pkgMap == nil {
    50  			covMeta.pkgMap = make(map[int]int)
    51  		}
    52  		if _, ok := covMeta.pkgMap[pkid]; ok {
    53  			throw("runtime.addCovMeta: coverage package map collision")
    54  		}
    55  		// Record the real slot (position on meta-list) for this
    56  		// package; we'll use the map to fix things up later on.
    57  		covMeta.pkgMap[pkid] = slot
    58  	}
    59  
    60  	// ID zero is reserved as invalid.
    61  	return uint32(slot + 1)
    62  }
    63  
    64  //go:linkname runtime_coverage_getCovMetaList runtime/coverage.getCovMetaList
    65  func runtime_coverage_getCovMetaList() []rtcov.CovMetaBlob {
    66  	return covMeta.metaList
    67  }
    68  
    69  //go:linkname runtime_coverage_getCovPkgMap runtime/coverage.getCovPkgMap
    70  func runtime_coverage_getCovPkgMap() map[int]int {
    71  	return covMeta.pkgMap
    72  }
    73  

View as plain text