Skip to content

Commit 2acf67a

Browse files
committed
Fuzz which files share a checker
Order independence should also mean that results do not depend on which files a checker has been given, since project and compiler checker pools assign files differently. Let tests pick the checker of every file, and have the check order fuzzer use a seeded random assignment over two to four checkers for half of its seeds.
1 parent 47c4bf9 commit 2acf67a

4 files changed

Lines changed: 90 additions & 25 deletions

File tree

‎tsc/internal/compiler/checkerpool.go‎

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type checkerPool struct {
3333
checkers []*checker.Checker
3434
locks []*sync.Mutex
3535
fileAssociations map[*ast.SourceFile]*checker.Checker
36+
associations []int // from ProgramFactories.AssignCheckers, if set
3637
}
3738

3839
var _ CheckerPool = (*checkerPool)(nil)
@@ -307,20 +308,35 @@ func newCheckerPool(program *Program) *checkerPool {
307308
}
308309

309310
func newCheckerPoolWithTracing(program *Program, tr *tracing.Tracing) *checkerPool {
311+
return newCheckerPoolWithAssignment(program, tr, nil)
312+
}
313+
314+
// newCheckerPoolWithAssignment is newCheckerPoolWithTracing with the checker
315+
// of each file given by assign, if set; see ProgramFactories.AssignCheckers.
316+
func newCheckerPoolWithAssignment(program *Program, tr *tracing.Tracing, assign func([]*ast.SourceFile) []int) *checkerPool {
317+
var associations []int
310318
checkerCount := 4
311-
if program.SingleThreaded() {
319+
if assign != nil {
320+
associations = assign(program.files)
312321
checkerCount = 1
313-
} else if c := program.Options().Checkers; c != nil {
314-
checkerCount = *c
322+
if len(associations) > 0 {
323+
checkerCount = slices.Max(associations) + 1
324+
}
325+
} else {
326+
if program.SingleThreaded() {
327+
checkerCount = 1
328+
} else if c := program.Options().Checkers; c != nil {
329+
checkerCount = *c
330+
}
331+
checkerCount = max(min(checkerCount, len(program.files), 256), 1)
315332
}
316333

317-
checkerCount = max(min(checkerCount, len(program.files), 256), 1)
318-
319334
pool := &checkerPool{
320-
program: program,
321-
checkers: make([]*checker.Checker, checkerCount),
322-
locks: make([]*sync.Mutex, checkerCount),
323-
tracing: tr,
335+
program: program,
336+
checkers: make([]*checker.Checker, checkerCount),
337+
locks: make([]*sync.Mutex, checkerCount),
338+
tracing: tr,
339+
associations: associations,
324340
}
325341

326342
return pool
@@ -380,8 +396,11 @@ func (p *checkerPool) createCheckers() {
380396

381397
wg.RunAndWait()
382398

383-
associations := make([]int, len(p.program.files))
384-
if checkerCount > 1 {
399+
associations := p.associations
400+
if associations == nil {
401+
associations = make([]int, len(p.program.files))
402+
}
403+
if checkerCount > 1 && p.associations == nil {
385404
baseWeights := make([]int, len(p.program.files))
386405
importCounts := make([]int, len(p.program.files))
387406
isDeclarationFile := make([]bool, len(p.program.files))

‎tsc/internal/compiler/program.go‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ type ProgramHosts struct {
6060
type ProgramFactories struct {
6161
CreateCheckerPool func(*Program) CheckerPool
6262
CreateModuleResolver func(module.ResolverOptions) module.Resolver
63+
// AssignCheckers, for testing, returns the index of the checker for each
64+
// file of the program, in place of the usual checker count and assignment.
65+
AssignCheckers func(files []*ast.SourceFile) []int
6366
}
6467

6568
func (p *ProgramConfig) canUseProjectReferenceSource() bool {
@@ -324,7 +327,7 @@ func NewProgram(opts ProgramOptions) *Program {
324327
defer opts.Tracing.Push(tracing.PhaseProgram, "createProgram", map[string]any{"configFilePath": opts.Config.CompilerOptions().ConfigFilePath}, true)()
325328
}
326329
p.processedFiles, p.resolutionData, p.moduleResolutionError = processAllProgramFiles(opts, p.SingleThreaded())
327-
p.initCheckerPool(opts.CreateCheckerPool)
330+
p.initCheckerPool(opts.CreateCheckerPool, opts.AssignCheckers)
328331
p.verifyCompilerOptions()
329332
p.collectContentMapperOptionDiagnostics()
330333
return p
@@ -445,19 +448,19 @@ func (p *Program) ReuseProgram(
445448
}
446449
}
447450
result.replaceSyntheticImports(newFile, newSupplementalFiles)
448-
result.initCheckerPool(createCheckerPool)
451+
result.initCheckerPool(createCheckerPool, nil)
449452
return result, newFile, true
450453
}
451454

452-
func (p *Program) initCheckerPool(create func(*Program) CheckerPool) {
455+
func (p *Program) initCheckerPool(create func(*Program) CheckerPool, assign func([]*ast.SourceFile) []int) {
453456
if !p.finishedProcessing {
454457
panic("Program must finish processing files before initializing checker pool")
455458
}
456459

457460
if create != nil {
458461
p.checkerPool = create(p)
459462
} else {
460-
pool := newCheckerPoolWithTracing(p, p.hosts.Tracing)
463+
pool := newCheckerPoolWithAssignment(p, p.hosts.Tracing, assign)
461464
p.checkerPool = pool
462465
p.compilerCheckerPool = pool
463466
}

‎tsc/internal/testutil/harnessutil/checkorderfuzz.go‎

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func fuzzCheckOrder(t *testing.T, host compiler.CompilerHost, config *tsoptions.
8686
fuzzConfig.Errors = config.Errors
8787

8888
ctx := context.Background()
89-
expected := fuzzRun(ctx, host, fuzzConfig, nil)
89+
expected := fuzzRun(ctx, host, fuzzConfig, nil, nil)
9090
seeds := make([]uint64, n)
9191
for i := range seeds {
9292
seeds[i] = uint64(i)
@@ -102,21 +102,40 @@ func fuzzCheckOrder(t *testing.T, host compiler.CompilerHost, config *tsoptions.
102102
h.Write([]byte(t.Name()))
103103
stream := h.Sum64()
104104
for _, seed := range seeds {
105-
queries := fuzzQueries(createProgram(host, fuzzConfig), rand.New(rand.NewPCG(seed, stream)))
105+
rng := rand.New(rand.NewPCG(seed, stream))
106+
queries := fuzzQueries(createProgram(host, fuzzConfig), rng)
107+
assign := fuzzAssignCheckers(rng)
106108
diverges := func(queries []fuzzQuery) bool {
107-
return fuzzDiff(expected, fuzzRun(ctx, host, fuzzConfig, queries)) != ""
109+
return fuzzDiff(expected, fuzzRun(ctx, host, fuzzConfig, assign, queries)) != ""
108110
}
109111
if !diverges(queries) {
110112
continue
111113
}
112-
queries = fuzzMinimize(queries, diverges)
113114
var b strings.Builder
114-
fmt.Fprintf(&b, "check order fuzzing with TS_CHECK_ORDER_FUZZ_SEED=%d changed results after querying:\n", seed)
115-
for _, q := range queries {
116-
fmt.Fprintf(&b, " %s\n", q)
115+
fmt.Fprintf(&b, "check order fuzzing with TS_CHECK_ORDER_FUZZ_SEED=%d changed results", seed)
116+
if assign != nil {
117+
b.WriteString(" with files checked by checkers:\n")
118+
program := createProgram(host, fuzzConfig).Program()
119+
files := program.GetSourceFiles()
120+
for i, c := range assign(files) {
121+
if !program.IsSourceFileDefaultLibrary(files[i].PathKey()) {
122+
fmt.Fprintf(&b, " %s: %d\n", files[i].FileName(), c)
123+
}
124+
}
125+
} else {
126+
b.WriteString("\n")
127+
}
128+
if diverges(nil) {
129+
queries = nil
130+
} else {
131+
queries = fuzzMinimize(queries, diverges)
132+
b.WriteString("after querying:\n")
133+
for _, q := range queries {
134+
fmt.Fprintf(&b, " %s\n", q)
135+
}
117136
}
118137
b.WriteString("results in program order (-) and after the queries (+):\n")
119-
b.WriteString(fuzzDiff(expected, fuzzRun(ctx, host, fuzzConfig, queries)))
138+
b.WriteString(fuzzDiff(expected, fuzzRun(ctx, host, fuzzConfig, assign, queries)))
120139
t.Error(b.String())
121140
}
122141
}
@@ -248,10 +267,29 @@ func fuzzQueryResultWithIDs(c *checker.Checker, node *ast.Node, kind fuzzQueryKi
248267
return ""
249268
}
250269

270+
// fuzzAssignCheckers returns, for half of the seeds, a random assignment of
271+
// files to up to four checkers, so that results cannot depend on which
272+
// files share a checker.
273+
func fuzzAssignCheckers(rng *rand.Rand) func([]*ast.SourceFile) []int {
274+
if rng.IntN(2) == 0 {
275+
return nil
276+
}
277+
count := 2 + rng.IntN(3)
278+
seed := rng.Uint64()
279+
return func(files []*ast.SourceFile) []int {
280+
r := rand.New(rand.NewPCG(seed, 0))
281+
associations := make([]int, len(files))
282+
for i := range associations {
283+
associations[i] = r.IntN(count)
284+
}
285+
return associations
286+
}
287+
}
288+
251289
// fuzzRun answers queries, given in the files of the program that
252290
// fuzzQueries was given, in a fresh program, then returns its results.
253-
func fuzzRun(ctx context.Context, host compiler.CompilerHost, config *tsoptions.ParsedCommandLine, queries []fuzzQuery) fuzzResult {
254-
program := createProgram(host, config)
291+
func fuzzRun(ctx context.Context, host compiler.CompilerHost, config *tsoptions.ParsedCommandLine, assign func([]*ast.SourceFile) []int, queries []fuzzQuery) fuzzResult {
292+
program := createProgramWithCheckers(host, config, assign)
255293
files := fuzzCheckedFiles(program)
256294
// Queries name nodes of another program over the same files, so find
257295
// the same nodes here by their index.

‎tsc/internal/testutil/harnessutil/harnessutil.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,10 @@ func getTestBuildInfoReader(host compiler.CompilerHost) *testBuildInfoReader {
11071107
}
11081108

11091109
func createProgram(host compiler.CompilerHost, config *tsoptions.ParsedCommandLine) compiler.ProgramLike {
1110+
return createProgramWithCheckers(host, config, nil)
1111+
}
1112+
1113+
func createProgramWithCheckers(host compiler.CompilerHost, config *tsoptions.ParsedCommandLine, assignCheckers func([]*ast.SourceFile) []int) compiler.ProgramLike {
11101114
var singleThreaded core.Tristate
11111115
if testutil.TestProgramIsSingleThreaded() {
11121116
singleThreaded = core.TSTrue
@@ -1116,6 +1120,7 @@ func createProgram(host compiler.CompilerHost, config *tsoptions.ParsedCommandLi
11161120
Config: config,
11171121
Host: host,
11181122
SingleThreaded: singleThreaded,
1123+
AssignCheckers: assignCheckers,
11191124
}
11201125
program := compiler.NewProgram(programOptions)
11211126
if config.CompilerOptions().Incremental.IsTrue() {

0 commit comments

Comments
 (0)