Android SDK lookup fails unexpectedly if Build$VERSION already failed to initialize

未关闭 适合新手
#1,572 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
android, java

调研方向

从 openjdk/src/main/java/org/conscrypt/Platform.java 开始,重点关注 Platform.androidSdkInt() 及其对 android.os.Build$VERSION 的反射查找。复现报告的 NoClassDefFoundError 路径,并确定需要扩展的平台检测测试。完成意味着查找能够处理初始化失败的情况,同时不破坏 Conscrypt 的初始化,并添加一个覆盖 Robolectric 与普通 JUnit 混合设置的回归测试。

由索引模型根据 Issue 内容生成。

描述

Hello!

Our Android project is a system application that uses a custom AOSP framework.jar. The test suite of this project consists of a mixture of Robolectric tests & ordinary unit tests. Since updating our dependency on Robolectric from 4.16.1 to 4.19-beta-1, we have been observing an issue that can be traced to a change in Conscrypt's Platform detection for Android. The issue is how android.os.Build$VERSION is loaded from Platform#androidSdkInt() and its fallback mechanism when the class lookup raises an error.

Consider the following two test classes. The first uses a Kotlin stdlib API that probes Android's Build class via reflection internally (link). If that lookup fails on any Throwable, the result is discarded as "not an Android environment". The second test is a random Robolectric test whose contents don't matter. Under the hood, the Robolectric sandbox initializes Conscrypt, which eventually calls into Platform#androidSdkInt() (link). This lookup only discards ReflectiveOperationException and UnsatisfiedLinkError, though. Any other exception is propagated and can cause issues, like in our example.

import org.junit.jupiter.api.Test
import kotlin.random.Random

class FirstTest {
    @Test
    fun test() {
        Random.nextBytes(512)
    }
}
import android.os.Build
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class SecondTest {
    @Test
    fun test() {
        println(Build.VERSION.SDK_INT)
    }
}
Stacktrace
Could not initialize class android.os.Build$VERSION
java.lang.NoClassDefFoundError: Could not initialize class android.os.Build$VERSION
	at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized0(Native Method)
	at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized(Unknown Source)
	at java.base/java.lang.reflect.Field.acquireFieldAccessor(Unknown Source)
	at java.base/java.lang.reflect.Field.getFieldAccessor(Unknown Source)
	at java.base/java.lang.reflect.Field.getInt(Unknown Source)
	at org.conscrypt.Platform.androidSdkInt(Platform.java:695)
	at org.conscrypt.Platform.javaVersion0(Platform.java:709)
	at org.conscrypt.Platform.<clinit>(Platform.java:100)
	at org.conscrypt.OpenSSLProvider.<init>(OpenSSLProvider.java:78)
	at org.robolectric.android.internal.AndroidTestEnvironment.createConscryptProvider(AndroidTestEnvironment.java:369)
	at org.robolectric.android.internal.AndroidTestEnvironment.setUpApplicationState(AndroidTestEnvironment.java:235)
	at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:330)
	at org.robolectric.internal.SandboxTestRunner.runInSandbox(SandboxTestRunner.java:451)
	at org.robolectric.internal.SandboxTestRunner.executeInSandbox(SandboxTestRunner.java:418)
	at org.robolectric.internal.SandboxTestRunner$5.evaluate(SandboxTestRunner.java:403)
	at org.robolectric.internal.SandboxTestRunner.access$000(SandboxTestRunner.java:64)
	at org.robolectric.internal.SandboxTestRunner$4.evaluate(SandboxTestRunner.java:292)
	at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:259)
	at org.robolectric.internal.SandboxTestRunner$3.lambda$evaluate$0(SandboxTestRunner.java:278)
	at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThreadWithClassLoader$1(Sandbox.java:97)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.UnsatisfiedLinkError: 'java.lang.String android.os.SystemProperties.native_get(java.lang.String, java.lang.String)' [in thread "Test worker"]
	at android.os.SystemProperties.native_get(Native Method)
	at android.os.SystemProperties.get(SystemProperties.java:129)
	at android.os.Build.getString(Build.java:1264)
	at android.os.Build.<clinit>(Build.java:51)
	at android.os.Build$VERSION.<clinit>(Build.java:229)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Unknown Source)
	at java.base/java.lang.Class.forName(Unknown Source)

In the (admittedly non-standard and weird) setup of our app, the JVM test classpath contains real, uninstrumented Android framework classes. If Conscrypt's own lookup of android.os.Build$VERSION is the first to trigger its initialization, it fails with UnsatisfiedLinkError and Conscrypt discards it properly. However, if anything else has already triggered (and failed) that initialization before (i.e. FirstTest, via Kotlin's stdlib), Conscrypt's lookup instead throws NoClassDefFoundError, which isn't caught. This causes Platform.<clint> to fail and Conscrypt is unusable for the rest of the JVM.

The issue did not happen with the older version of Robolectric as it relied on Conscrypt 2.5.2, which did not look up android.os.Build under the hood. My suggestion would be to broaden the catch clause of Conscrypt's Android lookup so that it discards all Throwables (or at least LinkageError), matching what Kotlin's stdlib does.

主要语言
Java
星标
1.4k
派生
332
平均合并
2 天 56 分钟
30 天内合并 PR
5

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google/conscrypt 的其他 Issue

查看 google/conscrypt 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。