Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/compiler/test_legacy_a64.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / compiler / test_legacy_a64.c` 551 lines ยท 29265 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * test_legacy_a64.c -- OSC-2 item 4: the legacy AArch64 writer src/aarch64_encoder.c 19 * (docs/osc/OSC-2-DESIGN.md section 4). 20 * OSC-2 slice; not a general Omega compiler; no self-hosting. 21 * 22 * (a) differential: for every emitter, in-range fields (exhaustive where the field 23 * space is <= ~2^24, otherwise seeded random plus every range edge) give the same 24 * word as the pre-OSC-2 masking formula (copied below as the oracle, old_*), and 25 * the word decodes through src/aarch64_decoder.c to the same op and fields (the 26 * fields that decoder reports); 27 * (b) refusal: every field of every emitter, given values just outside its range and 28 * extreme values, is refused: the child process dies with SIGABRT, its stderr is 29 * exactly "A64_LEGACY_FIELD_OUT_OF_RANGE =\n", and the 30 * output buffer and position (shared memory) are untouched. 31 * Usage: test_legacy_a64 [quick] (quick: random samples only, for the ASan leg) 32 * Final line: OSC2_LEGACY_A64_PASS or OSC2_LEGACY_A64_FAIL. 33 */ 34 #include 35 #include 36 #include 37 #include 38 #include 39 #include 40 #include 41 #include 42 #include 43 #include 44 45 #include "aarch64_decoder.h" 46 #include "aarch64_encoder.h" 47 48 static unsigned long n_checks, n_fail, n_words, n_decoded, n_refusal_cases, n_refusal_ok; 49 static int quick; 50 51 static void check(int cond, const char *fmt, ...) { 52 n_checks++; 53 if (cond) return; 54 n_fail++; 55 if (n_fail <= 40) { 56 va_list ap; 57 va_start(ap, fmt); 58 fputs("FAIL: ", stdout); 59 vprintf(fmt, ap); 60 fputc('\n', stdout); 61 va_end(ap); 62 } 63 } 64 65 static unsigned long long rng = 0x0a64c0de5eed0004ULL; 66 static unsigned long long rnd(void) { /* splitmix64 */ 67 unsigned long long z = (rng += 0x9E3779B97F4A7C15ULL); 68 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL; 69 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL; 70 return z ^ (z >> 31); 71 } 72 /* uniform in [lo, hi], with a 1/8 chance of an edge value */ 73 static long long pick(long long lo, long long hi) { 74 unsigned long long r = rnd(); 75 switch (r & 15) { 76 case 0: return lo; 77 case 1: return hi; 78 default: return lo + (long long)((r >> 4) % (unsigned long long)(hi - lo + 1)); 79 } 80 } 81 82 /* ---- oracle: the pre-OSC-2 formulas, verbatim apart from the return ---- */ 83 static uint32_t old_add_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { 84 return ((uint32_t)sf << 31) | (0x0B << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | (rd & 0x1f); 85 } 86 static uint32_t old_sub_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { 87 return ((uint32_t)sf << 31) | (1U << 30) | (0x0B << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | 88 (rd & 0x1f); 89 } 90 static uint32_t old_mul_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { 91 return ((uint32_t)sf << 31) | (0x1B << 24) | ((uint32_t)(rm & 0x1f) << 16) | (0x1f << 10) | ((uint32_t)(rn & 0x1f) << 5) | 92 (rd & 0x1f); 93 } 94 static uint32_t old_and_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { 95 return ((uint32_t)sf << 31) | (0x0A << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | (rd & 0x1f); 96 } 97 static uint32_t old_orr_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { 98 return ((uint32_t)sf << 31) | (0x2A << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | (rd & 0x1f); 99 } 100 static uint32_t old_eor_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { 101 return ((uint32_t)sf << 31) | (0x4A << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | (rd & 0x1f); 102 } 103 static uint32_t old_mov_reg(bool sf, uint8_t rd, uint8_t rm) { return old_orr_reg(sf, rd, REG_XZR, rm); } 104 static uint32_t old_movz(bool sf, uint8_t rd, uint16_t imm16, uint8_t shift) { 105 uint32_t hw = (shift / 16) & 3; 106 return ((uint32_t)sf << 31) | (0x52800000) | (hw << 21) | ((uint32_t)imm16 << 5) | (rd & 0x1f); 107 } 108 static uint32_t old_ret(void) { return 0xD65F03C0; } 109 static uint32_t old_b(int32_t imm26) { return 0x14000000 | ((uint32_t)imm26 & 0x03FFFFFF); } 110 static uint32_t old_b_cond(Aarch64Cond cond, int32_t imm19) { 111 return 0x54000000 | (((uint32_t)imm19 & 0x7FFFF) << 5) | ((uint32_t)cond & 0x0F); 112 } 113 static uint32_t old_cbz(bool sf, uint8_t rt, int32_t imm19) { 114 return ((uint32_t)sf << 31) | 0x34000000 | (((uint32_t)imm19 & 0x7FFFF) << 5) | (rt & 0x1F); 115 } 116 static uint32_t old_cbnz(bool sf, uint8_t rt, int32_t imm19) { 117 return ((uint32_t)sf << 31) | 0x35000000 | (((uint32_t)imm19 & 0x7FFFF) << 5) | (rt & 0x1F); 118 } 119 static uint32_t old_movk(bool sf, uint8_t rd, uint16_t imm16, uint8_t shift) { 120 uint32_t hw = (shift / 16) & 3; 121 return ((uint32_t)sf << 31) | 0x72800000 | (hw << 21) | ((uint32_t)imm16 << 5) | (rd & 0x1f); 122 } 123 static uint32_t old_adr(uint8_t rd, int32_t imm21) { 124 uint32_t immlo = ((uint32_t)imm21 & 3) << 29; 125 uint32_t immhi = (((uint32_t)imm21 >> 2) & 0x7FFFF) << 5; 126 return 0x10000000 | immlo | immhi | (rd & 0x1F); 127 } 128 static uint32_t old_ldr_uoff(bool sf, uint8_t rt, uint8_t rn, uint16_t uoff) { 129 uint32_t scale = sf ? 3 : 2; 130 uint32_t imm12 = ((uint32_t)uoff >> scale) & 0xFFF; 131 uint32_t base = sf ? 0xF9400000 : 0xB9400000; 132 return base | (imm12 << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); 133 } 134 static uint32_t old_str_uoff(bool sf, uint8_t rt, uint8_t rn, uint16_t uoff) { 135 uint32_t scale = sf ? 3 : 2; 136 uint32_t imm12 = ((uint32_t)uoff >> scale) & 0xFFF; 137 uint32_t base = sf ? 0xF9000000 : 0xB9000000; 138 return base | (imm12 << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); 139 } 140 static uint32_t old_ldrb_uoff(uint8_t rt, uint8_t rn, uint16_t uoff) { 141 return 0x39400000 | (((uint32_t)uoff & 0xFFF) << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); 142 } 143 static uint32_t old_strb_uoff(uint8_t rt, uint8_t rn, uint16_t uoff) { 144 return 0x39000000 | (((uint32_t)uoff & 0xFFF) << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); 145 } 146 static uint32_t old_post(uint32_t base, uint8_t rt, uint8_t rn, int16_t simm9) { 147 return base | (((uint32_t)simm9 & 0x1FF) << 12) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); 148 } 149 static uint32_t old_subs_imm(bool sf, uint8_t rd, uint8_t rn, uint16_t imm12) { 150 uint32_t base = sf ? 0xF1000000 : 0x71000000; 151 return base | (((uint32_t)imm12 & 0xFFF) << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rd & 0x1F); 152 } 153 static uint32_t old_subs_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { 154 uint32_t base = sf ? 0xEB000000 : 0x6B000000; 155 return base | ((uint32_t)(rm & 0x1F) << 16) | ((uint32_t)(rn & 0x1F) << 5) | (rd & 0x1F); 156 } 157 158 /* ---- one emitted word ---- */ 159 static uint8_t g_buf[8]; 160 static size_t g_pos; 161 static uint32_t word_of(int rc, const char *what) { 162 check(rc == 0 && g_pos == 4, "%s: emitter returned %d pos %zu", what, rc, g_pos); 163 n_words++; 164 return (uint32_t)g_buf[0] | (uint32_t)g_buf[1] << 8 | (uint32_t)g_buf[2] << 16 | (uint32_t)g_buf[3] << 24; 165 } 166 #define EMIT(call) (g_pos = 0, memset(g_buf, 0, sizeof g_buf), (call)) 167 #define B g_buf, &g_pos, 4 168 169 /* expected decoder fields; -1 = not reported by src/aarch64_decoder.c */ 170 typedef struct { DecodedOp op; int sf, rd, rn, rm, imm16, hw, br, cond; } Want; 171 static Want want(DecodedOp op) { Want w = {op, -1, -1, -1, -1, -1, -1, -1, -1}; return w; } 172 173 static void dcheck(const char *what, uint32_t w, Want e) { 174 DecodedInsn d; 175 int rc = aarch64_decode_instruction(w, &d); 176 n_decoded++; 177 check(rc == 0 && d.op == e.op, "%s: %08x decodes as op %d (rc %d), want %d", what, w, (int)d.op, rc, (int)e.op); 178 if (rc || d.op != e.op) return; 179 check(e.sf < 0 || (int)d.sf == e.sf, "%s: %08x sf %d want %d", what, w, d.sf, e.sf); 180 check(e.rd < 0 || d.rd == e.rd, "%s: %08x rd/rt %u want %d", what, w, d.rd, e.rd); 181 check(e.rn < 0 || d.rn == e.rn, "%s: %08x rn %u want %d", what, w, d.rn, e.rn); 182 check(e.rm < 0 || d.rm == e.rm, "%s: %08x rm %u want %d", what, w, d.rm, e.rm); 183 check(e.imm16 < 0 || d.imm16 == e.imm16, "%s: %08x imm16 %u want %d", what, w, d.imm16, e.imm16); 184 check(e.hw < 0 || d.hw == e.hw, "%s: %08x hw %u want %d", what, w, d.hw, e.hw); 185 check(e.br < 0 || d.branch_imm == e.br, "%s: %08x branch field %d want %d", what, w, d.branch_imm, e.br); 186 check(e.cond < 0 || (int)d.cond == e.cond, "%s: %08x cond %d want %d", what, w, (int)d.cond, e.cond); 187 } 188 189 typedef int (*Emit3)(uint8_t *, size_t *, size_t, bool, uint8_t, uint8_t, uint8_t); 190 typedef uint32_t (*Old3)(bool, uint8_t, uint8_t, uint8_t); 191 192 static void diff_reg3(const char *name, Emit3 f, Old3 o, DecodedOp op) { 193 for (int sf = 0; sf < 2; sf++) 194 for (int rd = 0; rd < 32; rd++) 195 for (int rn = 0; rn < 32; rn++) 196 for (int rm = 0; rm < 32; rm++) { /* exhaustive: 65536 words */ 197 uint32_t w = word_of(EMIT(f(B, sf, rd, rn, rm)), name); 198 check(w == o(sf, rd, rn, rm), "%s sf=%d rd=%d rn=%d rm=%d: %08x != old %08x", name, sf, rd, rn, rm, w, 199 o(sf, rd, rn, rm)); 200 Want e = want(op); 201 e.sf = sf; e.rd = rd; e.rn = rn; e.rm = rm; 202 dcheck(name, w, e); 203 } 204 } 205 206 static void diff_mov_reg(void) { 207 for (int sf = 0; sf < 2; sf++) 208 for (int rd = 0; rd < 32; rd++) 209 for (int rm = 0; rm < 32; rm++) { 210 uint32_t w = word_of(EMIT(aarch64_emit_mov_reg(B, sf, rd, rm)), "mov_reg"); 211 check(w == old_mov_reg(sf, rd, rm), "mov_reg sf=%d rd=%d rm=%d", sf, rd, rm); 212 Want e = want(DECODED_ORR); 213 e.sf = sf; e.rd = rd; e.rn = 31; e.rm = rm; 214 dcheck("mov_reg", w, e); 215 } 216 } 217 218 static void diff_movzk(int k) { 219 const char *name = k ? "movk" : "movz"; 220 for (int sf = 0; sf < 2; sf++) 221 for (int hw = 0; hw < (sf ? 4 : 2); hw++) 222 for (int rd = 0; rd < 32; rd++) { 223 unsigned step = quick ? 4093u : 1u; /* plain: all 65536 immediates */ 224 for (unsigned imm = 0; imm <= 0xFFFFu; imm += step) { 225 uint8_t shift = (uint8_t)(16 * hw); 226 uint32_t w = word_of(EMIT(k ? aarch64_emit_movk(B, sf, rd, (uint16_t)imm, shift) 227 : aarch64_emit_movz(B, sf, rd, (uint16_t)imm, shift)), name); 228 uint32_t ow = k ? old_movk(sf, rd, (uint16_t)imm, shift) : old_movz(sf, rd, (uint16_t)imm, shift); 229 check(w == ow, "%s sf=%d rd=%d imm=%u shift=%u: %08x != old %08x", name, sf, rd, imm, shift, w, ow); 230 Want e = want(k ? DECODED_MOVK : DECODED_MOVZ); 231 e.sf = sf; e.rd = rd; e.imm16 = (int)imm; e.hw = hw; 232 dcheck(name, w, e); 233 } 234 if (quick) { /* the top immediate too */ 235 uint32_t w = word_of(EMIT(k ? aarch64_emit_movk(B, sf, rd, 0xFFFF, (uint8_t)(16 * hw)) 236 : aarch64_emit_movz(B, sf, rd, 0xFFFF, (uint8_t)(16 * hw))), name); 237 check(w == (k ? old_movk(sf, rd, 0xFFFF, (uint8_t)(16 * hw)) : old_movz(sf, rd, 0xFFFF, (uint8_t)(16 * hw))), 238 "%s top imm", name); 239 } 240 } 241 } 242 243 static void diff_ret(void) { 244 uint32_t w = word_of(EMIT(aarch64_emit_ret(B)), "ret"); 245 check(w == old_ret(), "ret %08x", w); 246 Want e = want(DECODED_RET); 247 e.rn = 30; 248 dcheck("ret", w, e); 249 } 250 251 static void diff_b(void) { 252 long n = quick ? 20000 : 400000; 253 for (long i = 0; i < n; i++) { 254 int32_t imm = (int32_t)pick(-(1LL << 25), (1LL << 25) - 1); 255 uint32_t w = word_of(EMIT(aarch64_emit_b(B, imm)), "b"); 256 check(w == old_b(imm), "b imm26=%d: %08x != old %08x", imm, w, old_b(imm)); 257 Want e = want(DECODED_B); 258 e.br = (int)((uint32_t)imm & 0x03FFFFFF); 259 dcheck("b", w, e); 260 } 261 } 262 263 static void diff_b_cond(void) { 264 for (int c = 0; c < 16; c++) { 265 int32_t lo = -(1 << 18), hi = (1 << 18) - 1, step = quick ? 997 : 1; /* plain: exhaustive, 8M words */ 266 for (int32_t imm = lo; imm <= hi; imm += step) { 267 uint32_t w = word_of(EMIT(aarch64_emit_b_cond(B, (Aarch64Cond)c, imm)), "b_cond"); 268 check(w == old_b_cond((Aarch64Cond)c, imm), "b_cond c=%d imm19=%d", c, imm); 269 Want e = want(DECODED_B_COND); 270 e.cond = c; e.br = (int)((uint32_t)imm & 0x7FFFF); 271 dcheck("b_cond", w, e); 272 } 273 uint32_t w = word_of(EMIT(aarch64_emit_b_cond(B, (Aarch64Cond)c, hi)), "b_cond"); 274 check(w == old_b_cond((Aarch64Cond)c, hi), "b_cond c=%d hi", c); 275 } 276 } 277 278 static void diff_cbz(int nz) { 279 const char *name = nz ? "cbnz" : "cbz"; 280 long n = quick ? 20000 : 400000; 281 for (long i = 0; i < n; i++) { 282 int sf = (int)(rnd() & 1); 283 uint8_t rt = (uint8_t)pick(0, 31); 284 int32_t imm = (int32_t)pick(-(1LL << 18), (1LL << 18) - 1); 285 uint32_t w = word_of(EMIT(nz ? aarch64_emit_cbnz(B, sf, rt, imm) : aarch64_emit_cbz(B, sf, rt, imm)), name); 286 uint32_t ow = nz ? old_cbnz(sf, rt, imm) : old_cbz(sf, rt, imm); 287 check(w == ow, "%s sf=%d rt=%u imm19=%d: %08x != old %08x", name, sf, rt, imm, w, ow); 288 Want e = want(nz ? DECODED_CBNZ : DECODED_CBZ); 289 e.sf = sf; e.rd = rt; e.br = (int)((uint32_t)imm & 0x7FFFF); 290 dcheck(name, w, e); 291 } 292 } 293 294 static void diff_adr(void) { 295 long n = quick ? 20000 : 400000; 296 for (long i = 0; i < n; i++) { 297 uint8_t rd = (uint8_t)pick(0, 31); 298 int32_t imm = (int32_t)pick(-(1LL << 20), (1LL << 20) - 1); 299 uint32_t w = word_of(EMIT(aarch64_emit_adr(B, rd, imm)), "adr"); 300 check(w == old_adr(rd, imm), "adr rd=%u imm21=%d: %08x != old %08x", rd, imm, w, old_adr(rd, imm)); 301 /* architectural field check (the decoder reports only rd for ADR) */ 302 int32_t back = (int32_t)(((w >> 29) & 3) | (((w >> 5) & 0x7FFFF) << 2)); 303 back = (back ^ (1 << 20)) - (1 << 20); 304 check(back == imm, "adr imm21 %d reassembles as %d", imm, back); 305 Want e = want(DECODED_ADR); 306 e.rd = rd; 307 dcheck("adr", w, e); 308 } 309 } 310 311 static void diff_uoff(int st) { 312 const char *name = st ? "str_uoff" : "ldr_uoff"; 313 for (int sf = 0; sf < 2; sf++) { 314 unsigned sz = sf ? 8u : 4u; 315 for (int rt = 0; rt < 32; rt++) 316 for (int rn = 0; rn < 32; rn++) 317 for (unsigned k = 0; k <= 0xFFFu; k += quick ? 409u : 1u) { /* plain: exhaustive, 8M words */ 318 uint16_t uoff = (uint16_t)(k * sz); 319 uint32_t w = word_of(EMIT(st ? aarch64_emit_str_uoff(B, sf, rt, rn, uoff) 320 : aarch64_emit_ldr_uoff(B, sf, rt, rn, uoff)), name); 321 uint32_t ow = st ? old_str_uoff(sf, rt, rn, uoff) : old_ldr_uoff(sf, rt, rn, uoff); 322 check(w == ow, "%s sf=%d rt=%d rn=%d uoff=%u: %08x != old %08x", name, sf, rt, rn, uoff, w, ow); 323 check(((w >> 10) & 0xFFF) == k, "%s imm12 field", name); 324 Want e = want(st ? DECODED_STR : DECODED_LDR); 325 e.sf = sf; e.rd = rt; e.rn = rn; 326 dcheck(name, w, e); 327 } 328 } 329 } 330 331 static void diff_byte(int st) { 332 const char *name = st ? "strb_uoff" : "ldrb_uoff"; 333 for (int rt = 0; rt < 32; rt++) 334 for (int rn = 0; rn < 32; rn++) 335 for (unsigned uoff = 0; uoff <= 0xFFFu; uoff += quick ? 409u : 1u) { /* plain: exhaustive, 4M words */ 336 uint32_t w = word_of(EMIT(st ? aarch64_emit_strb_uoff(B, rt, rn, (uint16_t)uoff) 337 : aarch64_emit_ldrb_uoff(B, rt, rn, (uint16_t)uoff)), name); 338 uint32_t ow = st ? old_strb_uoff(rt, rn, (uint16_t)uoff) : old_ldrb_uoff(rt, rn, (uint16_t)uoff); 339 check(w == ow, "%s rt=%d rn=%d uoff=%u: %08x != old %08x", name, rt, rn, uoff, w, ow); 340 Want e = want(st ? DECODED_STRB : DECODED_LDRB); 341 e.rd = rt; e.rn = rn; 342 dcheck(name, w, e); 343 } 344 } 345 346 typedef int (*EmitPost)(uint8_t *, size_t *, size_t, uint8_t, uint8_t, int16_t); 347 static void diff_post(const char *name, EmitPost f, uint32_t base, DecodedOp op, int sf) { 348 for (int rt = 0; rt < 32; rt++) 349 for (int rn = 0; rn < 32; rn++) 350 for (int s = -256; s <= 255; s += quick ? 37 : 1) { /* plain: exhaustive, 512K words */ 351 uint32_t w = word_of(EMIT(f(B, rt, rn, (int16_t)s)), name); 352 uint32_t ow = old_post(base, rt, rn, (int16_t)s); 353 check(w == ow, "%s rt=%d rn=%d simm9=%d: %08x != old %08x", name, rt, rn, s, w, ow); 354 Want e = want(op); 355 e.sf = sf; e.rd = rt; e.rn = rn; 356 dcheck(name, w, e); 357 } 358 } 359 360 static void diff_subs_imm(void) { 361 for (int sf = 0; sf < 2; sf++) 362 for (int rd = 0; rd < 32; rd++) 363 for (int rn = 0; rn < 32; rn++) 364 for (unsigned imm = 0; imm <= 0xFFFu; imm += quick ? 409u : 1u) { /* plain: exhaustive, 8M words */ 365 uint32_t w = word_of(EMIT(aarch64_emit_subs_imm(B, sf, rd, rn, (uint16_t)imm)), "subs_imm"); 366 check(w == old_subs_imm(sf, rd, rn, (uint16_t)imm), "subs_imm sf=%d rd=%d rn=%d imm=%u", sf, rd, rn, imm); 367 Want e = want(DECODED_SUBS); 368 e.sf = sf; e.rd = rd; e.rn = rn; 369 dcheck("subs_imm", w, e); 370 } 371 } 372 373 /* ---- refusal path ---- */ 374 typedef struct { 375 int id; /* which call */ 376 long long a, b, c, d; /* arguments */ 377 const char *expect; /* exact stderr */ 378 } Refusal; 379 380 static void call(const Refusal *r, uint8_t *buf, size_t *pos) { 381 switch (r->id) { 382 #define R3(n, fn) case n: fn(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint8_t)r->c, (uint8_t)r->d); break; 383 R3(0, aarch64_emit_add_reg) R3(1, aarch64_emit_sub_reg) R3(2, aarch64_emit_mul_reg) R3(3, aarch64_emit_and_reg) 384 R3(4, aarch64_emit_orr_reg) R3(5, aarch64_emit_eor_reg) R3(6, aarch64_emit_subs_reg) 385 #undef R3 386 case 7: aarch64_emit_mov_reg(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint8_t)r->c); break; 387 case 8: aarch64_emit_movz(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint16_t)r->c, (uint8_t)r->d); break; 388 case 9: aarch64_emit_movk(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint16_t)r->c, (uint8_t)r->d); break; 389 case 10: aarch64_emit_b(buf, pos, 8, (int32_t)r->a); break; 390 case 11: aarch64_emit_b_cond(buf, pos, 8, (Aarch64Cond)r->a, (int32_t)r->b); break; 391 case 12: aarch64_emit_cbz(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (int32_t)r->c); break; 392 case 13: aarch64_emit_cbnz(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (int32_t)r->c); break; 393 case 14: aarch64_emit_adr(buf, pos, 8, (uint8_t)r->a, (int32_t)r->b); break; 394 case 15: aarch64_emit_ldr_uoff(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint8_t)r->c, (uint16_t)r->d); break; 395 case 16: aarch64_emit_str_uoff(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint8_t)r->c, (uint16_t)r->d); break; 396 case 17: aarch64_emit_ldrb_uoff(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (uint16_t)r->c); break; 397 case 18: aarch64_emit_strb_uoff(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (uint16_t)r->c); break; 398 case 19: aarch64_emit_ldr_post(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (int16_t)r->c); break; 399 case 20: aarch64_emit_str_post(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (int16_t)r->c); break; 400 case 21: aarch64_emit_ldr_x_post(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (int16_t)r->c); break; 401 case 22: aarch64_emit_str_x_post(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (int16_t)r->c); break; 402 case 23: aarch64_emit_subs_imm(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint8_t)r->c, (uint16_t)r->d); break; 403 } 404 } 405 406 static const Refusal refusals[] = { 407 {0, 1, 32, 0, 0, "aarch64_emit_add_reg rd=32"}, {0, 1, 0, 255, 0, "aarch64_emit_add_reg rn=255"}, 408 {0, 0, 0, 0, 33, "aarch64_emit_add_reg rm=33"}, {1, 1, 40, 0, 0, "aarch64_emit_sub_reg rd=40"}, 409 {1, 1, 0, 32, 0, "aarch64_emit_sub_reg rn=32"}, {1, 1, 0, 0, 32, "aarch64_emit_sub_reg rm=32"}, 410 {2, 1, 32, 0, 0, "aarch64_emit_mul_reg rd=32"}, {2, 1, 0, 32, 0, "aarch64_emit_mul_reg rn=32"}, 411 {2, 1, 0, 0, 128, "aarch64_emit_mul_reg rm=128"}, {3, 1, 32, 0, 0, "aarch64_emit_and_reg rd=32"}, 412 {3, 1, 0, 32, 0, "aarch64_emit_and_reg rn=32"}, {3, 1, 0, 0, 32, "aarch64_emit_and_reg rm=32"}, 413 {4, 1, 32, 0, 0, "aarch64_emit_orr_reg rd=32"}, {4, 1, 0, 32, 0, "aarch64_emit_orr_reg rn=32"}, 414 {4, 1, 0, 0, 32, "aarch64_emit_orr_reg rm=32"}, {5, 1, 32, 0, 0, "aarch64_emit_eor_reg rd=32"}, 415 {5, 1, 0, 32, 0, "aarch64_emit_eor_reg rn=32"}, {5, 1, 0, 0, 32, "aarch64_emit_eor_reg rm=32"}, 416 {6, 1, 32, 0, 0, "aarch64_emit_subs_reg rd=32"}, {6, 1, 0, 32, 0, "aarch64_emit_subs_reg rn=32"}, 417 {6, 1, 0, 0, 32, "aarch64_emit_subs_reg rm=32"}, {7, 1, 32, 0, 0, "aarch64_emit_mov_reg rd=32"}, 418 {7, 1, 0, 32, 0, "aarch64_emit_mov_reg rm=32"}, 419 {8, 1, 32, 0, 0, "aarch64_emit_movz rd=32"}, {8, 1, 0, 0, 8, "aarch64_emit_movz shift=8"}, 420 {8, 1, 0, 0, 17, "aarch64_emit_movz shift=17"}, {8, 1, 0, 0, 64, "aarch64_emit_movz shift=64"}, 421 {8, 0, 0, 0, 32, "aarch64_emit_movz shift=32"}, {8, 0, 0, 0, 48, "aarch64_emit_movz shift=48"}, 422 {9, 1, 32, 0, 16, "aarch64_emit_movk rd=32"}, {9, 1, 0, 0, 1, "aarch64_emit_movk shift=1"}, 423 {9, 1, 0, 0, 80, "aarch64_emit_movk shift=80"}, {9, 0, 0, 0, 32, "aarch64_emit_movk shift=32"}, 424 {10, 1LL << 25, 0, 0, 0, "aarch64_emit_b imm26=`33554432`"}, 425 {10, -(1LL << 25) - 1, 0, 0, 0, "aarch64_emit_b imm26=-`33554433`"}, 426 {10, 2147483647LL, 0, 0, 0, "aarch64_emit_b imm26=`2147483647`"}, 427 {10, -2147483647LL - 1, 0, 0, 0, "aarch64_emit_b imm26=-`2147483648`"}, 428 {11, 16, 0, 0, 0, "aarch64_emit_b_cond cond=16"}, {11, -1, 0, 0, 0, "aarch64_emit_b_cond cond=-1"}, 429 {11, 0, 1 << 18, 0, 0, "aarch64_emit_b_cond imm19=262144"}, 430 {11, 1, -(1 << 18) - 1, 0, 0, "aarch64_emit_b_cond imm19=-262145"}, 431 {12, 1, 32, 0, 0, "aarch64_emit_cbz rt=32"}, {12, 1, 0, 1 << 18, 0, "aarch64_emit_cbz imm19=262144"}, 432 {12, 0, 0, -(1 << 18) - 1, 0, "aarch64_emit_cbz imm19=-262145"}, 433 {13, 1, 32, 0, 0, "aarch64_emit_cbnz rt=32"}, {13, 1, 0, 1 << 18, 0, "aarch64_emit_cbnz imm19=262144"}, 434 {13, 0, 0, -(1 << 18) - 1, 0, "aarch64_emit_cbnz imm19=-262145"}, 435 {14, 32, 0, 0, 0, "aarch64_emit_adr rd=32"}, {14, 0, 1 << 20, 0, 0, "aarch64_emit_adr imm21=`1048576`"}, 436 {14, 0, -(1 << 20) - 1, 0, 0, "aarch64_emit_adr imm21=-`1048577`"}, 437 {15, 1, 32, 0, 0, "aarch64_emit_ldr_uoff rt=32"}, {15, 1, 0, 32, 0, "aarch64_emit_ldr_uoff rn=32"}, 438 {15, 1, 0, 0, 4, "aarch64_emit_ldr_uoff uoff=4"}, {15, 1, 0, 0, 32768, "aarch64_emit_ldr_uoff uoff=32768"}, 439 {15, 0, 0, 0, 2, "aarch64_emit_ldr_uoff uoff=2"}, {15, 0, 0, 0, 16384, "aarch64_emit_ldr_uoff uoff=16384"}, 440 {15, 1, 0, 0, 65535, "aarch64_emit_ldr_uoff uoff=65535"}, 441 {16, 1, 32, 0, 0, "aarch64_emit_str_uoff rt=32"}, {16, 1, 0, 32, 0, "aarch64_emit_str_uoff rn=32"}, 442 {16, 1, 0, 0, 12, "aarch64_emit_str_uoff uoff=12"}, {16, 1, 0, 0, 32768, "aarch64_emit_str_uoff uoff=32768"}, 443 {16, 0, 0, 0, 6, "aarch64_emit_str_uoff uoff=6"}, {16, 0, 0, 0, 16384, "aarch64_emit_str_uoff uoff=16384"}, 444 {17, 32, 0, 0, 0, "aarch64_emit_ldrb_uoff rt=32"}, {17, 0, 32, 0, 0, "aarch64_emit_ldrb_uoff rn=32"}, 445 {17, 0, 0, 4096, 0, "aarch64_emit_ldrb_uoff uoff=4096"}, 446 {17, 0, 0, 65535, 0, "aarch64_emit_ldrb_uoff uoff=65535"}, 447 {18, 32, 0, 0, 0, "aarch64_emit_strb_uoff rt=32"}, {18, 0, 32, 0, 0, "aarch64_emit_strb_uoff rn=32"}, 448 {18, 0, 0, 4096, 0, "aarch64_emit_strb_uoff uoff=4096"}, 449 {19, 32, 0, 0, 0, "aarch64_emit_ldr_post rt=32"}, {19, 0, 32, 0, 0, "aarch64_emit_ldr_post rn=32"}, 450 {19, 0, 0, 256, 0, "aarch64_emit_ldr_post simm9=256"}, 451 {19, 0, 0, -257, 0, "aarch64_emit_ldr_post simm9=-257"}, 452 {20, 32, 0, 0, 0, "aarch64_emit_str_post rt=32"}, {20, 0, 32, 0, 0, "aarch64_emit_str_post rn=32"}, 453 {20, 0, 0, 256, 0, "aarch64_emit_str_post simm9=256"}, 454 {20, 0, 0, -32768, 0, "aarch64_emit_str_post simm9=-32768"}, 455 {21, 32, 0, 0, 0, "aarch64_emit_ldr_x_post rt=32"}, {21, 0, 32, 0, 0, "aarch64_emit_ldr_x_post rn=32"}, 456 {21, 0, 0, 512, 0, "aarch64_emit_ldr_x_post simm9=512"}, 457 {21, 0, 0, -257, 0, "aarch64_emit_ldr_x_post simm9=-257"}, 458 {22, 32, 0, 0, 0, "aarch64_emit_str_x_post rt=32"}, {22, 0, 32, 0, 0, "aarch64_emit_str_x_post rn=32"}, 459 {22, 0, 0, 32767, 0, "aarch64_emit_str_x_post simm9=32767"}, 460 {22, 0, 0, -257, 0, "aarch64_emit_str_x_post simm9=-257"}, 461 {23, 1, 32, 0, 0, "aarch64_emit_subs_imm rd=32"}, {23, 1, 0, 32, 0, "aarch64_emit_subs_imm rn=32"}, 462 {23, 1, 0, 0, 4096, "aarch64_emit_subs_imm imm12=4096"}, 463 {23, 0, 0, 0, 65535, "aarch64_emit_subs_imm imm12=65535"}, 464 }; 465 466 typedef struct { uint8_t buf[8]; size_t pos; } Shared; 467 468 static void refusal_case(const Refusal *r, Shared *sh) { 469 char want[160], got[256]; 470 snprintf(want, sizeof want, "A64_LEGACY_FIELD_OUT_OF_RANGE %s\n", r->expect); 471 memset(sh->buf, 0xA5, sizeof sh->buf); 472 sh->pos = 0; 473 int fd[2]; 474 if (pipe(fd) != 0) { check(0, "pipe"); return; } 475 fflush(stdout); 476 pid_t pid = fork(); 477 if (pid == 0) { 478 close(fd[0]); 479 dup2(fd[1], 2); 480 struct rlimit nocore = {0, 0}; 481 setrlimit(RLIMIT_CORE, &nocore); /* the expected abort must not write a core dump */ 482 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); 483 call(r, sh->buf, &sh->pos); 484 _exit(0); /* reached only if the emitter did not refuse */ 485 } 486 close(fd[1]); 487 size_t n = 0; 488 ssize_t k; 489 while (n < sizeof got - 1 && (k = read(fd[0], got + n, sizeof got - 1 - n)) > 0) n += (size_t)k; 490 got[n] = '\0'; 491 close(fd[0]); 492 int st = 0; 493 waitpid(pid, &st, 0); 494 n_refusal_cases++; 495 check(WIFSIGNALED(st) && WTERMSIG(st) == SIGABRT, "refusal %s: child not aborted (status %d)", r->expect, st); 496 check(strcmp(got, want) == 0, "refusal %s: stderr \"%s\"", r->expect, got); 497 int untouched = sh->pos == 0; 498 for (size_t i = 0; i < sizeof sh->buf; i++) untouched &= sh->buf[i] == 0xA5; 499 check(untouched, "refusal %s: buffer or position written", r->expect); 500 if (WIFSIGNALED(st) && WTERMSIG(st) == SIGABRT && strcmp(got, want) == 0 && untouched) n_refusal_ok++; 501 } 502 503 int main(int argc, char **argv) { 504 quick = argc > 1 && strcmp(argv[1], "quick") == 0; 505 diff_reg3("add_reg", aarch64_emit_add_reg, old_add_reg, DECODED_ADD); 506 diff_reg3("sub_reg", aarch64_emit_sub_reg, old_sub_reg, DECODED_SUB); 507 diff_reg3("mul_reg", aarch64_emit_mul_reg, old_mul_reg, DECODED_MUL); 508 diff_reg3("and_reg", aarch64_emit_and_reg, old_and_reg, DECODED_AND); 509 diff_reg3("orr_reg", aarch64_emit_orr_reg, old_orr_reg, DECODED_ORR); 510 diff_reg3("eor_reg", aarch64_emit_eor_reg, old_eor_reg, DECODED_EOR); 511 diff_reg3("subs_reg", aarch64_emit_subs_reg, old_subs_reg, DECODED_SUBS); 512 diff_mov_reg(); 513 diff_movzk(0); 514 diff_movzk(1); 515 diff_ret(); 516 diff_b(); 517 diff_b_cond(); 518 diff_cbz(0); 519 diff_cbz(1); 520 diff_adr(); 521 diff_uoff(0); 522 diff_uoff(1); 523 diff_byte(0); 524 diff_byte(1); 525 diff_post("ldr_post", aarch64_emit_ldr_post, 0xB8400400, DECODED_LDR, 0); 526 diff_post("str_post", aarch64_emit_str_post, 0xB8000400, DECODED_STR, 0); 527 diff_post("ldr_x_post", aarch64_emit_ldr_x_post, 0xF8400400, DECODED_LDR, 1); 528 diff_post("str_x_post", aarch64_emit_str_x_post, 0xF8000400, DECODED_STR, 1); 529 diff_subs_imm(); 530 unsigned long diff_fail = n_fail; 531 532 /* buffer-full is unchanged: -1, nothing written */ 533 { 534 uint8_t b[4] = {1, 2, 3, 4}; 535 size_t p = 1; 536 check(aarch64_emit_ret(b, &p, 4) == -1 && p == 1 && b[1] == 2, "buffer-full path changed"); 537 } 538 539 Shared *sh = mmap(NULL, sizeof(Shared), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); 540 if (sh == MAP_FAILED) { puts("FAIL: mmap"); puts("OSC2_LEGACY_A64_FAIL"); return 1; } 541 size_t nref = sizeof refusals / sizeof refusals[0]; 542 for (size_t i = 0; i < nref; i++) refusal_case(&refusals[i], sh); 543 munmap(sh, sizeof(Shared)); 544 545 printf("legacy a64 differential: emitters=25 mode=%s words=%lu decoded=%lu mismatches=%lu\n", quick ? "quick" : "full", 546 n_words, n_decoded, diff_fail); 547 printf("legacy a64 refusal: cases=%lu named_abort_and_no_write=%lu\n", n_refusal_cases, n_refusal_ok); 548 printf("checks=%lu failures=%lu\n", n_checks, n_fail); 549 puts(n_fail ? "OSC2_LEGACY_A64_FAIL" : "OSC2_LEGACY_A64_PASS"); 550 return n_fail ? 1 : 0; 551 }