ART: Use null for the BootClassLoader in LoadLibrary

We must translate the fake BootClassLoader object to the internally
used null when loading libraries.

Bug: 29449194
Change-Id: I823d34e209149b5b96529a423c60a1d77bf8e9d1
diff --git a/runtime/java_vm_ext.cc b/runtime/java_vm_ext.cc
index c216412..c644cde 100644
--- a/runtime/java_vm_ext.cc
+++ b/runtime/java_vm_ext.cc
@@ -742,8 +742,14 @@
     // As the incoming class loader is reachable/alive during the call of this function,
     // it's okay to decode it without worrying about unexpectedly marking it alive.
     mirror::ClassLoader* loader = soa.Decode<mirror::ClassLoader*>(class_loader);
-    class_loader_allocator =
-        Runtime::Current()->GetClassLinker()->GetAllocatorForClassLoader(loader);
+
+    ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
+    if (class_linker->IsBootClassLoader(soa, loader)) {
+      loader = nullptr;
+      class_loader = nullptr;
+    }
+
+    class_loader_allocator = class_linker->GetAllocatorForClassLoader(loader);
     CHECK(class_loader_allocator != nullptr);
   }
   if (library != nullptr) {
diff --git a/test/150-loadlibrary/expected.txt b/test/150-loadlibrary/expected.txt
new file mode 100644
index 0000000..41feacf
--- /dev/null
+++ b/test/150-loadlibrary/expected.txt
@@ -0,0 +1,2 @@
+JNI_OnLoad called
+Success.
diff --git a/test/150-loadlibrary/info.txt b/test/150-loadlibrary/info.txt
new file mode 100644
index 0000000..089d044
--- /dev/null
+++ b/test/150-loadlibrary/info.txt
@@ -0,0 +1 @@
+Check that passing the BootClassLoader to loadLibrary works.
diff --git a/test/150-loadlibrary/src/Main.java b/test/150-loadlibrary/src/Main.java
new file mode 100644
index 0000000..5e34922
--- /dev/null
+++ b/test/150-loadlibrary/src/Main.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+public class Main {
+  public static void main(String[] args) throws Exception {
+    // Check whether we get the BootClassLoader (not null).
+    ClassLoader bootClassLoader = Object.class.getClassLoader();
+    if (bootClassLoader == null) {
+      throw new IllegalStateException("Expected non-null classloader for Object");
+    }
+
+    // Try to load libarttest(d) with the BootClassLoader. First construct the filename.
+    String libName = System.mapLibraryName(args[0]);
+    Method libPathsMethod = Runtime.class.getDeclaredMethod("getLibPaths");
+    libPathsMethod.setAccessible(true);
+    String[] libPaths = (String[])libPathsMethod.invoke(Runtime.getRuntime());
+    String fileName = null;
+    for (String p : libPaths) {
+      String candidate = p + libName;
+      if (new File(candidate).exists()) {
+          fileName = candidate;
+          break;
+      }
+    }
+    if (fileName == null) {
+      throw new IllegalStateException("Didn't find " + libName + " in " +
+          Arrays.toString(libPaths));
+    }
+
+    // Then call an internal function that accepts the classloader. Do not use load(), as it
+    // is deprecated and only there for backwards compatibility, and prints a warning to the
+    // log that we'd have to strip (it contains the pid).
+    Method m = Runtime.class.getDeclaredMethod("doLoad", String.class, ClassLoader.class);
+    m.setAccessible(true);
+    Object result = m.invoke(Runtime.getRuntime(), libName, bootClassLoader);
+    if (result != null) {
+      throw new IllegalStateException(result.toString());
+    }
+
+    System.out.println("Success.");
+  }
+}