Skip to content

jsonsimd: require AVX for hashAES, not just AES - #4

Merged
Calcium-Ion merged 1 commit into
MoeForge:mainfrom
lianxin255:fix/hashAES-requires-avx
Nov 13, 2026
Merged

Calcium-Ion merged 1 commit into
MoeForge:mainfrom
lianxin255:fix/hashAES-requires-avx

Conversation

@lianxin255

Copy link
Copy Markdown
Contributor

Summary

internal/jsonsimd.hashAES keeps to 128-bit lanes but is VEX-encoded throughout — it opens with VMOVDQU and runs VAESENC / VPXOR. VEX-encoded instructions require AVX, and the OS has to save YMM state for them to be legal in userspace.

cpuHasAES only checked CPUID.1.ECX bit 25 (AES). On a CPU that has AES-NI with AVX fused off, that check passes and the first VMOVDQU raises SIGILL.

This patch gates on AES together with OSXSAVE and AVX (CPUID.1.ECX bits 25, 27, 28) plus XGETBV reporting XMM and YMM state. Everything else is unchanged; under -tags purego the portable maphash.Bytes path 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:

flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36
        clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc
        rep_good nopl xtopology cpuid pni pclmulqdq ssse3 cx16 pdcm sse4_1 sse4_2
        movbe popcnt aes xsave osxsave rdrand hypervisor lahf_lm 3dnowprefetch
        fsgsbase sgx

aes is present, avx is absent. On that machine, new-api v1.0.0-rc.50 (which pins moego v0.1.0-alpha.2) crash-loops:

SIGILL: illegal instruction
PC=0x88b854 m=4 sigcode=2
instruction bytes: 0xc4 0xc1 0x7a 0x6f 0x0 0xc4 0xc1 0x7a 0x6f 0x48 0x10 0xc4 0xc1 0x7a 0x6f 0x50

jsonsimd.hashAES(...)                   internal/jsonsimd/hash_amd64.s:30
jsonsimd.(*HashKey).Sum(...)            internal/jsonsimd/hash.go:39
json.(*stampTable).put(...)             json/stamp.go:102
json.(*decodeState).stampInput(...)     json/stamp.go:190
...
common.unmarshalBodyReusable(...)       common/gin.go:153
relay/helper.GetAndValidateTextRequest(...)

0xc4 is the VEX prefix — VMOVDQU xmm0, [rax], illegal without AVX.

Why it is easy to miss

HashKey.Sum has a len(b) < 128 short path:

if !haveAES || len(b) < 128 {
    return maphash.Bytes(h.seed, b)
}

So GET /v1/models and the admin /api/* endpoints keep answering — their bodies are short. Only relay requests with a JSON body of 128 bytes or more reach hashAES, and those kill the process. With restart: always the 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:

request body rc.50 (moego alpha.2) rc.49 (moego alpha.1)
96 KB passes passes
121 KB 5/5 SIGILL 3/3 passes

Testing

  • go build ./... and go vet ./internal/jsonsimd/ clean.
  • go test ./internal/jsonsimd/ -run 'Hash|Sum' passes.
  • On an AVX-less target the detector now reports haveAES=false, FastSum()==false, and Sum takes the maphash.Bytes path instead of faulting.
  • On an AVX-capable amd64 host the hashAES kernel still runs — the added reads are the same CPUID leaf already used by cpuHasAVX2 in this package, plus XGETBV.

I could not run this on physical J4125 hardware; the confirmation there is the new-api side (rc.49 vs rc.50 with identical payloads, table above). A QEMU -cpu run with AVX masked would be a stronger test if a maintainer wants one.

Related

  • Reported downstream: QuantumNous/new-api#8314 — v1.0.0-rc.50: SIGILL crash loop on x86-64 CPUs with AES-NI but no AVX

An 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.

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
@Calcium-Ion
Calcium-Ion merged commit 000bde3 into MoeForge:main Nov 13, 2026
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.

2 participants