From 8b21cd9e356850b4cb8dee3083d64b6e7dd1894e Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Wed, 8 Mar 2017 09:20:54 -0800 Subject: [PATCH] PlatformDependent0 should enforce array index scale for byte[] explicitly Motivation: PlatformDependent0 makes assumptions that the array index scale for byte[] is always 1. If this is not the case the results from methods which make this assumption will be undefined. Modifications: - PlatformDependent0 should check if unsafe.arrayIndexScale(byte[].class) is not 1, and if so not use unsafe Result: Assumptions made by optimizations in PlatformDependent0 which use byte[] are explicitly enforced. --- .../io/netty/util/internal/PlatformDependent0.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common/src/main/java/io/netty/util/internal/PlatformDependent0.java b/common/src/main/java/io/netty/util/internal/PlatformDependent0.java index 351d4947dc..ea2bb9068f 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent0.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent0.java @@ -167,6 +167,16 @@ final class PlatformDependent0 { unsafe = null; } } + + if (unsafe != null) { + // There are assumptions made where ever BYTE_ARRAY_BASE_OFFSET is used (equals, hashCodeAscii, and + // primitive accessors) that arrayIndexScale == 1, and results are undefined if this is not the case. + long byteArrayIndexScale = unsafe.arrayIndexScale(byte[].class); + if (byteArrayIndexScale != 1) { + logger.debug("unsafe.arrayIndexScale is {} (expected: 1). Not using unsafe.", byteArrayIndexScale); + unsafe = null; + } + } } UNSAFE = unsafe;