Skip to content

Commit

Permalink
Showing 6 changed files with 20 additions and 63 deletions.
2 changes: 2 additions & 0 deletions core/pom.rb
Original file line number Diff line number Diff line change
@@ -78,6 +78,8 @@

jar 'me.qmx.jitescript:jitescript:0.4.1', :exclusions => ['org.ow2.asm:asm-all']

jar 'com.headius:modulator:1.0'

plugin_management do
plugin( 'org.eclipse.m2e:lifecycle-mapping:1.0.0',
'lifecycleMappingMetadata' => {
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
@@ -273,6 +273,11 @@ DO NOT MODIFIY - GENERATED CODE
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.headius</groupId>
<artifactId>modulator</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<defaultGoal>package</defaultGoal>
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;

import com.headius.modulator.Modulator;
import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.exceptions.RaiseException;
@@ -341,13 +342,13 @@ static JavaProxy castJavaProxy(final IRubyObject self) {
return (JavaProxy) self;
}

static <T extends AccessibleObject> T setAccessible(T accessible) {
static <T extends AccessibleObject & Member> T setAccessible(T accessible) {
// TODO: Replace flag that's false on 9 with proper module checks
if (!accessible.isAccessible() &&
!Ruby.isSecurityRestricted() &&
Options.JI_SETACCESSIBLE.load() &&
accessible instanceof Member) {
try { Helpers.trySetAccessible((Member) accessible); }
try { Modulator.trySetAccessible(accessible); }
catch (SecurityException e) {}
catch (RuntimeException re) {
rethrowIfNotInaccessibleObject(re);
52 changes: 1 addition & 51 deletions core/src/main/java/org/jruby/runtime/Helpers.java
Original file line number Diff line number Diff line change
@@ -3,9 +3,7 @@
import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.nio.channels.ClosedChannelException;
import java.nio.charset.Charset;
import java.util.ArrayList;
@@ -15,6 +13,7 @@
import java.util.Map;
import java.util.StringTokenizer;

import com.headius.modulator.Modulator;
import jnr.constants.platform.Errno;
import org.jruby.*;
import org.jruby.ast.ArgsNode;
@@ -2580,53 +2579,4 @@ public static boolean respondsToMethod(DynamicMethod method, boolean checkVisibi
(method.getVisibility() == PRIVATE || method.getVisibility() == PROTECTED));
}

/**
* When running on Java 9 this method will use module openness to do setAccessible, if possible.
*
* @param member the method, field, or constructor to attempt to setAccessible
* @return whether the setAccessible was successful
*/
public static boolean trySetAccessible(Member member) {
if (!(member instanceof AccessibleObject)) return false;

AccessibleObject ao = (AccessibleObject) member;

if (ao.isAccessible()) return true;

if (getModule != null) {
try {
Class<?> declaringClass = member.getDeclaringClass();
Object module = getModule.invoke(declaringClass);
if ((Boolean) isOpen.invoke(module, declaringClass.getPackage().getName())) {
ao.setAccessible(true);
return true;
}
return false;
} catch (Exception e) {
return false;
}
} else {
try {
ao.setAccessible(true);
return true;
} catch (Exception e) {
return false;
}
}
}

private static final Method getModule;
private static final Method isOpen;
static {
Method _getModule = null;
Method _isOpen = null;
try {
_getModule = Class.class.getMethod("getModule");
Class module = Class.forName("java.lang.Module");
_isOpen = module.getMethod("isOpen", String.class);
} catch (Exception e) {
}
getModule = _getModule;
isOpen = _isOpen;
}
}
11 changes: 5 additions & 6 deletions core/src/main/java/org/jruby/util/io/ChannelHelper.java
Original file line number Diff line number Diff line change
@@ -10,9 +10,8 @@
*/
package org.jruby.util.io;

import jnr.posix.util.FieldAccess;
import com.headius.modulator.Modulator;
import org.jruby.RubyInstanceConfig;
import org.jruby.runtime.Helpers;

import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
@@ -97,7 +96,7 @@ public static OutputStream unwrapFilterOutputStream(OutputStream filteredStream)
try {
Field out = FilterInputStream.class.getDeclaredField("out");
OutputStream tmpStream =
Helpers.trySetAccessible(out) ? (OutputStream) out.get(filteredStream) : null;
Modulator.trySetAccessible(out) ? (OutputStream) out.get(filteredStream) : null;

// try to unwrap as a Drip stream
if (!(tmpStream instanceof FilterOutputStream)) {
@@ -135,7 +134,7 @@ public static InputStream unwrapFilterInputStream(InputStream filteredStream) {
try {
Field in = FilterInputStream.class.getDeclaredField("in");
InputStream tmpStream =
Helpers.trySetAccessible(in) ? (InputStream) in.get(filteredStream) : null;
Modulator.trySetAccessible(in) ? (InputStream) in.get(filteredStream) : null;

// could not acquire
if (tmpStream == null) break;
@@ -164,7 +163,7 @@ private static OutputStream unwrapDripStream(OutputStream stream) {
if (isDripSwitchable(stream)) {
try {
Field out = stream.getClass().getDeclaredField("out");
return Helpers.trySetAccessible(out) ? (OutputStream) out.get(stream) : null;
return Modulator.trySetAccessible(out) ? (OutputStream) out.get(stream) : null;
} catch (Exception e) {
}
}
@@ -175,7 +174,7 @@ private static InputStream unwrapDripStream(InputStream stream) {
if (isDripSwitchable(stream)) {
try {
Field in = stream.getClass().getDeclaredField("in");
return Helpers.trySetAccessible(in) ? (InputStream) in.get(stream) : null;
return Modulator.trySetAccessible(in) ? (InputStream) in.get(stream) : null;
} catch (Exception e) {
}
}
8 changes: 4 additions & 4 deletions core/src/main/java/org/jruby/util/io/FilenoUtil.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.jruby.util.io;

import com.headius.modulator.Modulator;
import jnr.enxio.channels.NativeDeviceChannel;
import jnr.enxio.channels.NativeSocketChannel;
import jnr.posix.FileStat;
import jnr.posix.POSIX;
import jnr.unixsocket.UnixServerSocketChannel;
import jnr.unixsocket.UnixSocketChannel;
import org.jruby.runtime.Helpers;

import java.io.FileDescriptor;
import java.lang.reflect.Field;
@@ -142,7 +142,7 @@ private static class ReflectiveAccess {
selChImpl = Class.forName("sun.nio.ch.SelChImpl");
try {
getFD = selChImpl.getMethod("getFD");
if (!Helpers.trySetAccessible(getFD)) {
if (!Modulator.trySetAccessible(getFD)) {
getFD = null;
}
} catch (Exception e) {
@@ -161,7 +161,7 @@ private static class ReflectiveAccess {
fileChannelImpl = Class.forName("sun.nio.ch.FileChannelImpl");
try {
fd = fileChannelImpl.getDeclaredField("fd");
if (!Helpers.trySetAccessible(fd)) {
if (!Modulator.trySetAccessible(fd)) {
fd = null;
}
} catch (Exception e) {
@@ -177,7 +177,7 @@ private static class ReflectiveAccess {
Field ffd;
try {
ffd = FileDescriptor.class.getDeclaredField("fd");
if (!Helpers.trySetAccessible(ffd)) {
if (!Modulator.trySetAccessible(ffd)) {
ffd = null;
}
} catch (Exception e) {

0 comments on commit 4e62ac5

Please sign in to comment.