dvyukov/go-fuzz

go-fuzz-build: build fails at tip Go due to duplicated //go:build lines

findleyr opened this issue · 3 comments

I believe this is only an issue for the newly landed changes in tip Go for Go 1.17: golang.org/issues/41184

When built against tip Go, go-fuzz-build fails with, for example, the following error:

failed to execute go build: exit status 1
../../../../src/go/src/os/error.go:9: errno_unix.go: multiple //go:build comments
../../../../src/go/src/internal/poll/copy_file_range_linux.go:8: at.go: multiple //go:build comments
../../../../src/go/src/reflect/value.go:10: exp_asm.go: multiple //go:build comments
../../../../src/go/src/math/fma.go:7: bits_errors.go: multiple //go:build comments
../../../../src/go/src/fmt/print.go:10: dir_unix.go: multiple //go:build comments
../../../../src/go/src/go/scanner/scanner.go:15: path_unix.go: multiple //go:build comments
../../../../src/go/src/internal/fmtsort/sort.go:13: slice_go113.go: multiple //go:build comments
../../../../src/go/src/reflect/type.go:20: bytealg.go: multiple //go:build comments

Checking the instrumented goroot, we can see that this is indeed the case, for example, in errno_unix.go:

//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris                                                            
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris                                                                                  
                                                                                                                                                     
//line /usr/local/google/home/rfindley/src/go/src/internal/poll/errno_unix.go:5                                                                      
                                                                                                                                                     
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris                                                            
                                                                                                                                                     
//line /usr/local/google/home/rfindley/src/go/src/internal/poll/errno_unix.go:8                                                                      
package poll  

Naively skimming the code, it appears there is simultaneously special handling in trimComments to preserve comments starting with //go:, and also special handling to preserve initial comments (in initialComments). It looks like these overlap, resulting in the duplicated //go:build lines.

I was able to achieve what I wanted (fuzzing some recent changes to go/parser) by patching go-fuzz-build to just skip //go:build lines, but this is just a hack: a real solution should preserve (and not duplicate) the //go:build lines.

Apologies if I've misused terminology; this is my first time using go-fuzz. Thanks for building such a useful tool! go-fuzz found the crash I was looking for in just a few minutes :)

Thanks for the excellent bug report.

go-fuzz is slated to be replaced by something in the standard library. So your hack (“patching go-fuzz-build to just skip //go:build lines”) actually sounds like just what is called for to tide us over.

Want to send a PR, by chance?

Thanks for the quick response!

go-fuzz is slated to be replaced by something in the standard library.

Indeed! My concern was that https://golang.org/issue/44551 will, if accepted, only be experimental in 1.17. But actually, reading the transition plan for //go:build, I think you're right that it's safe to just skip //go:build lines in the interim.

Want to send a PR, by chance?

Sure, will do (but not tonight :))

mvdan commented

Ah, I just got bit by this. I'll just do a hack to locally remove the duplicate comment until I can jump to Go's own fuzzing.