Skip to content

Commit 3f2febe

Browse files
committed
pkg/aflow/tool/codesearcher: take into account DB format when caching
If format of the codesearch DB file changes, we need to create new DB rather than use old cached one. Add DB format hash to cache signature.
1 parent d002de2 commit 3f2febe

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pkg/aflow/tool/codesearcher/codesearcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ func (index) UnmarshalJSON([]byte) error {
138138
}
139139

140140
func prepare(ctx *aflow.Context, args prepareArgs) (prepareResult, error) {
141-
desc := fmt.Sprintf("kernel commit %v, config hash %v",
142-
args.KernelCommit, hash.String(args.KernelConfig))
141+
desc := fmt.Sprintf("kernel commit %v, config hash %v, databash hash %v",
142+
args.KernelCommit, hash.String(args.KernelConfig), codesearch.DatabaseFormatHash)
143143
dir, err := ctx.Cache("codesearch", desc, func(dir string) error {
144144
cfg := &clangtool.Config{
145145
ToolBin: args.CodesearchToolBin,

pkg/codesearch/database.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
package codesearch
55

66
import (
7+
"encoding/json"
78
"strings"
89

10+
"github.com/google/jsonschema-go/jsonschema"
911
"github.com/google/syzkaller/pkg/clangtool"
12+
"github.com/google/syzkaller/pkg/hash"
1013
)
1114

1215
type Database struct {
@@ -36,6 +39,24 @@ type LineRange struct {
3639
EndLine int `json:"end_line,omitempty"`
3740
}
3841

42+
// DatabaseFormatHash contains a hash uniquely identifying format of the database.
43+
// In covers both structure and semantics of the data, and is supposed to be used
44+
// for caching of the database files.
45+
var DatabaseFormatHash = func() string {
46+
// Semantic version should be bumped when the schema does not change,
47+
// but stored values changes.
48+
const semanticVersion = "1"
49+
schema, err := jsonschema.For[Database](nil)
50+
if err != nil {
51+
panic(err)
52+
}
53+
serialized, err := json.Marshal(schema)
54+
if err != nil {
55+
panic(err)
56+
}
57+
return hash.String(serialized, semanticVersion)
58+
}()
59+
3960
func (db *Database) Merge(other *Database) {
4061
db.Definitions = append(db.Definitions, other.Definitions...)
4162
}

0 commit comments

Comments
 (0)