← Back to Pull requests

(#1404) Contain reference and field failures in Kryo 4 #1405

Open
vjanelle opened this pull request with 667 lines diff across 15 files
View on GitHub
vjanelle vjanelle commented on Oct 06, 2026 02:17 UTC
Author

Malformed object references and allocation failures during cached-field operations can escape Kryo 4 as raw exceptions or errors. This change converts those failures to KryoException while retaining their causes and adding reference or field context.

  • Wrap exceptions from reference lookup with the reference ID and target class.

  • Wrap exceptions and OutOfMemoryError from ordinary and serialized transient field reads/writes with the field name, operation, and input/output position.

  • Rethrow existing KryoException instances unchanged and preserve existing read cleanup.
  • This addresses the demonstrated paths associated with Sonatype-2021-1215 and Sonatype-2021-1216. Public APIs, registration behavior, and valid serialized bytes are preserved. This provides exception containment; protection against general memory exhaustion and Sonatype recognition of a patched artifact require separate work.

    The related 5.x history, including the later revert of allocation-error wrapping, is documented in #1404. Kryo 4 remains relevant to downstream users of com.twitter:chill and Apache Spark.

    Validation:

  • 15 new regression tests: malformed map/list references, synthetic allocation failures, ordinary/transient fields, preserved exception identity, unrelated error propagation, and exact valid field bytes. Nine tests expose raw failures before the patch.

  • All 68 targeted field/reference/serialization compatibility tests passed with Java 8.

  • All 21 regression and compatibility tests passed against the shaded artifact.

  • Forty serialization cases across both reference resolvers and five output variants remained byte-for-byte identical; public API snapshots were unchanged.

  • Normal and shaded artifacts built successfully.

  • Eclipse formatter and formatter validation passed with Java 17 using the master branch's profile, scoped to the three changed files. The formatter also adjusted existing formatting in the two source files.
  • Closes #1404.

    Diff: 667 lines (+609 / -58) across 15 files Reviewer: vjanelle Review duration: 15733m
    All Vouch review confidence checks passed
    Residual risk 0.15 = Change Risk (0.76) × (1 − Review Confidence (0.80)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.76 · 667 lines diff across 15 files
    0.76
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.92 · 5 substantive comment(s)
    0.92
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    80%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.389
    Self Review self_review
    0.140
    Real PR Conversation Timeline
    12 items
    Copilot Copilot commented on code on Oct 07, 2026 05:05 UTC
    Code Review
    src/com/esotericsoftware/kryo/serializers/FieldSerializer.java:1
    @@ -556,6 +558,28 @@ public T read (Kryo kryo, Input input, Class<T> type) {
     		}
     	}
     
    +	private void writeField (Output output, T object, CachedField field) {
    +		try {
    +			field.write(output, object);
    +		} catch (KryoException ex) {
    +			throw ex;
    +		} catch (Exception | OutOfMemoryError ex) {
    +			throw new KryoException("Error writing field: " + field.getField().getName() + " (" + type.getName()
    +				+ ") at output position " + output.position(), ex);
    +		}
    +	}
    +
    +	private void readField (Input input, T object, CachedField field) {
    +		try {
    +			field.read(input, object);
    +		} catch (KryoException ex) {
    +			throw ex;
    +		} catch (Exception | OutOfMemoryError ex) {
    +			throw new KryoException("Error reading field: " + field.getField().getName() + " (" + type.getName()
    +				+ ") at input position " + input.position(), ex);
    

    These helpers are only used by the base FieldSerializer loops, but VersionFieldSerializer, TaggedFieldSerializer, and CompatibleFieldSerializer override read/write and invoke CachedField.read/write directly (for example, CompatibleFieldSerializer.java:65 and :145). Consequently the same exception or OutOfMemoryError still escapes raw when any of those public field serializers is used, so the containment is incomplete. Make the wrapper dispatch available to subclasses and use it in their loops, or centralize the cached-field calls before considering this fix complete.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Oct 07, 2026 05:05 UTC
    Reviewer

    <!-- ccr-overview-v2 -->

    Copilot review overview

    🟡 Changes recommended

    Field serializer subclasses bypass the new wrappers, and reference tests do not enable references, leaving containment and coverage incomplete.

    Review effort: Lite
    Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture>

    <details open>
    <summary><strong>Open (1)</strong></summary>

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> [Subclass serializers bypass exception-containment wrappers](#discussion_r4203244986) · New

  • </details>

    <details>
    <summary><strong>What changed in this PR</strong></summary>

    This PR adds Kryo 4 exception containment for invalid references and cached-field failures while preserving causes and valid serialization behavior.

    Changes:

  • Wraps reference lookup and field operation failures with contextual KryoExceptions.

  • Handles allocation errors during field reads and writes.

  • Adds regression and byte-compatibility tests.
  • | File | Summary |
    |---|---|
    | test/​com/​esotericsoftware/​kryo/​FailureContainmentTest.java | Adds reference, field failure, and compatibility tests. |
    | src/​com/​esotericsoftware/​kryo/​serializers/​FieldSerializer.java | Adds contextual field-operation failure wrapping. |
    | src/​com/​esotericsoftware/​kryo/​Kryo.java | Contains reference lookup failures with context. |
    </details>

    ---

    💡 <a href="/EsotericSoftware/kryo/new/kryo-4?filename=.github/skills/code-review/SKILL.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add a code-review agent skill</a> or configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review?tool=webui#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>

    theigl theigl commented on Oct 07, 2026 05:15 UTC
    Contributor

    Hi @vjanelle. I'm not a big fan of releasing Kryo 4, but I have done it once already for an important fix. Can you tell me why you want that change? Because some code scanner complains or because this is a real problem?

    vjanelle vjanelle commented on code on Oct 07, 2026 05:23 UTC
    Code Review
    src/com/esotericsoftware/kryo/Kryo.java:1
    @@ -854,7 +854,13 @@ int readReferenceOrNull (Input input, Class type, boolean mayBeNull) {
     		}
     		// The id is an object reference.
     		id -= 2; // - 2 because 0 and 1 are used for NULL and NOT_NULL.
    -		readObject = referenceResolver.getReadObject(type, id);
    +		try {
    +			readObject = referenceResolver.getReadObject(type, id);
    +		} catch (KryoException ex) {
    +			throw ex;
    +		} catch (Exception ex) {
    

    @theigl this is the main thing I think the advisories are complaining about / the fuzzers are tripping up on.

    vjanelle vjanelle commented on Oct 07, 2026 05:27 UTC
    Author

    @theigl Sonatype-2021-1215 and Sonatype-2021-1216 are about uncaught exceptions (same as what was patched in kryo 5), which could lead to crashing bugs. As for "important", this is not something we currently experience but a potential thing. Sonatype is taking the google ossfuzz reports(I think) and generating their own internal advisories which we then have to make decisions on. The advisories are private, but basically they just say that an exception that handles when Kryo is serialising code would not be caught, and that'll end up terminating the JVM.

    Unfortunately the DoD with FedRAMP is changing their rules come December and a lot of potential issues are getting escalated up. I can't honestly justify this beyond a potential crash bug, but "in the age of AI we can't just assume things" anymore.

    Sorry. >.< Only thing I can do is ask nicely, but I'll try to be following up with the apache spark project to see if we can excise/vendor com.twitter:chill, and/or update to kryo5.

    If it'll help, I can create an issue first in the spark jira about updating kryo. I do have to support spark 3.5 for a while though (which has an extended long term release from the apache spark project), so that would have to go into 4.x.

    theigl theigl commented on Oct 07, 2026 05:33 UTC
    Contributor

    Sonatype-2021-1215 and Sonatype-2021-1216 are about uncaught exceptions (same as what was patched in kryo 5), which could lead to crashing bugs. As for "important", this is not something we currently experience but a potential thing. Sonatype is taking the google ossfuzz reports(I think) and generating their own internal advisories which we then have to make decisions on. The advisories are private, but basically they just say that an exception that handles when Kryo is serialising code would not be caught, and that'll end up terminating the JVM.

    Unfortunately the DoD with FedRAMP is changing their rules come December and a lot of potential issues are getting escalated up. I can't honestly justify this beyond a potential crash bug, but "in the age of AI we can't just assume things" anymore.

    OK i understand. Are these the only 2 advisories about Kryo 4 or are there more?

    Sorry. >.< Only thing I can do is ask nicely, but I'll try to be following up with the apache spark project to see if we can excise/vendor com.twitter:chill, and/or update to kryo5.

    If it'll help, I can create an issue first in the spark jira about updating kryo. I do have to support spark 3.5 for a while though (which has an extended long term release from the apache spark project), so that would have to go into 4.x

    That would definitely be the way to go. Kryo 4 is more than 8 years old by now. If i remember correctly there were several efforts to upgrade chill to 5 and multiple PRs in that direction, but I'm not sure what happened to them.

    I'm currently working on Kryo 6 and I need to do a Kryo 5 release this week. I can't promise anything, but I'll try to look into this.

    theigl theigl commented on code on Oct 07, 2026 05:33 UTC
    Code Review
    src/com/esotericsoftware/kryo/serializers/FieldSerializer.java:1
    @@ -556,6 +558,28 @@ public T read (Kryo kryo, Input input, Class<T> type) {
     		}
     	}
     
    +	private void writeField (Output output, T object, CachedField field) {
    +		try {
    +			field.write(output, object);
    +		} catch (KryoException ex) {
    +			throw ex;
    +		} catch (Exception | OutOfMemoryError ex) {
    +			throw new KryoException("Error writing field: " + field.getField().getName() + " (" + type.getName()
    +				+ ") at output position " + output.position(), ex);
    +		}
    +	}
    +
    +	private void readField (Input input, T object, CachedField field) {
    +		try {
    +			field.read(input, object);
    +		} catch (KryoException ex) {
    +			throw ex;
    +		} catch (Exception | OutOfMemoryError ex) {
    +			throw new KryoException("Error reading field: " + field.getField().getName() + " (" + type.getName()
    +				+ ") at input position " + input.position(), ex);
    

    @vjanelle: Can you take a look at this please?

    vjanelle vjanelle commented on Oct 07, 2026 05:49 UTC
    Author

    @theigl appreciated.

    Those are the only two advisories about kryo 4 as far as I can tell. I can share the text with you privately if you don't mind me emailing you, it's nothing exciting however.

    vjanelle vjanelle commented on Oct 07, 2026 05:54 UTC
    Author

    @theigl oh, with regards to chill; I suspect it's abandoned now with the recent change in company ownership.

    vjanelle vjanelle commented on Oct 07, 2026 05:57 UTC
    Author
    vjanelle vjanelle commented on code on Oct 07, 2026 22:39 UTC
    Code Review
    src/com/esotericsoftware/kryo/serializers/FieldSerializer.java:1
    @@ -556,6 +558,28 @@ public T read (Kryo kryo, Input input, Class<T> type) {
     		}
     	}
     
    +	private void writeField (Output output, T object, CachedField field) {
    +		try {
    +			field.write(output, object);
    +		} catch (KryoException ex) {
    +			throw ex;
    +		} catch (Exception | OutOfMemoryError ex) {
    +			throw new KryoException("Error writing field: " + field.getField().getName() + " (" + type.getName()
    +				+ ") at output position " + output.position(), ex);
    +		}
    +	}
    +
    +	private void readField (Input input, T object, CachedField field) {
    +		try {
    +			field.read(input, object);
    +		} catch (KryoException ex) {
    +			throw ex;
    +		} catch (Exception | OutOfMemoryError ex) {
    +			throw new KryoException("Error reading field: " + field.getField().getName() + " (" + type.getName()
    +				+ ") at input position " + input.position(), ex);
    

    @theigl this will involve adding bounds checking (which was done to kryo5), will seperate that into its own commit.

    Reviewers
    Assignees
    vjanelle vjanelle
    Labels
    risk: low confidence: 80.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 76.4% (HIGH) · Higher = larger blast radius
    76.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 80.5% · Lower = rushed or shallow review
    80.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 14.9% · Safe to merge
    14.9%