Skip to content

Faster VarHandle, Unaligned and AES paths for NoSync - #21

Merged
Project516 merged 8 commits into
masterfrom
p516/jit-longs
Nov 2, 2026
Merged

Project516 merged 8 commits into
masterfrom
p516/jit-longs

Conversation

@Project516

@Project516 Project516 commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Closes #15.

java/security/SecureRandom/NoSync.java spent its time around the SHA and AES work, not in it. This PR does what HotSpot does there:

  • VarHandle call sites. ByteArrayAccess writes each digest word through a byte-array view VarHandle. Every write went through VarHandleGuards.guard_*, checkAccessModeThenIsDirect and VarForm.getMemberName. A compiled call site now keeps the handle it saw last and calls the handle's implementation directly, as HotSpot's folded guard does. The cache is filled only when the guard would link straight to the implementation: a direct handle, the site's erased type matching, and, for an exact handle, its type check already passed. Exact handles matter because ByteArrayAccess uses them.
  • Unsafe unaligned accessors. get/put{Long,Int,Short,Char}Unaligned, with and without the byte-order flag, are intrinsics (as in HotSpot): one DataView access instead of byte-by-byte assembly and an extra convEndian call. Each primitive array keeps its DataView in JArray.x.
  • AES. AESCrypt.implEncryptBlock is an intrinsic, like the SHA ones in crypto.ts. CTR_DRBG only encrypts, so decryption stays as bytecode.

NoSync on a small ARM board, two cores:

time
master 127 s CPU
this branch 81 s wall, 88 s CPU
HotSpot 3.3 s

It took 95 s there through the jtreg runner, beside another test, and 56 s on a CI runner (it was 521 s), so it returns to passing.txt.

Tests, all compared with HotSpot:

  • test/java/VarHandles.java: field, static, array and byte-view handles in compiled code; one call site fed alternating handles; exact handles that pass or throw WrongMethodTypeException at a shared site; out-of-bounds accesses.
  • test/java/Unaligned.java: byte-array views at every alignment, both byte orders, and heap, direct and view buffers.
  • test/java/CryptoMath.java gains AES-128/192/256 in ECB, CBC and CTR, with decryption round trips and an in-place overlapping encrypt.

Verified: pnpm test, pnpm typecheck, the SecureRandom OpenJDK tests through the jtreg runner. CI runs every OpenJDK test, since the JIT changed.

@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 0616bae5-895e-4135-89a5-78456b06a6d1
Commits

Reviewing files that changed from the base of the PR and between 3173a4e and 12ec01e.

Files selected for processing: 8
  • AGENTS.md
  • src/jit.ts
  • src/natives/crypto.ts
  • src/natives/misc.ts
  • test/java/CryptoMath.java
  • test/java/Unaligned.java
  • test/java/VarHandles.java
  • test/jtreg/passing.txt

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Recent review details
Context from checks skipped due to timeout: 6
  • GitHub Check: jtreg / run (3)
  • GitHub Check: jtreg / run (4)
  • GitHub Check: jtreg / run (1)
  • GitHub Check: jtreg / run (2)
  • GitHub Check: CodeRabbit
  • GitHub Check: test
Additional context used
Learnings: 1
Common learnings
Learnt from: CR
Repo: Project516/java-web

Timestamp: 2026-11-02T22:57:46.686Z
Learning: Source excerpt:
# java-web

## Direction

- Prove it with tests: programs whose output must match HotSpot exactly, and
  OpenJDK's own regression tests where they can run.
ast-grep (0.50.0)
test/java/CryptoMath.java

[warning] 46-46: Cipher in ECB mode is detected. ECB mode produces the same output for the same input each time which allows an attacker to intercept and replay the data. Further, ECB mode does not provide any integrity checking. See https://find-sec-bugs.github.io/bugs.htm#CIPHER_INTEGRITY.
Context: Cipher c = Cipher.getInstance("AES/ECB/NoPadding");
Note: [CWE-327] Use of a Broken or Risky Cryptographic Algorithm.

(ecb-cipher-java)

[warning] 46-46: ECB mode leaks plaintext structure; use an authenticated mode such as GCM
Context: Cipher.getInstance("AES/ECB/NoPadding")
Note: [CWE-327] Use of a Broken or Risky Cryptographic Algorithm.

(aes-ecb-insecure)

[warning] 46-46: Use of AES with ECB mode detected. ECB doesn't provide message confidentiality and is not semantically secure so should not be used. Instead, use a strong, secure cipher: Cipher.getInstance("AES/CBC/PKCS7PADDING"). See https://owasp.org/www-community/Using_the_Java_Cryptographic_Extensions for more information.
Context: Cipher.getInstance("AES/ECB/NoPadding")
Note: [CWE-327]: Use of a Broken or Risky Cryptographic Algorithm [OWASP A03:2017]: Sensitive Data Exposure [OWASP A02:2021]: Cryptographic Failures

(use-of-aes-ecb-java)

GitHub Check: CodeQL
test/java/CryptoMath.java

[failure] 38-38: Use of a broken or risky cryptographic algorithm
Cryptographic algorithm AES/ECB/NoPadding is insecure. ECB mode, as in AES/ECB/NoPadding for example, is vulnerable to replay and other attacks. Consider using GCM instead.
Cryptographic algorithm AES/CBC/PKCS5Padding is insecure. CBC mode with PKCS#5 or PKCS#7 padding is vulnerable to padding oracle attacks. Consider using GCM instead.

[failure] 46-46: Use of a broken or risky cryptographic algorithm
Cryptographic algorithm AES/ECB/NoPadding is insecure. ECB mode, as in AES/ECB/NoPadding for example, is vulnerable to replay and other attacks. Consider using GCM instead.

Additional comments: 7
src/natives/crypto.ts: 1

1-4: LGTM!

Also applies to: 15-15, 217-260, 263-265

test/java/CryptoMath.java: 1

7-8: LGTM!

Also applies to: 10-10, 13-13, 34-52

AGENTS.md: 1

52-52: LGTM!

test/jtreg/passing.txt: 1

1714-1714: LGTM!

src/natives/misc.ts: 1

23-29: LGTM!

Also applies to: 32-54, 143-173

src/jit.ts: 1

140-143: LGTM!

Also applies to: 157-176, 1224-1232

test/java/VarHandles.java: 1

1-88: LGTM!

Summary

Summary by CodeRabbit

  • New Features
    • Added AES block encryption support, including compatibility with 128-, 192-, and 256-bit keys.
    • Added support for unaligned primitive reads and writes with native or explicitly specified byte order.
  • Improvements
    • Improved VarHandle call performance for eligible handles while retaining fallback behavior for other cases.

The runtime adds VarHandle call caching, AES block encryption, and unaligned Unsafe access intrinsics. New Java programs exercise AES, VarHandles, and unaligned access. The jtreg passing-test list adds java/security/SecureRandom/NoSync.java.

Changes: Runtime fast paths

Layer / File(s) Summary
VarHandle call caching
src/jit.ts, test/java/VarHandles.java
VarHandle guard calls cache eligible implementations and use direct entry when available, with a run-loop fallback. The new test exercises shared call sites, type mismatches, and bounds checks.
AES block encryption
src/natives/crypto.ts, test/java/CryptoMath.java, AGENTS.md, test/jtreg/passing.txt
The crypto natives register AES block encryption with per-JVM cached tables and key-schedule slots. Tests cover three key sizes, three cipher modes, decryption, and overlapping ranges. The jtreg list adds NoSync.java.
Unaligned primitive access
src/natives/misc.ts, test/java/Unaligned.java
The runtime caches primitive-array DataViews and adds unaligned Unsafe reads and writes with native or explicit byte order. The test covers byte-array views and heap and direct buffers.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant GeneratedVarHandleCall
  participant runtime.vhTarget
  participant VarHandleCache
  participant JITCompiledImplementation
  participant RunLoop
  GeneratedVarHandleCall->>runtime.vhTarget: Populate cache on a miss
  runtime.vhTarget->>VarHandleCache: Store eligible handle and member implementation
  GeneratedVarHandleCall->>VarHandleCache: Check receiver and retrieve implementation
  VarHandleCache-->>GeneratedVarHandleCall: Return cached implementation on a match
  GeneratedVarHandleCall->>JITCompiledImplementation: Directly enter when eligible
  GeneratedVarHandleCall->>RunLoop: Call matching handle when direct entry is unavailable
Loading

Possibly related issues

  • Project516/java-web issue 15: The PR adds AES and JIT/runtime paths and restores the NoSync test entry, which relates to the issue’s NoSync runtime objective.
  • Project516/java-web issue 42: The PR adds runtime fast paths and restores the NoSync test entry, matching the issue’s release-checklist requirement for speed work.

Possibly related PRs

  • Project516/java-web#14: This PR restores the same NoSync test entry after PR #14 removed it due to runtime.
  • Project516/java-web#8: Both PRs address NoSync execution time; PR #8 changes the jtreg timeout, while this PR changes runtime paths used by the test.
  • Project516/java-web#12: PR #12 defines how passing.txt drives jtreg selection, and this PR adds NoSync to that list.

Suggested reviewers: project516-review-bot

Merge Risk: Minimal · up to 12ec0

No blocking defect is demonstrated; non-byte-array coverage would improve confidence.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 45.45% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title names three changed areas: VarHandle call sites, Unsafe unaligned accessors, and AES. The changeset matches each area: jit.ts adds the VarHandle one-entry cache, misc.ts adds the unaligned i…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description directly explains the VarHandle, Unsafe unaligned access, AES intrinsic, test, performance, and validation changes in the pull request.
Full details: Title check

Explanation

The title names three changed areas: VarHandle call sites, Unsafe unaligned accessors, and AES. The changeset matches each area: jit.ts adds the VarHandle one-entry cache, misc.ts adds the unaligned intrinsics, and crypto.ts adds AESCrypt.implEncryptBlock. The title does not name the main goal (speeding up NoSync), but it is not off-topic or vague.

Full details: Docstring Coverage

Explanation

Docstring coverage is 45.45% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 11 functions across 6 files. (2 skipped: 2 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Simplify code
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit taps a byte in place,
Then checks the cipher’s steady pace.
A handle finds its cached way,
While little paws test night and day.
The burrow’s list now lets NoSync play.

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

Comment thread test/java/CryptoMath.java Dismissed
Comment thread test/java/CryptoMath.java Dismissed

@project516-review-bot project516-review-bot Bot 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.

This PR adds three performance optimizations for the NoSync SecureRandom path: (1) VarHandle call site caching in the JIT to avoid repeated guard checks, (2) Unsafe unaligned access intrinsics using DataView for direct memory access instead of byte-by-byte assembly, and (3) an AES encryption intrinsic using T-tables. All three mirror HotSpot's intrinsic strategies. Tests cover VarHandle polymorphism, exact handles, unaligned access at every alignment and byte order, and AES round trips.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict approve

Comment thread src/natives/misc.ts Resolved
Comment thread src/natives/misc.ts Resolved
Comment thread src/natives/crypto.ts Resolved
Comment thread src/jit.ts Resolved

@coderabbitai coderabbitai Bot 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.

  • Fix comments: Fix CodeRabbit comments on this PR

Comment thread test/java/Unaligned.java Resolved

@project516-review-bot project516-review-bot Bot 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.

This PR adds three performance optimizations: VarHandle call site caching in the JIT (vhTarget), Unsafe unaligned access intrinsics using DataView, and an AES encryption intrinsic. The VarHandle cache stores the last handle and its implementation when the guard would link directly. Unaligned accessors use a cached DataView per primitive array for single-instruction reads/writes. AES implEncryptBlock is implemented with T-tables. Tests cover VarHandle polymorphism, unaligned access at all alignments, and AES modes.

Not approved: 4 earlier points are still open in the threads.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict comment

Comment thread src/natives/misc.ts Resolved

@project516-review-bot project516-review-bot Bot 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.

All points from the last review are settled in the threads.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict approve

@project516-review-bot project516-review-bot Bot 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.

This PR adds three JIT/runtime optimizations: (1) VarHandle call-site caching that folds the guard and calls the implementation directly when the handle and AccessDescriptor match, (2) Unsafe unaligned access intrinsics using DataView for single native-order reads/writes instead of byte-by-byte assembly, and (3) an AES encryption intrinsic (implEncryptBlock) using T-tables. The changes are focused on the NoSync SecureRandom path. Tests cover VarHandle polymorphism, exact handles, unaligned access at all alignments/orders, and AES round-trips. The settled points (reference-array bug in unaligned predicate, DataView clone sharing, convEndian shared scratch, VarHandle final-class assumption, AES bounds-check comment) are known and not raised here.

Not approved: 1 earlier point is still open in the threads.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict comment

@project516-review-bot project516-review-bot Bot 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.

All points from the last review are settled in the threads.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict approve

@Project516
Project516 merged commit 03bb1cd into master Nov 2, 2026
12 checks passed
@Project516
Project516 deleted the p516/jit-longs branch November 2, 2026 23:29
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.

Speed up crypto and BigInteger so NoSync and KeyPairGenerator/Failover can return to CI

2 participants