aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/subsystem/linux/coincidence.go
diff options
context:
space:
mode:
authorAleksandr Nogikh <nogikh@google.com>2023-02-10 12:14:36 +0100
committerAleksandr Nogikh <wp32pw@gmail.com>2023-02-10 14:34:44 +0100
commit95871dcc45f6531b4c692ff892aad56bdd95e16f (patch)
tree95c8ffe8b8a36b1dc9473cc3a07e7286595e0b2f /pkg/subsystem/linux/coincidence.go
parent0ee9f5fa4e372b5a2da4ac27418e6c5bccbcaf7a (diff)
pkg/subsystem: restructure the package
Remove the entity and match subpackages. Regenerate the linux.go file.
Diffstat (limited to 'pkg/subsystem/linux/coincidence.go')
-rw-r--r--pkg/subsystem/linux/coincidence.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/pkg/subsystem/linux/coincidence.go b/pkg/subsystem/linux/coincidence.go
new file mode 100644
index 000000000..86ee0794f
--- /dev/null
+++ b/pkg/subsystem/linux/coincidence.go
@@ -0,0 +1,55 @@
+// Copyright 2023 syzkaller project authors. All rights reserved.
+// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
+
+package linux
+
+import "github.com/google/syzkaller/pkg/subsystem"
+
+// CoincidenceMatrix represents a matrix that, for every pair of subsystems
+// A and B, stores the number of times A and B have coincided in the input data.
+// So far we only need it for subsystem.Subsystem, so its interface is tailored to it.
+type CoincidenceMatrix struct {
+ matrix map[*subsystem.Subsystem]map[*subsystem.Subsystem]int
+}
+
+func MakeCoincidenceMatrix() *CoincidenceMatrix {
+ return &CoincidenceMatrix{
+ matrix: make(map[*subsystem.Subsystem]map[*subsystem.Subsystem]int),
+ }
+}
+
+func (cm *CoincidenceMatrix) Record(items ...*subsystem.Subsystem) {
+ for i := 0; i < len(items); i++ {
+ for j := 0; j < len(items); j++ {
+ cm.inc(items[i], items[j])
+ }
+ }
+}
+
+func (cm *CoincidenceMatrix) Count(a *subsystem.Subsystem) int {
+ return cm.Get(a, a)
+}
+
+func (cm *CoincidenceMatrix) Get(a, b *subsystem.Subsystem) int {
+ return cm.matrix[a][b]
+}
+
+func (cm *CoincidenceMatrix) NonEmptyPairs(cb func(a, b *subsystem.Subsystem, val int)) {
+ for a, sub := range cm.matrix {
+ for b, val := range sub {
+ if a == b {
+ continue
+ }
+ cb(a, b, val)
+ }
+ }
+}
+
+func (cm *CoincidenceMatrix) inc(a, b *subsystem.Subsystem) {
+ subMatrix, ok := cm.matrix[a]
+ if !ok {
+ subMatrix = make(map[*subsystem.Subsystem]int)
+ cm.matrix[a] = subMatrix
+ }
+ subMatrix[b]++
+}