David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [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 | #ifndef ART_RUNTIME_HIDDEN_API_H_ |
| 18 | #define ART_RUNTIME_HIDDEN_API_H_ |
| 19 | |
| 20 | #include "hidden_api_access_flags.h" |
| 21 | #include "reflection.h" |
| 22 | #include "runtime.h" |
| 23 | |
| 24 | namespace art { |
| 25 | namespace hiddenapi { |
| 26 | |
| 27 | // Returns true if member with `access flags` should only be accessed from |
| 28 | // boot class path. |
| 29 | inline bool IsMemberHidden(uint32_t access_flags) { |
| 30 | if (!Runtime::Current()->AreHiddenApiChecksEnabled()) { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | switch (HiddenApiAccessFlags::DecodeFromRuntime(access_flags)) { |
| 35 | case HiddenApiAccessFlags::kWhitelist: |
| 36 | case HiddenApiAccessFlags::kLightGreylist: |
| 37 | case HiddenApiAccessFlags::kDarkGreylist: |
| 38 | return false; |
| 39 | case HiddenApiAccessFlags::kBlacklist: |
| 40 | return true; |
| 41 | } |
| 42 | } |
| 43 | |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame^] | 44 | // Returns true if we should warn about non-boot class path accessing member |
| 45 | // with `access_flags`. |
| 46 | inline bool ShouldWarnAboutMember(uint32_t access_flags) { |
| 47 | if (!Runtime::Current()->AreHiddenApiChecksEnabled()) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | switch (HiddenApiAccessFlags::DecodeFromRuntime(access_flags)) { |
| 52 | case HiddenApiAccessFlags::kWhitelist: |
| 53 | return false; |
| 54 | case HiddenApiAccessFlags::kLightGreylist: |
| 55 | case HiddenApiAccessFlags::kDarkGreylist: |
| 56 | return true; |
| 57 | case HiddenApiAccessFlags::kBlacklist: |
| 58 | // We should never access a blacklisted member from non-boot class path, |
| 59 | // but this function is called before we establish the origin of the access. |
| 60 | // Return false here, we do not want to warn when boot class path accesses |
| 61 | // a blacklisted member. |
| 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 66 | // Returns true if caller `num_frames` up the stack is in boot class path. |
| 67 | inline bool IsCallerInBootClassPath(Thread* self, size_t num_frames) |
| 68 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 69 | ObjPtr<mirror::Class> klass = GetCallingClass(self, num_frames); |
| 70 | if (klass == nullptr) { |
| 71 | // Unattached native thread. Assume that this is *not* boot class path. |
| 72 | return false; |
| 73 | } |
| 74 | return klass->IsBootStrapClassLoaded(); |
| 75 | } |
| 76 | |
| 77 | // Returns true if `caller` should not be allowed to access member with `access_flags`. |
| 78 | inline bool ShouldBlockAccessToMember(uint32_t access_flags, mirror::Class* caller) |
| 79 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 80 | return IsMemberHidden(access_flags) && |
| 81 | !caller->IsBootStrapClassLoaded(); |
| 82 | } |
| 83 | |
| 84 | // Returns true if `caller` should not be allowed to access `member`. |
| 85 | template<typename T> |
| 86 | inline bool ShouldBlockAccessToMember(T* member, ArtMethod* caller) |
| 87 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 88 | DCHECK(member != nullptr); |
| 89 | DCHECK(!caller->IsRuntimeMethod()); |
| 90 | return ShouldBlockAccessToMember(member->GetAccessFlags(), caller->GetDeclaringClass()); |
| 91 | } |
| 92 | |
| 93 | // Returns true if the caller `num_frames` up the stack should not be allowed |
| 94 | // to access `member`. |
| 95 | template<typename T> |
| 96 | inline bool ShouldBlockAccessToMember(T* member, Thread* self, size_t num_frames) |
| 97 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 98 | DCHECK(member != nullptr); |
| 99 | return IsMemberHidden(member->GetAccessFlags()) && |
| 100 | !IsCallerInBootClassPath(self, num_frames); // This is expensive. Save it for last. |
| 101 | } |
| 102 | |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame^] | 103 | // Issue a warning about field access. |
| 104 | inline void WarnAboutMemberAccess(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 105 | Runtime::Current()->SetPendingHiddenApiWarning(true); |
| 106 | LOG(WARNING) << "Access to hidden field " << field->PrettyField(); |
| 107 | } |
| 108 | |
| 109 | // Issue a warning about method access. |
| 110 | inline void WarnAboutMemberAccess(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 111 | Runtime::Current()->SetPendingHiddenApiWarning(true); |
| 112 | LOG(WARNING) << "Access to hidden method " << method->PrettyMethod(); |
| 113 | } |
| 114 | |
| 115 | // Set access flags of `member` to be in hidden API whitelist. This can be disabled |
| 116 | // with a Runtime::SetDedupHiddenApiWarnings. |
| 117 | template<typename T> |
| 118 | inline void MaybeWhitelistMember(T* member) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 119 | if (Runtime::Current()->ShouldDedupeHiddenApiWarnings()) { |
| 120 | member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime( |
| 121 | member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist)); |
| 122 | DCHECK(!ShouldWarnAboutMember(member->GetAccessFlags())); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Check if `caller` should be allowed to access `member` and warn if not. |
| 127 | template<typename T> |
| 128 | inline void MaybeWarnAboutMemberAccess(T* member, ArtMethod* caller) |
| 129 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 130 | DCHECK(member != nullptr); |
| 131 | DCHECK(!caller->IsRuntimeMethod()); |
| 132 | if (!Runtime::Current()->AreHiddenApiChecksEnabled() || |
| 133 | member == nullptr || |
| 134 | !ShouldWarnAboutMember(member->GetAccessFlags()) || |
| 135 | caller->GetDeclaringClass()->IsBootStrapClassLoaded()) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | WarnAboutMember(member); |
| 140 | MaybeWhitelistMember(member); |
| 141 | } |
| 142 | |
| 143 | // Check if the caller `num_frames` up the stack should be allowed to access |
| 144 | // `member` and warn if not. |
| 145 | template<typename T> |
| 146 | inline void MaybeWarnAboutMemberAccess(T* member, Thread* self, size_t num_frames) |
| 147 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 148 | if (!Runtime::Current()->AreHiddenApiChecksEnabled() || |
| 149 | member == nullptr || |
| 150 | !ShouldWarnAboutMember(member->GetAccessFlags())) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // Walk the stack to find the caller. This is *very* expensive. Save it for last. |
| 155 | ObjPtr<mirror::Class> klass = GetCallingClass(self, num_frames); |
| 156 | if (klass == nullptr) { |
| 157 | // Unattached native thread, assume that this is *not* boot class path |
| 158 | // and enforce the rules. |
| 159 | } else if (klass->IsBootStrapClassLoaded()) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | WarnAboutMemberAccess(member); |
| 164 | MaybeWhitelistMember(member); |
| 165 | } |
| 166 | |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 167 | } // namespace hiddenapi |
| 168 | } // namespace art |
| 169 | |
| 170 | #endif // ART_RUNTIME_HIDDEN_API_H_ |