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 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 27 | enum Action { |
| 28 | kAllow, |
| 29 | kAllowButWarn, |
| 30 | kDeny |
| 31 | }; |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 32 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 33 | inline Action GetMemberAction(uint32_t access_flags) { |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 34 | switch (HiddenApiAccessFlags::DecodeFromRuntime(access_flags)) { |
| 35 | case HiddenApiAccessFlags::kWhitelist: |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 36 | return kAllow; |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 37 | case HiddenApiAccessFlags::kLightGreylist: |
| 38 | case HiddenApiAccessFlags::kDarkGreylist: |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 39 | return kAllowButWarn; |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 40 | case HiddenApiAccessFlags::kBlacklist: |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 41 | return kDeny; |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 45 | // Issue a warning about field access. |
| 46 | inline void WarnAboutMemberAccess(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 1077bbc | 2018-01-31 14:33:08 +0000 | [diff] [blame^] | 47 | std::string tmp; |
| 48 | LOG(WARNING) << "Accessing hidden field " |
| 49 | << field->GetDeclaringClass()->GetDescriptor(&tmp) << "->" |
| 50 | << field->GetName() << ":" << field->GetTypeDescriptor(); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Issue a warning about method access. |
| 54 | inline void WarnAboutMemberAccess(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 1077bbc | 2018-01-31 14:33:08 +0000 | [diff] [blame^] | 55 | std::string tmp; |
| 56 | LOG(WARNING) << "Accessing hidden method " |
| 57 | << method->GetDeclaringClass()->GetDescriptor(&tmp) << "->" |
| 58 | << method->GetName() << method->GetSignature().ToString(); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 59 | } |
| 60 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 61 | // Returns true if access to `member` should be denied to the caller of the |
| 62 | // reflective query. The decision is based on whether the caller is in boot |
| 63 | // class path or not. Because different users of this function determine this |
| 64 | // in a different way, `fn_caller_in_boot(self)` is called and should return |
| 65 | // true if the caller is in boot class path. |
| 66 | // This function might print warnings into the log if the member is greylisted. |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 67 | template<typename T> |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 68 | inline bool ShouldBlockAccessToMember(T* member, |
| 69 | Thread* self, |
| 70 | std::function<bool(Thread*)> fn_caller_in_boot) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 71 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 72 | DCHECK(member != nullptr); |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 73 | |
| 74 | if (!Runtime::Current()->AreHiddenApiChecksEnabled()) { |
| 75 | // Exit early. Nothing to enforce. |
| 76 | return false; |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 77 | } |
| 78 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 79 | Action action = GetMemberAction(member->GetAccessFlags()); |
| 80 | if (action == kAllow) { |
| 81 | // Nothing to do. |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | // Member is hidden. Walk the stack to find the caller. |
| 86 | // This can be *very* expensive. Save it for last. |
| 87 | if (fn_caller_in_boot(self)) { |
| 88 | // Caller in boot class path. Exit. |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | // Member is hidden and we are not in the boot class path. Act accordingly. |
| 93 | if (action == kAllowButWarn) { |
| 94 | // Allow access to this member but print a warning. Depending on a runtime |
| 95 | // flag, we might move the member into whitelist and skip the warning the |
| 96 | // next time the member is used. |
| 97 | Runtime::Current()->SetPendingHiddenApiWarning(true); |
| 98 | if (Runtime::Current()->ShouldDedupeHiddenApiWarnings()) { |
| 99 | member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime( |
| 100 | member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist)); |
| 101 | } |
| 102 | WarnAboutMemberAccess(member); |
| 103 | return false; |
| 104 | } else { |
| 105 | DCHECK_EQ(action, hiddenapi::kDeny); |
| 106 | return true; |
| 107 | } |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 108 | } |
| 109 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 110 | // Returns true if access to member with `access_flags` should be denied to `caller`. |
| 111 | // This function should be called on statically linked uses of hidden API. |
| 112 | inline bool ShouldBlockAccessToMember(uint32_t access_flags, mirror::Class* caller) |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 113 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 114 | if (!Runtime::Current()->AreHiddenApiChecksEnabled()) { |
| 115 | // Exit early. Nothing to enforce. |
| 116 | return false; |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 117 | } |
| 118 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 119 | // Only continue if we want to deny access. Warnings are *not* printed. |
| 120 | if (GetMemberAction(access_flags) != kDeny) { |
| 121 | return false; |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 122 | } |
| 123 | |
David Brazdil | a02cb11 | 2018-01-31 11:36:39 +0000 | [diff] [blame] | 124 | // Member is hidden. Check if the caller is in boot class path. |
| 125 | if (caller == nullptr) { |
| 126 | // The caller is unknown. We assume that this is *not* boot class path. |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | return !caller->IsBootStrapClassLoaded(); |
David Brazdil | ee7d2fd | 2018-01-20 17:25:23 +0000 | [diff] [blame] | 131 | } |
| 132 | |
David Brazdil | 5a61bb7 | 2018-01-19 16:59:46 +0000 | [diff] [blame] | 133 | } // namespace hiddenapi |
| 134 | } // namespace art |
| 135 | |
| 136 | #endif // ART_RUNTIME_HIDDEN_API_H_ |