Skip to content

Commit

Permalink
fix illegal reflective access
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowySpirits committed Sep 16, 2022
1 parent ad72dbe commit abf6ea5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
32 changes: 21 additions & 11 deletions common/src/main/java/org/apache/rocketmq/common/UtilAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.Inet4Address;
import java.net.Inet6Address;
Expand All @@ -42,11 +43,15 @@
import java.util.zip.CRC32;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.validator.routines.InetAddressValidator;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.InternalLogger;
import org.apache.rocketmq.logging.InternalLoggerFactory;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import sun.misc.Unsafe;
import sun.nio.ch.DirectBuffer;

public class UtilAll {
private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.COMMON_LOGGER_NAME);
Expand Down Expand Up @@ -670,7 +675,19 @@ public static void cleanBuffer(final ByteBuffer buffer) {
if (buffer == null || !buffer.isDirect() || buffer.capacity() == 0) {
return;
}
invoke(invoke(viewed(buffer), "cleaner"), "clean");
if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9)) {
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
Unsafe unsafe = (Unsafe) field.get(null);
Method cleaner = method(unsafe, "invokeCleaner", new Class[] {ByteBuffer.class});
cleaner.invoke(unsafe, viewed(buffer));
} catch (Exception e) {
throw new IllegalStateException(e);
}
} else {
invoke(invoke(viewed(buffer), "cleaner"), "clean");
}
}

public static Object invoke(final Object target, final String methodName, final Class<?>... args) {
Expand All @@ -697,17 +714,10 @@ public static Method method(Object target, String methodName, Class<?>[] args) t
}

private static ByteBuffer viewed(ByteBuffer buffer) {
String methodName = "viewedBuffer";

Method[] methods = buffer.getClass().getMethods();
for (Method method : methods) {
if (method.getName().equals("attachment")) {
methodName = "attachment";
break;
}
if (!buffer.isDirect()) {
throw new IllegalArgumentException("buffer is non-direct");
}

ByteBuffer viewedBuffer = (ByteBuffer) invoke(buffer, methodName);
ByteBuffer viewedBuffer = (ByteBuffer) ((DirectBuffer) buffer).attachment();
if (viewedBuffer == null) {
return buffer;
} else {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<fastjson.version>1.2.69_noneautotype</fastjson.version>
<javassist.version>3.20.0-GA</javassist.version>
<jna.version>4.2.2</jna.version>
<commons-lang3.version>3.4</commons-lang3.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<commons-io.version>2.7</commons-io.version>
<guava.version>31.0.1-jre</guava.version>
<openmessaging.version>0.3.1-alpha</openmessaging.version>
Expand Down

0 comments on commit abf6ea5

Please sign in to comment.