• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013, Google LLC
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google LLC nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 package com.android.tools.smali.dexlib2.analysis;
32 
33 import com.android.tools.smali.dexlib2.Opcodes;
34 import com.android.tools.smali.dexlib2.analysis.reflection.ReflectionClassDef;
35 import com.android.tools.smali.dexlib2.analysis.util.LruCache;
36 import com.android.tools.smali.dexlib2.analysis.util.MemoizingSupplier;
37 import com.android.tools.smali.dexlib2.iface.ClassDef;
38 import com.android.tools.smali.dexlib2.immutable.ImmutableDexFile;
39 import com.android.tools.smali.util.IteratorUtils;
40 
41 import javax.annotation.Nonnull;
42 import java.io.IOException;
43 import java.io.Serializable;
44 import java.util.Arrays;
45 import java.util.Collections;
46 import java.util.HashSet;
47 import java.util.List;
48 import java.util.function.Supplier;
49 
50 public class ClassPath {
51     @Nonnull private final TypeProto unknownClass;
52     @Nonnull private List<ClassProvider> classProviders;
53     private final boolean checkPackagePrivateAccess;
54     public final int oatVersion;
55 
56     public static final int NOT_ART = -1;
57     public static final int NOT_SPECIFIED = -2;
58 
59     /**
60      * Creates a new ClassPath instance that can load classes from the given providers
61      *
62      * @param classProviders A varargs array of ClassProviders. When loading a class, these providers will be searched
63      *                       in order
64      */
ClassPath(ClassProvider... classProviders)65     public ClassPath(ClassProvider... classProviders) throws IOException {
66         this(Arrays.asList(classProviders), false, NOT_ART);
67     }
68 
69     /**
70      * Creates a new ClassPath instance that can load classes from the given providers
71      *
72      * @param classProviders An iterable of ClassProviders. When loading a class, these providers will be searched in
73      *                       order
74      */
ClassPath(Iterable<ClassProvider> classProviders)75     public ClassPath(Iterable<ClassProvider> classProviders) throws IOException {
76         this(classProviders, false, NOT_ART);
77     }
78 
79     /**
80      * Creates a new ClassPath instance that can load classes from the given providers
81      *
82      * @param classProviders An iterable of ClassProviders. When loading a class, these providers will be searched in
83      *                       order
84      * @param checkPackagePrivateAccess Whether checkPackagePrivateAccess is needed, enabled for ONLY early API 17 by
85      *                                  default
86      * @param oatVersion The applicable oat version, or NOT_ART
87      */
ClassPath(@onnull Iterable<? extends ClassProvider> classProviders, boolean checkPackagePrivateAccess, int oatVersion)88     public ClassPath(@Nonnull Iterable<? extends ClassProvider> classProviders, boolean checkPackagePrivateAccess,
89                      int oatVersion) {
90         // add fallbacks for certain special classes that must be present
91         unknownClass = new UnknownClassProto(this);
92         loadedClasses.put(unknownClass.getType(), unknownClass);
93         this.checkPackagePrivateAccess = checkPackagePrivateAccess;
94         this.oatVersion = oatVersion;
95 
96         loadPrimitiveType("Z");
97         loadPrimitiveType("B");
98         loadPrimitiveType("S");
99         loadPrimitiveType("C");
100         loadPrimitiveType("I");
101         loadPrimitiveType("J");
102         loadPrimitiveType("F");
103         loadPrimitiveType("D");
104         loadPrimitiveType("L");
105 
106         this.classProviders = (List<ClassProvider>)IteratorUtils.toList(classProviders);
107         this.classProviders.add(getBasicClasses());
108     }
109 
loadPrimitiveType(String type)110     private void loadPrimitiveType(String type) {
111         loadedClasses.put(type, new PrimitiveProto(this, type));
112     }
113 
getBasicClasses()114     private static ClassProvider getBasicClasses() {
115         // fallbacks for some special classes that we assume are present
116         return new DexClassProvider(new ImmutableDexFile(Opcodes.getDefault(),
117             Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
118                 new ReflectionClassDef(Class.class),
119                 new ReflectionClassDef(Cloneable.class),
120                 new ReflectionClassDef(Object.class),
121                 new ReflectionClassDef(Serializable.class),
122                 new ReflectionClassDef(String.class),
123                 new ReflectionClassDef(Throwable.class))))));
124     }
125 
isArt()126     public boolean isArt() {
127         return oatVersion != NOT_ART;
128     }
129 
130     @Nonnull
getClass(@onnull CharSequence type)131     public TypeProto getClass(@Nonnull CharSequence type) {
132         return loadedClasses.get(type.toString());
133     }
134 
135     @Nonnull private LruCache<String, TypeProto> loadedClasses = new LruCache<String, TypeProto>(30000) {
136         @Override protected TypeProto create(String key) {
137             if (key.charAt(0) == '[') {
138                 return new ArrayProto(ClassPath.this, key);
139             } else {
140                 return new ClassProto(ClassPath.this, key);
141             }
142         }
143     };
144 
145     @Nonnull
getClassDef(String type)146     public ClassDef getClassDef(String type) {
147         for (ClassProvider provider: classProviders) {
148             ClassDef classDef = provider.getClassDef(type);
149             if (classDef != null) {
150                 return classDef;
151             }
152         }
153         throw new UnresolvedClassException("Could not resolve class %s", type);
154     }
155 
156     @Nonnull
getUnknownClass()157     public TypeProto getUnknownClass() {
158         return unknownClass;
159     }
160 
shouldCheckPackagePrivateAccess()161     public boolean shouldCheckPackagePrivateAccess() {
162         return checkPackagePrivateAccess;
163     }
164 
165     private final Supplier<OdexedFieldInstructionMapper> fieldInstructionMapperSupplier = MemoizingSupplier.memoize(
166             new Supplier<OdexedFieldInstructionMapper>() {
167                 @Override public OdexedFieldInstructionMapper get() {
168                     return new OdexedFieldInstructionMapper(isArt());
169                 }
170             });
171 
172     @Nonnull
getFieldInstructionMapper()173     public OdexedFieldInstructionMapper getFieldInstructionMapper() {
174         return fieldInstructionMapperSupplier.get();
175     }
176 }
177