Thu, 11 Jul 2013 16:34:55 +0530
8020325: static property does not work on accessible, public classes
Reviewed-by: attila, hannesw, lagergren
1.1 --- a/make/build.xml Wed Jul 10 19:08:04 2013 +0530 1.2 +++ b/make/build.xml Thu Jul 11 16:34:55 2013 +0530 1.3 @@ -219,8 +219,10 @@ 1.4 target="${javac.target}" 1.5 debug="${javac.debug}" 1.6 encoding="${javac.encoding}" 1.7 - includeantruntime="false"> 1.8 - <compilerarg line="-extdirs """/> 1.9 + includeantruntime="false" fork="true"> 1.10 + <compilerarg value="-J-Djava.ext.dirs="/> 1.11 + <compilerarg value="-Xlint:unchecked"/> 1.12 + <compilerarg value="-Xlint:deprecation"/> 1.13 </javac> 1.14 1.15 <!-- tests that check nashorn internals and internal API -->
2.1 --- a/src/jdk/nashorn/api/scripting/NashornScriptEngine.java Wed Jul 10 19:08:04 2013 +0530 2.2 +++ b/src/jdk/nashorn/api/scripting/NashornScriptEngine.java Thu Jul 11 16:34:55 2013 +0530 2.3 @@ -195,11 +195,7 @@ 2.4 if (! Modifier.isPublic(clazz.getModifiers())) { 2.5 throw new SecurityException("attempt to implement non-public interfce: " + clazz); 2.6 } 2.7 - final String fullName = clazz.getName(); 2.8 - final int index = fullName.lastIndexOf('.'); 2.9 - if (index != -1) { 2.10 - sm.checkPackageAccess(fullName.substring(0, index)); 2.11 - } 2.12 + Context.checkPackageAccess(clazz.getName()); 2.13 } 2.14 2.15 final ScriptObject realSelf;
3.1 --- a/src/jdk/nashorn/internal/codegen/CodeGenerator.java Wed Jul 10 19:08:04 2013 +0530 3.2 +++ b/src/jdk/nashorn/internal/codegen/CodeGenerator.java Thu Jul 11 16:34:55 2013 +0530 3.3 @@ -1313,7 +1313,7 @@ 3.4 } 3.5 3.6 @Override 3.7 - public boolean enterLiteralNode(final LiteralNode literalNode) { 3.8 + public boolean enterLiteralNode(final LiteralNode<?> literalNode) { 3.9 assert literalNode.getSymbol() != null : literalNode + " has no symbol"; 3.10 load(literalNode).store(literalNode.getSymbol()); 3.11 return false;
4.1 --- a/src/jdk/nashorn/internal/codegen/Compiler.java Wed Jul 10 19:08:04 2013 +0530 4.2 +++ b/src/jdk/nashorn/internal/codegen/Compiler.java Thu Jul 11 16:34:55 2013 +0530 4.3 @@ -528,8 +528,8 @@ 4.4 return this.env; 4.5 } 4.6 4.7 - private String safeSourceName(final Source source) { 4.8 - String baseName = new File(source.getName()).getName(); 4.9 + private String safeSourceName(final Source src) { 4.10 + String baseName = new File(src.getName()).getName(); 4.11 4.12 final int index = baseName.lastIndexOf(".js"); 4.13 if (index != -1) {
5.1 --- a/src/jdk/nashorn/internal/lookup/Lookup.java Wed Jul 10 19:08:04 2013 +0530 5.2 +++ b/src/jdk/nashorn/internal/lookup/Lookup.java Thu Jul 11 16:34:55 2013 +0530 5.3 @@ -32,8 +32,6 @@ 5.4 import java.lang.invoke.MethodHandles; 5.5 import java.lang.invoke.MethodType; 5.6 import jdk.nashorn.internal.runtime.JSType; 5.7 -import jdk.nashorn.internal.runtime.Property; 5.8 -import jdk.nashorn.internal.runtime.PropertyMap; 5.9 import jdk.nashorn.internal.runtime.ScriptRuntime; 5.10 5.11 /**
6.1 --- a/src/jdk/nashorn/internal/objects/NativeDebug.java Wed Jul 10 19:08:04 2013 +0530 6.2 +++ b/src/jdk/nashorn/internal/objects/NativeDebug.java Thu Jul 11 16:34:55 2013 +0530 6.3 @@ -179,6 +179,9 @@ 6.4 6.5 /** 6.6 * Returns the property listener count for a script object 6.7 + * 6.8 + * @param self self reference 6.9 + * @param obj script object whose listener count is returned 6.10 * @return listener count 6.11 */ 6.12 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
7.1 --- a/src/jdk/nashorn/internal/objects/NativeNumber.java Wed Jul 10 19:08:04 2013 +0530 7.2 +++ b/src/jdk/nashorn/internal/objects/NativeNumber.java Thu Jul 11 16:34:55 2013 +0530 7.3 @@ -48,7 +48,6 @@ 7.4 import jdk.nashorn.internal.runtime.PropertyMap; 7.5 import jdk.nashorn.internal.runtime.ScriptObject; 7.6 import jdk.nashorn.internal.runtime.ScriptRuntime; 7.7 -import jdk.nashorn.internal.lookup.MethodHandleFactory; 7.8 import jdk.nashorn.internal.runtime.linker.PrimitiveLookup; 7.9 7.10 /**
8.1 --- a/src/jdk/nashorn/internal/objects/ScriptFunctionImpl.java Wed Jul 10 19:08:04 2013 +0530 8.2 +++ b/src/jdk/nashorn/internal/objects/ScriptFunctionImpl.java Thu Jul 11 16:34:55 2013 +0530 8.3 @@ -212,10 +212,10 @@ 8.4 // Instance of this class is used as global anonymous function which 8.5 // serves as Function.prototype object. 8.6 private static class AnonymousFunction extends ScriptFunctionImpl { 8.7 - private static final PropertyMap map$ = PropertyMap.newMap().setIsShared(); 8.8 + private static final PropertyMap anonmap$ = PropertyMap.newMap().setIsShared(); 8.9 8.10 static PropertyMap getInitialMap() { 8.11 - return map$; 8.12 + return anonmap$; 8.13 } 8.14 8.15 AnonymousFunction(final Global global) {
9.1 --- a/src/jdk/nashorn/internal/runtime/Context.java Wed Jul 10 19:08:04 2013 +0530 9.2 +++ b/src/jdk/nashorn/internal/runtime/Context.java Thu Jul 11 16:34:55 2013 +0530 9.3 @@ -39,13 +39,10 @@ 9.4 import java.util.concurrent.atomic.AtomicLong; 9.5 import java.net.MalformedURLException; 9.6 import java.net.URL; 9.7 -import java.security.AccessControlContext; 9.8 import java.security.AccessController; 9.9 import java.security.CodeSigner; 9.10 import java.security.CodeSource; 9.11 -import java.security.Permissions; 9.12 import java.security.PrivilegedAction; 9.13 -import java.security.ProtectionDomain; 9.14 import java.util.Map; 9.15 import jdk.internal.org.objectweb.asm.ClassReader; 9.16 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter; 9.17 @@ -208,7 +205,6 @@ 9.18 9.19 private static final ClassLoader myLoader = Context.class.getClassLoader(); 9.20 private static final StructureLoader sharedLoader; 9.21 - private static final AccessControlContext NO_PERMISSIONS_CONTEXT; 9.22 9.23 static { 9.24 sharedLoader = AccessController.doPrivileged(new PrivilegedAction<StructureLoader>() { 9.25 @@ -217,7 +213,6 @@ 9.26 return new StructureLoader(myLoader, null); 9.27 } 9.28 }); 9.29 - NO_PERMISSIONS_CONTEXT = new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) }); 9.30 } 9.31 9.32 /** 9.33 @@ -560,6 +555,21 @@ 9.34 } 9.35 9.36 /** 9.37 + * Checks that the given package can be accessed from current call stack. 9.38 + * 9.39 + * @param fullName fully qualified package name 9.40 + */ 9.41 + public static void checkPackageAccess(final String fullName) { 9.42 + final int index = fullName.lastIndexOf('.'); 9.43 + if (index != -1) { 9.44 + final SecurityManager sm = System.getSecurityManager(); 9.45 + if (sm != null) { 9.46 + sm.checkPackageAccess(fullName.substring(0, index)); 9.47 + } 9.48 + } 9.49 + } 9.50 + 9.51 + /** 9.52 * Lookup a Java class. This is used for JSR-223 stuff linking in from 9.53 * {@code jdk.nashorn.internal.objects.NativeJava} and {@code jdk.nashorn.internal.runtime.NativeJavaPackage} 9.54 * 9.55 @@ -571,19 +581,7 @@ 9.56 */ 9.57 public Class<?> findClass(final String fullName) throws ClassNotFoundException { 9.58 // check package access as soon as possible! 9.59 - final int index = fullName.lastIndexOf('.'); 9.60 - if (index != -1) { 9.61 - final SecurityManager sm = System.getSecurityManager(); 9.62 - if (sm != null) { 9.63 - AccessController.doPrivileged(new PrivilegedAction<Void>() { 9.64 - @Override 9.65 - public Void run() { 9.66 - sm.checkPackageAccess(fullName.substring(0, index)); 9.67 - return null; 9.68 - } 9.69 - }, NO_PERMISSIONS_CONTEXT); 9.70 - } 9.71 - } 9.72 + checkPackageAccess(fullName); 9.73 9.74 // try the script -classpath loader, if that is set 9.75 if (classPathLoader != null) {
10.1 --- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java Wed Jul 10 19:08:04 2013 +0530 10.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java Thu Jul 11 16:34:55 2013 +0530 10.3 @@ -43,6 +43,7 @@ 10.4 import jdk.internal.dynalink.beans.StaticClass; 10.5 import jdk.internal.dynalink.support.LinkRequestImpl; 10.6 import jdk.nashorn.internal.objects.NativeJava; 10.7 +import jdk.nashorn.internal.runtime.Context; 10.8 import jdk.nashorn.internal.runtime.ECMAException; 10.9 import jdk.nashorn.internal.runtime.ScriptFunction; 10.10 import jdk.nashorn.internal.runtime.ScriptObject; 10.11 @@ -101,13 +102,9 @@ 10.12 assert types != null && types.length > 0; 10.13 final SecurityManager sm = System.getSecurityManager(); 10.14 if (sm != null) { 10.15 - for (Class type : types) { 10.16 + for (Class<?> type : types) { 10.17 // check for restricted package access 10.18 - final String fullName = type.getName(); 10.19 - final int index = fullName.lastIndexOf('.'); 10.20 - if (index != -1) { 10.21 - sm.checkPackageAccess(fullName.substring(0, index)); 10.22 - } 10.23 + Context.checkPackageAccess(type.getName()); 10.24 } 10.25 } 10.26 return getAdapterInfo(types).getAdapterClassFor(classOverrides);
11.1 --- a/src/jdk/nashorn/internal/runtime/linker/ReflectionCheckLinker.java Wed Jul 10 19:08:04 2013 +0530 11.2 +++ b/src/jdk/nashorn/internal/runtime/linker/ReflectionCheckLinker.java Thu Jul 11 16:34:55 2013 +0530 11.3 @@ -25,10 +25,14 @@ 11.4 11.5 package jdk.nashorn.internal.runtime.linker; 11.6 11.7 +import java.lang.reflect.Modifier; 11.8 +import jdk.internal.dynalink.CallSiteDescriptor; 11.9 import jdk.internal.dynalink.linker.GuardedInvocation; 11.10 import jdk.internal.dynalink.linker.LinkRequest; 11.11 import jdk.internal.dynalink.linker.LinkerServices; 11.12 import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 11.13 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 11.14 +import jdk.nashorn.internal.runtime.Context; 11.15 11.16 /** 11.17 * Check java reflection permission for java reflective and java.lang.invoke access from scripts 11.18 @@ -52,6 +56,25 @@ 11.19 throws Exception { 11.20 final SecurityManager sm = System.getSecurityManager(); 11.21 if (sm != null) { 11.22 + final LinkRequest requestWithoutContext = origRequest.withoutRuntimeContext(); // Nashorn has no runtime context 11.23 + final Object self = requestWithoutContext.getReceiver(); 11.24 + // allow 'static' access on Class objects representing public classes of non-restricted packages 11.25 + if ((self instanceof Class) && Modifier.isPublic(((Class<?>)self).getModifiers())) { 11.26 + final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor(); 11.27 + final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0); 11.28 + // check for 'get' on 'static' property 11.29 + switch (operator) { 11.30 + case "getProp": 11.31 + case "getMethod": { 11.32 + if ("static".equals(desc.getNameToken(CallSiteDescriptor.NAME_OPERAND))) { 11.33 + Context.checkPackageAccess(((Class)self).getName()); 11.34 + // let bean linker do the actual linking part 11.35 + return null; 11.36 + } 11.37 + } 11.38 + break; 11.39 + } // fall through for all other stuff 11.40 + } 11.41 sm.checkPermission(new RuntimePermission("nashorn.JavaReflection")); 11.42 } 11.43 // let the next linker deal with actual linking
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/test/script/basic/JDK-8020325.js Thu Jul 11 16:34:55 2013 +0530 12.3 @@ -0,0 +1,38 @@ 12.4 +/* 12.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 12.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 12.7 + * 12.8 + * This code is free software; you can redistribute it and/or modify it 12.9 + * under the terms of the GNU General Public License version 2 only, as 12.10 + * published by the Free Software Foundation. 12.11 + * 12.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 12.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12.15 + * version 2 for more details (a copy is included in the LICENSE file that 12.16 + * accompanied this code). 12.17 + * 12.18 + * You should have received a copy of the GNU General Public License version 12.19 + * 2 along with this work; if not, write to the Free Software Foundation, 12.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 12.21 + * 12.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 12.23 + * or visit www.oracle.com if you need additional information or have any 12.24 + * questions. 12.25 + */ 12.26 + 12.27 +/** 12.28 + * JDK-8020325: static property does not work on accessible, public classes 12.29 + * 12.30 + * @test 12.31 + * @run 12.32 + */ 12.33 + 12.34 +function printStatic(obj) { 12.35 + print(obj.getClass().static); 12.36 +} 12.37 + 12.38 +printStatic(new java.util.ArrayList()); 12.39 +printStatic(new java.util.HashMap()); 12.40 +printStatic(new java.lang.Object()); 12.41 +printStatic(new (Java.type("java.lang.Object[]"))(0));
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/test/script/basic/JDK-8020325.js.EXPECTED Thu Jul 11 16:34:55 2013 +0530 13.3 @@ -0,0 +1,4 @@ 13.4 +[JavaClass java.util.ArrayList] 13.5 +[JavaClass java.util.HashMap] 13.6 +[JavaClass java.lang.Object] 13.7 +[JavaClass [Ljava.lang.Object;]
14.1 --- a/test/script/trusted/JDK-8006529.js Wed Jul 10 19:08:04 2013 +0530 14.2 +++ b/test/script/trusted/JDK-8006529.js Thu Jul 11 16:34:55 2013 +0530 14.3 @@ -39,21 +39,19 @@ 14.4 * and FunctionNode because of package-access check and so reflective calls. 14.5 */ 14.6 14.7 -var forName = java.lang.Class["forName(String)"] 14.8 - 14.9 -var Parser = forName("jdk.nashorn.internal.parser.Parser").static 14.10 -var Compiler = forName("jdk.nashorn.internal.codegen.Compiler").static 14.11 -var Context = forName("jdk.nashorn.internal.runtime.Context").static 14.12 -var ScriptEnvironment = forName("jdk.nashorn.internal.runtime.ScriptEnvironment").static 14.13 -var Source = forName("jdk.nashorn.internal.runtime.Source").static 14.14 -var FunctionNode = forName("jdk.nashorn.internal.ir.FunctionNode").static 14.15 -var Block = forName("jdk.nashorn.internal.ir.Block").static 14.16 -var VarNode = forName("jdk.nashorn.internal.ir.VarNode").static 14.17 -var ExecuteNode = forName("jdk.nashorn.internal.ir.ExecuteNode").static 14.18 -var UnaryNode = forName("jdk.nashorn.internal.ir.UnaryNode").static 14.19 -var BinaryNode = forName("jdk.nashorn.internal.ir.BinaryNode").static 14.20 -var ThrowErrorManager = forName("jdk.nashorn.internal.runtime.Context$ThrowErrorManager").static 14.21 -var Debug = forName("jdk.nashorn.internal.runtime.Debug").static 14.22 +var Parser = Java.type("jdk.nashorn.internal.parser.Parser") 14.23 +var Compiler = Java.type("jdk.nashorn.internal.codegen.Compiler") 14.24 +var Context = Java.type("jdk.nashorn.internal.runtime.Context") 14.25 +var ScriptEnvironment = Java.type("jdk.nashorn.internal.runtime.ScriptEnvironment") 14.26 +var Source = Java.type("jdk.nashorn.internal.runtime.Source") 14.27 +var FunctionNode = Java.type("jdk.nashorn.internal.ir.FunctionNode") 14.28 +var Block = Java.type("jdk.nashorn.internal.ir.Block") 14.29 +var VarNode = Java.type("jdk.nashorn.internal.ir.VarNode") 14.30 +var ExecuteNode = Java.type("jdk.nashorn.internal.ir.ExecuteNode") 14.31 +var UnaryNode = Java.type("jdk.nashorn.internal.ir.UnaryNode") 14.32 +var BinaryNode = Java.type("jdk.nashorn.internal.ir.BinaryNode") 14.33 +var ThrowErrorManager = Java.type("jdk.nashorn.internal.runtime.Context$ThrowErrorManager") 14.34 +var Debug = Java.type("jdk.nashorn.internal.runtime.Debug") 14.35 14.36 var parseMethod = Parser.class.getMethod("parse"); 14.37 var compileMethod = Compiler.class.getMethod("compile", FunctionNode.class);
15.1 --- a/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Wed Jul 10 19:08:04 2013 +0530 15.2 +++ b/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Thu Jul 11 16:34:55 2013 +0530 15.3 @@ -968,7 +968,7 @@ 15.4 15.5 // get implementation of a restricted package interface 15.6 try { 15.7 - log(Objects.toString(((Invocable)e).getInterface(PropertyAccessClass))); 15.8 + log(Objects.toString(((Invocable)e).getInterface((Class<?>)PropertyAccessClass))); 15.9 fail("should have thrown SecurityException"); 15.10 } catch (final Exception exp) { 15.11 if (! (exp instanceof SecurityException)) {