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

Jaroslav Bachorik jbachorik at openjdk.org
Wed Dec 3 10:23:46 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)

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

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

Changes: https://git.openjdk.org/jmc/pull/690/files
  Webrev: https://webrevs.openjdk.org/?repo=jmc&pr=690&range=00
  Issue: https://bugs.openjdk.org/browse/JMC-8475
  Stats: 420 lines in 5 files changed: 406 ins; 4 del; 10 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