Skip to content

Pin the CPU baseline of all natives; align branches for the Intel JCC erratum - #187

Merged
yawkat merged 8 commits into
mainfrom
claude/lz4-java-issue-186-8f0565
Nov 16, 2026
Merged

yawkat merged 8 commits into
mainfrom
claude/lz4-java-issue-186-8f0565

Conversation

@yawkat

@yawkat yawkat commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Fixes #186.

Since 1.13.0 the natives are built on Ubuntu 26.04 (ubuntu-latest). On 26.04 both the compiler defaults and the prebuilt libgcc/CRT objects linked into the library target newer CPUs than before, so some 1.13.0/1.14.0 natives crash with SIGILL on older CPUs:

Arch 1.12.0 (Ubuntu 24.04) 1.13.0 (Ubuntu 26.04) This PR
amd64 x86-64 x86-64-v3 (amd64v3 runner images) x86-64
s390x z13 z15 z13
riscv64 rv64gc RVA23 (V, Zba/Zbb/Zbs, Zicond, …) rv64gc
ppc64le POWER9 POWER9 POWER8
aarch64, i386 armv8-a, i686 armv8-a, i686 armv8-a, i686

glibc symbol requirements (at most GLIBC_2.27 on riscv64), macOS minimum versions and Windows DLL imports are unchanged.

Toolchain: Debian stable instead of the runner's Ubuntu

-march alone is not enough, because the toolchain's libgcc and CRT objects end up in the .so. For example, on 26.04 riscv64 calls __bswapdi2/__ctzdi2 from a libgcc built with Zbb (rev8).

The Linux natives are now built on ubuntu-latest inside a digest-pinned debian:trixie container:

  • Debian keeps x86-64, i686, armv8-a, POWER8, z196 and rv64gc as its port baselines, and its libgcc/CRT objects are built for them.
  • Renovate updates the digest.
  • The baseline check below guards Debian release bumps.

Container-specific fixes:

  • bash is set as the default shell, since it isn't the default in containers.
  • Toolchains are installed before checkout.
  • Maven is given setup-java's toolchains.xml explicitly, because the container's $HOME is not Maven's user.home.

Baselines and scripts/check_natives.py

  • The baseline flags now live in the pom.xml native profiles (native.arch.cflags, keyed on arch.id), so the PR test workflow and local builds compile the same code that ships. They come before native.cflags, and -Dnative.arch.cflags= overrides them.

    • Linux: -march=x86-64 -mtune=generic, -march=i686 -mtune=generic, -march=armv8-a, -mcpu=power8, -march=z13, -march=rv64gc -mabi=lp64d.
    • Windows: -march=x86-64 -mtune=generic. MinGW-Builds GCC defaults to nocona, so the 1.12.0 and 1.13.0 DLLs contained SSE3 (movddup).
  • ppc64le moves from POWER9 down to POWER8, which Debian, RHEL 8 and OpenJDK support. 1.12.0 already required POWER9.

  • scripts/check_natives.py runs once in the assemble job on the collected natives, and can also be run on a jar. It replaces the previous inline file-based architecture check. It checks:

    • the expected set of os/arch libraries, each in the right format (ELF/PE/Mach-O, machine, class, endianness);
    • instructions above each baseline:
      • amd64 and Windows: SSE3/SSSE3/SSE4/POPCNT/CX16 (x86-64-v2) and above;
      • i386: any MMX/SSE;
      • macOS x86_64: SSE4.1 and above;
      • aarch64: Armv8.1+ (LSE, RCpc, dot product, non-hint PAC) and SVE;
      • ppc64le: POWER9 and POWER10;
      • s390x: z14 to z16, including the vector enhancements;
      • riscv64: an allowlist of the rv64gc extensions in Tag_RISCV_arch;
    • Linux: only libc/ld.so are needed, and no glibc symbol version newer than 2.17 (2.27 on riscv64);
    • macOS: minimum macOS 10.9 (x86_64) and 11.0 (arm64), and only libSystem is needed;
    • Windows: only system DLLs are imported.

    It uses binutils-multiarch, plus llvm-objdump for Mach-O. Run on the published jars, it flags:

    • 1.13.0: amd64, ppc64le, s390x, riscv64, Windows;
    • 1.12.0: ppc64le, Windows.

JCC erratum alignment

Adds (in the profiles) -Wa,-mbranches-within-32B-boundaries (-mbranches-within-32B-boundaries with clang) for Linux amd64/i386, macOS x86_64 and Windows. Upstream lz4 enables this by default since 1.11.

Measured on an i9-10920X (Cascade Lake), 64 KiB blocks, median of 5 interleaved runs:

GCC -O2 + JCC clang -O3 + JCC
fast compression +10–14% +4–10%
HC9 compression +6–8% +6–8%
decompress_safe +16–20% +14–26%
xxhash ±0 ±0

-O3 made GCC decompression ~10% slower, so:

  • Linux stays at -O2.
  • The Windows profile (MinGW GCC) switches from -O3 to -O2.
  • macOS (clang) keeps -O3, which is faster than -O2 there.

Testing

  • Full release workflow dispatched on this branch, all jobs green, including scripts/check_natives.py in the assemble job (run). Every Linux library reports GCC: (Debian 14.2.0-19).
  • Execution under QEMU user mode: a compress/decompress/xxhash round-trip on the Calgary corpus, comparing the natives from the CI-built jar with the published ones. Each run uses a matching userland (Debian for ppc64le):
Arch QEMU CPU model This PR 1.12.0 1.13.0
amd64 SandyBridge OK OK SIGILL
riscv64 rv64 without V/Zba/Zbb/Zbs/Zbc/Zicond OK OK SIGILL
ppc64le power8 OK SIGILL SIGILL
i386 / aarch64 pentium3 / cortex-a53 OK OK OK

s390x can't be tested this way, because QEMU 8.2's TCG doesn't enforce the z14/z15 facilities. The static check covers it.

Not covered

  • JCC was only benchmarked on one Skylake-family Intel CPU, and macOS/Windows numbers come from the same CPU under Linux. Upstream reports it as neutral on AMD Zen 4/5 and Intel Lunar Lake.
  • macOS and Windows still use the runner's toolchain. Their baselines are pinned by flags and checked, but they're not containerised.

🤖 Generated with Claude Code

yawkat and others added 3 commits November 16, 2026 17:04
The amd64 and i386 natives were built without -march, so the CPU baseline
was whatever the runner's GCC defaults to. Since ubuntu-latest moved to
Ubuntu 26.04, whose GCC defaults to x86-64-v3, liblz4-java.so for amd64
contains AVX2/BMI2/LZCNT/MOVBE instructions and crashes with SIGILL on CPUs
without them.

Set -march=x86-64 (amd64) and -march=i686 (i386) explicitly, and fail the
build if the library contains AVX, BMI or MOVBE instructions.

Fixes #186

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pass -Wa,-mbranches-within-32B-boundaries for amd64 and i386, as upstream
lz4 does by default since 1.11 (JCC_FLAGS / LZ4_JCC_ERRATUM / jcc-erratum
in its Make, CMake and Meson builds). The microcode fix for the erratum
makes branches that cross or end on a 32-byte boundary slow on
Skylake-family Intel CPUs; the assembler pads code to avoid that.

On an i9-10920X (Cascade Lake), with 64 KiB blocks of the Calgary corpus
and of the lz4 source tree: fast compression +10-14%, HC9 compression
+6-8%, decompress_safe +16-20%, xxhash unchanged. Upstream reports no
change on AMD Zen 4/5 and Intel Lunar Lake. -O3 was measured too and made
decompression ~10% slower, so the natives stay at -O2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ubuntu 26.04's cross compilers default to z15 (s390x) and RVA23 (riscv64),
so the 1.13.0 natives for those also require newer CPUs than 1.12.0 did
(z13, rv64gc). Its libgcc and CRT objects are built for the same baselines
and get linked into the library even with -march set (e.g. Zbb rev8 in
__bswapdi2 on riscv64), so build the Linux natives on ubuntu-24.04 and set
the baseline explicitly for each arch at the level 1.12.0 had: armv8-a,
power9, z13, rv64gc.

Extend the baseline check to s390x (z14/z15 mnemonics) and riscv64
(Tag_RISCV_arch), and fix the mnemonic extraction for objdump targets that
put a tab between mnemonic and operands, which hid s390x hits.

Apply the JCC erratum mitigation to macOS x86_64 (clang: +14-26%
decompression, +4-10% compression on Cascade Lake, -O3 kept as it is
faster than -O2 with clang) and Windows, where -O2 replaces the profile's
-O3 as with GCC on Linux. Windows also gets an explicit x86-64 baseline and
the instruction check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yawkat yawkat changed the title Fix x86-64-v3 Linux amd64 natives; align branches for the JCC erratum Pin the CPU baseline of all natives; align branches for the Intel JCC erratum Nov 16, 2026
yawkat and others added 5 commits November 16, 2026 17:36
Building on ubuntu-24.04 only postpones the problem: the toolchain's
compiler defaults, libgcc and CRT objects decide the baseline, and Ubuntu
26.04 raised them. Build in a digest-pinned debian:trixie container on
ubuntu-latest instead. Debian keeps x86-64, i686, armv8-a, POWER8, z196 and
rv64gc, and its libgcc/CRT objects are built for those; Renovate updates the
digest and the baseline check guards release bumps.

Lower ppc64le from POWER9 to POWER8, which Debian, RHEL 8 and OpenJDK
support, and check for POWER9 (ISA 3.0) mnemonics. 1.12.0 and 1.13.0 both
already required POWER9.

Make the baseline check fail if objdump/readelf fail, and use bash, which
is not the default shell in container jobs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The -march/-mcpu baseline and the JCC erratum alignment were only set in the
release workflow, so the PR test workflow and local builds compiled
different code than what ships. Set them per arch.id in the native-linux,
native-macos and native-windows profiles (native.arch.cflags, before
native.cflags, overridable with -Dnative.arch.cflags), and build Windows with
-O2 in the profile instead of overriding its -O3 from CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace the inline per-job baseline checks and the inline architecture
check with scripts/check_natives.py, run once on the collected natives that
go into the jar. Besides the format of each expected library it checks:

- instructions above the baseline of every arch, now including x86-64-v2 and
  SSE3 (amd64, Windows), any MMX/SSE (i386), SSE4.1+ (macOS x86_64), Armv8.1+
  and SVE (aarch64), POWER9/POWER10, z14-z16 vector enhancements, and an
  rv64gc allowlist for Tag_RISCV_arch;
- glibc symbol versions (at most 2.17, 2.27 on riscv64) and DT_NEEDED;
- minimum macOS version and dylibs, imported Windows DLLs.

It can also be run on a jar. On the published jars it flags the 1.13.0
amd64, s390x, riscv64 and ppc64le libraries, the 1.12.0 ppc64le library,
and SSE3 in the 1.12.0 and 1.13.0 Windows DLLs (MinGW-Builds defaults to
nocona).

The Linux container installs the native gcc only for amd64/i386, runs apt
non-interactively and retries downloads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- pom.xml: GCC only takes -Wa,-mbranches-within-32B-boundaries and clang only
  the driver option, so the hard-coded GCC spelling broke clang builds (the
  fuzz profile) on x86, and the macOS spelling GCC builds. Probe both against
  native.cc in each profile like upstream lz4 does, falling back to none.
- check_natives.py: skip the jump tables clang puts into __text on macOS
  x86_64 (found through RIP-relative lea), which were decoded as junk
  instructions; report libraries anywhere under the root, not only at
  <os>/<arch>/; pin the glibc limits to what the libraries need today;
  add --targets and an --expect-failing self-test mode; find a versioned
  llvm-objdump-<N>.
- test.yml: check the native library each test job builds, and self-test the
  script against the published 1.13.0 jar.
- Install llvm in the assemble job only if no llvm-objdump is present.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yawkat
yawkat merged commit 11d4075 into main Nov 16, 2026
13 checks passed
@yawkat
yawkat deleted the claude/lz4-java-issue-186-8f0565 branch November 16, 2026 19:07
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.

linux-amd64 native library in 1.13.0/1.14.0 requires AVX2/BMI2 (x86-64-v3), SIGILL on older CPUs

1 participant