RFR: 8475: Writing events with fields not explicitly set can corrupt the recording [v2]

Jaroslav Bachorik jbachorik at openjdk.org
Wed Dec 3 16:24:51 UTC 2025


> ## Summary
> Fixes an issue where events written with builtin type fields that are not explicitly set could cause field misalignment during JFR recording parsing, leading to corrupted field values.
> 
> ## Problem
> When writing JFR events using the Writer API without explicitly setting values for builtin type fields (byte, char, short, int, long, float, double, boolean, string), the previous implementation in `Chunk.writeBuiltinType()` would skip writing null builtin values (except for strings). This caused field alignment issues during parsing, where subsequent field values would be read at incorrect offsets.
> 
> ## Solution
> Modified `Chunk.writeBuiltinType()` (lines 88-150) to write appropriate default values for null builtin types instead of skipping them:
> - Numeric types (byte, char, short, int, long, float, double): Write `0`
> - Boolean: Write `false`  
> - String: Continue to write null encoding (existing behavior)
> 
> Additionally enhanced `TypedValueImpl.getDefaultImplicitFieldValue()` to provide `System.nanoTime()` as default for `@Timestamp` annotated fields in event types, ensuring valid monotonic timestamps.
> 
> ## Testing
> Added comprehensive test `ImplicitEventFieldsTest.eventWithAllBuiltinFieldsUnset()` that:
> - Creates an event with all 9 builtin type fields
> - Writes the event without setting any builtin field values
> - Includes a final field with an explicit value (99999L) to verify alignment
> - Verifies all default values are correct and the explicit field reads back correctly
> 
> All 277 existing tests pass with the changes.
> 
> ## Files Changed
> - `Chunk.java`: Fixed builtin type serialization to write defaults instead of skipping
> - `TypedValueImpl.java`: Enhanced default value handling for timestamp fields
> - `ImplicitEventFieldsTest.java`: Added comprehensive test for builtin defaults
> - `Type.java`, `TypedValueBuilder.java`: Enhanced documentation
> 
> Fixes #8475
> 
> 🤖 Generated with [Claude Code](https://claude.com/claude-code)

Jaroslav Bachorik has refreshed the contents of this pull request, and previous commits have been removed. The incremental views will show differences compared to the previous content of the PR. The pull request contains one new commit since the last revision:

  8475: Writing events with fields not explicitly set can corrupt the recording

-------------

Changes:
  - all: https://git.openjdk.org/jmc/pull/690/files
  - new: https://git.openjdk.org/jmc/pull/690/files/a3918534..d6f67988

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jmc&pr=690&range=01
 - incr: https://webrevs.openjdk.org/?repo=jmc&pr=690&range=00-01

  Stats: 19 lines in 1 file changed: 10 ins; 0 del; 9 mod
  Patch: https://git.openjdk.org/jmc/pull/690.diff
  Fetch: git fetch https://git.openjdk.org/jmc.git pull/690/head:pull/690

PR: https://git.openjdk.org/jmc/pull/690


More information about the jmc-dev mailing list