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 | |
| 32 | namespace art { |
| 33 | namespace hiddenapi { |
| 34 | |
Mathew Inwood | 27199e6 | 2018-04-11 16:08:21 +0100 | [diff] [blame] | 35 | // 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 Inwood | 6d6012e | 2018-04-12 15:43:11 +0100 | [diff] [blame] | 38 | // 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 Inwood | 015a7ec | 2018-05-16 11:18:10 +0100 | [diff] [blame] | 41 | static constexpr bool kLogAllAccesses = false; |
Mathew Inwood | 27199e6 | 2018-04-11 16:08:21 +0100 | [diff] [blame] | 42 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 43 | static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) { |
| 44 | switch (value) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 45 | case AccessMethod::kNone: |
David Brazdil | 54a99cf | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 46 | LOG(FATAL) << "Internal access to hidden API should not be logged"; |
| 47 | UNREACHABLE(); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 48 | case AccessMethod::kReflection: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 49 | os << "reflection"; |
| 50 | break; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 51 | case AccessMethod::kJNI: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 52 | os << "JNI"; |
| 53 | break; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 54 | case AccessMethod::kLinking: |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 55 | os << "linking"; |
| 56 | break; |
| 57 | } |
| 58 | return os; |
| 59 | } |
| 60 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 61 | static 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 Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 74 | namespace detail { |
| 75 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 76 | // 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. |
| 78 | enum 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 Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 85 | MemberSignature::MemberSignature(ArtField* field) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 86 | class_name_ = field->GetDeclaringClass()->GetDescriptor(&tmp_); |
| 87 | member_name_ = field->GetName(); |
| 88 | type_signature_ = field->GetTypeDescriptor(); |
| 89 | type_ = kField; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | MemberSignature::MemberSignature(ArtMethod* method) { |
David Brazdil | 73a64f6 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 93 | // If this is a proxy method, print the signature of the interface method. |
| 94 | method = method->GetInterfaceMethodIfProxy( |
| 95 | Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 96 | |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 97 | class_name_ = method->GetDeclaringClass()->GetDescriptor(&tmp_); |
| 98 | member_name_ = method->GetName(); |
| 99 | type_signature_ = method->GetSignature().ToString(); |
| 100 | type_ = kMethod; |
| 101 | } |
| 102 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 103 | MemberSignature::MemberSignature(const ClassAccessor::Field& field) { |
| 104 | const DexFile& dex_file = field.GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 105 | const dex::FieldId& field_id = dex_file.GetFieldId(field.GetIndex()); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 106 | 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 | |
| 112 | MemberSignature::MemberSignature(const ClassAccessor::Method& method) { |
| 113 | const DexFile& dex_file = method.GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 114 | const dex::MethodId& method_id = dex_file.GetMethodId(method.GetIndex()); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 115 | 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 Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 121 | inline 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 Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const { |
| 131 | size_t pos = 0; |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 132 | for (const char* part : GetSignatureParts()) { |
| 133 | size_t count = std::min(prefix.length() - pos, strlen(part)); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 134 | 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 | |
| 145 | bool 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 | |
| 154 | void MemberSignature::Dump(std::ostream& os) const { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 155 | for (const char* part : GetSignatureParts()) { |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 156 | os << part; |
| 157 | } |
| 158 | } |
| 159 | |
David Brazdil | 47cd272 | 2018-10-23 12:50:02 +0100 | [diff] [blame] | 160 | void MemberSignature::WarnAboutAccess(AccessMethod access_method, hiddenapi::ApiList list) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 161 | LOG(WARNING) << "Accessing hidden " << (type_ == kField ? "field " : "method ") |
| 162 | << Dumpable<MemberSignature>(*this) << " (" << list << ", " << access_method << ")"; |
| 163 | } |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 164 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 165 | bool 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 | |
| 172 | bool MemberSignature::MemberNameAndTypeMatch(const MemberSignature& other) { |
| 173 | return member_name_ == other.member_name_ && type_signature_ == other.type_signature_; |
| 174 | } |
| 175 | |
Andrei Onea | 6ad020d | 2019-02-18 12:15:51 +0000 | [diff] [blame^] | 176 | void MemberSignature::LogAccessToEventLog(uint32_t sampled_value, |
| 177 | AccessMethod access_method, |
| 178 | bool access_denied) { |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 179 | #ifdef ART_TARGET_ANDROID |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 180 | if (access_method == AccessMethod::kLinking || access_method == AccessMethod::kNone) { |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 181 | // 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 Inwood | f59ca61 | 2018-05-03 11:30:01 +0100 | [diff] [blame] | 184 | // None does not correspond to actual access, so should also be ignored. |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 185 | return; |
| 186 | } |
Andrei Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 187 | Runtime* runtime = Runtime::Current(); |
| 188 | if (runtime->IsAotCompiler()) { |
| 189 | return; |
Mathew Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 190 | } |
Andrei Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 191 | JNIEnvExt* env = Thread::Current()->GetJniEnv(); |
Mathew Inwood | 5bcef17 | 2018-05-01 14:40:12 +0100 | [diff] [blame] | 192 | const std::string& package_name = Runtime::Current()->GetProcessPackageName(); |
Andrei Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 193 | 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 Inwood | 5bcef17 | 2018-05-01 14:40:12 +0100 | [diff] [blame] | 197 | } |
Mathew Inwood | 2d4d62f | 2018-04-12 13:56:37 +0100 | [diff] [blame] | 198 | std::ostringstream signature_str; |
| 199 | Dump(signature_str); |
Andrei Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 200 | 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 Onea | 6ad020d | 2019-02-18 12:15:51 +0000 | [diff] [blame^] | 207 | 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 Onea | a2d2bc2 | 2019-01-25 16:18:53 +0000 | [diff] [blame] | 213 | if (env->ExceptionCheck()) { |
| 214 | env->ExceptionClear(); |
| 215 | LOG(ERROR) << "Unable to report hidden api usage"; |
| 216 | } |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 217 | #else |
Andrei Onea | 6ad020d | 2019-02-18 12:15:51 +0000 | [diff] [blame^] | 218 | UNUSED(sampled_value); |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 219 | UNUSED(access_method); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 220 | UNUSED(access_denied); |
Nicolas Geoffray | 8a22907 | 2018-05-10 16:34:14 +0100 | [diff] [blame] | 221 | #endif |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 222 | } |
| 223 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 224 | void 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 Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 256 | static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtField*) { |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 257 | return true; |
| 258 | } |
| 259 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 260 | static ALWAYS_INLINE bool CanUpdateRuntimeFlags(ArtMethod* method) { |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 261 | return !method->IsIntrinsic(); |
| 262 | } |
| 263 | |
| 264 | template<typename T> |
| 265 | static ALWAYS_INLINE void MaybeWhitelistMember(Runtime* runtime, T* member) |
| 266 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 267 | if (CanUpdateRuntimeFlags(member) && runtime->ShouldDedupeHiddenApiWarnings()) { |
| 268 | member->SetAccessFlags(member->GetAccessFlags() | kAccPublicApi); |
David Brazdil | 8a6b2f3 | 2018-04-26 16:52:11 +0100 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 272 | static ALWAYS_INLINE uint32_t GetMemberDexIndex(ArtField* field) { |
| 273 | return field->GetDexFieldIndex(); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 274 | } |
| 275 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 276 | static 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 Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 282 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 283 | static void VisitMembers(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 284 | const dex::ClassDef& class_def, |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 285 | 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 | |
| 290 | static void VisitMembers(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 291 | const dex::ClassDef& class_def, |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 292 | 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 | |
| 297 | template<typename T> |
| 298 | uint32_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 Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 304 | DCHECK(!declaring_class.IsNull()) << "Attempting to access a runtime method"; |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 305 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 306 | ApiList flags; |
| 307 | DCHECK(!flags.IsValid()); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 308 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 309 | // 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 Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 317 | const dex::ClassDef* class_def = declaring_class->GetClassDef(); |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 318 | 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 Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 327 | } 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 Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 336 | const dex::ClassDef& original_class_def = original_dex->GetClassDef(class_def_idx); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 337 | 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 Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 342 | flags = ApiList(dex_member.GetHiddenapiFlags()); |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 343 | } |
| 344 | }; |
| 345 | VisitMembers(*original_dex, original_class_def, fn_visit); |
| 346 | } |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 347 | |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 348 | CHECK(flags.IsValid()) << "Could not find hiddenapi flags for " |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 349 | << Dumpable<MemberSignature>(MemberSignature(member)); |
David Brazdil | 90faceb | 2018-12-14 14:36:15 +0000 | [diff] [blame] | 350 | return flags.GetDexFlags(); |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 353 | template<typename T> |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 354 | void 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 | |
| 364 | template<typename T> |
| 365 | bool ShouldDenyAccessToMemberImpl(T* member, ApiList api_list, AccessMethod access_method) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 366 | DCHECK(member != nullptr); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 367 | Runtime* runtime = Runtime::Current(); |
David Brazdil | c5a96e4 | 2019-01-09 10:04:45 +0000 | [diff] [blame] | 368 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 369 | EnforcementPolicy policy = runtime->GetHiddenApiEnforcementPolicy(); |
David Brazdil | c5a96e4 | 2019-01-09 10:04:45 +0000 | [diff] [blame] | 370 | DCHECK(policy != EnforcementPolicy::kDisabled) |
| 371 | << "Should never enter this function when access checks are completely disabled"; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 372 | |
| 373 | const bool deny_access = |
| 374 | (policy == EnforcementPolicy::kEnabled) && |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 375 | IsSdkVersionSetAndMoreThan(runtime->GetTargetSdkVersion(), |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 376 | api_list.GetMaxAllowedSdkVersion()); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 377 | |
| 378 | MemberSignature member_signature(member); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 379 | |
Mathew Inwood | c8ce5f5 | 2018-04-05 13:58:55 +0100 | [diff] [blame] | 380 | // Check for an exemption first. Exempted APIs are treated as white list. |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 381 | 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 Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 388 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 389 | 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 Brazdil | b8c6619 | 2018-04-23 13:51:16 +0100 | [diff] [blame] | 393 | member_signature.WarnAboutAccess(access_method, api_list); |
Mathew Inwood | c8ce5f5 | 2018-04-05 13:58:55 +0100 | [diff] [blame] | 394 | } |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 395 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 396 | // 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 Onea | 6ad020d | 2019-02-18 12:15:51 +0000 | [diff] [blame^] | 404 | 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 Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 409 | } |
| 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 Inwood | 73ddda4 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 419 | return deny_access; |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 420 | } |
| 421 | |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 422 | // Need to instantiate these. |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 423 | template uint32_t GetDexFlags<ArtField>(ArtField* member); |
| 424 | template uint32_t GetDexFlags<ArtMethod>(ArtMethod* member); |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 425 | template void MaybeReportCorePlatformApiViolation(ArtField* member, |
| 426 | const AccessContext& caller_context, |
| 427 | AccessMethod access_method); |
| 428 | template void MaybeReportCorePlatformApiViolation(ArtMethod* member, |
| 429 | const AccessContext& caller_context, |
| 430 | AccessMethod access_method); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 431 | template bool ShouldDenyAccessToMemberImpl<ArtField>(ArtField* member, |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 432 | ApiList api_list, |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 433 | AccessMethod access_method); |
| 434 | template bool ShouldDenyAccessToMemberImpl<ArtMethod>(ArtMethod* member, |
David Brazdil | e768182 | 2018-12-14 16:25:33 +0000 | [diff] [blame] | 435 | ApiList api_list, |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 436 | AccessMethod access_method); |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 437 | } // namespace detail |
Narayan Kamath | e453a8d | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 438 | |
Andreas Gampe | 80f5fe5 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 439 | } // namespace hiddenapi |
| 440 | } // namespace art |