1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import java.io.File;
18 import java.lang.reflect.Method;
19 import java.util.Arrays;
20 
21 public class Main {
main(String[] args)22   public static void main(String[] args) throws Exception {
23     // Check whether we get the BootClassLoader (not null).
24     ClassLoader bootClassLoader = Object.class.getClassLoader();
25     if (bootClassLoader == null) {
26       throw new IllegalStateException("Expected non-null classloader for Object");
27     }
28 
29     // Try to load libarttest(d) with the BootClassLoader. First construct the filename.
30     String libName = System.mapLibraryName(args[0]);
31     Method libPathsMethod = Runtime.class.getDeclaredMethod("getLibPaths");
32     libPathsMethod.setAccessible(true);
33     String[] libPaths = (String[])libPathsMethod.invoke(Runtime.getRuntime());
34     String fileName = null;
35     for (String p : libPaths) {
36       String candidate = p + libName;
37       if (new File(candidate).exists()) {
38           fileName = candidate;
39           break;
40       }
41     }
42     if (fileName == null) {
43       throw new IllegalStateException("Didn't find " + libName + " in " +
44           Arrays.toString(libPaths));
45     }
46 
47     // Then call an internal function that accepts the classloader. Do not use load(), as it
48     // is deprecated and only there for backwards compatibility, and prints a warning to the
49     // log that we'd have to strip (it contains the pid).
50     Method m = Runtime.class.getDeclaredMethod("nativeLoad", String.class, ClassLoader.class);
51     m.setAccessible(true);
52     Object result = m.invoke(Runtime.getRuntime(), fileName, bootClassLoader);
53     if (result != null) {
54       throw new IllegalStateException(result.toString());
55     }
56 
57     System.out.println("Success.");
58   }
59 }
60