|
| 1 | +/* Copyright (c) 2008-2025, Nathan Sweet |
| 2 | + * All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following |
| 5 | + * conditions are met: |
| 6 | + * |
| 7 | + * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 8 | + * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following |
| 9 | + * disclaimer in the documentation and/or other materials provided with the distribution. |
| 10 | + * - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived |
| 11 | + * from this software without specific prior written permission. |
| 12 | + * |
| 13 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
| 14 | + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 15 | + * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 16 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 17 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 18 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
| 19 | + |
| 20 | +package com.esotericsoftware.kryo.serializers; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | + |
| 24 | +import com.esotericsoftware.kryo.Kryo; |
| 25 | +import com.esotericsoftware.kryo.KryoException; |
| 26 | +import com.esotericsoftware.kryo.KryoTestCase; |
| 27 | +import com.esotericsoftware.kryo.Serializer; |
| 28 | +import com.esotericsoftware.kryo.io.Input; |
| 29 | +import com.esotericsoftware.kryo.io.Output; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | + |
| 38 | +/** Verifies that an exception thrown while (de)serializing a generic field does not leak the generics stack and poison a reused |
| 39 | + * Kryo instance. |
| 40 | + * <p> |
| 41 | + * See <a href="https://github.com/EsotericSoftware/kryo/issues/1281">#1281</a>: when a serializer threw between |
| 42 | + * {@link com.esotericsoftware.kryo.util.Generics#pushGenericType pushGenericType} and the balancing |
| 43 | + * {@link com.esotericsoftware.kryo.util.Generics#popGenericType popGenericType}, the leaked entry survived {@link Kryo#reset()} |
| 44 | + * and caused a later, unrelated deserialization to fail with an {@link ArrayIndexOutOfBoundsException}. */ |
| 45 | +class GenericsResetTest extends KryoTestCase { |
| 46 | + |
| 47 | + @Test |
| 48 | + void testGenericsStackIsResetAfterDeserializationException () { |
| 49 | + kryo.setReferences(false); |
| 50 | + kryo.setRegistrationRequired(false); |
| 51 | + kryo.register(Holder.class); |
| 52 | + kryo.register(Item.class); |
| 53 | + kryo.register(Payload.class, new ThrowOnReadSerializer()); |
| 54 | + |
| 55 | + // A Holder whose List<Item> element holds a Payload. Reading the Payload throws, after the generic type for the |
| 56 | + // List<Item> field was pushed, leaking it onto the generics stack. |
| 57 | + Holder holder = new Holder(); |
| 58 | + holder.items = new ArrayList(); |
| 59 | + Item item = new Item(); |
| 60 | + item.payload = new Payload(); |
| 61 | + holder.items.add(item); |
| 62 | + |
| 63 | + Output output = new Output(512); |
| 64 | + kryo.writeClassAndObject(output, holder); |
| 65 | + byte[] failingBytes = output.toBytes(); |
| 66 | + |
| 67 | + // An unrelated, well-formed HashMap containing two non-empty nested maps. On a poisoned instance, reading the second |
| 68 | + // nested map mistakes the leaked List generic for a Map generic and indexes [1], throwing AIOOBE. |
| 69 | + byte[] validBytes = writeNestedMaps(); |
| 70 | + |
| 71 | + // The first deserialization fails inside the generic field. |
| 72 | + assertThrows(KryoException.class, () -> kryo.readClassAndObject(new Input(failingBytes))); |
| 73 | + |
| 74 | + // Kryo.reset() must have discarded the leaked generic type. |
| 75 | + assertEquals(0, kryo.getGenerics().getGenericTypesSize()); |
| 76 | + |
| 77 | + // A subsequent unrelated deserialization on the same instance must succeed. |
| 78 | + Object result = kryo.readClassAndObject(new Input(validBytes)); |
| 79 | + assertTrue(result instanceof Map); |
| 80 | + assertEquals(2, ((Map)result).size()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void testGenericsStackIsResetAfterSerializationException () { |
| 85 | + kryo.setReferences(false); |
| 86 | + kryo.setRegistrationRequired(false); |
| 87 | + kryo.register(Holder.class); |
| 88 | + kryo.register(Item.class); |
| 89 | + kryo.register(Payload.class, new ThrowOnWriteSerializer()); |
| 90 | + |
| 91 | + Holder holder = new Holder(); |
| 92 | + holder.items = new ArrayList(); |
| 93 | + Item item = new Item(); |
| 94 | + item.payload = new Payload(); |
| 95 | + holder.items.add(item); |
| 96 | + |
| 97 | + // Writing the Payload throws, after the generic type for the List<Item> field was pushed. |
| 98 | + assertThrows(KryoException.class, () -> kryo.writeClassAndObject(new Output(512), holder)); |
| 99 | + |
| 100 | + // Kryo.reset() must have discarded the leaked generic type. |
| 101 | + assertEquals(0, kryo.getGenerics().getGenericTypesSize()); |
| 102 | + |
| 103 | + // A subsequent unrelated serialization/deserialization on the same instance must succeed. |
| 104 | + byte[] validBytes = writeNestedMaps(); |
| 105 | + Object result = kryo.readClassAndObject(new Input(validBytes)); |
| 106 | + assertTrue(result instanceof Map); |
| 107 | + assertEquals(2, ((Map)result).size()); |
| 108 | + } |
| 109 | + |
| 110 | + /** A bare HashMap with two non-empty nested maps, mirroring the payload that triggers the AIOOBE on a poisoned instance. */ |
| 111 | + private byte[] writeNestedMaps () { |
| 112 | + Map<String, Object> map = new HashMap(); |
| 113 | + Map<String, String> nested1 = new HashMap(); |
| 114 | + nested1.put("a", "1"); |
| 115 | + Map<String, String> nested2 = new HashMap(); |
| 116 | + nested2.put("b", "2"); |
| 117 | + map.put("k1", nested1); |
| 118 | + map.put("k2", nested2); |
| 119 | + |
| 120 | + Output output = new Output(512); |
| 121 | + kryo.writeClassAndObject(output, map); |
| 122 | + return output.toBytes(); |
| 123 | + } |
| 124 | + |
| 125 | + public static class Holder { |
| 126 | + public List<Item> items; |
| 127 | + } |
| 128 | + |
| 129 | + public static class Item { |
| 130 | + public Object payload; |
| 131 | + } |
| 132 | + |
| 133 | + public static class Payload { |
| 134 | + } |
| 135 | + |
| 136 | + /** Writes nothing and always throws on read, simulating any read-time failure (eg a missing class). */ |
| 137 | + static class ThrowOnReadSerializer extends Serializer<Payload> { |
| 138 | + public void write (Kryo kryo, Output output, Payload object) { |
| 139 | + } |
| 140 | + |
| 141 | + public Payload read (Kryo kryo, Input input, Class<? extends Payload> type) { |
| 142 | + throw new RuntimeException("simulated read failure"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** Always throws on write, simulating any write-time failure. */ |
| 147 | + static class ThrowOnWriteSerializer extends Serializer<Payload> { |
| 148 | + public void write (Kryo kryo, Output output, Payload object) { |
| 149 | + throw new RuntimeException("simulated write failure"); |
| 150 | + } |
| 151 | + |
| 152 | + public Payload read (Kryo kryo, Input input, Class<? extends Payload> type) { |
| 153 | + return new Payload(); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments