Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "hidden_api.h" |
| 18 | |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 19 | #include <nativehelper/scoped_local_ref.h> |
| 20 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 21 | #include "art_field-inl.h" |
| 22 | #include "art_method-inl.h" |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 23 | #include "base/dumpable.h" |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 24 | #include "dex/class_accessor-inl.h" |
| 25 | #include "scoped_thread_state_change.h" |
| 26 | #include "thread-inl.h" |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 27 | #include "well_known_classes.h" |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 28 | |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 29 | #ifdef ART_TARGET_ANDROID |
| 30 | #include <metricslogger/metrics_logger.h> |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 31 | using android::metricslogger::ComplexEventLogger; |
| 32 | using android::metricslogger::ACTION_HIDDEN_API_ACCESSED; |
| 33 | using android::metricslogger::FIELD_HIDDEN_API_ACCESS_METHOD; |
| 34 | using android::metricslogger::FIELD_HIDDEN_API_ACCESS_DENIED; |
| 35 | using android::metricslogger::FIELD_HIDDEN_API_SIGNATURE; |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 36 | #endif |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 37 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 38 | namespace art { |
| 39 | namespace hiddenapi { |
| 40 | |
Mathew Inwood | 27199e6 | 2018-04-11 16:08:21 +0100 | [diff] [blame] | 41 | // Set to true if we should always print a warning in logcat for all hidden API accesses, not just |
| 42 | // dark grey and black. This can be set to true for developer preview / beta builds, but should be |
| 43 | // false for public release builds. |
Mathew Inwood | 6d6012e | 2018-04-12 15:43:11 +0100 | [diff] [blame] | 44 | // Note that when flipping this flag, you must also update the expectations of test 674-hiddenapi |
| 45 | // as it affects whether or not we warn for light grey APIs that have been added to the exemptions |
| 46 | // list. |
Mathew Inwood | 015a7ec | 2018-05-16 11:18:10 +0100 | [diff] [blame] | 47 | static constexpr bool kLogAllAccesses = false; |
Mathew Inwood | 27199e6 | 2018-04-11 16:08:21 +0100 | [diff] [blame] | 48 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 49 | static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) { |
| 50 | switch (value) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 51 | case AccessMethod::kNone: |
David Brazdil | 54a99cf | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 52 | LOG(FATAL) << "Internal access to hidden API should not be logged"; |
| 53 | UNREACHABLE(); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 54 | case AccessMethod::kReflection: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 55 | os << "reflection"; |
| 56 | break; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 57 | case AccessMethod::kJNI: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 58 | os << "JNI"; |
| 59 | break; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 60 | case AccessMethod::kLinking: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 61 | os << "linking"; |
| 62 | break; |
| 63 | } |
| 64 | return os; |
| 65 | } |
| 66 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 67 | namespace detail { |
| 68 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 69 | // Do not change the values of items in this enum, as they are written to the |
| 70 | // event log for offline analysis. Any changes will interfere with that analysis. |
| 71 | enum AccessContextFlags { |
| 72 | // Accessed member is a field if this bit is set, else a method |
| 73 | kMemberIsField = 1 << 0, |
| 74 | // Indicates if access was denied to the member, instead of just printing a warning. |
| 75 | kAccessDenied = 1 << 1, |
| 76 | }; |
| 77 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 78 | MemberSignature::MemberSignature(ArtField* field) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 79 | class_name_ = field->GetDeclaringClass()->GetDescriptor(&tmp_); |
| 80 | member_name_ = field->GetName(); |
| 81 | type_signature_ = field->GetTypeDescriptor(); |
| 82 | type_ = kField; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | MemberSignature::MemberSignature(ArtMethod* method) { |
David Brazdil | 73a64f6 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 86 | // If this is a proxy method, print the signature of the interface method. |
| 87 | method = method->GetInterfaceMethodIfProxy( |
| 88 | Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 89 | |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 90 | class_name_ = method->GetDeclaringClass()->GetDescriptor(&tmp_); |
| 91 | member_name_ = method->GetName(); |
| 92 | type_signature_ = method->GetSignature().ToString(); |
| 93 | type_ = kMethod; |
| 94 | } |
| 95 | |
| 96 | inline std::vector<const char*> MemberSignature::GetSignatureParts() const { |
| 97 | if (type_ == kField) { |
| 98 | return { class_name_.c_str(), "->", member_name_.c_str(), ":", type_signature_.c_str() }; |
| 99 | } else { |
| 100 | DCHECK_EQ(type_, kMethod); |
| 101 | return { class_name_.c_str(), "->", member_name_.c_str(), type_signature_.c_str() }; |
| 102 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const { |
| 106 | size_t pos = 0; |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 107 | for (const char* part : GetSignatureParts()) { |
| 108 | size_t count = std::min(prefix.length() - pos, strlen(part)); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 109 | if (prefix.compare(pos, count, part, 0, count) == 0) { |
| 110 | pos += count; |
| 111 | } else { |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | // We have a complete match if all parts match (we exit the loop without |
| 116 | // returning) AND we've matched the whole prefix. |
| 117 | return pos == prefix.length(); |
| 118 | } |
| 119 | |
| 120 | bool MemberSignature::IsExempted(const std::vector<std::string>& exemptions) { |
| 121 | for (const std::string& exemption : exemptions) { |
| 122 | if (DoesPrefixMatch(exemption)) { |
| 123 | return true; |
| 124 | } |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | void MemberSignature::Dump(std::ostream& os) const { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 130 | for (const char* part : GetSignatureParts()) { |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 131 | os << part; |
| 132 | } |
| 133 | } |
| 134 | |
David Brazdil | 47cd272 | 2018-10-23 12:50:02 +0100 | [diff] [blame] | 135 | void MemberSignature::WarnAboutAccess(AccessMethod access_method, hiddenapi::ApiList list) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 136 | LOG(WARNING) << "Accessing hidden " << (type_ == kField ? "field " : "method ") |
| 137 | << Dumpable<MemberSignature>(*this) << " (" << list << ", " << access_method << ")"; |
| 138 | } |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 139 | |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 140 | #ifdef ART_TARGET_ANDROID |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 141 | // Convert an AccessMethod enum to a value for logging from the proto enum. |
| 142 | // This method may look odd (the enum values are current the same), but it |
| 143 | // prevents coupling the internal enum to the proto enum (which should never |
| 144 | // be changed) so that we are free to change the internal one if necessary in |
| 145 | // future. |
| 146 | inline static int32_t GetEnumValueForLog(AccessMethod access_method) { |
| 147 | switch (access_method) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 148 | case AccessMethod::kNone: |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 149 | return android::metricslogger::ACCESS_METHOD_NONE; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 150 | case AccessMethod::kReflection: |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 151 | return android::metricslogger::ACCESS_METHOD_REFLECTION; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 152 | case AccessMethod::kJNI: |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 153 | return android::metricslogger::ACCESS_METHOD_JNI; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 154 | case AccessMethod::kLinking: |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 155 | return android::metricslogger::ACCESS_METHOD_LINKING; |
| 156 | default: |
| 157 | DCHECK(false); |
| 158 | } |
| 159 | } |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 160 | #endif |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 161 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 162 | void MemberSignature::LogAccessToEventLog(AccessMethod access_method, bool access_denied) { |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 163 | #ifdef ART_TARGET_ANDROID |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 164 | if (access_method == AccessMethod::kLinking || access_method == AccessMethod::kNone) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 165 | // Linking warnings come from static analysis/compilation of the bytecode |
| 166 | // and can contain false positives (i.e. code that is never run). We choose |
| 167 | // not to log these in the event log. |
Mathew Inwood | f59ca61 | 2018-05-03 11:30:01 +0100 | [diff] [blame] | 168 | // None does not correspond to actual access, so should also be ignored. |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 169 | return; |
| 170 | } |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 171 | ComplexEventLogger log_maker(ACTION_HIDDEN_API_ACCESSED); |
| 172 | log_maker.AddTaggedData(FIELD_HIDDEN_API_ACCESS_METHOD, GetEnumValueForLog(access_method)); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 173 | if (access_denied) { |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 174 | log_maker.AddTaggedData(FIELD_HIDDEN_API_ACCESS_DENIED, 1); |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 175 | } |
Mathew Inwood | 5bcef17 | 2018-05-01 14:40:12 +0100 | [diff] [blame] | 176 | const std::string& package_name = Runtime::Current()->GetProcessPackageName(); |
| 177 | if (!package_name.empty()) { |
| 178 | log_maker.SetPackageName(package_name); |
| 179 | } |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 180 | std::ostringstream signature_str; |
| 181 | Dump(signature_str); |
| 182 | log_maker.AddTaggedData(FIELD_HIDDEN_API_SIGNATURE, signature_str.str()); |
| 183 | log_maker.Record(); |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 184 | #else |
| 185 | UNUSED(access_method); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 186 | UNUSED(access_denied); |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 187 | #endif |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 188 | } |
| 189 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 190 | void MemberSignature::NotifyHiddenApiListener(AccessMethod access_method) { |
| 191 | if (access_method != AccessMethod::kReflection && access_method != AccessMethod::kJNI) { |
| 192 | // We can only up-call into Java during reflection and JNI down-calls. |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | Runtime* runtime = Runtime::Current(); |
| 197 | if (!runtime->IsAotCompiler()) { |
| 198 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 199 | |
| 200 | ScopedLocalRef<jobject> consumer_object(soa.Env(), |
| 201 | soa.Env()->GetStaticObjectField( |
| 202 | WellKnownClasses::dalvik_system_VMRuntime, |
| 203 | WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer)); |
| 204 | // If the consumer is non-null, we call back to it to let it know that we |
| 205 | // have encountered an API that's in one of our lists. |
| 206 | if (consumer_object != nullptr) { |
| 207 | std::ostringstream member_signature_str; |
| 208 | Dump(member_signature_str); |
| 209 | |
| 210 | ScopedLocalRef<jobject> signature_str( |
| 211 | soa.Env(), |
| 212 | soa.Env()->NewStringUTF(member_signature_str.str().c_str())); |
| 213 | |
| 214 | // Call through to Consumer.accept(String memberSignature); |
| 215 | soa.Env()->CallVoidMethod(consumer_object.get(), |
| 216 | WellKnownClasses::java_util_function_Consumer_accept, |
| 217 | signature_str.get()); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 222 | static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtField*) { |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 223 | return true; |
| 224 | } |
| 225 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 226 | static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtMethod* method) { |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 227 | return !method->IsIntrinsic(); |
| 228 | } |
| 229 | |
| 230 | template<typename T> |
| 231 | static ALWAYS_INLINE void MaybeWhitelistMember(Runtime* runtime, T* member) |
| 232 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 233 | if (CanUpdateRuntimeFlags(member) && runtime->ShouldDedupeHiddenApiWarnings()) { |
| 234 | member->SetAccessFlags(member->GetAccessFlags() | kAccPublicApi); |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 238 | static constexpr uint32_t kNoDexFlags = 0u; |
| 239 | static constexpr uint32_t kInvalidDexFlags = static_cast<uint32_t>(-1); |
| 240 | |
| 241 | uint32_t GetDexFlags(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 242 | ObjPtr<mirror::Class> declaring_class = field->GetDeclaringClass(); |
| 243 | DCHECK(declaring_class != nullptr) << "Fields always have a declaring class"; |
| 244 | |
| 245 | const DexFile::ClassDef* class_def = declaring_class->GetClassDef(); |
| 246 | if (class_def == nullptr) { |
| 247 | return kNoDexFlags; |
| 248 | } |
| 249 | |
| 250 | uint32_t flags = kInvalidDexFlags; |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame^] | 251 | DCHECK(!AreValidDexFlags(flags)); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 252 | |
| 253 | ClassAccessor accessor(declaring_class->GetDexFile(), |
| 254 | *class_def, |
| 255 | /* parse_hiddenapi_class_data= */ true); |
| 256 | auto fn_visit = [&](const ClassAccessor::Field& dex_field) { |
| 257 | if (dex_field.GetIndex() == field->GetDexFieldIndex()) { |
| 258 | flags = dex_field.GetHiddenapiFlags(); |
| 259 | } |
| 260 | }; |
| 261 | accessor.VisitFields(fn_visit, fn_visit); |
| 262 | |
| 263 | CHECK_NE(flags, kInvalidDexFlags) << "Could not find flags for field " << field->PrettyField(); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame^] | 264 | DCHECK(AreValidDexFlags(flags)); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 265 | return flags; |
| 266 | } |
| 267 | |
| 268 | uint32_t GetDexFlags(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 269 | ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass(); |
| 270 | if (declaring_class.IsNull()) { |
| 271 | DCHECK(method->IsRuntimeMethod()); |
| 272 | return kNoDexFlags; |
| 273 | } |
| 274 | |
| 275 | const DexFile::ClassDef* class_def = declaring_class->GetClassDef(); |
| 276 | if (class_def == nullptr) { |
| 277 | return kNoDexFlags; |
| 278 | } |
| 279 | |
| 280 | uint32_t flags = kInvalidDexFlags; |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame^] | 281 | DCHECK(!AreValidDexFlags(flags)); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 282 | |
| 283 | ClassAccessor accessor(declaring_class->GetDexFile(), |
| 284 | *class_def, |
| 285 | /* parse_hiddenapi_class_data= */ true); |
| 286 | auto fn_visit = [&](const ClassAccessor::Method& dex_method) { |
| 287 | if (dex_method.GetIndex() == method->GetDexMethodIndex()) { |
| 288 | flags = dex_method.GetHiddenapiFlags(); |
| 289 | } |
| 290 | }; |
| 291 | accessor.VisitMethods(fn_visit, fn_visit); |
| 292 | |
| 293 | CHECK_NE(flags, kInvalidDexFlags) << "Could not find flags for method " << method->PrettyMethod(); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame^] | 294 | DCHECK(AreValidDexFlags(flags)); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 295 | return flags; |
| 296 | } |
| 297 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 298 | template<typename T> |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 299 | bool ShouldDenyAccessToMemberImpl(T* member, |
| 300 | hiddenapi::ApiList api_list, |
| 301 | AccessMethod access_method) { |
| 302 | DCHECK(member != nullptr); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 303 | |
| 304 | Runtime* runtime = Runtime::Current(); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 305 | EnforcementPolicy policy = runtime->GetHiddenApiEnforcementPolicy(); |
| 306 | |
| 307 | const bool deny_access = |
| 308 | (policy == EnforcementPolicy::kEnabled) && |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 309 | IsSdkVersionSetAndMoreThan(runtime->GetTargetSdkVersion(), |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame^] | 310 | api_list.GetMaxAllowedSdkVersion()); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 311 | |
| 312 | MemberSignature member_signature(member); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 313 | |
Mathew Inwood | c8ce5f5 | 2018-04-05 13:58:55 +0100 | [diff] [blame] | 314 | // Check for an exemption first. Exempted APIs are treated as white list. |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 315 | if (member_signature.IsExempted(runtime->GetHiddenApiExemptions())) { |
| 316 | // Avoid re-examining the exemption list next time. |
| 317 | // Note this results in no warning for the member, which seems like what one would expect. |
| 318 | // Exemptions effectively adds new members to the whitelist. |
| 319 | MaybeWhitelistMember(runtime, member); |
| 320 | return false; |
| 321 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 322 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 323 | if (access_method != AccessMethod::kNone) { |
| 324 | // Print a log message with information about this class member access. |
| 325 | // We do this if we're about to deny access, or the app is debuggable. |
| 326 | if (kLogAllAccesses || deny_access || runtime->IsJavaDebuggable()) { |
David Brazdil | b8c6619 | 2018-04-23 13:51:16 +0100 | [diff] [blame] | 327 | member_signature.WarnAboutAccess(access_method, api_list); |
Mathew Inwood | c8ce5f5 | 2018-04-05 13:58:55 +0100 | [diff] [blame] | 328 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 329 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 330 | // If there is a StrictMode listener, notify it about this violation. |
| 331 | member_signature.NotifyHiddenApiListener(access_method); |
| 332 | |
| 333 | // If event log sampling is enabled, report this violation. |
| 334 | if (kIsTargetBuild && !kIsTargetLinux) { |
| 335 | uint32_t eventLogSampleRate = runtime->GetHiddenApiEventLogSampleRate(); |
| 336 | // Assert that RAND_MAX is big enough, to ensure sampling below works as expected. |
| 337 | static_assert(RAND_MAX >= 0xffff, "RAND_MAX too small"); |
| 338 | if (eventLogSampleRate != 0 && |
| 339 | (static_cast<uint32_t>(std::rand()) & 0xffff) < eventLogSampleRate) { |
| 340 | member_signature.LogAccessToEventLog(access_method, deny_access); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // If this access was not denied, move the member into whitelist and skip |
| 345 | // the warning the next time the member is accessed. |
| 346 | if (!deny_access) { |
| 347 | MaybeWhitelistMember(runtime, member); |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 351 | return deny_access; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // Need to instantiate this. |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 355 | template bool ShouldDenyAccessToMemberImpl<ArtField>(ArtField* member, |
| 356 | hiddenapi::ApiList api_list, |
| 357 | AccessMethod access_method); |
| 358 | template bool ShouldDenyAccessToMemberImpl<ArtMethod>(ArtMethod* member, |
| 359 | hiddenapi::ApiList api_list, |
| 360 | AccessMethod access_method); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 361 | } // namespace detail |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 362 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 363 | } // namespace hiddenapi |
| 364 | } // namespace art |