Relax annotation visibility so runtime includes build.
Previous behavior in M and earlier would allow annotations marked
VISIBILITY_BUILD to be visible to the runtime when they should not
have been. When targeting older sdks, revert to this behavior.
Bug: 28826610
Change-Id: I95e10da899ed11107ca78c62dd2e263808008310
diff --git a/runtime/dex_file.cc b/runtime/dex_file.cc
index 63f3f08..88696e9 100644
--- a/runtime/dex_file.cc
+++ b/runtime/dex_file.cc
@@ -1163,6 +1163,18 @@
return val;
}
+// Checks that visibility is as expected. Includes special behavior for M and
+// before to allow runtime and build visibility when expecting runtime.
+static bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
+ if (expected == DexFile::kDexVisibilityRuntime) {
+ int32_t sdk_version = Runtime::Current()->GetTargetSdkVersion();
+ if (sdk_version > 0 && sdk_version <= 23) {
+ return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
+ }
+ }
+ return actual == expected;
+}
+
const DexFile::AnnotationSetItem* DexFile::FindAnnotationSetForField(ArtField* field) const {
mirror::Class* klass = field->GetDeclaringClass();
const AnnotationsDirectoryItem* annotations_dir = GetAnnotationsDirectory(*klass->GetClassDef());
@@ -1640,7 +1652,7 @@
Handle<mirror::Class> annotation_class) const {
for (uint32_t i = 0; i < annotation_set->size_; ++i) {
const AnnotationItem* annotation_item = GetAnnotationItem(annotation_set, i);
- if (annotation_item->visibility_ != visibility) {
+ if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
continue;
}
const uint8_t* annotation = annotation_item->annotation_;
@@ -1758,6 +1770,8 @@
uint32_t dest_index = 0;
for (uint32_t i = 0; i < size; ++i) {
const AnnotationItem* annotation_item = GetAnnotationItem(annotation_set, i);
+ // Note that we do not use IsVisibilityCompatible here because older code
+ // was correct for this case.
if (annotation_item->visibility_ != visibility) {
continue;
}
@@ -2146,7 +2160,7 @@
const AnnotationItem* result = nullptr;
for (uint32_t i = 0; i < annotation_set->size_; ++i) {
const AnnotationItem* annotation_item = GetAnnotationItem(annotation_set, i);
- if (annotation_item->visibility_ != visibility) {
+ if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
continue;
}
const uint8_t* annotation = annotation_item->annotation_;