aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorattila <none@none>2014-02-20 20:06:01 +0100
committerattila <none@none>2014-02-20 20:06:01 +0100
commitcf57c5786a88783e3a4925a3d40b1d6c400a2162 (patch)
treebeeb49ed100c2d7241472a650414f7a57bd2b1d6 /src
parent633c20e09eb72b1c64d2305c075f8cd15c3c6cfd (diff)
downloadnashorn-cf57c5786a88783e3a4925a3d40b1d6c400a2162.tar.gz
8034968: Reduce access to Nashorn internals
Reviewed-by: ahgross, jlaskey, sundar
Diffstat (limited to 'src')
-rw-r--r--src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java27
-rw-r--r--src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java7
-rw-r--r--src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java2
-rw-r--r--src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java20
4 files changed, 36 insertions, 20 deletions
diff --git a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java
index 96819df0..14d0e14e 100644
--- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java
+++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java
@@ -131,20 +131,17 @@ import sun.reflect.CallerSensitive;
* implemented securely.
*/
final class JavaAdapterBytecodeGenerator {
- static final Type CONTEXT_TYPE = Type.getType(Context.class);
static final Type OBJECT_TYPE = Type.getType(Object.class);
- static final Type SCRIPT_OBJECT_TYPE = Type.getType(ScriptObject.class);
- static final String CONTEXT_TYPE_NAME = CONTEXT_TYPE.getInternalName();
static final String OBJECT_TYPE_NAME = OBJECT_TYPE.getInternalName();
static final String INIT = "<init>";
static final String GLOBAL_FIELD_NAME = "global";
- static final String SCRIPT_OBJECT_TYPE_DESCRIPTOR = SCRIPT_OBJECT_TYPE.getDescriptor();
+ static final String GLOBAL_TYPE_DESCRIPTOR = OBJECT_TYPE.getDescriptor();
- static final String SET_GLOBAL_METHOD_DESCRIPTOR = Type.getMethodDescriptor(Type.VOID_TYPE, SCRIPT_OBJECT_TYPE);
+ static final String SET_GLOBAL_METHOD_DESCRIPTOR = Type.getMethodDescriptor(Type.VOID_TYPE, OBJECT_TYPE);
static final String VOID_NOARG_METHOD_DESCRIPTOR = Type.getMethodDescriptor(Type.VOID_TYPE);
private static final Type SCRIPT_FUNCTION_TYPE = Type.getType(ScriptFunction.class);
@@ -155,7 +152,7 @@ final class JavaAdapterBytecodeGenerator {
OBJECT_TYPE, STRING_TYPE, METHOD_TYPE_TYPE);
private static final String GET_HANDLE_FUNCTION_DESCRIPTOR = Type.getMethodDescriptor(METHOD_HANDLE_TYPE,
SCRIPT_FUNCTION_TYPE, METHOD_TYPE_TYPE);
- private static final String GET_CLASS_INITIALIZER_DESCRIPTOR = Type.getMethodDescriptor(SCRIPT_OBJECT_TYPE);
+ private static final String GET_CLASS_INITIALIZER_DESCRIPTOR = Type.getMethodDescriptor(OBJECT_TYPE);
private static final Type RUNTIME_EXCEPTION_TYPE = Type.getType(RuntimeException.class);
private static final Type THROWABLE_TYPE = Type.getType(Throwable.class);
private static final Type UNSUPPORTED_OPERATION_TYPE = Type.getType(UnsupportedOperationException.class);
@@ -167,7 +164,7 @@ final class JavaAdapterBytecodeGenerator {
private static final String UNSUPPORTED_OPERATION_TYPE_NAME = UNSUPPORTED_OPERATION_TYPE.getInternalName();
private static final String METHOD_HANDLE_TYPE_DESCRIPTOR = METHOD_HANDLE_TYPE.getDescriptor();
- private static final String GET_GLOBAL_METHOD_DESCRIPTOR = Type.getMethodDescriptor(SCRIPT_OBJECT_TYPE);
+ private static final String GET_GLOBAL_METHOD_DESCRIPTOR = Type.getMethodDescriptor(OBJECT_TYPE);
private static final String GET_CLASS_METHOD_DESCRIPTOR = Type.getMethodDescriptor(Type.getType(Class.class));
// Package used when the adapter can't be defined in the adaptee's package (either because it's sealed, or because
@@ -259,7 +256,7 @@ final class JavaAdapterBytecodeGenerator {
}
private void generateGlobalFields() {
- cw.visitField(ACC_PRIVATE | ACC_FINAL | (classOverride ? ACC_STATIC : 0), GLOBAL_FIELD_NAME, SCRIPT_OBJECT_TYPE_DESCRIPTOR, null, null).visitEnd();
+ cw.visitField(ACC_PRIVATE | ACC_FINAL | (classOverride ? ACC_STATIC : 0), GLOBAL_FIELD_NAME, GLOBAL_TYPE_DESCRIPTOR, null, null).visitEnd();
usedFieldNames.add(GLOBAL_FIELD_NAME);
}
@@ -363,7 +360,7 @@ final class JavaAdapterBytecodeGenerator {
}
// Assign "global = Context.getGlobal()"
invokeGetGlobalWithNullCheck(mv);
- mv.putstatic(generatedClassName, GLOBAL_FIELD_NAME, SCRIPT_OBJECT_TYPE_DESCRIPTOR);
+ mv.putstatic(generatedClassName, GLOBAL_FIELD_NAME, GLOBAL_TYPE_DESCRIPTOR);
endInitMethod(mv);
}
@@ -508,7 +505,7 @@ final class JavaAdapterBytecodeGenerator {
// Assign "this.global = Context.getGlobal()"
mv.visitVarInsn(ALOAD, 0);
invokeGetGlobalWithNullCheck(mv);
- mv.putfield(generatedClassName, GLOBAL_FIELD_NAME, SCRIPT_OBJECT_TYPE_DESCRIPTOR);
+ mv.putfield(generatedClassName, GLOBAL_FIELD_NAME, GLOBAL_TYPE_DESCRIPTOR);
endInitMethod(mv);
}
@@ -524,11 +521,11 @@ final class JavaAdapterBytecodeGenerator {
}
private static void invokeGetGlobal(final InstructionAdapter mv) {
- mv.invokestatic(CONTEXT_TYPE_NAME, "getGlobal", GET_GLOBAL_METHOD_DESCRIPTOR);
+ mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getGlobal", GET_GLOBAL_METHOD_DESCRIPTOR);
}
private static void invokeSetGlobal(final InstructionAdapter mv) {
- mv.invokestatic(CONTEXT_TYPE_NAME, "setGlobal", SET_GLOBAL_METHOD_DESCRIPTOR);
+ mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "setGlobal", SET_GLOBAL_METHOD_DESCRIPTOR);
}
/**
@@ -652,10 +649,10 @@ final class JavaAdapterBytecodeGenerator {
// Load the creatingGlobal object
if(classOverride) {
// If class handle is defined, load the static defining global
- mv.getstatic(generatedClassName, GLOBAL_FIELD_NAME, SCRIPT_OBJECT_TYPE_DESCRIPTOR);
+ mv.getstatic(generatedClassName, GLOBAL_FIELD_NAME, GLOBAL_TYPE_DESCRIPTOR);
} else {
mv.visitVarInsn(ALOAD, 0);
- mv.getfield(generatedClassName, GLOBAL_FIELD_NAME, SCRIPT_OBJECT_TYPE_DESCRIPTOR);
+ mv.getfield(generatedClassName, GLOBAL_FIELD_NAME, GLOBAL_TYPE_DESCRIPTOR);
}
// stack: [creatingGlobal, handle]
final Label setupGlobal = new Label();
@@ -744,7 +741,7 @@ final class JavaAdapterBytecodeGenerator {
final Label methodEnd = new Label();
mv.visitLabel(methodEnd);
- mv.visitLocalVariable("currentGlobal", SCRIPT_OBJECT_TYPE_DESCRIPTOR, null, setupGlobal, methodEnd, currentGlobalVar);
+ mv.visitLocalVariable("currentGlobal", GLOBAL_TYPE_DESCRIPTOR, null, setupGlobal, methodEnd, currentGlobalVar);
mv.visitLocalVariable("globalsDiffer", Type.INT_TYPE.getDescriptor(), null, setupGlobal, methodEnd, globalsDifferVar);
if(throwableDeclared) {
diff --git a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java
index fa162d88..879d908c 100644
--- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java
+++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java
@@ -31,6 +31,8 @@ import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.security.SecureClassLoader;
import jdk.internal.dynalink.beans.StaticClass;
+import jdk.nashorn.internal.runtime.Context;
+import jdk.nashorn.internal.runtime.ScriptFunction;
/**
* This class encapsulates the bytecode of the adapter class and can be used to load it into the JVM as an actual Class.
@@ -85,13 +87,14 @@ final class JavaAdapterClassLoader {
@Override
public Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
try {
+ Context.checkPackageAccess(name);
return super.loadClass(name, resolve);
} catch (final SecurityException se) {
// we may be implementing an interface or extending a class that was
// loaded by a loader that prevents package.access. If so, it'd throw
// SecurityException for nashorn's classes!. For adapter's to work, we
- // should be able to refer to nashorn classes.
- if (name.startsWith("jdk.nashorn.internal.")) {
+ // should be able to refer to the few classes it needs in its implementation.
+ if(ScriptFunction.class.getName().equals(name) || JavaAdapterServices.class.getName().equals(name)) {
return myLoader.loadClass(name);
}
throw se;
diff --git a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java
index 5e0b890a..c3acb7c5 100644
--- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java
+++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java
@@ -248,7 +248,7 @@ public final class JavaAdapterFactory {
}
private static class AdapterInfo {
- private static final ClassAndLoader SCRIPT_OBJECT_LOADER = new ClassAndLoader(ScriptObject.class, true);
+ private static final ClassAndLoader SCRIPT_OBJECT_LOADER = new ClassAndLoader(ScriptFunction.class, true);
private final ClassLoader commonLoader;
// TODO: soft reference the JavaAdapterClassLoader objects. They can be recreated when needed.
diff --git a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java
index 1188c6b6..4cb8cfa7 100644
--- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java
+++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java
@@ -116,8 +116,8 @@ public final class JavaAdapterServices {
* static initializers.
* @return the thread-local JS object used to define methods for the class being initialized.
*/
- public static ScriptObject getClassOverrides() {
- final ScriptObject overrides = classOverrides.get();
+ public static Object getClassOverrides() {
+ final Object overrides = classOverrides.get();
assert overrides != null;
return overrides;
}
@@ -134,6 +134,22 @@ public final class JavaAdapterServices {
NO_PERMISSIONS_INVOKER.invokeExact(method, arg);
}
+ /**
+ * Set the current global scope
+ * @param global the global scope
+ */
+ public static void setGlobal(final Object global) {
+ Context.setGlobal((ScriptObject)global);
+ }
+
+ /**
+ * Get the current global scope
+ * @return the current global scope
+ */
+ public static Object getGlobal() {
+ return Context.getGlobal();
+ }
+
static void setClassOverrides(ScriptObject overrides) {
classOverrides.set(overrides);
}