Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coverage blocks overlapping #3575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 60 additions & 17 deletions pkg/neotest/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ type coverBlock struct {
// documentName makes it clear when a `string` maps path to the script file.
type documentName = string

type interval struct {
compiler.DebugSeqPoint
remove bool
}

func isCoverageEnabled() bool {
coverageLock.Lock()
defer coverageLock.Unlock()
Expand Down Expand Up @@ -150,31 +155,33 @@ func processCover() map[documentName][]coverBlock {
for documentName := range documents {
mappedBlocks := make(map[int]*coverBlock)

var allDocumentSeqPoints []compiler.DebugSeqPoint
for _, scriptRawCoverage := range rawCoverage {
di := scriptRawCoverage.debugInfo
documentSeqPoints := documentSeqPoints(di, documentName)

for _, point := range documentSeqPoints {
b := coverBlock{
startLine: uint(point.StartLine),
startCol: uint(point.StartCol),
endLine: uint(point.EndLine),
endCol: uint(point.EndCol),
stmts: 1 + uint(point.EndLine) - uint(point.StartLine),
counts: 0,
}
mappedBlocks[point.Opcode] = &b
documentSeqPoints := documentSeqPoints(scriptRawCoverage.debugInfo, documentName)
allDocumentSeqPoints = append(allDocumentSeqPoints, documentSeqPoints...)
}
allDocumentSeqPoints = resolveOverlaps(allDocumentSeqPoints)

for _, point := range allDocumentSeqPoints {
b := coverBlock{
startLine: uint(point.StartLine),
startCol: uint(point.StartCol),
endLine: uint(point.EndLine),
endCol: uint(point.EndCol),
stmts: 1 + uint(point.EndLine) - uint(point.StartLine),
counts: 0,
}
mappedBlocks[point.Opcode] = &b
}

for _, scriptRawCoverage := range rawCoverage {
di := scriptRawCoverage.debugInfo
documentSeqPoints := documentSeqPoints(di, documentName)

documentSeqPoints := documentSeqPoints(scriptRawCoverage.debugInfo, documentName)
for _, offset := range scriptRawCoverage.offsetsVisited {
for _, point := range documentSeqPoints {
if point.Opcode == offset {
mappedBlocks[point.Opcode].counts++
if _, ok := mappedBlocks[offset]; ok {
mappedBlocks[offset].counts++
}
}
}
}
Expand Down Expand Up @@ -202,6 +209,42 @@ func documentSeqPoints(di *compiler.DebugInfo, doc documentName) []compiler.Debu
return res
}

// resolveOverlaps removes overlaps from debug points.
// Its assumed that intervals can never overlap partially.
func resolveOverlaps(points []compiler.DebugSeqPoint) []compiler.DebugSeqPoint {
var intervals []interval
for _, p := range points {
intervals = append(intervals, interval{DebugSeqPoint: p})
}
for i := range intervals {
for j := range intervals {
inner := &intervals[i]
outer := &intervals[j]
// If interval 'i' is already removed than there exists an even smaller interval that is also included by 'j'.
// This also ensures that if there are 2 equal intervals then at least 1 will remain.
if i == j || inner.remove {
continue
}
// Outer interval start can't be after inner interval start.
if !(outer.StartLine < inner.StartLine || outer.StartLine == inner.StartLine && outer.StartCol <= inner.StartCol) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this algorithm is that we can't just remove all outer intervals, otherwise coverage for some parts of code is lost and the result looks strange a bit. Consider neofs-contracts as an example, here's the result with the old implementation:
image
And here's the result with the overlaps removal:
image

return runtime.CheckWitness( is an outer interval and it's just removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'm not quite sure why the first call to runtime.CheckWitness is marked as not covered, although it should be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'm not quite sure why the first call to runtime.CheckWitness is marked as not covered, although it should be.

Pretty sure, its because there is no SeqPoint for it

Copy link
Contributor Author

@Slava0135 Slava0135 Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have checked how Go itself handles statement coverage and it seems like it takes a line as a whole (except for conditions where paths can diverge, though still counts for outer function if inner function panics). So we could keep outer block if its one the same line as inner (both are one-liners), but remove outer block if its multi-line?

Copy link
Member

@AnnaShaleva AnnaShaleva Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give an example of contract with test where coverage blocks are overlapping?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PutNamed function in container contract.

continue
}
// Outer interval end can't be before inner interval end.
if !(outer.EndLine > inner.EndLine || outer.EndLine == inner.EndLine && outer.EndCol >= inner.EndCol) {
continue
}
outer.remove = true
}
}
var res []compiler.DebugSeqPoint
for i, v := range intervals {
if !v.remove {
res = append(res, points[i])
}
}
return res
}

func addScriptToCoverage(c *Contract) {
coverageLock.Lock()
defer coverageLock.Unlock()
Expand Down
Loading