Skip to content

fix: extend J9 VM detection to cover IBM JDK 8 (issue #7956) - #7961

Open
divine-punishment wants to merge 1 commit into
alibaba:mainfrom
divine-punishment:fix/ibm-j9-vm-detection
Open

divine-punishment wants to merge 1 commit into
alibaba:mainfrom
divine-punishment:fix/ibm-j9-vm-detection

Conversation

@divine-punishment

Copy link
Copy Markdown

What this PR does / why we need it?

Fixes #7956.

On IBM JDK 8 (java.vm.name is "IBM J9 VM"), JSON.toJSONString produced date strings with trailing \u0000.

Root cause: JDKUtils detects J9-based VMs with jvmName.contains("OpenJ9"), which only matches Eclipse OpenJ9. Classic IBM JDK 8 reports "IBM J9 VM", so all J9-specific guards were skipped: STRING_CREATOR_JDK8 (built from the HotSpot-internal package-private String(char[], boolean) constructor) was used by DateUtils to build date strings, and JSONWriterUTF16JDK8UF (Unsafe access to String.value) was selected as the writer. These HotSpot internals behave differently on J9-based VMs, yielding strings with trailing \0 — which is why only date fields were affected.

Summary of your change

  • JDKUtils.java: match "J9" in java.vm.name so both "IBM J9 VM" and "Eclipse OpenJ9 VM" are detected
  • JDKUtils.java: gate STRING_CREATOR_JDK8 initialization on !openj9, so IBM JDK 8 falls back to the standard new String(char[]) path
  • Add regression test Issue7956Test covering all built-in date formats and the JSON.toJSONString(map, format) scenario from the issue

No behavioral change on HotSpot — all detection happens at class-init time, outside hot paths. On IBM JDK 8 the Unsafe shortcuts are replaced by the standard constructor (slightly slower, but correct), consistent with the existing Eclipse OpenJ9 handling strategy.

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed. (No user-facing API change, docs not needed)
中文说明

修复内容

Fixes #7956:IBM JDK 8 上 JSON.toJSONString 序列化日期时结果末尾多出 \u0000。

根因:JDKUtils 用 jvmName.contains("OpenJ9") 检测 J9 虚拟机,只能命中 Eclipse OpenJ9;经典 IBM JDK 8 的 java.vm.name 是 "IBM J9 VM",导致所有 J9 防护失效:STRING_CREATOR_JDK8(基于 HotSpot 内部包私有构造器 String(char[], boolean))被 DateUtils 用于构造日期字符串,Writer 也选中了 Unsafe 路径的 JSONWriterUTF16JDK8UF。J9 系虚拟机对这些 HotSpot 内部机制的实现语义不同,导致构造出的字符串带尾部 \0,这也解释了为什么只有日期字段受影响。

修改方式

  • java.vm.name 匹配 "J9",同时覆盖 "IBM J9 VM" 与 "Eclipse OpenJ9 VM"
  • STRING_CREATOR_JDK8 初始化加 !openj9 门槛,IBM JDK 8 回退到标准 new String(char[]) 路径
  • 新增回归测试 Issue7956Test,覆盖全部内建日期格式及 issue 中的 JSON.toJSONString(map, format) 场景

对 HotSpot 用户零影响——检测全部发生在类初始化阶段,不在热路径上;与项目对 Eclipse OpenJ9 的既有处理策略一致。

验证

  • ./mvnw validate — Checkstyle 0 违规
  • ./mvnw -pl core clean test — 2170 个测试类 / 8350 个用例全部通过
  • ./mvnw -pl core clean test -Dfastjson2.creator=reflect — 通过

IBM JDK 8 reports java.vm.name as "IBM J9 VM", which was not matched by
the previous contains("OpenJ9") check. As a result all J9-specific guards
were skipped on IBM JDK 8: the Unsafe string paths were selected and
STRING_CREATOR_JDK8 (built from the HotSpot-internal String(char[], boolean)
constructor) was used to build date strings in DateUtils, producing strings
with trailing '\u0000' (issue alibaba#7956).

- Match "J9" in java.vm.name so both "IBM J9 VM" and "Eclipse OpenJ9 VM"
  are detected
- Gate STRING_CREATOR_JDK8 initialization on !openj9 so IBM JDK 8 falls
  back to the standard new String(char[]) path

No behavioral change on HotSpot; all detection happens at class-init time
outside hot paths.
@CLAassistant

CLAassistant commented Nov 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
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.

[BUG]在使用 ibm jdk 和 fastjson2 2.0.64 版本时,使用 JSON.toJSONString map 日期时,日期结果后面会多\u0000

2 participants