Skip to content

dgemm_kernel_16x2_skylakex.S: stack buffer overflow for K > 341 (wrong results, then crashes) on Clang/Windows AVX-512 builds #6232

Closed

Description

@IsaacGemal

A note from me (the human):

Hello.

Yesterday I was learning about OpenBLAS and pasted some kernel code into Claude to have it explain the codebase, and Claude flagged a likely bug, and was quite insistent about it.

So I went down the rabbit hole and decided to file an issue.

BLAS internals aren't my expertise, so I made a MRE you can verify quickly on the stock MSYS2 package with Claude, which you can find below.

Everything below the line is Claude's (AI) technical write-up. I ran the reproducer and saw the crash on the stock MSYS2 package; the kernel internals are its analysis.


Summary

The DGEMM kernel kernel/x86_64/dgemm_kernel_16x2_skylakex.S copies the B panel into two fixed 8,192-byte stack buffers at 24 bytes per k, so each holds only 341 values of k. The kernel is called with K-chunks up to DGEMM_Q, which is 384 for SkylakeX (param.h) and 512 when the AMD Zen 4/5 override in setparam-ref.c applies. For a kernel K-chunk above 341 the copy overruns its buffer, which first produces silently wrong results and then, at larger K, corrupts the stack frame and crashes.

This .S kernel is selected only for Clang-on-Windows builds (KERNEL.SKYLAKEX), so it is reached by SkylakeX / CooperLake / SapphireRapids DGEMM in those builds. It has been the selected kernel since 0.3.33 (the LLVM-miscompile workaround in 94e053a). gcc and NO_AVX512 builds use the .c kernel and are unaffected.

This is the same class of bug as #6013 / #6021 (the dgemm_kernel_4x8_haswell.S buffer), but in a different kernel that neither #6014 nor #6027 touches.

Reproduction on the stock MSYS2 package

Package: mingw-w64-clang-x86_64-openblas 0.3.34-1 straight from repo.msys2.org (reports OpenBLAS 0.3.34 DYNAMIC_ARCH ... Cooperlake). CPU: AMD Ryzen 7 7800X3D (Zen 4, AVX-512). Single-threaded. A plain square cblas_dgemm, with A all ones so that C[i][j] must equal the column sum sum_k B[k][j] for every row — the whole result matrix is checked, not one element:

Size (M=N=K) Result
128, 341, 342, 384 correct
400 WRONG — 79,200 of 160,000 entries
480 WRONG — 76,800 of 230,400 entries
500, 1000 process dies inside dgemm (no return)

So a stock-install OpenBLAS silently corrupts a 400×400 multiply and crashes on a 500×500 one. (The onset is above the 341 lower bound, and non-monotonic, because which overrun bytes affect the output depends on the data and the stack alignment — the signature of a buffer overflow rather than a clean cutoff. The run above was under Wine 9.0 driving the real Windows DLL via ctypes; native-Windows confirmation of the crash dump is still wanted.)

reproducer (ctypes, runs the prebuilt DLL directly — no compiler needed)
import ctypes, os, sys

here = os.path.dirname(os.path.abspath(__file__))
os.add_dll_directory(here)                       # folder holding libopenblas.dll + its deps
ob = ctypes.CDLL(os.path.join(here, "libopenblas.dll"))

ob.openblas_get_config.restype = ctypes.c_char_p
ob.openblas_get_corename.restype = ctypes.c_char_p
print("config:", ob.openblas_get_config().decode(), "| core:", ob.openblas_get_corename().decode())
ob.openblas_set_num_threads(1)

n = int(sys.argv[1]) if len(sys.argv) > 1 else 1000
dbl = ctypes.c_double
a = (dbl * (n * n))(); b = (dbl * (n * n))(); c = (dbl * (n * n))()
for i in range(n * n):
    a[i] = 1.0
    b[i] = float(i % 7)

ob.cblas_dgemm.restype = None
ob.cblas_dgemm.argtypes = [ctypes.c_int]*6 + [dbl] + \
    [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, dbl, ctypes.c_void_p, ctypes.c_int]

ob.cblas_dgemm(102, 111, 111, n, n, n, 1.0, a, n, b, n, 0.0, c, n)  # CblasColMajor, NoTrans, C = A*B

# A is all ones, so C[i][j] must equal sum_k B[k][j], the same for every row i.
exp = [float(sum((k + j * n) % 7 for k in range(n))) for j in range(n)]
wrong = sum(1 for j in range(n) for i in range(n) if c[j * n + i] != exp[j])
print("K=%-4d -> %s" % (n, "correct" if wrong == 0 else "WRONG (%d/%d)" % (wrong, n * n)))

Run per size, e.g. python repro.py 400 then python repro.py 500, with libopenblas.dll, libomp.dll, libwinpthread-1.dll and libunwind.dll from the package next to the script.

Who is affected

Needs OpenBLAS 0.3.33 or 0.3.34, built with Clang on Windows with AVX-512 kernels enabled, on a CPU that selects the SkylakeX / CooperLake / SapphireRapids table.

Build Affected?
MSYS2 CLANG64 (mingw-w64-clang-x86_64-openblas) Yes (confirmed above). Its mingw64/ucrt64 variants use gcc and are fine.
conda-forge Windows No — built NO_AVX512=1 (see #6013).
NumPy/SciPy scipy-openblas wheels No — MinGW gcc, .c kernel.
Linux, macOS, any gcc build No — .c kernel.
Other clang-cl builds (vcpkg, in-house) Likely, untested.

Mechanism (line numbers at 02f5854)

  • kernel/x86_64/KERNEL.SKYLAKEX:28-34 selects this .S when $(C_COMPILER)$(OSNAME) is CLANGWINNT; CMake takes the same branch (cmake/utils.cmake:46). CooperLake and SapphireRapids include it.
  • In dgemm_kernel_16x2_skylakex.S: L_BUFFER_SIZE 512*8*4 and LB2_OFFSET 512*8*2 (:104), BUFFER1 at rsp+128 and BUFFER2 at rsp+8320 (:114). The 6-column copy writes 3*SIZE = 24 bytes per k (:1714, unrolled :1667), so floor(8192/24) = 341 fit; k=342 already writes 16 bytes past the region. There is no capacity check (:1642, :1706, :1731).
  • Both copies finish before compute (BUFFER2 fill ends :1803, compute starts :1826), so for kernel K > 341 BUFFER1's spilled tail is overwritten by BUFFER2 before it is read → wrong results. At larger K the end of BUFFER2 reaches the saved registers and then (another STACKSIZE higher) the return address → crash. This is the 6-column path (kernel N ≥ 6); narrower N bypasses it.
  • param.h:1741: DGEMM_DEFAULT_Q 384. driver/level3/level3.c:324 passes the whole remainder when it is ≤ Q, so a single kernel chunk can be as large as Q.
  • kernel/setparam-ref.c (the #ifndef NO_AVX512 block, ~:2125): on AMD with L2 = 1024 KB, L3 a multiple of 32 MB and AVX-512F, sets dgemm_q = 512 for the SkylakeX / CooperLake / SapphireRapids tables, which widens the chunk further.

Suggested fixes

  1. Size the two buffers for the largest reachable chunk (2 × 512 × 24 bytes) and extend STACK_TOUCH for the larger Windows frame. Robust regardless of Q. (Preferred.)
  2. Cap the chunk below buffer capacity — but the driver rounds the chunk up to GEMM_UNROLL_M (16), so a naive Q=341 can still yield 352 (remaining K=680 → ceil(340/16)*16 = 352); use an aligned safe value such as 336, or enforce the limit after rounding. Must cover the plain Q=384 case too, not only the override.
  3. Skipping the Zen 4/5 override alone is not enough — it leaves the unsafe default Q=384.

Related

Activity

  1. IsaacGemal commented on Nov 6, 2026

    @IsaacGemal
    Author

    I do want to mention one more thing -- I tried to really drill down on this and polish this issue, but I kept getting caught by both Anthropic and OpenAI safeguards, so I'm unable to make more progress on verifying / testing the bug, etc. I don't want to risk my account getting banned or something. Crazy times.

  2. martin-frbg commented on Nov 6, 2026

    @martin-frbg
    Collaborator

    Thank you - reproduced on Ryzen 9 365 under Windows... guess I should be more careful with fallbacks to older kernels

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions