-
Notifications
You must be signed in to change notification settings - Fork 1.4k
pkg/aflow: expose reproducer test coverage to the LLM #7008
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
Open
ramosian-glider
wants to merge
2
commits into
google:master
Choose a base branch
from
ramosian-glider:hackathon-250326
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,7 @@ import ( | |||||||||
| "github.com/google/syzkaller/pkg/hash" | ||||||||||
| "github.com/google/syzkaller/pkg/instance" | ||||||||||
| "github.com/google/syzkaller/pkg/mgrconfig" | ||||||||||
| "github.com/google/syzkaller/pkg/osutil" | ||||||||||
| "github.com/google/syzkaller/pkg/report" | ||||||||||
| "github.com/google/syzkaller/pkg/symbolizer" | ||||||||||
| "github.com/google/syzkaller/sys/targets" | ||||||||||
|
|
@@ -142,47 +143,66 @@ func parseTestError(err *instance.TestError) string { | |||||||||
| return fmt.Sprintf("%v: %v\n%s", what, err.Title, extraInfo) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func ReproduceFunc(ctx *aflow.Context, args ReproduceArgs) (reproduceResult, error) { | ||||||||||
| func ReproduceFuncWithCoverage(ctx *aflow.Context, args ReproduceArgs, | ||||||||||
| collectCoverage bool) (reproduceResult, string, error) { | ||||||||||
| imageData, err := os.ReadFile(args.Image) | ||||||||||
| if err != nil { | ||||||||||
| return reproduceResult{}, err | ||||||||||
| return reproduceResult{}, "", err | ||||||||||
| } | ||||||||||
| desc := fmt.Sprintf("kernel commit %v, kernel config hash %v, image hash %v,"+ | ||||||||||
| " vm %v, vm config hash %v, C repro hash %v, syz repro hash %v, opts hash %v, version 4", | ||||||||||
| " vm %v, vm config hash %v, C repro hash %v, syz repro hash %v, opts hash %v, cov %v, version 5", | ||||||||||
| args.KernelCommit, hash.String(args.KernelConfig), hash.String(imageData), | ||||||||||
| args.Type, hash.String(args.VM), hash.String(args.ReproC), | ||||||||||
| hash.String(args.ReproSyz), hash.String(args.ReproOpts)) | ||||||||||
| hash.String(args.ReproSyz), hash.String(args.ReproOpts), collectCoverage) | ||||||||||
| type Cached struct { | ||||||||||
| BugTitle string | ||||||||||
| Report string | ||||||||||
| Error string | ||||||||||
| BugTitle string | ||||||||||
| Report string | ||||||||||
| Error string | ||||||||||
| CoverageID string | ||||||||||
| } | ||||||||||
| cached, err := aflow.CacheObject(ctx, "repro", desc, func() (Cached, error) { | ||||||||||
| var res Cached | ||||||||||
| workdir, err := ctx.TempDir() | ||||||||||
| if err != nil { | ||||||||||
| return res, err | ||||||||||
| } | ||||||||||
| testRes, err := RunTest(args, workdir, false) | ||||||||||
| testRes, err := RunTest(args, workdir, collectCoverage) | ||||||||||
| if testRes.Report != nil { | ||||||||||
| res.BugTitle = testRes.Report.Title | ||||||||||
| res.Report = string(testRes.Report.Report) | ||||||||||
| } | ||||||||||
| res.Error = testRes.BootError | ||||||||||
| // If the reproducer crashes the kernel, the VM halts abruptly and coverage | ||||||||||
| // often fails to flush, leaving testRes.Coverage empty. This is expected: we | ||||||||||
| // generally only want diagnostics on reproducers that failed to crash. | ||||||||||
| if err == nil && collectCoverage && len(testRes.Coverage) > 0 { | ||||||||||
| dir, err := ctx.Cache("coverage", desc, func(dir string) error { | ||||||||||
| return osutil.WriteJSON(filepath.Join(dir, "coverage.json"), testRes.Coverage) | ||||||||||
| }) | ||||||||||
| if err != nil { | ||||||||||
| return res, err | ||||||||||
| } | ||||||||||
| res.CoverageID = filepath.Base(dir) | ||||||||||
| } | ||||||||||
| return res, err | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟠 If the reproducer fails to crash the kernel, the `CoverageID` is currently being lost because it's not returned when `ErrDidNotCrash` is encountered. Returning the `CoverageID` even in this case is crucial for providing the LLM agent with the context it needs to debug why the reproducer failed.
Suggested change
|
||||||||||
| }) | ||||||||||
| if err != nil { | ||||||||||
| return reproduceResult{}, err | ||||||||||
| return reproduceResult{}, "", err | ||||||||||
| } | ||||||||||
| if cached.Error != "" { | ||||||||||
| return reproduceResult{}, errors.New(cached.Error) | ||||||||||
| return reproduceResult{}, "", errors.New(cached.Error) | ||||||||||
| } else if cached.Report == "" { | ||||||||||
| return reproduceResult{}, aflow.FlowError(ErrDidNotCrash) | ||||||||||
| return reproduceResult{}, "", aflow.FlowError(ErrDidNotCrash) | ||||||||||
| } | ||||||||||
| return reproduceResult{ | ||||||||||
| ReproducedBugTitle: cached.BugTitle, | ||||||||||
| ReproducedCrashReport: cached.Report, | ||||||||||
| }, nil | ||||||||||
| }, cached.CoverageID, nil | ||||||||||
ramosian-glider marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| func ReproduceFunc(ctx *aflow.Context, args ReproduceArgs) (reproduceResult, error) { | ||||||||||
| res, _, err := ReproduceFuncWithCoverage(ctx, args, false) | ||||||||||
| return res, err | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func symbolize(kernelObj string, coverage [][]uint64) ([][]symbolizer.Frame, error) { | ||||||||||
|
|
||||||||||
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,20 @@ | ||
| // Copyright 2026 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 aflow | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // NewTestContext creates an initialized dummy Context for internal aflow and tool unit tests. | ||
| func NewTestContext(t *testing.T) *Context { | ||
| cache, err := NewCache(t.TempDir(), 10000000) | ||
| require.NoError(t, err) | ||
|
|
||
| return &Context{ | ||
| cache: cache, | ||
| } | ||
| } |
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,140 @@ | ||
| // Copyright 2026 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 syzlang | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/google/syzkaller/pkg/aflow" | ||
| "github.com/google/syzkaller/pkg/osutil" | ||
| "github.com/google/syzkaller/pkg/symbolizer" | ||
| ) | ||
|
|
||
| var ( | ||
| ToolCoverageFiles = aflow.NewFuncTool("get-coverage-files", getCoverageFiles, ` | ||
| Tool returns a list of source files that were covered during a crash reproducer run. | ||
| The list is deduplicated and sorted, presenting a high-level summary of the covered code regions. | ||
| `) | ||
|
|
||
| ToolFileCoverage = aflow.NewFuncTool("get-file-coverage", getFileCoverage, ` | ||
| Tool evaluates code coverage for a specific file and returns coverage snippets highlighting the executed lines. | ||
| For each function that had hits in the file, it will dump the executed source lines with short context | ||
| around them. Covered lines are prefixed with '* '. | ||
| `) | ||
|
|
||
| CoverageTools = []aflow.Tool{ToolCoverageFiles, ToolFileCoverage} | ||
| ) | ||
|
|
||
| type CoverageFilesArgs struct { | ||
| CoverageID string `jsonschema:"CoverageID returned by the reproduce-crash tool."` | ||
| } | ||
|
|
||
| type CoverageFilesResult struct { | ||
| Files []string `jsonschema:"List of source files containing coverage."` | ||
| } | ||
|
|
||
| func getCoverageFiles(ctx *aflow.Context, state reproduceState, args CoverageFilesArgs) (CoverageFilesResult, error) { | ||
| coverage, err := readCoverage(ctx, args.CoverageID) | ||
| if err != nil { | ||
| return CoverageFilesResult{}, err | ||
| } | ||
|
|
||
| var files []string | ||
| for _, callcov := range coverage { | ||
| for _, frame := range callcov { | ||
| if frame.File != "" { | ||
| files = append(files, frame.File) | ||
| } | ||
| } | ||
| } | ||
| slices.Sort(files) | ||
| files = slices.Compact(files) | ||
tarasmadan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return CoverageFilesResult{Files: files}, nil | ||
| } | ||
|
|
||
| type FileCoverageArgs struct { | ||
| CoverageID string `jsonschema:"CoverageID returned by the reproduce-crash tool."` | ||
| Filename string `jsonschema:"Name of the source file to inspect."` | ||
| } | ||
|
|
||
| type FileCoverageResult struct { | ||
| Snippets []string `jsonschema:"List of formatted code snippets for each executed function in the requested file."` | ||
| } | ||
|
|
||
| func getFileCoverage(ctx *aflow.Context, state reproduceState, args FileCoverageArgs) (FileCoverageResult, error) { | ||
| if !filepath.IsLocal(args.Filename) { | ||
| return FileCoverageResult{}, aflow.BadCallError("filename must be a safe, local relative path") | ||
| } | ||
|
|
||
| coverage, err := readCoverage(ctx, args.CoverageID) | ||
| if err != nil { | ||
| return FileCoverageResult{}, err | ||
| } | ||
|
|
||
| funcLines := make(map[string][]int) | ||
| for _, callcov := range coverage { | ||
| for _, frame := range callcov { | ||
| if frame.File == args.Filename && frame.Func != "" { | ||
| funcLines[frame.Func] = append(funcLines[frame.Func], frame.Line) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(funcLines) == 0 { | ||
| return FileCoverageResult{}, aflow.BadCallError("no coverage found for file: %v", args.Filename) | ||
| } | ||
|
|
||
| srcBytes, err := os.ReadFile(filepath.Join(state.KernelSrc, args.Filename)) | ||
| if err != nil { | ||
| return FileCoverageResult{}, aflow.BadCallError("failed to read source file: %v", err) | ||
| } | ||
| srcLines := strings.Split(string(srcBytes), "\n") | ||
|
|
||
| var res FileCoverageResult | ||
|
|
||
| for fnName, lines := range funcLines { | ||
| if len(lines) == 0 { | ||
| continue | ||
| } | ||
| slices.Sort(lines) | ||
| lines = slices.Compact(lines) | ||
| minLine := lines[0] | ||
| maxLine := lines[len(lines)-1] | ||
|
|
||
| ctxPadding := 10 | ||
| start := max(1, minLine-ctxPadding) | ||
| end := min(len(srcLines), maxLine+ctxPadding) | ||
|
|
||
| var out strings.Builder | ||
| fmt.Fprintf(&out, "Function: %v\n", fnName) | ||
| for i := start; i <= end; i++ { | ||
| prefix := " " | ||
| if _, hit := slices.BinarySearch(lines, i); hit { | ||
| prefix = "* " | ||
| } | ||
| fmt.Fprintf(&out, "%s%4d: %s\n", prefix, i, srcLines[i-1]) | ||
| } | ||
| res.Snippets = append(res.Snippets, out.String()) | ||
| } | ||
| slices.Sort(res.Snippets) | ||
|
|
||
| return res, nil | ||
| } | ||
|
|
||
| func readCoverage(ctx *aflow.Context, coverageID string) ([][]symbolizer.Frame, error) { | ||
| if !filepath.IsLocal(coverageID) { | ||
| return nil, aflow.BadCallError("invalid CoverageID: %v", coverageID) | ||
| } | ||
| covFile := filepath.Join(ctx.CacheDir("coverage", coverageID), "coverage.json") | ||
| frames, err := osutil.ReadJSON[[][]symbolizer.Frame](covFile) | ||
| if err != nil { | ||
| return nil, aflow.BadCallError("invalid or missing CoverageID: %v", coverageID) | ||
| } | ||
| return frames, nil | ||
| } | ||
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,103 @@ | ||
| // Copyright 2026 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 syzlang | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/google/syzkaller/pkg/aflow" | ||
| "github.com/google/syzkaller/pkg/osutil" | ||
| "github.com/google/syzkaller/pkg/symbolizer" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCoverageFiles(t *testing.T) { | ||
| ctx := aflow.NewTestContext(t) | ||
|
|
||
| covID := "feedfacefeedfacefeedfacefeedfacefeedface" // 40 chars | ||
|
|
||
| // Mock a cached coverage file. | ||
| covDir := ctx.CacheDir("coverage", covID) | ||
| err := osutil.MkdirAll(covDir) | ||
| require.NoError(t, err) | ||
|
|
||
| dummyCov := [][]symbolizer.Frame{ | ||
| { | ||
| {File: "kernel/foo.c", Func: "foo", Line: 10}, | ||
| {File: "kernel/bar.c", Func: "bar", Line: 20}, | ||
| }, | ||
| { | ||
| {File: "kernel/foo.c", Func: "foo2", Line: 30}, | ||
| }, | ||
| } | ||
| err = osutil.WriteJSON(filepath.Join(covDir, "coverage.json"), dummyCov) | ||
| require.NoError(t, err) | ||
|
|
||
| res, err := getCoverageFiles(ctx, reproduceState{}, CoverageFilesArgs{ | ||
| CoverageID: covID, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, []string{"kernel/bar.c", "kernel/foo.c"}, res.Files) | ||
| } | ||
|
|
||
| func TestFileCoverage(t *testing.T) { | ||
| ctx := aflow.NewTestContext(t) | ||
|
|
||
| covID := "feedfacefeedfacefeedfacefeedfacefeedface" | ||
|
|
||
| // Mock a cached coverage file. | ||
| covDir := ctx.CacheDir("coverage", covID) | ||
| err := osutil.MkdirAll(covDir) | ||
| require.NoError(t, err) | ||
|
|
||
| dummyCov := [][]symbolizer.Frame{ | ||
| { | ||
| {File: "foo.c", Func: "foo", Line: 5}, | ||
| {File: "foo.c", Func: "foo", Line: 6}, | ||
| {File: "foo.c", Func: "foo", Line: 7}, | ||
| }, | ||
| } | ||
| err = osutil.WriteJSON(filepath.Join(covDir, "coverage.json"), dummyCov) | ||
| require.NoError(t, err) | ||
|
|
||
| kernelSrc := t.TempDir() | ||
| err = osutil.MkdirAll(kernelSrc) | ||
| require.NoError(t, err) | ||
|
|
||
| srcContent := `1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| void foo(void) { | ||
| int a = 1; | ||
| a++; | ||
| } | ||
| ` | ||
| err = os.WriteFile(filepath.Join(kernelSrc, "foo.c"), []byte(srcContent), 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| res, err := getFileCoverage(ctx, reproduceState{KernelSrc: kernelSrc}, FileCoverageArgs{ | ||
| CoverageID: covID, | ||
| Filename: "foo.c", | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Len(t, res.Snippets, 1) | ||
|
|
||
| expectedSnippet := `Function: foo | ||
| 1: 1 | ||
| 2: 2 | ||
| 3: 3 | ||
| 4: 4 | ||
| * 5: void foo(void) { | ||
| * 6: int a = 1; | ||
| * 7: a++; | ||
| 8: } | ||
| 9: | ||
| ` | ||
| require.Equal(t, expectedSnippet, res.Snippets[0]) | ||
| } |
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.