1 /* 2 * Copyright (C) 2018 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.lang.reflect.InvocationTargetException; 18 19 public class Linking { canAccess(String className, boolean takesParameter)20 public static boolean canAccess(String className, boolean takesParameter) throws Exception { 21 try { 22 Class<?> c = Class.forName(className); 23 if (takesParameter) { 24 c.getDeclaredMethod("access", Integer.TYPE).invoke(null, 42); 25 } else { 26 c.getDeclaredMethod("access").invoke(null); 27 } 28 return true; 29 } catch (InvocationTargetException ex) { 30 if (ex.getCause() instanceof NoSuchFieldError || ex.getCause() instanceof NoSuchMethodError) { 31 return false; 32 } else { 33 throw ex; 34 } 35 } 36 } 37 } 38 39 // INSTANCE FIELD GET 40 41 class LinkFieldGetSdk { access()42 public static int access() { 43 return new ParentClass().fieldPublicSdk; 44 } 45 } 46 47 class LinkFieldGetUnsupported { access()48 public static int access() { 49 return new ParentClass().fieldPublicUnsupported; 50 } 51 } 52 53 class LinkFieldGetConditionallyBlocked { access()54 public static int access() { 55 return new ParentClass().fieldPublicConditionallyBlocked; 56 } 57 } 58 59 class LinkFieldGetBlocklist { access()60 public static int access() { 61 return new ParentClass().fieldPublicBlocklist; 62 } 63 } 64 65 class LinkFieldGetBlocklistAndCorePlatformApi { access()66 public static int access() { 67 return new ParentClass().fieldPublicBlocklistAndCorePlatformApi; 68 } 69 } 70 71 // INSTANCE FIELD SET 72 73 class LinkFieldSetSdk { access(int x)74 public static void access(int x) { 75 // Need to use a different field from the getter to bypass DexCache. 76 new ParentClass().fieldPublicSdkB = x; 77 } 78 } 79 80 class LinkFieldSetUnsupported { access(int x)81 public static void access(int x) { 82 // Need to use a different field from the getter to bypass DexCache. 83 new ParentClass().fieldPublicUnsupportedB = x; 84 } 85 } 86 87 class LinkFieldSetConditionallyBlocked { access(int x)88 public static void access(int x) { 89 // Need to use a different field from the getter to bypass DexCache. 90 new ParentClass().fieldPublicConditionallyBlockedB = x; 91 } 92 } 93 94 class LinkFieldSetBlocklist { access(int x)95 public static void access(int x) { 96 // Need to use a different field from the getter to bypass DexCache. 97 new ParentClass().fieldPublicBlocklistB = x; 98 } 99 } 100 101 class LinkFieldSetBlocklistAndCorePlatformApi { access(int x)102 public static void access(int x) { 103 // Need to use a different field from the getter to bypass DexCache. 104 new ParentClass().fieldPublicBlocklistAndCorePlatformApiB = x; 105 } 106 } 107 108 // STATIC FIELD GET 109 110 class LinkFieldGetStaticSdk { access()111 public static int access() { 112 return ParentClass.fieldPublicStaticSdk; 113 } 114 } 115 116 class LinkFieldGetStaticUnsupported { access()117 public static int access() { 118 return ParentClass.fieldPublicStaticUnsupported; 119 } 120 } 121 122 class LinkFieldGetStaticConditionallyBlocked { access()123 public static int access() { 124 return ParentClass.fieldPublicStaticConditionallyBlocked; 125 } 126 } 127 128 class LinkFieldGetStaticBlocklist { access()129 public static int access() { 130 return ParentClass.fieldPublicStaticBlocklist; 131 } 132 } 133 134 class LinkFieldGetStaticBlocklistAndCorePlatformApi { access()135 public static int access() { 136 return ParentClass.fieldPublicStaticBlocklistAndCorePlatformApi; 137 } 138 } 139 140 // STATIC FIELD SET 141 142 class LinkFieldSetStaticSdk { access(int x)143 public static void access(int x) { 144 // Need to use a different field from the getter to bypass DexCache. 145 ParentClass.fieldPublicStaticSdkB = x; 146 } 147 } 148 149 class LinkFieldSetStaticUnsupported { access(int x)150 public static void access(int x) { 151 // Need to use a different field from the getter to bypass DexCache. 152 ParentClass.fieldPublicStaticUnsupportedB = x; 153 } 154 } 155 156 class LinkFieldSetStaticConditionallyBlocked { access(int x)157 public static void access(int x) { 158 // Need to use a different field from the getter to bypass DexCache. 159 ParentClass.fieldPublicStaticConditionallyBlockedB = x; 160 } 161 } 162 163 class LinkFieldSetStaticBlocklist { access(int x)164 public static void access(int x) { 165 // Need to use a different field from the getter to bypass DexCache. 166 ParentClass.fieldPublicStaticBlocklistB = x; 167 } 168 } 169 170 class LinkFieldSetStaticBlocklistAndCorePlatformApi { access(int x)171 public static void access(int x) { 172 // Need to use a different field from the getter to bypass DexCache. 173 ParentClass.fieldPublicStaticBlocklistAndCorePlatformApiB = x; 174 } 175 } 176 177 // INVOKE INSTANCE METHOD 178 179 class LinkMethodSdk { access()180 public static int access() { 181 return new ParentClass().methodPublicSdk(); 182 } 183 } 184 185 class LinkMethodUnsupported { access()186 public static int access() { 187 return new ParentClass().methodPublicUnsupported(); 188 } 189 } 190 191 class LinkMethodConditionallyBlocked { access()192 public static int access() { 193 return new ParentClass().methodPublicConditionallyBlocked(); 194 } 195 } 196 197 class LinkMethodBlocklist { access()198 public static int access() { 199 return new ParentClass().methodPublicBlocklist(); 200 } 201 } 202 203 class LinkMethodBlocklistAndCorePlatformApi { access()204 public static int access() { 205 return new ParentClass().methodPublicBlocklistAndCorePlatformApi(); 206 } 207 } 208 209 // INVOKE INSTANCE INTERFACE METHOD 210 211 class LinkMethodInterfaceSdk { access()212 public static int access() { 213 return SampleClass.getInterfaceInstance().methodPublicSdk(); 214 } 215 } 216 217 class LinkMethodInterfaceUnsupported { access()218 public static int access() { 219 return SampleClass.getInterfaceInstance().methodPublicUnsupported(); 220 } 221 } 222 223 class LinkMethodInterfaceConditionallyBlocked { access()224 public static int access() { 225 return SampleClass.getInterfaceInstance().methodPublicConditionallyBlocked(); 226 } 227 } 228 229 class LinkMethodInterfaceBlocklist { access()230 public static int access() { 231 return SampleClass.getInterfaceInstance().methodPublicBlocklist(); 232 } 233 } 234 235 class LinkMethodInterfaceBlocklistAndCorePlatformApi { access()236 public static int access() { 237 return SampleClass.getInterfaceInstance().methodPublicBlocklistAndCorePlatformApi(); 238 } 239 } 240 241 // INVOKE STATIC METHOD 242 243 class LinkMethodStaticSdk { access()244 public static int access() { 245 return ParentClass.methodPublicStaticSdk(); 246 } 247 } 248 249 class LinkMethodStaticUnsupported { access()250 public static int access() { 251 return ParentClass.methodPublicStaticUnsupported(); 252 } 253 } 254 255 class LinkMethodStaticConditionallyBlocked { access()256 public static int access() { 257 return ParentClass.methodPublicStaticConditionallyBlocked(); 258 } 259 } 260 261 class LinkMethodStaticBlocklist { access()262 public static int access() { 263 return ParentClass.methodPublicStaticBlocklist(); 264 } 265 } 266 267 class LinkMethodStaticBlocklistAndCorePlatformApi { access()268 public static int access() { 269 return ParentClass.methodPublicStaticBlocklistAndCorePlatformApi(); 270 } 271 } 272 273 // INVOKE INTERFACE STATIC METHOD 274 275 class LinkMethodInterfaceStaticSdk { access()276 public static int access() { 277 return ParentInterface.methodPublicStaticSdk(); 278 } 279 } 280 281 class LinkMethodInterfaceStaticUnsupported { access()282 public static int access() { 283 return ParentInterface.methodPublicStaticUnsupported(); 284 } 285 } 286 287 class LinkMethodInterfaceStaticConditionallyBlocked { access()288 public static int access() { 289 return ParentInterface.methodPublicStaticConditionallyBlocked(); 290 } 291 } 292 293 class LinkMethodInterfaceStaticBlocklist { access()294 public static int access() { 295 return ParentInterface.methodPublicStaticBlocklist(); 296 } 297 } 298 299 class LinkMethodInterfaceStaticBlocklistAndCorePlatformApi { access()300 public static int access() { 301 return ParentInterface.methodPublicStaticBlocklistAndCorePlatformApi(); 302 } 303 } 304