-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Refactor syz-declextract in preparation for codesearch tool #6455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dvyukov
merged 6 commits into
google:master
from
dvyukov:dvyukov-prepare-for-codesearch
Nov 17, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
556fae2
pkg/osutil: add Read/ParseJSON functions
dvyukov 978f251
tools/syz-declextract: update clangtool to the latest clang
dvyukov 9466890
pkg/clangtool: make more generic
dvyukov 4632326
pkg/clangtool/tooltest: add package
dvyukov 1a34186
pkg/clangtool/tooltest: add LoadOutput helper
dvyukov 76fc3ef
tools/clang/declextract: move from tools/syz-declextract/clangtool
dvyukov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // 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 tooltest | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/syzkaller/pkg/clangtool" | ||
| "github.com/google/syzkaller/pkg/osutil" | ||
| "github.com/google/syzkaller/pkg/testutil" | ||
| ) | ||
|
|
||
| var ( | ||
| FlagBin = flag.String("bin", "", "path to the clang tool binary to use") | ||
| FlagUpdate = flag.Bool("update", false, "update golden files") | ||
| ) | ||
|
|
||
| func TestClangTool[Output any, OutputPtr clangtool.OutputDataPtr[Output]](t *testing.T) { | ||
| if *FlagBin == "" { | ||
| t.Skipf("clang tool path is not specified, run with -bin=clangtool flag") | ||
| } | ||
| ForEachTestFile(t, func(t *testing.T, cfg *clangtool.Config, file string) { | ||
| out, err := clangtool.Run[Output, OutputPtr](cfg) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got, err := json.MarshalIndent(out, "", "\t") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| CompareGoldenData(t, file+".json", got) | ||
| }) | ||
| } | ||
|
|
||
| func LoadOutput[Output any, OutputPtr clangtool.OutputDataPtr[Output]](t *testing.T) OutputPtr { | ||
| out := OutputPtr(new(Output)) | ||
| forEachTestFile(t, func(t *testing.T, file string) { | ||
| tmp, err := osutil.ReadJSON[OutputPtr](file + ".json") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| out.Merge(tmp) | ||
| }) | ||
| out.SortAndDedup() | ||
| return out | ||
| } | ||
|
|
||
| func ForEachTestFile(t *testing.T, fn func(t *testing.T, cfg *clangtool.Config, file string)) { | ||
| forEachTestFile(t, func(t *testing.T, file string) { | ||
| t.Run(filepath.Base(file), func(t *testing.T) { | ||
| t.Parallel() | ||
| buildDir := t.TempDir() | ||
| commands := fmt.Sprintf(`[{ | ||
| "file": "%s", | ||
| "directory": "%s", | ||
| "command": "clang -c %s -DKBUILD_BASENAME=foo" | ||
| }]`, | ||
| file, buildDir, file) | ||
| dbFile := filepath.Join(buildDir, "compile_commands.json") | ||
| if err := os.WriteFile(dbFile, []byte(commands), 0600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cfg := &clangtool.Config{ | ||
| ToolBin: *FlagBin, | ||
| KernelSrc: osutil.Abs("testdata"), | ||
| KernelObj: buildDir, | ||
| CacheFile: filepath.Join(buildDir, filepath.Base(file)+".json"), | ||
| DebugTrace: &testutil.Writer{TB: t}, | ||
| } | ||
| fn(t, cfg, file) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| func forEachTestFile(t *testing.T, fn func(t *testing.T, file string)) { | ||
| files, err := filepath.Glob(filepath.Join(osutil.Abs("testdata"), "*.c")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(files) == 0 { | ||
| t.Fatal("found no source files") | ||
| } | ||
| for _, file := range files { | ||
| fn(t, file) | ||
| } | ||
| } | ||
|
|
||
| func CompareGoldenFile(t *testing.T, goldenFile, gotFile string) { | ||
| got, err := os.ReadFile(gotFile) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| CompareGoldenData(t, goldenFile, got) | ||
| } | ||
|
|
||
| func CompareGoldenData(t *testing.T, goldenFile string, got []byte) { | ||
| if *FlagUpdate { | ||
| if err := os.WriteFile(goldenFile, got, 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| want, err := os.ReadFile(goldenFile) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if diff := cmp.Diff(want, got); diff != "" { | ||
| t.Fatal(diff) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.