Skip to content

Commit

Permalink
[ISSUE openmessaging#238] Fix illegal reflective access
Browse files Browse the repository at this point in the history
  • Loading branch information
totalo authored Oct 3, 2022
1 parent b02cd0c commit e28522e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
4 changes: 4 additions & 0 deletions dledger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@
package io.openmessaging.storage.dledger.store.file;

import io.openmessaging.storage.dledger.utils.DLedgerUtils;
import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.Unsafe;
import sun.nio.ch.DirectBuffer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
Expand All @@ -31,8 +39,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultMmapFile extends ReferenceResource implements MmapFile {
public static final int OS_PAGE_SIZE = 1024 * 4;
Expand Down Expand Up @@ -116,9 +122,25 @@ public static void clean(final ByteBuffer buffer) {
if (buffer == null || !buffer.isDirect() || buffer.capacity() == 0) {
return;
}
invoke(invoke(viewed(buffer), "cleaner"), "clean");
if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9)) {
invokeAfterJava8(buffer);
} else {
invoke(invoke(viewed(buffer), "cleaner"), "clean");
}
}


private static void invokeAfterJava8(final ByteBuffer buffer) {
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);
}
}

private static Object invoke(final Object target, final String methodName, final Class<?>... args) {
return AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
try {
Expand All @@ -141,17 +163,11 @@ private static Method method(Object target, String methodName, Class<?>[] args)
}

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

Method[] methods = buffer.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<jcommander.version>1.72</jcommander.version>
<rocketmq-remoting.version>4.9.4</rocketmq-remoting.version>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<commons-lang3.version>3.12.0</commons-lang3.version>
<!-- Exclude all generated code -->
<sonar.jacoco.itReportPath>${project.basedir}/../test/target/jacoco-it.exec</sonar.jacoco.itReportPath>
<sonar.exclusions>file:**/generated-sources/**,**/test/**</sonar.exclusions>
Expand Down Expand Up @@ -104,6 +105,11 @@
<artifactId>jcommander</artifactId>
<version>${jcommander.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit e28522e

Please sign in to comment.