Hide java.lang.ClassValue from apps targeting T or earlier Android.
java.lang.ClassValue was added in Android U only and platform
agnostic Java libraries do
Class.forName("java.lang.ClassValue") check to use non-ClassValue
based implementation on Android. But proguarding tools also used
absence of ClassValue as a reason to remove computeValue method
implementation. As a result on Androd U such code no longer worked
and failed with AbstractMethodError exception.
Bug: 259501764
Test: art/test.py -b --host -r -t 2252-class-value
Test: previously failing app works fine with this patch applied
Change-Id: I15494b31aeac7f68d80c3292c885db7834e50ade
diff --git a/runtime/native/java_lang_Class.cc b/runtime/native/java_lang_Class.cc
index fb48bb8..c5fdda7 100644
--- a/runtime/native/java_lang_Class.cc
+++ b/runtime/native/java_lang_Class.cc
@@ -21,6 +21,7 @@
#include "art_field-inl.h"
#include "art_method-alloc-inl.h"
#include "base/enums.h"
+#include "base/sdk_version.h"
#include "class_linker-inl.h"
#include "class_root-inl.h"
#include "common_throws.h"
@@ -44,6 +45,7 @@
#include "mirror/proxy.h"
#include "mirror/string-alloc-inl.h"
#include "mirror/string-inl.h"
+#include "mirror/string.h"
#include "native_util.h"
#include "nativehelper/jni_macros.h"
#include "nativehelper/scoped_local_ref.h"
@@ -129,6 +131,25 @@
if (initialize) {
class_linker->EnsureInitialized(soa.Self(), c, true, true);
}
+
+ // java.lang.ClassValue was added in Android U, and proguarding tools
+ // used that as justification to remove computeValue method implementation.
+ // Usual pattern was to check that Class.forName("java.lang.ClassValue")
+ // call does not throw and use ClassValue-based implementation or fallback
+ // to other solution if it does throw.
+ // So far ClassValue is the only class with such a problem and hence this
+ // ad-hoc check.
+ // See b/259501764.
+ uint32_t targetSdkVersion = Runtime::Current()->GetTargetSdkVersion();
+ if (IsSdkVersionSetAndAtMost(targetSdkVersion, SdkVersion::kT)) {
+ ObjPtr<mirror::Class> java_lang_ClassValue =
+ WellKnownClasses::ToClass(WellKnownClasses::java_lang_ClassValue);
+ if (c.Get() == java_lang_ClassValue.Ptr()) {
+ soa.Self()->ThrowNewException("Ljava/lang/ClassNotFoundException;", "java.lang.ClassValue");
+ return nullptr;
+ }
+ }
+
return soa.AddLocalReference<jclass>(c.Get());
}
diff --git a/runtime/well_known_classes.cc b/runtime/well_known_classes.cc
index 50f0b61..6a4a87f 100644
--- a/runtime/well_known_classes.cc
+++ b/runtime/well_known_classes.cc
@@ -50,6 +50,7 @@
jclass WellKnownClasses::dalvik_annotation_optimization_NeverCompile;
jclass WellKnownClasses::dalvik_annotation_optimization_NeverInline;
jclass WellKnownClasses::java_lang_annotation_Annotation__array;
+jclass WellKnownClasses::java_lang_ClassValue;
jclass WellKnownClasses::java_lang_reflect_Parameter__array;
jclass WellKnownClasses::java_lang_StringFactory;
jclass WellKnownClasses::java_lang_System;
@@ -323,6 +324,7 @@
CacheClass(env, "dalvik/annotation/optimization/NeverInline");
java_lang_annotation_Annotation__array = CacheClass(env, "[Ljava/lang/annotation/Annotation;");
+ java_lang_ClassValue = CacheClass(env, "java/lang/ClassValue");
java_lang_reflect_Parameter__array = CacheClass(env, "[Ljava/lang/reflect/Parameter;");
java_lang_StringFactory = CacheClass(env, "java/lang/StringFactory");
java_lang_System = CacheClass(env, "java/lang/System");
@@ -796,6 +798,7 @@
dalvik_annotation_optimization_NeverCompile = nullptr;
dalvik_annotation_optimization_NeverInline = nullptr;
java_lang_annotation_Annotation__array = nullptr;
+ java_lang_ClassValue = nullptr;
java_lang_reflect_Parameter__array = nullptr;
java_lang_StringFactory = nullptr;
java_lang_System = nullptr;
diff --git a/runtime/well_known_classes.h b/runtime/well_known_classes.h
index 0bca63b..753d386 100644
--- a/runtime/well_known_classes.h
+++ b/runtime/well_known_classes.h
@@ -95,6 +95,7 @@
static jclass dalvik_annotation_optimization_NeverCompile;
static jclass dalvik_annotation_optimization_NeverInline;
static jclass java_lang_annotation_Annotation__array;
+ static jclass java_lang_ClassValue;
static jclass java_lang_reflect_Parameter__array;
static jclass java_lang_StringFactory;
static jclass java_lang_System;
diff --git a/test/2252-class-value-before-and-after-u/expected-stderr.txt b/test/2252-class-value-before-and-after-u/expected-stderr.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/2252-class-value-before-and-after-u/expected-stderr.txt
diff --git a/test/2252-class-value-before-and-after-u/expected-stdout.txt b/test/2252-class-value-before-and-after-u/expected-stdout.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/2252-class-value-before-and-after-u/expected-stdout.txt
diff --git a/test/2252-class-value-before-and-after-u/info.txt b/test/2252-class-value-before-and-after-u/info.txt
new file mode 100644
index 0000000..3a8d31f
--- /dev/null
+++ b/test/2252-class-value-before-and-after-u/info.txt
@@ -0,0 +1,2 @@
+java.lang.ClassValue should be visible to U or newer Android, but on older versions
+Class.forName("java.lang.ClassValue") should throw CNFE. See b/259501764.
\ No newline at end of file
diff --git a/test/2252-class-value-before-and-after-u/src-art/Main.java b/test/2252-class-value-before-and-after-u/src-art/Main.java
new file mode 100644
index 0000000..88746e1
--- /dev/null
+++ b/test/2252-class-value-before-and-after-u/src-art/Main.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import dalvik.system.VMRuntime;
+
+public class Main {
+ public static void main(String[] args) {
+ VMRuntime.getRuntime().setTargetSdkVersion(34);
+
+ try {
+ Class classValueClass = Class.forName("java.lang.ClassValue");
+ } catch (ClassNotFoundException ignored) {
+ throw new Error("java.lang.ClassValue should be available on targetSdkLevel 34");
+ }
+
+ VMRuntime.getRuntime().setTargetSdkVersion(33);
+ try {
+ Class classValueClass = Class.forName("java.lang.ClassValue");
+ throw new Error("Was able to find " + classValueClass + " on targetSdkLevel 33");
+ } catch (ClassNotFoundException expected) {}
+ }
+}