blob: 68ed102a9f92f59f52d933e0f9d0f1a5a69fe16e [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
32namespace art {
33namespace hiddenapi {
34
Mathew Inwood27199e62018-04-11 16:08:21 +010035// Set to true if we should always print a warning in logcat for all hidden API accesses, not just
36// dark grey and black. This can be set to true for developer preview / beta builds, but should be
37// false for public release builds.
Mathew Inwood6d6012e2018-04-12 15:43:11 +010038// Note that when flipping this flag, you must also update the expectations of test 674-hiddenapi
39// as it affects whether or not we warn for light grey APIs that have been added to the exemptions
40// list.
Mathew Inwood015a7ec2018-05-16 11:18:10 +010041static constexpr bool kLogAllAccesses = false;
Mathew Inwood27199e62018-04-11 16:08:21 +010042
Andreas Gampe80f5fe52018-03-28 16:23:24 -070043static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) {
44 switch (value) {
David Brazdilf50ac102018-10-17 18:00:06 +010045 case AccessMethod::kNone:
David Brazdil54a99cf2018-04-05 16:57:32 +010046 LOG(FATAL) << "Internal access to hidden API should not be logged";
47 UNREACHABLE();
David Brazdilf50ac102018-10-17 18:00:06 +010048 case AccessMethod::kReflection:
Andreas Gampe80f5fe52018-03-28 16:23:24 -070049 os << "reflection";
50 break;
David Brazdilf50ac102018-10-17 18:00:06 +010051 case AccessMethod::kJNI:
Andreas Gampe80f5fe52018-03-28 16:23:24 -070052 os << "JNI";
53 break;
David Brazdilf50ac102018-10-17 18:00:06 +010054 case AccessMethod::kLinking:
Andreas Gampe80f5fe52018-03-28 16:23:24 -070055 os << "linking";
56 break;
57 }
58 return os;
59}
60
David Brazdile7681822018-12-14 16:25:33 +000061static inline std::ostream& operator<<(std::ostream& os, const AccessContext& value)
62 REQUIRES_SHARED(Locks::mutator_lock_) {
63 if (!value.GetClass().IsNull()) {
64 std::string tmp;
65 os << value.GetClass()->GetDescriptor(&tmp);
66 } else if (value.GetDexFile() != nullptr) {
67 os << value.GetDexFile()->GetLocation();
68 } else {
69 os << "<unknown_caller>";
70 }
71 return os;
72}
73
Andreas Gampe80f5fe52018-03-28 16:23:24 -070074namespace detail {
75
David Brazdilf50ac102018-10-17 18:00:06 +010076// Do not change the values of items in this enum, as they are written to the
77// event log for offline analysis. Any changes will interfere with that analysis.
78enum AccessContextFlags {
79 // Accessed member is a field if this bit is set, else a method
80 kMemberIsField = 1 << 0,
81 // Indicates if access was denied to the member, instead of just printing a warning.
82 kAccessDenied = 1 << 1,
83};
84
Andreas Gampe80f5fe52018-03-28 16:23:24 -070085MemberSignature::MemberSignature(ArtField* field) {
Mathew Inwood73ddda42018-04-03 15:32:32 +010086 class_name_ = field->GetDeclaringClass()->GetDescriptor(&tmp_);
87 member_name_ = field->GetName();
88 type_signature_ = field->GetTypeDescriptor();
89 type_ = kField;
Andreas Gampe80f5fe52018-03-28 16:23:24 -070090}
91
92MemberSignature::MemberSignature(ArtMethod* method) {
David Brazdil6a1dab42019-02-28 18:45:15 +000093 DCHECK(method == method->GetInterfaceMethodIfProxy(kRuntimePointerSize))
94 << "Caller should have replaced proxy method with interface method";
Mathew Inwood73ddda42018-04-03 15:32:32 +010095 class_name_ = method->GetDeclaringClass()->GetDescriptor(&tmp_);
96 member_name_ = method->GetName();
97 type_signature_ = method->GetSignature().ToString();
98 type_ = kMethod;
99}
100
David Brazdil1a658632018-12-01 17:54:26 +0000101MemberSignature::MemberSignature(const ClassAccessor::Field& field) {
102 const DexFile& dex_file = field.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800103 const dex::FieldId& field_id = dex_file.GetFieldId(field.GetIndex());
David Brazdil1a658632018-12-01 17:54:26 +0000104 class_name_ = dex_file.GetFieldDeclaringClassDescriptor(field_id);
105 member_name_ = dex_file.GetFieldName(field_id);
106 type_signature_ = dex_file.GetFieldTypeDescriptor(field_id);
107 type_ = kField;
108}
109
110MemberSignature::MemberSignature(const ClassAccessor::Method& method) {
111 const DexFile& dex_file = method.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800112 const dex::MethodId& method_id = dex_file.GetMethodId(method.GetIndex());
David Brazdil1a658632018-12-01 17:54:26 +0000113 class_name_ = dex_file.GetMethodDeclaringClassDescriptor(method_id);
114 member_name_ = dex_file.GetMethodName(method_id);
115 type_signature_ = dex_file.GetMethodSignature(method_id).ToString();
116 type_ = kMethod;
117}
118
Mathew Inwood73ddda42018-04-03 15:32:32 +0100119inline std::vector<const char*> MemberSignature::GetSignatureParts() const {
120 if (type_ == kField) {
121 return { class_name_.c_str(), "->", member_name_.c_str(), ":", type_signature_.c_str() };
122 } else {
123 DCHECK_EQ(type_, kMethod);
124 return { class_name_.c_str(), "->", member_name_.c_str(), type_signature_.c_str() };
125 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700126}
127
128bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const {
129 size_t pos = 0;
Mathew Inwood73ddda42018-04-03 15:32:32 +0100130 for (const char* part : GetSignatureParts()) {
131 size_t count = std::min(prefix.length() - pos, strlen(part));
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700132 if (prefix.compare(pos, count, part, 0, count) == 0) {
133 pos += count;
134 } else {
135 return false;
136 }
137 }
138 // We have a complete match if all parts match (we exit the loop without
139 // returning) AND we've matched the whole prefix.
140 return pos == prefix.length();
141}
142
143bool MemberSignature::IsExempted(const std::vector<std::string>& exemptions) {
144 for (const std::string& exemption : exemptions) {
145 if (DoesPrefixMatch(exemption)) {
146 return true;
147 }
148 }
149 return false;
150}
151
152void MemberSignature::Dump(std::ostream& os) const {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100153 for (const char* part : GetSignatureParts()) {
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700154 os << part;
155 }
156}
157
David Brazdil47cd2722018-10-23 12:50:02 +0100158void MemberSignature::WarnAboutAccess(AccessMethod access_method, hiddenapi::ApiList list) {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100159 LOG(WARNING) << "Accessing hidden " << (type_ == kField ? "field " : "method ")
160 << Dumpable<MemberSignature>(*this) << " (" << list << ", " << access_method << ")";
161}
David Brazdilf50ac102018-10-17 18:00:06 +0100162
David Brazdil1a658632018-12-01 17:54:26 +0000163bool MemberSignature::Equals(const MemberSignature& other) {
164 return type_ == other.type_ &&
165 class_name_ == other.class_name_ &&
166 member_name_ == other.member_name_ &&
167 type_signature_ == other.type_signature_;
168}
169
170bool MemberSignature::MemberNameAndTypeMatch(const MemberSignature& other) {
171 return member_name_ == other.member_name_ && type_signature_ == other.type_signature_;
172}
173
Andrei Onea6ad020d2019-02-18 12:15:51 +0000174void MemberSignature::LogAccessToEventLog(uint32_t sampled_value,
175 AccessMethod access_method,
176 bool access_denied) {
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100177#ifdef ART_TARGET_ANDROID
David Brazdilf50ac102018-10-17 18:00:06 +0100178 if (access_method == AccessMethod::kLinking || access_method == AccessMethod::kNone) {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100179 // Linking warnings come from static analysis/compilation of the bytecode
180 // and can contain false positives (i.e. code that is never run). We choose
181 // not to log these in the event log.
Mathew Inwoodf59ca612018-05-03 11:30:01 +0100182 // None does not correspond to actual access, so should also be ignored.
Mathew Inwood73ddda42018-04-03 15:32:32 +0100183 return;
184 }
Andrei Oneaa2d2bc22019-01-25 16:18:53 +0000185 Runtime* runtime = Runtime::Current();
186 if (runtime->IsAotCompiler()) {
187 return;
Mathew Inwood73ddda42018-04-03 15:32:32 +0100188 }
Andrei Oneaa2d2bc22019-01-25 16:18:53 +0000189 JNIEnvExt* env = Thread::Current()->GetJniEnv();
Mathew Inwood5bcef172018-05-01 14:40:12 +0100190 const std::string& package_name = Runtime::Current()->GetProcessPackageName();
Andrei Oneaa2d2bc22019-01-25 16:18:53 +0000191 ScopedLocalRef<jstring> package_str(env, env->NewStringUTF(package_name.c_str()));
192 if (env->ExceptionCheck()) {
193 env->ExceptionClear();
194 LOG(ERROR) << "Unable to allocate string for package name which called hidden api";
Mathew Inwood5bcef172018-05-01 14:40:12 +0100195 }
Mathew Inwood2d4d62f2018-04-12 13:56:37 +0100196 std::ostringstream signature_str;
197 Dump(signature_str);
Andrei Oneaa2d2bc22019-01-25 16:18:53 +0000198 ScopedLocalRef<jstring> signature_jstr(env,
199 env->NewStringUTF(signature_str.str().c_str()));
200 if (env->ExceptionCheck()) {
201 env->ExceptionClear();
202 LOG(ERROR) << "Unable to allocate string for hidden api method signature";
203 }
204 env->CallStaticVoidMethod(WellKnownClasses::dalvik_system_VMRuntime,
Andrei Onea6ad020d2019-02-18 12:15:51 +0000205 WellKnownClasses::dalvik_system_VMRuntime_hiddenApiUsed,
206 sampled_value,
207 package_str.get(),
208 signature_jstr.get(),
209 static_cast<jint>(access_method),
210 access_denied);
Andrei Oneaa2d2bc22019-01-25 16:18:53 +0000211 if (env->ExceptionCheck()) {
212 env->ExceptionClear();
213 LOG(ERROR) << "Unable to report hidden api usage";
214 }
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100215#else
Andrei Onea6ad020d2019-02-18 12:15:51 +0000216 UNUSED(sampled_value);
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100217 UNUSED(access_method);
David Brazdilf50ac102018-10-17 18:00:06 +0100218 UNUSED(access_denied);
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100219#endif
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700220}
221
David Brazdilf50ac102018-10-17 18:00:06 +0100222void MemberSignature::NotifyHiddenApiListener(AccessMethod access_method) {
223 if (access_method != AccessMethod::kReflection && access_method != AccessMethod::kJNI) {
224 // We can only up-call into Java during reflection and JNI down-calls.
225 return;
226 }
227
228 Runtime* runtime = Runtime::Current();
229 if (!runtime->IsAotCompiler()) {
230 ScopedObjectAccessUnchecked soa(Thread::Current());
231
232 ScopedLocalRef<jobject> consumer_object(soa.Env(),
233 soa.Env()->GetStaticObjectField(
234 WellKnownClasses::dalvik_system_VMRuntime,
235 WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer));
236 // If the consumer is non-null, we call back to it to let it know that we
237 // have encountered an API that's in one of our lists.
238 if (consumer_object != nullptr) {
239 std::ostringstream member_signature_str;
240 Dump(member_signature_str);
241
242 ScopedLocalRef<jobject> signature_str(
243 soa.Env(),
244 soa.Env()->NewStringUTF(member_signature_str.str().c_str()));
245
246 // Call through to Consumer.accept(String memberSignature);
247 soa.Env()->CallVoidMethod(consumer_object.get(),
248 WellKnownClasses::java_util_function_Consumer_accept,
249 signature_str.get());
250 }
251 }
252}
253
David Brazdil85865692018-10-30 17:26:20 +0000254static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtField*) {
David Brazdil8a6b2f32018-04-26 16:52:11 +0100255 return true;
256}
257
David Brazdil85865692018-10-30 17:26:20 +0000258static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtMethod* method) {
David Brazdil8a6b2f32018-04-26 16:52:11 +0100259 return !method->IsIntrinsic();
260}
261
262template<typename T>
263static ALWAYS_INLINE void MaybeWhitelistMember(Runtime* runtime, T* member)
264 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil85865692018-10-30 17:26:20 +0000265 if (CanUpdateRuntimeFlags(member) && runtime->ShouldDedupeHiddenApiWarnings()) {
266 member->SetAccessFlags(member->GetAccessFlags() | kAccPublicApi);
David Brazdil8a6b2f32018-04-26 16:52:11 +0100267 }
268}
269
David Brazdil1a658632018-12-01 17:54:26 +0000270static ALWAYS_INLINE uint32_t GetMemberDexIndex(ArtField* field) {
271 return field->GetDexFieldIndex();
David Brazdil85865692018-10-30 17:26:20 +0000272}
273
David Brazdil1a658632018-12-01 17:54:26 +0000274static ALWAYS_INLINE uint32_t GetMemberDexIndex(ArtMethod* method)
275 REQUIRES_SHARED(Locks::mutator_lock_) {
276 // Use the non-obsolete method to avoid DexFile mismatch between
277 // the method index and the declaring class.
278 return method->GetNonObsoleteMethod()->GetDexMethodIndex();
279}
David Brazdil85865692018-10-30 17:26:20 +0000280
David Brazdil1a658632018-12-01 17:54:26 +0000281static void VisitMembers(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800282 const dex::ClassDef& class_def,
David Brazdil1a658632018-12-01 17:54:26 +0000283 const std::function<void(const ClassAccessor::Field&)>& fn_visit) {
284 ClassAccessor accessor(dex_file, class_def, /* parse_hiddenapi_class_data= */ true);
285 accessor.VisitFields(fn_visit, fn_visit);
286}
287
288static void VisitMembers(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800289 const dex::ClassDef& class_def,
David Brazdil1a658632018-12-01 17:54:26 +0000290 const std::function<void(const ClassAccessor::Method&)>& fn_visit) {
291 ClassAccessor accessor(dex_file, class_def, /* parse_hiddenapi_class_data= */ true);
292 accessor.VisitMethods(fn_visit, fn_visit);
293}
294
295template<typename T>
296uint32_t GetDexFlags(T* member) REQUIRES_SHARED(Locks::mutator_lock_) {
297 static_assert(std::is_same<T, ArtField>::value || std::is_same<T, ArtMethod>::value);
David Brazdil6a1dab42019-02-28 18:45:15 +0000298 constexpr bool kMemberIsField = std::is_same<T, ArtField>::value;
David Brazdil1a658632018-12-01 17:54:26 +0000299 using AccessorType = typename std::conditional<std::is_same<T, ArtField>::value,
300 ClassAccessor::Field, ClassAccessor::Method>::type;
301
302 ObjPtr<mirror::Class> declaring_class = member->GetDeclaringClass();
David Brazdil90faceb2018-12-14 14:36:15 +0000303 DCHECK(!declaring_class.IsNull()) << "Attempting to access a runtime method";
David Brazdil85865692018-10-30 17:26:20 +0000304
David Brazdil90faceb2018-12-14 14:36:15 +0000305 ApiList flags;
306 DCHECK(!flags.IsValid());
David Brazdil85865692018-10-30 17:26:20 +0000307
David Brazdil1a658632018-12-01 17:54:26 +0000308 // Check if the declaring class has ClassExt allocated. If it does, check if
309 // the pre-JVMTI redefine dex file has been set to determine if the declaring
310 // class has been JVMTI-redefined.
311 ObjPtr<mirror::ClassExt> ext(declaring_class->GetExtData());
312 const DexFile* original_dex = ext.IsNull() ? nullptr : ext->GetPreRedefineDexFile();
313 if (LIKELY(original_dex == nullptr)) {
314 // Class is not redefined. Find the class def, iterate over its members and
315 // find the entry corresponding to this `member`.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800316 const dex::ClassDef* class_def = declaring_class->GetClassDef();
David Brazdil6a1dab42019-02-28 18:45:15 +0000317 if (class_def == nullptr) {
318 // ClassDef is not set for proxy classes. Only their fields can ever be inspected.
319 DCHECK(declaring_class->IsProxyClass())
320 << "Only proxy classes are expected not to have a class def";
321 DCHECK(kMemberIsField)
322 << "Interface methods should be inspected instead of proxy class methods";
323 flags = ApiList::Greylist();
324 } else {
325 uint32_t member_index = GetMemberDexIndex(member);
326 auto fn_visit = [&](const AccessorType& dex_member) {
327 if (dex_member.GetIndex() == member_index) {
328 flags = ApiList(dex_member.GetHiddenapiFlags());
329 }
330 };
331 VisitMembers(declaring_class->GetDexFile(), *class_def, fn_visit);
332 }
David Brazdil1a658632018-12-01 17:54:26 +0000333 } else {
334 // Class was redefined using JVMTI. We have a pointer to the original dex file
335 // and the class def index of this class in that dex file, but the field/method
336 // indices are lost. Iterate over all members of the class def and find the one
337 // corresponding to this `member` by name and type string comparison.
338 // This is obviously very slow, but it is only used when non-exempt code tries
339 // to access a hidden member of a JVMTI-redefined class.
340 uint16_t class_def_idx = ext->GetPreRedefineClassDefIndex();
341 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800342 const dex::ClassDef& original_class_def = original_dex->GetClassDef(class_def_idx);
David Brazdil1a658632018-12-01 17:54:26 +0000343 MemberSignature member_signature(member);
344 auto fn_visit = [&](const AccessorType& dex_member) {
345 MemberSignature cur_signature(dex_member);
346 if (member_signature.MemberNameAndTypeMatch(cur_signature)) {
347 DCHECK(member_signature.Equals(cur_signature));
David Brazdil90faceb2018-12-14 14:36:15 +0000348 flags = ApiList(dex_member.GetHiddenapiFlags());
David Brazdil1a658632018-12-01 17:54:26 +0000349 }
350 };
351 VisitMembers(*original_dex, original_class_def, fn_visit);
352 }
David Brazdil85865692018-10-30 17:26:20 +0000353
David Brazdil90faceb2018-12-14 14:36:15 +0000354 CHECK(flags.IsValid()) << "Could not find hiddenapi flags for "
David Brazdil1a658632018-12-01 17:54:26 +0000355 << Dumpable<MemberSignature>(MemberSignature(member));
David Brazdil90faceb2018-12-14 14:36:15 +0000356 return flags.GetDexFlags();
David Brazdil85865692018-10-30 17:26:20 +0000357}
358
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700359template<typename T>
David Brazdile7681822018-12-14 16:25:33 +0000360void MaybeReportCorePlatformApiViolation(T* member,
361 const AccessContext& caller_context,
362 AccessMethod access_method) {
363 if (access_method != AccessMethod::kNone) {
364 MemberSignature sig(member);
365 LOG(ERROR) << "CorePlatformApi violation: " << Dumpable<MemberSignature>(sig)
366 << " from " << caller_context << " using " << access_method;
367 }
368}
369
370template<typename T>
371bool ShouldDenyAccessToMemberImpl(T* member, ApiList api_list, AccessMethod access_method) {
David Brazdilf50ac102018-10-17 18:00:06 +0100372 DCHECK(member != nullptr);
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700373 Runtime* runtime = Runtime::Current();
David Brazdilc5a96e42019-01-09 10:04:45 +0000374
David Brazdilf50ac102018-10-17 18:00:06 +0100375 EnforcementPolicy policy = runtime->GetHiddenApiEnforcementPolicy();
David Brazdilc5a96e42019-01-09 10:04:45 +0000376 DCHECK(policy != EnforcementPolicy::kDisabled)
377 << "Should never enter this function when access checks are completely disabled";
David Brazdilf50ac102018-10-17 18:00:06 +0100378
379 const bool deny_access =
380 (policy == EnforcementPolicy::kEnabled) &&
David Brazdil2bb2fbd2018-11-13 18:24:26 +0000381 IsSdkVersionSetAndMoreThan(runtime->GetTargetSdkVersion(),
David Brazdildcfa89b2018-10-31 11:04:10 +0000382 api_list.GetMaxAllowedSdkVersion());
David Brazdilf50ac102018-10-17 18:00:06 +0100383
384 MemberSignature member_signature(member);
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700385
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100386 // Check for an exemption first. Exempted APIs are treated as white list.
David Brazdilf50ac102018-10-17 18:00:06 +0100387 if (member_signature.IsExempted(runtime->GetHiddenApiExemptions())) {
388 // Avoid re-examining the exemption list next time.
389 // Note this results in no warning for the member, which seems like what one would expect.
390 // Exemptions effectively adds new members to the whitelist.
391 MaybeWhitelistMember(runtime, member);
392 return false;
393 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700394
David Brazdilf50ac102018-10-17 18:00:06 +0100395 if (access_method != AccessMethod::kNone) {
396 // Print a log message with information about this class member access.
397 // We do this if we're about to deny access, or the app is debuggable.
398 if (kLogAllAccesses || deny_access || runtime->IsJavaDebuggable()) {
David Brazdilb8c66192018-04-23 13:51:16 +0100399 member_signature.WarnAboutAccess(access_method, api_list);
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100400 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700401
David Brazdilf50ac102018-10-17 18:00:06 +0100402 // If there is a StrictMode listener, notify it about this violation.
403 member_signature.NotifyHiddenApiListener(access_method);
404
405 // If event log sampling is enabled, report this violation.
406 if (kIsTargetBuild && !kIsTargetLinux) {
407 uint32_t eventLogSampleRate = runtime->GetHiddenApiEventLogSampleRate();
408 // Assert that RAND_MAX is big enough, to ensure sampling below works as expected.
409 static_assert(RAND_MAX >= 0xffff, "RAND_MAX too small");
Andrei Onea6ad020d2019-02-18 12:15:51 +0000410 if (eventLogSampleRate != 0) {
411 const uint32_t sampled_value = static_cast<uint32_t>(std::rand()) & 0xffff;
412 if (sampled_value < eventLogSampleRate) {
413 member_signature.LogAccessToEventLog(sampled_value, access_method, deny_access);
414 }
David Brazdilf50ac102018-10-17 18:00:06 +0100415 }
416 }
417
418 // If this access was not denied, move the member into whitelist and skip
419 // the warning the next time the member is accessed.
420 if (!deny_access) {
421 MaybeWhitelistMember(runtime, member);
Mathew Inwood73ddda42018-04-03 15:32:32 +0100422 }
423 }
424
David Brazdilf50ac102018-10-17 18:00:06 +0100425 return deny_access;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700426}
427
David Brazdile7681822018-12-14 16:25:33 +0000428// Need to instantiate these.
David Brazdil1a658632018-12-01 17:54:26 +0000429template uint32_t GetDexFlags<ArtField>(ArtField* member);
430template uint32_t GetDexFlags<ArtMethod>(ArtMethod* member);
David Brazdile7681822018-12-14 16:25:33 +0000431template void MaybeReportCorePlatformApiViolation(ArtField* member,
432 const AccessContext& caller_context,
433 AccessMethod access_method);
434template void MaybeReportCorePlatformApiViolation(ArtMethod* member,
435 const AccessContext& caller_context,
436 AccessMethod access_method);
David Brazdilf50ac102018-10-17 18:00:06 +0100437template bool ShouldDenyAccessToMemberImpl<ArtField>(ArtField* member,
David Brazdile7681822018-12-14 16:25:33 +0000438 ApiList api_list,
David Brazdilf50ac102018-10-17 18:00:06 +0100439 AccessMethod access_method);
440template bool ShouldDenyAccessToMemberImpl<ArtMethod>(ArtMethod* member,
David Brazdile7681822018-12-14 16:25:33 +0000441 ApiList api_list,
David Brazdilf50ac102018-10-17 18:00:06 +0100442 AccessMethod access_method);
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700443} // namespace detail
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100444
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700445} // namespace hiddenapi
446} // namespace art