blob: af5e67a4614945150f2e92a6216c0924f694700c [file] [log] [blame]
Andreas Gampe80f5fe52018-03-28 16:23:24 -07001/*
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#include "hidden_api.h"
18
Narayan Kamathe453a8d2018-04-03 15:23:46 +010019#include <nativehelper/scoped_local_ref.h>
20
David Brazdil85865692018-10-30 17:26:20 +000021#include "art_field-inl.h"
22#include "art_method-inl.h"
Andreas Gampe80f5fe52018-03-28 16:23:24 -070023#include "base/dumpable.h"
David Brazdil1a658632018-12-01 17:54:26 +000024#include "class_root.h"
David Brazdil85865692018-10-30 17:26:20 +000025#include "dex/class_accessor-inl.h"
David Brazdil1a658632018-12-01 17:54:26 +000026#include "dex/dex_file_loader.h"
27#include "mirror/class_ext.h"
David Brazdil85865692018-10-30 17:26:20 +000028#include "scoped_thread_state_change.h"
29#include "thread-inl.h"
Narayan Kamathe453a8d2018-04-03 15:23:46 +010030#include "well_known_classes.h"
Andreas Gampe80f5fe52018-03-28 16:23:24 -070031
Nicolas Geoffray8a229072018-05-10 16:34:14 +010032#ifdef ART_TARGET_ANDROID
33#include <metricslogger/metrics_logger.h>
Mathew Inwood2d4d62f2018-04-12 13:56:37 +010034using android::metricslogger::ComplexEventLogger;
35using android::metricslogger::ACTION_HIDDEN_API_ACCESSED;
36using android::metricslogger::FIELD_HIDDEN_API_ACCESS_METHOD;
37using android::metricslogger::FIELD_HIDDEN_API_ACCESS_DENIED;
38using android::metricslogger::FIELD_HIDDEN_API_SIGNATURE;
Nicolas Geoffray8a229072018-05-10 16:34:14 +010039#endif
Mathew Inwood2d4d62f2018-04-12 13:56:37 +010040
Andreas Gampe80f5fe52018-03-28 16:23:24 -070041namespace art {
42namespace hiddenapi {
43
Mathew Inwood27199e62018-04-11 16:08:21 +010044// Set to true if we should always print a warning in logcat for all hidden API accesses, not just
45// dark grey and black. This can be set to true for developer preview / beta builds, but should be
46// false for public release builds.
Mathew Inwood6d6012e2018-04-12 15:43:11 +010047// Note that when flipping this flag, you must also update the expectations of test 674-hiddenapi
48// as it affects whether or not we warn for light grey APIs that have been added to the exemptions
49// list.
Mathew Inwood015a7ec2018-05-16 11:18:10 +010050static constexpr bool kLogAllAccesses = false;
Mathew Inwood27199e62018-04-11 16:08:21 +010051
Andreas Gampe80f5fe52018-03-28 16:23:24 -070052static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) {
53 switch (value) {
David Brazdilf50ac102018-10-17 18:00:06 +010054 case AccessMethod::kNone:
David Brazdil54a99cf2018-04-05 16:57:32 +010055 LOG(FATAL) << "Internal access to hidden API should not be logged";
56 UNREACHABLE();
David Brazdilf50ac102018-10-17 18:00:06 +010057 case AccessMethod::kReflection:
Andreas Gampe80f5fe52018-03-28 16:23:24 -070058 os << "reflection";
59 break;
David Brazdilf50ac102018-10-17 18:00:06 +010060 case AccessMethod::kJNI:
Andreas Gampe80f5fe52018-03-28 16:23:24 -070061 os << "JNI";
62 break;
David Brazdilf50ac102018-10-17 18:00:06 +010063 case AccessMethod::kLinking:
Andreas Gampe80f5fe52018-03-28 16:23:24 -070064 os << "linking";
65 break;
66 }
67 return os;
68}
69
David Brazdile7681822018-12-14 16:25:33 +000070static inline std::ostream& operator<<(std::ostream& os, const AccessContext& value)
71 REQUIRES_SHARED(Locks::mutator_lock_) {
72 if (!value.GetClass().IsNull()) {
73 std::string tmp;
74 os << value.GetClass()->GetDescriptor(&tmp);
75 } else if (value.GetDexFile() != nullptr) {
76 os << value.GetDexFile()->GetLocation();
77 } else {
78 os << "<unknown_caller>";
79 }
80 return os;
81}
82
Andreas Gampe80f5fe52018-03-28 16:23:24 -070083namespace detail {
84
David Brazdilf50ac102018-10-17 18:00:06 +010085// Do not change the values of items in this enum, as they are written to the
86// event log for offline analysis. Any changes will interfere with that analysis.
87enum AccessContextFlags {
88 // Accessed member is a field if this bit is set, else a method
89 kMemberIsField = 1 << 0,
90 // Indicates if access was denied to the member, instead of just printing a warning.
91 kAccessDenied = 1 << 1,
92};
93
Andreas Gampe80f5fe52018-03-28 16:23:24 -070094MemberSignature::MemberSignature(ArtField* field) {
Mathew Inwood73ddda42018-04-03 15:32:32 +010095 class_name_ = field->GetDeclaringClass()->GetDescriptor(&tmp_);
96 member_name_ = field->GetName();
97 type_signature_ = field->GetTypeDescriptor();
98 type_ = kField;
Andreas Gampe80f5fe52018-03-28 16:23:24 -070099}
100
101MemberSignature::MemberSignature(ArtMethod* method) {
David Brazdil73a64f62018-05-02 16:53:06 +0100102 // If this is a proxy method, print the signature of the interface method.
103 method = method->GetInterfaceMethodIfProxy(
104 Runtime::Current()->GetClassLinker()->GetImagePointerSize());
105
Mathew Inwood73ddda42018-04-03 15:32:32 +0100106 class_name_ = method->GetDeclaringClass()->GetDescriptor(&tmp_);
107 member_name_ = method->GetName();
108 type_signature_ = method->GetSignature().ToString();
109 type_ = kMethod;
110}
111
David Brazdil1a658632018-12-01 17:54:26 +0000112MemberSignature::MemberSignature(const ClassAccessor::Field& field) {
113 const DexFile& dex_file = field.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800114 const dex::FieldId& field_id = dex_file.GetFieldId(field.GetIndex());
David Brazdil1a658632018-12-01 17:54:26 +0000115 class_name_ = dex_file.GetFieldDeclaringClassDescriptor(field_id);
116 member_name_ = dex_file.GetFieldName(field_id);
117 type_signature_ = dex_file.GetFieldTypeDescriptor(field_id);
118 type_ = kField;
119}
120
121MemberSignature::MemberSignature(const ClassAccessor::Method& method) {
122 const DexFile& dex_file = method.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800123 const dex::MethodId& method_id = dex_file.GetMethodId(method.GetIndex());
David Brazdil1a658632018-12-01 17:54:26 +0000124 class_name_ = dex_file.GetMethodDeclaringClassDescriptor(method_id);
125 member_name_ = dex_file.GetMethodName(method_id);
126 type_signature_ = dex_file.GetMethodSignature(method_id).ToString();
127 type_ = kMethod;
128}
129
Mathew Inwood73ddda42018-04-03 15:32:32 +0100130inline std::vector<const char*> MemberSignature::GetSignatureParts() const {
131 if (type_ == kField) {
132 return { class_name_.c_str(), "->", member_name_.c_str(), ":", type_signature_.c_str() };
133 } else {
134 DCHECK_EQ(type_, kMethod);
135 return { class_name_.c_str(), "->", member_name_.c_str(), type_signature_.c_str() };
136 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700137}
138
139bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const {
140 size_t pos = 0;
Mathew Inwood73ddda42018-04-03 15:32:32 +0100141 for (const char* part : GetSignatureParts()) {
142 size_t count = std::min(prefix.length() - pos, strlen(part));
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700143 if (prefix.compare(pos, count, part, 0, count) == 0) {
144 pos += count;
145 } else {
146 return false;
147 }
148 }
149 // We have a complete match if all parts match (we exit the loop without
150 // returning) AND we've matched the whole prefix.
151 return pos == prefix.length();
152}
153
154bool MemberSignature::IsExempted(const std::vector<std::string>& exemptions) {
155 for (const std::string& exemption : exemptions) {
156 if (DoesPrefixMatch(exemption)) {
157 return true;
158 }
159 }
160 return false;
161}
162
163void MemberSignature::Dump(std::ostream& os) const {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100164 for (const char* part : GetSignatureParts()) {
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700165 os << part;
166 }
167}
168
David Brazdil47cd2722018-10-23 12:50:02 +0100169void MemberSignature::WarnAboutAccess(AccessMethod access_method, hiddenapi::ApiList list) {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100170 LOG(WARNING) << "Accessing hidden " << (type_ == kField ? "field " : "method ")
171 << Dumpable<MemberSignature>(*this) << " (" << list << ", " << access_method << ")";
172}
David Brazdilf50ac102018-10-17 18:00:06 +0100173
David Brazdil1a658632018-12-01 17:54:26 +0000174bool MemberSignature::Equals(const MemberSignature& other) {
175 return type_ == other.type_ &&
176 class_name_ == other.class_name_ &&
177 member_name_ == other.member_name_ &&
178 type_signature_ == other.type_signature_;
179}
180
181bool MemberSignature::MemberNameAndTypeMatch(const MemberSignature& other) {
182 return member_name_ == other.member_name_ && type_signature_ == other.type_signature_;
183}
184
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100185#ifdef ART_TARGET_ANDROID
Mathew Inwood2d4d62f2018-04-12 13:56:37 +0100186// Convert an AccessMethod enum to a value for logging from the proto enum.
187// This method may look odd (the enum values are current the same), but it
188// prevents coupling the internal enum to the proto enum (which should never
189// be changed) so that we are free to change the internal one if necessary in
190// future.
191inline static int32_t GetEnumValueForLog(AccessMethod access_method) {
192 switch (access_method) {
David Brazdilf50ac102018-10-17 18:00:06 +0100193 case AccessMethod::kNone:
Mathew Inwood2d4d62f2018-04-12 13:56:37 +0100194 return android::metricslogger::ACCESS_METHOD_NONE;
David Brazdilf50ac102018-10-17 18:00:06 +0100195 case AccessMethod::kReflection:
Mathew Inwood2d4d62f2018-04-12 13:56:37 +0100196 return android::metricslogger::ACCESS_METHOD_REFLECTION;
David Brazdilf50ac102018-10-17 18:00:06 +0100197 case AccessMethod::kJNI:
Mathew Inwood2d4d62f2018-04-12 13:56:37 +0100198 return android::metricslogger::ACCESS_METHOD_JNI;
David Brazdilf50ac102018-10-17 18:00:06 +0100199 case AccessMethod::kLinking:
Mathew Inwood2d4d62f2018-04-12 13:56:37 +0100200 return android::metricslogger::ACCESS_METHOD_LINKING;
201 default:
202 DCHECK(false);
203 }
204}
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100205#endif
Mathew Inwood73ddda42018-04-03 15:32:32 +0100206
David Brazdilf50ac102018-10-17 18:00:06 +0100207void MemberSignature::LogAccessToEventLog(AccessMethod access_method, bool access_denied) {
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100208#ifdef ART_TARGET_ANDROID
David Brazdilf50ac102018-10-17 18:00:06 +0100209 if (access_method == AccessMethod::kLinking || access_method == AccessMethod::kNone) {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100210 // Linking warnings come from static analysis/compilation of the bytecode
211 // and can contain false positives (i.e. code that is never run). We choose
212 // not to log these in the event log.
Mathew Inwoodf59ca612018-05-03 11:30:01 +0100213 // None does not correspond to actual access, so should also be ignored.
Mathew Inwood73ddda42018-04-03 15:32:32 +0100214 return;
215 }
Mathew Inwood2d4d62f2018-04-12 13:56:37 +0100216 ComplexEventLogger log_maker(ACTION_HIDDEN_API_ACCESSED);
217 log_maker.AddTaggedData(FIELD_HIDDEN_API_ACCESS_METHOD, GetEnumValueForLog(access_method));
David Brazdilf50ac102018-10-17 18:00:06 +0100218 if (access_denied) {
Mathew Inwood2d4d62f2018-04-12 13:56:37 +0100219 log_maker.AddTaggedData(FIELD_HIDDEN_API_ACCESS_DENIED, 1);
Mathew Inwood73ddda42018-04-03 15:32:32 +0100220 }
Mathew Inwood5bcef172018-05-01 14:40:12 +0100221 const std::string& package_name = Runtime::Current()->GetProcessPackageName();
222 if (!package_name.empty()) {
223 log_maker.SetPackageName(package_name);
224 }
Mathew Inwood2d4d62f2018-04-12 13:56:37 +0100225 std::ostringstream signature_str;
226 Dump(signature_str);
227 log_maker.AddTaggedData(FIELD_HIDDEN_API_SIGNATURE, signature_str.str());
228 log_maker.Record();
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100229#else
230 UNUSED(access_method);
David Brazdilf50ac102018-10-17 18:00:06 +0100231 UNUSED(access_denied);
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100232#endif
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700233}
234
David Brazdilf50ac102018-10-17 18:00:06 +0100235void MemberSignature::NotifyHiddenApiListener(AccessMethod access_method) {
236 if (access_method != AccessMethod::kReflection && access_method != AccessMethod::kJNI) {
237 // We can only up-call into Java during reflection and JNI down-calls.
238 return;
239 }
240
241 Runtime* runtime = Runtime::Current();
242 if (!runtime->IsAotCompiler()) {
243 ScopedObjectAccessUnchecked soa(Thread::Current());
244
245 ScopedLocalRef<jobject> consumer_object(soa.Env(),
246 soa.Env()->GetStaticObjectField(
247 WellKnownClasses::dalvik_system_VMRuntime,
248 WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer));
249 // If the consumer is non-null, we call back to it to let it know that we
250 // have encountered an API that's in one of our lists.
251 if (consumer_object != nullptr) {
252 std::ostringstream member_signature_str;
253 Dump(member_signature_str);
254
255 ScopedLocalRef<jobject> signature_str(
256 soa.Env(),
257 soa.Env()->NewStringUTF(member_signature_str.str().c_str()));
258
259 // Call through to Consumer.accept(String memberSignature);
260 soa.Env()->CallVoidMethod(consumer_object.get(),
261 WellKnownClasses::java_util_function_Consumer_accept,
262 signature_str.get());
263 }
264 }
265}
266
David Brazdil85865692018-10-30 17:26:20 +0000267static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtField*) {
David Brazdil8a6b2f32018-04-26 16:52:11 +0100268 return true;
269}
270
David Brazdil85865692018-10-30 17:26:20 +0000271static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtMethod* method) {
David Brazdil8a6b2f32018-04-26 16:52:11 +0100272 return !method->IsIntrinsic();
273}
274
275template<typename T>
276static ALWAYS_INLINE void MaybeWhitelistMember(Runtime* runtime, T* member)
277 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil85865692018-10-30 17:26:20 +0000278 if (CanUpdateRuntimeFlags(member) && runtime->ShouldDedupeHiddenApiWarnings()) {
279 member->SetAccessFlags(member->GetAccessFlags() | kAccPublicApi);
David Brazdil8a6b2f32018-04-26 16:52:11 +0100280 }
281}
282
David Brazdil1a658632018-12-01 17:54:26 +0000283static ALWAYS_INLINE uint32_t GetMemberDexIndex(ArtField* field) {
284 return field->GetDexFieldIndex();
David Brazdil85865692018-10-30 17:26:20 +0000285}
286
David Brazdil1a658632018-12-01 17:54:26 +0000287static ALWAYS_INLINE uint32_t GetMemberDexIndex(ArtMethod* method)
288 REQUIRES_SHARED(Locks::mutator_lock_) {
289 // Use the non-obsolete method to avoid DexFile mismatch between
290 // the method index and the declaring class.
291 return method->GetNonObsoleteMethod()->GetDexMethodIndex();
292}
David Brazdil85865692018-10-30 17:26:20 +0000293
David Brazdil1a658632018-12-01 17:54:26 +0000294static void VisitMembers(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800295 const dex::ClassDef& class_def,
David Brazdil1a658632018-12-01 17:54:26 +0000296 const std::function<void(const ClassAccessor::Field&)>& fn_visit) {
297 ClassAccessor accessor(dex_file, class_def, /* parse_hiddenapi_class_data= */ true);
298 accessor.VisitFields(fn_visit, fn_visit);
299}
300
301static void VisitMembers(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800302 const dex::ClassDef& class_def,
David Brazdil1a658632018-12-01 17:54:26 +0000303 const std::function<void(const ClassAccessor::Method&)>& fn_visit) {
304 ClassAccessor accessor(dex_file, class_def, /* parse_hiddenapi_class_data= */ true);
305 accessor.VisitMethods(fn_visit, fn_visit);
306}
307
308template<typename T>
309uint32_t GetDexFlags(T* member) REQUIRES_SHARED(Locks::mutator_lock_) {
310 static_assert(std::is_same<T, ArtField>::value || std::is_same<T, ArtMethod>::value);
311 using AccessorType = typename std::conditional<std::is_same<T, ArtField>::value,
312 ClassAccessor::Field, ClassAccessor::Method>::type;
313
314 ObjPtr<mirror::Class> declaring_class = member->GetDeclaringClass();
David Brazdil90faceb2018-12-14 14:36:15 +0000315 DCHECK(!declaring_class.IsNull()) << "Attempting to access a runtime method";
David Brazdil85865692018-10-30 17:26:20 +0000316
David Brazdil90faceb2018-12-14 14:36:15 +0000317 ApiList flags;
318 DCHECK(!flags.IsValid());
David Brazdil85865692018-10-30 17:26:20 +0000319
David Brazdil1a658632018-12-01 17:54:26 +0000320 // Check if the declaring class has ClassExt allocated. If it does, check if
321 // the pre-JVMTI redefine dex file has been set to determine if the declaring
322 // class has been JVMTI-redefined.
323 ObjPtr<mirror::ClassExt> ext(declaring_class->GetExtData());
324 const DexFile* original_dex = ext.IsNull() ? nullptr : ext->GetPreRedefineDexFile();
325 if (LIKELY(original_dex == nullptr)) {
326 // Class is not redefined. Find the class def, iterate over its members and
327 // find the entry corresponding to this `member`.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800328 const dex::ClassDef* class_def = declaring_class->GetClassDef();
David Brazdil90faceb2018-12-14 14:36:15 +0000329 DCHECK(class_def != nullptr) << "Class def should always be set for initialized classes";
330
331 uint32_t member_index = GetMemberDexIndex(member);
332 auto fn_visit = [&](const AccessorType& dex_member) {
333 if (dex_member.GetIndex() == member_index) {
334 flags = ApiList(dex_member.GetHiddenapiFlags());
335 }
336 };
337 VisitMembers(declaring_class->GetDexFile(), *class_def, fn_visit);
David Brazdil1a658632018-12-01 17:54:26 +0000338 } else {
339 // Class was redefined using JVMTI. We have a pointer to the original dex file
340 // and the class def index of this class in that dex file, but the field/method
341 // indices are lost. Iterate over all members of the class def and find the one
342 // corresponding to this `member` by name and type string comparison.
343 // This is obviously very slow, but it is only used when non-exempt code tries
344 // to access a hidden member of a JVMTI-redefined class.
345 uint16_t class_def_idx = ext->GetPreRedefineClassDefIndex();
346 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800347 const dex::ClassDef& original_class_def = original_dex->GetClassDef(class_def_idx);
David Brazdil1a658632018-12-01 17:54:26 +0000348 MemberSignature member_signature(member);
349 auto fn_visit = [&](const AccessorType& dex_member) {
350 MemberSignature cur_signature(dex_member);
351 if (member_signature.MemberNameAndTypeMatch(cur_signature)) {
352 DCHECK(member_signature.Equals(cur_signature));
David Brazdil90faceb2018-12-14 14:36:15 +0000353 flags = ApiList(dex_member.GetHiddenapiFlags());
David Brazdil1a658632018-12-01 17:54:26 +0000354 }
355 };
356 VisitMembers(*original_dex, original_class_def, fn_visit);
357 }
David Brazdil85865692018-10-30 17:26:20 +0000358
David Brazdil90faceb2018-12-14 14:36:15 +0000359 CHECK(flags.IsValid()) << "Could not find hiddenapi flags for "
David Brazdil1a658632018-12-01 17:54:26 +0000360 << Dumpable<MemberSignature>(MemberSignature(member));
David Brazdil90faceb2018-12-14 14:36:15 +0000361 return flags.GetDexFlags();
David Brazdil85865692018-10-30 17:26:20 +0000362}
363
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700364template<typename T>
David Brazdile7681822018-12-14 16:25:33 +0000365void MaybeReportCorePlatformApiViolation(T* member,
366 const AccessContext& caller_context,
367 AccessMethod access_method) {
368 if (access_method != AccessMethod::kNone) {
369 MemberSignature sig(member);
370 LOG(ERROR) << "CorePlatformApi violation: " << Dumpable<MemberSignature>(sig)
371 << " from " << caller_context << " using " << access_method;
372 }
373}
374
375template<typename T>
376bool ShouldDenyAccessToMemberImpl(T* member, ApiList api_list, AccessMethod access_method) {
David Brazdilf50ac102018-10-17 18:00:06 +0100377 DCHECK(member != nullptr);
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700378 Runtime* runtime = Runtime::Current();
David Brazdilc5a96e42019-01-09 10:04:45 +0000379
David Brazdilf50ac102018-10-17 18:00:06 +0100380 EnforcementPolicy policy = runtime->GetHiddenApiEnforcementPolicy();
David Brazdilc5a96e42019-01-09 10:04:45 +0000381 DCHECK(policy != EnforcementPolicy::kDisabled)
382 << "Should never enter this function when access checks are completely disabled";
David Brazdilf50ac102018-10-17 18:00:06 +0100383
384 const bool deny_access =
385 (policy == EnforcementPolicy::kEnabled) &&
David Brazdil2bb2fbd2018-11-13 18:24:26 +0000386 IsSdkVersionSetAndMoreThan(runtime->GetTargetSdkVersion(),
David Brazdildcfa89b2018-10-31 11:04:10 +0000387 api_list.GetMaxAllowedSdkVersion());
David Brazdilf50ac102018-10-17 18:00:06 +0100388
389 MemberSignature member_signature(member);
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700390
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100391 // Check for an exemption first. Exempted APIs are treated as white list.
David Brazdilf50ac102018-10-17 18:00:06 +0100392 if (member_signature.IsExempted(runtime->GetHiddenApiExemptions())) {
393 // Avoid re-examining the exemption list next time.
394 // Note this results in no warning for the member, which seems like what one would expect.
395 // Exemptions effectively adds new members to the whitelist.
396 MaybeWhitelistMember(runtime, member);
397 return false;
398 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700399
David Brazdilf50ac102018-10-17 18:00:06 +0100400 if (access_method != AccessMethod::kNone) {
401 // Print a log message with information about this class member access.
402 // We do this if we're about to deny access, or the app is debuggable.
403 if (kLogAllAccesses || deny_access || runtime->IsJavaDebuggable()) {
David Brazdilb8c66192018-04-23 13:51:16 +0100404 member_signature.WarnAboutAccess(access_method, api_list);
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100405 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700406
David Brazdilf50ac102018-10-17 18:00:06 +0100407 // If there is a StrictMode listener, notify it about this violation.
408 member_signature.NotifyHiddenApiListener(access_method);
409
410 // If event log sampling is enabled, report this violation.
411 if (kIsTargetBuild && !kIsTargetLinux) {
412 uint32_t eventLogSampleRate = runtime->GetHiddenApiEventLogSampleRate();
413 // Assert that RAND_MAX is big enough, to ensure sampling below works as expected.
414 static_assert(RAND_MAX >= 0xffff, "RAND_MAX too small");
415 if (eventLogSampleRate != 0 &&
416 (static_cast<uint32_t>(std::rand()) & 0xffff) < eventLogSampleRate) {
417 member_signature.LogAccessToEventLog(access_method, deny_access);
418 }
419 }
420
421 // If this access was not denied, move the member into whitelist and skip
422 // the warning the next time the member is accessed.
423 if (!deny_access) {
424 MaybeWhitelistMember(runtime, member);
Mathew Inwood73ddda42018-04-03 15:32:32 +0100425 }
426 }
427
David Brazdilf50ac102018-10-17 18:00:06 +0100428 return deny_access;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700429}
430
David Brazdile7681822018-12-14 16:25:33 +0000431// Need to instantiate these.
David Brazdil1a658632018-12-01 17:54:26 +0000432template uint32_t GetDexFlags<ArtField>(ArtField* member);
433template uint32_t GetDexFlags<ArtMethod>(ArtMethod* member);
David Brazdile7681822018-12-14 16:25:33 +0000434template void MaybeReportCorePlatformApiViolation(ArtField* member,
435 const AccessContext& caller_context,
436 AccessMethod access_method);
437template void MaybeReportCorePlatformApiViolation(ArtMethod* member,
438 const AccessContext& caller_context,
439 AccessMethod access_method);
David Brazdilf50ac102018-10-17 18:00:06 +0100440template bool ShouldDenyAccessToMemberImpl<ArtField>(ArtField* member,
David Brazdile7681822018-12-14 16:25:33 +0000441 ApiList api_list,
David Brazdilf50ac102018-10-17 18:00:06 +0100442 AccessMethod access_method);
443template bool ShouldDenyAccessToMemberImpl<ArtMethod>(ArtMethod* member,
David Brazdile7681822018-12-14 16:25:33 +0000444 ApiList api_list,
David Brazdilf50ac102018-10-17 18:00:06 +0100445 AccessMethod access_method);
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700446} // namespace detail
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100447
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700448} // namespace hiddenapi
449} // namespace art