github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/cover/cover.go (about)

     1  // Copyright 2015 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  // Package cover provides types for working with coverage information (arrays of covered PCs).
     5  package cover
     6  
     7  type Cover map[uint32]struct{}
     8  
     9  func (cov *Cover) Merge(raw []uint32) {
    10  	c := *cov
    11  	if c == nil {
    12  		c = make(Cover)
    13  		*cov = c
    14  	}
    15  	for _, pc := range raw {
    16  		c[pc] = struct{}{}
    17  	}
    18  }
    19  
    20  // Merge merges raw into coverage and returns newly added PCs. Overwrites/mutates raw.
    21  func (cov *Cover) MergeDiff(raw []uint32) []uint32 {
    22  	c := *cov
    23  	if c == nil {
    24  		c = make(Cover)
    25  		*cov = c
    26  	}
    27  	n := 0
    28  	for _, pc := range raw {
    29  		if _, ok := c[pc]; ok {
    30  			continue
    31  		}
    32  		c[pc] = struct{}{}
    33  		raw[n] = pc
    34  		n++
    35  	}
    36  	return raw[:n]
    37  }
    38  
    39  func (cov Cover) Serialize() []uint32 {
    40  	res := make([]uint32, 0, len(cov))
    41  	for pc := range cov {
    42  		res = append(res, pc)
    43  	}
    44  	return res
    45  }