blob: 95289b2dff92b5877fc0f263c07848452e7a56d0 [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 Brazdil73a64f62018-05-02 16:53:06 +010093 // If this is a proxy method, print the signature of the interface method.
94 method = method->GetInterfaceMethodIfProxy(
95 Runtime::Current()->GetClassLinker()->GetImagePointerSize());
96
Mathew Inwood73ddda42018-04-03 15:32:32 +010097 class_name_ = method->GetDeclaringClass()->GetDescriptor(&tmp_);
98 member_name_ = method->GetName();
99 type_signature_ = method->GetSignature().ToString();
100 type_ = kMethod;
101}
102
David Brazdil1a658632018-12-01 17:54:26 +0000103MemberSignature::MemberSignature(const ClassAccessor::Field& field) {
104 const DexFile& dex_file = field.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800105 const dex::FieldId& field_id = dex_file.GetFieldId(field.GetIndex());
David Brazdil1a658632018-12-01 17:54:26 +0000106 class_name_ = dex_file.GetFieldDeclaringClassDescriptor(field_id);
107 member_name_ = dex_file.GetFieldName(field_id);
108 type_signature_ = dex_file.GetFieldTypeDescriptor(field_id);
109 type_ = kField;
110}
111
112MemberSignature::MemberSignature(const ClassAccessor::Method& method) {
113 const DexFile& dex_file = method.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800114 const dex::MethodId& method_id = dex_file.GetMethodId(method.GetIndex());
David Brazdil1a658632018-12-01 17:54:26 +0000115 class_name_ = dex_file.GetMethodDeclaringClassDescriptor(method_id);
116 member_name_ = dex_file.GetMethodName(method_id);
117 type_signature_ = dex_file.GetMethodSignature(method_id).ToString();
118 type_ = kMethod;
119}
120
Mathew Inwood73ddda42018-04-03 15:32:32 +0100121inline std::vector<const char*> MemberSignature::GetSignatureParts() const {
122 if (type_ == kField) {
123 return { class_name_.c_str(), "->", member_name_.c_str(), ":", type_signature_.c_str() };
124 } else {
125 DCHECK_EQ(type_, kMethod);
126 return { class_name_.c_str(), "->", member_name_.c_str(), type_signature_.c_str() };
127 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700128}
129
130bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const {
131 size_t pos = 0;
Mathew Inwood73ddda42018-04-03 15:32:32 +0100132 for (const char* part : GetSignatureParts()) {
133 size_t count = std::min(prefix.length() - pos, strlen(part));
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700134 if (prefix.compare(pos, count, part, 0, count) == 0) {
135 pos += count;
136 } else {
137 return false;
138 }
139 }
140 // We have a complete match if all parts match (we exit the loop without
141 // returning) AND we've matched the whole prefix.
142 return pos == prefix.length();
143}
144
145bool MemberSignature::IsExempted(const std::vector<std::string>& exemptions) {
146 for (const std::string& exemption : exemptions) {
147 if (DoesPrefixMatch(exemption)) {
148 return true;
149 }
150 }
151 return false;
152}
153
154void MemberSignature::Dump(std::ostream& os) const {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100155 for (const char* part : GetSignatureParts()) {
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700156 os << part;
157 }
158}
159
David Brazdil47cd2722018-10-23 12:50:02 +0100160void MemberSignature::WarnAboutAccess(AccessMethod access_method, hiddenapi::ApiList list) {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100161 LOG(WARNING) << "Accessing hidden " << (type_ == kField ? "field " : "method ")
162 << Dumpable<MemberSignature>(*this) << " (" << list << ", " << access_method << ")";
163}
David Brazdilf50ac102018-10-17 18:00:06 +0100164
David Brazdil1a658632018-12-01 17:54:26 +0000165bool MemberSignature::Equals(const MemberSignature& other) {
166 return type_ == other.type_ &&
167 class_name_ == other.class_name_ &&
168 member_name_ == other.member_name_ &&
169 type_signature_ == other.type_signature_;
170}
171
172bool MemberSignature::MemberNameAndTypeMatch(const MemberSignature& other) {
173 return member_name_ == other.member_name_ && type_signature_ == other.type_signature_;
174}
175
Andrei Onea6ad020d2019-02-18 12:15:51 +0000176void MemberSignature::LogAccessToEventLog(uint32_t sampled_value,
177 AccessMethod access_method,
178 bool access_denied) {
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100179#ifdef ART_TARGET_ANDROID
David Brazdilf50ac102018-10-17 18:00:06 +0100180 if (access_method == AccessMethod::kLinking || access_method == AccessMethod::kNone) {
Mathew Inwood73ddda42018-04-03 15:32:32 +0100181 // Linking warnings come from static analysis/compilation of the bytecode
182 // and can contain false positives (i.e. code that is never run). We choose
183 // not to log these in the event log.
Mathew Inwoodf59ca612018-05-03 11:30:01 +0100184 // None does not correspond to actual access, so should also be ignored.
Mathew Inwood73ddda42018-04-03 15:32:32 +0100185 return;
186 }
Andrei Oneaa2d2bc22019-01-25 16:18:53 +0000187 Runtime* runtime = Runtime::Current();
188 if (runtime->IsAotCompiler()) {
189 return;
Mathew Inwood73ddda42018-04-03 15:32:32 +0100190 }
Andrei Oneaa2d2bc22019-01-25 16:18:53 +0000191 JNIEnvExt* env = Thread::Current()->GetJniEnv();
Mathew Inwood5bcef172018-05-01 14:40:12 +0100192 const std::string& package_name = Runtime::Current()->GetProcessPackageName();
Andrei Oneaa2d2bc22019-01-25 16:18:53 +0000193 ScopedLocalRef<jstring> package_str(env, env->NewStringUTF(package_name.c_str()));
194 if (env->ExceptionCheck()) {
195 env->ExceptionClear();
196 LOG(ERROR) << "Unable to allocate string for package name which called hidden api";
Mathew Inwood5bcef172018-05-01 14:40:12 +0100197 }
Mathew Inwood2d4d62f2018-04-12 13:56:37 +0100198 std::ostringstream signature_str;
199 Dump(signature_str);
Andrei Oneaa2d2bc22019-01-25 16:18:53 +0000200 ScopedLocalRef<jstring> signature_jstr(env,
201 env->NewStringUTF(signature_str.str().c_str()));
202 if (env->ExceptionCheck()) {
203 env->ExceptionClear();
204 LOG(ERROR) << "Unable to allocate string for hidden api method signature";
205 }
206 env->CallStaticVoidMethod(WellKnownClasses::dalvik_system_VMRuntime,
Andrei Onea6ad020d2019-02-18 12:15:51 +0000207 WellKnownClasses::dalvik_system_VMRuntime_hiddenApiUsed,
208 sampled_value,
209 package_str.get(),
210 signature_jstr.get(),
211 static_cast<jint>(access_method),
212 access_denied);
Andrei Oneaa2d2bc22019-01-25 16:18:53 +0000213 if (env->ExceptionCheck()) {
214 env->ExceptionClear();
215 LOG(ERROR) << "Unable to report hidden api usage";
216 }
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100217#else
Andrei Onea6ad020d2019-02-18 12:15:51 +0000218 UNUSED(sampled_value);
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100219 UNUSED(access_method);
David Brazdilf50ac102018-10-17 18:00:06 +0100220 UNUSED(access_denied);
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100221#endif
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700222}
223
David Brazdilf50ac102018-10-17 18:00:06 +0100224void MemberSignature::NotifyHiddenApiListener(AccessMethod access_method) {
225 if (access_method != AccessMethod::kReflection && access_method != AccessMethod::kJNI) {
226 // We can only up-call into Java during reflection and JNI down-calls.
227 return;
228 }
229
230 Runtime* runtime = Runtime::Current();
231 if (!runtime->IsAotCompiler()) {
232 ScopedObjectAccessUnchecked soa(Thread::Current());
233
234 ScopedLocalRef<jobject> consumer_object(soa.Env(),
235 soa.Env()->GetStaticObjectField(
236 WellKnownClasses::dalvik_system_VMRuntime,
237 WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer));
238 // If the consumer is non-null, we call back to it to let it know that we
239 // have encountered an API that's in one of our lists.
240 if (consumer_object != nullptr) {
241 std::ostringstream member_signature_str;
242 Dump(member_signature_str);
243
244 ScopedLocalRef<jobject> signature_str(
245 soa.Env(),
246 soa.Env()->NewStringUTF(member_signature_str.str().c_str()));
247
248 // Call through to Consumer.accept(String memberSignature);
249 soa.Env()->CallVoidMethod(consumer_object.get(),
250 WellKnownClasses::java_util_function_Consumer_accept,
251 signature_str.get());
252 }
253 }
254}
255
David Brazdil85865692018-10-30 17:26:20 +0000256static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtField*) {
David Brazdil8a6b2f32018-04-26 16:52:11 +0100257 return true;
258}
259
David Brazdil85865692018-10-30 17:26:20 +0000260static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtMethod* method) {
David Brazdil8a6b2f32018-04-26 16:52:11 +0100261 return !method->IsIntrinsic();
262}
263
264template<typename T>
265static ALWAYS_INLINE void MaybeWhitelistMember(Runtime* runtime, T* member)
266 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil85865692018-10-30 17:26:20 +0000267 if (CanUpdateRuntimeFlags(member) && runtime->ShouldDedupeHiddenApiWarnings()) {
268 member->SetAccessFlags(member->GetAccessFlags() | kAccPublicApi);
David Brazdil8a6b2f32018-04-26 16:52:11 +0100269 }
270}
271
David Brazdil1a658632018-12-01 17:54:26 +0000272static ALWAYS_INLINE uint32_t GetMemberDexIndex(ArtField* field) {
273 return field->GetDexFieldIndex();
David Brazdil85865692018-10-30 17:26:20 +0000274}
275
David Brazdil1a658632018-12-01 17:54:26 +0000276static ALWAYS_INLINE uint32_t GetMemberDexIndex(ArtMethod* method)
277 REQUIRES_SHARED(Locks::mutator_lock_) {
278 // Use the non-obsolete method to avoid DexFile mismatch between
279 // the method index and the declaring class.
280 return method->GetNonObsoleteMethod()->GetDexMethodIndex();
281}
David Brazdil85865692018-10-30 17:26:20 +0000282
David Brazdil1a658632018-12-01 17:54:26 +0000283static void VisitMembers(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800284 const dex::ClassDef& class_def,
David Brazdil1a658632018-12-01 17:54:26 +0000285 const std::function<void(const ClassAccessor::Field&)>& fn_visit) {
286 ClassAccessor accessor(dex_file, class_def, /* parse_hiddenapi_class_data= */ true);
287 accessor.VisitFields(fn_visit, fn_visit);
288}
289
290static void VisitMembers(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800291 const dex::ClassDef& class_def,
David Brazdil1a658632018-12-01 17:54:26 +0000292 const std::function<void(const ClassAccessor::Method&)>& fn_visit) {
293 ClassAccessor accessor(dex_file, class_def, /* parse_hiddenapi_class_data= */ true);
294 accessor.VisitMethods(fn_visit, fn_visit);
295}
296
297template<typename T>
298uint32_t GetDexFlags(T* member) REQUIRES_SHARED(Locks::mutator_lock_) {
299 static_assert(std::is_same<T, ArtField>::value || std::is_same<T, ArtMethod>::value);
300 using AccessorType = typename std::conditional<std::is_same<T, ArtField>::value,
301 ClassAccessor::Field, ClassAccessor::Method>::type;
302
303 ObjPtr<mirror::Class> declaring_class = member->GetDeclaringClass();
David Brazdil90faceb2018-12-14 14:36:15 +0000304 DCHECK(!declaring_class.IsNull()) << "Attempting to access a runtime method";
David Brazdil85865692018-10-30 17:26:20 +0000305
David Brazdil90faceb2018-12-14 14:36:15 +0000306 ApiList flags;
307 DCHECK(!flags.IsValid());
David Brazdil85865692018-10-30 17:26:20 +0000308
David Brazdil1a658632018-12-01 17:54:26 +0000309 // Check if the declaring class has ClassExt allocated. If it does, check if
310 // the pre-JVMTI redefine dex file has been set to determine if the declaring
311 // class has been JVMTI-redefined.
312 ObjPtr<mirror::ClassExt> ext(declaring_class->GetExtData());
313 const DexFile* original_dex = ext.IsNull() ? nullptr : ext->GetPreRedefineDexFile();
314 if (LIKELY(original_dex == nullptr)) {
315 // Class is not redefined. Find the class def, iterate over its members and
316 // find the entry corresponding to this `member`.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800317 const dex::ClassDef* class_def = declaring_class->GetClassDef();
David Brazdil90faceb2018-12-14 14:36:15 +0000318 DCHECK(class_def != nullptr) << "Class def should always be set for initialized classes";
319
320 uint32_t member_index = GetMemberDexIndex(member);
321 auto fn_visit = [&](const AccessorType& dex_member) {
322 if (dex_member.GetIndex() == member_index) {
323 flags = ApiList(dex_member.GetHiddenapiFlags());
324 }
325 };
326 VisitMembers(declaring_class->GetDexFile(), *class_def, fn_visit);
David Brazdil1a658632018-12-01 17:54:26 +0000327 } else {
328 // Class was redefined using JVMTI. We have a pointer to the original dex file
329 // and the class def index of this class in that dex file, but the field/method
330 // indices are lost. Iterate over all members of the class def and find the one
331 // corresponding to this `member` by name and type string comparison.
332 // This is obviously very slow, but it is only used when non-exempt code tries
333 // to access a hidden member of a JVMTI-redefined class.
334 uint16_t class_def_idx = ext->GetPreRedefineClassDefIndex();
335 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800336 const dex::ClassDef& original_class_def = original_dex->GetClassDef(class_def_idx);
David Brazdil1a658632018-12-01 17:54:26 +0000337 MemberSignature member_signature(member);
338 auto fn_visit = [&](const AccessorType& dex_member) {
339 MemberSignature cur_signature(dex_member);
340 if (member_signature.MemberNameAndTypeMatch(cur_signature)) {
341 DCHECK(member_signature.Equals(cur_signature));
David Brazdil90faceb2018-12-14 14:36:15 +0000342 flags = ApiList(dex_member.GetHiddenapiFlags());
David Brazdil1a658632018-12-01 17:54:26 +0000343 }
344 };
345 VisitMembers(*original_dex, original_class_def, fn_visit);
346 }
David Brazdil85865692018-10-30 17:26:20 +0000347
David Brazdil90faceb2018-12-14 14:36:15 +0000348 CHECK(flags.IsValid()) << "Could not find hiddenapi flags for "
David Brazdil1a658632018-12-01 17:54:26 +0000349 << Dumpable<MemberSignature>(MemberSignature(member));
David Brazdil90faceb2018-12-14 14:36:15 +0000350 return flags.GetDexFlags();
David Brazdil85865692018-10-30 17:26:20 +0000351}
352
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700353template<typename T>
David Brazdile7681822018-12-14 16:25:33 +0000354void MaybeReportCorePlatformApiViolation(T* member,
355 const AccessContext& caller_context,
356 AccessMethod access_method) {
357 if (access_method != AccessMethod::kNone) {
358 MemberSignature sig(member);
359 LOG(ERROR) << "CorePlatformApi violation: " << Dumpable<MemberSignature>(sig)
360 << " from " << caller_context << " using " << access_method;
361 }
362}
363
364template<typename T>
365bool ShouldDenyAccessToMemberImpl(T* member, ApiList api_list, AccessMethod access_method) {
David Brazdilf50ac102018-10-17 18:00:06 +0100366 DCHECK(member != nullptr);
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700367 Runtime* runtime = Runtime::Current();
David Brazdilc5a96e42019-01-09 10:04:45 +0000368
David Brazdilf50ac102018-10-17 18:00:06 +0100369 EnforcementPolicy policy = runtime->GetHiddenApiEnforcementPolicy();
David Brazdilc5a96e42019-01-09 10:04:45 +0000370 DCHECK(policy != EnforcementPolicy::kDisabled)
371 << "Should never enter this function when access checks are completely disabled";
David Brazdilf50ac102018-10-17 18:00:06 +0100372
373 const bool deny_access =
374 (policy == EnforcementPolicy::kEnabled) &&
David Brazdil2bb2fbd2018-11-13 18:24:26 +0000375 IsSdkVersionSetAndMoreThan(runtime->GetTargetSdkVersion(),
David Brazdildcfa89b2018-10-31 11:04:10 +0000376 api_list.GetMaxAllowedSdkVersion());
David Brazdilf50ac102018-10-17 18:00:06 +0100377
378 MemberSignature member_signature(member);
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700379
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100380 // Check for an exemption first. Exempted APIs are treated as white list.
David Brazdilf50ac102018-10-17 18:00:06 +0100381 if (member_signature.IsExempted(runtime->GetHiddenApiExemptions())) {
382 // Avoid re-examining the exemption list next time.
383 // Note this results in no warning for the member, which seems like what one would expect.
384 // Exemptions effectively adds new members to the whitelist.
385 MaybeWhitelistMember(runtime, member);
386 return false;
387 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700388
David Brazdilf50ac102018-10-17 18:00:06 +0100389 if (access_method != AccessMethod::kNone) {
390 // Print a log message with information about this class member access.
391 // We do this if we're about to deny access, or the app is debuggable.
392 if (kLogAllAccesses || deny_access || runtime->IsJavaDebuggable()) {
David Brazdilb8c66192018-04-23 13:51:16 +0100393 member_signature.WarnAboutAccess(access_method, api_list);
Mathew Inwoodc8ce5f52018-04-05 13:58:55 +0100394 }
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700395
David Brazdilf50ac102018-10-17 18:00:06 +0100396 // If there is a StrictMode listener, notify it about this violation.
397 member_signature.NotifyHiddenApiListener(access_method);
398
399 // If event log sampling is enabled, report this violation.
400 if (kIsTargetBuild && !kIsTargetLinux) {
401 uint32_t eventLogSampleRate = runtime->GetHiddenApiEventLogSampleRate();
402 // Assert that RAND_MAX is big enough, to ensure sampling below works as expected.
403 static_assert(RAND_MAX >= 0xffff, "RAND_MAX too small");
Andrei Onea6ad020d2019-02-18 12:15:51 +0000404 if (eventLogSampleRate != 0) {
405 const uint32_t sampled_value = static_cast<uint32_t>(std::rand()) & 0xffff;
406 if (sampled_value < eventLogSampleRate) {
407 member_signature.LogAccessToEventLog(sampled_value, access_method, deny_access);
408 }
David Brazdilf50ac102018-10-17 18:00:06 +0100409 }
410 }
411
412 // If this access was not denied, move the member into whitelist and skip
413 // the warning the next time the member is accessed.
414 if (!deny_access) {
415 MaybeWhitelistMember(runtime, member);
Mathew Inwood73ddda42018-04-03 15:32:32 +0100416 }
417 }
418
David Brazdilf50ac102018-10-17 18:00:06 +0100419 return deny_access;
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700420}
421
David Brazdile7681822018-12-14 16:25:33 +0000422// Need to instantiate these.
David Brazdil1a658632018-12-01 17:54:26 +0000423template uint32_t GetDexFlags<ArtField>(ArtField* member);
424template uint32_t GetDexFlags<ArtMethod>(ArtMethod* member);
David Brazdile7681822018-12-14 16:25:33 +0000425template void MaybeReportCorePlatformApiViolation(ArtField* member,
426 const AccessContext& caller_context,
427 AccessMethod access_method);
428template void MaybeReportCorePlatformApiViolation(ArtMethod* member,
429 const AccessContext& caller_context,
430 AccessMethod access_method);
David Brazdilf50ac102018-10-17 18:00:06 +0100431template bool ShouldDenyAccessToMemberImpl<ArtField>(ArtField* member,
David Brazdile7681822018-12-14 16:25:33 +0000432 ApiList api_list,
David Brazdilf50ac102018-10-17 18:00:06 +0100433 AccessMethod access_method);
434template bool ShouldDenyAccessToMemberImpl<ArtMethod>(ArtMethod* member,
David Brazdile7681822018-12-14 16:25:33 +0000435 ApiList api_list,
David Brazdilf50ac102018-10-17 18:00:06 +0100436 AccessMethod access_method);
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700437} // namespace detail
Narayan Kamathe453a8d2018-04-03 15:23:46 +0100438
Andreas Gampe80f5fe52018-03-28 16:23:24 -0700439} // namespace hiddenapi
440} // namespace art