Skip to content

Commit 898ae4d

Browse files
committed
fix #277, fix #260: fix crash
Fix crash because of parallel access to ssa.Program
1 parent 952cc0b commit 898ae4d

File tree

5 files changed

+24
-28
lines changed

5 files changed

+24
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/dist/
44
/.idea/
55
/test/path
6+
/golangci-lint

Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
test:
2-
go install ./cmd/...
3-
GL_TEST_RUN=1 golangci-lint run -v
4-
GL_TEST_RUN=1 golangci-lint run --fast --no-config -v
5-
GL_TEST_RUN=1 golangci-lint run --no-config -v
2+
go build -o golangci-lint ./cmd/golangci-lint
3+
GL_TEST_RUN=1 ./golangci-lint run -v
4+
GL_TEST_RUN=1 ./golangci-lint run --fast --no-config -v
5+
GL_TEST_RUN=1 ./golangci-lint run --no-config -v
66
GL_TEST_RUN=1 go test -v ./...
77

8+
test_race:
9+
go build -race -o golangci-lint ./cmd/golangci-lint
10+
GL_TEST_RUN=1 ./golangci-lint run -v --deadline=5m
11+
812
test_linters:
913
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestSourcesFromTestdataWithIssuesDir/$T
1014

pkg/golinters/megacheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (m Megacheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.I
106106
return nil, nil
107107
}
108108

109-
issues := runMegacheck(lintCtx.Program, lintCtx.SSAProgram, lintCtx.LoaderConfig,
109+
issues := runMegacheck(lintCtx.Program, lintCtx.MegacheckSSAProgram, lintCtx.LoaderConfig,
110110
m.StaticcheckEnabled, m.GosimpleEnabled, m.UnusedEnabled, lintCtx.Settings().Unused.CheckExported)
111111
if len(issues) == 0 {
112112
return nil, nil

pkg/lint/linter/context.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ type Context struct {
1717
LoaderConfig *loader.Config // deprecated, don't use for new linters
1818
Program *loader.Program // deprecated, use Packages for new linters
1919

20-
SSAProgram *ssa.Program
20+
SSAProgram *ssa.Program // for unparam and interfacer: they don't change it
21+
MegacheckSSAProgram *ssa.Program // for megacheck: it modifies ssa program
2122

2223
Cfg *config.Config
2324
ASTCache *astcache.Cache

pkg/lint/load.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,16 @@ func (cl ContextLoader) makeFakeLoaderProgram(pkgs []*packages.Package) *loader.
126126
}
127127
}
128128

129-
func (cl ContextLoader) buildSSAProgram(pkgs []*packages.Package) *ssa.Program {
129+
func (cl ContextLoader) buildSSAProgram(pkgs []*packages.Package, name string) *ssa.Program {
130130
startedAt := time.Now()
131+
var pkgsBuiltDuration time.Duration
131132
defer func() {
132-
cl.log.Infof("SSA repr building took %s", time.Since(startedAt))
133+
cl.log.Infof("SSA %srepr building timing: packages building %s, total %s",
134+
name, pkgsBuiltDuration, time.Since(startedAt))
133135
}()
134136

135137
ssaProg, _ := ssautil.Packages(pkgs, ssa.GlobalDebug)
138+
pkgsBuiltDuration = time.Since(startedAt)
136139
ssaProg.Build()
137140
return ssaProg
138141
}
@@ -300,24 +303,10 @@ func (cl ContextLoader) Load(ctx context.Context, linters []linter.Config) (*lin
300303
prog = cl.makeFakeLoaderProgram(pkgs)
301304
}
302305

303-
var ssaProg *ssa.Program
306+
var ssaProg, megacheckSSAProg *ssa.Program
304307
if loadMode == packages.LoadAllSyntax {
305-
ssaProg = cl.buildSSAProgram(pkgs)
306-
for _, pkginfo := range prog.InitialPackages() {
307-
if pkginfo == nil {
308-
cl.log.Infof("Pkginfo is nil")
309-
continue
310-
}
311-
if pkginfo.Pkg == nil {
312-
cl.log.Infof("Pkg %#v: types package is nil", *pkginfo)
313-
continue
314-
}
315-
ssaPkg := ssaProg.Package(pkginfo.Pkg)
316-
if ssaPkg == nil {
317-
cl.log.Infof("Pkg %#v: ssaPkg is nil: %#v", *pkginfo, *pkginfo.Pkg)
318-
continue
319-
}
320-
}
308+
ssaProg = cl.buildSSAProgram(pkgs, "")
309+
megacheckSSAProg = cl.buildSSAProgram(pkgs, "for megacheck ")
321310
}
322311

323312
astLog := cl.log.Child("astcache")
@@ -327,9 +316,10 @@ func (cl ContextLoader) Load(ctx context.Context, linters []linter.Config) (*lin
327316
}
328317

329318
ret := &linter.Context{
330-
Packages: pkgs,
331-
Program: prog,
332-
SSAProgram: ssaProg,
319+
Packages: pkgs,
320+
Program: prog,
321+
SSAProgram: ssaProg,
322+
MegacheckSSAProgram: megacheckSSAProg,
333323
LoaderConfig: &loader.Config{
334324
Cwd: "", // used by depguard and fallbacked to os.Getcwd
335325
Build: nil, // used by depguard and megacheck and fallbacked to build.Default

0 commit comments

Comments
 (0)