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