feat(verify): add universal --ground-truth verification against go tool asm
Assisted-by: Qwen 3.8 Max Preview
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package verify
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGroundTruthBasic(t *testing.T) {
|
||||
// Use the simple test kernel — it assembles with go tool asm.
|
||||
gt, err := GroundTruth("../testdata/verify/basic_amd64.s")
|
||||
if err != nil {
|
||||
t.Fatalf("GroundTruth: %v", err)
|
||||
}
|
||||
if len(gt) == 0 {
|
||||
t.Fatal("no functions extracted from ground truth")
|
||||
}
|
||||
// The "add" function should be present and non-empty.
|
||||
code, ok := gt["add"]
|
||||
if !ok {
|
||||
t.Fatalf("function 'add' not found in ground truth; got: %v", keys(gt))
|
||||
}
|
||||
if len(code) == 0 {
|
||||
t.Fatal("add: zero-length code")
|
||||
}
|
||||
t.Logf("ground truth functions: %v", keys(gt))
|
||||
}
|
||||
|
||||
func TestGroundTruthComparison(t *testing.T) {
|
||||
// Assemble with gasm and compare against go tool asm.
|
||||
k, err := Load("../testdata/verify/basic_amd64.s")
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
defer k.Close()
|
||||
|
||||
gt, err := GroundTruth("../testdata/verify/basic_amd64.s")
|
||||
if err != nil {
|
||||
t.Fatalf("GroundTruth: %v", err)
|
||||
}
|
||||
|
||||
for _, name := range k.FuncNames() {
|
||||
fl, _ := k.Func(name)
|
||||
gasmCode := k.Image().Code[fl.Offset : fl.Offset+fl.Size]
|
||||
goCode, ok := gt[name]
|
||||
if !ok {
|
||||
t.Errorf("%s: not in ground truth", name)
|
||||
continue
|
||||
}
|
||||
if len(gasmCode) != len(goCode) {
|
||||
t.Errorf("%s: size mismatch: gasm=%d go=%d", name, len(gasmCode), len(goCode))
|
||||
continue
|
||||
}
|
||||
for i := range gasmCode {
|
||||
if gasmCode[i] != goCode[i] {
|
||||
t.Errorf("%s: byte %d differs: gasm=%02x go=%02x", name, i, gasmCode[i], goCode[i])
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroundTruthBadFile(t *testing.T) {
|
||||
_, err := GroundTruth("/nonexistent/file_amd64.s")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nonexistent file")
|
||||
}
|
||||
}
|
||||
|
||||
func keys(m map[string][]byte) []string {
|
||||
out := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
out = append(out, k)
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user