Skip to content

Commit c961f27

Browse files
egonelbreclaude
andcommitted
add a cross-compilable disassembly fixture program
testdata/testprog is a dependency-free program with a varied noinline instruction mix for exercising the disassembler and code view by hand; a goobj test cross-compiles it for amd64 and arm64 and checks both binaries disassemble. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 84d84c2 commit c961f27

2 files changed

Lines changed: 158 additions & 0 deletions

File tree

‎internal/goobj/testprog_test.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package goobj
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
10+
"loov.dev/lensm/internal/disasm"
11+
)
12+
13+
// TestLoadCrossCompiledTestprog cross-compiles testdata/testprog for each
14+
// supported architecture and checks the result disassembles.
15+
func TestLoadCrossCompiledTestprog(t *testing.T) {
16+
if testing.Short() {
17+
t.Skip("builds test binaries")
18+
}
19+
root, err := filepath.Abs("../..")
20+
if err != nil {
21+
t.Fatal(err)
22+
}
23+
for _, arch := range []string{"amd64", "arm64"} {
24+
t.Run(arch, func(t *testing.T) {
25+
bin := filepath.Join(t.TempDir(), "testprog_"+arch+".exe")
26+
cmd := exec.Command("go", "build", "-o", bin, "./testdata/testprog")
27+
cmd.Dir = root
28+
cmd.Env = append(os.Environ(), "GOOS=linux", "GOARCH="+arch, "CGO_ENABLED=0")
29+
if out, err := cmd.CombinedOutput(); err != nil {
30+
t.Fatalf("go build: %v\n%s", err, out)
31+
}
32+
33+
file, err := Load(bin)
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
defer file.Close()
38+
39+
for _, fn := range file.Funcs() {
40+
if !strings.Contains(fn.Name(), "main.sumInts") {
41+
continue
42+
}
43+
code, err := fn.Load(disasm.Options{Context: 1})
44+
if err != nil {
45+
t.Fatal(err)
46+
}
47+
if code.Arch != arch {
48+
t.Errorf("Arch = %q, want %q", code.Arch, arch)
49+
}
50+
if len(code.Insts) == 0 {
51+
t.Error("main.sumInts disassembled to 0 instructions")
52+
} else if code.Insts[0].NativeText == "" {
53+
t.Error("main.sumInts has no native syntax")
54+
}
55+
return
56+
}
57+
t.Fatal("main.sumInts not found in binary")
58+
})
59+
}
60+
}

‎testdata/testprog/main.go‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Command testprog is a small dependency-free fixture for exercising the
2+
// disassembler and code view by hand or from tests. It cross-compiles to any
3+
// Go target, e.g.:
4+
//
5+
// GOOS=linux GOARCH=amd64 go build -o testprog_amd64.exe ./testdata/testprog
6+
// GOOS=linux GOARCH=arm64 go build -o testprog_arm64.exe ./testdata/testprog
7+
//
8+
// The functions are noinline and their results printed, so each survives as a
9+
// distinct symbol with a varied instruction mix: integer and floating-point
10+
// arithmetic, memory traffic, bit tricks, branches, and calls.
11+
package main
12+
13+
import (
14+
"math"
15+
"math/bits"
16+
)
17+
18+
//go:noinline
19+
func sumInts(xs []int) int {
20+
total := 0
21+
for _, x := range xs {
22+
total += x
23+
}
24+
return total
25+
}
26+
27+
//go:noinline
28+
func dot(a, b []float64) float64 {
29+
total := 0.0
30+
for i := range a {
31+
total += a[i] * b[i]
32+
}
33+
return total
34+
}
35+
36+
//go:noinline
37+
func popcountAll(xs []uint64) int {
38+
total := 0
39+
for _, x := range xs {
40+
total += bits.OnesCount64(x)
41+
}
42+
return total
43+
}
44+
45+
//go:noinline
46+
func collatzSteps(n int) int {
47+
steps := 0
48+
for n != 1 {
49+
if n%2 == 0 {
50+
n /= 2
51+
} else {
52+
n = 3*n + 1
53+
}
54+
steps++
55+
}
56+
return steps
57+
}
58+
59+
//go:noinline
60+
func mat4Mul(a, b *[16]float64) [16]float64 {
61+
var out [16]float64
62+
for row := range 4 {
63+
for col := range 4 {
64+
for k := range 4 {
65+
out[row*4+col] += a[row*4+k] * b[k*4+col]
66+
}
67+
}
68+
}
69+
return out
70+
}
71+
72+
//go:noinline
73+
func normalize(v []float64) {
74+
length := 0.0
75+
for _, x := range v {
76+
length += x * x
77+
}
78+
length = math.Sqrt(length)
79+
for i := range v {
80+
v[i] /= length
81+
}
82+
}
83+
84+
func main() {
85+
ints := []int{3, 1, 4, 1, 5, 9, 2, 6}
86+
floats := []float64{2.7, 1.8, 2.8, 1.8}
87+
words := []uint64{0xdeadbeef, 0xcafebabe, 0x0ddba11}
88+
89+
var a, b [16]float64
90+
for i := range a {
91+
a[i] = float64(i)
92+
b[i] = float64(15 - i)
93+
}
94+
m := mat4Mul(&a, &b)
95+
normalize(floats)
96+
97+
println(sumInts(ints), dot(floats, floats), popcountAll(words), collatzSteps(27), m[5])
98+
}

0 commit comments

Comments
 (0)