blob: fa47e3a17a8912a7dfda2100c8f689c306fe83dd [file] [log] [blame]
Andreas Gampeaa120012018-03-28 16:23:24 -07001/*
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
Narayan Kamathf5f1f802018-04-03 15:23:46 +010019#include <nativehelper/scoped_local_ref.h>
20
Andreas Gampeaa120012018-03-28 16:23:24 -070021#include "base/dumpable.h"
Narayan Kamathf5f1f802018-04-03 15:23:46 +010022#include "thread-current-inl.h"
23#include "well_known_classes.h"
Andreas Gampeaa120012018-03-28 16:23:24 -070024
25namespace art {
26namespace hiddenapi {
27
28static inline std::ostream& operator<<(std::ostream& os, AccessMethod value) {
29 switch (value) {
David Brazdil4525e0b2018-04-05 16:57:32 +010030 case kNone:
31 LOG(FATAL) << "Internal access to hidden API should not be logged";
32 UNREACHABLE();
Andreas Gampeaa120012018-03-28 16:23:24 -070033 case kReflection:
34 os << "reflection";
35 break;
36 case kJNI:
37 os << "JNI";
38 break;
39 case kLinking:
40 os << "linking";
41 break;
42 }
43 return os;
44}
45
46static constexpr bool EnumsEqual(EnforcementPolicy policy, HiddenApiAccessFlags::ApiList apiList) {
47 return static_cast<int>(policy) == static_cast<int>(apiList);
48}
49
50// GetMemberAction-related static_asserts.
51static_assert(
Andreas Gampeaa120012018-03-28 16:23:24 -070052 EnumsEqual(EnforcementPolicy::kDarkGreyAndBlackList, HiddenApiAccessFlags::kDarkGreylist) &&
53 EnumsEqual(EnforcementPolicy::kBlacklistOnly, HiddenApiAccessFlags::kBlacklist),
54 "Mismatch between EnforcementPolicy and ApiList enums");
55static_assert(
Mathew Inwooda8503d92018-04-05 16:10:25 +010056 EnforcementPolicy::kJustWarn < EnforcementPolicy::kDarkGreyAndBlackList &&
Andreas Gampeaa120012018-03-28 16:23:24 -070057 EnforcementPolicy::kDarkGreyAndBlackList < EnforcementPolicy::kBlacklistOnly,
58 "EnforcementPolicy values ordering not correct");
59
60namespace detail {
61
62MemberSignature::MemberSignature(ArtField* field) {
63 member_type_ = "field";
64 signature_parts_ = {
65 field->GetDeclaringClass()->GetDescriptor(&tmp_),
66 "->",
67 field->GetName(),
68 ":",
69 field->GetTypeDescriptor()
70 };
71}
72
73MemberSignature::MemberSignature(ArtMethod* method) {
74 member_type_ = "method";
75 signature_parts_ = {
76 method->GetDeclaringClass()->GetDescriptor(&tmp_),
77 "->",
78 method->GetName(),
79 method->GetSignature().ToString()
80 };
81}
82
83bool MemberSignature::DoesPrefixMatch(const std::string& prefix) const {
84 size_t pos = 0;
85 for (const std::string& part : signature_parts_) {
86 size_t count = std::min(prefix.length() - pos, part.length());
87 if (prefix.compare(pos, count, part, 0, count) == 0) {
88 pos += count;
89 } else {
90 return false;
91 }
92 }
93 // We have a complete match if all parts match (we exit the loop without
94 // returning) AND we've matched the whole prefix.
95 return pos == prefix.length();
96}
97
98bool MemberSignature::IsExempted(const std::vector<std::string>& exemptions) {
99 for (const std::string& exemption : exemptions) {
100 if (DoesPrefixMatch(exemption)) {
101 return true;
102 }
103 }
104 return false;
105}
106
107void MemberSignature::Dump(std::ostream& os) const {
108 for (std::string part : signature_parts_) {
109 os << part;
110 }
111}
112
113void MemberSignature::WarnAboutAccess(AccessMethod access_method,
114 HiddenApiAccessFlags::ApiList list) {
115 LOG(WARNING) << "Accessing hidden " << member_type_ << " " << Dumpable<MemberSignature>(*this)
116 << " (" << list << ", " << access_method << ")";
117}
118
119template<typename T>
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100120Action GetMemberActionImpl(T* member, Action action, AccessMethod access_method) {
David Brazdil4525e0b2018-04-05 16:57:32 +0100121 DCHECK_NE(action, kAllow);
122
Andreas Gampeaa120012018-03-28 16:23:24 -0700123 // Get the signature, we need it later.
124 MemberSignature member_signature(member);
125
126 Runtime* runtime = Runtime::Current();
127
128 if (action == kDeny) {
129 // If we were about to deny, check for an exemption first.
130 // Exempted APIs are treated as light grey list.
131 if (member_signature.IsExempted(runtime->GetHiddenApiExemptions())) {
132 action = kAllowButWarn;
133 // Avoid re-examining the exemption list next time.
134 // Note this results in the warning below showing "light greylist", which
135 // seems like what one would expect. Exemptions effectively add new members to
136 // the light greylist.
137 member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime(
138 member->GetAccessFlags(), HiddenApiAccessFlags::kLightGreylist));
139 }
140 }
141
David Brazdil4525e0b2018-04-05 16:57:32 +0100142 if (access_method != kNone) {
143 // Print a log message with information about this class member access.
144 // We do this regardless of whether we block the access or not.
145 member_signature.WarnAboutAccess(access_method,
146 HiddenApiAccessFlags::DecodeFromRuntime(member->GetAccessFlags()));
147 }
Andreas Gampeaa120012018-03-28 16:23:24 -0700148
Andreas Gampeaa120012018-03-28 16:23:24 -0700149 if (action == kDeny) {
150 // Block access
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100151 return action;
Andreas Gampeaa120012018-03-28 16:23:24 -0700152 }
153
154 // Allow access to this member but print a warning.
155 DCHECK(action == kAllowButWarn || action == kAllowButWarnAndToast);
156
David Brazdil4525e0b2018-04-05 16:57:32 +0100157 if (access_method != kNone) {
158 // Depending on a runtime flag, we might move the member into whitelist and
159 // skip the warning the next time the member is accessed.
160 if (runtime->ShouldDedupeHiddenApiWarnings()) {
161 member->SetAccessFlags(HiddenApiAccessFlags::EncodeForRuntime(
162 member->GetAccessFlags(), HiddenApiAccessFlags::kWhitelist));
163 }
Andreas Gampeaa120012018-03-28 16:23:24 -0700164
David Brazdil4525e0b2018-04-05 16:57:32 +0100165 // If this action requires a UI warning, set the appropriate flag.
166 if (action == kAllowButWarnAndToast || runtime->ShouldAlwaysSetHiddenApiWarningFlag()) {
167 runtime->SetPendingHiddenApiWarning(true);
168 }
Andreas Gampeaa120012018-03-28 16:23:24 -0700169 }
170
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100171 return action;
Andreas Gampeaa120012018-03-28 16:23:24 -0700172}
173
174// Need to instantiate this.
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100175template Action GetMemberActionImpl<ArtField>(ArtField* member,
176 Action action,
177 AccessMethod access_method);
178template Action GetMemberActionImpl<ArtMethod>(ArtMethod* member,
179 Action action,
180 AccessMethod access_method);
Andreas Gampeaa120012018-03-28 16:23:24 -0700181} // namespace detail
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100182
183template<typename T>
184void NotifyHiddenApiListener(T* member) {
185 Runtime* runtime = Runtime::Current();
186 if (!runtime->IsAotCompiler()) {
187 ScopedObjectAccessUnchecked soa(Thread::Current());
188
189 ScopedLocalRef<jobject> consumer_object(soa.Env(),
190 soa.Env()->GetStaticObjectField(
191 WellKnownClasses::dalvik_system_VMRuntime,
192 WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer));
193 // If the consumer is non-null, we call back to it to let it know that we
194 // have encountered an API that's in one of our lists.
195 if (consumer_object != nullptr) {
196 detail::MemberSignature member_signature(member);
197 std::ostringstream member_signature_str;
198 member_signature.Dump(member_signature_str);
199
200 ScopedLocalRef<jobject> signature_str(
201 soa.Env(),
202 soa.Env()->NewStringUTF(member_signature_str.str().c_str()));
203
204 // Call through to Consumer.accept(String memberSignature);
205 soa.Env()->CallVoidMethod(consumer_object.get(),
206 WellKnownClasses::java_util_function_Consumer_accept,
207 signature_str.get());
208 }
209 }
210}
211
212template void NotifyHiddenApiListener<ArtMethod>(ArtMethod* member);
213template void NotifyHiddenApiListener<ArtField>(ArtField* member);
214
Andreas Gampeaa120012018-03-28 16:23:24 -0700215} // namespace hiddenapi
216} // namespace art