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 | |
| 19 | #include "base/dumpable.h" |
| 20 | |
| 21 | namespace art { |
| 22 | namespace hiddenapi { |
| 23 | |
| 24 | static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) { |
| 25 | switch (value) { |
| 26 | case kReflection: |
| 27 | os << "reflection"; |
| 28 | break; |
| 29 | case kJNI: |
| 30 | os << "JNI"; |
| 31 | break; |
| 32 | case kLinking: |
| 33 | os << "linking"; |
| 34 | break; |
| 35 | } |
| 36 | return os; |
| 37 | } |
| 38 | |
| 39 | static constexpr bool EnumsEqual(EnforcementPolicy policy, HiddenApiAccessFlags::ApiList apiList) { |
| 40 | return static_cast<int>(policy) == static_cast<int>(apiList); |
| 41 | } |
| 42 | |
| 43 | // GetMemberAction-related static_asserts. |
| 44 | static_assert( |
| 45 | EnumsEqual(EnforcementPolicy::kAllLists, HiddenApiAccessFlags::kLightGreylist) && |
| 46 | EnumsEqual(EnforcementPolicy::kDarkGreyAndBlackList, HiddenApiAccessFlags::kDarkGreylist) && |
| 47 | EnumsEqual(EnforcementPolicy::kBlacklistOnly, HiddenApiAccessFlags::kBlacklist), |
| 48 | "Mismatch between EnforcementPolicy and ApiList enums"); |
| 49 | static_assert( |
| 50 | EnforcementPolicy::kAllLists < EnforcementPolicy::kDarkGreyAndBlackList && |
| 51 | EnforcementPolicy::kDarkGreyAndBlackList < EnforcementPolicy::kBlacklistOnly, |
| 52 | "EnforcementPolicy values ordering not correct"); |
| 53 | |
| 54 | namespace detail { |
| 55 | |
| 56 | MemberSignature::MemberSignature(ArtField* field) { |
| 57 | member_type_ = "field"; |
| 58 | signature_parts_ = { |
| 59 | field->GetDeclaringClass()->GetDescriptor(&tmp_), |
| 60 | "->", |
| 61 | field->GetName(), |
| 62 | ":", |
| 63 | field->GetTypeDescriptor() |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | MemberSignature::MemberSignature(ArtMethod* method) { |
| 68 | member_type_ = "method"; |
| 69 | signature_parts_ = { |
| 70 | method->GetDeclaringClass()->GetDescriptor(&tmp_), |
| 71 | "->", |
| 72 | method->GetName(), |
| 73 | method->GetSignature().ToString() |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const { |
| 78 | size_t pos = 0; |
| 79 | for (const std::string& part : signature_parts_) { |
| 80 | size_t count = std::min(prefix.length() - pos, part.length()); |
| 81 | if (prefix.compare(pos, count, part, 0, count) == 0) { |
| 82 | pos += count; |
| 83 | } else { |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | // We have a complete match if all parts match (we exit the loop without |
| 88 | // returning) AND we've matched the whole prefix. |
| 89 | return pos == prefix.length(); |
| 90 | } |
| 91 | |
| 92 | bool MemberSignature::IsExempted(const std::vector<std::string>& exemptions) { |
| 93 | for (const std::string& exemption : exemptions) { |
| 94 | if (DoesPrefixMatch(exemption)) { |
| 95 | return true; |
| 96 | } |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | void MemberSignature::Dump(std::ostream& os) const { |
| 102 | for (std::string part : signature_parts_) { |
| 103 | os << part; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void MemberSignature::WarnAboutAccess(AccessMethod access_method, |
| 108 | HiddenApiAccessFlags::ApiList list) { |
| 109 | LOG(WARNING) << "Accessing hidden " << member_type_ << " " << Dumpable<MemberSignature>(*this) |
| 110 | << " (" << list << ", " << access_method << ")"; |
| 111 | } |
| 112 | |
| 113 | template<typename T> |
| 114 | bool ShouldBlockAccessToMemberImpl(T* member, Action action, AccessMethod access_method) { |
| 115 | // Get the signature, we need it later. |
| 116 | MemberSignature member_signature(member); |
| 117 | |
| 118 | Runtime* runtime = Runtime::Current(); |
| 119 | |
| 120 | if (action == kDeny) { |
| 121 | // If we were about to deny, check for an exemption first. |
| 122 | // Exempted APIs are treated as light grey list. |
| 123 | if (member_signature.IsExempted(runtime->GetHiddenApiExemptions())) { |
| 124 | action = kAllowButWarn; |
| 125 | // Avoid re-examining the exemption list next time. |
| 126 | // Note this results in the warning below showing "light greylist", which |
| 127 | // seems like what one would expect. Exemptions effectively add new members to |
| 128 | // the light greylist. |
| 129 | member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime( |
| 130 | member->GetAccessFlags(), HiddenApiAccessFlags::kLightGreylist)); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Print a log message with information about this class member access. |
| 135 | // We do this regardless of whether we block the access or not. |
| 136 | member_signature.WarnAboutAccess(access_method, |
| 137 | HiddenApiAccessFlags::DecodeFromRuntime(member->GetAccessFlags())); |
| 138 | |
| 139 | if (action == kDeny) { |
| 140 | // Block access |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | // Allow access to this member but print a warning. |
| 145 | DCHECK(action == kAllowButWarn || action == kAllowButWarnAndToast); |
| 146 | |
| 147 | // Depending on a runtime flag, we might move the member into whitelist and |
| 148 | // skip the warning the next time the member is accessed. |
| 149 | if (runtime->ShouldDedupeHiddenApiWarnings()) { |
| 150 | member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime( |
| 151 | member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist)); |
| 152 | } |
| 153 | |
| 154 | // If this action requires a UI warning, set the appropriate flag. |
| 155 | if (action == kAllowButWarnAndToast || runtime->ShouldAlwaysSetHiddenApiWarningFlag()) { |
| 156 | runtime->SetPendingHiddenApiWarning(true); |
| 157 | } |
| 158 | |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // Need to instantiate this. |
| 163 | template bool ShouldBlockAccessToMemberImpl<ArtField>(ArtField* member, |
| 164 | Action action, |
| 165 | AccessMethod access_method); |
| 166 | template bool ShouldBlockAccessToMemberImpl<ArtMethod>(ArtMethod* member, |
| 167 | Action action, |
| 168 | AccessMethod access_method); |
| 169 | |
| 170 | } // namespace detail |
| 171 | } // namespace hiddenapi |
| 172 | } // namespace art |