blob: 0a403411e4802fd3818512045b3f3dc1f3662e9c [file] [log] [blame]
David Brazdil5a61bb72018-01-19 16:59:46 +00001/*
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#ifndef ART_RUNTIME_HIDDEN_API_H_
18#define ART_RUNTIME_HIDDEN_API_H_
19
David Brazdil85865692018-10-30 17:26:20 +000020#include "art_field.h"
21#include "art_method.h"
David Brazdildcfa89b2018-10-31 11:04:10 +000022#include "base/hiddenapi_flags.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080023#include "base/locks.h"
David Brazdil85865692018-10-30 17:26:20 +000024#include "intrinsics_enum.h"
David Brazdil8ce3bfa2018-03-12 18:01:18 +000025#include "mirror/class-inl.h"
David Brazdil5a61bb72018-01-19 16:59:46 +000026#include "reflection.h"
27#include "runtime.h"
28
29namespace art {
30namespace hiddenapi {
31
Mathew Inwood597d7f62018-03-22 11:36:47 +000032// Hidden API enforcement policy
33// This must be kept in sync with ApplicationInfo.ApiEnforcementPolicy in
34// frameworks/base/core/java/android/content/pm/ApplicationInfo.java
35enum class EnforcementPolicy {
David Brazdilf50ac102018-10-17 18:00:06 +010036 kDisabled = 0,
Mathew Inwooda8503d92018-04-05 16:10:25 +010037 kJustWarn = 1, // keep checks enabled, but allow everything (enables logging)
David Brazdilf50ac102018-10-17 18:00:06 +010038 kEnabled = 2, // ban dark grey & blacklist
39 kMax = kEnabled,
Mathew Inwood597d7f62018-03-22 11:36:47 +000040};
41
42inline EnforcementPolicy EnforcementPolicyFromInt(int api_policy_int) {
43 DCHECK_GE(api_policy_int, 0);
44 DCHECK_LE(api_policy_int, static_cast<int>(EnforcementPolicy::kMax));
45 return static_cast<EnforcementPolicy>(api_policy_int);
46}
47
Andrei Oneaa2d2bc22019-01-25 16:18:53 +000048// Hidden API access method
49// Thist must be kept in sync with VMRuntime.HiddenApiUsageLogger.ACCESS_METHOD_*
David Brazdilf50ac102018-10-17 18:00:06 +010050enum class AccessMethod {
Andrei Oneaa2d2bc22019-01-25 16:18:53 +000051 kNone = 0, // internal test that does not correspond to an actual access by app
52 kReflection = 1,
53 kJNI = 2,
54 kLinking = 3,
Mathew Inwood1fd97f22018-04-03 15:32:32 +010055};
56
David Brazdile7681822018-12-14 16:25:33 +000057// Represents the API domain of a caller/callee.
58class AccessContext {
David Brazdilf50ac102018-10-17 18:00:06 +010059 public:
David Brazdile7681822018-12-14 16:25:33 +000060 // Initialize to either the fully-trusted or fully-untrusted domain.
61 explicit AccessContext(bool is_trusted)
62 : klass_(nullptr),
63 dex_file_(nullptr),
64 domain_(ComputeDomain(is_trusted)) {}
David Brazdilf50ac102018-10-17 18:00:06 +010065
David Brazdile7681822018-12-14 16:25:33 +000066 // Initialize from class loader and dex file (via dex cache).
David Brazdilf50ac102018-10-17 18:00:06 +010067 AccessContext(ObjPtr<mirror::ClassLoader> class_loader, ObjPtr<mirror::DexCache> dex_cache)
David Brazdile7681822018-12-14 16:25:33 +000068 REQUIRES_SHARED(Locks::mutator_lock_)
69 : klass_(nullptr),
70 dex_file_(GetDexFileFromDexCache(dex_cache)),
71 domain_(ComputeDomain(class_loader, dex_file_)) {}
David Brazdilf50ac102018-10-17 18:00:06 +010072
David Brazdile7681822018-12-14 16:25:33 +000073 // Initialize from Class.
74 explicit AccessContext(ObjPtr<mirror::Class> klass)
75 REQUIRES_SHARED(Locks::mutator_lock_)
76 : klass_(klass),
77 dex_file_(GetDexFileFromDexCache(klass->GetDexCache())),
78 domain_(ComputeDomain(klass, dex_file_)) {}
79
80 ObjPtr<mirror::Class> GetClass() const { return klass_; }
81 const DexFile* GetDexFile() const { return dex_file_; }
82 Domain GetDomain() const { return domain_; }
83
84 bool IsUntrustedDomain() const { return domain_ == Domain::kApplication; }
85
86 // Returns true if this domain is always allowed to access the domain of `callee`.
87 bool CanAlwaysAccess(const AccessContext& callee) const {
88 return IsDomainMoreTrustedThan(domain_, callee.domain_);
89 }
David Brazdilf50ac102018-10-17 18:00:06 +010090
91 private:
David Brazdile7681822018-12-14 16:25:33 +000092 static const DexFile* GetDexFileFromDexCache(ObjPtr<mirror::DexCache> dex_cache)
David Brazdilf50ac102018-10-17 18:00:06 +010093 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdile7681822018-12-14 16:25:33 +000094 return dex_cache.IsNull() ? nullptr : dex_cache->GetDexFile();
David Brazdilf50ac102018-10-17 18:00:06 +010095 }
96
David Brazdile7681822018-12-14 16:25:33 +000097 static Domain ComputeDomain(bool is_trusted) {
98 return is_trusted ? Domain::kCorePlatform : Domain::kApplication;
99 }
David Brazdilf50ac102018-10-17 18:00:06 +0100100
David Brazdile7681822018-12-14 16:25:33 +0000101 static Domain ComputeDomain(ObjPtr<mirror::ClassLoader> class_loader, const DexFile* dex_file)
102 REQUIRES_SHARED(Locks::mutator_lock_) {
103 if (dex_file == nullptr) {
104 return ComputeDomain(/* is_trusted= */ class_loader.IsNull());
David Brazdilf50ac102018-10-17 18:00:06 +0100105 }
106
David Brazdile7681822018-12-14 16:25:33 +0000107 Domain dex_domain = dex_file->GetHiddenapiDomain();
108 if (class_loader.IsNull() && dex_domain == Domain::kApplication) {
David Brazdile7e26d12019-02-28 15:04:14 +0000109 LOG(WARNING) << "DexFile " << dex_file->GetLocation()
110 << " is in boot classpath but is assigned the application domain";
111 dex_file->SetHiddenapiDomain(Domain::kPlatform);
David Brazdile7681822018-12-14 16:25:33 +0000112 dex_domain = Domain::kPlatform;
113 }
114 return dex_domain;
115 }
116
117 static Domain ComputeDomain(ObjPtr<mirror::Class> klass, const DexFile* dex_file)
118 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdilf50ac102018-10-17 18:00:06 +0100119 // Check other aspects of the context.
David Brazdile7681822018-12-14 16:25:33 +0000120 Domain domain = ComputeDomain(klass->GetClassLoader(), dex_file);
121
122 if (domain == Domain::kApplication &&
123 klass->ShouldSkipHiddenApiChecks() &&
124 Runtime::Current()->IsJavaDebuggable()) {
125 // Class is known, it is marked trusted and we are in debuggable mode.
126 domain = ComputeDomain(/* is_trusted= */ true);
127 }
128
129 return domain;
David Brazdilf50ac102018-10-17 18:00:06 +0100130 }
131
David Brazdile7681822018-12-14 16:25:33 +0000132 // Pointer to declaring class of the caller/callee (null if not provided).
133 // This is not safe across GC but we're only using this class for passing
134 // information about the caller to the access check logic and never retain
135 // the AccessContext instance beyond that.
136 const ObjPtr<mirror::Class> klass_;
137
138 // DexFile of the caller/callee (null if not provided).
139 const DexFile* const dex_file_;
140
141 // Computed domain of the caller/callee.
142 const Domain domain_;
David Brazdil068d68d2018-02-12 13:04:17 -0800143};
144
David Brazdil32bde992018-05-14 15:24:34 +0100145class ScopedHiddenApiEnforcementPolicySetting {
146 public:
147 explicit ScopedHiddenApiEnforcementPolicySetting(EnforcementPolicy new_policy)
148 : initial_policy_(Runtime::Current()->GetHiddenApiEnforcementPolicy()) {
149 Runtime::Current()->SetHiddenApiEnforcementPolicy(new_policy);
150 }
151
152 ~ScopedHiddenApiEnforcementPolicySetting() {
153 Runtime::Current()->SetHiddenApiEnforcementPolicy(initial_policy_);
154 }
155
156 private:
157 const EnforcementPolicy initial_policy_;
158 DISALLOW_COPY_AND_ASSIGN(ScopedHiddenApiEnforcementPolicySetting);
159};
160
Andreas Gampeaa120012018-03-28 16:23:24 -0700161// Implementation details. DO NOT ACCESS DIRECTLY.
162namespace detail {
David Brazdilee7d2fd2018-01-20 17:25:23 +0000163
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000164// Class to encapsulate the signature of a member (ArtField or ArtMethod). This
165// is used as a helper when matching prefixes, and when logging the signature.
166class MemberSignature {
167 private:
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100168 enum MemberType {
169 kField,
170 kMethod,
171 };
172
173 std::string class_name_;
174 std::string member_name_;
175 std::string type_signature_;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000176 std::string tmp_;
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100177 MemberType type_;
178
179 inline std::vector<const char*> GetSignatureParts() const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000180
181 public:
Andreas Gampeaa120012018-03-28 16:23:24 -0700182 explicit MemberSignature(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_);
183 explicit MemberSignature(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil1a658632018-12-01 17:54:26 +0000184 explicit MemberSignature(const ClassAccessor::Field& field);
185 explicit MemberSignature(const ClassAccessor::Method& method);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000186
Andreas Gampeaa120012018-03-28 16:23:24 -0700187 void Dump(std::ostream& os) const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000188
David Brazdil1a658632018-12-01 17:54:26 +0000189 bool Equals(const MemberSignature& other);
190 bool MemberNameAndTypeMatch(const MemberSignature& other);
191
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000192 // Performs prefix match on this member. Since the full member signature is
193 // composed of several parts, we match each part in turn (rather than
194 // building the entire thing in memory and performing a simple prefix match)
Andreas Gampeaa120012018-03-28 16:23:24 -0700195 bool DoesPrefixMatch(const std::string& prefix) const;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000196
Andreas Gampeaa120012018-03-28 16:23:24 -0700197 bool IsExempted(const std::vector<std::string>& exemptions);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000198
David Brazdil47cd2722018-10-23 12:50:02 +0100199 void WarnAboutAccess(AccessMethod access_method, ApiList list);
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100200
Andrei Onea6ad020d2019-02-18 12:15:51 +0000201 void LogAccessToEventLog(uint32_t sampled_value, AccessMethod access_method, bool access_denied);
David Brazdilf50ac102018-10-17 18:00:06 +0100202
203 // Calls back into managed code to notify VMRuntime.nonSdkApiUsageConsumer that
204 // |member| was accessed. This is usually called when an API is on the black,
205 // dark grey or light grey lists. Given that the callback can execute arbitrary
206 // code, a call to this method can result in thread suspension.
207 void NotifyHiddenApiListener(AccessMethod access_method);
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000208};
David Brazdilee7d2fd2018-01-20 17:25:23 +0000209
David Brazdil85865692018-10-30 17:26:20 +0000210// Locates hiddenapi flags for `field` in the corresponding dex file.
211// NB: This is an O(N) operation, linear with the number of members in the class def.
David Brazdil1a658632018-12-01 17:54:26 +0000212template<typename T>
213uint32_t GetDexFlags(T* member) REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil85865692018-10-30 17:26:20 +0000214
Andreas Gampeaa120012018-03-28 16:23:24 -0700215template<typename T>
David Brazdile7681822018-12-14 16:25:33 +0000216void MaybeReportCorePlatformApiViolation(T* member,
217 const AccessContext& caller_context,
218 AccessMethod access_method)
219 REQUIRES_SHARED(Locks::mutator_lock_);
220
221template<typename T>
David Brazdilf50ac102018-10-17 18:00:06 +0100222bool ShouldDenyAccessToMemberImpl(T* member, ApiList api_list, AccessMethod access_method)
Andreas Gampeaa120012018-03-28 16:23:24 -0700223 REQUIRES_SHARED(Locks::mutator_lock_);
224
David Brazdil6a1dab42019-02-28 18:45:15 +0000225inline ArtField* GetInterfaceMemberIfProxy(ArtField* field) { return field; }
226
227inline ArtMethod* GetInterfaceMemberIfProxy(ArtMethod* method)
228 REQUIRES_SHARED(Locks::mutator_lock_) {
229 return method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
230}
231
Andreas Gampeaa120012018-03-28 16:23:24 -0700232} // namespace detail
233
David Brazdil85865692018-10-30 17:26:20 +0000234// Returns access flags for the runtime representation of a class member (ArtField/ArtMember).
235ALWAYS_INLINE inline uint32_t CreateRuntimeFlags(const ClassAccessor::BaseItem& member) {
236 uint32_t runtime_flags = 0u;
237
David Brazdil90faceb2018-12-14 14:36:15 +0000238 ApiList api_list(member.GetHiddenapiFlags());
239 DCHECK(api_list.IsValid());
David Brazdil85865692018-10-30 17:26:20 +0000240
David Brazdil90faceb2018-12-14 14:36:15 +0000241 if (api_list.Contains(ApiList::Whitelist())) {
David Brazdil85865692018-10-30 17:26:20 +0000242 runtime_flags |= kAccPublicApi;
David Brazdil90faceb2018-12-14 14:36:15 +0000243 } else {
244 // Only add domain-specific flags for non-public API members.
245 // This simplifies hardcoded values for intrinsics.
246 if (api_list.Contains(ApiList::CorePlatformApi())) {
247 runtime_flags |= kAccCorePlatformApi;
248 }
David Brazdil85865692018-10-30 17:26:20 +0000249 }
250
251 DCHECK_EQ(runtime_flags & kAccHiddenapiBits, runtime_flags)
252 << "Runtime flags not in reserved access flags bits";
253 return runtime_flags;
254}
255
256// Extracts hiddenapi runtime flags from access flags of ArtField.
257ALWAYS_INLINE inline uint32_t GetRuntimeFlags(ArtField* field)
258 REQUIRES_SHARED(Locks::mutator_lock_) {
259 return field->GetAccessFlags() & kAccHiddenapiBits;
260}
261
262// Extracts hiddenapi runtime flags from access flags of ArtMethod.
263// Uses hardcoded values for intrinsics.
264ALWAYS_INLINE inline uint32_t GetRuntimeFlags(ArtMethod* method)
265 REQUIRES_SHARED(Locks::mutator_lock_) {
266 if (UNLIKELY(method->IsIntrinsic())) {
267 switch (static_cast<Intrinsics>(method->GetIntrinsic())) {
268 case Intrinsics::kSystemArrayCopyChar:
269 case Intrinsics::kStringGetCharsNoCheck:
270 case Intrinsics::kReferenceGetReferent:
271 case Intrinsics::kMemoryPeekByte:
272 case Intrinsics::kMemoryPokeByte:
273 case Intrinsics::kUnsafeCASInt:
274 case Intrinsics::kUnsafeCASLong:
275 case Intrinsics::kUnsafeCASObject:
276 case Intrinsics::kUnsafeGet:
277 case Intrinsics::kUnsafeGetAndAddInt:
278 case Intrinsics::kUnsafeGetAndAddLong:
279 case Intrinsics::kUnsafeGetAndSetInt:
280 case Intrinsics::kUnsafeGetAndSetLong:
281 case Intrinsics::kUnsafeGetAndSetObject:
282 case Intrinsics::kUnsafeGetLong:
283 case Intrinsics::kUnsafeGetLongVolatile:
284 case Intrinsics::kUnsafeGetObject:
285 case Intrinsics::kUnsafeGetObjectVolatile:
286 case Intrinsics::kUnsafeGetVolatile:
287 case Intrinsics::kUnsafePut:
288 case Intrinsics::kUnsafePutLong:
289 case Intrinsics::kUnsafePutLongOrdered:
290 case Intrinsics::kUnsafePutLongVolatile:
291 case Intrinsics::kUnsafePutObject:
292 case Intrinsics::kUnsafePutObjectOrdered:
293 case Intrinsics::kUnsafePutObjectVolatile:
294 case Intrinsics::kUnsafePutOrdered:
295 case Intrinsics::kUnsafePutVolatile:
296 case Intrinsics::kUnsafeLoadFence:
297 case Intrinsics::kUnsafeStoreFence:
298 case Intrinsics::kUnsafeFullFence:
299 case Intrinsics::kCRC32Update:
Evgeny Astigeevich15c5b972018-11-20 13:41:40 +0000300 case Intrinsics::kCRC32UpdateBytes:
Evgeny Astigeevich776a7c22018-12-17 11:40:34 +0000301 case Intrinsics::kCRC32UpdateByteBuffer:
David Brazdil85865692018-10-30 17:26:20 +0000302 case Intrinsics::kStringNewStringFromBytes:
303 case Intrinsics::kStringNewStringFromChars:
304 case Intrinsics::kStringNewStringFromString:
305 case Intrinsics::kMemoryPeekIntNative:
306 case Intrinsics::kMemoryPeekLongNative:
307 case Intrinsics::kMemoryPeekShortNative:
308 case Intrinsics::kMemoryPokeIntNative:
309 case Intrinsics::kMemoryPokeLongNative:
310 case Intrinsics::kMemoryPokeShortNative:
311 case Intrinsics::kVarHandleFullFence:
312 case Intrinsics::kVarHandleAcquireFence:
313 case Intrinsics::kVarHandleReleaseFence:
314 case Intrinsics::kVarHandleLoadLoadFence:
315 case Intrinsics::kVarHandleStoreStoreFence:
316 case Intrinsics::kVarHandleCompareAndExchange:
317 case Intrinsics::kVarHandleCompareAndExchangeAcquire:
318 case Intrinsics::kVarHandleCompareAndExchangeRelease:
319 case Intrinsics::kVarHandleCompareAndSet:
320 case Intrinsics::kVarHandleGet:
321 case Intrinsics::kVarHandleGetAcquire:
322 case Intrinsics::kVarHandleGetAndAdd:
323 case Intrinsics::kVarHandleGetAndAddAcquire:
324 case Intrinsics::kVarHandleGetAndAddRelease:
325 case Intrinsics::kVarHandleGetAndBitwiseAnd:
326 case Intrinsics::kVarHandleGetAndBitwiseAndAcquire:
327 case Intrinsics::kVarHandleGetAndBitwiseAndRelease:
328 case Intrinsics::kVarHandleGetAndBitwiseOr:
329 case Intrinsics::kVarHandleGetAndBitwiseOrAcquire:
330 case Intrinsics::kVarHandleGetAndBitwiseOrRelease:
331 case Intrinsics::kVarHandleGetAndBitwiseXor:
332 case Intrinsics::kVarHandleGetAndBitwiseXorAcquire:
333 case Intrinsics::kVarHandleGetAndBitwiseXorRelease:
334 case Intrinsics::kVarHandleGetAndSet:
335 case Intrinsics::kVarHandleGetAndSetAcquire:
336 case Intrinsics::kVarHandleGetAndSetRelease:
337 case Intrinsics::kVarHandleGetOpaque:
338 case Intrinsics::kVarHandleGetVolatile:
339 case Intrinsics::kVarHandleSet:
340 case Intrinsics::kVarHandleSetOpaque:
341 case Intrinsics::kVarHandleSetRelease:
342 case Intrinsics::kVarHandleSetVolatile:
343 case Intrinsics::kVarHandleWeakCompareAndSet:
344 case Intrinsics::kVarHandleWeakCompareAndSetAcquire:
345 case Intrinsics::kVarHandleWeakCompareAndSetPlain:
346 case Intrinsics::kVarHandleWeakCompareAndSetRelease:
347 return 0u;
348 default:
349 // Remaining intrinsics are public API. We DCHECK that in SetIntrinsic().
350 return kAccPublicApi;
351 }
352 } else {
353 return method->GetAccessFlags() & kAccHiddenapiBits;
354 }
355}
356
David Brazdilf50ac102018-10-17 18:00:06 +0100357// Returns true if access to `member` should be denied in the given context.
358// The decision is based on whether the caller is in a trusted context or not.
359// Because determining the access context can be expensive, a lambda function
360// "fn_get_access_context" is lazily invoked after other criteria have been
361// considered.
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000362// This function might print warnings into the log if the member is hidden.
David Brazdilee7d2fd2018-01-20 17:25:23 +0000363template<typename T>
David Brazdilf50ac102018-10-17 18:00:06 +0100364inline bool ShouldDenyAccessToMember(T* member,
David Brazdil4bcd6572019-02-02 20:08:44 +0000365 const std::function<AccessContext()>& fn_get_access_context,
David Brazdilf50ac102018-10-17 18:00:06 +0100366 AccessMethod access_method)
David Brazdilee7d2fd2018-01-20 17:25:23 +0000367 REQUIRES_SHARED(Locks::mutator_lock_) {
368 DCHECK(member != nullptr);
David Brazdil6a1dab42019-02-28 18:45:15 +0000369
370 // Get the runtime flags encoded in member's access flags.
371 // Note: this works for proxy methods because they inherit access flags from their
372 // respective interface methods.
David Brazdile7681822018-12-14 16:25:33 +0000373 const uint32_t runtime_flags = GetRuntimeFlags(member);
David Brazdilee7d2fd2018-01-20 17:25:23 +0000374
David Brazdil85865692018-10-30 17:26:20 +0000375 // Exit early if member is public API. This flag is also set for non-boot class
376 // path fields/methods.
David Brazdile7681822018-12-14 16:25:33 +0000377 if ((runtime_flags & kAccPublicApi) != 0) {
David Brazdilf50ac102018-10-17 18:00:06 +0100378 return false;
David Brazdila02cb112018-01-31 11:36:39 +0000379 }
380
David Brazdile7681822018-12-14 16:25:33 +0000381 // Determine which domain the caller and callee belong to.
382 // This can be *very* expensive. This is why ShouldDenyAccessToMember
383 // should not be called on every individual access.
384 const AccessContext caller_context = fn_get_access_context();
385 const AccessContext callee_context(member->GetDeclaringClass());
386
387 // Non-boot classpath callers should have exited early.
388 DCHECK(!callee_context.IsUntrustedDomain());
389
390 // Check if the caller is always allowed to access members in the callee context.
391 if (caller_context.CanAlwaysAccess(callee_context)) {
David Brazdilc5a96e42019-01-09 10:04:45 +0000392 return false;
393 }
394
David Brazdile7681822018-12-14 16:25:33 +0000395 // Check if this is platform accessing core platform. We may warn if `member` is
396 // not part of core platform API.
397 switch (caller_context.GetDomain()) {
398 case Domain::kApplication: {
399 DCHECK(!callee_context.IsUntrustedDomain());
400
401 // Exit early if access checks are completely disabled.
402 EnforcementPolicy policy = Runtime::Current()->GetHiddenApiEnforcementPolicy();
403 if (policy == EnforcementPolicy::kDisabled) {
404 return false;
405 }
406
David Brazdil6a1dab42019-02-28 18:45:15 +0000407 // If this is a proxy method, look at the interface method instead.
408 member = detail::GetInterfaceMemberIfProxy(member);
409
David Brazdile7681822018-12-14 16:25:33 +0000410 // Decode hidden API access flags from the dex file.
411 // This is an O(N) operation scaling with the number of fields/methods
412 // in the class. Only do this on slow path and only do it once.
413 ApiList api_list(detail::GetDexFlags(member));
414 DCHECK(api_list.IsValid());
415
416 // Member is hidden and caller is not exempted. Enter slow path.
417 return detail::ShouldDenyAccessToMemberImpl(member, api_list, access_method);
418 }
419
420 case Domain::kPlatform: {
421 DCHECK(callee_context.GetDomain() == Domain::kCorePlatform);
422
423 // Member is part of core platform API. Accessing it is allowed.
424 if ((runtime_flags & kAccCorePlatformApi) != 0) {
425 return false;
426 }
427
428 // Allow access if access checks are disabled.
429 EnforcementPolicy policy = Runtime::Current()->GetCorePlatformApiEnforcementPolicy();
430 if (policy == EnforcementPolicy::kDisabled) {
431 return false;
432 }
433
David Brazdil6a1dab42019-02-28 18:45:15 +0000434 // If this is a proxy method, look at the interface method instead.
435 member = detail::GetInterfaceMemberIfProxy(member);
436
David Brazdile7681822018-12-14 16:25:33 +0000437 // Access checks are not disabled, report the violation.
David Brazdila6d61842019-01-22 22:44:11 +0000438 // detail::MaybeReportCorePlatformApiViolation(member, caller_context, access_method);
David Brazdile7681822018-12-14 16:25:33 +0000439
440 // Deny access if the policy is enabled.
441 return policy == EnforcementPolicy::kEnabled;
442 }
443
444 case Domain::kCorePlatform: {
445 LOG(FATAL) << "CorePlatform domain should be allowed to access all domains";
446 UNREACHABLE();
447 }
David Brazdila02cb112018-01-31 11:36:39 +0000448 }
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000449}
450
David Brazdilf50ac102018-10-17 18:00:06 +0100451// Helper method for callers where access context can be determined beforehand.
452// Wraps AccessContext in a lambda and passes it to the real ShouldDenyAccessToMember.
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000453template<typename T>
David Brazdilf50ac102018-10-17 18:00:06 +0100454inline bool ShouldDenyAccessToMember(T* member,
David Brazdile7681822018-12-14 16:25:33 +0000455 const AccessContext& access_context,
David Brazdilf50ac102018-10-17 18:00:06 +0100456 AccessMethod access_method)
David Brazdilee7d2fd2018-01-20 17:25:23 +0000457 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdile7681822018-12-14 16:25:33 +0000458 return ShouldDenyAccessToMember(member, [&]() { return access_context; }, access_method);
David Brazdilee7d2fd2018-01-20 17:25:23 +0000459}
460
David Brazdil5a61bb72018-01-19 16:59:46 +0000461} // namespace hiddenapi
462} // namespace art
463
464#endif // ART_RUNTIME_HIDDEN_API_H_