feat(format): fuzz targets for the parser and formatter
Assisted-by: GLM 5.3 Flash
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package parser
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// FuzzParse hammers the parser with arbitrary input. The contract: no panic,
|
||||
// and always a usable file, whether or not diagnostics were reported. The
|
||||
// seed corpus carries the repository's kernels, so a plain `go test` run
|
||||
// replays every seed as a regression case and CI exercises them without any
|
||||
// fuzzing budget.
|
||||
func FuzzParse(f *testing.F) {
|
||||
for _, pattern := range []string{
|
||||
"../testdata/*.s",
|
||||
"../testdata/verify/*.s",
|
||||
} {
|
||||
files, _ := filepath.Glob(pattern)
|
||||
for _, path := range files {
|
||||
if b, err := os.ReadFile(path); err == nil {
|
||||
f.Add(string(b))
|
||||
}
|
||||
}
|
||||
}
|
||||
f.Add("TEXT ·f(SB), NOSPLIT, $0\n\tRET\n")
|
||||
f.Add("garbage ### ??? ::: \xff\xfe\n")
|
||||
f.Add("#define A(x) x+1\nTEXT ·f(SB), $0\n\tA(2)\n\tRET\n")
|
||||
f.Add("DATA t<>+0(SB)/8, $1\nGLOBL t<>(SB), RODATA, $8\n")
|
||||
f.Add("TEXT ·f(SB), $0\n\tJMP (AX)\n\tCALL (BX)\n\tRET\n")
|
||||
|
||||
f.Fuzz(func(t *testing.T, src string) {
|
||||
file, _ := Parse("fuzz.s", src)
|
||||
if file == nil {
|
||||
t.Fatal("Parse returned a nil file")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user