Skip to content

Commit 4e66dda

Browse files
committed
ixdiff: update golang.org/x/arch to ab53fe6c43b6
x/arch v0.31.0 does not decode VEX-encoded MULXQ, which bits.Mul64 can compile to at GOAMD64=v3. The undecodable first byte became a BYTE pseudo-instruction and decoding resumed inside the instruction, so the rest of the function decoded as garbage: junk opcodes in the histogram, wrong instruction and spill counts, and noisy function diffs. The x86 relocation walk failed on such functions and fell back to full analysis. Update to the commit the Go tree vendors, which decodes MULXQ with the same Go syntax as go tool objdump. Add a fixture function written in assembly around a MULXQ, since the compiler emits it only for some code shapes, and tests at the decoder, the relocation walk, and the listing. The x86 decoder still misreads VZEROUPPER, so its length correction stays.
1 parent 37c6ac6 commit 4e66dda

8 files changed

Lines changed: 121 additions & 4 deletions

File tree

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/google/go-cmp v0.7.0
77
github.com/loov/disasm v0.1.3
88
github.com/zeebo/clingy v0.0.0-20260908180858-32a9e61e718e
9-
golang.org/x/arch v0.31.0
9+
golang.org/x/arch v0.31.1-0.20260922213335-ab53fe6c43b6
1010
golang.org/x/sync v0.23.0
1111
)
1212

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ github.com/zeebo/clingy v0.0.0-20260908180858-32a9e61e718e h1:PsLYYMKHqJgYbIkFsp
1212
github.com/zeebo/clingy v0.0.0-20260908180858-32a9e61e718e/go.mod h1:UYtPwAnfxW957MVe+h9FNm9JNWBh9eLjy4O8n/WOmZ0=
1313
github.com/zeebo/errs/v2 v2.0.5 h1:edtyFoQq9OBBsNPcu0b0wHtRvA8haukAHrGyOfWQGNI=
1414
github.com/zeebo/errs/v2 v2.0.5/go.mod h1:OKmvVZt4UqpyJrYFykDKm168ZquJ55pbbIVUICNmLN0=
15-
golang.org/x/arch v0.31.0 h1:22MlEb14/O/EPCYHFxsDdv5TuLD5dMjT5e2QeJw4ULk=
16-
golang.org/x/arch v0.31.0/go.mod h1:KcJSod3cqT2dKcjBxqTyGfbumNikqU9p5tHJinPJnuY=
15+
golang.org/x/arch v0.31.1-0.20260922213335-ab53fe6c43b6 h1:hVlIbIBR5er4f8JgYMQl3x6fARED1/L2lgEMVwHTlNE=
16+
golang.org/x/arch v0.31.1-0.20260922213335-ab53fe6c43b6/go.mod h1:KcJSod3cqT2dKcjBxqTyGfbumNikqU9p5tHJinPJnuY=
1717
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
1818
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=

‎internal/norm/reloc_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ func TestRelocOnly_AMD64(t *testing.T) {
809809
return []byte{0x48, 0x8b, 0x05, byte(disp), byte(disp >> 8), byte(disp >> 16), byte(disp >> 24)}
810810
}
811811
ret := []byte{0xc3}
812+
mulxq := []byte{0xc4, 0x62, 0xa3, 0xf6, 0xd2} // MULXQ DX, R11, R10
812813
vzeroupper := []byte{0xc5, 0xf8, 0x77}
813814
cat := func(bs ...[]byte) []byte { return bytes.Join(bs, nil) }
814815

@@ -889,6 +890,15 @@ func TestRelocOnly_AMD64(t *testing.T) {
889890
new: cat(vzeroupper, call(0xff8), ret),
890891
want: false,
891892
},
893+
{
894+
// The call ends at offset 10. old: 0x1000a+0xff6 = 0x11000
895+
// (callee); new: 0x2000a+0x1ff6 = 0x22000. MULXQ is VEX
896+
// encoded and must decode for the walk to reach the call.
897+
name: "same callee after mulxq at shifted address",
898+
old: cat(mulxq, call(0xff6), ret),
899+
new: cat(mulxq, call(0x1ff6), ret),
900+
want: true,
901+
},
892902
{
893903
// 0x06 does not decode in 64-bit mode; the equal bytes
894904
// behind it must not be accepted unexamined.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package norm
2+
3+
import (
4+
"testing"
5+
6+
"golang.org/x/arch/x86/x86asm"
7+
)
8+
9+
// TestDecodeX86_MULXQ checks VEX-encoded MULXQ, which bits.Mul64
10+
// compiles to at GOAMD64=v3. A decoder that does not know it reports
11+
// the bytes as undecodable, and resuming one byte later lands inside
12+
// the instruction and loses sync with the code. The bytes and syntax
13+
// are from go tool objdump.
14+
func TestDecodeX86_MULXQ(t *testing.T) {
15+
tests := []struct {
16+
code []byte
17+
want string
18+
}{
19+
{[]byte{0xc4, 0x62, 0xa3, 0xf6, 0xd2}, "MULXQ DX, R11, R10"},
20+
{[]byte{0xc4, 0x62, 0xfb, 0xf6, 0xf9}, "MULXQ CX, AX, R15"},
21+
{[]byte{0xc4, 0x62, 0xfb, 0xf6, 0xfe}, "MULXQ SI, AX, R15"},
22+
}
23+
for _, tt := range tests {
24+
t.Run(tt.want, func(t *testing.T) {
25+
in, err := decodeX86(tt.code, 64)
26+
if err != nil {
27+
t.Fatalf("decodeX86(% x): %v", tt.code, err)
28+
}
29+
if got := x86asm.GoSyntax(in, 0, nil); in.Len != len(tt.code) || got != tt.want {
30+
t.Errorf("decodeX86(% x) = %d-byte %q, want %d-byte %q",
31+
tt.code, in.Len, got, len(tt.code), tt.want)
32+
}
33+
})
34+
}
35+
}
36+
37+
func TestDecodeX86_MULXQThenRETStaysInStep(t *testing.T) {
38+
code := []byte{0xc4, 0x62, 0xa3, 0xf6, 0xd2, 0xc3}
39+
var got []string
40+
for off := 0; off < len(code); {
41+
in, err := decodeX86(code[off:], 64)
42+
if err != nil {
43+
t.Fatalf("decodeX86 at offset %d: %v", off, err)
44+
}
45+
got = append(got, x86asm.GoSyntax(in, 0, nil))
46+
off += in.Len
47+
}
48+
if len(got) != 2 || got[0] != "MULXQ DX, R11, R10" || got[1] != "RET" {
49+
t.Errorf("decoded %q, want [MULXQ DX, R11, R10, RET]", got)
50+
}
51+
}

‎internal/testbin/testbin.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Config struct {
2121
GOARCH string // target architecture; host architecture when empty
2222
GCFlags string // passed as -gcflags
2323
LDFlags string // passed as -ldflags
24-
Tags string // passed as -tags; "pad" adds a layout-shifting function
24+
Tags string // passed as -tags; "pad" adds a layout-shifting function, "mulx" an amd64 MULXQ
2525
}
2626

2727
// Build compiles the fixture program with cfg and returns the path to
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build mulx
2+
3+
package main
4+
5+
import "os"
6+
7+
// mul64 returns the 128-bit product of a and b. It is written in
8+
// assembly around a VEX-encoded MULXQ so tests can check that VEX
9+
// instructions decode in step with the code; the compiler only emits
10+
// MULXQ for some code shapes, and not on every toolchain.
11+
func mul64(a, b uint64) (hi, lo uint64)
12+
13+
func init() {
14+
if os.Getenv("IXDIFF_MULX") == "run" {
15+
println(mul64(uint64(len(os.Args)), 3))
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build mulx
2+
3+
#include "textflag.h"
4+
5+
// func mul64(a, b uint64) (hi, lo uint64)
6+
TEXT ·mul64(SB), NOSPLIT, $0-32
7+
MOVQ a+0(FP), DX
8+
MOVQ b+8(FP), R8
9+
MULXQ R8, R11, R10
10+
MOVQ R10, hi+16(FP)
11+
MOVQ R11, lo+24(FP)
12+
RET

‎ixdiff/ixdiff_test.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
"os/exec"
66
"path/filepath"
7+
"strings"
78
"testing"
89

910
"github.com/google/go-cmp/cmp"
@@ -121,6 +122,32 @@ func TestFunc_StatsMatchesSeparateMethods(t *testing.T) {
121122
}
122123
}
123124

125+
// TestFunc_TextDecodesVEXMULXQ lists a function containing the
126+
// VEX-encoded MULXQ that bits.Mul64 can compile to at GOAMD64=v3.
127+
// Undecoded, it surfaces as BYTE pseudo-instructions followed by
128+
// garbage decoded from mid-instruction offsets.
129+
func TestFunc_TextDecodesVEXMULXQ(t *testing.T) {
130+
bin := open(t, testbin.Config{GOOS: "linux", GOARCH: "amd64", Tags: "mulx"})
131+
f, ok := bin.Func("main.mul64")
132+
if !ok {
133+
t.Fatal("Func(main.mul64) not found")
134+
}
135+
insts, err := f.Text()
136+
if err != nil {
137+
t.Fatalf("Text: %v", err)
138+
}
139+
var mulx bool
140+
for _, in := range insts {
141+
if strings.HasPrefix(in.Text, "BYTE") {
142+
t.Errorf("undecoded bytes at %#x: %s", in.Addr, in.Text)
143+
}
144+
mulx = mulx || strings.HasPrefix(in.Text, "MULXQ ")
145+
}
146+
if !mulx {
147+
t.Errorf("main.mul64 has no MULXQ; listing: %+v", insts)
148+
}
149+
}
150+
124151
func TestCompare_ClassifiesEveryPair(t *testing.T) {
125152
d := compare(t, base, noinline)
126153

0 commit comments

Comments
 (0)