8072369: [TESTBUG] Remove potentially insecure class cast in some hotspot tests
authorkshefov
Thu, 15 Oct 2015 18:00:00 +0300
changeset 33187 384728911193
parent 33186 8bd166b4c145
child 33189 e7d20eb45c7d
8072369: [TESTBUG] Remove potentially insecure class cast in some hotspot tests Reviewed-by: twisti, kvn, iignatyev, tpivovarova
hotspot/test/compiler/c2/5057225/Test5057225.java
hotspot/test/compiler/c2/6603011/Test.java
hotspot/test/compiler/c2/6800154/Test6800154.java
hotspot/test/compiler/c2/6805724/Test6805724.java
hotspot/test/compiler/codegen/6823354/Test6823354.java
hotspot/test/testlibrary/jdk/test/lib/Utils.java
--- a/hotspot/test/compiler/c2/5057225/Test5057225.java	Thu Oct 15 11:20:04 2015 +0000
+++ b/hotspot/test/compiler/c2/5057225/Test5057225.java	Thu Oct 15 18:00:00 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,11 +25,11 @@
  * @test
  * @bug 5057225
  * @summary Remove useless I2L conversions
- *
+ * @library /testlibrary
  * @run main/othervm -Xcomp -XX:CompileOnly=Test5057225.doload Test5057225
  */
 
-import java.net.URLClassLoader;
+import jdk.test.lib.Utils;
 
 public class Test5057225 {
     static byte[]  ba = new byte[]  { -1 };
@@ -89,8 +89,9 @@
 
     static void loadAndRunClass(String classname) throws Exception {
         Class cl = Class.forName(classname);
-        URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();
-        ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
+        ClassLoader apploader = cl.getClassLoader();
+        ClassLoader loader
+                = Utils.getTestClassPathURLClassLoader(apploader.getParent());
         Class c = loader.loadClass(classname);
         Runnable r = (Runnable) c.newInstance();
         r.run();
--- a/hotspot/test/compiler/c2/6603011/Test.java	Thu Oct 15 11:20:04 2015 +0000
+++ b/hotspot/test/compiler/c2/6603011/Test.java	Thu Oct 15 18:00:00 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,7 @@
  * @test
  * @bug 6603011
  * @summary long/int division by constant
- *
+ * @library /testlibrary
  * @run main/othervm -Xcomp -Xbatch -XX:-Inline Test
  */
 
@@ -36,7 +36,7 @@
 //   dividend and divisor combinations are tested
 //
 
-import java.net.*;
+import jdk.test.lib.Utils;
 
 class s {
   static int  divi(int  dividend, int  divisor) { return dividend / divisor; }
@@ -189,10 +189,10 @@
   // This allows the JIT to see q.DIVISOR as a final constant, and change
   // any divisions or mod operations into multiplies.
   public static void test_divisor(int divisor,
-                                  URLClassLoader apploader) throws Exception {
+                                  ClassLoader apploader) throws Exception {
     System.setProperty("divisor", "" + divisor);
-    ClassLoader loader = new URLClassLoader(apploader.getURLs(),
-                                            apploader.getParent());
+    ClassLoader loader
+                = Utils.getTestClassPathURLClassLoader(apploader.getParent());
     Class c = loader.loadClass("Test");
     Runnable r = (Runnable)c.newInstance();
     r.run();
@@ -200,7 +200,7 @@
 
   public static void main(String[] args) throws Exception {
     Class cl = Class.forName("Test");
-    URLClassLoader apploader = (URLClassLoader)cl.getClassLoader();
+    ClassLoader apploader = cl.getClassLoader();
 
 
     // Test every divisor between -100 and 100.
--- a/hotspot/test/compiler/c2/6800154/Test6800154.java	Thu Oct 15 11:20:04 2015 +0000
+++ b/hotspot/test/compiler/c2/6800154/Test6800154.java	Thu Oct 15 18:00:00 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,11 +25,11 @@
  * @test
  * @bug 6800154
  * @summary Add comments to long_by_long_mulhi() for better understandability
- *
+ * @library /testlibrary
  * @run main/othervm -Xcomp -XX:CompileOnly=Test6800154.divcomp Test6800154
  */
 
-import java.net.URLClassLoader;
+import jdk.test.lib.Utils;
 
 public class Test6800154 implements Runnable {
     static final long[] DIVIDENDS = {
@@ -78,12 +78,13 @@
     public static void main(String[] args) throws Exception
     {
         Class cl = Class.forName("Test6800154");
-        URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();
+        ClassLoader apploader = cl.getClassLoader();
 
         // Iterate over all divisors.
         for (int i = 0; i < DIVISORS.length; i++) {
             System.setProperty("divisor", "" + DIVISORS[i]);
-            ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
+            ClassLoader loader
+                    = Utils.getTestClassPathURLClassLoader(apploader.getParent());
             Class c = loader.loadClass("Test6800154");
             Runnable r = (Runnable) c.newInstance();
             r.run();
--- a/hotspot/test/compiler/c2/6805724/Test6805724.java	Thu Oct 15 11:20:04 2015 +0000
+++ b/hotspot/test/compiler/c2/6805724/Test6805724.java	Thu Oct 15 18:00:00 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,12 +24,13 @@
 /**
  * @test
  * @bug 6805724
- * @summary ModLNode::Ideal() generates functionally incorrect graph when divisor is any (2^k-1) constant.
- *
+ * @summary ModLNode::Ideal() generates functionally incorrect graph
+ *          when divisor is any (2^k-1) constant.
+ * @library /testlibrary
  * @run main/othervm -Xcomp -XX:CompileOnly=Test6805724.fcomp Test6805724
  */
 
-import java.net.URLClassLoader;
+import jdk.test.lib.Utils;
 
 public class Test6805724 implements Runnable {
     // Initialize DIVISOR so that it is final in this class.
@@ -65,13 +66,14 @@
 
     public static void main(String args[]) throws Exception {
         Class cl = Class.forName("Test6805724");
-        URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();
+        ClassLoader apploader = cl.getClassLoader();
 
         // Iterate over all 2^k-1 divisors.
         for (int k = 1; k < Long.SIZE; k++) {
             long divisor = (1L << k) - 1;
             System.setProperty("divisor", "" + divisor);
-            ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
+            ClassLoader loader
+                    = Utils.getTestClassPathURLClassLoader(apploader.getParent());
             Class c = loader.loadClass("Test6805724");
             Runnable r = (Runnable) c.newInstance();
             r.run();
--- a/hotspot/test/compiler/codegen/6823354/Test6823354.java	Thu Oct 15 11:20:04 2015 +0000
+++ b/hotspot/test/compiler/codegen/6823354/Test6823354.java	Thu Oct 15 18:00:00 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,11 +25,11 @@
  * @test
  * @bug 6823354
  * @summary These methods can be instrinsified by using bit scan, bit test, and population count instructions.
- *
+ * @library /testlibrary
  * @run main/othervm -Xcomp -XX:CompileOnly=Test6823354.lzcomp,Test6823354.tzcomp,.dolzcomp,.dotzcomp Test6823354
  */
 
-import java.net.URLClassLoader;
+import jdk.test.lib.Utils;
 
 public class Test6823354 {
     // Arrays of corner case values.
@@ -197,8 +197,9 @@
 
     static void loadandrunclass(String classname) throws Exception {
         Class cl = Class.forName(classname);
-        URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();
-        ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
+        ClassLoader apploader = cl.getClassLoader();
+        ClassLoader loader
+                = Utils.getTestClassPathURLClassLoader(apploader.getParent());
         Class c = loader.loadClass(classname);
         Runnable r = (Runnable) c.newInstance();
         r.run();
--- a/hotspot/test/testlibrary/jdk/test/lib/Utils.java	Thu Oct 15 11:20:04 2015 +0000
+++ b/hotspot/test/testlibrary/jdk/test/lib/Utils.java	Thu Oct 15 18:00:00 2015 +0300
@@ -23,11 +23,15 @@
 
 package jdk.test.lib;
 
+import java.io.File;
 import static jdk.test.lib.Asserts.assertTrue;
 import java.io.IOException;
 import java.lang.reflect.Field;
 import java.net.InetAddress;
+import java.net.MalformedURLException;
 import java.net.ServerSocket;
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.net.UnknownHostException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -52,6 +56,11 @@
 public final class Utils {
 
     /**
+     * Returns the value of 'test.class.path' system property.
+     */
+    public static final String TEST_CLASS_PATH = System.getProperty("test.class.path", ".");
+
+    /**
      * Returns the sequence used by operating system to separate lines.
      */
     public static final String NEW_LINE = System.getProperty("line.separator");
@@ -474,6 +483,27 @@
         }
     }
     /**
+     * @param parent a class loader to be the parent for the returned one
+     * @return an UrlClassLoader with urls made of the 'test.class.path' jtreg
+     *         property and with the given parent
+     */
+    public static URLClassLoader getTestClassPathURLClassLoader(ClassLoader parent) {
+        URL[] urls = Arrays.stream(TEST_CLASS_PATH.split(File.pathSeparator))
+                .map(Paths::get)
+                .map(Path::toUri)
+                .map(x -> {
+                    try {
+                        return x.toURL();
+                    } catch (MalformedURLException ex) {
+                        throw new Error("Test issue. JTREG property"
+                                + " 'test.class.path'"
+                                + " is not defined correctly", ex);
+                    }
+                }).toArray(URL[]::new);
+        return new URLClassLoader(urls, parent);
+    }
+
+    /**
      * Runs runnable and checks that it throws expected exception. If exceptionException is null it means
      * that we expect no exception to be thrown.
      * @param runnable what we run