diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2025-11-17 07:50:28 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2025-11-17 08:54:02 +0000 |
| commit | a41f43a1ab652ec0629a63d0812a30bfec0e0faf (patch) | |
| tree | 8e9cbf006b8801400fbdb2b538b1f9755141230f /pkg/declextract | |
| parent | cbc18b106abc7ee5cbca73a11b24265328d29516 (diff) | |
pkg/clangtool: make more generic
Make it possible to use pkg/clangtool with other types than declextract.Output.
Diffstat (limited to 'pkg/declextract')
| -rw-r--r-- | pkg/declextract/declextract.go | 7 | ||||
| -rw-r--r-- | pkg/declextract/entity.go | 44 | ||||
| -rw-r--r-- | pkg/declextract/fileops.go | 3 | ||||
| -rw-r--r-- | pkg/declextract/interface.go | 5 | ||||
| -rw-r--r-- | pkg/declextract/typing.go | 4 |
5 files changed, 24 insertions, 39 deletions
diff --git a/pkg/declextract/declextract.go b/pkg/declextract/declextract.go index 923e2440f..b22257bf8 100644 --- a/pkg/declextract/declextract.go +++ b/pkg/declextract/declextract.go @@ -12,6 +12,7 @@ import ( "slices" "strings" + "github.com/google/syzkaller/pkg/clangtool" "github.com/google/syzkaller/pkg/cover" "github.com/google/syzkaller/pkg/ifaceprobe" ) @@ -136,8 +137,8 @@ func (ctx *context) processConsts() map[string]string { Value: fmt.Sprint(ci.Value), }) } - ctx.includes = sortAndDedupSlice(ctx.includes) - ctx.defines = sortAndDedupSlice(ctx.defines) + ctx.includes = clangtool.SortAndDedupSlice(ctx.includes) + ctx.defines = clangtool.SortAndDedupSlice(ctx.defines) // These additional includes must be at the top, because other kernel headers // are broken and won't compile without these additional ones included first. ctx.includes = append([]string{ @@ -194,7 +195,7 @@ func (ctx *context) processSyscalls() { } ctx.emitSyscall(&syscalls, call, "", "", -1, "") } - ctx.Syscalls = sortAndDedupSlice(syscalls) + ctx.Syscalls = clangtool.SortAndDedupSlice(syscalls) } func (ctx *context) emitSyscall(syscalls *[]*Syscall, call *Syscall, diff --git a/pkg/declextract/entity.go b/pkg/declextract/entity.go index d0a309a0f..bd8f143d1 100644 --- a/pkg/declextract/entity.go +++ b/pkg/declextract/entity.go @@ -4,11 +4,9 @@ package declextract import ( - "bytes" - "crypto/sha256" - "encoding/json" - "slices" "strings" + + "github.com/google/syzkaller/pkg/clangtool" ) type Output struct { @@ -244,16 +242,16 @@ func (out *Output) Merge(other *Output) { } func (out *Output) SortAndDedup() { - out.Functions = sortAndDedupSlice(out.Functions) - out.Consts = sortAndDedupSlice(out.Consts) - out.Enums = sortAndDedupSlice(out.Enums) - out.Structs = sortAndDedupSlice(out.Structs) - out.Syscalls = sortAndDedupSlice(out.Syscalls) - out.FileOps = sortAndDedupSlice(out.FileOps) - out.Ioctls = sortAndDedupSlice(out.Ioctls) - out.IouringOps = sortAndDedupSlice(out.IouringOps) - out.NetlinkFamilies = sortAndDedupSlice(out.NetlinkFamilies) - out.NetlinkPolicies = sortAndDedupSlice(out.NetlinkPolicies) + out.Functions = clangtool.SortAndDedupSlice(out.Functions) + out.Consts = clangtool.SortAndDedupSlice(out.Consts) + out.Enums = clangtool.SortAndDedupSlice(out.Enums) + out.Structs = clangtool.SortAndDedupSlice(out.Structs) + out.Syscalls = clangtool.SortAndDedupSlice(out.Syscalls) + out.FileOps = clangtool.SortAndDedupSlice(out.FileOps) + out.Ioctls = clangtool.SortAndDedupSlice(out.Ioctls) + out.IouringOps = clangtool.SortAndDedupSlice(out.IouringOps) + out.NetlinkFamilies = clangtool.SortAndDedupSlice(out.NetlinkFamilies) + out.NetlinkPolicies = clangtool.SortAndDedupSlice(out.NetlinkPolicies) } // SetSoureFile attaches the source file to the entities that need it. @@ -285,21 +283,3 @@ func (out *Output) SetSourceFile(file string, updatePath func(string) string) { op.SourceFile = file } } - -func sortAndDedupSlice[Slice ~[]E, E comparable](s Slice) Slice { - dedup := make(map[[sha256.Size]byte]E) - text := make(map[E][]byte) - for _, e := range s { - t, _ := json.Marshal(e) - dedup[sha256.Sum256(t)] = e - text[e] = t - } - s = make([]E, 0, len(dedup)) - for _, e := range dedup { - s = append(s, e) - } - slices.SortFunc(s, func(a, b E) int { - return bytes.Compare(text[a], text[b]) - }) - return s -} diff --git a/pkg/declextract/fileops.go b/pkg/declextract/fileops.go index 8b2ea629a..8af85189f 100644 --- a/pkg/declextract/fileops.go +++ b/pkg/declextract/fileops.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/google/syzkaller/pkg/ast" + "github.com/google/syzkaller/pkg/clangtool" ) const ( @@ -243,7 +244,7 @@ func (ctx *context) mapFileToFops(funcs map[*Function]bool, funcToFops map[*Func best = append(best, fops) } } - best = sortAndDedupSlice(best) + best = clangtool.SortAndDedupSlice(best) // Now, filter out some excessive file_operations. // An example of an excessive case is if we have 2 file_operations with just read+write, // currently we emit generic read/write operations, so we would emit completly equal diff --git a/pkg/declextract/interface.go b/pkg/declextract/interface.go index f386f398b..73806ec4b 100644 --- a/pkg/declextract/interface.go +++ b/pkg/declextract/interface.go @@ -8,6 +8,7 @@ import ( "slices" "strings" + "github.com/google/syzkaller/pkg/clangtool" "github.com/google/syzkaller/pkg/cover" ) @@ -55,7 +56,7 @@ func (ctx *context) noteInterface(iface *Interface) { } func (ctx *context) finishInterfaces() { - ctx.interfaces = sortAndDedupSlice(ctx.interfaces) + ctx.interfaces = clangtool.SortAndDedupSlice(ctx.interfaces) count := make(map[string]int) for _, iface := range ctx.interfaces { count[iface.Type+iface.Name]++ @@ -77,7 +78,7 @@ func (ctx *context) finishInterfaces() { iface.Access = AccessUnknown } } - ctx.interfaces = sortAndDedupSlice(ctx.interfaces) + ctx.interfaces = clangtool.SortAndDedupSlice(ctx.interfaces) } func (ctx *context) processFunctions() { diff --git a/pkg/declextract/typing.go b/pkg/declextract/typing.go index f5fcdbdd3..e839d7bc4 100644 --- a/pkg/declextract/typing.go +++ b/pkg/declextract/typing.go @@ -8,6 +8,8 @@ import ( "fmt" "slices" "strings" + + "github.com/google/syzkaller/pkg/clangtool" ) // Argument/field type inference based on data flow analysis. @@ -346,7 +348,7 @@ func (ctx *context) inferCommandVariants(name, file string, arg int) []string { visited := make(map[*typingNode]bool) ctx.walkCommandVariants(n, &variants, visited, 0) } - return sortAndDedupSlice(variants) + return clangtool.SortAndDedupSlice(variants) } func (ctx *context) collectCommandVariants(fn *Function, arg int, variants *[]string) { |
