aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/subsystem/linux/path_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/path_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/path_coincidence.go')
-rw-r--r--pkg/subsystem/linux/path_coincidence.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/pkg/subsystem/linux/path_coincidence.go b/pkg/subsystem/linux/path_coincidence.go
new file mode 100644
index 000000000..44182bd6a
--- /dev/null
+++ b/pkg/subsystem/linux/path_coincidence.go
@@ -0,0 +1,68 @@
+// 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 (
+ "io/fs"
+ "regexp"
+ "runtime"
+ "sync"
+
+ "github.com/google/syzkaller/pkg/subsystem"
+)
+
+func BuildCoincidenceMatrix(root fs.FS, list []*subsystem.Subsystem,
+ excludeRe *regexp.Regexp) (*CoincidenceMatrix, error) {
+ // Create a matcher.
+ matcher := subsystem.MakePathMatcher(list)
+ chPaths, chResult := extractSubsystems(matcher)
+ // The final consumer goroutine.
+ cm := MakeCoincidenceMatrix()
+ ready := make(chan struct{})
+ go func() {
+ for items := range chResult {
+ cm.Record(items...)
+ }
+ ready <- struct{}{}
+ }()
+ // Source of data.
+ err := fs.WalkDir(root, ".", func(path string, info fs.DirEntry, err error) error {
+ if err != nil || info.IsDir() {
+ return err
+ }
+ if !includePathRe.MatchString(path) ||
+ (excludeRe != nil && excludeRe.MatchString(path)) {
+ return nil
+ }
+ chPaths <- path
+ return nil
+ })
+ close(chPaths)
+ <-ready
+ return cm, err
+}
+
+var (
+ includePathRe = regexp.MustCompile(`(?:/|\.(?:c|h|S))$`)
+)
+
+func extractSubsystems(matcher *subsystem.PathMatcher) (chan<- string, <-chan []*subsystem.Subsystem) {
+ procs := runtime.NumCPU()
+ paths, output := make(chan string, procs), make(chan []*subsystem.Subsystem, procs)
+ var wg sync.WaitGroup
+ for i := 0; i < procs; i++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ for path := range paths {
+ output <- matcher.Match(path)
+ }
+ }()
+ }
+ go func() {
+ wg.Wait()
+ close(output)
+ }()
+ return paths, output
+}