| // Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| package com.android.tools.r8; | |
| import static com.android.tools.r8.ToolHelper.DexVm.Version.V17_1_0; | |
| import static org.junit.Assert.assertEquals; | |
| import static org.junit.Assert.assertFalse; | |
| import static org.junit.Assert.assertTrue; | |
| import static org.junit.Assume.assumeTrue; | |
| import com.android.tools.r8.TestBase.Backend; | |
| import com.android.tools.r8.TestRuntime.CfRuntime; | |
| import com.android.tools.r8.TestRuntime.CfVm; | |
| import com.android.tools.r8.TestRuntime.DexRuntime; | |
| import com.android.tools.r8.TestRuntime.NoneRuntime; | |
| import com.android.tools.r8.ToolHelper.DexVm; | |
| import com.android.tools.r8.utils.AndroidApiLevel; | |
| import com.android.tools.r8.utils.internal.exceptions.Unreachable; | |
| import java.nio.file.Path; | |
| import java.util.List; | |
| import java.util.function.Predicate; | |
| // Actual test parameters for a specific configuration. Currently just the runtime configuration. | |
| public class TestParameters { | |
| private final TestRuntime runtime; | |
| private final AndroidApiLevel apiLevel; | |
| private final PartialCompilationTestParameters partialCompilationTestParameters; | |
| private final boolean representativeApiLevelForRuntime; | |
| public TestParameters(TestRuntime runtime) { | |
| this(runtime, null, PartialCompilationTestParameters.NONE); | |
| } | |
| public TestParameters( | |
| TestRuntime runtime, | |
| AndroidApiLevel apiLevel, | |
| PartialCompilationTestParameters partialCompilationTestParameters) { | |
| this(runtime, apiLevel, partialCompilationTestParameters, true); | |
| } | |
| public TestParameters( | |
| TestRuntime runtime, | |
| AndroidApiLevel apiLevel, | |
| PartialCompilationTestParameters partialCompilationTestParameters, | |
| boolean representativeApiLevelForRuntime) { | |
| assert runtime != null; | |
| this.runtime = runtime; | |
| this.apiLevel = apiLevel; | |
| this.partialCompilationTestParameters = partialCompilationTestParameters; | |
| this.representativeApiLevelForRuntime = representativeApiLevelForRuntime; | |
| } | |
| public static TestParametersCollection justNoneRuntime() { | |
| return new TestParametersBuilder().withNoneRuntime().build(); | |
| } | |
| public boolean canHaveDalvikIntUsedAsNonIntPrimitiveTypeBug() { | |
| return isCfRuntime() || getApiLevel().isLessThan(AndroidApiLevel.L); | |
| } | |
| public boolean canHaveIssueWithInlinedMonitors() { | |
| return isCfRuntime() || getApiLevel().isLessThanOrEqualTo(AndroidApiLevel.M); | |
| } | |
| /** | |
| * Returns true if the runtime uses resolution to lookup the constructor targeted by a given | |
| * invoke, so that it is valid to have non-rebound constructor invokes. | |
| * | |
| * <p>Example: If value `v` is an uninitialized instanceof type `T`, then calling `T.<init>()` | |
| * succeeds on ART even if `T.<init>()` does not exists, as ART will resolve the constructor, find | |
| * `Object.<init>()`, and use this method to initialize `v`. On the JVM and on Dalvik, this is a | |
| * runtime error. | |
| */ | |
| public boolean canHaveNonReboundConstructorInvoke() { | |
| return isDexRuntime() && getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.L); | |
| } | |
| public boolean canInitNewInstanceUsingSuperclassConstructor() { | |
| return canHaveNonReboundConstructorInvoke(); | |
| } | |
| public boolean hasDefaultInterfaceMethodsSupport() { | |
| return isCfRuntime() | |
| || getApiLevel() | |
| .isGreaterThanOrEqualTo(TestBase.apiLevelWithDefaultInterfaceMethodsSupport()); | |
| } | |
| public boolean hasSealedClassesReflectionSupport() { | |
| return isCfRuntime() | |
| || (isDexRuntime() | |
| && getApiLevel() | |
| .isGreaterThanOrEqualTo(TestBase.apiLevelWithSealedClassesReflectionSupport())); | |
| } | |
| public boolean hasSealedClassesRuntimeCheck() { | |
| return isCfRuntime(CfRuntime::hasSealedClassesSupport) | |
| || (isDexRuntime() | |
| && getApiLevel() | |
| .isGreaterThanOrEqualTo(TestBase.apiLevelWithSealedClassesReflectionSupport()) | |
| && getDexRuntimeVersion().isNewerThanOrEqual(V17_1_0)); | |
| } | |
| public boolean canUseDefaultAndStaticInterfaceMethods() { | |
| assert isCfRuntime() || isDexRuntime(); | |
| assert !isCfRuntime() || apiLevel == null | |
| : "Use canUseDefaultAndStaticInterfaceMethodsWhenDesugaring when using CF api levels."; | |
| return isCfRuntime() | |
| || getApiLevel() | |
| .isGreaterThanOrEqualTo(TestBase.apiLevelWithDefaultInterfaceMethodsSupport()); | |
| } | |
| public boolean canUseDefaultAndStaticInterfaceMethodsWhenDesugaring() { | |
| assert isCfRuntime() || isDexRuntime(); | |
| assert apiLevel != null; | |
| return getApiLevel() | |
| .isGreaterThanOrEqualTo(TestBase.apiLevelWithDefaultInterfaceMethodsSupport()); | |
| } | |
| public boolean canUseJavaLangInvokeVarHandleStoreStoreFence() { | |
| return isDexRuntime() && getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.T); | |
| } | |
| public boolean canUseJavaLangDivideUnsigned() { | |
| return isCfRuntime() || getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.O); | |
| } | |
| public boolean canUseNativeDexPC() { | |
| assert isCfRuntime() || isDexRuntime(); | |
| return isDexRuntime() && getDexRuntimeVersion().isNewerThanOrEqual(DexVm.Version.V8_1_0); | |
| } | |
| public boolean canUseNativeMultidex() { | |
| return isDexRuntime() && getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.L); | |
| } | |
| public boolean canUseNestBasedAccesses() { | |
| // TODO(b/247047415): Update test when a DEX VM natively supporting nests is added. | |
| assertFalse(getApiLevel() != null && getApiLevel().getMajor() > 37); | |
| assert isCfRuntime() || isDexRuntime(); | |
| return isCfRuntime(cfRuntime -> cfRuntime.isNewerThanOrEqual(CfVm.JDK11)); | |
| } | |
| public boolean canUseNestBasedAccessesWhenDesugaring() { | |
| assert isCfRuntime() || isDexRuntime(); | |
| assert apiLevel != null; | |
| return false; | |
| } | |
| public boolean canUseFilledNewArrayOnNonStringObjects() { | |
| return isDexRuntime() && getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.N); | |
| } | |
| public boolean canUseUnsafeGetAndSet() { | |
| return isDexRuntime() && getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.N); | |
| } | |
| public boolean canUseUnsafeStoreFence() { | |
| return isDexRuntime() && getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.N); | |
| } | |
| public boolean canUseSubTypesInFilledNewArray() { | |
| return isDexRuntime() && getApiLevel().isGreaterThan(AndroidApiLevel.U); | |
| } | |
| public boolean corelibWithExecutorServiceImplementingAutoClosable() { | |
| return isDexRuntime() && getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.BAKLAVA); | |
| } | |
| public boolean frameworkHasBuildVersionCodesFull() { | |
| return isDexRuntime() && getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.BAKLAVA); | |
| } | |
| public boolean isAccessModificationEnabled(boolean allowAccessModification) { | |
| return allowAccessModification || isAccessModificationEnabledByDefault(); | |
| } | |
| public boolean isAccessModificationEnabledByDefault() { | |
| return true; | |
| } | |
| public boolean runtimeWithClassValue() { | |
| assert isCfRuntime() || isDexRuntime(); | |
| return isCfRuntime() || getDexRuntimeVersion().isNewerThanOrEqual(DexVm.Version.V14_0_0); | |
| } | |
| // Convenience predicates. | |
| public boolean isDexRuntime() { | |
| return runtime.isDex(); | |
| } | |
| public DexRuntime asDexRuntime() { | |
| return getRuntime().asDex(); | |
| } | |
| public boolean isCfRuntime() { | |
| return runtime.isCf(); | |
| } | |
| public boolean isCfRuntime(CfVm vm) { | |
| return isCfRuntime(cfRuntime -> cfRuntime.getVm() == vm); | |
| } | |
| public boolean isCfRuntime(Predicate<CfRuntime> predicate) { | |
| if (!isCfRuntime()) { | |
| return false; | |
| } | |
| List<CfRuntime> cfRuntimes = runtime.asCfRuntimes(); | |
| boolean result = predicate.test(cfRuntimes.get(0)); | |
| for (int i = 1; i < cfRuntimes.size(); i++) { | |
| if (predicate.test(cfRuntimes.get(i)) != result) { | |
| throw new Unreachable( | |
| "Ambiguous predicate on CollapsedCfRuntimes: " | |
| + runtime | |
| + "; use applyIfCfRuntime() or withoutCollapsedCfRuntimes()"); | |
| } | |
| } | |
| return result; | |
| } | |
| public CfRuntime asCfRuntime() { | |
| return getRuntime().asCf(); | |
| } | |
| public boolean isDexRuntimeVersion(DexVm.Version vm) { | |
| return isDexRuntime() && getDexRuntimeVersion().isEqualTo(vm); | |
| } | |
| public boolean isDexRuntimeVersionNewerThanOrEqual(DexVm.Version vm) { | |
| return isDexRuntime() && getDexRuntimeVersion().isNewerThanOrEqual(vm); | |
| } | |
| public boolean isDexRuntimeVersionOlderThanOrEqual(DexVm.Version vm) { | |
| return isDexRuntime() && getDexRuntimeVersion().isOlderThanOrEqual(vm); | |
| } | |
| public boolean isNoneRuntime() { | |
| return runtime == NoneRuntime.getInstance(); | |
| } | |
| public void configureApiLevel(TestAppViewBuilder testAppViewBuilder) { | |
| testAppViewBuilder.setMinApi(apiLevel); | |
| } | |
| public void configureApiLevel(TestCompilerBuilder<?, ?, ?, ?, ?> testCompilerBuilder) { | |
| testCompilerBuilder.setMinApi(apiLevel); | |
| } | |
| public boolean hasApiLevel() { | |
| return apiLevel != null; | |
| } | |
| // TODO(b/270021825): Tests should not access the underlying API level directly, but may be | |
| // allowed to query if the api level satisfies some condition. | |
| @Deprecated | |
| public AndroidApiLevel getApiLevel() { | |
| if (runtime.isDex() && apiLevel == null) { | |
| throw new RuntimeException( | |
| "Use of getApiLevel without configured API levels for TestParametersCollection."); | |
| } | |
| return apiLevel; | |
| } | |
| public Path getDefaultAndroidJar() { | |
| assert isDexRuntime(); | |
| return ToolHelper.getFirstSupportedAndroidJar(getApiLevel()); | |
| } | |
| public Path getDefaultAndroidJarAbove(AndroidApiLevel minimumCompileApiLevel) { | |
| assert isDexRuntime(); | |
| return ToolHelper.getFirstSupportedAndroidJar(getApiLevel().max(minimumCompileApiLevel)); | |
| } | |
| public Path getDefaultRuntimeLibrary() { | |
| return isCfRuntime() ? TestDeps.getJava8RuntimeJar() : getDefaultAndroidJar(); | |
| } | |
| public CfRuntime getCfRuntime() { | |
| assert isCfRuntime(); | |
| return runtime.asCf(); | |
| } | |
| public PartialCompilationTestParameters getPartialCompilationTestParameters() { | |
| return partialCompilationTestParameters; | |
| } | |
| public boolean isRandomPartialCompilation() { | |
| return getPartialCompilationTestParameters().isRandom(); | |
| } | |
| // Access to underlying runtime/wrapper. | |
| public TestRuntime getRuntime() { | |
| return runtime; | |
| } | |
| public boolean isOrSimulateNoneRuntime() { | |
| return isNoneRuntime() | |
| || (runtime != null | |
| && runtime.isCf() | |
| && runtime.asCfRuntimes().contains(TestRuntime.getDefaultCfRuntime())); | |
| } | |
| public boolean isRepresentativeApiLevelForRuntime() { | |
| return representativeApiLevelForRuntime; | |
| } | |
| // Helper function to get the "backend" for a given runtime target. | |
| public Backend getBackend() { | |
| return runtime.getBackend(); | |
| } | |
| @Override | |
| public String toString() { | |
| StringBuilder builder = new StringBuilder(runtime.toString()); | |
| if (apiLevel != null) { | |
| builder.append(", api:").append(apiLevel.getNumericString()); | |
| } | |
| if (partialCompilationTestParameters.isSome()) { | |
| builder.append(", partial:").append(partialCompilationTestParameters); | |
| } | |
| return builder.toString(); | |
| } | |
| public void assertCfRuntime() { | |
| assertTrue(runtime.isCf()); | |
| } | |
| public void assertNoneRuntime() { | |
| assertEquals(NoneRuntime.getInstance(), runtime); | |
| } | |
| public void assertIsRepresentativeApiLevelForRuntime() { | |
| assertTrue(representativeApiLevelForRuntime); | |
| } | |
| public TestParameters assumeCfRuntime() { | |
| assumeTrue(isCfRuntime()); | |
| return this; | |
| } | |
| public TestParameters assumeDexRuntime() { | |
| return assumeDexRuntime(null); | |
| } | |
| public TestParameters assumeDexRuntime(String unusedReason) { | |
| assumeTrue(isDexRuntime()); | |
| return this; | |
| } | |
| public TestParameters assumeNoneRuntime() { | |
| assumeTrue(isNoneRuntime()); | |
| return this; | |
| } | |
| public TestParameters assumeIsOrSimulateNoneRuntime() { | |
| assumeTrue(isOrSimulateNoneRuntime()); | |
| return this; | |
| } | |
| public TestParameters assumeNoPartialCompilation() { | |
| return assumeNoPartialCompilation(null); | |
| } | |
| public TestParameters assumeNoPartialCompilation(String unusedReason) { | |
| assumeTrue(getPartialCompilationTestParameters().isNone()); | |
| return this; | |
| } | |
| public boolean isJvmTestParameters() { | |
| if (isCfRuntime()) { | |
| return apiLevel == null || representativeApiLevelForRuntime; | |
| } | |
| return false; | |
| } | |
| public TestParameters assumeJvmTestParameters() { | |
| assumeCfRuntime(); | |
| assumeTrue(apiLevel == null || representativeApiLevelForRuntime); | |
| assumeTrue(partialCompilationTestParameters.isNone()); | |
| return this; | |
| } | |
| public TestParameters assumeProguardTestParameters() { | |
| assumeCfRuntime(); | |
| assumeTrue(apiLevel == null || representativeApiLevelForRuntime); | |
| assumeTrue(partialCompilationTestParameters.isNone()); | |
| return this; | |
| } | |
| public TestParameters assumeR8TestParameters() { | |
| assumeTrue(isR8TestParameters()); | |
| return this; | |
| } | |
| public boolean isR8TestParameters() { | |
| assertFalse( | |
| "No need to use is/assumeR8TestParameters() when not using api levels for CF", | |
| isCfRuntime() && apiLevel == null); | |
| assertTrue(apiLevel != null || representativeApiLevelForRuntime); | |
| return (isDexRuntime() || representativeApiLevelForRuntime) && !isNoneRuntime(); | |
| } | |
| public TestParameters assumeCanUseR8Partial() { | |
| assumeTrue(canUseR8Partial()); | |
| return this; | |
| } | |
| public boolean canUseR8Partial() { | |
| return (isDexRuntime() || isNoneRuntime()) | |
| && (apiLevel == null || apiLevel.isGreaterThanOrEqualTo(AndroidApiLevel.L)); | |
| } | |
| public TestParameters assumeRuntimeTestParameters() { | |
| assertFalse( | |
| "No need to use assumeRuntimeTestParameters() when not using api levels for CF", | |
| apiLevel == null); | |
| assumeTrue(isDexRuntime() || representativeApiLevelForRuntime); | |
| return this; | |
| } | |
| public DexVm getDexVm() { | |
| assertTrue(isDexRuntime()); | |
| return getRuntime().asDex().getVm(); | |
| } | |
| public DexVm.Version getDexRuntimeVersion() { | |
| assertTrue(isDexRuntime()); | |
| return getRuntime().asDex().getVm().getVersion(); | |
| } | |
| TestParameters withPartialCompilationTestParameters( | |
| PartialCompilationTestParameters partialCompilationTestParameters) { | |
| assert getPartialCompilationTestParameters().isNone(); | |
| return new TestParameters( | |
| runtime, apiLevel, partialCompilationTestParameters, representativeApiLevelForRuntime); | |
| } | |
| } |