"""Bounded host interpretation of selected AArch64 integer instructions. This is a host control-flow check, not CPU emulation or guest evidence. System register observations are explicit test inputs. Unknown instructions, reads, writes, branch targets and excessive execution fail closed. """ import re class AdmissionMachine: def __init__(self, instructions, regions, hardware): self.instructions = instructions self.regions = [(start, bytearray(data)) for start, data in regions] self.hardware = hardware self.registers = [0] * 31 self.vectors = [0] * 32 self.sp = 0 self.flags = (False, False, False, False) self.trace = [] self.writes = [] @staticmethod def width(reg): if reg.startswith(('q', 'v')): return 128 return 32 if reg.startswith('w') else 64 def value(self, operand): operand = operand.strip() if operand.startswith('#'): return int(operand[1:], 0) if re.fullmatch('q(?:[12]?[0-9]|3[01])', operand): return self.vectors[int(operand[1:])] if operand in ('xzr', 'wzr'): return 0 if operand in ('sp', 'wsp'): return self.sp & ((1 << self.width(operand)) - 1) assert re.fullmatch('[xw](?:[12]?[0-9]|30)', operand), operand return self.registers[int(operand[1:])] & ((1 << self.width(operand)) - 1) def assign(self, reg, value): value &= (1 << self.width(reg)) - 1 if re.fullmatch('q(?:[12]?[0-9]|3[01])', reg): self.vectors[int(reg[1:])] = value return if reg in ('xzr', 'wzr'): return if reg in ('sp', 'wsp'): self.sp = value else: assert re.fullmatch('[xw](?:[12]?[0-9]|30)', reg), reg self.registers[int(reg[1:])] = value def locate(self, address, size): matches = [(data, address - start) for start, data in self.regions if start <= address and address + size <= start + len(data)] assert len(matches) == 1, ('unmapped or ambiguous access', hex(address), size) return matches[0] def read(self, address, size): data, at = self.locate(address, size) return int.from_bytes(data[at:at + size], 'little') def write(self, address, size, value): data, at = self.locate(address, size) data[at:at + size] = (value & ((1 << (size * 8)) - 1)).to_bytes(size, 'little') self.writes.append((address, size)) def compare(self, left, right, width): mask = (1 << width) - 1 left &= mask right &= mask result = (left - right) & mask self.flags = (bool(result >> (width - 1)), result == 0, left >= right, bool(((left ^ right) & (left ^ result)) >> (width - 1))) def condition(self, name): n, z, c, v = self.flags conditions = {'eq': z, 'ne': not z, 'hs': c, 'cs': c, 'lo': not c, 'cc': not c, 'hi': c and not z, 'ls': not c or z, 'ge': n == v, 'lt': n != v, 'gt': not z and n == v, 'le': z or n != v, 'mi': n, 'pl': not n, 'vs': v, 'vc': not v, 'al': True} assert name in conditions, name return conditions[name] def compare_add(self, left, right, width): mask = (1 << width) - 1 left &= mask right &= mask result = (left + right) & mask self.flags = (bool(result >> (width - 1)), result == 0, left + right > mask, bool(~(left ^ right) & (left ^ result) & (1 << (width - 1)))) def run(self, start, stop, maximum=4096): pc = start for _ in range(maximum): if pc == stop: return assert pc in self.instructions, ('unbound instruction', hex(pc)) mnemonic, arguments = self.instructions[pc] self.trace.append(pc) args = [a.strip() for a in arguments.split(',')] next_pc = pc + 4 if mnemonic == 'mrs': assert args[1].lower() in self.hardware, args[1] self.assign(args[0], self.hardware[args[1].lower()]) elif mnemonic == 'isb': assert not arguments elif mnemonic == 'mov': self.assign(args[0], self.value(args[1])) elif mnemonic == 'adrp': self.assign(args[0], int(args[1], 0)) elif mnemonic in ('add', 'adds', 'sub', 'and', 'orr', 'subs'): assert len(args) in (3, 4), arguments a, b = self.value(args[1]), self.value(args[2]) if len(args) == 4: shift = re.fullmatch(r'lsl #(\d+)', args[3]) assert shift, arguments b <<= int(shift[1]) value = a + b if mnemonic in ('add', 'adds') else a - b if mnemonic in ('sub', 'subs') else a & b if mnemonic == 'and' else a | b if mnemonic == 'subs': self.compare(a, b, self.width(args[0])) elif mnemonic == 'adds': self.compare_add(a, b, self.width(args[0])) self.assign(args[0], value) elif mnemonic in ('lsl', 'lsr'): value, shift = self.value(args[1]), self.value(args[2]) self.assign(args[0], value << shift if mnemonic == 'lsl' else value >> shift) elif mnemonic == 'ubfx': self.assign(args[0], (self.value(args[1]) >> self.value(args[2])) & ((1 << self.value(args[3])) - 1)) elif mnemonic == 'movi': assert args == ['v0.2d', '#0000000000000000'], arguments self.vectors[0] = 0 elif mnemonic == 'madd': self.assign(args[0], self.value(args[1]) * self.value(args[2]) + self.value(args[3])) elif mnemonic == 'cmp': self.compare(self.value(args[0]), self.value(args[1]), self.width(args[0])) elif mnemonic == 'cmn': self.compare_add(self.value(args[0]), self.value(args[1]), self.width(args[0])) elif mnemonic == 'ccmp': if self.condition(args[3]): self.compare(self.value(args[0]), self.value(args[1]), self.width(args[0])) else: bits = self.value(args[2]) assert 0 <= bits < 16 self.flags = tuple(bool(bits & (1 << bit)) for bit in (3, 2, 1, 0)) elif mnemonic == 'cset': self.assign(args[0], int(self.condition(args[1]))) elif mnemonic == 'tst': width = self.width(args[0]) result = (self.value(args[0]) & self.value(args[1])) & ((1 << width) - 1) self.flags = (bool(result >> (width - 1)), result == 0, False, False) elif mnemonic == 'b' or mnemonic.startswith('b.'): if mnemonic == 'b' or self.condition(mnemonic[2:]): next_pc = int(args[0], 0) elif mnemonic in ('cbz', 'cbnz'): if (self.value(args[0]) == 0) == (mnemonic == 'cbz'): next_pc = int(args[1], 0) elif mnemonic in ('tbz', 'tbnz'): clear = not (self.value(args[0]) & (1 << self.value(args[1]))) if clear == (mnemonic == 'tbz'): next_pc = int(args[2], 0) elif mnemonic == 'bl': self.assign('x30', pc + 4) next_pc = int(args[0], 0) elif mnemonic == 'ret': assert not arguments next_pc = self.value('x30') elif mnemonic in ('ldr', 'ldrb', 'ldar', 'str', 'strb', 'strh', 'ldrh', 'ldurb', 'ldur', 'ldp', 'stp'): match = re.fullmatch(r'(.+?),\s*\[([xw]\d+|sp)(?:,\s*(#[^\]]+|[xw]\d+))?\](!)?(?:,\s*(#.+))?', arguments) assert match, (mnemonic, arguments) registers = [r.strip() for r in match[1].split(',')] paired = mnemonic in ('ldp', 'stp') assert len(registers) == (2 if paired else 1) size = 1 if mnemonic.endswith('b') else 2 if mnemonic.endswith('h') else self.width(registers[0]) // 8 base = self.value(match[2]) offset = self.value(match[3]) if match[3] else 0 address = base + offset if match[4]: self.assign(match[2], address) for i, reg in enumerate(registers): if mnemonic.startswith('ld'): self.assign(reg, self.read(address + i * size, size)) else: self.write(address + i * size, size, self.value(reg)) if match[5]: assert not match[4] self.assign(match[2], base + self.value(match[5])) else: raise AssertionError(('unsupported instruction', hex(pc), mnemonic, arguments)) pc = next_pc raise AssertionError('bounded native admission execution exceeded')