github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/coverage/apis.go (about) 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 import ( 8 "fmt" 9 "internal/coverage" 10 "io" 11 "reflect" 12 "sync/atomic" 13 "unsafe" 14 ) 15 16 // WriteMetaDir writes a coverage meta-data file for the currently 17 // running program to the directory specified in 'dir'. An error will 18 // be returned if the operation can't be completed successfully (for 19 // example, if the currently running program was not built with 20 // "-cover", or if the directory does not exist). 21 func WriteMetaDir(dir string) error { 22 if !finalHashComputed { 23 return fmt.Errorf("error: no meta-data available (binary not built with -cover?)") 24 } 25 return emitMetaDataToDirectory(dir, getCovMetaList()) 26 } 27 28 // WriteMeta writes the meta-data content (the payload that would 29 // normally be emitted to a meta-data file) for the currently running 30 // program to the writer 'w'. An error will be returned if the 31 // operation can't be completed successfully (for example, if the 32 // currently running program was not built with "-cover", or if a 33 // write fails). 34 func WriteMeta(w io.Writer) error { 35 if w == nil { 36 return fmt.Errorf("error: nil writer in WriteMeta") 37 } 38 if !finalHashComputed { 39 return fmt.Errorf("error: no meta-data available (binary not built with -cover?)") 40 } 41 ml := getCovMetaList() 42 return writeMetaData(w, ml, cmode, cgran, finalHash) 43 } 44 45 // WriteCountersDir writes a coverage counter-data file for the 46 // currently running program to the directory specified in 'dir'. An 47 // error will be returned if the operation can't be completed 48 // successfully (for example, if the currently running program was not 49 // built with "-cover", or if the directory does not exist). The 50 // counter data written will be a snapshot taken at the point of the 51 // call. 52 func WriteCountersDir(dir string) error { 53 if cmode != coverage.CtrModeAtomic { 54 return fmt.Errorf("WriteCountersDir invoked for program built with -covermode=%s (please use -covermode=atomic)", cmode.String()) 55 } 56 return emitCounterDataToDirectory(dir) 57 } 58 59 // WriteCounters writes coverage counter-data content for the 60 // currently running program to the writer 'w'. An error will be 61 // returned if the operation can't be completed successfully (for 62 // example, if the currently running program was not built with 63 // "-cover", or if a write fails). The counter data written will be a 64 // snapshot taken at the point of the invocation. 65 func WriteCounters(w io.Writer) error { 66 if w == nil { 67 return fmt.Errorf("error: nil writer in WriteCounters") 68 } 69 if cmode != coverage.CtrModeAtomic { 70 return fmt.Errorf("WriteCounters invoked for program built with -covermode=%s (please use -covermode=atomic)", cmode.String()) 71 } 72 // Ask the runtime for the list of coverage counter symbols. 73 cl := getCovCounterList() 74 if len(cl) == 0 { 75 return fmt.Errorf("program not built with -cover") 76 } 77 if !finalHashComputed { 78 return fmt.Errorf("meta-data not written yet, unable to write counter data") 79 } 80 81 pm := getCovPkgMap() 82 s := &emitState{ 83 counterlist: cl, 84 pkgmap: pm, 85 } 86 return s.emitCounterDataToWriter(w) 87 } 88 89 // ClearCounters clears/resets all coverage counter variables in the 90 // currently running program. It returns an error if the program in 91 // question was not built with the "-cover" flag. Clearing of coverage 92 // counters is also not supported for programs not using atomic 93 // counter mode (see more detailed comments below for the rationale 94 // here). 95 func ClearCounters() error { 96 cl := getCovCounterList() 97 if len(cl) == 0 { 98 return fmt.Errorf("program not built with -cover") 99 } 100 if cmode != coverage.CtrModeAtomic { 101 return fmt.Errorf("ClearCounters invoked for program built with -covermode=%s (please use -covermode=atomic)", cmode.String()) 102 } 103 104 // Implementation note: this function would be faster and simpler 105 // if we could just zero out the entire counter array, but for the 106 // moment we go through and zero out just the slots in the array 107 // corresponding to the counter values. We do this to avoid the 108 // following bad scenario: suppose that a user builds their Go 109 // program with "-cover", and that program has a function (call it 110 // main.XYZ) that invokes ClearCounters: 111 // 112 // func XYZ() { 113 // ... do some stuff ... 114 // coverage.ClearCounters() 115 // if someCondition { <<--- HERE 116 // ... 117 // } 118 // } 119 // 120 // At the point where ClearCounters executes, main.XYZ has not yet 121 // finished running, thus as soon as the call returns the line 122 // marked "HERE" above will trigger the writing of a non-zero 123 // value into main.XYZ's counter slab. However since we've just 124 // finished clearing the entire counter segment, we will have lost 125 // the values in the prolog portion of main.XYZ's counter slab 126 // (nctrs, pkgid, funcid). This means that later on at the end of 127 // program execution as we walk through the entire counter array 128 // for the program looking for executed functions, we'll zoom past 129 // main.XYZ's prolog (which was zero'd) and hit the non-zero 130 // counter value corresponding to the "HERE" block, which will 131 // then be interpreted as the start of another live function. 132 // Things will go downhill from there. 133 // 134 // This same scenario is also a potential risk if the program is 135 // running on an architecture that permits reordering of 136 // writes/stores, since the inconsistency described above could 137 // arise here. Example scenario: 138 // 139 // func ABC() { 140 // ... // prolog 141 // if alwaysTrue() { 142 // XYZ() // counter update here 143 // } 144 // } 145 // 146 // In the instrumented version of ABC, the prolog of the function 147 // will contain a series of stores to the initial portion of the 148 // counter array to write number-of-counters, pkgid, funcid. Later 149 // in the function there is also a store to increment a counter 150 // for the block containing the call to XYZ(). If the CPU is 151 // allowed to reorder stores and decides to issue the XYZ store 152 // before the prolog stores, this could be observable as an 153 // inconsistency similar to the one above. Hence the requirement 154 // for atomic counter mode: according to package atomic docs, 155 // "...operations that happen in a specific order on one thread, 156 // will always be observed to happen in exactly that order by 157 // another thread". Thus we can be sure that there will be no 158 // inconsistency when reading the counter array from the thread 159 // running ClearCounters. 160 161 var sd []atomic.Uint32 162 163 bufHdr := (*reflect.SliceHeader)(unsafe.Pointer(&sd)) 164 for _, c := range cl { 165 bufHdr.Data = uintptr(unsafe.Pointer(c.Counters)) 166 bufHdr.Len = int(c.Len) 167 bufHdr.Cap = int(c.Len) 168 for i := 0; i < len(sd); i++ { 169 // Skip ahead until the next non-zero value. 170 sdi := sd[i].Load() 171 if sdi == 0 { 172 continue 173 } 174 // We found a function that was executed; clear its counters. 175 nCtrs := sdi 176 for j := 0; j < int(nCtrs); j++ { 177 sd[i+coverage.FirstCtrOffset+j].Store(0) 178 } 179 // Move to next function. 180 i += coverage.FirstCtrOffset + int(nCtrs) - 1 181 } 182 } 183 return nil 184 }