CVE-2019-11938 (published 2020-03-10T21:15Z)

Java Facebook Thrift servers would not error upon receiving messages declaring containers of sizes larger than the payload. As a result, malicious clients could send short messages which would result in a large memory allocation, potentially leading to denial of service. This issue affects Facebook Thrift prior to v2019.12.09.00.

Severity: MEDIUM
CVSS v2 base score: 5.0 (AV:N/AC:L/Au:N/C:N/I:N/A:P)
CVSS v3 base score: 7.5, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H)

Weakness information:
cwe_id: CWE-770
cwe_name: Allocation of Resources Without Limits or Throttling
description: The product allocates a reusable resource or group of resources on behalf of an actor without imposing any restrictions on the size or number of resources that can be allocated, in violation of the intended security policy for that actor.
extended_description: Code frequently has to work with limited resources, so programmers must be careful to ensure that resources are not consumed too quickly, or too easily.  Without use of quotas, resource limits, or other protection mechanisms, it can be easy for an attacker to consume many resources by rapidly making many requests, or causing larger resources to be used than is needed. When too many resources are allocated, or if a single resource is too large, then it can prevent the code from working correctly, possibly leading to a denial of service.
         

Fix information:
repo_url: https://github.com/facebook/fbthrift
commit_details: repo_url: https://github.com/facebook/fbthrift
author: Steve Gury
author_date: 2019-12-06 13:11:57-08:00
msg: Java: Check the size of the remaining frame before deserializing a string

Summary:
In order to avoid over-allocating memory for malformed or truncated frame, we
ensure that we have enough data in the current frame.

This is a partial fix for CVE-2019-11938.

Reviewed By: vitaut

Differential Revision: D14505601

fbshipit-source-id: c90f248828b067a3a5debcc8df6a3f4e9da6d195
repository: repo_url: https://github.com/facebook/fbthrift
repo_name: facebook/fbthrift
description: Facebook's branch of Apache Thrift, including a new C++ server.
repo_language: C++
owner: facebook
file_changes: filename: TBinaryProtocol.java
change_type: ModificationType.MODIFY
diff: @@ -343,6 +343,7 @@ public class TBinaryProtocol extends TProtocol {
   }
 
   public String readStringBody(int size) throws TException {
+    ensureContainerHasEnough(size, TType.BYTE);
     checkReadLength(size);
     byte[] buf = new byte[size];
     trans_.readAll(buf, 0, size);
@@ -351,6 +352,7 @@ public class TBinaryProtocol extends TProtocol {
 
   public byte[] readBinary() throws TException {
     int size = readI32();
+    ensureContainerHasEnough(size, TType.BYTE);
     checkReadLength(size);
     byte[] buf = new byte[size];
     trans_.readAll(buf, 0, size);

programming_language: Java
filename: TCompactProtocol.java
change_type: ModificationType.MODIFY
diff: @@ -638,6 +638,7 @@ public class TCompactProtocol extends TProtocol {
       return new byte[0];
     }
 
+    ensureContainerHasEnough(length, TType.BYTE);
     byte[] buf = new byte[length];
     trans_.readAll(buf, 0, length);
     return buf;

programming_language: Java
filename: testing_data_structures.thrift
change_type: ModificationType.MODIFY
diff: @@ -62,3 +62,7 @@ struct MySetStruct {
 struct MyMapStruct {
   1: map<i64, string> mapping
 }
+
+struct MyStringStruct {
+  1: string aLongString
+}

programming_language: Scala
filename: TruncatedFrameTest.java
change_type: ModificationType.MODIFY
diff: @@ -19,6 +19,7 @@ package com.facebook.thrift;
 import com.facebook.thrift.java.test.MyListStruct;
 import com.facebook.thrift.java.test.MyMapStruct;
 import com.facebook.thrift.java.test.MySetStruct;
+import com.facebook.thrift.java.test.MyStringStruct;
 import com.facebook.thrift.protocol.TBinaryProtocol;
 import com.facebook.thrift.protocol.TCompactProtocol;
 import com.facebook.thrift.protocol.TProtocol;
@@ -52,7 +53,7 @@ public class TruncatedFrameTest extends junit.framework.TestCase {
     (byte) 0x00,
     (byte) 0x00,
     (byte) 0x00,
-    (byte) 0x01, // value = 2L
+    (byte) 0x02, // value = 2L
     (byte) 0x00,
     (byte) 0x00,
     (byte) 0x00,
@@ -60,7 +61,7 @@ public class TruncatedFrameTest extends junit.framework.TestCase {
     (byte) 0x00,
     (byte) 0x00,
     (byte) 0x00,
-    (byte) 0x01, // value = 3L
+    (byte) 0x03, // value = 3L
     (byte) 0x00, // Stop
   };
 
@@ -139,7 +140,7 @@ public class TruncatedFrameTest extends junit.framework.TestCase {
     (byte) 0x00,
     (byte) 0x00,
     (byte) 0x00,
-    (byte) 0x01, // value = 2L
+    (byte) 0x02, // value = 2L
     (byte) 0x00,
     (byte) 0x00,
     (byte) 0x00,
@@ -147,7 +148,7 @@ public class TruncatedFrameTest extends junit.framework.TestCase {
     (byte) 0x00,
     (byte) 0x00,
     (byte) 0x00,
-    (byte) 0x01, // value = 3L
+    (byte) 0x03, // value = 3L
     (byte) 0x00, // Stop
   };
 
@@ -273,16 +274,61 @@ public class TruncatedFrameTest extends junit.framework.TestCase {
     testTruncated(new MyMapStruct(), iprot);
   }
 
-  private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
+  private static final byte[] kBinaryStringEncoding = {
+    TType.STRING, // Field Type = string
+    (byte) 0x00,
+    (byte) 0x01, // Field id = 1
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0xFF, // string length (255!)
+    (byte) 0x48,
+    (byte) 0x65,
+    (byte) 0x6C,
+    (byte) 0x6C,
+    (byte) 0x6F,
+    (byte) 0x2C,
+    (byte) 0x20,
+    (byte) 0x57,
+    (byte) 0x6F,
+    (byte) 0x72,
+    (byte) 0x6C,
+    (byte) 0x64,
+    (byte) 0x21, // string chars: "Hello, World!"
+    (byte) 0x00, // Stop
+  };
 
-  private static String bytesToHex(byte[] bytes, int length) {
-    String out = "";
-    for (int j = 0; j < length; j++) {
-      int v = bytes[j] & 0xFF;
-      out += hexArray[v >>> 4];
-      out += hexArray[v & 0x0F];
-      out += " ";
-    }
-    return out;
+  private static final byte[] kCompactStringEncoding = {
+    (byte) 0b00011000, // field id delta (0001) + type (1000) = Binary
+    (byte) 0xFF,
+    (byte) 0x0F, // string size (varint) = 0x0FFF (4095)
+    (byte) 0x48,
+    (byte) 0x65,
+    (byte) 0x6C,
+    (byte) 0x6C,
+    (byte) 0x6F,
+    (byte) 0x2C,
+    (byte) 0x20,
+    (byte) 0x57,
+    (byte) 0x6F,
+    (byte) 0x72,
+    (byte) 0x6C,
+    (byte) 0x64,
+    (byte) 0x21, // string chars: "Hello, World!"
+    (byte) 0x00, // Stop
+  };
+
+  @Test
+  public void testStringBinary() throws Exception {
+    TMemoryInputTransport buf = new TMemoryInputTransport(kBinaryStringEncoding);
+    TProtocol iprot = new TBinaryProtocol(buf);
+    testTruncated(new MyStringStruct(), iprot);
+  }
+
+  @Test
+  public void testStringCompact() throws Exception {
+    TMemoryInputTransport buf = new TMemoryInputTransport(kCompactStringEncoding);
+    TProtocol iprot = new TCompactProtocol(buf);
+    testTruncated(new MyStringStruct(), iprot);
   }
 }

programming_language: Java
repo_url: https://github.com/facebook/fbthrift
commit_details: repo_url: https://github.com/facebook/fbthrift
author: Steve Gury
author_date: 2019-12-06 13:11:57-08:00
msg: Java: Check the size of the remaining frame before deserializing collection

Summary:
In order to avoid over-allocating memory for malformed or truncated frame, we ensure that we have enough data (we only check for the lower bound) in the current frame.

This is a partial fix for CVE-2019-11938.

Reviewed By: vitaut

Differential Revision: D14500775

fbshipit-source-id: ca8b38965514d6319addcb72c8999a6854a94a88
repository: repo_url: https://github.com/facebook/fbthrift
repo_name: facebook/fbthrift
description: Facebook's branch of Apache Thrift, including a new C++ server.
repo_language: C++
owner: facebook
file_changes: filename: TBinaryProtocol.java
change_type: ModificationType.MODIFY
diff: @@ -225,19 +225,29 @@ public class TBinaryProtocol extends TProtocol {
   public void readFieldEnd() {}
 
   public TMap readMapBegin() throws TException {
-    return new TMap(readByte(), readByte(), readI32());
+    byte keyType = readByte();
+    byte valueType = readByte();
+    int size = readI32();
+    ensureMapHasEnough(size, keyType, valueType);
+    return new TMap(keyType, valueType, size);
   }
 
   public void readMapEnd() {}
 
   public TList readListBegin() throws TException {
-    return new TList(readByte(), readI32());
+    byte type = readByte();
+    int size = readI32();
+    ensureContainerHasEnough(size, type);
+    return new TList(type, size);
   }
 
   public void readListEnd() {}
 
   public TSet readSetBegin() throws TException {
-    return new TSet(readByte(), readI32());
+    byte type = readByte();
+    int size = readI32();
+    ensureContainerHasEnough(size, type);
+    return new TSet(type, size);
   }
 
   public void readSetEnd() {}
@@ -368,4 +378,35 @@ public class TBinaryProtocol extends TProtocol {
       }
     }
   }
+
+  @Override
+  protected int typeMinimumSize(byte type) {
+    switch (type & 0x0f) {
+      case TType.BOOL:
+      case TType.BYTE:
+        return 1;
+      case TType.I16:
+        return 2;
+      case TType.I32:
+      case TType.FLOAT:
+        return 4;
+      case TType.DOUBLE:
+      case TType.I64:
+        return 8;
+      case TType.STRING:
+        return 4;
+      case TType.LIST:
+      case TType.SET:
+        // type (1 byte) + size (4 bytes)
+        return 1 + 4;
+      case TType.MAP:
+        // key type (1 byte) + value type (1 byte) + size (4 bytes)
+        return 1 + 1 + 4;
+      case TType.STRUCT:
+        return 1;
+      default:
+        throw new TProtocolException(
+            TProtocolException.INVALID_DATA, "Unexpected data type " + (byte) (type & 0x0f));
+    }
+  }
 }

programming_language: Java
filename: TCompactProtocol.java
change_type: ModificationType.MODIFY
diff: @@ -513,8 +513,12 @@ public class TCompactProtocol extends TProtocol {
   public TMap readMapBegin() throws TException {
     int size = readVarint32();
     byte keyAndValueType = size == 0 ? 0 : readByte();
-    return new TMap(
-        getTType((byte) (keyAndValueType >> 4)), getTType((byte) (keyAndValueType & 0xf)), size);
+    byte keyType = getTType((byte) (keyAndValueType >> 4));
+    byte valueType = getTType((byte) (keyAndValueType & 0xf));
+    if (size > 0) {
+      ensureMapHasEnough(size, keyType, valueType);
+    }
+    return new TMap(keyType, valueType, size);
   }
 
   /**
@@ -529,6 +533,7 @@ public class TCompactProtocol extends TProtocol {
       size = readVarint32();
     }
     byte type = getTType(size_and_type);
+    ensureContainerHasEnough(size, type);
     return new TList(type, size);
   }
 
@@ -829,4 +834,30 @@ public class TCompactProtocol extends TProtocol {
   private byte getCompactType(byte ttype) {
     return ttypeToCompactType[ttype];
   }
+
+  @Override
+  protected int typeMinimumSize(byte type) {
+    switch (type & 0x0f) {
+      case TType.BOOL:
+      case TType.BYTE:
+      case TType.I16: // because of variable length encoding
+      case TType.I32: // because of variable length encoding
+      case TType.I64: // because of variable length encoding
+        return 1;
+      case TType.FLOAT:
+        return 4;
+      case TType.DOUBLE:
+        return 8;
+      case TType.STRING:
+      case TType.STRUCT:
+      case TType.MAP:
+      case TType.SET:
+      case TType.LIST:
+      case TType.ENUM:
+        return 1;
+      default:
+        throw new TProtocolException(
+            TProtocolException.INVALID_DATA, "Unexpected data type " + (byte) (type & 0x0f));
+    }
+  }
 }

programming_language: Java
filename: TProtocol.java
change_type: ModificationType.MODIFY
diff: @@ -179,4 +179,31 @@ public abstract class TProtocol {
   public Class<? extends IScheme> getScheme() {
     return StandardScheme.class;
   }
+
+  /** Return the minimum size of a type */
+  protected int typeMinimumSize(byte type) {
+    return 1;
+  }
+
+  protected void ensureContainerHasEnough(int size, byte type) {
+    int minimumExpected = size * typeMinimumSize(type);
+    ensureHasEnoughBytes(minimumExpected);
+  }
+
+  protected void ensureMapHasEnough(int size, byte keyType, byte valueType) {
+    int minimumExpected = size * (typeMinimumSize(keyType) + typeMinimumSize(valueType));
+    ensureHasEnoughBytes(minimumExpected);
+  }
+
+  private void ensureHasEnoughBytes(int minimumExpected) {
+    int remaining = trans_.getBytesRemainingInBuffer();
+    if (remaining < 0) {
+      return; // Some transport are not buffered
+    }
+    if (remaining < minimumExpected) {
+      throw new TProtocolException(
+          TProtocolException.INVALID_DATA,
+          "Not enough bytes to read the entire message, the data appears to be truncated");
+    }
+  }
 }

programming_language: Java
filename: TProtocolDecorator.java
change_type: ModificationType.MODIFY
diff: @@ -217,4 +217,9 @@ public abstract class TProtocolDecorator extends TProtocol {
   public byte[] readBinary() throws TException {
     return concreteProtocol.readBinary();
   }
+
+  @Override
+  protected int typeMinimumSize(byte type) {
+    return concreteProtocol.typeMinimumSize(type);
+  }
 }

programming_language: Java
filename: testing_data_structures.thrift
change_type: ModificationType.MODIFY
diff: @@ -14,7 +14,8 @@
  * limitations under the License.
  */
 
-namespace java com.facebook.thrift.test
+namespace java com.facebook.thrift.java.test
+namespace java.swift com.facebook.thrift.javaswift.test
 
 struct MySimpleStruct {
   1: i64 id,
@@ -49,3 +50,15 @@ enum BigEnum {
   NINETEEN = 19,
   TWENTY = 20,
 }
+
+struct MyListStruct {
+  1: list<i64> ids
+}
+
+struct MySetStruct {
+  1: set<i64> ids
+}
+
+struct MyMapStruct {
+  1: map<i64, string> mapping
+}

programming_language: Scala
filename: StructTest.java
change_type: ModificationType.MODIFY
diff: @@ -21,9 +21,9 @@ import static org.hamcrest.Matchers.not;
 import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertThat;
 
-import com.facebook.thrift.test.BigEnum;
-import com.facebook.thrift.test.MySimpleStruct;
-import com.facebook.thrift.test.SmallEnum;
+import com.facebook.thrift.java.test.BigEnum;
+import com.facebook.thrift.java.test.MySimpleStruct;
+import com.facebook.thrift.java.test.SmallEnum;
 import junit.framework.TestCase;
 import org.junit.Test;
 

programming_language: Java
filename: TruncatedFrameTest.java
change_type: ModificationType.ADD
diff: @@ -0,0 +1,288 @@
+/*
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.facebook.thrift;
+
+import com.facebook.thrift.java.test.MyListStruct;
+import com.facebook.thrift.java.test.MyMapStruct;
+import com.facebook.thrift.java.test.MySetStruct;
+import com.facebook.thrift.protocol.TBinaryProtocol;
+import com.facebook.thrift.protocol.TCompactProtocol;
+import com.facebook.thrift.protocol.TProtocol;
+import com.facebook.thrift.protocol.TProtocolException;
+import com.facebook.thrift.protocol.TType;
+import com.facebook.thrift.transport.TMemoryInputTransport;
+import org.junit.Test;
+
+public class TruncatedFrameTest extends junit.framework.TestCase {
+  private static final byte[] kBinaryListEncoding = {
+    TType.LIST, // Field Type = List
+    (byte) 0x00,
+    (byte) 0x01, // Field id = 1
+    TType.I64, // List type = i64
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0xFF, // List length (255 > 3!)
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x01, // value = 1L
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x01, // value = 2L
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x01, // value = 3L
+    (byte) 0x00, // Stop
+  };
+
+  private static final byte[] kCompactListEncoding = {
+    (byte) 0b00011001, // field id delta (0001) + type (1001) = List
+    (byte) 0b11100110, // list size (0111) and 7>3 + list type (0110) = i64
+    (byte) 0x02, // value = 1 (zigzag encoded)
+    (byte) 0x04, // value = 2 (zigzag encoded)
+    (byte) 0x06, // value = 3 (zigzag encoded)
+    (byte) 0x00, // Stop
+  };
+
+  private static final byte[] kCompactListEncoding2 = {
+    (byte) 0b00011001, // field id delta (0001) + type (1001) = List
+    (byte) 0b11110110, // list size magic marker (1111) + list type (0110) = i64
+    (byte) 0x64, // list actual size (varint of 1 byte here) = 100
+    (byte) 0x02, // value = 1 (zigzag encoded)
+    (byte) 0x04, // value = 2 (zigzag encoded)
+    (byte) 0x06, // value = 3 (zigzag encoded)
+    (byte) 0x00, // Stop
+  };
+
+  public static void testTruncated(TBase struct, TProtocol iprot) throws Exception {
+    try {
+      struct.read(iprot);
+      assertTrue("Not reachable", false);
+    } catch (TProtocolException ex) {
+      assertEquals(
+          "Not enough bytes to read the entire message, the data appears to be truncated",
+          ex.getMessage());
+    }
+  }
+
+  @Test
+  public static void testListBinary() throws Exception {
+    TMemoryInputTransport buf = new TMemoryInputTransport(kBinaryListEncoding);
+    TProtocol iprot = new TBinaryProtocol(buf);
+    testTruncated(new MyListStruct(), iprot);
+  }
+
+  @Test
+  public static void testListCompact() throws Exception {
+    TMemoryInputTransport buf = new TMemoryInputTransport(kCompactListEncoding);
+    TProtocol iprot = new TCompactProtocol(buf);
+    testTruncated(new MyListStruct(), iprot);
+  }
+
+  @Test
+  public static void testLongListCompact() throws Exception {
+    TMemoryInputTransport buf = new TMemoryInputTransport(kCompactListEncoding2);
+    TProtocol iprot = new TCompactProtocol(buf);
+    testTruncated(new MyListStruct(), iprot);
+  }
+
+  private static final byte[] kBinarySetEncoding = {
+    TType.SET, // Field Type = Set
+    (byte) 0x00,
+    (byte) 0x01, // Field id = 1
+    TType.I64, // Set type = i64
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0xFF, // Set length (255 > 3!)
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x01, // value = 1L
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x01, // value = 2L
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x01, // value = 3L
+    (byte) 0x00, // Stop
+  };
+
+  private static final byte[] kCompactSetEncoding = {
+    (byte) 0b00011010, // field id delta (0001) + type (1010) = Set
+    (byte) 0b01110110, // set size (0111) and 7>3 + set type (0110) = i64
+    (byte) 0x02, // value = 1 (zigzag encoded)
+    (byte) 0x04, // value = 2 (zigzag encoded)
+    (byte) 0x06, // value = 3 (zigzag encoded)
+    (byte) 0x00, // Stop
+  };
+
+  private static final byte[] kCompactSetEncoding2 = {
+    (byte) 0b00011010, // field id delta (0001) + type (1010) = Set
+    (byte) 0b11110110, // set size magic marker (1111) + set type (0110) = i64
+    (byte) 0x64, // set actual size (varint of 1 byte here) = 100
+    (byte) 0x02, // value = 1 (zigzag encoded)
+    (byte) 0x04, // value = 2 (zigzag encoded)
+    (byte) 0x06, // value = 3 (zigzag encoded)
+    (byte) 0x00, // Stop
+  };
+
+  @Test
+  public static void testSetBinary() throws Exception {
+    TMemoryInputTransport buf = new TMemoryInputTransport(kBinarySetEncoding);
+    TProtocol iprot = new TBinaryProtocol(buf);
+    testTruncated(new MySetStruct(), iprot);
+  }
+
+  @Test
+  public static void testSetCompact() throws Exception {
+    TMemoryInputTransport buf = new TMemoryInputTransport(kCompactSetEncoding);
+    TProtocol iprot = new TCompactProtocol(buf);
+    testTruncated(new MySetStruct(), iprot);
+  }
+
+  @Test
+  public static void testLongSetCompact() throws Exception {
+    TMemoryInputTransport buf = new TMemoryInputTransport(kCompactSetEncoding2);
+    TProtocol iprot = new TCompactProtocol(buf);
+    testTruncated(new MySetStruct(), iprot);
+  }
+
+  private static final byte[] kBinaryMapEncoding = {
+    TType.MAP, // field type = Map
+    (byte) 0x00,
+    (byte) 0x01, // field id = 1
+    TType.I64, // key type = i64
+    TType.STRING, // value type = string
+    (byte) 0x00,
+    (byte) 0xFF,
+    (byte) 0xFF,
+    (byte) 0xFF, // size = 0x00FFFFFF
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00, // key = 0
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x01, // string size = 1
+    (byte) 0x30, // string value = "0"
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x01, // key = 1
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x01, // string size = 1
+    (byte) 0x31, // string value = "1"
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x02, // key = 2
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x00,
+    (byte) 0x01, // string size = 1
+    (byte) 0x32, // string value = "2"
+    (byte) 0x00, // Stop
+  };
+
+  private static final byte[] kCompactMapEncoding = {
+    (byte) 0b00011011, // field id delta (0001) + type (1011) = Map
+    (byte) 0x64, // map size (varint = 100)
+    (byte) 0b01101000, // key type (0110) i64, value type (1000) string
+    (byte) 0x00, // key value = 0
+    (byte) 0x01, // value: string size = 1
+    (byte) 0x30, // string content = "0"
+    (byte) 0x02, // key value = 1 (zigzag encoded)
+    (byte) 0x01, // value: string size = 1
+    (byte) 0x31, // string content = "1"
+    (byte) 0x04, // key value = 2 (zigzag encoded)
+    (byte) 0x01, // value: string size = 1
+    (byte) 0x32, // string content = "2"
+    (byte) 0x00, // Stop
+  };
+
+  @Test
+  public static void testMapBinary() throws Exception {
+    TMemoryInputTransport buf = new TMemoryInputTransport(kBinaryMapEncoding);
+    TProtocol iprot = new TBinaryProtocol(buf);
+    testTruncated(new MyMapStruct(), iprot);
+  }
+
+  @Test
+  public static void testMapCompact() throws Exception {
+    TMemoryInputTransport buf = new TMemoryInputTransport(kCompactMapEncoding);
+    TProtocol iprot = new TCompactProtocol(buf);
+    testTruncated(new MyMapStruct(), iprot);
+  }
+
+  private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
+
+  private static String bytesToHex(byte[] bytes, int length) {
+    String out = "";
+    for (int j = 0; j < length; j++) {
+      int v = bytes[j] & 0xFF;
+      out += hexArray[v >>> 4];
+      out += hexArray[v & 0x0F];
+      out += " ";
+    }
+    return out;
+  }
+}

programming_language: Java