From 072c7d4969862c84430cc2daef20a8f7f3ba78a2 Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Tue, 19 Apr 2022 19:41:40 -0400 Subject: cmd/compile,cmd/link: hooks for identifying coverage counters Add a new "coverage counter" classification for variables to be used for storing code coverage counter values (somewhat in the same way that we identify fuzzer counters). Tagging such variables allows us to aggregate them in the linker, and to treat updates specially. Updates #51430. Change-Id: Ib49fb05736ffece98bcc2f7a7c37e991b7f67bbb Reviewed-on: https://go-review.googlesource.com/c/go/+/401235 Reviewed-by: David Chase Run-TryBot: Than McIntosh TryBot-Result: Gopher Robot --- src/cmd/compile/internal/gc/obj.go | 6 ++++++ src/cmd/compile/internal/inline/inl.go | 23 +++++++++++++++++++++++ src/cmd/compile/internal/ir/name.go | 6 ++++++ src/cmd/compile/internal/ssa/writebarrier.go | 3 ++- 4 files changed, 37 insertions(+), 1 deletion(-) (limited to 'src/cmd/compile') diff --git a/src/cmd/compile/internal/gc/obj.go b/src/cmd/compile/internal/gc/obj.go index 715b8ee263..504072bb17 100644 --- a/src/cmd/compile/internal/gc/obj.go +++ b/src/cmd/compile/internal/gc/obj.go @@ -195,6 +195,9 @@ func dumpGlobal(n *ir.Name) { } types.CalcSize(n.Type()) ggloblnod(n) + if n.CoverageCounter() || n.CoverageAuxVar() { + return + } base.Ctxt.DwarfGlobal(base.Ctxt.Pkgpath, types.TypeSymName(n.Type()), n.Linksym()) } @@ -316,6 +319,9 @@ func ggloblnod(nam *ir.Name) { if nam.Libfuzzer8BitCounter() { s.Type = objabi.SLIBFUZZER_8BIT_COUNTER } + if nam.CoverageCounter() { + s.Type = objabi.SCOVERAGE_COUNTER + } if nam.Sym().Linkname != "" { // Make sure linkname'd symbol is non-package. When a symbol is // both imported and linkname'd, s.Pkg may not set to "_" in diff --git a/src/cmd/compile/internal/inline/inl.go b/src/cmd/compile/internal/inline/inl.go index a7fd704b85..14adbf5d43 100644 --- a/src/cmd/compile/internal/inline/inl.go +++ b/src/cmd/compile/internal/inline/inl.go @@ -37,6 +37,7 @@ import ( "cmd/compile/internal/typecheck" "cmd/compile/internal/types" "cmd/internal/obj" + "cmd/internal/objabi" "cmd/internal/src" ) @@ -471,6 +472,28 @@ func (v *hairyVisitor) doNode(n ir.Node) bool { } } } + + case ir.OAS: + // Special case for coverage counter updates and coverage + // function registrations. Although these correspond to real + // operations, we treat them as zero cost for the moment. This + // is primarily due to the existence of tests that are + // sensitive to inlining-- if the insertion of coverage + // instrumentation happens to tip a given function over the + // threshold and move it from "inlinable" to "not-inlinable", + // this can cause changes in allocation behavior, which can + // then result in test failures (a good example is the + // TestAllocations in crypto/ed25519). + n := n.(*ir.AssignStmt) + if n.X.Op() == ir.OINDEX { + n := n.X.(*ir.IndexExpr) + if n.X.Op() == ir.ONAME && n.X.Type().IsArray() { + n := n.X.(*ir.Name) + if n.Linksym().Type == objabi.SCOVERAGE_COUNTER { + return false + } + } + } } v.budget-- diff --git a/src/cmd/compile/internal/ir/name.go b/src/cmd/compile/internal/ir/name.go index 310481f6f0..f537ba4981 100644 --- a/src/cmd/compile/internal/ir/name.go +++ b/src/cmd/compile/internal/ir/name.go @@ -236,6 +236,8 @@ const ( nameInlLocal // PAUTO created by inliner, derived from callee local nameOpenDeferSlot // if temporary var storing info for open-coded defers nameLibfuzzer8BitCounter // if PEXTERN should be assigned to __sancov_cntrs section + nameCoverageCounter // instrumentation counter var for cmd/cover + nameCoverageAuxVar // instrumentation pkg ID variable cmd/cover nameAlias // is type name an alias ) @@ -251,6 +253,8 @@ func (n *Name) InlFormal() bool { return n.flags&nameInlFormal != func (n *Name) InlLocal() bool { return n.flags&nameInlLocal != 0 } func (n *Name) OpenDeferSlot() bool { return n.flags&nameOpenDeferSlot != 0 } func (n *Name) Libfuzzer8BitCounter() bool { return n.flags&nameLibfuzzer8BitCounter != 0 } +func (n *Name) CoverageCounter() bool { return n.flags&nameCoverageCounter != 0 } +func (n *Name) CoverageAuxVar() bool { return n.flags&nameCoverageAuxVar != 0 } func (n *Name) setReadonly(b bool) { n.flags.set(nameReadonly, b) } func (n *Name) SetNeedzero(b bool) { n.flags.set(nameNeedzero, b) } @@ -264,6 +268,8 @@ func (n *Name) SetInlFormal(b bool) { n.flags.set(nameInlFormal, func (n *Name) SetInlLocal(b bool) { n.flags.set(nameInlLocal, b) } func (n *Name) SetOpenDeferSlot(b bool) { n.flags.set(nameOpenDeferSlot, b) } func (n *Name) SetLibfuzzer8BitCounter(b bool) { n.flags.set(nameLibfuzzer8BitCounter, b) } +func (n *Name) SetCoverageCounter(b bool) { n.flags.set(nameCoverageCounter, b) } +func (n *Name) SetCoverageAuxVar(b bool) { n.flags.set(nameCoverageAuxVar, b) } // OnStack reports whether variable n may reside on the stack. func (n *Name) OnStack() bool { diff --git a/src/cmd/compile/internal/ssa/writebarrier.go b/src/cmd/compile/internal/ssa/writebarrier.go index cb8c0a5e0e..f5a7ed5928 100644 --- a/src/cmd/compile/internal/ssa/writebarrier.go +++ b/src/cmd/compile/internal/ssa/writebarrier.go @@ -652,7 +652,8 @@ func IsSanitizerSafeAddr(v *Value) bool { // read-only once initialized. return true case OpAddr: - return v.Aux.(*obj.LSym).Type == objabi.SRODATA || v.Aux.(*obj.LSym).Type == objabi.SLIBFUZZER_8BIT_COUNTER + vt := v.Aux.(*obj.LSym).Type + return vt == objabi.SRODATA || vt == objabi.SLIBFUZZER_8BIT_COUNTER || vt == objabi.SCOVERAGE_COUNTER || vt == objabi.SCOVERAGE_AUXVAR } return false } -- cgit v1.3