changeset 59963:3fa59689c0fe

8248112: array index out of bound in FileMapInfo::check_paths Summary: Check runtime classpath lengths after calling create_path_array(). Reviewed-by: iklam, minqi
author ccheung
date Fri, 26 Jun 2020 19:26:30 +0000
parents 4d1a122a5e4e
children 0d3b9e32d012
files src/hotspot/share/memory/filemap.cpp test/hotspot/jtreg/runtime/cds/appcds/BootClassPathMismatch.java test/hotspot/jtreg/runtime/cds/appcds/WrongClasspath.java
diffstat 3 files changed, 47 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/hotspot/share/memory/filemap.cpp	Fri Jun 26 11:56:21 2020 -0700
+++ b/src/hotspot/share/memory/filemap.cpp	Fri Jun 26 19:26:30 2020 +0000
@@ -749,6 +749,11 @@
         num = rp_len;
       }
       mismatch = check_paths(1, num, rp_array);
+    } else {
+      // create_path_array() ignores non-existing paths. Although the dump time and runtime boot classpath lengths
+      // are the same initially, after the call to create_path_array(), the runtime boot classpath length could become
+      // shorter. We consider boot classpath mismatch in this case.
+      mismatch = true;
     }
   }
 
@@ -775,6 +780,12 @@
       // None of the jar file specified in the runtime -cp exists.
       return classpath_failure("None of the jar file specified in the runtime -cp exists: -Djava.class.path=", appcp);
     }
+    if (rp_array->length() < shared_app_paths_len) {
+      // create_path_array() ignores non-existing paths. Although the dump time and runtime app classpath lengths
+      // are the same initially, after the call to create_path_array(), the runtime app classpath length could become
+      // shorter. We consider app classpath mismatch in this case.
+      return classpath_failure("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
+    }
 
     // Handling of non-existent entries in the classpath: we eliminate all the non-existent
     // entries from both the dump time classpath (ClassLoader::update_class_path_entry_list)
--- a/test/hotspot/jtreg/runtime/cds/appcds/BootClassPathMismatch.java	Fri Jun 26 11:56:21 2020 -0700
+++ b/test/hotspot/jtreg/runtime/cds/appcds/BootClassPathMismatch.java	Fri Jun 26 19:26:30 2020 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2020, 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
@@ -28,6 +28,7 @@
  * @requires vm.cds
  * @library /test/lib
  * @compile test-classes/Hello.java
+ * @compile test-classes/C2.java
  * @run driver BootClassPathMismatch
  */
 
@@ -61,6 +62,7 @@
             test.testBootClassPathMatchWithAppend();
         }
         test.testBootClassPathMatch();
+        test.testBootClassPathMismatchTwoJars();
     }
 
     /* Archive contains boot classes only, with Hello class on -Xbootclasspath/a path.
@@ -215,6 +217,25 @@
             .assertAbnormalExit(mismatchMessage);
     }
 
+    /* Archive contains app classes, with 2 jars in bootclasspath at dump time.
+     *
+     * Error should be detected if:
+     * dump time: -Xbootclasspath/a:hello.jar:jar2.jar
+     * run-time : -Xbootclasspath/a:hello.jar:jar2.jarx
+     * Note: the second jar (jar2.jarx) specified for run-time doesn't exist.
+     */
+    public void testBootClassPathMismatchTwoJars() throws Exception {
+        String appJar = JarBuilder.getOrCreateHelloJar();
+        String jar2 = ClassFileInstaller.writeJar("jar2.jar", "pkg/C2");
+        String jars = appJar + File.pathSeparator + jar2;
+        String appClasses[] = {"Hello", "pkg/C2"};
+        TestCommon.dump(
+            appJar, appClasses, "-Xbootclasspath/a:" + jars);
+        TestCommon.run(
+                "-cp", appJar, "-Xbootclasspath/a:" + jars + "x", "Hello")
+            .assertAbnormalExit(mismatchMessage);
+    }
+
     private static void copyHelloToNewDir() throws Exception {
         String classDir = System.getProperty("test.classes");
         String dstDir = classDir + File.separator + "newdir";
--- a/test/hotspot/jtreg/runtime/cds/appcds/WrongClasspath.java	Fri Jun 26 11:56:21 2020 -0700
+++ b/test/hotspot/jtreg/runtime/cds/appcds/WrongClasspath.java	Fri Jun 26 19:26:30 2020 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2020, 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
@@ -28,9 +28,11 @@
  * @requires vm.cds
  * @library /test/lib
  * @compile test-classes/Hello.java
+ * @compile test-classes/C2.java
  * @run driver WrongClasspath
  */
 
+import java.io.File;
 import jdk.test.lib.process.OutputAnalyzer;
 
 public class WrongClasspath {
@@ -48,5 +50,16 @@
         "Hello")
         .assertAbnormalExit("Unable to use shared archive",
                             "shared class paths mismatch");
+
+    // Dump CDS archive with 2 jars: -cp hello.jar:jar2.jar
+    // Run with 2 jars but the second jar doesn't exist: -cp hello.jarjar2.jarx
+    // Shared class paths mismatch should be detected.
+    String jar2 = ClassFileInstaller.writeJar("jar2.jar", "pkg/C2");
+    String jars = appJar + File.pathSeparator + jar2;
+    TestCommon.testDump(jars, TestCommon.list("Hello", "pkg/C2"));
+    TestCommon.run(
+        "-cp", jars + "x", "Hello")
+        .assertAbnormalExit("Unable to use shared archive",
+                            "shared class paths mismatch");
   }
 }