Fix `--secondary-class-loader-context` for run-tests.

Change the default context to flat PathClassLoader if the
`--secondary` flag is passed and keep the old PCL hierarchy
otherwise. Explicitly pass that flag in tests where this
default is not correct.

Test: testrunner.py --host --optimizing
Test: Make the "ClassLoaderContext classpath size mismatch"
      warning FATAL and retest; only 172-app-image-twice
      fails and the failure is expected for that test.
Bug: 166088871
Change-Id: Ic50f206741e6d1f98e08b5db2adc2d7b067266a7
diff --git a/test/146-bad-interface/run b/test/146-bad-interface/run
index 937b4fd..2b4bbb0 100755
--- a/test/146-bad-interface/run
+++ b/test/146-bad-interface/run
@@ -14,4 +14,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-exec ${RUN} --secondary "${@}"
+# Use the `--secondary-class-loader-context` switch to compile the secondary dex
+# file with the right class loader context. Do not use `--secondary` as we're
+# loading the *-ex.jar file in a separate class loader.
+exec ${RUN} "${@}" \
+    --secondary-class-loader-context "PCL[$DEX_LOCATION/$TEST_NAME.jar]"
diff --git a/test/146-bad-interface/src-art/Main.java b/test/146-bad-interface/src-art/Main.java
index 958ec7c..77a2682 100644
--- a/test/146-bad-interface/src-art/Main.java
+++ b/test/146-bad-interface/src-art/Main.java
@@ -23,11 +23,11 @@
 public class Main {
   static final String DEX_LOCATION = System.getenv("DEX_LOCATION");
   static final String DEX_FILES =
-      DEX_LOCATION + "/146-bad-interface-ex.jar" + ":" +
-      DEX_LOCATION + "/146-bad-interface.jar";
+      DEX_LOCATION + "/146-bad-interface.jar" + ":" +
+      DEX_LOCATION + "/146-bad-interface-ex.jar";
   public static void main(String[] args) {
     try {
-      PathClassLoader p = new PathClassLoader(DEX_FILES, Main.class.getClassLoader());
+      PathClassLoader p = new PathClassLoader(DEX_FILES, Main.class.getClassLoader().getParent());
       Class<?> c = Class.forName("A", true, p);
       Object o = c.newInstance();
       Class<?> runner = Class.forName("InvokeInf", true, p);
diff --git a/test/164-resolution-trampoline-dex-cache/run b/test/164-resolution-trampoline-dex-cache/run
index 5e77cd5..33a6f01 100644
--- a/test/164-resolution-trampoline-dex-cache/run
+++ b/test/164-resolution-trampoline-dex-cache/run
@@ -14,9 +14,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Use the --secondary switch to add secondary dex file to class path.
 # Make sure we compile the required method using speed-profile compiler filter.
 # Enable JIT through runtime option to avoid the compiler filter set by --jit.
-exec ${RUN} "${@}" --secondary \
+exec ${RUN} "${@}" \
     -Xcompiler-option --compiler-filter=speed-profile --profile \
     --runtime-option -Xusejit:true
diff --git a/test/727-checker-unresolved-class/run b/test/727-checker-unresolved-class/run
index d087a8f..1c9dd11 100644
--- a/test/727-checker-unresolved-class/run
+++ b/test/727-checker-unresolved-class/run
@@ -17,14 +17,11 @@
 if [[ "$TEST_RUNTIME" == "jvm" ]]; then
   exec ${RUN} $@
 else
-  # Compile the secondary dex file with the right class loader hierarchy with
-  #     --secondary-class-loader-context.
   # Append graphs for checker tests (we run dex2oat twice) with
   #     --dump-cfg-append.
   # Make some classes unresolved for AOT compilation with
   #     --updatable-bcp-packages-file.
   exec ${RUN} $@ \
-      --secondary-class-loader-context "PCL[];PCL[$DEX_LOCATION/$TEST_NAME.jar]" \
       -Xcompiler-option --dump-cfg-append \
       -Xcompiler-option --updatable-bcp-packages-file="$DEX_LOCATION/res/updateable.txt"
 fi
diff --git a/test/929-search/run b/test/929-search/run
index 67923a7..fb6b1b8 100755
--- a/test/929-search/run
+++ b/test/929-search/run
@@ -16,6 +16,8 @@
 
 # This test checks whether dex files can be injected into parent classloaders. App images preload
 # classes, which will make the injection moot. Turn off app images to avoid the issue.
+# Pass the correct `--secondary-class-loader-context` for the "-ex" jar.
 
 ./default-run "$@" --jvmti \
-                   --no-app-image
+                   --no-app-image \
+                   --secondary-class-loader-context "PCL[$DEX_LOCATION/$TEST_NAME.jar]"
diff --git a/test/936-search-onload/run b/test/936-search-onload/run
index 67923a7..fb6b1b8 100755
--- a/test/936-search-onload/run
+++ b/test/936-search-onload/run
@@ -16,6 +16,8 @@
 
 # This test checks whether dex files can be injected into parent classloaders. App images preload
 # classes, which will make the injection moot. Turn off app images to avoid the issue.
+# Pass the correct `--secondary-class-loader-context` for the "-ex" jar.
 
 ./default-run "$@" --jvmti \
-                   --no-app-image
+                   --no-app-image \
+                   --secondary-class-loader-context "PCL[$DEX_LOCATION/$TEST_NAME.jar]"
diff --git a/test/etc/run-test-jar b/test/etc/run-test-jar
index b94c475..56cb4a8 100755
--- a/test/etc/run-test-jar
+++ b/test/etc/run-test-jar
@@ -1020,7 +1020,17 @@
   # If the name ends in -ex then this is a secondary dex file
   if [ "${name:${#name}-3}" = "-ex" ]; then
     # Lazily realize the default value in case DEX_LOCATION/TEST_NAME change
-    [ -z "$SECONDARY_CLASS_LOADER_CONTEXT" ] && SECONDARY_CLASS_LOADER_CONTEXT="PCL[];PCL[$DEX_LOCATION/$TEST_NAME.jar]"
+    if [ "x$SECONDARY_CLASS_LOADER_CONTEXT" = "x" ]; then
+      if [ "x$SECONDARY_DEX" = "x" ]; then
+        # Tests without `--secondary` load the "-ex" jar in a separate PathClassLoader
+        # that is a child of the main PathClassLoader. If the class loader is constructed
+        # in any other way, the test needs to specify the secondary CLC explicitly.
+        SECONDARY_CLASS_LOADER_CONTEXT="PCL[];PCL[$DEX_LOCATION/$TEST_NAME.jar]"
+      else
+        # Tests with `--secondary` load the `-ex` jar a part of the main PathClassLoader.
+        SECONDARY_CLASS_LOADER_CONTEXT="PCL[$DEX_LOCATION/$TEST_NAME.jar]"
+      fi
+    fi
     class_loader_context="'--class-loader-context=$SECONDARY_CLASS_LOADER_CONTEXT'"
     $enable_app_image && [ "$SECONDARY_APP_IMAGE" = "y" ] || enable_app_image=false
   fi