Commit e8f9ce0
committed
fix(hpc): validate int8_gemm_amx_tiled slice lengths (codex P1)
Per codex review on PR #185: `int8_gemm_amx_tiled` is a safe public
function (no `unsafe` in the signature) but its inner loop read
`b_i8` via `core::slice::from_raw_parts(b_i8.as_ptr().add(row), 16)`
without any length check. Callers passing mismatched (m, n, k) vs
slice lengths could trigger out-of-bounds reads / UB instead of a
panic. Before PR #185 this logic lived only in
`matmul_i8_to_i32`'s private AMX arm (where the public `pack_contig`
preceded it and bounded everything), but the factored helper is now
reachable from `gemm_u8_i8` and any future caller.
Fix:
1. Add three boundary assertions at function entry matching
`gemm_u8_i8`'s contract:
a_u8.len() >= m * k
b_i8.len() >= k * n
c.len() >= m * n
These panic with descriptive messages on undersized input — the
safety contract is now enforced at the public function boundary,
not at the unsafe pointer-arithmetic site inside the hot loop.
2. Replace the `unsafe { core::slice::from_raw_parts(...) }` B-pack
line with safe `b_tile[..].copy_from_slice(&b_i8[row..row + 16])`.
The bounds-check inside the loop is now redundant given the
function-entry assertions, but the compiler should elide it once
the invariant is proven; either way the code becomes panicking-
safe instead of UB-on-misuse.
3. Update the doc-comment `# Panics` section to list the boundary
panics alongside the existing debug-only AMX / alignment
assertions.
New regression test `amx_tiled_panics_on_undersized_b`:
* Constructs `b: Vec<i8>` half-a-j_tile shorter than the claimed
`k * n`.
* Calls `int8_gemm_amx_tiled` and asserts the expected panic
fires before any unsafe slice arithmetic.
* `#[should_panic(expected = "b_i8.len()")]` catches the exact
assertion message; works on any host (the boundary check fires
before the `debug_assert!(amx_available())` so the test passes
on AMX-less CI runners too).
Verification:
* 2097 lib tests pass (was 2096 — +1 new regression test).
* cargo clippy --lib --tests --features rayon,native -- -D warnings
clean.
* cargo fmt --all --check clean.
The matmul_i8_to_i32 path that delegates to int8_gemm_amx_tiled
inherits the assertions transparently via the call chain. No
behavior change for valid input — only mismatched-shape callers
that would have hit UB now get a clean panic instead.
https://claude.ai/code/session_01HbqooFZHAjaUtFEzhA1R2u1 parent 38d4800 commit e8f9ce0
1 file changed
Lines changed: 40 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
341 | 341 | | |
342 | 342 | | |
343 | 343 | | |
344 | | - | |
345 | | - | |
346 | | - | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
347 | 351 | | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
348 | 360 | | |
349 | 361 | | |
350 | 362 | | |
| |||
354 | 366 | | |
355 | 367 | | |
356 | 368 | | |
357 | | - | |
358 | | - | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
359 | 373 | | |
360 | 374 | | |
361 | | - | |
362 | | - | |
| 375 | + | |
363 | 376 | | |
364 | 377 | | |
365 | 378 | | |
| |||
513 | 526 | | |
514 | 527 | | |
515 | 528 | | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
516 | 549 | | |
517 | 550 | | |
518 | 551 | | |
| |||
0 commit comments