| 1 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package arm64asm |
| 6 |
|
| 7 |
import ( |
| 8 |
"strconv" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestObjdumpARM64TestDecodeGNUSyntaxdata(t *testing.T) { |
| 14 |
testObjdumpARM64(t, testdataCases(t, "gnu")) |
| 15 |
} |
| 16 |
func TestObjdumpARM64TestDecodeGoSyntaxdata(t *testing.T) { |
| 17 |
testObjdumpARM64(t, testdataCases(t, "plan9")) |
| 18 |
} |
| 19 |
func TestObjdumpARM64Manual(t *testing.T) { testObjdumpARM64(t, hexCases(t, objdumpManualTests)) } |
| 20 |
func TestObjdumpARM64Cond(t *testing.T) { testObjdumpARM64(t, condCases(t)) } |
| 21 |
func TestObjdumpARM64(t *testing.T) { testObjdumpARM64(t, JSONCases(t)) } |
| 22 |
|
| 23 |
// objdumpManualTests holds test cases that will be run by TestObjdumpARMManual. |
| 24 |
// If you are debugging a few cases that turned up in a longer run, it can be useful |
| 25 |
// to list them here and then use -run=Manual, particularly with tracing enabled. |
| 26 |
// Note that these are byte sequences, so they must be reversed from the usual |
| 27 |
// word presentation. |
| 28 |
var objdumpManualTests = ` |
| 29 |
bf2003d5 |
| 30 |
9f2003d5 |
| 31 |
7f2003d5 |
| 32 |
5f2003d5 |
| 33 |
3f2003d5 |
| 34 |
1f2003d5 |
| 35 |
df4d03d5 |
| 36 |
ff4d03d5 |
| 37 |
28d91b14 |
| 38 |
da6cb530 |
| 39 |
15e5e514 |
| 40 |
ff4603d5 |
| 41 |
df4803d5 |
| 42 |
bf4100d5 |
| 43 |
9f3f03d5 |
| 44 |
9f3e03d5 |
| 45 |
9f3d03d5 |
| 46 |
9f3b03d5 |
| 47 |
9f3a03d5 |
| 48 |
9f3903d5 |
| 49 |
9f3703d5 |
| 50 |
9f3603d5 |
| 51 |
9f3503d5 |
| 52 |
9f3303d5 |
| 53 |
9f3203d5 |
| 54 |
9f3103d5 |
| 55 |
ff4603d5 |
| 56 |
df4803d5 |
| 57 |
bf4100d5 |
| 58 |
a3681b53 |
| 59 |
47dc78d3 |
| 60 |
0500a012 |
| 61 |
0500e092 |
| 62 |
0500a052 |
| 63 |
0500a0d2 |
| 64 |
cd5a206e |
| 65 |
cd5a202e |
| 66 |
743d050e |
| 67 |
743d0a0e |
| 68 |
743d0c0e |
| 69 |
743d084e |
| 70 |
` |
| 71 |
|
| 72 |
// allowedMismatchObjdump reports whether the mismatch between text and dec |
| 73 |
// should be allowed by the test. |
| 74 |
func allowedMismatchObjdump(text string, inst *Inst, dec ExtInst) bool { |
| 75 |
// Skip unsupported instructions |
| 76 |
if hasPrefix(dec.text, todo...) { |
| 77 |
return true |
| 78 |
} |
| 79 |
// GNU objdump has incorrect alias conditions for following instructions |
| 80 |
if inst.Enc&0x000003ff == 0x000003ff && hasPrefix(dec.text, "negs") && hasPrefix(text, "cmp") { |
| 81 |
return true |
| 82 |
} |
| 83 |
// GNU objdump "NV" is equal to our "AL" |
| 84 |
if strings.HasSuffix(dec.text, " nv") && strings.HasSuffix(text, " al") { |
| 85 |
return true |
| 86 |
} |
| 87 |
if strings.HasPrefix(dec.text, "b.nv") && strings.HasPrefix(text, "b.al") { |
| 88 |
return true |
| 89 |
} |
| 90 |
// GNU objdump recognizes invalid binaries as following instructions |
| 91 |
if hasPrefix(dec.text, "hint", "mrs", "msr", "bfc", "orr", "mov") { |
| 92 |
return true |
| 93 |
} |
| 94 |
if strings.HasPrefix(text, "hint") { |
| 95 |
return true |
| 96 |
} |
| 97 |
// GNU objdump recognizes reserved valuse as valid ones |
| 98 |
if strings.Contains(text, "unknown instruction") && hasPrefix(dec.text, "fmla", "fmul", "fmulx", "fcvtzs", "fcvtzu", "fmls", "fmov", "scvtf", "ucvtf") { |
| 99 |
return true |
| 100 |
} |
| 101 |
// Some old objdump recognizes ldur*/stur*/prfum as ldr*/str*/prfm |
| 102 |
for k, v := range oldObjdumpMismatch { |
| 103 |
if strings.HasPrefix(dec.text, k) && strings.Replace(dec.text, k, v, 1) == text { |
| 104 |
return true |
| 105 |
} |
| 106 |
} |
| 107 |
// New objdump supports some newer mnemonics than this package. This |
| 108 |
// package should be updated to support the new mnemonics and the sense |
| 109 |
// of this reversed to continue passing with older objdumps but that |
| 110 |
// requires internal ARM tooling. |
| 111 |
if newForm, ok := newMnemonics[text]; ok && newForm == dec.text { |
| 112 |
return true |
| 113 |
} |
| 114 |
slcMismatches := map[string]string{ |
| 115 |
"pldslckeep": "#0x06", |
| 116 |
"plislckeep": "#0x0e", |
| 117 |
"pstslckeep": "#0x16", |
| 118 |
"pldslcstrm": "#0x07", |
| 119 |
"plislcstrm": "#0x0f", |
| 120 |
"pstslcstrm": "#0x17", |
| 121 |
} |
| 122 |
decTextNormalized := dec.text |
| 123 |
for k, v := range slcMismatches { |
| 124 |
decTextNormalized = strings.Replace(decTextNormalized, k, v, -1) |
| 125 |
} |
| 126 |
|
| 127 |
// GNU objdump misses spaces between operands for some instructions (e.g., "ld1 {v10.2s, v11.2s}, [x23],#16") |
| 128 |
if strings.Replace(text, " ", "", -1) == strings.Replace(decTextNormalized, " ", "", -1) { |
| 129 |
return true |
| 130 |
} |
| 131 |
|
| 132 |
// GNU objdump uses ranges for register lists (e.g., "{v12.8h-v13.8h}" vs "{v12.8h, v13.8h}") |
| 133 |
if strings.Contains(dec.text, "{") && strings.Contains(dec.text, "}") && strings.Contains(text, "{") { |
| 134 |
i1 := strings.Index(text, "{") |
| 135 |
j1 := strings.Index(text, "}") |
| 136 |
i2 := strings.Index(decTextNormalized, "{") |
| 137 |
j2 := strings.Index(decTextNormalized, "}") |
| 138 |
if i1 >= 0 && j1 > i1 && i2 >= 0 && j2 > i2 { |
| 139 |
list1 := strings.Replace(text[i1+1:j1], " ", "", -1) |
| 140 |
list2 := strings.Replace(decTextNormalized[i2+1:j2], " ", "", -1) |
| 141 |
rest1 := strings.Replace(text[:i1], " ", "", -1) + strings.Replace(text[j1+1:], " ", "", -1) |
| 142 |
rest2 := strings.Replace(decTextNormalized[:i2], " ", "", -1) + strings.Replace(decTextNormalized[j2+1:], " ", "", -1) |
| 143 |
if rest1 == rest2 { |
| 144 |
// The register list is the only differing part. |
| 145 |
p1 := strings.Split(list1, ",") // Our list (e.g., "v31.8b, v0.8b") |
| 146 |
p2 := strings.FieldsFunc(list2, func(r rune) bool { return r == ',' || r == '-' }) // Their list (e.g., "v31.8b-v0.8b") |
| 147 |
// Check if the sequence of registers in the list matches the range. |
| 148 |
if len(p1) > 0 && len(p2) > 0 { |
| 149 |
// parseReg converts a register name like "v31.8b" into its number (31) and arrangement ("8b"). |
| 150 |
parseReg := func(s string) (int, string, bool) { |
| 151 |
if len(s) > 1 && (s[0] == 'v') { |
| 152 |
s = s[1:] |
| 153 |
} |
| 154 |
parts := strings.Split(s, ".") |
| 155 |
if len(parts) == 0 { |
| 156 |
return 0, "", false |
| 157 |
} |
| 158 |
num, err := strconv.Atoi(parts[0]) |
| 159 |
if err != nil { |
| 160 |
return 0, "", false |
| 161 |
} |
| 162 |
arr := "" |
| 163 |
if len(parts) > 1 { |
| 164 |
arr = parts[1] |
| 165 |
} |
| 166 |
return num, arr, true |
| 167 |
} |
| 168 |
|
| 169 |
startNum, startArr, ok1 := parseReg(p2[0]) |
| 170 |
endNum, _, ok2 := parseReg(p2[len(p2)-1]) |
| 171 |
|
| 172 |
if ok1 && ok2 { |
| 173 |
// Calculate expected count in a ring buffer wrapping modulo 32. |
| 174 |
count := (endNum-startNum+32)%32 + 1 |
| 175 |
|
| 176 |
if len(p1) == count { |
| 177 |
match := true |
| 178 |
for i, regStr := range p1 { |
| 179 |
regNum, regArr, ok := parseReg(regStr) |
| 180 |
if !ok || regNum != (startNum+i)%32 || regArr != startArr { |
| 181 |
match = false |
| 182 |
break |
| 183 |
} |
| 184 |
} |
| 185 |
if match { |
| 186 |
return true |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
return false |
| 195 |
} |
| 196 |
|
| 197 |
// TODO: system instruction. |
| 198 |
var todo = strings.Fields(` |
| 199 |
sys |
| 200 |
at |
| 201 |
ic |
| 202 |
hvc |
| 203 |
smc |
| 204 |
`) |
| 205 |
|
| 206 |
// Following instructions can't be covered because they are just aliases to another instructions which are always preferred |
| 207 |
var Ncover = strings.Fields(` |
| 208 |
sbfm |
| 209 |
asrv |
| 210 |
bfm |
| 211 |
ubfm |
| 212 |
lslv |
| 213 |
lsrv |
| 214 |
rorv |
| 215 |
ins |
| 216 |
dup |
| 217 |
`) |
| 218 |
|
| 219 |
// Some old objdump wrongly decodes following instructions and allow their mismatches to avoid false alarm |
| 220 |
var oldObjdumpMismatch = map[string]string{ |
| 221 |
//oldObjValue correctValue |
| 222 |
"ldr": "ldur", |
| 223 |
"ldrb": "ldurb", |
| 224 |
"ldrh": "ldurh", |
| 225 |
"ldrsb": "ldursb", |
| 226 |
"ldrsh": "ldursh", |
| 227 |
"ldrsw": "ldursw", |
| 228 |
"str": "stur", |
| 229 |
"strb": "sturb", |
| 230 |
"strh": "sturh", |
| 231 |
"prfm": "prfum", |
| 232 |
} |
| 233 |
|
| 234 |
var newMnemonics = map[string]string{ |
| 235 |
"dsb #0x00": "ssbb", |
| 236 |
"dsb #0x04": "pssbb", |
| 237 |
} |