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