From 7b44bda6732d2db89137e909565e54713d97cbc6 Mon Sep 17 00:00:00 2001 From: Rachel Amber <77208679+Holastor@users.noreply.github.com> Date: Fri, 27 Nov 2026 13:29:58 +0500 Subject: [PATCH] startup: refuse compute buffer loads through a modified descriptor (#2901) Fixes #2901 Refs #2618 When translating compute buffer loads (such as buffer_load_dword ... offen or non-indexed loads), Stage.buffer_address() resolved the resource key solely via self.descriptor(), which looks only at word 0. If earlier scalar instructions edited or cleared words 1-3 (stride, NUM_RECORDS, or format/OOB) without a proven transformation, the load translated anyway instead of refusing. Add Stage.load_descriptor() to validate 4-word descriptor operands against symbol table entries and modification tracking before emitting buffer loads. Resolve self.descriptor() first so existing select and branch join checks take precedence. Like store_descriptor(), allow proven stride-0 and structured-to-raw transformations for non-indexed loads (stride0_ok=not idxen, raw_conversion_ok=not idxen), while refusing other modified descriptors with 'buffer load through a modified descriptor' or 'indexed load through a modified descriptor'. Formatted buffer loads retain their existing specialized validation, preserving the indexed modified-descriptor guard across both idxen and idxen+offen paths. Add tests/startup_gfx2hlsl_load_desc_check.py covering all 6 reproduction variants from #2901 for both loads and stores, proven stride-0 raw loads, proven structured-to-raw conversions, unproven stride and raw conversion edits, indexed formatted loads with modified descriptors in L-uint, and negative control mutants. Register the test in CTest and CI check lists. --- .../startup_gfx2hlsl_load_desc.txt | 1 + tests/cmake/startup_gfx2hlsl_load_desc.cmake | 6 + tests/startup_gfx2hlsl_compute_check.py | 4 + tests/startup_gfx2hlsl_load_desc_check.py | 143 ++++++++++++++++++ tools/startup/gfx2hlsl.py | 49 +++++- 5 files changed, 196 insertions(+), 7 deletions(-) create mode 100644 tests/checks/python-linux/startup_gfx2hlsl_load_desc.txt create mode 100644 tests/cmake/startup_gfx2hlsl_load_desc.cmake create mode 100644 tests/startup_gfx2hlsl_load_desc_check.py diff --git a/tests/checks/python-linux/startup_gfx2hlsl_load_desc.txt b/tests/checks/python-linux/startup_gfx2hlsl_load_desc.txt new file mode 100644 index 0000000000..390b287a5b --- /dev/null +++ b/tests/checks/python-linux/startup_gfx2hlsl_load_desc.txt @@ -0,0 +1 @@ +tests/startup_gfx2hlsl_load_desc_check.py diff --git a/tests/cmake/startup_gfx2hlsl_load_desc.cmake b/tests/cmake/startup_gfx2hlsl_load_desc.cmake new file mode 100644 index 0000000000..de7f6226d5 --- /dev/null +++ b/tests/cmake/startup_gfx2hlsl_load_desc.cmake @@ -0,0 +1,6 @@ +# gfx2hlsl compute buffer load through a modified descriptor (#2901): +# validates that compute buffer loads through a modified or overwritten descriptor word are refused. +if(EXISTS "${INTAKE_PYTHON}") + add_test(NAME startup_gfx2hlsl_load_desc_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_load_desc_check.py") +endif() diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index 3c2a847383..255e872870 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -1505,6 +1505,10 @@ def check_formatted_loads(): assert hlsl.count(' = ((ud3 >> ') == width # Word 3 is overwritten in an otherwise intact native descriptor. refused('words were rewritten', lambda: translate('L-uint', code=prefix + bytes.fromhex('80038fbe') + LOAD_L + end)) + # An instruction writes a derived descriptor word to a separate SGPR (s21), leaving operand s[12:15] intact. + # The indexed formatted load is refused because the descriptor is modified. + refused('indexed load through a modified descriptor', + lambda: translate('L-uint', code=prefix + bytes.fromhex('0dff1587ffff00c0') + LOAD_L + end)) # At a branch target, there is no local descriptor load to prove. refused('local table load', lambda: translate('L-uint', code=prefix + bytes.fromhex('000082bf') + LOAD_L + end)) local_load = bytes.fromhex('000082bf') + prefix + LOAD_L + end diff --git a/tests/startup_gfx2hlsl_load_desc_check.py b/tests/startup_gfx2hlsl_load_desc_check.py new file mode 100644 index 0000000000..597f201589 --- /dev/null +++ b/tests/startup_gfx2hlsl_load_desc_check.py @@ -0,0 +1,143 @@ +"""Verify that compute buffer loads through a modified or overwritten descriptor word are refused. + +Refs #2901, #2618. +""" +from pathlib import Path +import struct +import sys + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) + +import gfx2hlsl as G # noqa: E402 +import startup_gfx2hlsl_compute_check as C # noqa: E402 + + +def sop2(op, d, a, b, lit=None): + return struct.pack(' 105: raise Unsupported('buffer soffset %s' % ops[3]) soffset = self.S(sreg[1]) - idxen, offen = 'idxen' in mods, 'offen' in mods terms = [] if idxen and offen: - if key in self.pipe.modified: + if key in getattr(self.pipe, 'modified', ()): raise Unsupported('indexed load through a modified descriptor') r = reg_range(ops[1]) if not r or r[0] != 'v' or r[2] - r[1] != 1 or r[2] > 255: @@ -6639,7 +6643,7 @@ def buffer_address(self, ops, mods): terms.append('%s * ud%d' % (self.u('v%d' % r[1]), stride)) terms.append(self.u('v%d' % r[2])) elif idxen: - if key in self.pipe.modified: + if key in getattr(self.pipe, 'modified', ()): raise Unsupported('indexed load through a modified descriptor') r = reg_range(ops[1]) if not r or r[0] != 'v' or r[1] != r[2] or r[1] > 255: @@ -6789,6 +6793,37 @@ def op_buffer_store_vs(self, ops, mods, count): self.emit('a = (%s%d) & 0xfffffffcu;' % (soffset, offset) if soffset else 'a = %d;' % offset) self.emit(store if self.exec_known else 'if (exec & 1) %s' % store) + def load_descriptor(self, tok, what, *, stride0_ok=False, raw_conversion_ok=False): + """Resource key for a 4-word buffer load descriptor operand, checking for unproved modifications. + + A descriptor modification is accepted only for proven safe transformations: + - stride0_ok: Allows word 1 (STRIDE[29:16]) to be zeroed via `s_and_b32 ... 0xc000ffff`. + Permitted on non-indexed buffer loads (offen or off) where byte addressing is used and + the hardware stride field is not consulted. + - raw_conversion_ok: Allows the complete RDNA2 structured-to-raw buffer conversion: + word 1 stride zeroed (`stride0`), word 2 scaled by the extracted stride (`byte_count`), + and word 3 OOB_SELECT set to raw mode (`oob3` via 0x30000000). Permitted when the load + is non-indexed and the resulting byte range is validated. + Any other scalar ALU modifications or unproven overwrites raise Unsupported. + """ + key = self.descriptor(tok) + rsrc = reg_range(tok) + first = self.sym.get(rsrc[1]) if rsrc and rsrc[0] == 's' and rsrc[2] - rsrc[1] == 3 else None + if first and first[0] == 'desc': + loaded = tuple(desc_word(first, first[3] + k) for k in range(4)) + actual = tuple(self.sym.get(rsrc[1] + k) for k in range(4)) + valid = names_one_value(first) and actual == loaded + if names_one_value(first) and stride0_ok: + cleared = loaded[1] + ('stride0',) + valid = valid or actual == (loaded[0], cleared, *loaded[2:]) + if not valid and raw_conversion_ok: + valid = actual == (loaded[0], cleared, loaded[2] + ('byte_count',), loaded[3] + ('oob3',)) + if (not valid or key in getattr(self.pipe, 'other_modified', ()) + or (key in getattr(self.pipe, 'modified', ()) and not stride0_ok)): + raise Unsupported('%s through a modified descriptor' % what) + return key + return key + def store_descriptor(self, tok, words, what, stride0_ok=False, raw_conversion_ok=False): """Resource key for a single loaded descriptor or a proven raw transformation of it.""" rsrc = reg_range(tok) @@ -6805,7 +6840,7 @@ def store_descriptor(self, tok, words, what, stride0_ok=False, raw_conversion_ok if not valid and raw_conversion_ok: valid = actual == (loaded[0], cleared, loaded[2] + ('byte_count',), loaded[3] + ('oob3',)) if (not valid or key in getattr(self.pipe, 'other_modified', ()) - or (key in self.pipe.modified and not stride0_ok)): + or (key in getattr(self.pipe, 'modified', ()) and not stride0_ok)): raise Unsupported('%s through a modified descriptor' % what) return key @@ -7199,7 +7234,7 @@ def mubuf_format(self, ops, mods, count): For compute, the caller must validate the native descriptor and reachable byte addresses, and bind the SRV and entry constants from that same descriptor snapshot. """ - buf, address = self.buffer_address(ops, mods) + buf, address = self.buffer_address(ops, mods, is_formatted=True) dst = reg_range(ops[0]) if not dst or dst[0] != 'v' or dst[2] - dst[1] + 1 != count or dst[2] > 255: raise Unsupported('formatted buffer load destination %s for %d components' % (ops[0], count))