Skip to content

Keep ParparVM integer division from trapping on x86 - #6025

Merged
shai-almog merged 1 commit into
masterfrom
fix/parparvm-safe-int-div
Oct 16, 2026
Merged

shai-almog merged 1 commit into
masterfrom
fix/parparvm-safe-int-div

Conversation

@shai-almog

Copy link
Copy Markdown
Collaborator

Problem

ParparVM translated idiv/irem/ldiv/lrem into a raw C / or %. In C both are undefined for a zero divisor and for MIN_VALUE / -1 (the quotient does not fit), and -fwrapv only covers + - *. What happens is up to the CPU:

arm64 (sdiv) x86-64 (idiv)
7 / 0, 7 % 0 0, 7 SIGFPE, process dies
MIN / -1, MIN % -1 MIN, 0 SIGFPE, process dies

So native Windows/Linux builds and x86 servers running the backend crash outright, while arm64 keeps going. On the backend that is a remote crash whenever a divisor comes from a request.

Fix

Integer division and remainder go through four static inline helpers in cn1_globals.h (cn1IntDiv, cn1IntRem, cn1LongDiv, cn1LongRem), used by both the stack form (BasicInstruction, the BC_* macros) and the inline-expression form (ArithmeticExpression).

They do not throw ArithmeticException. They answer what arm64 hardware already answers, so iOS devices see no behaviour change and every target now agrees: x/0 == 0, x%0 == x, MIN/-1 == MIN, MIN%-1 == 0.

Cost: one unsigned compare, (uint32_t)b + 1u <= 1u, which is true only for 0 and -1. From the -O2 assembly:

  • constant divisors (/ 10, % 7) fold the check away entirely, so the code is the same multiply-shift as before;
  • a variable divisor adds one cmp/ja on x86, and on arm64 clang makes it branch-free (sdiv + csel).

Tests

BytecodeInstructionIntegrationTest's fixture gains divisionEdges(): 13 int/long checks in both the expression and the stack form. The divisors are read from heap fields so clang cannot constant-fold them. The test asserts the exact result line (RESULT=9000, a zero failure bitmask) and that the generated C uses the helpers.

  • The full class passes on arm64 (45 tests).
  • The fixture was also built as x86_64 under Rosetta: it passes with this change, and with the helpers reduced to raw / and % it fails with exit 136 (SIGFPE). The Linux CI runners are x86, so the test bites there.
  • SpotBugs on ByteCodeTranslator: 0 findings.

πŸ€– Generated with Claude Code

A translated idiv/irem/ldiv/lrem was a raw C / or %, which is undefined for a
zero divisor and for MIN_VALUE / -1. arm64's sdiv answers both without
trapping; x86's idiv raises SIGFPE and kills the process (native Windows and
Linux, x86 servers running the backend). Route them through static inline
helpers in cn1_globals.h that answer what arm64 already answers -- x/0 == 0,
x%0 == x, MIN/-1 == MIN, MIN%-1 == 0 -- so devices see no change. One unsigned
compare covers both edge divisors and folds away for constant divisors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shai-almog

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
πŸ“ Code Review βœ… Completed 2026-10-16T02:50:54.997630Z 1a8bbff Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with πŸ‘€ while any review is running, comments if it has suggestions, and reacts with πŸ‘ once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 1a8bbffde0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with πŸ‘.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

Copy link
Copy Markdown
Contributor

Cloudflare Preview

@shai-almog

shai-almog commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

Compared 206 screenshots: 206 matched.
βœ… JavaScript-port screenshot tests passed.

@github-actions

Copy link
Copy Markdown
Contributor

βœ… Continuous Quality Report

Test & Coverage

Static Analysis

  • SpotBugs [Report archive]
    • βœ… ByteCodeTranslator: 0 findings (no issues)
    • βœ… android: 0 findings (no issues)
    • βœ… android-compat: 0 findings (no issues)
    • βœ… android-res-compiler: 0 findings (no issues)
    • βœ… backend: 0 findings (no issues)
    • βœ… backend-test: 0 findings (no issues)
    • βœ… build-engine: 0 findings (no issues)
    • βœ… build-hint-catalog: 0 findings (no issues)
    • βœ… build-hint-tools: 0 findings (no issues)
    • βœ… codenameone-gradle-plugin: 0 findings (no issues)
    • βœ… codenameone-maven-plugin: 0 findings (no issues)
    • βœ… core-unittests: 0 findings (no issues)
    • βœ… ios: 0 findings (no issues)
    • βœ… javac: 0 findings (no issues)
    • βœ… project-model: 0 findings (no issues)
  • βœ… PMD: 0 findings (no issues) [Report archive]
  • βœ… Checkstyle: 0 findings (no issues) [Report archive]

Generated automatically by the PR CI workflow.

@shai-almog

shai-almog commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

Compared 185 screenshots: 185 matched.
Native Windows port, REAL shipping pipeline: the hellocodenameone screenshot suite rendered by a binary CROSS-COMPILED on Linux (clang-cl + xwin, WebView2 linked) and RUN on a Windows x64 runner. Compared against the in-repo baseline in scripts/windows/screenshots.

Benchmark Results

Detailed Performance Metrics

Metric Duration
SIMD kernel backend SSE2 (x64) / NEON (arm64) native kernels
SIMD int-add (64K x300) java 63ms / native 6ms = 10.5x speedup
SIMD float-mul (64K x300) java 56ms / native 5ms = 11.2x speedup
SIMD kernel correctness PASS (native result == scalar reference)
Base64 native bridge unavailable (CN1 + SIMD + image benchmarks only)
Base64 payload size 8192 bytes
Base64 benchmark iterations 6000
Base64 SIMD byte path gated to scalar (CPU autovectorizes scalar; explicit SIMD not beneficial here)
Base64 CN1 encode 83.000 ms
Base64 CN1 decode 89.000 ms
Base64 SIMD encode 103.000 ms
Base64 encode ratio (SIMD/CN1) 1.241x (24.1% slower)
Base64 SIMD decode 98.000 ms
Base64 decode ratio (SIMD/CN1) 1.101x (10.1% slower)
Image encode benchmark iterations 100
Image createMask (SIMD off) 7.000 ms
Image createMask (SIMD on) 3.000 ms
Image createMask ratio (SIMD on/off) 0.429x (57.1% faster)
Image applyMask (SIMD off) 23.000 ms
Image applyMask (SIMD on) 25.000 ms
Image applyMask ratio (SIMD on/off) 1.087x (8.7% slower)
Image modifyAlpha (SIMD off) 30.000 ms
Image modifyAlpha (SIMD on) 17.000 ms
Image modifyAlpha ratio (SIMD on/off) 0.567x (43.3% faster)
Image modifyAlpha removeColor (SIMD off) 33.000 ms
Image modifyAlpha removeColor (SIMD on) 20.000 ms
Image modifyAlpha removeColor ratio (SIMD on/off) 0.606x (39.4% faster)

@shai-almog

shai-almog commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

Compared 185 screenshots: 185 matched.
Native Windows port (x64 / Intel-AMD): full hellocodenameone screenshot suite rendered offscreen with Direct2D/DirectWrite, plus the real benchmarks (base64 native/CN1/SIMD, image createMask/applyMask/modifyAlpha/PNG/JPEG, SSE2 SIMD kernels). Compared against the in-repo baseline in scripts/windows/screenshots.

Benchmark Results

Detailed Performance Metrics

Metric Duration
SIMD kernel backend SSE2 (x64) / NEON (arm64) native kernels
SIMD int-add (64K x300) java 84ms / native 4ms = 21.0x speedup
SIMD float-mul (64K x300) java 57ms / native 4ms = 14.2x speedup
SIMD kernel correctness PASS (native result == scalar reference)
Base64 native bridge unavailable (CN1 + SIMD + image benchmarks only)
Base64 payload size 8192 bytes
Base64 benchmark iterations 6000
Base64 SIMD byte path gated to scalar (CPU autovectorizes scalar; explicit SIMD not beneficial here)
Base64 CN1 encode 75.000 ms
Base64 CN1 decode 98.000 ms
Base64 SIMD encode 116.000 ms
Base64 encode ratio (SIMD/CN1) 1.547x (54.7% slower)
Base64 SIMD decode 110.000 ms
Base64 decode ratio (SIMD/CN1) 1.122x (12.2% slower)
Image encode benchmark iterations 100
Image createMask (SIMD off) 11.000 ms
Image createMask (SIMD on) 3.000 ms
Image createMask ratio (SIMD on/off) 0.273x (72.7% faster)
Image applyMask (SIMD off) 19.000 ms
Image applyMask (SIMD on) 23.000 ms
Image applyMask ratio (SIMD on/off) 1.211x (21.1% slower)
Image modifyAlpha (SIMD off) 28.000 ms
Image modifyAlpha (SIMD on) 27.000 ms
Image modifyAlpha ratio (SIMD on/off) 0.964x (3.6% faster)
Image modifyAlpha removeColor (SIMD off) 72.000 ms
Image modifyAlpha removeColor (SIMD on) 16.000 ms
Image modifyAlpha removeColor ratio (SIMD on/off) 0.222x (77.8% faster)

ParparVM vs HotSpot (JDK 25): Windows x64

Runner CPU: AMD64 Family 25 Model 1 Stepping 1, AuthenticAMD (baseline windows-x64@amd64-family-25-model-1-authenticamd)

Ratios are ParparVM / JDK 25: below 1.00x ParparVM is faster (time) or smaller (RAM). Median of 5 interleaved, paired rounds; every run's output was verified. A ratio more than 15% (time) / 15% (RAM) away from its baseline in vm/selfhost/perf-baseline/ fails: above it is a regression, below it an improvement that has to be rebaselined (a row whose calibration runs were noisier carries a wider tolerance), and for RAM the change must also exceed 0.05x in absolute terms. Both run unpinned on all of the runner's CPUs, with their own default thread counts.

Benchmark Cores Time RAM Status
hello (WinHelloMain) 4 1.42x (base 1.22x, +15.9%) 0.82x (base 0.94x, -12.2%) ok
translator (self) 4 0.72x (base 0.60x, +18.6%) 0.55x (base 0.54x, +0.8%) ok
intArithmetic 4 1.20x (base 1.10x, +8.7%) 0.06x (base 0.06x, +4.7%) ok
longArithmetic 4 1.08x (base 1.08x, +0.1%) 0.05x (base 0.05x, +0.9%) ok
mathTranscendental 4 0.77x (base 0.79x, -2.1%) 0.06x (base 0.06x, -4.3%) ok
arraySequential 4 1.53x (base 1.36x, +12.3%) 0.39x (base 0.39x, +0.2%) ok
arrayRandom 4 0.99x (base 1.06x, -6.2%) 0.23x (base 0.23x, +0.0%) ok
objectAllocation 4 1.59x (base 1.56x, +1.8%) 0.29x (base 0.39x, -24.6%) ok
valueEscape 4 0.10x (base 0.10x, +0.2%) 0.04x (base 0.04x, +0.4%) ok
hashMapChurn 4 1.80x (base 1.80x, +0.3%) 0.09x (base 0.09x, +1.5%) ok
stringBuilding 4 1.25x (base 1.22x, +2.5%) 0.32x (base 0.32x, -0.3%) ok
recursion 4 1.41x (base 1.49x, -5.8%) 0.06x (base 0.06x, -1.0%) ok
quicksort 4 1.11x (base 1.13x, -2.4%) 0.12x (base 0.12x, -0.5%) ok

Result: no regression

@shai-almog
shai-almog merged commit aa31e41 into master Oct 16, 2026
@shai-almog
shai-almog deleted the fix/parparvm-safe-int-div branch October 16, 2026 03:57
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.

1 participant