Skip to content

Fix SHA-256 SHA-NI AV on unaligned K256 constants (closes #75)#76

Merged
Xor-el merged 1 commit into
masterfrom
fix/sha256-shani-unaligned-constants
Jun 16, 2026
Merged

Fix SHA-256 SHA-NI AV on unaligned K256 constants (closes #75)#76
Xor-el merged 1 commit into
masterfrom
fix/sha256-shani-unaligned-constants

Conversation

@Xor-el

@Xor-el Xor-el commented Jun 16, 2026

Copy link
Copy Markdown
Owner

The SHA-NI SHA-256 compress routine added the round constants with a legacy-SSE paddd xmm0, [r9 + offset] memory operand. Such operands require 16-byte alignment, but K256 is a plain global const with no alignment guarantee, so a non-aligned layout triggered a #GP / access violation (0xC0000005) on x86-64.

Load each constant quad with movdqu first and add register-to-register (paddd is commutative, so the result is unchanged). This drops the alignment requirement entirely, matching the SSE2/SSSE3/AVX2 backends, and leaves the xmm7 palignr scratch untouched.

The SHA-NI SHA-256 compress routine added the round constants with a
legacy-SSE `paddd xmm0, [r9 + offset]` memory operand. Such operands
require 16-byte alignment, but K256 is a plain global const with no
alignment guarantee, so a non-aligned layout triggered a #GP / access
violation (0xC0000005) on x86-64.

Load each constant quad with `movdqu` first and add register-to-register
(`paddd` is commutative, so the result is unchanged). This drops the
alignment requirement entirely, matching the SSE2/SSSE3/AVX2 backends,
and leaves the xmm7 palignr scratch untouched.
@Xor-el Xor-el merged commit 0c84e27 into master Jun 16, 2026
24 checks passed

@beytullahakyuz beytullahakyuz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it on a Windows x64 system. The bug has been fixed. Thank you.

@Xor-el Xor-el deleted the fix/sha256-shani-unaligned-constants branch June 16, 2026 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Access Violation (GPF) in SHA256 SHA-NI implementation on Windows x64 due to unaligned memory access

2 participants