Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions internal/codeguard/runner/support/corpus.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,22 @@ func readCappedFile(path string) ([]byte, error) {
// Each cached slot carries its own sync.Once, so concurrent callers racing on a
// cold slot compute it exactly once and every caller observes the same result.
type fileCorpus struct {
mu sync.Mutex
targets map[string]*targetListing
reads map[string]*fileRead
asts map[string]*goParse
scripts map[string]*scriptParse
mu sync.Mutex
targets map[string]*targetListing
reads map[string]*fileRead
asts map[string]*goParse
scripts map[string]*scriptParse
scriptBytes int
scriptCount int
scriptParse chan struct{}
}

// maxTreeSitterScanBytes bounds the source represented by retained script
// trees during one scan. Parsing is also serialized because the pure-Go
// runtime's transient heap is much larger than its input.
const maxTreeSitterScanBytes = 256 * 1024
const maxTreeSitterScanFiles = 64

type targetListing struct {
once sync.Once
files []string
Expand Down Expand Up @@ -77,10 +86,11 @@ type scriptParse struct {

func newFileCorpus() *fileCorpus {
return &fileCorpus{
targets: map[string]*targetListing{},
reads: map[string]*fileRead{},
asts: map[string]*goParse{},
scripts: map[string]*scriptParse{},
targets: map[string]*targetListing{},
reads: map[string]*fileRead{},
asts: map[string]*goParse{},
scripts: map[string]*scriptParse{},
scriptParse: make(chan struct{}, 1),
}
}

Expand Down Expand Up @@ -153,12 +163,20 @@ func (c *fileCorpus) parseScript(path string, data []byte, lang checkSupport.Scr
c.mu.Lock()
entry, ok := c.scripts[key]
if !ok {
if c.scriptCount >= maxTreeSitterScanFiles || c.scriptBytes+len(data) > maxTreeSitterScanBytes {
c.mu.Unlock()
return nil, fmt.Errorf("tree-sitter scan budget of %d files or %d bytes exhausted", maxTreeSitterScanFiles, maxTreeSitterScanBytes)
}
entry = &scriptParse{}
c.scripts[key] = entry
c.scriptCount++
c.scriptBytes += len(data)
}
c.mu.Unlock()

entry.once.Do(func() {
c.scriptParse <- struct{}{}
defer func() { <-c.scriptParse }()
entry.tree, entry.err = checkSupport.ParseScriptSource(path, data, lang)
})
return entry.tree, entry.err
Expand Down
38 changes: 38 additions & 0 deletions internal/codeguard/runner/support/corpus_script_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package support

import (
"strings"
"testing"

checkSupport "github.com/devr-tools/codeguard/internal/codeguard/checks/support"
)

func TestParseScriptEnforcesAggregateBudget(t *testing.T) {
corpus := newFileCorpus()
data := []byte("const value = 1;\n")
corpus.scriptBytes = maxTreeSitterScanBytes - len(data)

if _, err := corpus.parseScript("first.ts", data, checkSupport.ScriptLangTypeScript); err != nil {
t.Fatalf("parse at budget boundary: %v", err)
}
if _, err := corpus.parseScript("second.ts", data, checkSupport.ScriptLangTypeScript); err == nil ||
!strings.Contains(err.Error(), "scan budget") {
t.Fatalf("parse beyond budget error = %v, want scan budget error", err)
}
if got := len(corpus.scripts); got != 1 {
t.Fatalf("cached script count = %d, want 1", got)
}
}

func TestParseScriptEnforcesFileCountBudget(t *testing.T) {
corpus := newFileCorpus()
corpus.scriptCount = maxTreeSitterScanFiles

if _, err := corpus.parseScript("extra.ts", nil, checkSupport.ScriptLangTypeScript); err == nil ||
!strings.Contains(err.Error(), "scan budget") {
t.Fatalf("parse beyond file budget error = %v, want scan budget error", err)
}
if got := len(corpus.scripts); got != 0 {
t.Fatalf("cached script count = %d, want 0", got)
}
}
Loading