Andreas Gampe | aa12001 | 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 | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 19 | #include <nativehelper/scoped_local_ref.h> |
| 20 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 21 | #include "base/dumpable.h" |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 22 | #include "thread-current-inl.h" |
| 23 | #include "well_known_classes.h" |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | namespace hiddenapi { |
| 27 | |
| 28 | static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) { |
| 29 | switch (value) { |
| 30 | case kReflection: |
| 31 | os << "reflection"; |
| 32 | break; |
| 33 | case kJNI: |
| 34 | os << "JNI"; |
| 35 | break; |
| 36 | case kLinking: |
| 37 | os << "linking"; |
| 38 | break; |
| 39 | } |
| 40 | return os; |
| 41 | } |
| 42 | |
| 43 | static constexpr bool EnumsEqual(EnforcementPolicy policy, HiddenApiAccessFlags::ApiList apiList) { |
| 44 | return static_cast<int>(policy) == static_cast<int>(apiList); |
| 45 | } |
| 46 | |
| 47 | // GetMemberAction-related static_asserts. |
| 48 | static_assert( |
| 49 | EnumsEqual(EnforcementPolicy::kAllLists, HiddenApiAccessFlags::kLightGreylist) && |
| 50 | EnumsEqual(EnforcementPolicy::kDarkGreyAndBlackList, HiddenApiAccessFlags::kDarkGreylist) && |
| 51 | EnumsEqual(EnforcementPolicy::kBlacklistOnly, HiddenApiAccessFlags::kBlacklist), |
| 52 | "Mismatch between EnforcementPolicy and ApiList enums"); |
| 53 | static_assert( |
| 54 | EnforcementPolicy::kAllLists < EnforcementPolicy::kDarkGreyAndBlackList && |
| 55 | EnforcementPolicy::kDarkGreyAndBlackList < EnforcementPolicy::kBlacklistOnly, |
| 56 | "EnforcementPolicy values ordering not correct"); |
| 57 | |
| 58 | namespace detail { |
| 59 | |
| 60 | MemberSignature::MemberSignature(ArtField* field) { |
| 61 | member_type_ = "field"; |
| 62 | signature_parts_ = { |
| 63 | field->GetDeclaringClass()->GetDescriptor(&tmp_), |
| 64 | "->", |
| 65 | field->GetName(), |
| 66 | ":", |
| 67 | field->GetTypeDescriptor() |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | MemberSignature::MemberSignature(ArtMethod* method) { |
| 72 | member_type_ = "method"; |
| 73 | signature_parts_ = { |
| 74 | method->GetDeclaringClass()->GetDescriptor(&tmp_), |
| 75 | "->", |
| 76 | method->GetName(), |
| 77 | method->GetSignature().ToString() |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const { |
| 82 | size_t pos = 0; |
| 83 | for (const std::string& part : signature_parts_) { |
| 84 | size_t count = std::min(prefix.length() - pos, part.length()); |
| 85 | if (prefix.compare(pos, count, part, 0, count) == 0) { |
| 86 | pos += count; |
| 87 | } else { |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | // We have a complete match if all parts match (we exit the loop without |
| 92 | // returning) AND we've matched the whole prefix. |
| 93 | return pos == prefix.length(); |
| 94 | } |
| 95 | |
| 96 | bool MemberSignature::IsExempted(const std::vector<std::string>& exemptions) { |
| 97 | for (const std::string& exemption : exemptions) { |
| 98 | if (DoesPrefixMatch(exemption)) { |
| 99 | return true; |
| 100 | } |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | void MemberSignature::Dump(std::ostream& os) const { |
| 106 | for (std::string part : signature_parts_) { |
| 107 | os << part; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void MemberSignature::WarnAboutAccess(AccessMethod access_method, |
| 112 | HiddenApiAccessFlags::ApiList list) { |
| 113 | LOG(WARNING) << "Accessing hidden " << member_type_ << " " << Dumpable<MemberSignature>(*this) |
| 114 | << " (" << list << ", " << access_method << ")"; |
| 115 | } |
| 116 | |
| 117 | template<typename T> |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 118 | Action GetMemberActionImpl(T* member, Action action, AccessMethod access_method) { |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 119 | // Get the signature, we need it later. |
| 120 | MemberSignature member_signature(member); |
| 121 | |
| 122 | Runtime* runtime = Runtime::Current(); |
| 123 | |
| 124 | if (action == kDeny) { |
| 125 | // If we were about to deny, check for an exemption first. |
| 126 | // Exempted APIs are treated as light grey list. |
| 127 | if (member_signature.IsExempted(runtime->GetHiddenApiExemptions())) { |
| 128 | action = kAllowButWarn; |
| 129 | // Avoid re-examining the exemption list next time. |
| 130 | // Note this results in the warning below showing "light greylist", which |
| 131 | // seems like what one would expect. Exemptions effectively add new members to |
| 132 | // the light greylist. |
| 133 | member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime( |
| 134 | member->GetAccessFlags(), HiddenApiAccessFlags::kLightGreylist)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Print a log message with information about this class member access. |
| 139 | // We do this regardless of whether we block the access or not. |
| 140 | member_signature.WarnAboutAccess(access_method, |
| 141 | HiddenApiAccessFlags::DecodeFromRuntime(member->GetAccessFlags())); |
| 142 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 143 | if (action == kDeny) { |
| 144 | // Block access |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 145 | return action; |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // Allow access to this member but print a warning. |
| 149 | DCHECK(action == kAllowButWarn || action == kAllowButWarnAndToast); |
| 150 | |
| 151 | // Depending on a runtime flag, we might move the member into whitelist and |
| 152 | // skip the warning the next time the member is accessed. |
| 153 | if (runtime->ShouldDedupeHiddenApiWarnings()) { |
| 154 | member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime( |
| 155 | member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist)); |
| 156 | } |
| 157 | |
| 158 | // If this action requires a UI warning, set the appropriate flag. |
| 159 | if (action == kAllowButWarnAndToast || runtime->ShouldAlwaysSetHiddenApiWarningFlag()) { |
| 160 | runtime->SetPendingHiddenApiWarning(true); |
| 161 | } |
| 162 | |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 163 | return action; |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Need to instantiate this. |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 167 | template Action GetMemberActionImpl<ArtField>(ArtField* member, |
| 168 | Action action, |
| 169 | AccessMethod access_method); |
| 170 | template Action GetMemberActionImpl<ArtMethod>(ArtMethod* member, |
| 171 | Action action, |
| 172 | AccessMethod access_method); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 173 | } // namespace detail |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 174 | |
| 175 | template<typename T> |
| 176 | void NotifyHiddenApiListener(T* member) { |
| 177 | Runtime* runtime = Runtime::Current(); |
| 178 | if (!runtime->IsAotCompiler()) { |
| 179 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 180 | |
| 181 | ScopedLocalRef<jobject> consumer_object(soa.Env(), |
| 182 | soa.Env()->GetStaticObjectField( |
| 183 | WellKnownClasses::dalvik_system_VMRuntime, |
| 184 | WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer)); |
| 185 | // If the consumer is non-null, we call back to it to let it know that we |
| 186 | // have encountered an API that's in one of our lists. |
| 187 | if (consumer_object != nullptr) { |
| 188 | detail::MemberSignature member_signature(member); |
| 189 | std::ostringstream member_signature_str; |
| 190 | member_signature.Dump(member_signature_str); |
| 191 | |
| 192 | ScopedLocalRef<jobject> signature_str( |
| 193 | soa.Env(), |
| 194 | soa.Env()->NewStringUTF(member_signature_str.str().c_str())); |
| 195 | |
| 196 | // Call through to Consumer.accept(String memberSignature); |
| 197 | soa.Env()->CallVoidMethod(consumer_object.get(), |
| 198 | WellKnownClasses::java_util_function_Consumer_accept, |
| 199 | signature_str.get()); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | template void NotifyHiddenApiListener<ArtMethod>(ArtMethod* member); |
| 205 | template void NotifyHiddenApiListener<ArtField>(ArtField* member); |
| 206 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 207 | } // namespace hiddenapi |
| 208 | } // namespace art |