|
| 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 | +} |
0 commit comments