Repository navigation
jsonsimd: require AVX for hashAES, not just AES - #4
Merged
Merged
Conversation
hashAES keeps to 128-bit lanes but is VEX-encoded throughout: it opens with VMOVDQU and runs VAESENC/VPXOR. VEX-encoded instructions need AVX, and the OS has to save YMM state for them to be legal in user space. cpuHasAES only checked CPUID.1.ECX bit 25 (AES). On a CPU that has AES-NI with AVX fused off the check passes and the first VMOVDQU raises SIGILL -- the process dies, restart policy brings it back, and it dies again on the next large payload. That combination is not exotic: Pentium, Celeron and Atom SKUs ship AES-NI with AVX fused off. A Celeron J4125 (Gemini Lake) reports 'aes sse4_1 sse4_2' with no 'avx', and new-api v1.0.0-rc.50 crash-loops on it whenever a relay request carries a JSON body of 128 bytes or more. Bodies under 128 bytes take the maphash short path, so the service looks healthy while every real request kills it. Gate on AES together with OSXSAVE and AVX (CPUID.1.ECX bits 25, 27, 28) and on XGETBV reporting XMM and YMM state. The remaining CPUID reads are left as they are. Under -tags purego the portable maphash path runs, as before.
Calcium-Ion
added a commit
that referenced
this pull request
Nov 13, 2026
The previous change (#4) keeps the VEX-encoded AES kernel, which json uses to hash the bytes it stamps, off x86-64 CPUs that have AES but not AVX, so programs no longer crash there with SIGILL. Those CPUs fell back to hash/maphash, 4 to 8 times slower from 128 bytes on. They now hash with the AES instructions in their legacy SSE encodings: - With AES and AVX, and the XMM and YMM state enabled by the operating system, json runs the VEX-encoded kernel, as before. - With AES alone, a kernel in legacy SSE encodings, which needs only SSE2 and AES, gives the same hashes at about the same speed. - Without AES, json hashes with hash/maphash, as before. Results are the same on every CPU. The other amd64 kernels of base64, json, ojson and regexp already check every instruction set they use before they run: AVX2 with the operating system's support for YMM state, and BMI1, BMI2, POPCNT and PCLMULQDQ where they use them. The arm64 kernels use only the base NEON instructions. Tests: - Each package compares its results with its SIMD kernels and without them, as on CPUs without AVX2 or AVX, and for json also without AES. - Setting MOEGO_TEST_CPU to a list of "avx2", "avx" and "aes" runs all tests of a package as on a CPU without those features. - The ojson tests that called the AVX2 kernels directly now skip on CPUs without AVX2, so the tests pass there. Fixes #3
8 of 13 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
internal/jsonsimd.hashAESkeeps to 128-bit lanes but is VEX-encoded throughout — it opens withVMOVDQUand runsVAESENC/VPXOR. VEX-encoded instructions require AVX, and the OS has to save YMM state for them to be legal in userspace.cpuHasAESonly checkedCPUID.1.ECXbit 25 (AES). On a CPU that has AES-NI with AVX fused off, that check passes and the firstVMOVDQUraisesSIGILL.This patch gates on AES together with OSXSAVE and AVX (
CPUID.1.ECXbits 25, 27, 28) plusXGETBVreporting XMM and YMM state. Everything else is unchanged; under-tags puregothe portablemaphash.Bytespath runs exactly as before.Why it matters
That instruction combination is not exotic. Pentium, Celeron and Atom SKUs ship AES-NI with AVX fused off — they are common as low-power home servers and NAS boxes.
A Celeron J4125 (Gemini Lake) reports:
aesis present,avxis absent. On that machine,new-apiv1.0.0-rc.50 (which pinsmoego v0.1.0-alpha.2) crash-loops:0xc4is the VEX prefix —VMOVDQU xmm0, [rax], illegal without AVX.Why it is easy to miss
HashKey.Sumhas alen(b) < 128short path:So
GET /v1/modelsand the admin/api/*endpoints keep answering — their bodies are short. Only relay requests with a JSON body of 128 bytes or more reachhashAES, and those kill the process. Withrestart: alwaysthe service comes back, passes its health check, and dies again on the next real request. It reads as intermittent 502s rather than a crash loop.Measured on a J4125:
SIGILLTesting
go build ./...andgo vet ./internal/jsonsimd/clean.go test ./internal/jsonsimd/ -run 'Hash|Sum'passes.haveAES=false,FastSum()==false, andSumtakes themaphash.Bytespath instead of faulting.hashAESkernel still runs — the added reads are the sameCPUIDleaf already used bycpuHasAVX2in this package, plusXGETBV.I could not run this on physical J4125 hardware; the confirmation there is the
new-apiside (rc.49 vs rc.50 with identical payloads, table above). AQEMU -cpurun with AVX masked would be a stronger test if a maintainer wants one.Related
QuantumNous/new-api#8314—v1.0.0-rc.50: SIGILL crash loop on x86-64 CPUs with AES-NI but no AVXAn alternative to this patch is to drop VEX encoding from the 128-bit path (plain
MOVDQU+AESENC), which would work on those CPUs rather than falling back. That is a bigger change; this one restores correctness first.