Commit Graph

5 Commits

Author SHA1 Message Date
6455daffee hw_oracle/drm: harden ELF64 bounds, subprocess timeouts, and temp cleanup 2026-11-26 21:50:53 +08:00
b68ce6cccd hw_oracle: remove the temp workdir when the constructor fails (HW-ORACLE-WORKDIR-LEAK)
DrmOracle.__init__ and Oracle.__init__ create a temporary workdir and then
can raise Unavailable (a skipped probe, exit 77). close() is never reached
for an object whose constructor raised, so every skipped run left a
leonida-hw-oracle-* directory in the temp dir.

Remove the directory when the constructor made it and then fails. A
caller-supplied workdir is left alone. tests/test_hw_oracle_workdir_leak.py
covers the early, late and caller-supplied cases with no GPU or libdrm.
2026-11-24 10:53:58 +01:00
634370e59a hw_oracle: run the Linux DRM launcher on GFX11 and GFX12
hw_oracle_drm.c refused every compute engine but version 10, so on an
RX 9070 (gfx1201, engine 12.0) `f32_fuzz.py --oracle hw` could not run
at all: 'hardware oracle unavailable: compute engine 12.0: only version
10 (GFX10, RDNA1 and RDNA2) is supported'.

The registers the launcher writes sit at the same SH offsets on GFX10,
GFX11 and GFX12 — Mesa's register JSON and AMD's PAL offset headers
(gfx9_plus_merged_offset.h, gfx12_merged_offset.h) agree on them, and
PAL's gfx12CmdUtil.cpp still builds plain SET_SH_REG — so only the
preamble and the cu-mask writes branch:

- Engines 11 and 12 take the gfx11 init of libdrm's own dispatch test
  (tests/amdgpu/shader_test_util.c, 2.4.115): REQ_CTRL on its own, the
  four COMPUTE_USER_ACCUM registers, no CP_COHER_START_DELAY. The
  checksum stays in the SH space, as PAL writes it on GFX11
  (gfx9PipelineChunkCs.cpp): libdrm's gfx11 path sends it as UCONFIG
  0x22a, which does not reach the register.
- The indexed COMPUTE_STATIC_THREAD_MGMT_SE0..3 writes stop at engine 11:
  GFX12's SE masks reset to 0xffffffff (AMD's gfx12_merged_default.h) and
  neither PAL nor RADV touches them there.
- arch_name covers the GC 11 and GC 12 families with Mesa's
  ac_identify_chip and amdgpu_asic_addr.h ranges (NV4 also needs the IP
  minor, for gfx1210; amd_family.c names GFX1156 gfx1153 too); a chip
  only labelable by generation gets a representative, which drm.py maps
  the way it already mapped gfx10xx. The engine-version check takes 10,
  11 and 12; the gfx10 stream itself is unchanged.

drm.py's ELF and descriptor parsing needed nothing: the code-object-v5
layout it reads is generation-independent, and COMPUTE_PGM_RSRC2's
LDS_SIZE is bits 15..23 in 512-byte units on all three generations
(Mesa ac_binary.c). asm.py already documents GFX12 (b94733a04).

tests/test_hw_oracle.py: the device-line-to-arch mapping with the new
fallbacks, and a launcher build test that compiles hw_oracle_drm.c
exactly as drm.py does, wherever gcc and the libdrm_amdgpu headers are
installed and without a GPU.

Verified on hardware: the author's RX 9070 (gfx1201, engine 12.0,
Linux/RADV) ran --oracle hw over the five min/max/med3 ops, 583 f32
programs: 451 match, 0 mismatch, 2 known deviation, 0 assembler
rejections; a curated 39-program run before it matched on all 14 judged.
No RDNA3 hardware was dispatched — the gfx11 stream mirrors libdrm's
hw-proven gfx11 test. CPU: tests/test_hw_oracle.py 59 OK (3 skips),
tests/test_f32_fuzz.py 29 OK (1 skip), tests/test_f32_fuzz_gfx12.py
7 OK, compileall clean.

AI-assisted (GLM-5.3).
2026-11-23 19:30:59 +01:00
f23b69060c hw_oracle (Linux): run the LDS fuzzers through libdrm
The libdrm launcher refused a kernel whose descriptor has a group segment
and capped its output at 4096 words, so lds_fuzz --oracle hw was exit 77
on Linux and no LDS program ran on RDNA2 outside Windows. Both are now
gone; docs/shader_validation.md records the gfx1036 counts.

- drm.py: program COMPUTE_PGM_RSRC2.LDS_SIZE (bits 15..23, 512-byte
  units) from the descriptor's group segment instead of refusing it. The
  assembler leaves that field 0 (measured: 4096 bytes of LDS give group
  4096 and rsrc2 0x84), so the launcher fills it as the HIP runtime does.
  Only scratch memory (private) and COMPUTE_PGM_RSRC3 stay unsupported.
- hw_oracle_drm.c: the output buffer takes 1..16384 words, matching the
  Windows launcher, so a 64x64 image_fuzz case (16384 words) fits.
- asm.py: a wrapped (non-raw) kernel's LDS goes into its descriptor too,
  so the Linux launcher has a group segment to read.
- lds_fuzz.py: the "more than 4096 words" wording no longer applies.

Tests: test_hw_oracle.py gains a descriptor unit test (group segment to
LDS_SIZE, 65536 ceiling) and two OnHardware tests (a kernel that writes,
barriers and reads back LDS; 8192 words from 1024 threads), plus a check
that LDS reaches the descriptor and metadata of a wrapped kernel. Verified
on the integrated Radeon of a Ryzen 7 7700X (gfx1036, RDNA2, Linux
7.3.5-zen1-1-zen, libdrm 2.4.134, AMD clang 23.1.5): lds_fuzz --oracle hw
--programs 500 --seed 1 matches 0 of 1000 (wave32+wave64) mismatches,
image_fuzz --oracle hw --programs 120 --seed 1 0 of 120.

AI-assisted (OpenCode).
2026-11-22 23:40:40 +01:00
d3ca882585 Tools: a Linux backend for the hardware oracle through libdrm_amdgpu, and its first runs on RDNA2
tools/hw_oracle ran only on Windows, through the HIP runtime in the AMD
driver, and so far only on gfx1150. On Linux the same kernels now run as
raw compute dispatches through the kernel driver: no ROCm, only clang
with the amdgcn target, gcc and the libdrm headers.

- hw_oracle_drm.c is the launcher. Its command stream is libdrm's own
  compute dispatch test for GFX10 (amdgpu_test_dispatch_memset), with
  what belongs to the kernel put in: the entry point, the group size,
  the kernarg pointer in two user SGPRs, and COMPUTE_PGM_RSRC1,
  COMPUTE_PGM_RSRC2 and the wave size as the kernel's descriptor has
  them. So the .amdhsa_* directives (register counts, float rounding
  and denormal modes, IEEE, DX10 clamp) take effect as under the HIP
  runtime. It prints the same lines as hw_oracle.c. Compute engine
  version 10 only (RDNA1 and RDNA2).
- drm.py is the backend: it assembles with clang, takes .text, the
  kernel offsets and the kernel descriptors out of the object, and runs
  the launcher. A descriptor that asks for what the launcher does not
  provide (LDS, scratch, more user SGPRs) is refused. The render node
  must be named in LEONIDA_HW_DRM_NODE, and a node with a display
  connected is refused unless LEONIDA_HW_DRM_ALLOW_DISPLAY=1.
- runner.open_oracle() picks the backend for the platform. The Windows
  path is unchanged.
- asm.raw_kernel_text puts the GFX10 names of the global memory
  instructions back for gfx10xx. crosslane_fuzz stores with
  global_store_b32, which a GFX10 assembler does not have, so on RDNA2
  every one of its programs was rejected.

One thing the launcher had to learn: while it rewrote one command buffer
in place, the second kernel of every process ran the first kernel's
commands (200 of 200 runs). Every submission now gets command memory of
its own; 24,500 distinct kernels in both wave sizes then each returned
their own words, and a test runs 300 of them.

Results on the integrated GPU of a Ryzen 9 7950X3D (Raphael, gfx1036),
Linux 7.3.5, libdrm 2.4.134, clang 23.1.5:

- salu_diff --oracle hw: 70,000 programs, 69,021 run, 0 mismatches
  (seeds 1-8; seeds 1-4 with --bfe-any; seeds 1-2 with --inline64). The
  s_bfe_i32 rule measured on gfx1150 holds on RDNA2, and s_ff0_i32_b32
  and s_ff0_i32_b64, which gfx1150 lacks, match (2,000 programs).
  --oracle translator,hw: 3,000 programs pass.
- crosslane_fuzz --oracle hw: 32,000 programs, 0 mismatches: seeds 1-6
  (12,000 wave32, 12,000 wave64, 6,586 with the permlane hypothesis)
  and seeds 1-2 with --dpp (8,000, 972 with the DPP hypothesis).
- f32_fuzz --oracle hw, seeds 1-6: 38,858 programs, 28,671 match, 0
  mismatches, 56 known deviations (an output modifier turns -0 into +0,
  as on gfx1150), the rest skipped or not judged. The four MODE settings
  read back as the descriptors set them.
- Controls: a reference with s_min_i32's SCC changed from < to <= gives
  2 mismatches in 5,000 programs, minimized to that instruction;
  crosslane_fuzz --permlane-fetch-inactive mismatches in 239 of 1,000;
  f32_fuzz --negative-control catches all six wrong models.
- No GPU fault, timeout or reset in the kernel log.

Tests: tests/test_hw_oracle.py gains tests of the backend that need no
GPU (reading the object and its descriptors, tokens, the display check,
the platform choice, the GFX10 mnemonics) and two on hardware: 300
distinct kernels, and an f32 rounding mode set in the descriptor that
must change a sum. The HIP-only test skips on Linux.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-22 04:57:17 -05:00