Skip to content

Commit 11d0ac5

Browse files
0xalpharushelopezanishnaik
authored
fix: dedup pc by codehash for unique pc metric (#485)
* fix: dedup pc by codehash for unique pc metric * fix: unify coverage across multiple instances of a codehash when counting unique PCs --------- Co-authored-by: Emilio López <[email protected]> Co-authored-by: anishnaik <[email protected]>
1 parent 0112ddc commit 11d0ac5

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

fuzzing/coverage/coverage_maps.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package coverage
33
import (
44
"golang.org/x/exp/slices"
55

6+
"sync"
7+
68
compilationTypes "github.com/crytic/medusa/compilation/types"
79
"github.com/crytic/medusa/utils"
810
"github.com/ethereum/go-ethereum/common"
911
"github.com/ethereum/go-ethereum/crypto"
10-
"sync"
1112
)
1213

1314
// CoverageMaps represents a data structure used to identify instruction execution coverage of various smart contracts
@@ -249,6 +250,10 @@ func (cm *CoverageMaps) UniquePCs() uint64 {
249250
uniquePCs := uint64(0)
250251
// Iterate across each contract deployment
251252
for _, mapsByAddress := range cm.maps {
253+
// Consider the coverage of all of the different deployments of this codehash as a set
254+
// And mark a PC as hit if any of the instances has a hit for it
255+
uniquePCsForHash := make(map[int]struct{})
256+
252257
for _, contractCoverageMap := range mapsByAddress {
253258
// TODO: Note we are not checking for nil dereference here because we are guaranteed that the successful
254259
// coverage and reverted coverage arrays have been instantiated if we are iterating over it
@@ -259,18 +264,20 @@ func (cm *CoverageMaps) UniquePCs() uint64 {
259264
for i, hits := range contractCoverageMap.successfulCoverage.executedFlags {
260265
// If we hit the PC at least once, we have a unique PC hit
261266
if hits != 0 {
262-
uniquePCs++
267+
uniquePCsForHash[i] = struct{}{}
263268

264269
// Do not count both success and revert
265270
continue
266271
}
267272

268273
// This is only executed if the PC was not executed successfully
269274
if contractCoverageMap.revertedCoverage.executedFlags != nil && contractCoverageMap.revertedCoverage.executedFlags[i] != 0 {
270-
uniquePCs++
275+
uniquePCsForHash[i] = struct{}{}
271276
}
272277
}
273278
}
279+
280+
uniquePCs += uint64(len(uniquePCsForHash))
274281
}
275282
return uniquePCs
276283
}

0 commit comments

Comments
 (0)