From 280ea308c321115445df610f1a75b05bbadca5f3 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 17 Nov 2025 11:17:23 +0100 Subject: pkg/codesearch: add skeleton for code searching tool Add a clang tool that is used for code indexing (tools/clang/codesearch/). It follows conventions and build procedure of the declextract tool. Add pkg/codesearch package that aggregates the info exposed by the clang tools, and allows doing simple queries: - show source code of an entity (function, struct, etc) - show entity comment - show all entities defined in a source file Add tools/syz-codesearch wrapper tool that allows to create index for a kernel build, and then run code queries on it. --- tools/syz-codesearch/codesearch.go | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tools/syz-codesearch/codesearch.go (limited to 'tools/syz-codesearch') diff --git a/tools/syz-codesearch/codesearch.go b/tools/syz-codesearch/codesearch.go new file mode 100644 index 000000000..afd3840c7 --- /dev/null +++ b/tools/syz-codesearch/codesearch.go @@ -0,0 +1,66 @@ +// Copyright 2025 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 main + +import ( + "flag" + "fmt" + "os" + + "github.com/google/syzkaller/pkg/clangtool" + "github.com/google/syzkaller/pkg/codesearch" + "github.com/google/syzkaller/pkg/tool" +) + +func main() { + var ( + flagDatabase = flag.String("database", "", "path to input/output database file (mandatory)") + flagKernelSrc = flag.String("kernel-src", "", "path to kernel source directory (mandatory)") + flagKernelObj = flag.String("kernel-obj", "", "path to kernel build directory (mandatory)") + ) + flag.Parse() + if len(flag.Args()) == 0 || *flagDatabase == "" || *flagKernelSrc == "" || *flagKernelObj == "" { + printUsageAndExit() + } + cmd, args := flag.Args()[0], flag.Args()[1:] + if cmd == "index" { + if len(args) != 1 { + printUsageAndExit() + } + cfg := &clangtool.Config{ + ToolBin: args[0], + KernelSrc: *flagKernelSrc, + KernelObj: *flagKernelObj, + CacheFile: *flagDatabase, + DebugTrace: os.Stderr, + } + + if _, err := clangtool.Run[codesearch.Database](cfg); err != nil { + tool.Fail(err) + } + return + } + index, err := codesearch.NewIndex(*flagDatabase, []string{*flagKernelSrc, *flagKernelObj}) + if err != nil { + tool.Fail(err) + } + res, err := index.Command(cmd, args) + if err != nil { + tool.Fail(err) + } + os.Stdout.WriteString(res) +} + +func printUsageAndExit() { + fmt.Printf(`syz-codesearch usage: +syz-codesearch [flags] command [command arguments] +commands and their arguments: +`) + for _, cmd := range codesearch.Commands { + fmt.Printf(" - %v [%v args]\n", cmd.Name, cmd.NArgs) + } + fmt.Printf("\nflags:\n") + flag.PrintDefaults() + os.Exit(1) +} -- cgit mrf-deployment