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 | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 24 | #include "class_root.h" |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 25 | #include "dex/class_accessor-inl.h" |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 26 | #include "dex/dex_file_loader.h" |
| 27 | #include "mirror/class_ext.h" |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 28 | #include "scoped_thread_state_change.h" |
| 29 | #include "thread-inl.h" |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 30 | #include "well_known_classes.h" |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 31 | |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 32 | #ifdef ART_TARGET_ANDROID |
| 33 | #include <metricslogger/metrics_logger.h> |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 34 | using android::metricslogger::ComplexEventLogger; |
| 35 | using android::metricslogger::ACTION_HIDDEN_API_ACCESSED; |
| 36 | using android::metricslogger::FIELD_HIDDEN_API_ACCESS_METHOD; |
| 37 | using android::metricslogger::FIELD_HIDDEN_API_ACCESS_DENIED; |
| 38 | using android::metricslogger::FIELD_HIDDEN_API_SIGNATURE; |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 39 | #endif |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 40 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 41 | namespace art { |
| 42 | namespace hiddenapi { |
| 43 | |
Mathew Inwood | 27199e6 | 2018-04-11 16:08:21 +0100 | [diff] [blame] | 44 | // 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 Inwood | 6d6012e | 2018-04-12 15:43:11 +0100 | [diff] [blame] | 47 | // 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 Inwood | 015a7ec | 2018-05-16 11:18:10 +0100 | [diff] [blame] | 50 | static constexpr bool kLogAllAccesses = false; |
Mathew Inwood | 27199e6 | 2018-04-11 16:08:21 +0100 | [diff] [blame] | 51 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 52 | static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) { |
| 53 | switch (value) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 54 | case AccessMethod::kNone: |
David Brazdil | 54a99cf | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 55 | LOG(FATAL) << "Internal access to hidden API should not be logged"; |
| 56 | UNREACHABLE(); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 57 | case AccessMethod::kReflection: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 58 | os << "reflection"; |
| 59 | break; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 60 | case AccessMethod::kJNI: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 61 | os << "JNI"; |
| 62 | break; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 63 | case AccessMethod::kLinking: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 64 | os << "linking"; |
| 65 | break; |
| 66 | } |
| 67 | return os; |
| 68 | } |
| 69 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 70 | static 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 Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 83 | namespace detail { |
| 84 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 85 | // 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. |
| 87 | enum 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 Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 94 | MemberSignature::MemberSignature(ArtField* field) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 95 | class_name_ = field->GetDeclaringClass()->GetDescriptor(&tmp_); |
| 96 | member_name_ = field->GetName(); |
| 97 | type_signature_ = field->GetTypeDescriptor(); |
| 98 | type_ = kField; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | MemberSignature::MemberSignature(ArtMethod* method) { |
David Brazdil | 73a64f6 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 102 | // If this is a proxy method, print the signature of the interface method. |
| 103 | method = method->GetInterfaceMethodIfProxy( |
| 104 | Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 105 | |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 106 | class_name_ = method->GetDeclaringClass()->GetDescriptor(&tmp_); |
| 107 | member_name_ = method->GetName(); |
| 108 | type_signature_ = method->GetSignature().ToString(); |
| 109 | type_ = kMethod; |
| 110 | } |
| 111 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 112 | MemberSignature::MemberSignature(const ClassAccessor::Field& field) { |
| 113 | const DexFile& dex_file = field.GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 114 | const dex::FieldId& field_id = dex_file.GetFieldId(field.GetIndex()); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 115 | 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 | |
| 121 | MemberSignature::MemberSignature(const ClassAccessor::Method& method) { |
| 122 | const DexFile& dex_file = method.GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 123 | const dex::MethodId& method_id = dex_file.GetMethodId(method.GetIndex()); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 124 | 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 Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 130 | inline 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 Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const { |
| 140 | size_t pos = 0; |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 141 | for (const char* part : GetSignatureParts()) { |
| 142 | size_t count = std::min(prefix.length() - pos, strlen(part)); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 143 | 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 | |
| 154 | bool 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 | |
| 163 | void MemberSignature::Dump(std::ostream& os) const { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 164 | for (const char* part : GetSignatureParts()) { |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 165 | os << part; |
| 166 | } |
| 167 | } |
| 168 | |
David Brazdil | 47cd272 | 2018-10-23 12:50:02 +0100 | [diff] [blame] | 169 | void MemberSignature::WarnAboutAccess(AccessMethod access_method, hiddenapi::ApiList list) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 170 | LOG(WARNING) << "Accessing hidden " << (type_ == kField ? "field " : "method ") |
| 171 | << Dumpable<MemberSignature>(*this) << " (" << list << ", " << access_method << ")"; |
| 172 | } |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 173 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 174 | bool 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 | |
| 181 | bool MemberSignature::MemberNameAndTypeMatch(const MemberSignature& other) { |
| 182 | return member_name_ == other.member_name_ && type_signature_ == other.type_signature_; |
| 183 | } |
| 184 | |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 185 | #ifdef ART_TARGET_ANDROID |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 186 | // 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. |
| 191 | inline static int32_t GetEnumValueForLog(AccessMethod access_method) { |
| 192 | switch (access_method) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 193 | case AccessMethod::kNone: |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 194 | return android::metricslogger::ACCESS_METHOD_NONE; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 195 | case AccessMethod::kReflection: |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 196 | return android::metricslogger::ACCESS_METHOD_REFLECTION; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 197 | case AccessMethod::kJNI: |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 198 | return android::metricslogger::ACCESS_METHOD_JNI; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 199 | case AccessMethod::kLinking: |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 200 | return android::metricslogger::ACCESS_METHOD_LINKING; |
| 201 | default: |
| 202 | DCHECK(false); |
| 203 | } |
| 204 | } |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 205 | #endif |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 206 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 207 | void MemberSignature::LogAccessToEventLog(AccessMethod access_method, bool access_denied) { |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 208 | #ifdef ART_TARGET_ANDROID |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 209 | if (access_method == AccessMethod::kLinking || access_method == AccessMethod::kNone) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 210 | // 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 Inwood | f59ca61 | 2018-05-03 11:30:01 +0100 | [diff] [blame] | 213 | // None does not correspond to actual access, so should also be ignored. |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 214 | return; |
| 215 | } |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 216 | ComplexEventLogger log_maker(ACTION_HIDDEN_API_ACCESSED); |
| 217 | log_maker.AddTaggedData(FIELD_HIDDEN_API_ACCESS_METHOD, GetEnumValueForLog(access_method)); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 218 | if (access_denied) { |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 219 | log_maker.AddTaggedData(FIELD_HIDDEN_API_ACCESS_DENIED, 1); |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 220 | } |
Mathew Inwood | 5bcef17 | 2018-05-01 14:40:12 +0100 | [diff] [blame] | 221 | const std::string& package_name = Runtime::Current()->GetProcessPackageName(); |
| 222 | if (!package_name.empty()) { |
| 223 | log_maker.SetPackageName(package_name); |
| 224 | } |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 225 | std::ostringstream signature_str; |
| 226 | Dump(signature_str); |
| 227 | log_maker.AddTaggedData(FIELD_HIDDEN_API_SIGNATURE, signature_str.str()); |
| 228 | log_maker.Record(); |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 229 | #else |
| 230 | UNUSED(access_method); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 231 | UNUSED(access_denied); |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 232 | #endif |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 233 | } |
| 234 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 235 | void 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 Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 267 | static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtField*) { |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 268 | return true; |
| 269 | } |
| 270 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 271 | static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtMethod* method) { |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 272 | return !method->IsIntrinsic(); |
| 273 | } |
| 274 | |
| 275 | template<typename T> |
| 276 | static ALWAYS_INLINE void MaybeWhitelistMember(Runtime* runtime, T* member) |
| 277 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 278 | if (CanUpdateRuntimeFlags(member) && runtime->ShouldDedupeHiddenApiWarnings()) { |
| 279 | member->SetAccessFlags(member->GetAccessFlags() | kAccPublicApi); |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 283 | static ALWAYS_INLINE uint32_t GetMemberDexIndex(ArtField* field) { |
| 284 | return field->GetDexFieldIndex(); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 285 | } |
| 286 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 287 | static 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 Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 293 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 294 | static void VisitMembers(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 295 | const dex::ClassDef& class_def, |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 296 | 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 | |
| 301 | static void VisitMembers(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 302 | const dex::ClassDef& class_def, |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 303 | 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 | |
| 308 | template<typename T> |
| 309 | uint32_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 Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 315 | DCHECK(!declaring_class.IsNull()) << "Attempting to access a runtime method"; |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 316 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 317 | ApiList flags; |
| 318 | DCHECK(!flags.IsValid()); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 319 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 320 | // 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 Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 328 | const dex::ClassDef* class_def = declaring_class->GetClassDef(); |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 329 | 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 Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 338 | } 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 Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 347 | const dex::ClassDef& original_class_def = original_dex->GetClassDef(class_def_idx); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 348 | 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 Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 353 | flags = ApiList(dex_member.GetHiddenapiFlags()); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 354 | } |
| 355 | }; |
| 356 | VisitMembers(*original_dex, original_class_def, fn_visit); |
| 357 | } |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 358 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 359 | CHECK(flags.IsValid()) << "Could not find hiddenapi flags for " |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 360 | << Dumpable<MemberSignature>(MemberSignature(member)); |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 361 | return flags.GetDexFlags(); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 364 | template<typename T> |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 365 | void 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 | |
| 375 | template<typename T> |
| 376 | bool ShouldDenyAccessToMemberImpl(T* member, ApiList api_list, AccessMethod access_method) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 377 | DCHECK(member != nullptr); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 378 | Runtime* runtime = Runtime::Current(); |
David Brazdil | c5a96e4 | 2019-01-09 10:04:45 +0000 | [diff] [blame] | 379 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 380 | EnforcementPolicy policy = runtime->GetHiddenApiEnforcementPolicy(); |
David Brazdil | c5a96e4 | 2019-01-09 10:04:45 +0000 | [diff] [blame] | 381 | DCHECK(policy != EnforcementPolicy::kDisabled) |
| 382 | << "Should never enter this function when access checks are completely disabled"; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 383 | |
| 384 | const bool deny_access = |
| 385 | (policy == EnforcementPolicy::kEnabled) && |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 386 | IsSdkVersionSetAndMoreThan(runtime->GetTargetSdkVersion(), |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 387 | api_list.GetMaxAllowedSdkVersion()); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 388 | |
| 389 | MemberSignature member_signature(member); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 390 | |
Mathew Inwood | c8ce5f5 | 2018-04-05 13:58:55 +0100 | [diff] [blame] | 391 | // Check for an exemption first. Exempted APIs are treated as white list. |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 392 | 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 Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 399 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 400 | 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 Brazdil | b8c6619 | 2018-04-23 13:51:16 +0100 | [diff] [blame] | 404 | member_signature.WarnAboutAccess(access_method, api_list); |
Mathew Inwood | c8ce5f5 | 2018-04-05 13:58:55 +0100 | [diff] [blame] | 405 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 406 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 407 | // 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 Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 428 | return deny_access; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 429 | } |
| 430 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 431 | // Need to instantiate these. |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 432 | template uint32_t GetDexFlags<ArtField>(ArtField* member); |
| 433 | template uint32_t GetDexFlags<ArtMethod>(ArtMethod* member); |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 434 | template void MaybeReportCorePlatformApiViolation(ArtField* member, |
| 435 | const AccessContext& caller_context, |
| 436 | AccessMethod access_method); |
| 437 | template void MaybeReportCorePlatformApiViolation(ArtMethod* member, |
| 438 | const AccessContext& caller_context, |
| 439 | AccessMethod access_method); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 440 | template bool ShouldDenyAccessToMemberImpl<ArtField>(ArtField* member, |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 441 | ApiList api_list, |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 442 | AccessMethod access_method); |
| 443 | template bool ShouldDenyAccessToMemberImpl<ArtMethod>(ArtMethod* member, |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 444 | ApiList api_list, |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 445 | AccessMethod access_method); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 446 | } // namespace detail |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 447 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 448 | } // namespace hiddenapi |
| 449 | } // namespace art |