"""GPU test of the address guard (rdna2spirv.py --address-guard; docs/d3d12_vulkan.md, "Address guard"). Assembles runtime/test/shaders/cs_address_guard.asm, translates it with --address-guard and runs it with vk_compute_test twice on a 2 MiB heap: once with the guard's header (the page table backs the arena and the page at 0x90000), once without its magic (the guard off). Every address the program uses is inside the heap's buffer, so neither run can fault, whatever the guard does: the guard-on run shows that the refused accesses read 0 and left memory alone although memory was there, and the guard-off run that the same module then reads it. python -I scripts/run_address_guard_gpu_test.py --build runtime/build [--llvm-mc CMD] [--work DIR] Pin the GPU (MESA_VK_DEVICE_SELECT, VK_DRIVER_FILES) as for scripts/run_gpu_tests.py. Exit 0 when both runs give the expected words, 1 otherwise, 77 when vk_compute_test or llvm-mc is missing. """ import argparse import os import shutil import struct import subprocess import sys import tempfile ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "tools")) import address_guard as ag # noqa: E402 NAME = "cs_address_guard" HEAP_BYTES = 2 << 20 LIMIT = ag.RESERVED_END # the "arena": offsets below it pass without a lookup TABLE = 0x100000 # the page table, inside the buffer but past the arena BACKED = [(0x90000, 0x91000)] # the one page outside the arena the table backs SEEDS = {0x90000: 0xA11CE000, 0x90004: 0xA11CE004, 0x90FFC: 0xF00DF00D, 0x91000: 0xF00DF00E, 0xA0000: 0xBADBAD00, 0xA0004: 0xBADBAD04, 0xA0008: 0xBADBAD08} # out[0..7] (cs_address_guard.asm's header) and other words after the run. EXPECTED_ON = [0xA11CE000, 0, 0, 0, 0, 0, 0xA11CE004, 0] EXPECTED_OFF = [0xA11CE000, 0xBADBAD00, 0xBADBAD08, 0xF00DF00D, 0xF00DF00E, 0xBADBAD00, 0xA11CE004, 0xBADBAD00] MEMORY_ON = {0xA0004: 0xBADBAD04, 0xA0008: 0xBADBAD08, # untouched ag.DUMP + 4: 0x5707, ag.DUMP + 8: 1, # where the refused store and atomic went ag.HEADER + 24: 6, # six refused accesses ag.HEADER + 32: 0xA0000, ag.HEADER + 36: 0} # the last one, the s_load at 0xa0000 MEMORY_OFF = {0xA0004: 0x5707, 0xA0008: 0xBADBAD09, ag.DUMP + 4: 0, ag.HEADER + 24: 0} def heap(guard_on): """The heap's bytes: header (without the magic when not `guard_on`), seeds and page table; zero elsewhere.""" words = ag.table_words(BACKED) end = TABLE + 4 * len(words) assert end <= HEAP_BYTES buf = bytearray(end) hdr = bytearray(ag.header(LIMIT, TABLE)) if not guard_on: hdr[0:8] = bytes(8) buf[ag.HEADER:ag.HEADER + len(hdr)] = hdr for at, value in SEEDS.items(): struct.pack_into("