blob: 7e41c1dd3c92fc22a94f4742b20eb48185172d91 [file] [log] [blame]
David Brazdil5a61bb72018-01-19 16:59:46 +00001/*
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
24namespace art {
25namespace hiddenapi {
26
David Brazdila02cb112018-01-31 11:36:39 +000027enum Action {
28 kAllow,
29 kAllowButWarn,
30 kDeny
31};
David Brazdil5a61bb72018-01-19 16:59:46 +000032
David Brazdila02cb112018-01-31 11:36:39 +000033inline Action GetMemberAction(uint32_t access_flags) {
David Brazdil5a61bb72018-01-19 16:59:46 +000034 switch (HiddenApiAccessFlags::DecodeFromRuntime(access_flags)) {
35 case HiddenApiAccessFlags::kWhitelist:
David Brazdila02cb112018-01-31 11:36:39 +000036 return kAllow;
David Brazdil5a61bb72018-01-19 16:59:46 +000037 case HiddenApiAccessFlags::kLightGreylist:
38 case HiddenApiAccessFlags::kDarkGreylist:
David Brazdila02cb112018-01-31 11:36:39 +000039 return kAllowButWarn;
David Brazdil5a61bb72018-01-19 16:59:46 +000040 case HiddenApiAccessFlags::kBlacklist:
David Brazdila02cb112018-01-31 11:36:39 +000041 return kDeny;
David Brazdil5a61bb72018-01-19 16:59:46 +000042 }
43}
44
David Brazdilee7d2fd2018-01-20 17:25:23 +000045// Issue a warning about field access.
46inline void WarnAboutMemberAccess(ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdilee7d2fd2018-01-20 17:25:23 +000047 LOG(WARNING) << "Access to hidden field " << field->PrettyField();
48}
49
50// Issue a warning about method access.
51inline void WarnAboutMemberAccess(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdilee7d2fd2018-01-20 17:25:23 +000052 LOG(WARNING) << "Access to hidden method " << method->PrettyMethod();
53}
54
David Brazdila02cb112018-01-31 11:36:39 +000055// 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 Brazdilee7d2fd2018-01-20 17:25:23 +000061template<typename T>
David Brazdila02cb112018-01-31 11:36:39 +000062inline bool ShouldBlockAccessToMember(T* member,
63 Thread* self,
64 std::function<bool(Thread*)> fn_caller_in_boot)
David Brazdilee7d2fd2018-01-20 17:25:23 +000065 REQUIRES_SHARED(Locks::mutator_lock_) {
66 DCHECK(member != nullptr);
David Brazdila02cb112018-01-31 11:36:39 +000067
68 if (!Runtime::Current()->AreHiddenApiChecksEnabled()) {
69 // Exit early. Nothing to enforce.
70 return false;
David Brazdilee7d2fd2018-01-20 17:25:23 +000071 }
72
David Brazdila02cb112018-01-31 11:36:39 +000073 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 Brazdilee7d2fd2018-01-20 17:25:23 +0000102}
103
David Brazdila02cb112018-01-31 11:36:39 +0000104// 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.
106inline bool ShouldBlockAccessToMember(uint32_t access_flags, mirror::Class* caller)
David Brazdilee7d2fd2018-01-20 17:25:23 +0000107 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdila02cb112018-01-31 11:36:39 +0000108 if (!Runtime::Current()->AreHiddenApiChecksEnabled()) {
109 // Exit early. Nothing to enforce.
110 return false;
David Brazdilee7d2fd2018-01-20 17:25:23 +0000111 }
112
David Brazdila02cb112018-01-31 11:36:39 +0000113 // Only continue if we want to deny access. Warnings are *not* printed.
114 if (GetMemberAction(access_flags) != kDeny) {
115 return false;
David Brazdilee7d2fd2018-01-20 17:25:23 +0000116 }
117
David Brazdila02cb112018-01-31 11:36:39 +0000118 // 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 Brazdilee7d2fd2018-01-20 17:25:23 +0000125}
126
David Brazdil5a61bb72018-01-19 16:59:46 +0000127} // namespace hiddenapi
128} // namespace art
129
130#endif // ART_RUNTIME_HIDDEN_API_H_