diff --git a/.github/workflows/debug.yml b/.github/workflows/debug.yml index b0f21d2304..6ef3e75efc 100644 --- a/.github/workflows/debug.yml +++ b/.github/workflows/debug.yml @@ -221,6 +221,21 @@ jobs: exit "$status" fi + # ADFA-7138: builds the minified release and the debug variant of a sample app that opted into the + # SQLite debugger, with the real debugger, rewrite runtime and requery AARs, and runs + # scripts/check-release-apk-no-sqlite-debugger.sh on both. The release must pass; the debug APK must + # fail, which shows the check still sees the debugger. The AARs it builds are the ones the assemble + # below bundles, so they are not built twice. + - name: Check that an opted-in app's release APK carries no SQLite debugger + run: | + command -v unzip || { sudo apt-get update && sudo apt-get install -y unzip; } + # The test stages :logger, :logsender and :build-info into build-local Maven repositories, which + # needs no signing. With the job's signing key present MavenPublishConf runs the Sign tasks, and + # they fail, so the key is left out of this step. + env -u ORG_GRADLE_PROJECT_signingInMemoryKey -u ORG_GRADLE_PROJECT_signingInMemoryKeyId \ + -u ORG_GRADLE_PROJECT_signingInMemoryKeyPassword \ + flox activate -d flox/base -- ./gradlew :gradle-plugin:sqliteReleaseApkTest --no-daemon + - name: Install advancecomp (advzip, used by recompressApk) run: | command -v advzip || { sudo apt-get update && sudo apt-get install -y advancecomp; } diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 62828449ad..8a7174cc29 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -95,7 +95,7 @@ Strategy: **layer-and-subsystem based**, not feature-by-feature. The Gradle buil - **`app` depends inward; libraries never depend on `app`.** Subsystems are consumed by `app`, not vice versa. - **Vendored forks are substituted, not imported ad hoc.** `composite-builds/build-deps` and `build-deps-common` provide forked `javac`/`jdt`/`layoutlib`/etc.; `settings.gradle.kts` substitutes them in for `com.itsaky.androidide.build:*`. Don't add a Maven coordinate for something already substituted. Pebble is likewise built from source, as `subprojects:pebble`, for a lexer fix Android needs (its `NOTICE` says what changed); depend on `projects.subprojects.pebble`, not `io.pebbletemplates:pebble`. -- **All module config flows through `composite-builds/build-logic`.** Every Android module gets the `v7`/`v8` ABI flavors centrally (`AndroidModuleConf.kt`) — there is no flavorless `assembleDebug`. `:plugin-api`, `:subprojects:sqlite-js` and `:subprojects:sqlite-debugger`, libraries for other projects that need one artifact, are intentionally excluded from flavors. +- **All module config flows through `composite-builds/build-logic`.** Every Android module gets the `v7`/`v8` ABI flavors centrally (`AndroidModuleConf.kt`) — there is no flavorless `assembleDebug`. `:plugin-api`, `:subprojects:sqlite-js`, `:subprojects:sqlite-debugger` and `:subprojects:sqlite-rewrite-runtime`, libraries for other projects that need one artifact, are intentionally excluded from flavors. - **`RepositoriesMode.FAIL_ON_PROJECT_REPOS`** — repositories are declared once in `settings.gradle.kts`; modules must not declare their own. - **Avoid new dependencies.** Check `gradle/libs.versions.toml` first; the build almost certainly already has it. @@ -104,11 +104,12 @@ Strategy: **layer-and-subsystem based**, not feature-by-feature. The Gradle buil These structural facts shape every module. Day-to-day build *commands* live in `CLAUDE.md`; the rules that produce them live here. - **Centralized convention logic.** All Android module setup flows through `composite-builds/build-logic` (`conf/AndroidModuleConf.kt`). Modules stay thin and share configuration, so understanding any module's setup starts here. -- **ABI product flavors.** Every Android module *except* `:plugin-api`, `:subprojects:sqlite-js` and `:subprojects:sqlite-debugger` gets two flavors on the `abi` dimension — `v7` (`armeabi-v7a`) and `v8` (`arm64-v8a`) — defined centrally. There is no flavorless variant; tasks are `assembleV8Debug`, `assembleV7Release`, etc. +- **ABI product flavors.** Every Android module *except* `:plugin-api`, `:subprojects:sqlite-js`, `:subprojects:sqlite-debugger` and `:subprojects:sqlite-rewrite-runtime` gets two flavors on the `abi` dimension — `v7` (`armeabi-v7a`) and `v8` (`arm64-v8a`) — defined centrally. There is no flavorless variant; tasks are `assembleV8Debug`, `assembleV7Release`, etc. - **SDK levels** (`build-logic/.../build/config/BuildConfig.kt`): `COMPILE_SDK=36`, `MIN_SDK=28`, `TARGET_SDK=28`. **`TARGET_SDK` is deliberately pinned at 28:** higher targets enforce W^X (write-xor-execute), which blocks executing code from app-writable files. That is fatal for an on-device IDE that compiles and runs code (Gradle, `javac`, Termux binaries), so it is a hard requirement, not tech debt. `MIN_SDK_FOR_APPS_BUILT_WITH_COGO=16` is the floor for the apps a *user* builds with CoGo — distinct from CoGo's own `MIN_SDK`. - **Native asset bundling.** The on-device LLM (`llama-impl`) ships as a per-flavor native AAR, wired through the root `build.gradle.kts` (`bundleLlamaV8Assets` / `assembleV8Assets`, …); prebuilt per-flavor assets live under `assets/release/v7/` and `assets/release/v8/`. - **sqlite-js for apps built with CoGo** (ADFA-7082). `subprojects:sqlite-js` builds the AAR that the JavaScript-in-SQLite project template depends on: the sqlite-js release vendored (see its README), for arm64-v8a and armeabi-v7a, minSdk 26, without core-library desugaring. Nothing in CoGo depends on it. Its default entry point runs no code stored in a database; loading stored functions is a separate entry point, only for databases the app made, since loading runs their code. `test-sqlite-js.yml` runs its host tests on pull requests. The manual `release-sqlite-js.yml` publishes it to the moving `sqlite-js-latest` release, from which dev-assets fetches it into the template, as it fetches `plugin-api-latest` for the plugin template. - **SQLite debugger for apps built with CoGo** (ADFA-7131). `subprojects:sqlite-debugger` builds an AAR with a debug-only SQLite debugger and MCP server for apps built with CoGo: a native extension on requery's SQLite plus a loopback HTTP server. Like sqlite-js it has no ABI flavors, and nothing in CoGo depends on it; see its README. +- **AARs bundled for apps built with CoGo.** `:logsender`, `:subprojects:sqlite-debugger` and `:subprojects:sqlite-rewrite-runtime` are built as AARs and bundled into the IDE's assets (`AndroidIDEAssetsPlugin`). Code on the Go extracts them and passes their paths to Gradle as properties, and its Gradle plugin injects each into the app: logsender and the debugger into debuggable variants, the rewrite runtime into every variant of a project with `cogo.sqliteDebugger=true`. `:subprojects:sqlite-rewrite-runtime` is Java-only, has no ABI flavors and holds the helpers the build-time rewrite to requery redirects to (ADFA-7138); see `gradle-plugin/docs/sqlite-rewrite.md`. - **Native lib compression** (ADFA-2306, ADFA-4729). The app manifest hard-codes `android:extractNativeLibs="true"` (required: the installer must materialize libs in `nativeLibraryDir`, e.g. `libshizuku.so` is an executable the adb shell runs from there). That attribute overrides the `jniLibs.useLegacyPackaging` DSL, so AGP packages `lib//*.so` deflate-compressed in **every** APK — ~5.9 MB smaller (`libtree-sitter-kotlin.so` alone is 4.18 MB → 339 kB). The trap is the `recompressApk` post-step (release always, debug in CI only): its no-compress lists in `app/build.gradle.kts` must NOT contain `"so"`, or it silently re-stores the libs and undoes the saving — which is what ADFA-2306 fixed for release and ADFA-4729 for CI debug. Locally built debug APKs (including the e2e farm's) never run that step and were always fine. - **Quick Build shares the Gradle distribution's toolchain** (ADFA-4931). `:app`'s `quickBuildDaemonZip` leaves the Kotlin compiler and eight other jars out of the daemon zip, because the on-device Gradle distribution already carries them; `QuickBuildArtifactStager` links them in at provision time. That makes the daemon's Kotlin version the distribution's, so `:quickbuild:daemon` takes its Kotlin artifacts at `org.adfa.constants.GRADLE_DISTRIBUTION_KOTLIN_VERSION`, and `:app:quickBuildDistJarCheck` fails the build when the distribution it packages lacks a linked jar. See [ADR 0016](docs/adr/0016-quick-build-compiles-outside-gradle.md). - **`app` package layout is by concern, not feature:** `activities`, `fragments`, `services`, `di`, `agent`, `viewmodel(s)`, `repositories`, `roomData`, `localWebServer`, `preferences`, `ui` (Compose screens live under `ui/compose`), `templates/manager` (the Manager screen's `.cgt`-parsing data layer, with direct filesystem access to `Environment.TEMPLATES_DIR` — distinct from the plugin-facing `IdeTemplateService` in `plugin-api`/`plugin-manager`), `utils`, …. diff --git a/CLAUDE.md b/CLAUDE.md index 7334588a8c..1d9f9d7499 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -15,7 +15,7 @@ flox activate -d flox/local -- ./gradlew ``` - **Debug APK (arm64, the usual target):** `flox activate -d flox/local -- ./gradlew :app:assembleV8Debug --parallel --max-workers=6` -- **Module unit tests:** `flox activate -d flox/local -- ./gradlew :editor:testV7DebugUnitTest` - unit tests are per-flavor, there is no plain `test` task. `:plugin-api`, `:subprojects:sqlite-js` and `:subprojects:sqlite-debugger` are the modules without flavors: `:plugin-api:testDebugUnitTest`. +- **Module unit tests:** `flox activate -d flox/local -- ./gradlew :editor:testV7DebugUnitTest` - unit tests are per-flavor, there is no plain `test` task. `:plugin-api`, `:subprojects:sqlite-js`, `:subprojects:sqlite-debugger` and `:subprojects:sqlite-rewrite-runtime` are the modules without flavors: `:plugin-api:testDebugUnitTest`. - **Single unit test:** append `--tests "com.itsaky.androidide.SomeTest"` to the flavored task above. - **Fast iteration:** during multi-file/multi-module changes, verify with targeted `:module:compileV8DebugKotlin`/`compileV8DebugJavaWithJavac` invocations (batch several modules into one Gradle call) rather than a full assemble. Reserve `:app:assembleV8Debug` for final end-to-end verification — it's slow (multi-minute) in this multi-module project, and running it after every small change adds up. - **Long-running commands: narrate them, and background only the read-only ones.** Anything that can exceed ~60s — `assembleV8Debug`, a full test sweep, a cold Gradle invocation of any kind (daemon start plus configuring this many modules is ~20s before any task runs, and far worse under memory pressure), and `git push`, which runs Spotless through the hook — gets a line before it starts saying what is running and roughly how long it takes, and a status line every couple of minutes while it runs: elapsed time, the last output line, whether it is still progressing. A silent terminal is indistinguishable from a hang, and saying which it is is your job, not the user's to ask. @@ -26,7 +26,7 @@ When the user names a CI/CD job ("the sonar job", "the analyze workflow"), read ### ABI product flavors (v7 / v8) -Every module except `:plugin-api`, `:subprojects:sqlite-js` and `:subprojects:sqlite-debugger` carries `v7` (`armeabi-v7a`) and `v8` (`arm64-v8a`) flavors, so build tasks are `assembleV8Debug`, `assembleV7Release`, etc. — there is **no** flavorless `assembleDebug`. Those three build one artifact for other projects. See [ARCHITECTURE.md](ARCHITECTURE.md) (Build & Module Configuration) for flavors, native asset bundling, and SDK levels. +Every module except `:plugin-api`, `:subprojects:sqlite-js`, `:subprojects:sqlite-debugger` and `:subprojects:sqlite-rewrite-runtime` carries `v7` (`armeabi-v7a`) and `v8` (`arm64-v8a`) flavors, so build tasks are `assembleV8Debug`, `assembleV7Release`, etc. — there is **no** flavorless `assembleDebug`. Those four build one artifact for other projects. See [ARCHITECTURE.md](ARCHITECTURE.md) (Build & Module Configuration) for flavors, native asset bundling, and SDK levels. ### Emulator / device diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 55c5fdec0f..8164293585 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -523,6 +523,9 @@ dependencies { // (QuickBuildArtifactStager). evaluationDependsOn(":quickbuild:runtime") evaluationDependsOn(":quickbuild:daemon") +// The assets plugin bundles its release AAR (ADFA-7131). +evaluationDependsOn(":subprojects:sqlite-debugger") +evaluationDependsOn(":subprojects:sqlite-rewrite-runtime") // ADFA-4931: jars the daemon loads from the on-device Gradle distribution instead of shipping its // own copy, since `gradle-dists/` and `quickbuild/` both sit under . diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt index 40f2dc6425..5942453004 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt @@ -879,9 +879,7 @@ open class EditorHandlerActivity : popup.setOnMenuItemClickListener { item -> when (item.itemId) { R.id.action_quick_build -> { - // Through the registry, same as Standard Run below, so the menu entry - // and the toolbar tap share one code path (incl. the analytics event). - if (quickBuild != null && quickBuild.enabled) registry.executeAction(quickBuild, data) + runQuickBuildAction(data) true } @@ -907,6 +905,22 @@ open class EditorHandlerActivity : popup.show() } + /** + * Runs the toolbar's Quick Build action through the registry, as the button's own tap does, so + * the dropdown row and a status line that asks for a tap share the button's code path + * (including the analytics event). Does nothing while the button is greyed out, as the button + * does. + */ + private fun runQuickBuildAction(data: ActionData) { + val registry = getInstance() as DefaultActionsRegistry + val quickBuild = registry.findAction(EDITOR_TOOLBAR, QuickBuildAction.ID) ?: return + if (quickBuild.enabled) registry.executeAction(quickBuild, data) + } + + override fun onQuickBuildStatusLineTapped() { + runQuickBuildAction(createToolbarActionData()) + } + private fun createToolbarActionData(): ActionData { val data = ActionData.create(this) val currentEditor = getCurrentEditor() diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt index 126b52f792..59598551fb 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt @@ -541,6 +541,10 @@ abstract class ProjectHandlerActivity : // setStatus resets ownership (any caller takes the bar over); reclaim it. setStatus(getString(update.text, *update.args.toTypedArray())) ownsQuickBuildStatus = true + // After setStatus, which drops any tap the line had. + update.tapLabel?.let { label -> + contentOrNull?.bottomSheet?.setStatusTap(getString(label), ::onQuickBuildStatusLineTapped) + } } } @@ -1438,9 +1442,17 @@ abstract class ProjectHandlerActivity : gravity: Int, ) { ownsQuickBuildStatus = false + // A tap belongs to the line that asked for it; the next writer's line is plain text. + contentOrNull?.bottomSheet?.setStatusTap(null, null) super.doSetStatus(text, gravity) } + /** + * A tap on a Quick Build status line that asks for one: it does what a tap on the Quick Build + * button does, through the same action, so the line cannot promise what the button would not. + */ + protected abstract fun onQuickBuildStatusLineTapped() + fun appendBuildOutput(str: String) { if (_binding == null || isDestroyed || isFinishing) return content.bottomSheet.appendBuildOut(str) diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt index 7b616cfc23..03aed21800 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt @@ -15,6 +15,7 @@ import com.itsaky.androidide.tooling.api.messages.BuildRunType import com.itsaky.androidide.tooling.api.messages.GradleBuildParams import com.itsaky.androidide.tooling.api.messages.TaskExecutionMessage import com.itsaky.androidide.tooling.api.messages.result.TaskExecutionResult +import com.itsaky.androidide.utils.Environment import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay @@ -111,6 +112,10 @@ class GradleQuickBuildProvisioner( built } + ProxyAppBuildResult.Refused -> { + return ProvisionOutcome.Failure(QuickBuildMessage.DisabledBySqliteDebugger) + } + is ProxyAppBuildResult.Failed -> { return ProvisionOutcome.Failure( built.message?.let(QuickBuildMessage::Literal) @@ -164,6 +169,7 @@ class GradleQuickBuildProvisioner( extraSourceRoots = proxyApp.sourceRoots, stableIdsFile = proxyApp.stableIdsFile, libraryResourceFlats = proxyApp.libraryResourceFlats, + gradleUserHome = Environment.GRADLE_USER_HOME, ), variantName = buildResult.variantName, baselineGeneration = buildResult.baselineGeneration, @@ -201,6 +207,10 @@ class GradleQuickBuildProvisioner( return ProxyAppRebuildOutcome.BuildSlotBusy } + ProxyAppBuildResult.Refused -> { + return ProxyAppRebuildOutcome.Failure(QuickBuildMessage.DisabledBySqliteDebugger) + } + is ProxyAppBuildResult.Failed -> { return ProxyAppRebuildOutcome.Failure( built.message?.let(QuickBuildMessage::Literal) @@ -246,6 +256,7 @@ class GradleQuickBuildProvisioner( extraSourceRoots = buildResult.proxyApp.sourceRoots, stableIdsFile = buildResult.proxyApp.stableIdsFile, libraryResourceFlats = buildResult.proxyApp.libraryResourceFlats, + gradleUserHome = Environment.GRADLE_USER_HOME, ), ) } @@ -312,6 +323,13 @@ class GradleQuickBuildProvisioner( /** Another Gradle build owns the single slot, so nothing ran. */ data object SlotBusy : ProxyAppBuildResult + /** + * Quick Build is deliberately off for this project (the SQLite debugger is on), so nothing + * ran and nothing failed. Split from [Failed] so the session can say so rather than offer a + * retry. + */ + data object Refused : ProxyAppBuildResult + /** * [message] replaces the caller's generic wording when the cause is one the user can * act on. Null keeps the generic "proxy app build failed" for genuine build failures. @@ -340,11 +358,19 @@ class GradleQuickBuildProvisioner( log.info("A Gradle build is already in progress; not staging for the Quick Build proxy app build") return ProxyAppBuildResult.SlotBusy } - stage(context, paths) - val projectManager = lookupProjectManager() val projectRoot = File(projectManager.projectDirPath) + // Before staging, which a refused build need not pay for: the Gradle plugin never applies Quick + // Build to an opted-in project, so building would only end in "variant setup missing". Only once + // the model has the app modules: during a sync this check would read the root's file without + // theirs, so a root opt-in would refuse a project whose module turns it off. The second check, + // after the wait for the model, decides then. + if (projectManager.workspace != null) { + sqliteDebuggerRefusal(projectManager, projectRoot)?.let { return it } + } + stage(context, paths) + // The project model only exists once CoGo's Gradle sync has populated it, and a tap // during sync is common (the user opens a project and reaches straight for Quick // Build). Queue behind the sync rather than failing: the session is already in @@ -355,6 +381,9 @@ class GradleQuickBuildProvisioner( context.getString(R.string.quick_build_waiting_for_sync), ) } + // Again, whatever the first check saw: a sync during the staging or the wait can publish modules, and + // one of them can carry the opt-in in its own gradle.properties. + sqliteDebuggerRefusal(projectManager, projectRoot)?.let { return it } val module = quickBuildModule(projectManager) @@ -526,6 +555,33 @@ class GradleQuickBuildProvisioner( } } + /** + * The refusal for a project with `cogo.sqliteDebugger=true`, or null. It reads the app modules' + * own `gradle.properties` only for the modules [projectManager] knows, which is none before the + * sync publishes the model, so it decides only once the model is published. + */ + private fun sqliteDebuggerRefusal( + projectManager: IProjectManager, + projectRoot: File, + ): ProxyAppBuildResult.Refused? { + // Every app module's gradle.properties is read, although Quick Build builds only the first + // app module: the opt-in is decided per project, and the first file that defines the + // property decides it (the Gradle user home, then the modules in order, then the root), so + // a module's `false` overrides a root `true` as it does in Gradle. + val enabled = + QuickBuildProjectSupport.isSqliteDebuggerEnabled( + projectRoot, + Environment.GRADLE_USER_HOME, + projectManager.getAndroidAppModules().map { File(it.projectDirPath) }, + ) + if (!enabled) return null + log.info( + "Quick Build is not provisioned: {} is true in the Gradle user home's, an app module's or the project's gradle.properties", + GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED, + ) + return ProxyAppBuildResult.Refused + } + /** `:app` -> `/app`; nested paths (`:feature:home`) map to nested dirs. */ private fun moduleDir( projectRoot: File, diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildFlashes.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildFlashes.kt index 30294662df..1b0167691c 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildFlashes.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildFlashes.kt @@ -77,6 +77,7 @@ class QuickBuildFlashes { // nothing here either. QuickBuildTransition.SessionStopped, QuickBuildTransition.StartFailed, + QuickBuildTransition.StartRefused, -> { flashedFailure = null null diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildMessages.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildMessages.kt index fe4ce8daa3..0f76e5a6c7 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildMessages.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildMessages.kt @@ -82,6 +82,10 @@ fun QuickBuildMessage.resolve(context: Context): String = context.getString(R.string.quick_build_scratch_dir_unavailable, path) } + QuickBuildMessage.DisabledBySqliteDebugger -> { + context.getString(R.string.quick_build_disabled_sqlite_debugger) + } + QuickBuildMessage.DaemonRejectedConfiguration -> { context.getString(R.string.quick_build_daemon_rejected_config) } diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildOutputLines.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildOutputLines.kt index 3b8d9ef50a..4cf21345b6 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildOutputLines.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildOutputLines.kt @@ -48,6 +48,12 @@ fun quickBuildOutputLines( listOf("could not start - tap Quick Build to retry.") } + // The refusal itself went to the flash and the log line that made it; a refusal is not a + // failure, so this only says what the tap does. + QuickBuildTransition.StartRefused -> { + listOf("off while the SQLite debugger is on - tap Quick Build to see why.") + } + is QuickBuildTransition.ProvisioningStarted -> { when (val kind = transition.kind) { is ProvisioningKind.Rebaseline -> { diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt index bdef298fd4..6ad9f0beac 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt @@ -2,6 +2,12 @@ package com.itsaky.androidide.quickbuild import androidx.annotation.StringRes import com.itsaky.androidide.resources.R +import com.itsaky.androidide.tooling.api.GradlePluginConfig +import org.slf4j.LoggerFactory +import java.io.File +import java.io.IOException +import java.io.StringReader +import java.util.Properties /** * The reasons Quick Build refuses a project up front, as string resources. @@ -61,4 +67,66 @@ object QuickBuildProjectSupport { } else { null } + + /** + * Whether one `gradle.properties` text opts in, decided as the Gradle plugin does: the value is + * exactly `true`. + * + * @param gradleProperties the file's text, or null when there is none. + */ + fun isSqliteDebuggerEnabled(gradleProperties: String?): Boolean = gradleProperties != null && propertyValue(gradleProperties) == "true" + + /** + * Whether the project opts in to `cogo.sqliteDebugger=true`, in which case Quick Build must not + * provision it: the Gradle plugin moves such a project to requery and never applies Quick Build + * to it, so a build would succeed and end in a "variant setup missing" message that blames the + * variant. Decided per project, from all app modules (see [moduleDirs]). + * + * Reads the opt-in from the same files the Gradle plugin's `findProperty` sees in + * Code on the Go's builds: the Gradle user home `gradle.properties` (`GRADLE_USER_HOME`, which + * overrides the project files), the app module's, and the root's. Gradle loads a + * `gradle.properties` from every project directory into that project, and a subproject also + * inherits its parent's properties, so the first of these that defines the property wins. + * `-P` arguments are not read: Code on the Go passes none for this property. + * + * A file that cannot be read is reported to [onReadFailure] and counts as not defining it. + * + * @param moduleDirs the directories of the app modules the plugin is applied to. + */ + fun isSqliteDebuggerEnabled( + projectRoot: File, + gradleUserHome: File?, + moduleDirs: List = emptyList(), + onReadFailure: (File, Throwable) -> Unit = { file, error -> + log.warn("Cannot read {}; treating it as not defining the SQLite debugger opt-in", file, error) + }, + readText: (File) -> String = File::readText, + ): Boolean { + val files = + listOfNotNull(gradleUserHome) + moduleDirs + projectRoot + for (entry in files) { + val file = File(entry, "gradle.properties") + val text = + try { + file.takeIf { it.isFile }?.let(readText) + } catch (e: IOException) { + onReadFailure(file, e) + null + } ?: continue + val value = propertyValue(text) ?: continue + return value == "true" + } + return false + } + + private fun propertyValue(gradleProperties: String): String? = + try { + Properties() + .apply { load(StringReader(gradleProperties)) } + .getProperty(GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED) + } catch (e: IllegalArgumentException) { + null + } + + private val log = LoggerFactory.getLogger("QB-Support") } diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt index 47f9f10694..eef44fcf6e 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt @@ -21,11 +21,16 @@ sealed interface QuickBuildStatusBarUpdate { * @property args positional format arguments for [text], in order. * @property onlyIfOwned apply only if Quick Build's text is still on the bar, so a passive * refresh cannot clobber a line another writer took over. + * @property tapLabel set for a [text] that asks for a tap: a tap on the line then does what a + * tap on the Quick Build button does, and this is what TalkBack says the tap does. Null for + * a line that is plain text. The tap lasts only while this text is on the bar; the next + * write to the bar, by anyone, drops it. */ data class Show( @param:StringRes @get:StringRes val text: Int, val args: List = emptyList(), val onlyIfOwned: Boolean = false, + @param:StringRes @get:StringRes val tapLabel: Int? = null, ) : QuickBuildStatusBarUpdate /** Clear the bar - but only if the last write was Quick Build's (the caller tracks that). */ @@ -88,6 +93,16 @@ fun quickBuildStatusBarUpdate( QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_start_failed) } + QuickBuildTransition.StartRefused -> { + // Not a failure: Quick Build is deliberately off here, and a retry cannot help. The text + // says "tap for details", so the line itself takes the tap: it runs the button's tap, + // which re-runs the cheap check and shows the full refusal again. + QuickBuildStatusBarUpdate.Show( + R.string.quick_build_status_off_sqlite_debugger, + tapLabel = R.string.quick_build_status_off_sqlite_debugger_action, + ) + } + is QuickBuildTransition.ProvisioningStarted -> { when (transition.kind) { is ProvisioningKind.Rebaseline -> { diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildTransitions.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildTransitions.kt index 289192acbc..e24a80b995 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildTransitions.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildTransitions.kt @@ -30,6 +30,13 @@ internal sealed interface QuickBuildTransition { */ data object StartFailed : QuickBuildTransition + /** + * A session start was declined on purpose: Quick Build is off for this project while the + * SQLite debugger is on. Nothing failed and retrying cannot help, so surfaces must not say + * "failed" or offer a retry; the tap shows the refusal again. + */ + data object StartRefused : QuickBuildTransition + /** * A full Gradle build started. * @@ -132,7 +139,9 @@ internal fun quickBuildTransition( } return when (current) { is QuickBuildStatus.Hidden -> { - if (current.lastStartFailed) { + if (current.lastStartFailed && current.startRefused) { + QuickBuildTransition.StartRefused + } else if (current.lastStartFailed) { QuickBuildTransition.StartFailed } else { QuickBuildTransition.SessionStopped diff --git a/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt b/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt index 88452a86ad..640054e6c9 100644 --- a/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt +++ b/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt @@ -53,6 +53,8 @@ import com.itsaky.androidide.tooling.api.ForwardingToolingApiClient import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_JDWP_ENABLED import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_LOG_SENDER_AAR import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_LOG_SENDER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR import com.itsaky.androidide.tooling.api.IToolingApiClient import com.itsaky.androidide.tooling.api.IToolingApiServer import com.itsaky.androidide.tooling.api.messages.BuildId @@ -726,6 +728,10 @@ class GradleBuildService : extraArgs.add("-P${PROPERTY_JDWP_ENABLED}=$enableJdwp") extraArgs.add("-P${PROPERTY_LOG_SENDER_ENABLED}=$enableLogSender") extraArgs.add("-P${PROPERTY_LOG_SENDER_AAR}=${Environment.LOGSENDER_AAR.absolutePath}") + // Always passed, like logsender's: the plugin reads it only for a project that opted in, and + // then names the missing file instead of a missing property. + extraArgs.add("-P${PROPERTY_SQLITE_DEBUGGER_AAR}=${Environment.SQLITE_DEBUGGER_AAR.absolutePath}") + extraArgs.add("-P${PROPERTY_SQLITE_REWRITE_RUNTIME_AAR}=${Environment.SQLITE_REWRITE_RUNTIME_AAR.absolutePath}") extraArgs.addAll(relocatedBuildOutputArgs(isSync)) diff --git a/app/src/main/java/com/itsaky/androidide/ui/EditorBottomSheet.kt b/app/src/main/java/com/itsaky/androidide/ui/EditorBottomSheet.kt index f4cf3638cd..c2294b296a 100644 --- a/app/src/main/java/com/itsaky/androidide/ui/EditorBottomSheet.kt +++ b/app/src/main/java/com/itsaky/androidide/ui/EditorBottomSheet.kt @@ -421,6 +421,7 @@ class EditorBottomSheet binding.viewOptionsOutputAction.clearLongPressHelp() binding.copyDiagnosticsFab.setOnClickListener(null) binding.headerContainer.setOnClickListener(null) + setStatusTap(null, null) removeOnLayoutChangeListener(fabLayoutChangeListener) ViewCompat.setOnApplyWindowInsetsListener(this, null) @@ -757,6 +758,20 @@ class EditorBottomSheet } } + /** + * Makes the status line a button for a line that asks for a tap, or plain text again with a + * null [onTap]. Main thread only, like the status writes it follows; see [bindStatusLineTap]. + * + * @param actionLabel what the tap does, for TalkBack's "double-tap to" announcement. + * @param onTap what the tap does, or null for plain text. + */ + fun setStatusTap( + actionLabel: CharSequence?, + onTap: (() -> Unit)?, + ) { + bindStatusLineTap(binding.buildStatus.statusText, actionLabel, onTap) + } + /** * Whether a user build is narrating into the status line. While it is, the collapsed * header keeps the tallest height it has needed; when it stops, the header re-fits the diff --git a/app/src/main/java/com/itsaky/androidide/ui/StatusLineTap.kt b/app/src/main/java/com/itsaky/androidide/ui/StatusLineTap.kt new file mode 100644 index 0000000000..0ccb582194 --- /dev/null +++ b/app/src/main/java/com/itsaky/androidide/ui/StatusLineTap.kt @@ -0,0 +1,37 @@ +package com.itsaky.androidide.ui + +import android.widget.TextView +import androidx.core.view.ViewCompat +import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat + +/** + * Makes the editor's status line a button while its text asks for a tap, and plain text again + * when it does not. + * + * Plain text the rest of the time, so a tap on the line falls through to the header under it, + * which expands the bottom sheet. The line's size is left alone: the collapsed header is measured + * from it, and at a 2x font scale it needs every row it takes. + * + * @param statusText the status line. + * @param actionLabel what a tap does, which TalkBack reads as "double-tap to [actionLabel]". + * Ignored without [onTap]. + * @param onTap what a tap does, or null to make the line plain text again. + */ +internal fun bindStatusLineTap( + statusText: TextView, + actionLabel: CharSequence?, + onTap: (() -> Unit)?, +) { + if (onTap == null) { + statusText.setOnClickListener(null) + statusText.isClickable = false + statusText.isFocusable = false + ViewCompat.removeAccessibilityAction(statusText, AccessibilityActionCompat.ACTION_CLICK.id) + return + } + statusText.setOnClickListener { onTap() } + // Focusable so a keyboard or switch user can reach it, not only a finger. + statusText.isFocusable = true + // A null command keeps the default click; only the spoken label is replaced. + ViewCompat.replaceAccessibilityAction(statusText, AccessibilityActionCompat.ACTION_CLICK, actionLabel, null) +} diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt index 168ca584de..987a2f03c5 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt @@ -69,6 +69,114 @@ class GradleQuickBuildProvisionerFailureArmsTest { assertThat(gradle.executedTasks).isNull() } + @Test + fun `a project that opted into the SQLite debugger is refused before staging, with its own message`() = + runTest { + File(projectRoot, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") + var staged = 0 + val gradle = FakeBuildService() + + val outcome = + testProvisioner( + context = context, + projectRoot = projectRoot, + buildService = gradle, + stage = { _, _ -> staged++ }, + ).provision() + + assertThat(failureText(outcome)).isEqualTo(context.getString(R.string.quick_build_disabled_sqlite_debugger)) + assertThat(staged).isEqualTo(0) + assertThat(gradle.executedTasks).isNull() + } + + @Test + fun `an opt-in in the app module's gradle properties is refused when the tap comes during the sync`() = + runTest { + val moduleDir = moduleDir() + File(moduleDir, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") + val gradle = FakeBuildService() + val project = + SyncingProjectManager( + FakeProjectManager( + projectRoot, + appModules = listOf(androidModule(buildDir = File(moduleDir, "build"), projectDir = moduleDir)), + ), + ) + + val outcome = + testProvisioner( + context = context, + projectRoot = projectRoot, + buildService = gradle, + projectManager = project, + // The sync publishes the modules while Quick Build stages, so only a check made after + // the wait for the model can read the module's gradle.properties. + stage = { _, _ -> project.published = true }, + ).provision() + + assertThat(failureText(outcome)).isEqualTo(context.getString(R.string.quick_build_disabled_sqlite_debugger)) + assertThat(gradle.executedTasks).isNull() + } + + @Test + fun `a root opt-in that the app module turns off is not refused when the tap comes during the sync`() = + runTest { + File(projectRoot, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") + val moduleDir = moduleDir() + File(moduleDir, "gradle.properties").writeText("cogo.sqliteDebugger=false\n") + val gradle = FakeBuildService() + val project = + SyncingProjectManager( + FakeProjectManager( + projectRoot, + appModules = listOf(androidModule(buildDir = File(moduleDir, "build"), projectDir = moduleDir)), + ), + ) + + val outcome = + testProvisioner( + context = context, + projectRoot = projectRoot, + buildService = gradle, + projectManager = project, + stage = { _, _ -> project.published = true }, + ).provision() + + // The module's file comes before the root's, so its false decides, as in Gradle: the build runs. + assertThat(failureText(outcome)).isNotEqualTo(context.getString(R.string.quick_build_disabled_sqlite_debugger)) + assertThat(gradle.executedTasks).isNotNull() + } + + @Test + fun `an opt-in in a module that a sync publishes during the staging is refused`() = + runTest { + val app = moduleDir() + val feature = File(projectRoot, "feature").apply { mkdirs() } + File(feature, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") + val appModule = androidModule(buildDir = File(app, "build"), projectDir = app) + val featureModule = androidModule(path = ":feature", buildDir = File(feature, "build"), projectDir = feature) + val gradle = FakeBuildService() + val project = + ResyncingProjectManager( + FakeProjectManager(projectRoot, appModules = listOf(appModule)), + FakeProjectManager(projectRoot, appModules = listOf(appModule, featureModule)), + ) + + val outcome = + testProvisioner( + context = context, + projectRoot = projectRoot, + buildService = gradle, + projectManager = project, + // The model was already published, so the first check read the old modules; the module with the + // opt-in appears only after it. + stage = { _, _ -> project.resynced = true }, + ).provision() + + assertThat(failureText(outcome)).isEqualTo(context.getString(R.string.quick_build_disabled_sqlite_debugger)) + assertThat(gradle.executedTasks).isNull() + } + @Test fun `a slot that goes busy between the staging and the build still reads as a busy slot`() = runTest { @@ -167,6 +275,21 @@ class GradleQuickBuildProvisionerFailureArmsTest { .isEqualTo(context.getString(R.string.quick_build_variant_setup_missing, "debug")) } + @Test + fun `a project with the SQLite debugger on is refused as an off state, not a setup failure`() = + runTest { + File(projectRoot, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") + val gradle = FakeBuildService(result = TaskExecutionResult(true, null)) + + val outcome = testProvisioner(context, projectRoot, buildService = gradle).provision() + + // Its own message, so the session can tell this deliberate refusal from a fault. + assertThat(outcome).isEqualTo(ProvisionOutcome.Failure(QuickBuildMessage.DisabledBySqliteDebugger)) + assertThat(failureText(outcome)).isEqualTo(context.getString(R.string.quick_build_disabled_sqlite_debugger)) + assertThat(failureText(outcome)).isNotEqualTo(context.getString(R.string.quick_build_setup_failed)) + assertThat(gradle.executedTasks).isNull() + } + @Test fun `a Gradle build that failed falls back to the generic setup-failure text`() = runTest { diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt index b46b121d99..1ce6bd7b00 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt @@ -133,6 +133,44 @@ internal class FakeProjectManager( */ internal fun unsyncedProjectManager(projectRoot: File): FakeProjectManager = FakeProjectManager(projectRoot, workspace = null) +/** + * A project model that has no workspace and no modules until [published] is set, when it answers + * as [synced] does: a tap during the sync, and the sync finishing while Quick Build works. + */ +internal class SyncingProjectManager( + private val synced: FakeProjectManager, +) : IProjectManager by synced { + var published = false + + override val workspace: Workspace? + get() = if (published) synced.workspace else null + + override fun getAndroidModules(): List = if (published) synced.getAndroidModules() else emptyList() + + override fun getAndroidAppModules(): List = if (published) synced.getAndroidAppModules() else emptyList() +} + +/** + * A project whose model is published from the start, and replaced by a sync that runs while Quick Build stages: + * [before]'s modules until [resynced] is set, [synced]'s after. + */ +internal class ResyncingProjectManager( + private val before: FakeProjectManager, + private val synced: FakeProjectManager, +) : IProjectManager by synced { + var resynced = false + + private val current: FakeProjectManager + get() = if (resynced) synced else before + + override val workspace: Workspace? + get() = current.workspace + + override fun getAndroidModules(): List = current.getAndroidModules() + + override fun getAndroidAppModules(): List = current.getAndroidAppModules() +} + /** * An Android module as the Build Variants sidebar presents it. * @@ -144,15 +182,19 @@ internal fun unsyncedProjectManager(projectRoot: File): FakeProjectManager = Fak * @param variantName the selected variant, or null for the mid-sync read the provisioner * falls back to the default variant on. * @param buildDir the module's build directory as the synced model reports it. + * @param projectDir the module's directory, where the SQLite debugger opt-in check reads its + * `gradle.properties`. */ internal fun androidModule( path: String = ":app", variantName: String? = "debug", buildDir: File = File("/fake-project/app/build"), + projectDir: File = File("/fake-project/app"), ): AndroidModule = mockk().also { module -> every { module.path } returns path every { module.buildDirPath } returns buildDir.path + every { module.projectDirPath } returns projectDir.path every { module.getSelectedVariant() } returns variantName?.let { AndroidModels.AndroidVariant @@ -257,7 +299,10 @@ internal fun testProvisioner( projectRoot: File, buildService: BuildService? = FakeBuildService(), projectManager: IProjectManager = - FakeProjectManager(projectRoot, appModules = listOf(androidModule(buildDir = File(projectRoot, "app/build")))), + FakeProjectManager( + projectRoot, + appModules = listOf(androidModule(buildDir = File(projectRoot, "app/build"), projectDir = File(projectRoot, "app"))), + ), installer: ProxyAppInstaller = recordingInstaller(), packages: InstalledPackages = FakePackages(), apkCertSha256: (File) -> String? = { null }, diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt index 0c3af40b4b..9a6ab5b9ee 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt @@ -76,4 +76,80 @@ class QuickBuildProjectSupportTest { assertThat(QuickBuildProjectSupport.nonDebuggableVariantMessage("staging")).isNull() assertThat(QuickBuildProjectSupport.nonDebuggableVariantMessage("demoStaging")).isNull() } + + @Test + fun `gradle properties decide the opt-in as the Gradle plugin does`() { + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("org.gradle.jvmargs=-Xmx2g\ncogo.sqliteDebugger=true\n")).isTrue() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("cogo.sqliteDebugger = true")).isTrue() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("#cogo.sqliteDebugger=true")).isFalse() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("cogo.sqliteDebugger=false")).isFalse() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("cogo.sqliteDebugger=TRUE")).isFalse() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("cogo.sqliteDebuggerX=true")).isFalse() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("")).isFalse() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(null as String?)).isFalse() + } + + private fun withDirs(block: (root: java.io.File, home: java.io.File, module: java.io.File) -> Unit) { + val base = + java.nio.file.Files + .createTempDirectory("qb-sqlite") + .toFile() + try { + block( + java.io.File(base, "root").apply { mkdirs() }, + java.io.File(base, "home").apply { mkdirs() }, + java.io.File(base, "root/app").apply { mkdirs() }, + ) + } finally { + base.deleteRecursively() + } + } + + private fun java.io.File.props(text: String) = java.io.File(this, "gradle.properties").writeText(text) + + @Test + fun `the root gradle properties file is read, a missing one is not an opt-in`() = + withDirs { root, home, _ -> + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(root, home)).isFalse() + root.props("cogo.sqliteDebugger=true\n") + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(root, home)).isTrue() + } + + @Test + fun `the Gradle user home gradle properties opts in`() = + withDirs { root, home, module -> + home.props("cogo.sqliteDebugger=true\n") + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(root, home, listOf(module))).isTrue() + } + + @Test + fun `the app module gradle properties opts in`() = + withDirs { root, home, module -> + module.props("cogo.sqliteDebugger=true\n") + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(root, home, listOf(module))).isTrue() + } + + @Test + fun `false in a higher-priority file overrides true in the root`() = + withDirs { root, home, module -> + root.props("cogo.sqliteDebugger=true\n") + home.props("cogo.sqliteDebugger=false\n") + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(root, home, listOf(module))).isFalse() + } + + @Test + fun `an unreadable file is logged and treated as not set`() = + withDirs { root, home, _ -> + home.props("cogo.sqliteDebugger=true\n") + val failures = mutableListOf() + val enabled = + QuickBuildProjectSupport.isSqliteDebuggerEnabled( + root, + home, + onReadFailure = { file, _ -> failures += file }, + readText = { throw java.io.IOException("denied") }, + ) + assertThat(enabled).isFalse() + assertThat(failures).containsExactly(java.io.File(home, "gradle.properties")) + } } diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt index 1bd4a2acc3..68f104fafb 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt @@ -4,6 +4,7 @@ import com.google.common.truth.Truth.assertThat import com.itsaky.androidide.resources.R import org.appdevforall.cotg.quickbuild.domain.classify.InvalidationReason import org.appdevforall.cotg.quickbuild.domain.reload.BuildDiagnostic +import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildMessage import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildSessionState import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildStatus import org.appdevforall.cotg.quickbuild.domain.session.SessionEvent @@ -278,6 +279,38 @@ class QuickBuildStatusBarTest { .isEqualTo(QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_start_failed)) } + @Test + fun `a refused start says Quick Build is off, not that setup failed, and a real failure still does`() { + val reducer = SessionReducer() + val refused = + reducer + .reduce( + QuickBuildSessionState.Provisioning(), + SessionEvent.ProvisioningFailed(QuickBuildMessage.DisabledBySqliteDebugger), + ).state + val failed = + reducer + .reduce( + QuickBuildSessionState.Provisioning(), + SessionEvent.ProvisioningFailed(QuickBuildMessage.RebuildFailed), + ).state + + // The line says "tap for details", so it carries the tap; QuickBuildStatusLineTapTest clicks it. + val off = + QuickBuildStatusBarUpdate.Show( + R.string.quick_build_status_off_sqlite_debugger, + tapLabel = R.string.quick_build_status_off_sqlite_debugger_action, + ) + assertThat(update(QuickBuildStatus.Provisioning(), QuickBuildStatus.from(refused))).isEqualTo(off) + // A recreation resubscribes with no previous status and must read the same. + assertThat(update(null, QuickBuildStatus.from(refused))).isEqualTo(off) + assertThat(update(QuickBuildStatus.Provisioning(), QuickBuildStatus.from(failed))) + .isEqualTo(QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_start_failed)) + // A save clears the carried refusal as it does a failure. + assertThat(QuickBuildStatus.from(reducer.reduce(refused, SessionEvent.FileSaved).state)) + .isEqualTo(QuickBuildStatus.Hidden()) + } + @Test fun `closing the project ends the failed-start story, so the next one keeps its own line`() { // The bar cannot tell a recreation - where the failed-start line must come back, pinned diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusLineTapTest.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusLineTapTest.kt new file mode 100644 index 0000000000..ee210b7a4f --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusLineTapTest.kt @@ -0,0 +1,127 @@ +package com.itsaky.androidide.quickbuild + +import android.content.Context +import android.view.LayoutInflater +import android.view.View.MeasureSpec +import android.view.accessibility.AccessibilityNodeInfo +import androidx.appcompat.view.ContextThemeWrapper +import androidx.test.core.app.ApplicationProvider +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.R +import com.itsaky.androidide.databinding.LayoutEditorBuildStatusBinding +import com.itsaky.androidide.ui.bindStatusLineTap +import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildMessage +import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildSessionState +import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildStatus +import org.appdevforall.cotg.quickbuild.domain.session.SessionEvent +import org.appdevforall.cotg.quickbuild.domain.session.SessionReducer +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import com.itsaky.androidide.resources.R as Res + +/** + * The Quick Build status line takes the tap its text asks for (F221). + * + * While the SQLite debugger is on, the line reads "Quick Build: off while the SQLite debugger is + * on - tap for details", and tapping it did nothing: only the toolbar button showed the refusal. + * A tap on the line must now run the button's tap, which shows the refusal again. + * + * The editor activity is far beyond a JVM test, so this drives the parts it joins: the real + * status mapping, the real status-line layout, and the binder the bottom sheet uses, applied the + * way ProjectHandlerActivity applies them (every write drops the tap, then a Show that asks for one + * binds it). The tap callback stands in for the activity's run of the toolbar action. + */ +@RunWith(RobolectricTestRunner::class) +class QuickBuildStatusLineTapTest { + private val context = ApplicationProvider.getApplicationContext() + + /** The real status block, inflated against the app's theme, which its text colour needs. */ + private val line = + LayoutEditorBuildStatusBinding.inflate( + LayoutInflater.from(ContextThemeWrapper(context, R.style.Theme_AndroidIDE)), + ) + + private var quickBuildTaps = 0 + + private val refused = + QuickBuildStatus.from( + SessionReducer() + .reduce( + QuickBuildSessionState.Provisioning(), + SessionEvent.ProvisioningFailed(QuickBuildMessage.DisabledBySqliteDebugger), + ).state, + ) + + private fun show(update: QuickBuildStatusBarUpdate?) { + val show = update as QuickBuildStatusBarUpdate.Show + bindStatusLineTap(line.statusText, null, null) + line.statusText.text = context.getString(show.text, *show.args.toTypedArray()) + show.tapLabel?.let { label -> + bindStatusLineTap(line.statusText, context.getString(label)) { quickBuildTaps++ } + } + } + + private fun clickAction(): AccessibilityNodeInfo.AccessibilityAction? = + line.statusText + .createAccessibilityNodeInfo() + .actionList + .singleOrNull { it.id == AccessibilityNodeInfo.ACTION_CLICK } + + private fun measuredHeight(): Int { + line.root.measure( + MeasureSpec.makeMeasureSpec(WIDTH_PX, MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), + ) + return line.statusText.measuredHeight + } + + @Test + fun `a tap on the refused line runs the Quick Build button's tap`() { + show(quickBuildStatusBarUpdate(QuickBuildStatus.Provisioning(), refused)) + assertThat(line.statusText.text.toString()) + .isEqualTo(context.getString(Res.string.quick_build_status_off_sqlite_debugger)) + + assertThat(line.statusText.performClick()).isTrue() + + assertThat(quickBuildTaps).isEqualTo(1) + } + + @Test + fun `the refused line is a labelled button for TalkBack and the keyboard`() { + show(quickBuildStatusBarUpdate(null, refused)) + + assertThat(line.statusText.isClickable).isTrue() + assertThat(line.statusText.isFocusable).isTrue() + assertThat(clickAction()?.label?.toString()) + .isEqualTo(context.getString(Res.string.quick_build_status_off_sqlite_debugger_action)) + // TalkBack's double-tap is an accessibility click, not a touch. + assertThat(line.statusText.performAccessibilityAction(AccessibilityNodeInfo.ACTION_CLICK, null)).isTrue() + assertThat(quickBuildTaps).isEqualTo(1) + } + + @Test + fun `the tap leaves the line's size alone`() { + show(quickBuildStatusBarUpdate(QuickBuildStatus.Provisioning(), refused)) + val withTap = measuredHeight() + bindStatusLineTap(line.statusText, null, null) + + assertThat(withTap).isEqualTo(measuredHeight()) + } + + @Test + fun `the next line is plain text again, so a tap on it reaches the header`() { + show(quickBuildStatusBarUpdate(QuickBuildStatus.Provisioning(), refused)) + show(quickBuildStatusBarUpdate(refused, QuickBuildStatus.Provisioning())) + + assertThat(line.statusText.isClickable).isFalse() + assertThat(line.statusText.isFocusable).isFalse() + assertThat(clickAction()).isNull() + assertThat(line.statusText.performClick()).isFalse() + assertThat(quickBuildTaps).isEqualTo(0) + } + + private companion object { + const val WIDTH_PX = 600 + } +} diff --git a/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java b/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java index 3977d3fc94..714bc4a16a 100755 --- a/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java +++ b/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java @@ -129,6 +129,8 @@ public static void init(@NonNull BaseApplication app, Runnable onFinish) { ensureToolingJar(app); extractLogSender(app); + extractSqliteDebugger(); + extractSqliteRewriteRuntime(); writeNoMediaFile(); extractCogoPlugin(); @@ -208,6 +210,28 @@ static boolean isToolingJarCurrent(File toolingJarFile, File stampFile, String s return toolingJarFile.isFile() && stamp != null && stamp.equals(readStampFile(stampFile)); } + /** + * Copies the stream to a temp sibling of the target, syncs it, then renames it over the target. {@link #init} extracts the SQLite AARs in the background, and GradleBuildService passes their paths to a build without waiting, so a build that starts meanwhile must find the previous complete file or the new one, never a missing or partly written one. rename(2) within one directory atomically replaces the target on Linux; syncing first means a power loss after the rename cannot leave the target empty. The temp file is deleted when any step fails. Its name is fixed, not unique: init runs once per process, and the next run overwrites what a killed process left. Does not close the stream. + */ + @VisibleForTesting + static void replaceAtomically(InputStream source, File target) throws IOException { + final var dir = Objects.requireNonNull(target.getParentFile()); + dir.mkdirs(); + final var tempFile = new File(dir, target.getName() + ".part"); + try { + try (final var fos = new FileOutputStream(tempFile)) { + IoUtilsKt.transferToStream(source, fos); + fos.getFD().sync(); + } + if (!tempFile.renameTo(target)) { + throw new IOException("Could not move " + tempFile + " to " + target); + } + } finally { + // gone after a successful rename; a leftover of a failed copy is what this removes + tempFile.delete(); + } + } + private static void deleteIdeenv() { final var file = new File(Environment.BIN_DIR, "ideenv"); if (file.exists() && !file.delete()) { @@ -261,6 +285,26 @@ private static void extractLogSender(BaseApplication app) { Environment.LOGSENDER_AAR.getAbsolutePath()); } + private static void extractSqliteAar(String asset, File aar) { + try (InputStream source = BaseApplication.getBaseInstance().getAssets().open(asset)) { + replaceAtomically(source, aar); + } catch (IOException err) { + LOG.error("Failed to extract {} to {}", asset, aar, err); + } + } + + @WorkerThread + private static void extractSqliteDebugger() { + // One AAR with both ABIs, unlike logsender's per-flavor AARs + extractSqliteAar(getCommonAsset("sqlite-debugger-release.aar"), Environment.SQLITE_DEBUGGER_AAR); + } + + @WorkerThread + private static void extractSqliteRewriteRuntime() { + // Java only, so one AAR for both ABIs + extractSqliteAar(getCommonAsset("sqlite-rewrite-runtime-release.aar"), Environment.SQLITE_REWRITE_RUNTIME_AAR); + } + private static void generateKeystore() { try { String keystorePath = Environment.KEYSTORE_RELEASE.getPath(); diff --git a/common/src/main/java/com/itsaky/androidide/utils/Environment.java b/common/src/main/java/com/itsaky/androidide/utils/Environment.java index ce5e1f0db9..44c1585bed 100755 --- a/common/src/main/java/com/itsaky/androidide/utils/Environment.java +++ b/common/src/main/java/com/itsaky/androidide/utils/Environment.java @@ -18,6 +18,8 @@ import static org.adfa.constants.ConstantsKt.GRADLE_DISTRIBUTION_VERSION; import static org.adfa.constants.ConstantsKt.LOGSENDER_AAR_NAME; +import static org.adfa.constants.ConstantsKt.SQLITE_DEBUGGER_AAR_NAME; +import static org.adfa.constants.ConstantsKt.SQLITE_REWRITE_RUNTIME_AAR_NAME; import android.annotation.SuppressLint; import android.content.Context; @@ -66,6 +68,10 @@ public final class Environment { public static File OPT_DIR; public static File LOGSENDER_DIR; public static File LOGSENDER_AAR; + public static File SQLITE_DEBUGGER_DIR; + public static File SQLITE_DEBUGGER_AAR; + public static File SQLITE_REWRITE_RUNTIME_DIR; + public static File SQLITE_REWRITE_RUNTIME_AAR; public static File PROJECTS_DIR; // split assets vars @@ -154,6 +160,10 @@ public static void init(Context context) { OPT_DIR = mkdirIfNotExists(new File(PREFIX, "opt")); LOGSENDER_DIR = mkdirIfNotExists(new File(ANDROIDIDE_HOME, "logsender")); LOGSENDER_AAR = mkdirIfNotExists(new File(LOGSENDER_DIR, LOGSENDER_AAR_NAME)); + SQLITE_DEBUGGER_DIR = mkdirIfNotExists(new File(ANDROIDIDE_HOME, "sqlite-debugger")); + SQLITE_DEBUGGER_AAR = new File(SQLITE_DEBUGGER_DIR, SQLITE_DEBUGGER_AAR_NAME); + SQLITE_REWRITE_RUNTIME_DIR = mkdirIfNotExists(new File(ANDROIDIDE_HOME, "sqlite-rewrite-runtime")); + SQLITE_REWRITE_RUNTIME_AAR = new File(SQLITE_REWRITE_RUNTIME_DIR, SQLITE_REWRITE_RUNTIME_AAR_NAME); PROJECTS_DIR = mkdirIfNotExists(new File(FileUtil.getExternalStorageDir(), PROJECTS_FOLDER)); // NOTE: change location of android.jar from ANDROIDIDE_HOME to inside android-sdk // and don't create the dir if it doesn't exist diff --git a/common/src/test/java/com/itsaky/androidide/managers/ToolsManagerSqliteAarTest.kt b/common/src/test/java/com/itsaky/androidide/managers/ToolsManagerSqliteAarTest.kt new file mode 100644 index 0000000000..3d95db8389 --- /dev/null +++ b/common/src/test/java/com/itsaky/androidide/managers/ToolsManagerSqliteAarTest.kt @@ -0,0 +1,87 @@ +package com.itsaky.androidide.managers + +import com.google.common.truth.Truth.assertThat +import org.junit.Assert.assertThrows +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File +import java.io.IOException +import java.io.InputStream + +/** + * App init extracts the SQLite debugger and rewrite runtime AARs in the background, and a build gets their paths + * without waiting, so [ToolsManager.replaceAtomically] must never leave a missing or partly written AAR at the path. + */ +class ToolsManagerSqliteAarTest { + @get:Rule + val tempFolder = TemporaryFolder() + + /** Hands out one byte per read, and records what a build reading [target] would find before each one. */ + private class WatchingStream( + private val bytes: ByteArray, + private val target: File, + ) : InputStream() { + val seen = mutableListOf() + private var next = 0 + + override fun read(): Int { + seen += if (target.isFile) target.readText() else null + return if (next < bytes.size) bytes[next++].toInt() and 0xff else -1 + } + + override fun read( + b: ByteArray, + off: Int, + len: Int, + ): Int { + if (len == 0) return 0 + val c = read() + if (c < 0) return -1 + b[off] = c.toByte() + return 1 + } + } + + @Test + fun `a build that reads the AAR while it is replaced finds the previous complete file`() { + val aar = File(tempFolder.root, "sqlite-debugger/sqlite-debugger.aar") + aar.parentFile!!.mkdirs() + aar.writeText("previous-install-aar") + val source = WatchingStream("this-install-aar".toByteArray(), aar) + + ToolsManager.replaceAtomically(source, aar) + + assertThat(source.seen.toSet()).containsExactly("previous-install-aar") + assertThat(aar.readText()).isEqualTo("this-install-aar") + assertThat(File(aar.parentFile, "sqlite-debugger.aar.part").exists()).isFalse() + } + + @Test + fun `a copy that fails leaves the previous AAR and no temp file`() { + val aar = File(tempFolder.root, "sqlite-debugger/sqlite-debugger.aar") + aar.parentFile!!.mkdirs() + aar.writeText("previous-install-aar") + val failing = + object : InputStream() { + private var left = 4 + + override fun read(): Int = if (left-- > 0) 'x'.code else throw IOException("asset read failed") + } + + val e = assertThrows(IOException::class.java) { ToolsManager.replaceAtomically(failing, aar) } + + assertThat(e).hasMessageThat().isEqualTo("asset read failed") + assertThat(aar.readText()).isEqualTo("previous-install-aar") + assertThat(aar.parentFile!!.list()).asList().containsExactly("sqlite-debugger.aar") + } + + @Test + fun `the first extraction creates the directory and the AAR`() { + val aar = File(tempFolder.root, "sqlite-rewrite-runtime/sqlite-rewrite-runtime.aar") + + ToolsManager.replaceAtomically("runtime-aar".byteInputStream(), aar) + + assertThat(aar.readText()).isEqualTo("runtime-aar") + } +} diff --git a/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt b/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt index cf9d02b859..88d6c608a0 100644 --- a/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt +++ b/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt @@ -83,6 +83,13 @@ const val DOCUMENTATION_DB = "documentation.db" const val LOGSENDER_AAR_NAME = "logsender.aar" +// The SQLite debugger AAR (ADFA-7131), injected into debug builds of apps built with CoGo +const val SQLITE_DEBUGGER_AAR_NAME = "sqlite-debugger.aar" + +// The helpers the build-time android.database.sqlite -> requery rewrite redirects to (ADFA-7138), +// injected into every variant of apps built with CoGo that opted in +const val SQLITE_REWRITE_RUNTIME_AAR_NAME = "sqlite-rewrite-runtime.aar" + // Generated Gradle Api Jar const val GRADLE_API_NAME_JAR = "gradle-api-$GRADLE_DISTRIBUTION_VERSION.jar" const val GRADLE_API_NAME_JAR_ZIP = "${GRADLE_API_NAME_JAR}.zip" diff --git a/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt b/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt index fa69885a18..28dad8c713 100644 --- a/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt +++ b/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt @@ -71,6 +71,12 @@ class AndroidIDEAssetsPlugin : Plugin { // Add logsender.aar registerLogSenderAarCopierTask(variant, variantNameCapitalized) + // Add sqlite-debugger-release.aar + registerSqliteDebuggerAarCopierTask(variant, variantNameCapitalized) + + // Add sqlite-rewrite-runtime-release.aar + registerSqliteRewriteRuntimeAarCopierTask(variant, variantNameCapitalized) + // Add plugin-artifacts.zip registerPluginArtifactsCopierTask(variant, variantNameCapitalized) @@ -216,6 +222,39 @@ class AndroidIDEAssetsPlugin : Plugin { } } + /** + * The debugger module has no v7/v8 flavors: one AAR carries both ABIs, so every variant bundles + * the same file. The release build type, as for logsender. + */ + private fun Project.registerSqliteDebuggerAarCopierTask( + variant: Variant, + variantName: String, + ) { + addProjectArtifactToAssets( + variant = variant, + taskName = "copy${variantName}SqliteDebuggerAar", + projectPath = ":subprojects:sqlite-debugger", + projectTask = "assembleRelease", + ) { project -> + project.layout.buildDirectory.file("outputs/aar/sqlite-debugger-release.aar") + } + } + + /** Java only, no v7/v8 flavors: every variant bundles the same file. The release build type, as above. */ + private fun Project.registerSqliteRewriteRuntimeAarCopierTask( + variant: Variant, + variantName: String, + ) { + addProjectArtifactToAssets( + variant = variant, + taskName = "copy${variantName}SqliteRewriteRuntimeAar", + projectPath = ":subprojects:sqlite-rewrite-runtime", + projectTask = "assembleRelease", + ) { project -> + project.layout.buildDirectory.file("outputs/aar/sqlite-rewrite-runtime-release.aar") + } + } + private inline fun Project.addProjectArtifactToAssets( variant: Variant, taskName: String, diff --git a/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/conf/AndroidModuleConf.kt b/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/conf/AndroidModuleConf.kt index e418cb787a..3330baec4d 100644 --- a/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/conf/AndroidModuleConf.kt +++ b/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/conf/AndroidModuleConf.kt @@ -55,15 +55,24 @@ private val disableCoreLibDesugaringForModules = ":subprojects:sqlite-js", // Injected into debug builds of apps built with CoGo like :logsender (ADFA-7131). ":subprojects:sqlite-debugger", + // Injected into every variant of apps built with CoGo that opted into the rewrite, for the + // rewritten classes to call (ADFA-7138). + ":subprojects:sqlite-rewrite-runtime", ) /** * Modules without the v7/v8 flavors: libraries for other projects, which need one artifact. * :subprojects:sqlite-js and :subprojects:sqlite-debugger are AARs for apps built with CoGo, with - * all the ABIs they run on. + * all the ABIs they run on. :subprojects:sqlite-rewrite-runtime is Java-only, one AAR for apps + * built with CoGo. */ private val modulesWithoutAbiFlavors = - setOf(":plugin-api", ":subprojects:sqlite-js", ":subprojects:sqlite-debugger") + setOf( + ":plugin-api", + ":subprojects:sqlite-js", + ":subprojects:sqlite-debugger", + ":subprojects:sqlite-rewrite-runtime", + ) /** * The name of the build type used for automated testing. diff --git a/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java b/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java index 4bd6180f1e..705f174240 100644 --- a/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java +++ b/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java @@ -64,6 +64,21 @@ public final class GradlePluginConfig { */ public static final String PROPERTY_LOG_SENDER_AAR = "androidide.logsender.aar"; + /** + * Property, normally in the project's gradle.properties, that opts the project into the SQLite debugger (ADFA-7131). The debugger is added to debuggable variants only, and only when the value is true. + */ + public static final String PROPERTY_SQLITE_DEBUGGER_ENABLED = "cogo.sqliteDebugger"; + + /** + * The path to the SQLite debugger AAR file, passed by Code on the Go like {@link #PROPERTY_LOG_SENDER_AAR}. + */ + public static final String PROPERTY_SQLITE_DEBUGGER_AAR = "androidide.sqliteDebugger.aar"; + + /** + * The path to the AAR with the helpers the SQLite rewrite redirects to (ADFA-7138), passed by Code on the Go like {@link #PROPERTY_SQLITE_DEBUGGER_AAR}. + */ + public static final String PROPERTY_SQLITE_REWRITE_RUNTIME_AAR = "androidide.sqliteRewriteRuntime.aar"; + /** * Property that is set in tests to indicate that the plugin is being applied in a test environment. *

diff --git a/gradle-plugin/build.gradle.kts b/gradle-plugin/build.gradle.kts index 06560adcf6..f8f9159f7e 100644 --- a/gradle-plugin/build.gradle.kts +++ b/gradle-plugin/build.gradle.kts @@ -35,9 +35,7 @@ description = "Gradle Plugin for projects that are built with AndroidIDE" // projectsEvaluated sweep silently misses projects under configure-on-demand. val mavenLocalStagingProjects = listOf(":logsender", ":logger", ":build-info") -tasks.named("test") { - useJUnitPlatform() - +fun Test.stageMavenLocalRepos() { val stagedRepos = mavenLocalStagingProjects.map { path -> dependsOn("$path:publishAllPublicationsToBuildMavenLocalRepository") @@ -65,6 +63,75 @@ tasks.named("test") { } } +// The SQLite rewrite tests find the jars they compile against in the cache of this build's Gradle user home, which +// GRADLE_USER_HOME or --gradle-user-home can move from ~/.gradle. An argument, not an input: the path is per machine. +val gradleUserHomePath: String = gradle.gradleUserHomeDir.absolutePath + +fun Test.passGradleUserHome() { + jvmArgumentProviders.add(CommandLineArgumentProvider { listOf("-Dcogo.test.gradleUserHome=$gradleUserHomePath") }) +} + +val sqliteReleaseApkTestClass = "com.itsaky.androidide.gradle.SqliteReleaseApkTest" + +tasks.named("test") { + useJUnitPlatform() + stageMavenLocalRepos() + passGradleUserHome() + // Run by sqliteReleaseApkTest below, which builds the AARs it needs + filter { excludeTestsMatching(sqliteReleaseApkTestClass) } +} + +// The release-APK guard (ADFA-7138): SqliteReleaseApkTest builds the minified release and the debug variant +// of a sample app that opted into the SQLite debugger, with the real debugger, rewrite runtime and requery +// AARs, and runs scripts/check-release-apk-no-sqlite-debugger.sh on both APKs. A task of its own because it +// needs the NDK-built debugger AAR, which the other tests do not; CI's build_apk job runs it on every push. +val requeryAar = + configurations.create("sqliteReleaseApkTestRequery") { + isCanBeConsumed = false + isTransitive = false + } + +dependencies { + add(requeryAar.name, variantOf(libs.tests.requery.sqliteAndroid) { artifactType("aar") }) +} + +// Under configure-on-demand the AAR projects are not configured yet when the task below reads their build +// directories, as in :app +val sqliteReleaseApkTestAarProjects = + mapOf( + "debugger" to ":subprojects:sqlite-debugger", + "rewriteRuntime" to ":subprojects:sqlite-rewrite-runtime", + ) +sqliteReleaseApkTestAarProjects.values.forEach(::evaluationDependsOn) + +tasks.register("sqliteReleaseApkTest") { + description = "Builds an opted-in sample app's release APK and checks that it carries no SQLite debugger" + group = "verification" + val testSourceSet = sourceSets["test"] + testClassesDirs = testSourceSet.output.classesDirs + classpath = testSourceSet.runtimeClasspath + useJUnitPlatform() + stageMavenLocalRepos() + passGradleUserHome() + filter { includeTestsMatching(sqliteReleaseApkTestClass) } + + // The tasks AndroidIDEAssetsPlugin bundles these AARs with, so this task and :app:assemble build them once + for ((name, path) in sqliteReleaseApkTestAarProjects) { + dependsOn("$path:assembleRelease") + val aar = + project(path) + .layout.buildDirectory + .file("outputs/aar/${path.substringAfterLast(':')}-release.aar") + .get() + .asFile + inputs.file(aar).withPropertyName("${name}Aar") + systemProperty("cogo.test.sqlite.${name}Aar", aar.absolutePath) + } + inputs.files(requeryAar).withPropertyName("requeryAar") + inputs.file(rootProject.file("scripts/check-release-apk-no-sqlite-debugger.sh")).withPropertyName("checkScript") + jvmArgumentProviders.add(CommandLineArgumentProvider { listOf("-Dcogo.test.sqlite.requeryAar=${requeryAar.singleFile.absolutePath}") }) +} + configurations { val androidBuildTool = create("androidBuildTool") @@ -93,10 +160,21 @@ dependencies { // claim the minAgpCheck guard below keeps honest by recompiling them against it. add("androidBuildTool", libs.android.gradle.plugin) + // The SQLite rewrite (ADFA-7138). AGP's runtime classpath carries ASM, but its API + // artifacts expose only asm itself, so the rewrite compiles against the rest of it here. + compileOnly(libs.ow2.asm) + compileOnly(libs.ow2.asmCommons) + compileOnly(libs.ow2.asmTree) + compileOnly(libs.ow2.asmAnalysis) + testImplementation(gradleTestKit()) testImplementation(libs.tests.junit.jupiter) testImplementation(libs.tests.google.truth) testImplementation(projects.shared) + testImplementation(libs.ow2.asm) + testImplementation(libs.ow2.asmCommons) + testImplementation(libs.ow2.asmTree) + testImplementation(libs.ow2.asmAnalysis) testRuntimeOnly(libs.tests.junit.platformLauncher) } @@ -125,6 +203,10 @@ dependencies { "minAgpCheckImplementation"(libs.composite.constants) "minAgpCheckImplementation"(projects.gradlePluginConfig) "minAgpCheckImplementation"(projects.buildInfo) + "minAgpCheckCompileOnly"(libs.ow2.asm) + "minAgpCheckCompileOnly"(libs.ow2.asmCommons) + "minAgpCheckCompileOnly"(libs.ow2.asmTree) + "minAgpCheckCompileOnly"(libs.ow2.asmAnalysis) } tasks.named("check") { @@ -160,6 +242,15 @@ gradlePlugin { "Gradle plugin for applying LogSender-specific configuration to projects that are built with AndroidIDE" tags.set(setOf("androidide", "logsender")) } + + create("sqliteDebuggerPlugin") { + id = "${BuildConfig.PACKAGE_NAME}.sqlitedebugger" + implementationClass = "${BuildConfig.PACKAGE_NAME}.gradle.SqliteDebuggerPlugin" + displayName = "AndroidIDE SQLite Debugger Gradle Plugin" + description = + "Gradle plugin for adding the SQLite debugger to debuggable variants of projects that are built with AndroidIDE" + tags.set(setOf("androidide", "sqlite")) + } } } diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md new file mode 100644 index 0000000000..c44aa062fe --- /dev/null +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -0,0 +1,253 @@ +# SQLite rewrite (ADFA-7138) + +When `cogo.sqliteDebugger=true`, the Gradle plugin registers an AGP class transform +(`com.itsaky.androidide.gradle.sqlite.SqliteRewriteVisitorFactory`, scope ALL, every variant) that moves +the app's and its libraries' classes from `android.database.sqlite` to requery 3.49.0, so debug and release +run the same SQLite. User source does not change. + +## Turning it on + +An existing project needs: + +1. `cogo.sqliteDebugger=true` in `gradle.properties`. The plugin reads it with Gradle's `findProperty`, so the + project's root file, the app module's own `gradle.properties` and the Gradle user home's + (`GRADLE_USER_HOME`, `~/.gradle` under Code on the Go's home) all work, the user home first. The IDE's Quick + Build check (`QuickBuildProjectSupport.isSqliteDebuggerEnabled`) reads those same three files, in that order, + and logs a file it cannot read; it does not see `-P` arguments, which Code on the Go does not pass for this. +2. requery's AAR (`sqlite-android.aar`) in the app's `libs/`, as a file dependency + (`implementation(files("libs/sqlite-android.aar"))`), as the ADFA-7082 template does. +3. androidx.sqlite and androidx.core on the runtime classpath (`androidx.sqlite:sqlite`, which Room, WorkManager + and most androidx libraries bring, and `androidx.core:core`, which AppCompat brings). A file dependency brings + none of the dependencies of the AAR: requery implements androidx.sqlite's interfaces, takes and calls + androidx.core's `CancellationSignal` (the rewrite converts android's signal to it) and throws its + `OperationCanceledException`, and its statement cache extends androidx.collection's `LruCache`, which + androidx.core brings. + +The helper classes the rewrite redirects to (`SqliteRewriteRuntime`) are not in `libs/`: Code on the Go bundles +`subprojects/sqlite-rewrite-runtime`'s release AAR, extracts it with the other tools and passes its path as +`androidide.sqliteRewriteRuntime.aar`, as it does for the debugger AAR. `SqliteRewritePlugin` adds it to the +runtime classpath of every variant of a project that opted in, and fails the build when the file is missing or +when a variant's runtime classpath cannot be reached (a variant type the plugin does not know, named in the message): +the rewrite is already registered for it, so going on would give an app that throws `NoClassDefFoundError`. +`SqliteDebuggerPlugin` fails the same way for the variants it adds the debugger to. + +A task `checkSqliteRewriteClasspath` runs before the compile and the transform of each variant. It fails +with an actionable message when the variant's runtime classpath has no +`io/requery/android/database/sqlite/SQLiteDatabase.class`, `androidx/sqlite/db/SupportSQLiteDatabase.class` or +`androidx/core/os/CancellationSignal.class`. + +The property is required because requery reaches apps only through `libs/`. Without it the rewritten classes +cannot load, so the transform runs only on projects that opted in. Commenting the line out turns it off. + +## Rules + +- Rename: the 10 classes requery shares (`SQLiteClosable`, `SQLiteCursor`, `SQLiteCursorDriver`, + `SQLiteDatabase`, `SQLiteDatabase$CursorFactory`, `SQLiteOpenHelper`, `SQLiteProgram`, `SQLiteQuery`, + `SQLiteQueryBuilder`, `SQLiteStatement`) and `android.database.DatabaseErrorHandler` and + `DefaultDatabaseErrorHandler`. androidx's `FrameworkSQLiteOpenHelperFactory` is not renamed; see the design + change below. Kept: the exceptions, `SQLiteTransactionListener`, `android.database.Cursor`, `CursorWindow`. + Overrides count: a `SQLiteOpenHelper` subclass or a `SQLiteCursorDriver`/`CursorFactory` implementation + ends up with requery's descriptors. +- Descriptor: `String[]` becomes `Object[]` (selectionArgs) for `query`, `queryWithFactory`, `rawQuery`, + `rawQueryWithFactory` and `SQLiteCursorDriver.query`, in calls and in the override declaration. +- Redirect to `org.appdevforall.sqliterewrite.SqliteRewriteRuntime`: `SQLiteDatabase.openDatabase` with + flags (Android's flag values differ from requery's; the flags are compiled into the app), + `Context.openOrCreateDatabase` / `deleteDatabase` (INVOKEVIRTUAL on `Context` or a subclass; subclasses + are found with AGP's class loader, plus a fixed list of the framework ones), and an + `android.os.CancellationSignal` argument of a requery-bound call, converted right before the call (it + is always the last argument), and `android.database.DatabaseUtils.queryNumEntries`, `longForQuery` and + `stringForQuery` (every overload; requery has no `DatabaseUtils`, so the runtime carries Android's own + implementations, written against Android's types). +- Fail the build, naming class, method and line: a call to a member requery lacks (for example + `execPerConnectionSQL`, `setCustomScalarFunction`, `SQLiteQueryBuilder.isStrict`; also the deprecated cursor + members `SQLiteCursor` inherits from android's `AbstractCursor` and `AbstractWindowedCursor` that requery's + copies lack: `isBlob`, `isFloat`, `isLong`, `isString`, `getUpdatedField`, `isFieldUpdated`, + `getNotificationUris`, `setNotificationUris`, and the protected field `mWindow`; `SqliteRewriteResolutionTest` probes the inherited members of every + renamed class against requery), SQL text with `COLLATE UNICODE` (any case or whitespace, in any string + constant, which covers Room's generated `_Impl` classes: requery's native SQLite has no ICU, so the collation + does not exist), any use of + `SQLiteDatabase.OpenParams`, a call that passes android's `CursorWindow` to a renamed class (calls on + `CursorWindow` itself are fine), and a cast of + an SQLite cursor to `android.database.AbstractWindowedCursor`, `AbstractCursor` or `CrossProcessCursor` + (requery's cursors extend its own copies of the first two, and implement only `android.database.Cursor`). + The same conversion without a cast fails too: a value declared `SQLiteCursor`, or a subclass, returned, passed, + called on, stored in a field, or held in a local as one of those three types (`AbstractCursor up(SQLiteCursor c) + { return c; }`). javac writes no `CHECKCAST` for it, and the renamed class would not verify. So does storing one + into an array of those types (`AbstractCursor[] a; a[0] = c;`), which would throw `ArrayStoreException`: the + array must be one on every path to the store, from a parameter, field, call or `new`. + A cast guarded by `instanceof` of the same type on the same local (`if (c instanceof X) ((X) c)`, `&&`, `?:`, + or an early return when it is false) is not reported when the guard holds on every path to the cast: in + `if (b || c instanceof X) ((X) c)` the cast is reached with `b` alone, so it is reported. `COLLATE LOCALIZED` only logs a warning: it works, but + compares bytes (no ICU) instead of sorting by locale. All violations of a class are reported together, with + the way out for a library class, which the user cannot change: comment out `cogo.sqliteDebugger=true`. A platform method outside the renamed set whose + descriptor mentions a renamed class (found with javap over android.jar: the other `DatabaseUtils` methods + `blobFileDescriptorForQuery` and `bindObjectToProgram`, `DatabaseUtils$InsertHelper`, `OpenParams` + accessors) cannot link once the descriptor is renamed, so a call to one that is not redirected above fails + the build. `SqliteRewriteResolutionTest` probes every such member in android.jar. + A `Context.openOrCreateDatabase` / `deleteDatabase` call that cannot be redirected also fails the build: + `super.` calls (INVOKESPECIAL), calls whose owner class cannot be resolved (it might be a Context), and + a method with one of those signatures that overrides it in a Context subclass (the redirect would skip + the override). So does an override of `SQLiteQueryBuilder.query` with a `CancellationSignal` in a subclass: its + calls get requery's descriptor while the override keeps Android's, so they would reach requery's method and skip + it. Retyping the override would mean turning requery's signal back into Android's inside its body. + Likewise a call on an owner that AGP's class loader cannot resolve fails when its name and descriptor are those of + a member a rule above changes or rejects in a renamed class another class can extend (not the final + `SQLiteDatabase`, `SQLiteQuery` or `SQLiteStatement`) and the descriptor names an SQLite-related type (any + `android.database.sqlite` or requery type, `Cursor`, `CancellationSignal`, `DatabaseErrorHandler`): the owner may + inherit that member, and its renamed ancestors are unknown. When the descriptor names none (`isStrict()Z`, + `isString(I)Z`, `setWindow(CursorWindow)`, `mWindow`), the call or field access only logs a warning with class, + method and line: libraries reference optional classes the app does not ship, and failing every app build over them + would be worse than missing the case. `CursorWindow` is not an SQLite type: it carries every ContentProvider cursor + across processes, and android's kept `AbstractWindowedCursor` declares `setWindow(CursorWindow)` and `mWindow` too, + so a library's own windowed cursor matches them. +- Method references: a handle in an invokedynamic (`db::rawQuery`, `ctx::deleteDatabase`) that any rule + above would change, redirect or reject fails the build, naming class, method and line, with the advice to + write a lambda (its body is an ordinary call, which the rules handle). The rules are not applied to the + handle itself: it cannot be redirected in place, and its descriptor is fixed by the call site. +- Frames: `COPY_FRAMES`, AGP's default. The one change that breaks them is a retyped + `SQLiteCursorDriver.query` implementation, whose `String[]` parameter becomes `Object[]` while its body, and + the full frames copied from it, still say `String[]`. For each non-abstract method whose descriptor is retyped + that way, the rewriter inserts `ALOAD n; CHECKCAST [Ljava/lang/String;; ASTORE n` at method entry, so the body + stays valid, and writes every frame of that method in full (compressed frames are relative to the implicit first + frame, which now has the wide type). A driver that is handed an `Object[]` with an element that is not a + `String` throws `ClassCastException` on entry; requery's own drivers pass strings. + The mode is per variant, but `COPY_FRAMES` cannot weaken another instrumentation in the same build (Hilt, Firebase, + an app's own `transformClassesWith`) that needs frames computed: `setAsmFramesComputationMode` keeps the highest + mode any plugin asks for (AGP 7.3.0, 8.8.2 and 9.3.1 compare the enum ordinals; checked with javap), and + `COPY_FRAMES` is the lowest. Checked once (2026-11-17, AGP 9.3.1, not a standing test) in the order a last-call-wins + rule would break: CoGo applies its plugin in `afterEvaluate`, so its `onVariants` runs after the app's. The + functional tests' sample app, opted in, got a `SQLiteOpenHelper` subclass, a `SQLiteCursorDriver` and a second + `AsmClassVisitorFactory` (scope ALL) that inserts `if (0 != 0) throw null` at the start of every method but + constructors of the app, androidx.sqlite and Room, and asks for `COMPUTE_FRAMES_FOR_INSTRUMENTED_METHODS`. + demoDebug and demoRelease assembled (D8, R8). Their transformed classes, 232 each, pass ASM's `CheckClassAdapter` + and the JVM's verifier (each class linked against android.jar and the instrumented dependency jars), except Room's + `LimitOffsetDataSource`, which cannot load because the app has no `androidx.paging`. `Driver.query` carries the + `CHECKCAST` prologue and the branch, with a computed frame at the branch target. The control, the same build + without the request for computed frames, fails the JVM's verifier (`Expecting a stackmap frame at branch + target`), so the check does see stale frames. + +Excluded: `io.requery.**`. The helper's package is renamed only (a redirect would call itself). +`androidx.sqlite.db.framework.**` and `androidx.core.database.**` are rewritten but exempt from the fail rule: see +below, and androidx.core's `SQLiteCursorCompat$Api28Impl` calls `SQLiteCursor.setFillWindowForwardOnly`, which requery +lacks, so failing the build for it would fail every app that depends on androidx.core (found by the transform test +on androidx.core 1.9.0). Calling `SQLiteCursorCompat.setFillWindowForwardOnly` on requery's cursor throws +`NoSuchMethodError`. + +## Survey: what reaches Android's SQLite (androidx.sqlite framework 2.6.2, Room 2.8.4, WorkManager 2.10.0) + +| Where | Reference | Handling | +| --- | --- | --- | +| `RoomDatabase$Builder.build`, `PrePackagedCopyOpenHelper`, WorkManager's `WorkDatabase$Companion` | `new FrameworkSQLiteOpenHelperFactory` | kept: androidx's factory runs on requery through the renamed classes (design change below) | +| `FrameworkSQLiteDatabase`, `FrameworkSQLiteOpenHelper$OpenHelper`, `FrameworkSQLiteProgram`/`Statement` | every member of the renamed classes they use | all resolve in requery after the rules; `SqliteRewriteResolutionTest` transforms the real 2.6.2 framework classes, verifies them and resolves every reference (access and static-ness included) | +| `FrameworkSQLiteDatabase$Api30Impl` | `execPerConnectionSQL` (NO in requery's own class) | the only reference to a member requery lacks; it resolves to `SupportSQLiteDatabase`'s default method, which throws `UnsupportedOperationException` | +| `FrameworkSQLiteDatabase.query(.., CancellationSignal)` | `rawQueryWithFactory` with android's signal | signal converted | +| `FrameworkSQLiteOpenHelper$OpenHelper`, `PrePackagedCopyOpenHelper` | `Context.deleteDatabase`, `getDatabasePath` | redirect (`deleteDatabase`); `getDatabasePath` needs none | +| Room `DBUtil.query` | `instanceof` and cast to `AbstractWindowedCursor`, `CursorWindow.getNumRows` | untouched: the value comes from `SupportSQLiteDatabase.query`, not an SQLite API, and the cast is guarded by `instanceof`, which is false for requery's cursor (Room skips its copy optimisation) | +| Room `checkUniquenessException` | `SQLiteConstraintException` | kept class | +| `androidx.sqlite.driver.AndroidSQLiteDriver.open` | `SQLiteDatabase.openOrCreateDatabase(String, null)` and `new FrameworkSQLiteDatabase(SQLiteDatabase)` | works only if `FrameworkSQLiteDatabase` is rewritten too | + +### Design change (2026-11-13): androidx's factory stays + +The first version renamed `new FrameworkSQLiteOpenHelperFactory()` to requery's `RequerySQLiteOpenHelperFactory` +and excluded the factory class. That is dropped. requery's factory reads only the context, the name and the +callback of the `SupportSQLiteOpenHelper.Configuration` (checked with javap): it ignores `noBackupDirectory` and +`allowDataLossOnRecovery`, and has none of the framework's cross-process lock. WorkManager 2.10 sets the first +two, so its database would be recreated empty after `WorkDatabasePathHelper.migrateDatabase` on each start. + +Instead the androidx.sqlite framework classes (`androidx.sqlite.db.framework.*`, rewritten with the fail rule +off) run on requery through the normal rename and descriptor rules: `FrameworkSQLiteOpenHelper`, its inner +`OpenHelper` (a subclass of the renamed `SQLiteOpenHelper`), `FrameworkSQLiteDatabase`, `-Statement` and +`-Program` keep their behaviour, including the no-backup directory and the recovery flags, and call requery's +classes. They are not excluded, which also keeps `androidx.sqlite.driver.AndroidSQLiteDriver` working: it builds a +`FrameworkSQLiteDatabase` from a plain `SQLiteDatabase`. + +Every reference of these classes resolves in requery. The one member requery's own `SQLiteDatabase` lacks is +`execPerConnectionSQL`, which `FrameworkSQLiteDatabase$Api30Impl` calls; it resolves to the default method of +`SupportSQLiteDatabase` (checked with javap), which throws `UnsupportedOperationException`. So +`execPerConnectionSQL` through the `SupportSQLiteDatabase` interface stays unsupported, and the fail rule is off for +this package, so the build does not report it. + +## Checking a release APK + +`scripts/check-release-apk-no-sqlite-debugger.sh ` fails when an APK carries the debugger (native library, +provider, `` entry or classes). The release build must never contain the debugger. R8 renames and drops +classes in a minified release, so the class check can miss them; the manifest and native-library checks are the +ones that hold. + +`:gradle-plugin:sqliteReleaseApkTest` (`SqliteReleaseApkTest`) runs it on every push, in the `build_apk` job of +`.github/workflows/debug.yml`. It builds the minified release and the debug variant of the functional tests' sample +app, opted in, with the debugger and rewrite runtime AARs this repo builds and requery 3.49.0 from JitPack as a file +dependency, and runs the script on both APKs. The release must pass. The debug APK must fail on the provider and on +`libsqlitedbg.so`, which shows the check still sees what the AAR brings. The task builds the AARs it needs, so +`:gradle-plugin:test` leaves this test out. QA can still run the script on any release APK. + +## Known limits + +- Room's `BundledSQLiteDriver` is not covered (it brings its own SQLite). +- requery's native library is built without ICU: there is no `UNICODE` collator (a `COLLATE UNICODE` string + constant fails the build, see above) and `LOCALIZED` compares bytes (a warning). +- `SupportSQLiteDatabase.execPerConnectionSQL` is unsupported (see the design change): no build failure, an + `UnsupportedOperationException` when called. +- Passing an Android `CursorWindow` where a (renamed) `SQLiteClosable` is expected cannot work: android's + `CursorWindow` extends android's `SQLiteClosable`, which stays the platform's class. Calls that name a + `CursorWindow` in a renamed class's descriptor fail the build; calls on a `CursorWindow` are left alone. +- The items below were reported by review and not verified here: `ATTACH` on a WAL database (requery runs it on + one connection only; Android turns write-ahead logging off on the first `ATTACH`); page size and + `wal_autocheckpoint` defaults can differ from the platform's; there is no compatibility WAL mode; the cursor + window is a fixed 2 MB, and a row larger than it throws `IllegalStateException`; `beginTransactionReadOnly` + is not in requery (it fails the build) and its read-only semantics have no counterpart; requery has no + `StrictMode` hooks, so StrictMode does not report database access on the main thread. +- Quick Build is not applied to a project with `cogo.sqliteDebugger=true`: its hot payload is compiled by + the Quick Build daemon from the project's source (and its baseline payload is diverted out of the classes + pipeline), so those classes would still use `android.database.sqlite` next to rewritten baseline classes. + The plugin logs why and the build is a normal one. The IDE does not provision Quick Build for such a project + either (`GradleQuickBuildProvisioner.sqliteDebuggerRefusal`): it shows `quick_build_disabled_sqlite_debugger`, which + says Quick Build is off while the SQLite debugger is on. The editor's status line then reads + `quick_build_status_off_sqlite_debugger`, and a tap on it, like a tap on the Quick Build button, shows that + message again. +- Classes loaded by name (`Class.forName`, reflection, strings naming `android.database.sqlite.*`) are not + rewritten. A string constant that names one of the renamed classes (dots or slashes, `$` or `.` before a nested + class, alone or in a type descriptor) logs a warning with class, method and line, in the classes the fail rule + covers; names of kept classes such as `SQLiteException`, and longer names that contain a renamed one, such as + `xandroid.database.sqlite.SQLiteDatabase`, do not. +- Stores of an SQLite cursor into an array known only as a `Cursor[]` or `Object[]` are allowed on purpose (failing + them would fail ordinary apps), but such an array can be an `AbstractCursor[]` at run time (arrays are covariant), + and a store whose array type is unknown where branches merge is not checked. Both can still throw + `ArrayStoreException`, in release builds too (the rewrite runs in every variant of an opted-in app). Cursors loaded + from arrays are not followed. +- The `AbstractWindowedCursor` check follows the cursor through locals and the stack only. A cast of a + cursor that reached the method through a field, a parameter or another call is not seen (the app throws + `ClassCastException` at run time), and a cast of a cursor from an SQLite API to a type that is not an + SQLite cursor cannot be a false positive. +- Compile-time constants are copied into callers, so uses of `NO_LOCALIZED_COLLATORS`, `JOURNAL_MODE_*` or `SYNC_MODE_*` are + not visible to the fail rule. `NO_LOCALIZED_COLLATORS` is dropped by the flag translation. +- The file permission bits of the `mode` of `Context.openOrCreateDatabase` are not applied. +- A direct subclass of `SQLiteQueryBuilder` or `SQLiteCursor` that overrides a member requery lacks is not + reported. +- On a `SQLiteCursor` subclass whose class the build cannot resolve, `getWindow()` is not detected, and + `setWindow(CursorWindow)` and `mWindow` only log a warning; each fails at run time. `getWindow()` is not in the rule + table, and failing every unresolved member that names a `CursorWindow` would fail ordinary apps (android's kept + `AbstractWindowedCursor` declares all three, and `CrossProcessCursor` declares `getWindow()`). On any class the + build cannot resolve, a call that matches the rule table fails the build only when its descriptor names an + SQLite-related type (see Rules); any other match, such as `isString(I)Z`, `isStrict()Z` or `setWindow(CursorWindow)`, + logs a warning with class, method and line and is left as it is, and fails at run time if the class does extend the + renamed class. +- `instanceof` is not rewritten: `cursor instanceof AbstractWindowedCursor`, `AbstractCursor` or + `CrossProcessCursor` is silently false for requery's cursors (no build failure), which is what Room's + `DBUtil.query` relies on. +- A cancelled query throws `androidx.core.os.OperationCanceledException`, not + `android.os.OperationCanceledException`: requery checks androidx.core's signal, which the rewrite converts Android's + to, and its native layer throws the same class (checked with javap and in requery 3.49.0's `.so`). The two classes + are unrelated, and the rewrite does not change `catch` clauses, so a `catch (android.os.OperationCanceledException e)` + around a query, or around reading its cursor, no longer catches a cancellation, which reaches the caller instead. An + app that catches it should catch both, `catch (android.os.OperationCanceledException | + androidx.core.os.OperationCanceledException e)`, which works with the SQLite debugger on and off. +- Android's `CancellationSignal` holds one listener: the conversion replaces any listener the caller set. Each + query gets its own androidx.core signal, and cancelling the Android signal cancels every one still in use, so + two lazy queries that share it both stop (requery keeps a query's signal for its cursor's later reads). The + runtime's forwarder is held only as the Android signal's listener (`SqliteRewriteRuntime.FORWARDERS` holds signals + and forwarders weakly), so nothing of a signal the app dropped is kept: a strong value outlived its key until the + map's next call. A caller that sets its own listener after a query lets that forwarder go: the next query on the + signal gets a new one, and queries from before the caller's listener no longer stop with the signal. +- Which `SQLiteException` subclass requery throws for an error code (for example `SQLiteConstraintException`, which + Room catches) is decided by its native layer; this rewrite does not change it and the survey did not check it. diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt index 0a880dfeff..d82dce3507 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt @@ -16,10 +16,12 @@ */ package com.itsaky.androidide.gradle +import com.itsaky.androidide.gradle.sqlite.SqliteRewritePlugin import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_JDWP_ENABLED import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_LOG_SENDER_ENABLED import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_PROFILEABLE_ENABLED import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_QUICK_BUILD_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED import org.gradle.api.GradleException import org.gradle.api.Plugin import org.gradle.api.Project @@ -42,6 +44,16 @@ class AndroidIDEGradlePlugin : Plugin { * `QuickBuildPluginTest`. */ internal const val QUICK_BUILD_PLUGIN_CLASS = "com.itsaky.androidide.gradle.QuickBuildPlugin" + + /** + * Quick Build ships edited classes as a separate payload, compiled by its own daemon from the + * project's source and so never passed through the SQLite rewrite. Mixed with rewritten + * baseline classes it would hand android.database.sqlite objects to requery ones. + */ + internal const val QUICK_BUILD_SQLITE_DEBUGGER_MESSAGE = + "Quick Build is not applied: cogo.sqliteDebugger=true moves the app to requery at build time, " + + "and Quick Build's hot payload is compiled separately and would still use android.database.sqlite. " + + "Building normally; use a Standard Run, or remove the property to use Quick Build." } override fun apply(target: Project) { @@ -55,6 +67,14 @@ class AndroidIDEGradlePlugin : Plugin { pluginManager.apply(LogSenderPlugin::class.java) } + // One opt-in for both (D13): the rewrite moves the app to requery in every variant, the + // debugger needs requery and goes into the debuggable variants only. + val isSqliteDebuggerEnabled = findProperty(PROPERTY_SQLITE_DEBUGGER_ENABLED) == "true" + if (isSqliteDebuggerEnabled) { + pluginManager.apply(SqliteRewritePlugin::class.java) + pluginManager.apply(SqliteDebuggerPlugin::class.java) + } + val isJdwpEnabled = findProperty(PROPERTY_JDWP_ENABLED) == "true" if (isJdwpEnabled) { pluginManager.apply(JdwpPlugin::class.java) @@ -66,7 +86,9 @@ class AndroidIDEGradlePlugin : Plugin { } val isQuickBuildEnabled = findProperty(PROPERTY_QUICK_BUILD_ENABLED) == "true" - if (isQuickBuildEnabled) { + if (isQuickBuildEnabled && isSqliteDebuggerEnabled) { + logger.lifecycle(QUICK_BUILD_SQLITE_DEBUGGER_MESSAGE) + } else if (isQuickBuildEnabled) { // By name, not ::class: Quick Build classes load (and touch newer AGP APIs) // only when the property enables them - see QUICK_BUILD_PLUGIN_CLASS. pluginManager.apply(Class.forName(QUICK_BUILD_PLUGIN_CLASS)) diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt index 4819ca0dd9..4cb348c462 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt @@ -85,12 +85,40 @@ class LogSenderPlugin : Plugin { } } } +} - private fun ApplicationVariant.withRuntimeConfiguration(action: Configuration.() -> Unit) { - if (this is ApplicationVariantImpl) { - variantDependencies.runtimeClasspath.action() - } else if (this is AnalyticsEnabledApplicationVariant) { - delegate.withRuntimeConfiguration(action) - } +/** + * The variant's runtime classpath configuration, or null for a variant type it does not recognize + * (a wrapper a future AGP may add). + */ +internal fun ApplicationVariant.runtimeConfigurationOrNull(): Configuration? = + when (this) { + is ApplicationVariantImpl -> variantDependencies.runtimeClasspath + is AnalyticsEnabledApplicationVariant -> delegate.runtimeConfigurationOrNull() + else -> null } + +/** + * Runs [action] on the variant's runtime classpath configuration. Used by [LogSenderPlugin], and + * does nothing for a variant type it does not recognize; the SQLite plugins use + * [requireRuntimeConfiguration] instead. + */ +internal fun ApplicationVariant.withRuntimeConfiguration(action: Configuration.() -> Unit) { + runtimeConfigurationOrNull()?.action() +} + +/** + * Like [withRuntimeConfiguration], but fails the build when the configuration cannot be reached. For + * plugins that have already registered something that needs what [action] adds: skipping it would + * build an app that throws NoClassDefFoundError on the device. + */ +internal fun ApplicationVariant.requireRuntimeConfiguration(action: Configuration.() -> Unit) { + val configuration = + runtimeConfigurationOrNull() + ?: throw GradleException( + "Cannot reach the runtime classpath of variant '$name': its type ${javaClass.name} is not one this " + + "plugin knows. The SQLite debugger cannot add its runtime dependencies to it, so the app would " + + "fail at run time. Remove 'cogo.sqliteDebugger' from gradle.properties to build without it.", + ) + configuration.action() } diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteAar.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteAar.kt new file mode 100644 index 0000000000..73e88d2fad --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteAar.kt @@ -0,0 +1,37 @@ +package com.itsaky.androidide.gradle + +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import org.gradle.api.GradleException +import org.gradle.api.Project +import java.io.File + +/** + * The AAR that [property] names, for a plugin applied only when `cogo.sqliteDebugger=true`. + * Fails the build when the property is unset or the file is missing: the project opted in, and a + * build that quietly drops the feature leaves the user wondering why it does nothing. A missing file + * can be transient: app init extracts the AARs in the background, and a build started right after a + * fresh install can run before the first extraction ends, so the message also says to retry. + * + * @param aarLabel what the AAR is called in the "not found" message. + * @param feature what the user's opt-in turns on, for the "remove the line" advice. + */ +internal fun Project.requireSqliteAar( + property: String, + aarLabel: String, + feature: String, +): File { + val aar = + findProperty(property)?.let { File(it.toString()) } + ?: throw GradleException( + "'$PROPERTY_SQLITE_DEBUGGER_ENABLED' is true but no property '$property' is set. " + + "Remove the line from gradle.properties to build without the $feature.", + ) + if (!aar.isFile) { + throw GradleException( + "$aarLabel not found at '${aar.absolutePath}'. " + + "Remove '$PROPERTY_SQLITE_DEBUGGER_ENABLED' from gradle.properties to build without it, " + + "or retry in a few seconds if Code on the Go has just started.", + ) + } + return aar +} diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteDebuggerPlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteDebuggerPlugin.kt new file mode 100644 index 0000000000..a52cfbb5f7 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteDebuggerPlugin.kt @@ -0,0 +1,84 @@ +package com.itsaky.androidide.gradle + +import com.android.build.api.variant.ApplicationAndroidComponentsExtension +import com.android.build.api.variant.ApplicationVariant +import com.android.build.api.variant.Component +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import org.adfa.constants.KOTLIN_VERSION +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.logging.Logging +import java.lang.reflect.Method + +/** + * Adds the SQLite debugger (ADFA-7131) to the debuggable variants of an Android application, as + * [LogSenderPlugin] does for LogSender. [AndroidIDEGradlePlugin] applies it only when the project + * sets [PROPERTY_SQLITE_DEBUGGER_ENABLED] to `true`. + * + * Fails the build when the AAR is not there: the project opted in, and a build that quietly drops + * the debugger leaves the user wondering why no database shows up. + */ +class SqliteDebuggerPlugin : Plugin { + companion object { + private val logger = Logging.getLogger(SqliteDebuggerPlugin::class.java) + + /** + * The debugger is Kotlin, and the AAR is a file dependency, which brings no POM and so no + * kotlin-stdlib. KOTLIN_VERSION is the version CoGo's local Maven repository always holds + * (new projects are generated with it), so this resolves offline. Gradle keeps the highest + * version of a module, so an app on a newer Kotlin is not downgraded. + */ + internal const val KOTLIN_STDLIB_COORDINATES = "org.jetbrains.kotlin:kotlin-stdlib:$KOTLIN_VERSION" + + /** + * `Component.debuggable`, which AGP added after the oldest version this plugin supports, so it is + * reached by reflection: minAgpCheck compiles this file against that oldest version. Null there. + */ + private val componentDebuggable: Method? = runCatching { Component::class.java.getMethod("getDebuggable") }.getOrNull() + + /** Null when this AGP has no `Variant.debuggable`. */ + internal fun readDebuggable(variant: ApplicationVariant): Boolean? = componentDebuggable?.invoke(variant) as Boolean? + } + + override fun apply(target: Project) { + if (!target.plugins.hasPlugin(APP_PLUGIN)) { + return + } + + logger.info("Applying {} to project '${target.path}'", SqliteDebuggerPlugin::class.simpleName) + + if (target.isTestEnv) { + logger.lifecycle("Applying {} to project '{}'", javaClass.simpleName, target.path) + } + + val aar = target.requireSqliteAar(PROPERTY_SQLITE_DEBUGGER_AAR, "SQLite debugger AAR", "SQLite debugger") + + target.extensions.getByType(ApplicationAndroidComponentsExtension::class.java).apply { + val addDebugger = { variant: ApplicationVariant -> + variant.requireRuntimeConfiguration { + logger.lifecycle( + "Adding SQLite debugger dependency to variant '{}' of project '{}'", + variant.name, + target.path, + ) + + dependencies.add(target.dependencies.create(target.fileTree(aar))) + dependencies.add(target.dependencies.create(KOTLIN_STDLIB_COORDINATES)) + } + } + // onDebuggableVariants reads variantBuilder.debuggable in beforeVariants, which AGP 8.11 rejects + // when a plugin is applied from CoGo's init script (see QuickBuildPlugin). Variant.debuggable in + // onVariants is the sanctioned read; AGPs that predate it still allow the builder's. + if (componentDebuggable != null) { + onVariants { variant -> + if (readDebuggable(variant) == true) { + addDebugger(variant) + } + } + } else { + onDebuggableVariants(addDebugger) + } + } + } +} diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolver.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolver.kt index ba9e0201f2..b8f22e4967 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolver.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolver.kt @@ -120,6 +120,10 @@ class ComponentProxiabilityResolver( "com.itsaky.androidide.quickbuild.runtime.QuickBuildKeepAliveService" to "the quick-build runtime's own keep-alive, not user code; skipping keeps it out " + "of the component list and the deploy policy's restart rule", + // Guard only: AndroidIDEGradlePlugin never applies Quick Build to a project that opted into the SQLite debugger. + "org.appdevforall.sqlitedebugger.DebuggerStartup" to + "CoGo's injected SQLite debugger provider (ADFA-7131); it runs once from the base APK dex, " + + "and skipping keeps it out of the component list and the deploy policy's restart rule", "com.google.firebase.components.ComponentDiscoveryService" to "Firebase plumbing whose class never travels in the payload; skipping keeps it out " + "of the component list and the deploy policy's restart rule", diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/CheckSqliteRewriteClasspathTask.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/CheckSqliteRewriteClasspathTask.kt new file mode 100644 index 0000000000..38ed312638 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/CheckSqliteRewriteClasspathTask.kt @@ -0,0 +1,87 @@ +package com.itsaky.androidide.gradle.sqlite + +import org.gradle.api.DefaultTask +import org.gradle.api.GradleException +import org.gradle.api.file.ConfigurableFileCollection +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Classpath +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.TaskAction +import java.io.File +import java.util.zip.ZipFile + +/** + * Fails the build, before anything is compiled or rewritten, when the variant's runtime classpath + * lacks what the rewritten classes load: requery itself, androidx.sqlite (whose interfaces requery + * implements) and androidx.core (whose CancellationSignal requery's API takes, and the rewrite + * converts android's signal to). requery reaches an app as a file dependency from `libs/`, which + * brings none of its own dependencies. + */ +abstract class CheckSqliteRewriteClasspathTask : DefaultTask() { + @get:Input + abstract val variantName: Property + + @get:Classpath + abstract val runtimeClasspath: ConfigurableFileCollection + + /** Written on success, so an unchanged classpath is not scanned again. */ + @get:OutputFile + abstract val stamp: RegularFileProperty + + @TaskAction + fun check() { + val files = runtimeClasspath.files + val missing = REQUIRED.filter { (_, entry) -> files.none { contains(it, entry) } } + if (missing.isNotEmpty()) { + throw GradleException(message(variantName.get(), missing.map { it.first })) + } + stamp.get().asFile.apply { + parentFile.mkdirs() + writeText("ok") + } + } + + private fun contains( + file: File, + entry: String, + ): Boolean = + when { + file.isDirectory -> File(file, entry).isFile + file.isFile -> runCatching { ZipFile(file).use { it.getEntry(entry) != null } }.getOrDefault(false) + else -> false + } + + companion object { + internal const val REQUERY_ENTRY = "io/requery/android/database/sqlite/SQLiteDatabase.class" + internal const val ANDROIDX_SQLITE_ENTRY = "androidx/sqlite/db/SupportSQLiteDatabase.class" + internal const val ANDROIDX_CORE_ENTRY = "${SqliteRewriteRules.CORE_SIGNAL}.class" + + private val REQUIRED = + listOf( + "requery's SQLiteDatabase ($REQUERY_ENTRY)" to REQUERY_ENTRY, + "androidx.sqlite's SupportSQLiteDatabase ($ANDROIDX_SQLITE_ENTRY)" to ANDROIDX_SQLITE_ENTRY, + "androidx.core's CancellationSignal ($ANDROIDX_CORE_ENTRY)" to ANDROIDX_CORE_ENTRY, + ) + + internal fun message( + variant: String, + missing: List, + ) = "cogo.sqliteDebugger=true moves the app to requery, but the runtime classpath of variant '$variant' has no " + + missing.joinToString(" and no ") + + ". requery's AAR comes from the project's libs/ directory as a file dependency, which brings none of its own " + + "dependencies: add requery's AAR to libs/ (implementation(files(\"libs/sqlite-android.aar\"))), androidx.sqlite " + + "(implementation(\"androidx.sqlite:sqlite:\"), or a library that depends on it, such as Room or WorkManager) " + + "and androidx.core (implementation(\"androidx.core:core:\"), or a library that depends on it, such as AppCompat). " + + "Remove cogo.sqliteDebugger from gradle.properties to build without them." + + /** The tasks of a variant that must not start before the check: compiling and the rewrite itself. */ + internal fun guardedTaskNames(capitalizedVariant: String) = + setOf( + "compile${capitalizedVariant}JavaWithJavac", + "compile${capitalizedVariant}Kotlin", + "transform${capitalizedVariant}ClassesWithAsm", + ) + } +} diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewritePlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewritePlugin.kt new file mode 100644 index 0000000000..5fece80083 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewritePlugin.kt @@ -0,0 +1,83 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle.sqlite + +import com.android.build.api.instrumentation.InstrumentationScope +import com.android.build.api.variant.ApplicationAndroidComponentsExtension +import com.itsaky.androidide.gradle.APP_PLUGIN +import com.itsaky.androidide.gradle.requireRuntimeConfiguration +import com.itsaky.androidide.gradle.requireSqliteAar +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.artifacts.type.ArtifactTypeDefinition +import org.gradle.api.logging.Logging + +/** + * Registers the android.database.sqlite -> requery class rewrite on every variant (ADFA-7138), adds the + * helpers the rewritten classes call to every variant's runtime classpath, and checks that requery and + * androidx.sqlite are there too. + * + * Applied only when [PROPERTY_SQLITE_DEBUGGER_ENABLED] is "true": the rewritten classes need requery in + * the app, and it reaches apps only through the template's libs/ (ADFA-7082), so a project that + * did not opt in would crash at class load. The helper AAR is Code on the Go's, passed in like the + * debugger's; the build fails when it is missing, because the project opted in. + */ +class SqliteRewritePlugin : Plugin { + companion object { + private val logger = Logging.getLogger(SqliteRewritePlugin::class.java) + + /** AGP's artifact-type attribute for a dependency's classes as a jar; AGP-internal, so the raw string. */ + internal const val CLASSES_JAR_ARTIFACT_TYPE = "android-classes-jar" + } + + override fun apply(target: Project) { + if (!target.plugins.hasPlugin(APP_PLUGIN)) { + return + } + logger.info("Applying SqliteRewritePlugin to project '${target.path}'") + + val runtimeAar = + target.requireSqliteAar(PROPERTY_SQLITE_REWRITE_RUNTIME_AAR, "SQLite rewrite runtime AAR", "SQLite rewrite") + + target.extensions.getByType(ApplicationAndroidComponentsExtension::class.java).onVariants { variant -> + variant.instrumentation.transformClassesWith( + SqliteRewriteVisitorFactory::class.java, + InstrumentationScope.ALL, + ) {} + // Frames are copied; the rewriter keeps the one method whose descriptor changes valid itself. + variant.instrumentation.setAsmFramesComputationMode(SqliteRewriter.FRAMES_MODE) + + val capitalized = variant.name.replaceFirstChar { it.uppercase() } + variant.requireRuntimeConfiguration { + logger.lifecycle("Adding SQLite rewrite runtime dependency to variant '{}' of project '{}'", variant.name, target.path) + dependencies.add(target.dependencies.create(target.fileTree(runtimeAar))) + + val check = + target.tasks.register( + "check${capitalized}SqliteRewriteClasspath", + CheckSqliteRewriteClasspathTask::class.java, + ) { task -> + task.variantName.set(variant.name) + task.runtimeClasspath.from( + incoming + .artifactView { view -> + view.attributes { + it.attribute( + ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, + CLASSES_JAR_ARTIFACT_TYPE, + ) + } + view.setLenient(true) + }.files, + ) + task.stamp.set(target.layout.buildDirectory.file("intermediates/sqlite_rewrite_classpath/${variant.name}/ok")) + } + target.tasks + .matching { it.name in CheckSqliteRewriteClasspathTask.guardedTaskNames(capitalized) } + .configureEach { it.dependsOn(check) } + } + } + } +} diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRules.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRules.kt new file mode 100644 index 0000000000..d06adc1c59 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRules.kt @@ -0,0 +1,324 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle.sqlite + +/** + * The rule tables of the android.database.sqlite -> requery rewrite (ADFA-7138), from the Wave 0 + * comparison of android.jar against requery 3.49.0. Names are JVM internal names and descriptors + * with Android's original names: the rename runs after the other rules. + */ +internal object SqliteRewriteRules { + const val SQLITE_PKG = "android/database/sqlite/" + private const val REQUERY_SQLITE_PKG = "io/requery/android/database/sqlite/" + private const val REQUERY_DB_PKG = "io/requery/android/database/" + + const val DATABASE = "${SQLITE_PKG}SQLiteDatabase" + const val CURSOR_FACTORY = "${SQLITE_PKG}SQLiteDatabase\$CursorFactory" + const val CURSOR_DRIVER = "${SQLITE_PKG}SQLiteCursorDriver" + const val OPEN_HELPER = "${SQLITE_PKG}SQLiteOpenHelper" + const val QUERY_BUILDER = "${SQLITE_PKG}SQLiteQueryBuilder" + const val SQLITE_CURSOR = "${SQLITE_PKG}SQLiteCursor" + const val OPEN_PARAMS = "${SQLITE_PKG}SQLiteDatabase\$OpenParams" + const val ERROR_HANDLER = "android/database/DatabaseErrorHandler" + const val ABSTRACT_WINDOWED_CURSOR = "android/database/AbstractWindowedCursor" + const val ABSTRACT_CURSOR = "android/database/AbstractCursor" + const val CROSS_PROCESS_CURSOR = "android/database/CrossProcessCursor" + + /** + * Android cursor types that requery's cursors do not extend or implement: its SQLiteCursor extends + * its own AbstractWindowedCursor and AbstractCursor, which implement only android.database.Cursor. + */ + val ANDROID_ONLY_CURSOR_TYPES: Set = setOf(ABSTRACT_WINDOWED_CURSOR, ABSTRACT_CURSOR, CROSS_PROCESS_CURSOR) + const val CURSOR_WINDOW = "android/database/CursorWindow" + const val ANDROID_SIGNAL = "android/os/CancellationSignal" + const val CORE_SIGNAL = "androidx/core/os/CancellationSignal" + const val CONTEXT = "android/content/Context" + + val COLLATE_UNICODE = Regex("COLLATE\\s+UNICODE", RegexOption.IGNORE_CASE) + val COLLATE_LOCALIZED = Regex("COLLATE\\s+LOCALIZED", RegexOption.IGNORE_CASE) + + const val HELPER = "org/appdevforall/sqliterewrite/SqliteRewriteRuntime" + const val HELPER_PACKAGE_PREFIX = "org/appdevforall/sqliterewrite/" + const val FRAMEWORK_PACKAGE_PREFIX = "androidx/sqlite/db/framework/" + + /** + * androidx.core's SQLiteCursorCompat calls SQLiteCursor.setFillWindowForwardOnly, which requery lacks, from a + * helper only an app that calls the compat method reaches. Every app that depends on androidx.core has the + * class, so failing the build for it would fail them all. + */ + const val CORE_DATABASE_PACKAGE_PREFIX = "androidx/core/database/" + const val REQUERY_PACKAGE_PREFIX = "io/requery/" + + /** The 10 shared classes and the two error handlers requery's copies use. */ + val RENAMES: Map = + buildMap { + for (name in listOf( + "SQLiteClosable", + "SQLiteCursor", + "SQLiteCursorDriver", + "SQLiteDatabase", + "SQLiteDatabase\$CursorFactory", + "SQLiteOpenHelper", + "SQLiteProgram", + "SQLiteQuery", + "SQLiteQueryBuilder", + "SQLiteStatement", + )) { + put("$SQLITE_PKG$name", "$REQUERY_SQLITE_PKG$name") + } + put(ERROR_HANDLER, "${REQUERY_DB_PKG}DatabaseErrorHandler") + put("android/database/DefaultDatabaseErrorHandler", "${REQUERY_DB_PKG}DefaultDatabaseErrorHandler") + } + + /** The Android classes that get renamed. */ + val SQLITE_CLASSES: Set = RENAMES.keys.filter { it.startsWith(SQLITE_PKG) }.toSet() + + /** + * A renamed class in a string, as Class.forName or reflection spells it: dots or slashes, `$` or `.` before a + * nested class. Longest names first, and neither preceded nor followed by more of a name, so a kept class such as + * SQLiteDatabaseLockedException does not match SQLiteDatabase, nor does a longer name such as + * xandroid.database.sqlite.SQLiteDatabase. The `L` of a type descriptor may precede it, as in + * `[Landroid.database.sqlite.SQLiteDatabase;`, the name Class.forName takes for an array of it. + */ + val RENAMED_CLASS_NAME: Regex = + Regex( + RENAMES.keys + .sortedByDescending { it.length } + .joinToString( + "|", + prefix = "(?:(?> = + mapOf( + "${SQLITE_PKG}SQLiteDatabase" to listOf("${SQLITE_PKG}SQLiteClosable"), + "${SQLITE_PKG}SQLiteProgram" to listOf("${SQLITE_PKG}SQLiteClosable"), + "${SQLITE_PKG}SQLiteQuery" to listOf("${SQLITE_PKG}SQLiteProgram", "${SQLITE_PKG}SQLiteClosable"), + "${SQLITE_PKG}SQLiteStatement" to listOf("${SQLITE_PKG}SQLiteProgram", "${SQLITE_PKG}SQLiteClosable"), + ) + + /** + * Members requery lacks, or has with another meaning. Key: "owner.name" -> the descriptors it lacks; + * other overloads are not matched, so every entry names its descriptors. + */ + val MISSING_MEMBERS: Map> = + buildMap> { + fun no( + owner: String, + name: String, + desc: String, + ) { + getOrPut("$SQLITE_PKG$owner.$name") { mutableSetOf() }.add(desc) + } + no("SQLiteClosable", "onAllReferencesReleasedFromContainer", "()V") + no("SQLiteClosable", "releaseReferenceFromContainer", "()V") + no("SQLiteCursor", "setFillWindowForwardOnly", "(Z)V") + // requery has only the 3-argument constructor, and its window is its own CursorWindow + no("SQLiteCursor", "", "(L$DATABASE;L$CURSOR_DRIVER;Ljava/lang/String;L${SQLITE_PKG}SQLiteQuery;)V") + no("SQLiteCursor", "setWindow", "(L$CURSOR_WINDOW;)V") + // inherited from android's AbstractCursor and AbstractWindowedCursor, which requery's copies lack + // those (found with javap over both jars; SqliteRewriteResolutionTest probes the rest) + for (name in listOf("isBlob", "isFloat", "isLong", "isString", "isFieldUpdated")) { + no("SQLiteCursor", name, "(I)Z") + } + no("SQLiteCursor", "getUpdatedField", "(I)Ljava/lang/Object;") + // requery's own mWindow has its own CursorWindow type + no("SQLiteCursor", "mWindow", "L$CURSOR_WINDOW;") + no("SQLiteCursor", "getNotificationUris", "()Ljava/util/List;") + no("SQLiteCursor", "setNotificationUris", "(Landroid/content/ContentResolver;Ljava/util/List;)V") + // The constants of SQLiteDatabase that requery lacks (JOURNAL_MODE_*, SYNC_MODE_*, NO_LOCALIZED_COLLATORS, + // SQLITE_MAX_LIKE_PATTERN_LENGTH) are not listed: javac copies them into callers, so no field access remains. + no("SQLiteDatabase", "setLockingEnabled", "(Z)V") + no("SQLiteDatabase", "isDbLockedByOtherThreads", "()Z") + no("SQLiteDatabase", "yieldIfContended", "()Z") + no("SQLiteDatabase", "getSyncedTables", "()Ljava/util/Map;") + no("SQLiteDatabase", "createInMemory", "(L$OPEN_PARAMS;)L$DATABASE;") + no("SQLiteDatabase", "setCustomScalarFunction", "(Ljava/lang/String;Ljava/util/function/UnaryOperator;)V") + no("SQLiteDatabase", "setCustomAggregateFunction", "(Ljava/lang/String;Ljava/util/function/BinaryOperator;)V") + no("SQLiteDatabase", "execPerConnectionSQL", "(Ljava/lang/String;[Ljava/lang/Object;)V") + no("SQLiteDatabase", "markTableSyncable", "(Ljava/lang/String;Ljava/lang/String;)V") + no("SQLiteDatabase", "markTableSyncable", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V") + no("SQLiteOpenHelper", "setLookasideConfig", "(II)V") + no("SQLiteOpenHelper", "setOpenParams", "(L$OPEN_PARAMS;)V") + no("SQLiteOpenHelper", "setIdleConnectionTimeout", "(J)V") + no("SQLiteProgram", "getUniqueId", "()I") + no("SQLiteQueryBuilder", "isDistinct", "()Z") + no("SQLiteQueryBuilder", "appendWhereStandalone", "(Ljava/lang/CharSequence;)V") + no("SQLiteQueryBuilder", "getProjectionMap", "()Ljava/util/Map;") + no("SQLiteQueryBuilder", "setProjectionGreylist", "(Ljava/util/Collection;)V") + no("SQLiteQueryBuilder", "getProjectionGreylist", "()Ljava/util/Collection;") + no("SQLiteQueryBuilder", "getCursorFactory", "()L$CURSOR_FACTORY;") + no("SQLiteQueryBuilder", "isStrict", "()Z") + no("SQLiteQueryBuilder", "setStrictColumns", "(Z)V") + no("SQLiteQueryBuilder", "isStrictColumns", "()Z") + no("SQLiteQueryBuilder", "setStrictGrammar", "(Z)V") + no("SQLiteQueryBuilder", "isStrictGrammar", "()Z") + no("SQLiteQueryBuilder", "insert", "(L$DATABASE;Landroid/content/ContentValues;)J") + no("SQLiteQueryBuilder", "update", "(L$DATABASE;Landroid/content/ContentValues;Ljava/lang/String;[Ljava/lang/String;)I") + no("SQLiteQueryBuilder", "delete", "(L$DATABASE;Ljava/lang/String;[Ljava/lang/String;)I") + // requery's versions have no selectionArgs parameter + no( + "SQLiteQueryBuilder", + "buildQuery", + "([Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", + ) + no( + "SQLiteQueryBuilder", + "buildUnionSubQuery", + "(Ljava/lang/String;[Ljava/lang/String;Ljava/util/Set;ILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", + ) + // added to android.jar after the Wave 0 members.tsv (API 35/36); requery has none of them + no("SQLiteDatabase", "beginTransactionReadOnly", "()V") + no("SQLiteDatabase", "beginTransactionWithListenerReadOnly", "(L${SQLITE_PKG}SQLiteTransactionListener;)V") + no("SQLiteDatabase", "createRawStatement", "(Ljava/lang/String;)L${SQLITE_PKG}SQLiteRawStatement;") + no("SQLiteDatabase", "getLastChangedRowCount", "()J") + no("SQLiteDatabase", "getLastInsertRowId", "()J") + no("SQLiteDatabase", "getTotalChangedRowCount", "()J") + // the OpenParams overloads + no("SQLiteDatabase", "openDatabase", "(Ljava/io/File;L$OPEN_PARAMS;)L$DATABASE;") + no("SQLiteOpenHelper", "", "(Landroid/content/Context;Ljava/lang/String;IL$OPEN_PARAMS;)V") + } + + /** + * Method names with an entry in [MISSING_MEMBERS], so other calls need no hierarchy lookup. Constructors + * are left out: a subclass has no constructor of its parent's, so only the renamed classes themselves match. + */ + val MISSING_MEMBER_NAMES: Set = + MISSING_MEMBERS.keys + .map { it.substringAfterLast('.') } + .filter { it != "" } + .toSet() + + /** Names of the fields the missing-member rule can match: entries with a field descriptor. */ + val MISSING_FIELD_NAMES: Set = + MISSING_MEMBERS + .filterValues { descs -> descs.any { !it.startsWith("(") } } + .keys + .map { it.substringAfterLast('.') } + .toSet() + + /** A call whose descriptor changes: selectionArgs-style String[] to Object[], and the signal. */ + class DescriptorRule( + val owner: String, + val name: String, + val desc: String, + val objectArrayParams: Set, + ) + + private val S = "Ljava/lang/String;" + private val SA = "[Ljava/lang/String;" + private val SIG = "L$ANDROID_SIGNAL;" + private const val CF = "L$CURSOR_FACTORY;" + private const val CUR = "Landroid/database/Cursor;" + + val DESCRIPTOR_RULES: List = + run { + fun rule( + name: String, + params: String, + ret: String, + vararg objectArrays: Int, + ) = listOf( + DescriptorRule(DATABASE, name, "($params)$ret", objectArrays.toSet()), + DescriptorRule(DATABASE, name, "($params$SIG)$ret", objectArrays.toSet()), + ) + rule("query", "Z$S$SA$S$SA$S$S$S$S", CUR, 4) + + rule("query", "$S$SA$S$SA$S$S$S", CUR, 3) + + rule("query", "$S$SA$S$SA$S$S$S$S", CUR, 3) + + rule("queryWithFactory", "${CF}Z$S$SA$S$SA$S$S$S$S", CUR, 5) + + rule("rawQuery", "$S$SA", CUR, 1) + + rule("rawQueryWithFactory", "$CF$S$SA$S", CUR, 2) + + // the one signal-only entry; its signal-less form is not an Android method + DescriptorRule(DATABASE, "validateSql", "(${S}$SIG)V", emptySet()) + + DescriptorRule(QUERY_BUILDER, "query", "(L$DATABASE;$SA$S$SA$S$S$S$S$SIG)$CUR", emptySet()) + } + + /** Names of the calls whose descriptor changes. */ + val DESCRIPTOR_RULE_NAMES: Set = DESCRIPTOR_RULES.map { it.name }.toSet() + + /** The renamed classes android.jar declares final: no other class extends them. */ + private val FINAL_CLASSES: Set = setOf(DATABASE, "${SQLITE_PKG}SQLiteQuery", "${SQLITE_PKG}SQLiteStatement") + + /** + * Name plus descriptor of each member that a rule changes or rejects and that another class can inherit (not a + * constructor, nor a member of a final class), mapped to the renamed class that declares it. A class the build + * cannot resolve may inherit one of these, so a call that matches one exactly cannot be checked. Such a call + * fails the build only when its descriptor names an SQLite-related type ([SQLITE_DESCRIPTOR_TOKENS]); otherwise + * it is a warning, since a bare name and descriptor like isStrict()Z, or setWindow(CursorWindow) on any windowed + * cursor, is common in unrelated classes. + */ + val INHERITABLE_RULE_MEMBERS: Map = + buildMap { + for ((key, descs) in MISSING_MEMBERS) { + val owner = key.substringBeforeLast('.') + val name = key.substringAfterLast('.') + if (owner in FINAL_CLASSES || name == "") continue + for (desc in descs) putIfAbsent(name + desc, owner) + } + for (rule in DESCRIPTOR_RULES) { + if (rule.owner !in FINAL_CLASSES) putIfAbsent(rule.name + rule.desc, rule.owner) + } + } + + /** SQLiteCursorDriver.query, matched on any owner: an app class may implement the interface. */ + const val DRIVER_QUERY_NAME = "query" + const val DRIVER_QUERY_DESC = "($CF[Ljava/lang/String;)Landroid/database/Cursor;" + const val DRIVER_QUERY_NEW_DESC = "($CF[Ljava/lang/Object;)Landroid/database/Cursor;" + + const val HELPER_OPEN_DESC = "(Ljava/lang/String;${CF}I)L$DATABASE;" + const val HELPER_OPEN_HANDLER_DESC = "(Ljava/lang/String;${CF}IL$ERROR_HANDLER;)L$DATABASE;" + const val CONTEXT_OPEN_DESC = "(Ljava/lang/String;I$CF)L$DATABASE;" + const val CONTEXT_OPEN_HANDLER_DESC = "(Ljava/lang/String;I${CF}L$ERROR_HANDLER;)L$DATABASE;" + const val CONTEXT_DELETE_DESC = "(Ljava/lang/String;)Z" + const val HELPER_SIGNAL_DESC = "(L$ANDROID_SIGNAL;)L$CORE_SIGNAL;" + + const val DATABASE_UTILS = "android/database/DatabaseUtils" + + /** + * DatabaseUtils methods that take renamed types and that the runtime reimplements, as name plus + * Android descriptor. requery has no DatabaseUtils. The rest of DatabaseUtils' and other platform + * methods that mention renamed types fail the build. + */ + val REDIRECTED_UTILS: Set = + run { + val db = "L$DATABASE;" + val stmt = "L${SQLITE_PKG}SQLiteStatement;" + setOf( + "queryNumEntries($db$S)J", + "queryNumEntries($db$S$S)J", + "queryNumEntries($db$S$S$SA)J", + "longForQuery($db$S$SA)J", + "longForQuery($stmt$SA)J", + "stringForQuery($db$S$SA)$S", + "stringForQuery($stmt$SA)$S", + ) + } + + /** How a renamed class appears in a descriptor. */ + val RENAMED_DESCRIPTOR_TOKENS: List = RENAMES.keys.map { "L$it;" } + + /** + * What marks a descriptor as SQLite-related: a class in either package, Cursor, the signals, or the error handler. + * Not android.database.CursorWindow, which is not an SQLite type: it carries every ContentProvider cursor across + * processes, and android's kept AbstractWindowedCursor declares setWindow(CursorWindow) and mWindow too, so a + * library's own windowed cursor on an owner the build cannot resolve would match SQLiteCursor's. + */ + val SQLITE_DESCRIPTOR_TOKENS: List = + listOf("L$SQLITE_PKG", "L$REQUERY_DB_PKG", CUR, "L$ANDROID_SIGNAL;", "L$CORE_SIGNAL;", "L$ERROR_HANDLER;") + + val CONTEXT_FAMILY: Set = + setOf( + CONTEXT, + "android/content/ContextWrapper", + "android/view/ContextThemeWrapper", + "android/app/Activity", + "android/app/Application", + "android/app/Service", + "android/app/IntentService", + ) +} diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteVisitorFactory.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteVisitorFactory.kt new file mode 100644 index 0000000000..6d70446ab1 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteVisitorFactory.kt @@ -0,0 +1,34 @@ +package com.itsaky.androidide.gradle.sqlite + +import com.android.build.api.instrumentation.AsmClassVisitorFactory +import com.android.build.api.instrumentation.ClassContext +import com.android.build.api.instrumentation.ClassData +import com.android.build.api.instrumentation.InstrumentationParameters +import org.objectweb.asm.ClassVisitor + +/** Moves classes from android.database.sqlite to requery; see [SqliteRewriter]. */ +abstract class SqliteRewriteVisitorFactory : AsmClassVisitorFactory { + override fun createClassVisitor( + classContext: ClassContext, + nextClassVisitor: ClassVisitor, + ): ClassVisitor = SqliteRewriter.createVisitor(nextClassVisitor, AgpHierarchy(classContext)) + + override fun isInstrumentable(classData: ClassData): Boolean = !RewritePolicy.isExcluded(classData.className.replace('.', '/')) + + private class AgpHierarchy( + private val context: ClassContext, + ) : ClassHierarchy { + override fun superTypes(internalName: String): Collection? { + val data = context.loadClassData(internalName.replace('/', '.')) ?: return null + val result = linkedSetOf() + val pending = ArrayDeque(data.superClasses + data.interfaces) + while (pending.isNotEmpty()) { + val name = pending.removeFirst() + if (result.add(name.replace('.', '/'))) { + context.loadClassData(name)?.let { pending.addAll(it.interfaces) } + } + } + return result + } + } +} diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriter.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriter.kt new file mode 100644 index 0000000000..2baf2d0ddd --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriter.kt @@ -0,0 +1,1042 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle.sqlite + +import com.android.build.api.instrumentation.FramesComputationMode +import org.gradle.api.logging.Logging +import org.objectweb.asm.ClassReader +import org.objectweb.asm.ClassVisitor +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.Handle +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import org.objectweb.asm.commons.ClassRemapper +import org.objectweb.asm.commons.Remapper +import org.objectweb.asm.tree.AbstractInsnNode +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.FieldInsnNode +import org.objectweb.asm.tree.FrameNode +import org.objectweb.asm.tree.InsnList +import org.objectweb.asm.tree.InvokeDynamicInsnNode +import org.objectweb.asm.tree.LabelNode +import org.objectweb.asm.tree.LdcInsnNode +import org.objectweb.asm.tree.LineNumberNode +import org.objectweb.asm.tree.MethodInsnNode +import org.objectweb.asm.tree.MethodNode +import org.objectweb.asm.tree.TypeInsnNode +import org.objectweb.asm.tree.VarInsnNode +import org.objectweb.asm.tree.analysis.Analyzer +import org.objectweb.asm.tree.analysis.AnalyzerException +import org.objectweb.asm.tree.analysis.BasicInterpreter +import org.objectweb.asm.tree.analysis.BasicValue +import org.objectweb.asm.tree.analysis.Frame +import org.objectweb.asm.tree.analysis.Interpreter +import com.itsaky.androidide.gradle.sqlite.SqliteRewriteRules as Rules + +/** Thrown with every unsupported reference of a class, so the build names them all at once. */ +class SqliteRewriteException( + message: String, +) : RuntimeException(message) + +/** All supertypes (classes and interfaces, transitive) of a class in internal names, or null. */ +fun interface ClassHierarchy { + fun superTypes(internalName: String): Collection? + + companion object { + val NONE = ClassHierarchy { null } + } +} + +/** Which rules run on a class. The rename always runs. */ +enum class RewritePolicy( + val redirect: Boolean, + val fail: Boolean, +) { + FULL(true, true), + + /** androidx's framework classes (still reachable through AndroidSQLiteDriver; see the docs) and androidx.core's database helpers. */ + NO_FAIL(true, false), + + /** The runtime helper: its redirect targets must not be redirected themselves. */ + RENAME_ONLY(false, false), + ; + + companion object { + fun forClass(internalName: String): RewritePolicy = + when { + internalName.startsWith(Rules.HELPER_PACKAGE_PREFIX) -> RENAME_ONLY + internalName.startsWith(Rules.FRAMEWORK_PACKAGE_PREFIX) -> NO_FAIL + internalName.startsWith(Rules.CORE_DATABASE_PACKAGE_PREFIX) -> NO_FAIL + else -> FULL + } + + /** Classes the rewrite never touches: requery's own. */ + fun isExcluded(internalName: String): Boolean = internalName.startsWith(Rules.REQUERY_PACKAGE_PREFIX) + } +} + +object SqliteRewriter { + /** The transform cannot tell an app class from a library class, so the message covers both. */ + private const val WAY_OUT = + "If this class is the app's, change the code above. If it is in a library, the library cannot run on " + + "requery: comment out cogo.sqliteDebugger=true in gradle.properties to build without the SQLite debugger." + + private val remapper = + object : Remapper() { + override fun map(internalName: String): String = Rules.RENAMES[internalName] ?: internalName + } + + /** + * What the plugin registers with AGP, and what [rewrite] writes. Frames are copied, so the one change + * that invalidates them, a retyped `SQLiteCursorDriver.query` parameter, is handled in [ClassRewrite]. + */ + val FRAMES_MODE = FramesComputationMode.COPY_FRAMES + + private val logger by lazy { Logging.getLogger(SqliteRewriter::class.java) } + + /** For tests: the transformed bytes of one class. */ + fun rewrite( + bytes: ByteArray, + hierarchy: ClassHierarchy = ClassHierarchy.NONE, + warn: (String) -> Unit = {}, + ): ByteArray { + val writer = ClassWriter(ClassWriter.COMPUTE_MAXS) + ClassReader(bytes).accept(createVisitor(writer, hierarchy, warn), 0) + return writer.toByteArray() + } + + /** + * Buffers the class, applies the call rules on the original (Android) names, then renames on + * the way to [next]. Throws from visitEnd, which fails the AGP transform with the message. + */ + fun createVisitor( + next: ClassVisitor, + hierarchy: ClassHierarchy, + warn: (String) -> Unit = { logger.warn(it) }, + ): ClassVisitor = + object : ClassNode(Opcodes.ASM9) { + override fun visitEnd() { + super.visitEnd() + if (RewritePolicy.isExcluded(name)) { + accept(next) + return + } + ClassRewrite(this, RewritePolicy.forClass(name), hierarchy, warn).run() + accept(ClassRemapper(next, remapper)) + } + } + + /** [instanceOf]: the types an `instanceof` proved the value to be, on every path to here. */ + private class TaintValue( + type: Type?, + val sqliteCursor: Boolean, + val instanceOf: Set = emptySet(), + ) : BasicValue(type) { + // Frame.merge keeps the old value when the merged one equals it, so the taint and the proofs must count + override fun equals(other: Any?): Boolean = + other is BasicValue && super.equals(other) && sqliteCursor == ((other as? TaintValue)?.sqliteCursor ?: false) && + instanceOf == ((other as? TaintValue)?.instanceOf ?: emptySet()) + + override fun hashCode(): Int = (super.hashCode() * 31 + if (sqliteCursor) 1 else 0) * 31 + instanceOf.hashCode() + + fun provedInstanceOf(type: String) = TaintValue(this.type, sqliteCursor, instanceOf + type) + } + + /** + * A frame that learns from `ALOAD n; INSTANCEOF X; IFEQ/IFNE`, written without anything between them (no label, so + * no other path joins there): on the edge where the test succeeded, local n's value is proved an X. The proof goes + * with the value through copies, and a merge keeps it only when every path brings it. + */ + private class GuardFrame : Frame { + constructor(numLocals: Int, maxStack: Int) : super(numLocals, maxStack) + constructor(frame: Frame) : super(frame) + + private var local = -1 + private var type: String? = null + private var provedOnJump = false + private var before: BasicValue? = null + + override fun execute( + insn: AbstractInsnNode, + interpreter: Interpreter, + ) { + local = -1 + before = null + val test = insn.previous as? TypeInsnNode + val load = test?.previous as? VarInsnNode + if ((insn.opcode == Opcodes.IFEQ || insn.opcode == Opcodes.IFNE) && test?.opcode == Opcodes.INSTANCEOF && + load?.opcode == Opcodes.ALOAD + ) { + local = load.`var` + type = test.desc + provedOnJump = insn.opcode == Opcodes.IFNE + } + super.execute(insn, interpreter) + } + + // Called on this frame once per edge of the jump, before it is merged there, in no order this relies on + override fun initJumpTarget( + opcode: Int, + target: LabelNode?, + ) { + if (local < 0) return + val value = before ?: getLocal(local).also { before = it } + val proved = (target != null) == provedOnJump + setLocal(local, if (proved && value is TaintValue) value.provedInstanceOf(type!!) else value) + } + } + + /** + * A reference whose static type, on some path, is SQLiteCursor or a subclass, which the rename makes requery's + * ([renamedCursor]), or, on every path, an array of one of the Android-only cursor types ([androidCursorArray], + * that element type). + */ + private class StaticCursorValue( + type: Type?, + val renamedCursor: Boolean, + val androidCursorArray: String? = null, + ) : BasicValue(type) { + // as TaintValue: Frame.merge keeps the old value when the merged one equals it + override fun equals(other: Any?): Boolean = + other is BasicValue && super.equals(other) && renamedCursor == ((other as? StaticCursorValue)?.renamedCursor ?: false) && + androidCursorArray == (other as? StaticCursorValue)?.androidCursorArray + + override fun hashCode(): Int = (super.hashCode() * 31 + if (renamedCursor) 1 else 0) * 31 + androidCursorArray.hashCode() + } + + private class ClassRewrite( + val cn: ClassNode, + val policy: RewritePolicy, + val hierarchy: ClassHierarchy, + val warn: (String) -> Unit, + ) { + val violations = linkedSetOf() + private val warned = hashSetOf() + + fun run() { + if (policy.fail) { + checkDeclarations() + } + for (m in cn.methods) { + if (policy.fail && isContextDatabaseMethod(m.name, m.desc) && + (cn.superName in Rules.CONTEXT_FAMILY || isContext(cn.name)) + ) { + violations += "method '${m.name}${m.desc}' overrides Context.${m.name}; calls to it are redirected to requery's " + + "runtime and would skip the override" + } + if (policy.fail) bypassedOverride(m)?.let { violations += it } + if (policy.redirect && m.name == Rules.DRIVER_QUERY_NAME && m.desc == Rules.DRIVER_QUERY_DESC) { + retypeParameters(m, Rules.DRIVER_QUERY_NEW_DESC) + } + rewriteMethod(m) + } + if (violations.isNotEmpty()) { + throw SqliteRewriteException( + "Code on the Go cannot move '${cn.name.replace('/', '.')}' from android.database.sqlite to " + + "requery (cogo.sqliteDebugger=true):\n" + violations.joinToString("\n") { " - $it" } + + "\n" + WAY_OUT, + ) + } + } + + /** + * Gives [m] the descriptor [newDesc], which differs from its own in parameters that become wider + * (`String[]` to `Object[]`), and keeps the body valid. The body and its copied frames still treat those + * parameters as the old type, so each is cast back on entry. Frames before the first full one are relative + * to the method's implicit first frame, which now has the wide type, so every frame is written in full. + */ + fun retypeParameters( + m: MethodNode, + newDesc: String, + ) { + val oldDesc = m.desc + m.desc = newDesc + if (m.instructions.size() == 0) return + expandFrames(m, oldDesc) + val old = Type.getArgumentTypes(oldDesc) + val new = Type.getArgumentTypes(newDesc) + var slot = if (m.access and Opcodes.ACC_STATIC != 0) 0 else 1 + val prologue = InsnList() + for (i in old.indices) { + if (old[i] != new[i]) { + prologue.add(VarInsnNode(Opcodes.ALOAD, slot)) + prologue.add(TypeInsnNode(Opcodes.CHECKCAST, old[i].internalName)) + prologue.add(VarInsnNode(Opcodes.ASTORE, slot)) + } + slot += old[i].size + } + m.instructions.insert(prologue) + } + + /** Rewrites every compressed frame of [m] as a full one, starting from [originalDesc]'s parameters. */ + fun expandFrames( + m: MethodNode, + originalDesc: String, + ) { + for (full in fullFrames(m, originalDesc)) { + full.node.type = Opcodes.F_NEW + full.node.local = ArrayList(full.locals) + full.node.stack = ArrayList(full.stack) + } + } + + /** A frame of a method with its locals and stack in full, as a compressed frame leaves them implied. */ + class FullFrame( + val node: FrameNode, + val locals: List, + val stack: List, + ) + + /** Every frame of [m] in full, starting from [originalDesc]'s parameters; changes nothing. */ + fun fullFrames( + m: MethodNode, + originalDesc: String, + ): List { + val result = ArrayList() + var locals = ArrayList() + if (m.access and Opcodes.ACC_STATIC == 0) locals += cn.name + for (t in Type.getArgumentTypes(originalDesc)) { + locals += + when (t.sort) { + Type.BOOLEAN, Type.CHAR, Type.BYTE, Type.SHORT, Type.INT -> Opcodes.INTEGER + Type.FLOAT -> Opcodes.FLOAT + Type.LONG -> Opcodes.LONG + Type.DOUBLE -> Opcodes.DOUBLE + else -> t.internalName + } + } + for (frame in m.instructions) { + if (frame !is FrameNode) continue + var stack: List = emptyList() + when (frame.type) { + Opcodes.F_NEW, Opcodes.F_FULL -> { + locals = ArrayList(frame.local) + stack = ArrayList(frame.stack) + } + + Opcodes.F_APPEND -> { + locals.addAll(frame.local) + } + + Opcodes.F_CHOP -> { + repeat(frame.local.size) { locals.removeAt(locals.lastIndex) } + } + + Opcodes.F_SAME1 -> { + stack = ArrayList(frame.stack) + } + } + result += FullFrame(frame, ArrayList(locals), stack) + } + return result + } + + /** + * A method that overrides one whose calls [decide] retypes, SQLiteQueryBuilder's `query` with a + * CancellationSignal: the calls get requery's descriptor and the declaration keeps Android's, so they would + * reach requery's inherited method and skip the override. Retyping the declaration too would mean turning + * requery's signal back into Android's in a body written for Android's, so the build fails instead. + */ + fun bypassedOverride(m: MethodNode): String? { + if (m.access and (Opcodes.ACC_STATIC or Opcodes.ACC_PRIVATE) != 0 || m.name !in Rules.DESCRIPTOR_RULE_NAMES) return null + val ancestors = cn.superName?.let { renamedAncestors(it) }.orEmpty() + val rule = Rules.DESCRIPTOR_RULES.firstOrNull { it.owner in ancestors && it.name == m.name && it.desc == m.desc } ?: return null + return "method '${m.name}${m.desc}' overrides ${simple(rule.owner)}.${m.name}, whose calls the rewrite gives requery's " + + "descriptor; they would reach requery's method and skip the override" + } + + fun checkDeclarations() { + for (f in cn.fields) { + if (f.desc.contains(Rules.OPEN_PARAMS)) violations += "field '${f.name}' has type OpenParams, which requery lacks" + } + for (m in cn.methods) { + if (m.desc.contains(Rules.OPEN_PARAMS)) { + violations += "method '${m.name}${m.desc}' uses OpenParams, which requery lacks" + } + } + } + + private val ancestorsCache = HashMap>() + private val superTypesCache = HashMap?>() + + // not getOrPut, which asks again for an owner that cannot be resolved (null) + private fun superTypes(owner: String): Collection? = + if (owner in superTypesCache) superTypesCache[owner] else hierarchy.superTypes(owner).also { superTypesCache[owner] = it } + + /** Owners that cannot extend a renamed class: the JDK, Kotlin, and Android outside the renamed set. */ + private fun cannotBeSqlite(owner: String): Boolean = + owner.startsWith("java/") || owner.startsWith("javax/") || owner.startsWith("kotlin/") || owner.startsWith("kotlinx/") || + (owner.startsWith("android/") && owner !in Rules.SQLITE_CLASSES) + + /** + * The renamed classes that [owner] is or extends, nearest first, so the rule tables see them all. None for an + * owner that cannot be resolved: [unresolvedOwner] reports the calls a rule could match on one. + */ + fun renamedAncestors(owner: String): List { + if (owner.startsWith("[")) return emptyList() + return ancestorsCache.getOrPut(owner) { + val direct = + when { + owner in Rules.SQLITE_CLASSES -> listOf(owner) + cannotBeSqlite(owner) -> emptyList() + else -> superTypes(owner)?.filter { it in Rules.SQLITE_CLASSES }.orEmpty() + } + direct.flatMap { listOf(it) + Rules.RENAMED_SUPERS[it].orEmpty() }.distinct() + } + } + + fun isContext(owner: String): Boolean = owner in Rules.CONTEXT_FAMILY || superTypes(owner)?.contains(Rules.CONTEXT) == true + + fun rewriteMethod(m: MethodNode) { + val tainted = if (policy.fail) cursorCastTaint(m) else emptySet() + val conversions = if (policy.fail) implicitCursorConversions(m) else emptyMap() + var line = -1 + var insn: AbstractInsnNode? = m.instructions.first + while (insn != null) { + val following = insn.next + if (insn is LineNumberNode) { + line = insn.line + } + conversions[insn]?.let { violate(m, line, it) } + when (insn) { + is MethodInsnNode -> { + methodInsn(m, insn, line) + } + + is FieldInsnNode -> { + fieldInsn(m, insn, line) + } + + is TypeInsnNode -> { + typeInsn(m, insn, line, tainted) + } + + is LdcInsnNode -> { + (insn.cst as? Type)?.let { + if (it.descriptor.contains(Rules.OPEN_PARAMS)) violate(m, line, "OpenParams, which requery lacks") + } + (insn.cst as? String)?.let { sqlConstant(m, line, it) } + } + + is InvokeDynamicInsnNode -> { + invokeDynamic(m, insn, line) + } + } + insn = following + } + } + + /** requery's native SQLite has no ICU: there is no UNICODE collator, and LOCALIZED compares bytes. */ + fun sqlConstant( + m: MethodNode, + line: Int, + text: String, + ) { + if (!policy.fail) return + if (Rules.COLLATE_UNICODE.containsMatchIn(text)) { + violate( + m, + line, + "SQL text uses COLLATE UNICODE, which does not exist in requery's SQLite (no ICU); the statement would fail", + ) + } + if (Rules.COLLATE_LOCALIZED.containsMatchIn(text)) { + warn( + "${site(m, line)}: COLLATE LOCALIZED works in requery's SQLite but sorts by " + + "byte value (no ICU), not by the device locale", + ) + } + // A warning, not a failure: the string may only be printed. Strings are not rewritten, so a class loaded by + // this name is Android's, which the rewritten code cannot mix with requery's. + Rules.RENAMED_CLASS_NAME.find(text)?.let { + warn( + "${site(m, line)}: the string \"${it.value}\" names a class the rewrite moves " + + "to requery. Strings are not rewritten: Class.forName or reflection on it reaches Android's class, not requery's", + ) + } + } + + fun where(line: Int) = if (line >= 0) "line $line" else "no line number" + + /** Where a warning points: class, method and line, as `p.Dao.query (line 12)`. */ + fun site( + m: MethodNode, + line: Int, + ) = "${cn.name.replace('/', '.')}.${m.name} (${where(line)})" + + fun invokeDynamic( + m: MethodNode, + insn: InvokeDynamicInsnNode, + line: Int, + ) { + if (insn.desc.contains(Rules.OPEN_PARAMS)) violate(m, line, "OpenParams, which requery lacks") + for (arg in insn.bsmArgs) { + when (arg) { + is String -> sqlConstant(m, line, arg) + is Handle -> handle(m, line, arg) + is Type -> if (arg.descriptor.contains(Rules.OPEN_PARAMS)) violate(m, line, "OpenParams, which requery lacks") + } + } + } + + fun violate( + m: MethodNode, + line: Int, + what: String, + ) { + if (!policy.fail) return + violations += "${m.name}${m.desc} (${where(line)}): $what" + } + + fun fieldInsn( + m: MethodNode, + f: FieldInsnNode, + line: Int, + ) { + fieldProblem(m, line, f.owner, f.name, f.desc)?.let { violate(m, line, it) } + } + + /** [m] and [line] locate the access, for a warning. */ + fun fieldProblem( + m: MethodNode, + line: Int, + owner: String, + name: String, + desc: String, + ): String? { + if (desc.contains(Rules.OPEN_PARAMS) || owner == Rules.OPEN_PARAMS) { + return "OpenParams, which requery lacks" + } + // only SQLiteCursor.mWindow is listed today; skip the hierarchy lookup for every other field + if (name !in Rules.MISSING_FIELD_NAMES) return null + return renamedAncestors(owner) + .firstOrNull { Rules.MISSING_MEMBERS["$it.$name"]?.contains(desc) == true } + ?.let { "${simple(it)}.$name does not exist in requery" } + ?: unresolvedOwner(m, line, owner, name, desc) + } + + /** + * Why `owner.name desc` cannot be checked and must fail the build, or null: [owner] cannot be resolved, so its + * renamed ancestors are unknown, the member has the name and descriptor of one a rule changes or rejects in a + * renamed class another class can extend, and the descriptor names an SQLite-related type. Like a Context + * database method on an unresolved owner, this fails under the full policy rather than pass a call that may + * break at run time. A match on name and descriptor alone (isStrict()Z, isString(I)Z, setWindow(CursorWindow)) + * is only a warning, naming class, method and [line] of [m]: libraries reference optional classes the app does + * not ship, and most of them are not SQLite classes. The same warning at the same place is logged once. + */ + fun unresolvedOwner( + m: MethodNode, + line: Int, + owner: String, + name: String, + desc: String, + ): String? { + val declaring = Rules.INHERITABLE_RULE_MEMBERS[name + desc] ?: return null + if (owner.startsWith("[") || owner in Rules.SQLITE_CLASSES || cannotBeSqlite(owner) || RewritePolicy.isExcluded(owner)) { + return null + } + if (superTypes(owner) != null) return null + val cannot = "${owner.replace('/', '.')} cannot be resolved, so the rewrite cannot tell whether its $name$desc is " + if (Rules.SQLITE_DESCRIPTOR_TOKENS.any { desc.contains(it) }) { + return cannot + "${simple(declaring)}.$name, which it changes or rejects" + } + if (policy.fail) { + val use = if (desc.startsWith("(")) "call" else "field access" + val message = + "${site(m, line)}: $cannot${simple(declaring)}.$name, which it changes or rejects; " + + "the $use is left as it is, and fails at run time if the owner extends that class" + if (warned.add(message)) warn(message) + } + return null + } + + fun typeInsn( + m: MethodNode, + t: TypeInsnNode, + line: Int, + tainted: Set, + ) { + if (t.desc.startsWith(Rules.OPEN_PARAMS)) { + violate(m, line, "OpenParams, which requery lacks") + } + if (t.opcode == Opcodes.CHECKCAST && t.desc in Rules.ANDROID_ONLY_CURSOR_TYPES && t in tainted) { + violate( + m, + line, + "cast of an SQLite cursor to ${t.desc.replace('/', '.')}; requery's cursors are not instances of it, " + + "so the cast would throw", + ) + } + } + + /** What the rules do with one call or method handle. */ + sealed class Action { + object None : Action() + + class Fail( + val what: String, + ) : Action() + + /** Replace the call by an INVOKESTATIC of the runtime helper. */ + class Redirect( + val name: String, + val desc: String, + ) : Action() + + /** Same call, new descriptor; [signal] inserts the CancellationSignal conversion. */ + class Retype( + val desc: String, + val signal: Boolean, + ) : Action() + } + + /** The platform methods on Context that are redirected: name, then the descriptors they match. */ + private fun isContextDatabaseMethod( + name: String, + desc: String, + ) = (name == "openOrCreateDatabase" && (desc == Rules.CONTEXT_OPEN_DESC || desc == Rules.CONTEXT_OPEN_HANDLER_DESC)) || + (name == "deleteDatabase" && desc == Rules.CONTEXT_DELETE_DESC) + + /** + * Decides, without changing anything, what happens to `owner.name desc` invoked with [opcode] + * (the opcode a method handle's kind stands for). Redirects and retyping apply only when the + * policy allows; a [Action.Fail] is reported only when it fails. [m] and [line] locate the call, for a warning. + */ + fun decide( + m: MethodNode, + line: Int, + opcode: Int, + owner: String, + name: String, + desc: String, + ): Action { + if (owner == Rules.OPEN_PARAMS || desc.contains(Rules.OPEN_PARAMS)) { + return Action.Fail("OpenParams, which requery lacks") + } + if (policy.redirect) { + redirectTarget(opcode, owner, name, desc)?.let { return it } + if (name == Rules.DRIVER_QUERY_NAME && desc == Rules.DRIVER_QUERY_DESC) { + return Action.Retype(Rules.DRIVER_QUERY_NEW_DESC, false) + } + } + if (isContextDatabaseMethod(name, desc) && !owner.startsWith("[")) { + // the redirect above did not take it: super.* calls and owners that are not known to be a Context + if (opcode == Opcodes.INVOKESPECIAL && isContext(owner)) { + return Action.Fail( + "super.$name on a Context cannot be redirected to requery; call it on this or on a Context instead", + ) + } + if (superTypes(owner) == null && owner !in Rules.CONTEXT_FAMILY) { + return Action.Fail( + "$owner.$name is a Context database method if $owner is a Context, " + + "but $owner cannot be resolved, so it cannot be redirected to requery", + ) + } + } + + // the lookup is skipped for owners no rule below can match, which is nearly every call + val ancestors = if (needsAncestors(owner, name, desc)) renamedAncestors(owner) else emptyList() + if (ancestors.isEmpty()) { + if (owner.startsWith("android/") && mentionsRenamed(desc)) { + return Action.Fail( + "${simple(owner)}.$name$desc takes or returns an SQLite class that requery replaces; " + + "the platform method would not link", + ) + } + unresolvedOwner(m, line, owner, name, desc)?.let { return Action.Fail(it) } + return Action.None + } + for (c in ancestors) { + if (Rules.MISSING_MEMBERS["$c.$name"]?.contains(desc) == true) { + return Action.Fail("${simple(c)}.$name$desc does not exist in requery") + } + } + if (desc.contains(Rules.CURSOR_WINDOW)) { + return Action.Fail("${simple(ancestors.first())}.$name uses android.database.CursorWindow; requery has its own") + } + if (!policy.redirect) return Action.None + val rule = + Rules.DESCRIPTOR_RULES.firstOrNull { it.owner in ancestors && it.name == name && it.desc == desc } ?: return Action.None + val args = Type.getArgumentTypes(desc).map { it.descriptor }.toMutableList() + rule.objectArrayParams.forEach { args[it] = "[Ljava/lang/Object;" } + val signal = args.last() == "L${Rules.ANDROID_SIGNAL};" + if (signal) { + args[args.lastIndex] = "L${Rules.CORE_SIGNAL};" + } + return Action.Retype("(" + args.joinToString("") + ")" + Type.getReturnType(desc).descriptor, signal) + } + + fun methodInsn( + m: MethodNode, + mi: MethodInsnNode, + line: Int, + ) { + when (val action = decide(m, line, mi.opcode, mi.owner, mi.name, mi.desc)) { + Action.None -> {} + + is Action.Fail -> { + violate(m, line, action.what) + } + + is Action.Redirect -> { + mi.setOpcode(Opcodes.INVOKESTATIC) + mi.owner = Rules.HELPER + mi.name = action.name + mi.desc = action.desc + mi.itf = false + } + + is Action.Retype -> { + if (action.signal) { + // requery checks androidx.core's signal, so a cancelled query throws androidx.core's + // OperationCanceledException, which a catch of Android's does not catch (see the docs) + m.instructions.insertBefore( + mi, + MethodInsnNode(Opcodes.INVOKESTATIC, Rules.HELPER, "toCancellationSignal", Rules.HELPER_SIGNAL_DESC, false), + ) + } + mi.desc = action.desc + } + } + } + + /** + * A method handle among an invokedynamic's bootstrap arguments (`db::rawQuery`) points at a + * member the call rules would change or reject, but the handle itself cannot be patched in + * place the way an instruction is. Such handles fail the build with a hint to write a lambda, + * whose body is an ordinary call. + */ + fun handle( + m: MethodNode, + line: Int, + h: Handle, + ) { + val what = + when (h.tag) { + Opcodes.H_GETFIELD, Opcodes.H_GETSTATIC, Opcodes.H_PUTFIELD, Opcodes.H_PUTSTATIC -> { + fieldProblem(m, line, h.owner, h.name, h.desc) + } + + else -> { + val opcode = + when (h.tag) { + Opcodes.H_INVOKESTATIC -> Opcodes.INVOKESTATIC + Opcodes.H_INVOKEINTERFACE -> Opcodes.INVOKEINTERFACE + Opcodes.H_INVOKEVIRTUAL -> Opcodes.INVOKEVIRTUAL + else -> Opcodes.INVOKESPECIAL + } + when (val action = decide(m, line, opcode, h.owner, h.name, h.desc)) { + Action.None -> null + is Action.Fail -> action.what + is Action.Redirect -> "the runtime takes this call over" + is Action.Retype -> "its descriptor changes in requery" + } + } + } + if (what != null) { + violate(m, line, "method reference ${simple(h.owner)}::${h.name} cannot be rewritten ($what); use a lambda instead") + } + } + + /** Returns the replacement for a call the runtime helper takes over, or null. */ + fun redirectTarget( + opcode: Int, + owner: String, + name: String, + desc: String, + ): Action.Redirect? = + when { + opcode == Opcodes.INVOKESTATIC && owner == Rules.DATABASE && name == "openDatabase" && + (desc == Rules.HELPER_OPEN_DESC || desc == Rules.HELPER_OPEN_HANDLER_DESC) -> { + Action.Redirect(name, desc) + } + + opcode == Opcodes.INVOKEVIRTUAL && name == "openOrCreateDatabase" && + (desc == Rules.CONTEXT_OPEN_DESC || desc == Rules.CONTEXT_OPEN_HANDLER_DESC) && isContext(owner) -> { + Action.Redirect(name, "(L${Rules.CONTEXT};" + desc.substring(1)) + } + + opcode == Opcodes.INVOKEVIRTUAL && name == "deleteDatabase" && desc == Rules.CONTEXT_DELETE_DESC && isContext(owner) -> { + Action.Redirect(name, "(L${Rules.CONTEXT};Ljava/lang/String;)Z") + } + + opcode == Opcodes.INVOKESTATIC && owner == Rules.DATABASE_UTILS && (name + desc) in Rules.REDIRECTED_UTILS -> { + Action.Redirect(name, desc) + } + + else -> { + null + } + } + + fun needsAncestors( + owner: String, + name: String, + desc: String, + ) = owner in Rules.SQLITE_CLASSES || name in Rules.MISSING_MEMBER_NAMES || name in Rules.DESCRIPTOR_RULE_NAMES || + desc.contains(Rules.CURSOR_WINDOW) + + fun mentionsRenamed(desc: String) = desc.contains("android/database/") && Rules.RENAMED_DESCRIPTOR_TOKENS.any { desc.contains(it) } + + fun simple(name: String) = name.substringAfterLast('/') + + /** SQLiteCursor, or a class that extends it, which the rename makes a subclass of requery's SQLiteCursor. */ + fun isRenamedCursorType(type: Type?): Boolean = + type != null && type.sort == Type.OBJECT && type.descriptor != "Lnull;" && + Rules.SQLITE_CURSOR in renamedAncestors(type.internalName) + + /** [type]'s internal name when it is one of the Android-only cursor types, or null. */ + private fun androidOnlyCursor(type: Type): String? = if (type.sort == Type.OBJECT) androidOnlyCursor(type.internalName) else null + + /** A type in a frame or an internal name, when it is one of the Android-only cursor types, or null. */ + private fun androidOnlyCursor(entry: Any?): String? = (entry as? String)?.takeIf { it in Rules.ANDROID_ONLY_CURSOR_TYPES } + + /** [type]'s element type when it is a one-dimensional array of an Android-only cursor type, or null. */ + private fun androidCursorArray(type: Type?): String? = + if (type != null && type.sort == Type.ARRAY && type.dimensions == 1) androidOnlyCursor(type.elementType) else null + + /** Whether [m] names an Android-only cursor type where a value could be converted to it without a cast. */ + private fun mayConvertToAndroidCursor(m: MethodNode): Boolean { + fun names(desc: String) = Rules.ANDROID_ONLY_CURSOR_TYPES.any { desc.contains("L$it;") } + // the parameters too: an array parameter is all that a store into one needs + if (names(m.desc)) return true + return m.instructions.any { insn -> + when (insn) { + is MethodInsnNode -> insn.owner in Rules.ANDROID_ONLY_CURSOR_TYPES || names(insn.desc) + is InvokeDynamicInsnNode -> names(insn.desc) + is FieldInsnNode -> names(insn.desc) + is TypeInsnNode -> names(insn.desc) || (insn.opcode == Opcodes.ANEWARRAY && androidOnlyCursor(insn.desc) != null) + is FrameNode -> (insn.local.orEmpty() + insn.stack.orEmpty()).any { androidOnlyCursor(it) != null } + else -> false + } + } + } + + /** + * The instructions where a value whose static type is SQLiteCursor, or a class that extends it, is used as an + * AbstractWindowedCursor, AbstractCursor or CrossProcessCursor without a cast, by the message for each: returned, + * passed, called on, stored in a field, or held where a frame declares one of them. javac writes no CHECKCAST + * for such an upcast, and requery's SQLiteCursor neither extends those classes nor implements the interface, so + * the renamed class would not verify (or, for the interface, the call would fail). Also an AASTORE of such a value + * into an array that is, on every path to it, one of those types' arrays (from a parameter, field, call or + * ANEWARRAY): it would throw ArrayStoreException. Not checked: stores into a Cursor[] or Object[], allowed on + * purpose although one can be such an array at run time; a store whose array type differs between the paths + * that merge before it; values loaded from arrays, which are not followed. + */ + fun implicitCursorConversions(m: MethodNode): Map { + if (!mayConvertToAndroidCursor(m)) return emptyMap() + val interpreter = + object : BasicInterpreter(Opcodes.ASM9) { + override fun newValue(type: Type?): BasicValue? { + val v = super.newValue(type) ?: return null + return if (v.isReference) StaticCursorValue(v.type, isRenamedCursorType(type), androidCursorArray(type)) else v + } + + override fun merge( + value1: BasicValue, + value2: BasicValue, + ): BasicValue { + val s1 = value1 as? StaticCursorValue + val s2 = value2 as? StaticCursorValue + // BasicInterpreter.merge answers UNINITIALIZED for values that differ in either flag + if ((s1 != null || s2 != null) && value1.type != null && value1.type == value2.type) { + val merged = + StaticCursorValue( + value1.type, + s1?.renamedCursor == true || s2?.renamedCursor == true, + s1?.androidCursorArray?.takeIf { it == s2?.androidCursorArray }, + ) + return if (merged == value1) value1 else merged + } + return super.merge(value1, value2) + } + } + val frames: Array?> = + try { + Analyzer(interpreter).analyze(cn.name, m) + } catch (e: AnalyzerException) { + return emptyMap() + } + val declared = fullFrames(m, m.desc).associateBy { it.node } + val returnType = Type.getReturnType(m.desc) + val result = LinkedHashMap() + for ((i, insn) in m.instructions.withIndex()) { + val f = frames[i] ?: continue + + fun renamed(value: BasicValue?) = (value as? StaticCursorValue)?.renamedCursor == true + + fun fromTop(depth: Int) = if (depth < f.stackSize) f.getStack(f.stackSize - 1 - depth) else null + if (insn.opcode == Opcodes.AASTORE) { + val element = (fromTop(2) as? StaticCursorValue)?.androidCursorArray + if (element != null && renamed(fromTop(0))) { + result[insn] = "an SQLite cursor stored into an array of ${element.replace('/', '.')}; requery's SQLiteCursor " + + "is not one, so the store would throw ArrayStoreException" + } + continue + } + val target: String? = + when { + insn is MethodInsnNode || insn is InvokeDynamicInsnNode -> { + val args = Type.getArgumentTypes((insn as? MethodInsnNode)?.desc ?: (insn as InvokeDynamicInsnNode).desc) + args.indices.firstNotNullOfOrNull { k -> + androidOnlyCursor(args[k])?.takeIf { renamed(fromTop(args.size - 1 - k)) } + } + ?: (insn as? MethodInsnNode) + ?.takeIf { + it.opcode != Opcodes.INVOKESTATIC && it.owner in Rules.ANDROID_ONLY_CURSOR_TYPES && + renamed(fromTop(args.size)) + }?.owner + } + + insn is FieldInsnNode && (insn.opcode == Opcodes.PUTFIELD || insn.opcode == Opcodes.PUTSTATIC) -> { + androidOnlyCursor(Type.getType(insn.desc))?.takeIf { renamed(fromTop(0)) } + } + + insn.opcode == Opcodes.ARETURN -> { + androidOnlyCursor(returnType)?.takeIf { renamed(fromTop(0)) } + } + + insn is FrameNode -> { + declared[insn]?.let { full -> + var slot = 0 + var hit: String? = null + for (t in full.locals) { + if (hit == null && slot < f.locals && renamed(f.getLocal(slot))) hit = androidOnlyCursor(t) + slot += if (t == Opcodes.LONG || t == Opcodes.DOUBLE) 2 else 1 + } + hit ?: full.stack.indices.firstNotNullOfOrNull { k -> + androidOnlyCursor(full.stack[k])?.takeIf { k < f.stackSize && renamed(f.getStack(k)) } + } + } + } + + else -> { + null + } + } + if (target != null) { + result[insn] = "an SQLite cursor used as ${target.replace('/', '.')} without a cast; requery's SQLiteCursor is not " + + "one, so the rewritten class would not verify or the call would fail" + } + } + return result + } + + /** + * The CHECKCASTs to AbstractWindowedCursor whose operand came from an SQLite API (a call on + * one of the renamed classes returning a cursor, or a parameter or instance field declared as + * SQLiteCursor), through locals and the stack, unless an `instanceof` of the cast's type proved + * the operand on every path to the cast (see [GuardFrame]): `if (c instanceof X) ((X) c)`, `&&`, + * `?:` and an early exit when it is false, but not `b || c instanceof X`, which reaches the cast + * with `b` alone. Approximate: values that went through other calls, array elements or static + * fields are not followed (a miss). + */ + fun cursorCastTaint(m: MethodNode): Set { + val casts = + m.instructions.filter { + it is TypeInsnNode && it.opcode == Opcodes.CHECKCAST && it.desc in Rules.ANDROID_ONLY_CURSOR_TYPES + } + if (casts.isEmpty()) return emptySet() + val interpreter = + object : BasicInterpreter(Opcodes.ASM9) { + override fun newValue(type: Type?): BasicValue? = + super.newValue(type)?.let { + if (it.type == + null + ) { + it + } else { + TaintValue(it.type, false) + } + } + + override fun newParameterValue( + isInstanceMethod: Boolean, + local: Int, + type: Type?, + ): BasicValue? { + val r = super.newParameterValue(isInstanceMethod, local, type) + return if (r != null && type?.descriptor == "L${Rules.SQLITE_CURSOR};") TaintValue(r.type, true) else r + } + + override fun unaryOperation( + insn: AbstractInsnNode, + value: BasicValue, + ): BasicValue? { + val r = super.unaryOperation(insn, value) + if (insn is FieldInsnNode && r != null && insn.desc == "L${Rules.SQLITE_CURSOR};") { + return TaintValue(r.type, true) + } + if (insn.opcode == Opcodes.CHECKCAST && r != null) { + val castTo = (insn as TypeInsnNode).desc + val operand = value as? TaintValue + return TaintValue( + r.type, + operand?.sqliteCursor == true || castTo == Rules.SQLITE_CURSOR, + operand?.instanceOf.orEmpty(), + ) + } + return r + } + + override fun naryOperation( + insn: AbstractInsnNode, + values: MutableList, + ): BasicValue? { + val r = super.naryOperation(insn, values) + if (insn is MethodInsnNode && r != null && r.type != null) { + val ret = Type.getReturnType(insn.desc) + val cursorLike = + ret.sort == Type.OBJECT && + (ret.internalName == "android/database/Cursor" || ret.internalName == Rules.SQLITE_CURSOR) + if (cursorLike && (renamedAncestors(insn.owner).isNotEmpty() || insn.owner == Rules.CURSOR_FACTORY)) { + return TaintValue(r.type, true) + } + } + return r + } + + override fun merge( + value1: BasicValue, + value2: BasicValue, + ): BasicValue { + val t1 = value1 as? TaintValue + val t2 = value2 as? TaintValue + // BasicInterpreter.merge answers UNINITIALIZED for values that differ, taint and proofs included + if ((t1 != null || t2 != null) && value1.type != null && value1.type == value2.type) { + val merged = + TaintValue( + value1.type, + t1?.sqliteCursor == true || t2?.sqliteCursor == true, + t1?.instanceOf.orEmpty() intersect t2?.instanceOf.orEmpty(), + ) + return if (merged == value1) value1 else merged + } + return super.merge(value1, value2) + } + } + val analyzer = + object : Analyzer(interpreter) { + override fun newFrame( + numLocals: Int, + numStack: Int, + ): Frame = GuardFrame(numLocals, numStack) + + override fun newFrame(frame: Frame): Frame = GuardFrame(frame) + } + val frames: Array?> = + try { + analyzer.analyze(cn.name, m) + } catch (e: AnalyzerException) { + return emptySet() + } + val insns = m.instructions + return casts + .filter { cast -> + val frame = frames[insns.indexOf(cast)] + val operand = frame?.let { it.getStack(it.stackSize - 1) } as? TaintValue + operand != null && operand.sqliteCursor && (cast as TypeInsnNode).desc !in operand.instanceOf + }.toSet() + } + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/RuntimeConfigurationTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/RuntimeConfigurationTest.kt new file mode 100644 index 0000000000..506e1b8ddb --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/RuntimeConfigurationTest.kt @@ -0,0 +1,42 @@ +package com.itsaky.androidide.gradle + +import com.android.build.api.variant.ApplicationVariant +import com.google.common.truth.Truth.assertThat +import org.gradle.api.GradleException +import org.junit.jupiter.api.Test +import java.lang.reflect.Proxy + +/** + * A variant type the helper does not know cannot be given the SQLite runtime dependencies, and a + * build that went on would crash on the device (NoClassDefFoundError). A TestKit build cannot produce + * such a variant, so this tests the decision function with a stand-in variant. + */ +class RuntimeConfigurationTest { + private val unknownVariant = + Proxy.newProxyInstance( + javaClass.classLoader, + arrayOf(ApplicationVariant::class.java), + ) { _, method, _ -> + if (method.name == "getName") "stagingDebug" else null + } as ApplicationVariant + + @Test + fun `requireRuntimeConfiguration fails naming the variant and its class`() { + var ran = false + val failure = runCatching { unknownVariant.requireRuntimeConfiguration { ran = true } }.exceptionOrNull() + + assertThat(failure).isInstanceOf(GradleException::class.java) + assertThat(failure!!.message).contains("variant 'stagingDebug'") + assertThat(failure.message).contains(unknownVariant.javaClass.name) + assertThat(ran).isFalse() + } + + @Test + fun `withRuntimeConfiguration still skips an unknown variant for LogSender`() { + var ran = false + unknownVariant.withRuntimeConfiguration { ran = true } + + assertThat(ran).isFalse() + assertThat(unknownVariant.runtimeConfigurationOrNull()).isNull() + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteBuildSupport.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteBuildSupport.kt new file mode 100644 index 0000000000..4024a65278 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteBuildSupport.kt @@ -0,0 +1,68 @@ +package com.itsaky.androidide.gradle + +import java.io.File +import java.nio.file.Path +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + +/** Prints what each variant's runtime classpath declares, without resolving anything. */ +internal fun printDepsInitScript(dir: File): File = + File(dir, "print-deps.init.gradle").apply { + writeText( + """ + allprojects { p -> + p.afterEvaluate { + if (p.plugins.hasPlugin('com.android.application')) { + p.tasks.register('printRuntimeDeps') { + doLast { + ['demoDebug', 'fullDebug', 'demoRelease', 'fullRelease'].each { v -> + p.configurations.getByName(v + 'RuntimeClasspath').allDependencies.each { d -> + if (d instanceof org.gradle.api.artifacts.FileCollectionDependency) { + d.files.each { println "RTDEP ${'$'}v file ${'$'}{it.name}" } + } else { + println "RTDEP ${'$'}v ${'$'}{d.group}:${'$'}{d.name}:${'$'}{d.version}" + } + } + } + } + } + } + } + } + """.trimIndent(), + ) + } + +/** A jar or AAR file with the given entries, for stand-ins whose class bytes nobody reads. */ +internal fun standIn( + file: File, + vararg entries: Pair, +): File { + file.parentFile.mkdirs() + ZipOutputStream(file.outputStream()).use { zip -> + for ((name, bytes) in entries) { + zip.putNextEntry(ZipEntry(name)) + zip.write(bytes) + zip.closeEntry() + } + } + return file +} + +/** A copy of the sample project in [into], without build output, with [extraBuildScript] appended to the app's script. */ +internal fun copySampleProject( + into: File, + extraBuildScript: String, +): Path { + val source = openProject().toFile() + val dest = File(into, "project") + source + .walkTopDown() + .onEnter { it.name != "build" && it.name != ".gradle" } + .forEach { file -> + val target = File(dest, file.relativeTo(source).path) + if (file.isDirectory) target.mkdirs() else file.copyTo(target, overwrite = true) + } + File(dest, "app/build.gradle.kts").appendText("\n" + extraBuildScript + "\n") + return dest.toPath() +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteDebuggerPluginTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteDebuggerPluginTest.kt new file mode 100644 index 0000000000..38c5146d07 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteDebuggerPluginTest.kt @@ -0,0 +1,147 @@ +package com.itsaky.androidide.gradle + +import com.android.build.api.variant.ApplicationVariant +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_QUICK_BUILD_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_QUICK_BUILD_RUNTIME_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR +import org.adfa.constants.KOTLIN_VERSION +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.UnexpectedBuildResultException +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File +import java.lang.reflect.Proxy + +/** + * The SQLite debugger (ADFA-7131) reaches the debuggable variants' runtime classpath, and only + * when the project opts in. The fixture project has the debuggable `demoDebug`/`fullDebug` and the + * non-debuggable `demoRelease`/`fullRelease` variants. + */ +class SqliteDebuggerPluginTest { + private val aarName = "sqlite-debugger-release.aar" + private val stdlib = "org.jetbrains.kotlin:kotlin-stdlib:$KOTLIN_VERSION" + private val debugVariants = listOf("demoDebug", "fullDebug") + private val releaseVariants = listOf("demoRelease", "fullRelease") + + private fun build( + dir: File, + enabled: String?, + aar: File?, + quickBuildAar: File? = null, + runtimeAar: File? = File(dir, "sqlite-rewrite-runtime-release.aar").apply { writeText("aar") }, + ): BuildResult = + buildProject(task = ":app:printRuntimeDeps", configureArgs = { + it += "--init-script" + it += printDepsInitScript(dir).absolutePath + if (enabled != null) it += "-P$PROPERTY_SQLITE_DEBUGGER_ENABLED=$enabled" + if (aar != null) it += "-P$PROPERTY_SQLITE_DEBUGGER_AAR=${aar.absolutePath}" + if (runtimeAar != null) it += "-P$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR=${runtimeAar.absolutePath}" + if (quickBuildAar != null) { + it += "-P$PROPERTY_QUICK_BUILD_ENABLED=true" + it += "-P$PROPERTY_QUICK_BUILD_RUNTIME_AAR=${quickBuildAar.absolutePath}" + } + }) + + private fun BuildResult.declared(variant: String) = output.lines().filter { it.startsWith("RTDEP $variant ") } + + @Test + fun `enabled adds the AAR and kotlin-stdlib to debuggable variants only`( + @TempDir dir: File, + ) { + val aar = File(dir, aarName).apply { writeText("aar") } + val result = build(dir, "true", aar) + + assertThat(result.output).contains("Applying SqliteDebuggerPlugin to project ':app'") + for (variant in debugVariants) { + assertThat(result.declared(variant)).contains("RTDEP $variant file $aarName") + assertThat(result.declared(variant)).contains("RTDEP $variant $stdlib") + assertThat(result.output) + .contains("Adding SQLite debugger dependency to variant '$variant' of project ':app'") + } + for (variant in releaseVariants) { + // The fixture's own libraries declare other kotlin-stdlib versions in every variant + assertThat(result.declared(variant).filter { it.contains(aarName) || it.endsWith(stdlib) }) + .isEmpty() + assertThat(result.output) + .doesNotContain("Adding SQLite debugger dependency to variant '$variant' of project ':app'") + } + } + + @Test + fun `debuggable is read from the variant itself, not from a builder`() { + fun variant(debuggable: Boolean): ApplicationVariant = + Proxy.newProxyInstance( + ApplicationVariant::class.java.classLoader, + arrayOf(ApplicationVariant::class.java), + ) { _, method, _ -> + when (method.name) { + "getDebuggable" -> debuggable + else -> throw UnsupportedOperationException(method.name) + } + } as ApplicationVariant + + assertThat(SqliteDebuggerPlugin.readDebuggable(variant(true))).isTrue() + assertThat(SqliteDebuggerPlugin.readDebuggable(variant(false))).isFalse() + } + + @Test + fun `quick build is not applied to a project that opted into the debugger`( + @TempDir dir: File, + ) { + val aar = File(dir, aarName).apply { writeText("aar") } + val runtime = File(dir, "quickbuild-runtime.aar").apply { writeText("aar") } + + val withDebugger = build(dir, "true", aar, runtime) + assertThat(withDebugger.output).contains(AndroidIDEGradlePlugin.QUICK_BUILD_SQLITE_DEBUGGER_MESSAGE) + assertThat(withDebugger.output).doesNotContain("Configuring Quick Build for variant") + assertThat(withDebugger.output).contains("Adding SQLite debugger dependency to variant 'demoDebug'") + + // the control: without the opt-in the same invocation configures Quick Build + val without = build(dir, null, aar, runtime) + assertThat(without.output).doesNotContain(AndroidIDEGradlePlugin.QUICK_BUILD_SQLITE_DEBUGGER_MESSAGE) + assertThat(without.output).contains("Configuring Quick Build for variant") + } + + @Test + fun `a property that is not true adds nothing`( + @TempDir dir: File, + ) { + val aar = File(dir, aarName).apply { writeText("aar") } + for (enabled in listOf(null, "false")) { + val result = build(dir, enabled, aar) + + assertThat(result.output).doesNotContain("Applying SqliteDebuggerPlugin") + for (variant in debugVariants + releaseVariants) { + assertThat(result.declared(variant).filter { it.contains(aarName) }).isEmpty() + } + } + } + + @Test + fun `enabled without the AAR property fails the build and says how to fix it`( + @TempDir dir: File, + ) { + val failure = runCatching { build(dir, "true", null) }.exceptionOrNull() + + assertThat(failure).isInstanceOf(UnexpectedBuildResultException::class.java) + assertThat(failure!!.message).contains("no property '$PROPERTY_SQLITE_DEBUGGER_AAR' is set") + } + + @Test + fun `enabled with a missing AAR file fails the build and says how to fix it`( + @TempDir dir: File, + ) { + val failure = runCatching { build(dir, "true", File(dir, "absent.aar")) }.exceptionOrNull() + + assertThat(failure).isInstanceOf(UnexpectedBuildResultException::class.java) + assertThat(failure!!.message).contains("SQLite debugger AAR not found") + // app init extracts the AAR in the background, so right after a fresh install it can be missing for a while + assertThat(failure.message).contains( + "Remove '$PROPERTY_SQLITE_DEBUGGER_ENABLED' from gradle.properties to build without it, " + + "or retry in a few seconds if Code on the Go has just started.", + ) + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt new file mode 100644 index 0000000000..b82e66bd76 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt @@ -0,0 +1,89 @@ +package com.itsaky.androidide.gradle + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import com.itsaky.androidide.gradle.sqlite.ProcessResult +import com.itsaky.androidide.gradle.sqlite.requireEnvironment +import com.itsaky.androidide.gradle.sqlite.runBounded +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR +import com.itsaky.androidide.utils.FileProvider +import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File + +/** + * The release-APK guard (ADFA-7138): an app that opted into the SQLite debugger gets it in its debuggable + * variants only. Builds the sample app's minified release and its debug variant with the real debugger, + * rewrite runtime and requery AARs, and runs scripts/check-release-apk-no-sqlite-debugger.sh on both APKs. + * + * The debug APK is the control: it must fail the check, which shows the check still sees the debugger the + * AAR brings. R8 renames and drops classes in a release, so the dex grep of the script can miss them; the + * manifest provider and libsqlitedbg.so are what the control requires to fail. + * + * Run by the sqliteReleaseApkTest task, which builds the AARs and passes their paths. Run without an AAR's path, as + * from an IDE, it skips with a message, and fails instead when the environment variable CI is set (requireEnvironment). + */ +class SqliteReleaseApkTest { + private fun aar(name: String): File { + val path = System.getProperty("cogo.test.sqlite.${name}Aar") + requireEnvironment(path != null && File(path).isFile, "the $name AAR: run the :gradle-plugin:sqliteReleaseApkTest task") + return File(path!!) + } + + private fun checkApk(apk: File): ProcessResult { + assertWithMessage("APK not built").that(apk.isFile).isTrue() + val script = FileProvider.projectRoot().resolve("scripts/check-release-apk-no-sqlite-debugger.sh").toFile() + val result = runBounded(listOf("bash", script.path, apk.path), timeoutSeconds = 60) + println("${apk.name}: exit ${result.exitCode}\n${result.output}") + return result + } + + @Test + fun `an opted-in app's release APK carries no SQLite debugger, and its debug APK fails the check`( + @TempDir dir: File, + ) { + val debuggerAar = aar("debugger") + val rewriteRuntimeAar = aar("rewriteRuntime") + val requeryAar = aar("requery") + + // compileSdk: the platform CI's flox environment installs. requery comes from libs/ in a real app. + val project = + copySampleProject( + dir, + """ + android { compileSdk = 36 } + dependencies { implementation(files("${requeryAar.invariantSeparatorsPath}")) } + """.trimIndent(), + ) + // R8 does not fit in the fixture's 512m daemon + project.resolve("gradle.properties").toFile().appendText("\norg.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8\n") + + val result = + buildProject(task = ":app:assembleDemoRelease", projectDir = project, configureArgs = { + it += ":app:assembleDemoDebug" + it += "-P$PROPERTY_SQLITE_DEBUGGER_ENABLED=true" + it += "-P$PROPERTY_SQLITE_DEBUGGER_AAR=${debuggerAar.absolutePath}" + it += "-P$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR=${rewriteRuntimeAar.absolutePath}" + }) + + // The release is a real opted-in build: its classpath check accepts requery's AAR as a file dependency, + // and it is rewritten, then minified + assertThat(result.task(":app:checkDemoReleaseSqliteRewriteClasspath")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":app:transformDemoReleaseClassesWithAsm")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":app:minifyDemoReleaseWithR8")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.output).contains("Adding SQLite debugger dependency to variant 'demoDebug' of project ':app'") + + val apks = project.resolve("app/build/outputs/apk/demo").toFile() + val release = checkApk(File(apks, "release/app-demo-release-unsigned.apk")) + assertWithMessage(release.output).that(release.exitCode).isEqualTo(0) + assertThat(release.output).contains("OK: no SQLite debugger found") + + val debug = checkApk(File(apks, "debug/app-demo-debug.apk")) + assertWithMessage(debug.output).that(debug.exitCode).isEqualTo(1) + assertThat(debug.output).contains("FAIL: manifest declares provider org.appdevforall.sqlitedebugger.DebuggerStartup") + assertThat(debug.output).containsMatch("FAIL: native library lib/[^/]+/libsqlitedbg\\.so") + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteBuildTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteBuildTest.kt new file mode 100644 index 0000000000..e3915e03e1 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteBuildTest.kt @@ -0,0 +1,51 @@ +package com.itsaky.androidide.gradle + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File + +/** + * The class rewrite (ADFA-7138) is registered only for projects that opt in: without requery in + * the app the rewritten classes cannot load. AGP schedules an ASM transform task per variant + * exactly when a class visitor is registered, so `--dry-run` tells the two cases apart without + * compiling the sample app. + */ +class SqliteRewriteBuildTest { + @Test + fun `a project with the property gets the class transform and one without does not`( + @TempDir dir: File, + ) { + // The same property also adds the debugger, which needs its AAR; --dry-run never reads it. + val aar = File(dir, "sqlite-debugger-release.aar").apply { writeText("aar") } + val runtimeAar = File(dir, "sqlite-rewrite-runtime-release.aar").apply { writeText("aar") } + val enabled = + buildProject( + task = ":app:assembleDemoDebug", + configureArgs = { + it.add("-P$PROPERTY_SQLITE_DEBUGGER_ENABLED=true") + it.add("-P$PROPERTY_SQLITE_DEBUGGER_AAR=${aar.absolutePath}") + it.add("-P$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR=${runtimeAar.absolutePath}") + it.add("--dry-run") + }, + ) + val disabled = + buildProject( + task = ":app:assembleDemoDebug", + configureArgs = { it.add("--dry-run") }, + ) + + assertThat(enabled.output).contains("transformDemoDebugClassesWithAsm") + // the classpath check runs before the compile and the rewrite of its variant + val lines = enabled.output.lines() + val check = lines.indexOfFirst { it.contains("checkDemoDebugSqliteRewriteClasspath") } + assertThat(check).isAtLeast(0) + assertThat(check).isLessThan(lines.indexOfFirst { it.contains("transformDemoDebugClassesWithAsm") }) + assertThat(check).isLessThan(lines.indexOfFirst { it.contains("compileDemoDebugJavaWithJavac") }) + assertThat(disabled.output).doesNotContain("ClassesWithAsm") + assertThat(disabled.output).doesNotContain("SqliteRewriteClasspath") + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewritePluginTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewritePluginTest.kt new file mode 100644 index 0000000000..141cae889f --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewritePluginTest.kt @@ -0,0 +1,205 @@ +package com.itsaky.androidide.gradle + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.gradle.sqlite.CheckSqliteRewriteClasspathTask +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.TaskOutcome +import org.gradle.testkit.runner.UnexpectedBuildResultException +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File +import java.nio.file.Path + +/** + * The helpers the rewritten classes call (ADFA-7138) are injected by Code on the Go into every variant of a + * project that opted in, and a per-variant task checks that requery, androidx.sqlite and androidx.core are on + * the runtime classpath before anything is compiled. + */ +class SqliteRewritePluginTest { + private val runtimeAarName = "sqlite-rewrite-runtime-release.aar" + private val allVariants = listOf("demoDebug", "fullDebug", "demoRelease", "fullRelease") + private val requeryEntry = CheckSqliteRewriteClasspathTask.REQUERY_ENTRY + private val androidxSqliteEntry = CheckSqliteRewriteClasspathTask.ANDROIDX_SQLITE_ENTRY + private val androidxCoreEntry = CheckSqliteRewriteClasspathTask.ANDROIDX_CORE_ENTRY + + private fun build( + dir: File, + task: String, + enabled: String? = "true", + runtimeAar: File? = File(dir, runtimeAarName).apply { writeText("aar") }, + project: Path? = null, + extraArgs: List = emptyList(), + ): BuildResult { + val debuggerAar = File(dir, "sqlite-debugger-release.aar").apply { writeText("aar") } + return buildProject(task = task, projectDir = project, configureArgs = { + if (task == ":app:printRuntimeDeps") { + it += "--init-script" + it += printDepsInitScript(dir).absolutePath + } + if (enabled != null) it += "-P$PROPERTY_SQLITE_DEBUGGER_ENABLED=$enabled" + it += "-P$PROPERTY_SQLITE_DEBUGGER_AAR=${debuggerAar.absolutePath}" + if (runtimeAar != null) it += "-P$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR=${runtimeAar.absolutePath}" + it += extraArgs + }) + } + + private fun BuildResult.declared(variant: String) = output.lines().filter { it.startsWith("RTDEP $variant ") } + + private fun failureOf(block: () -> BuildResult): String { + val failure = runCatching(block).exceptionOrNull() + assertThat(failure).isInstanceOf(UnexpectedBuildResultException::class.java) + return failure!!.message!! + } + + @Test + fun `the helper AAR is added to every variant when opted in, not only the debuggable ones`( + @TempDir dir: File, + ) { + val result = build(dir, ":app:printRuntimeDeps") + + for (variant in allVariants) { + assertThat(result.declared(variant)).contains("RTDEP $variant file $runtimeAarName") + assertThat(result.output).contains("Adding SQLite rewrite runtime dependency to variant '$variant' of project ':app'") + } + } + + @Test + fun `a project that did not opt in gets no helper AAR and no check`( + @TempDir dir: File, + ) { + for (enabled in listOf(null, "false")) { + val result = build(dir, ":app:printRuntimeDeps", enabled) + + for (variant in allVariants) { + assertThat(result.declared(variant).filter { it.contains(runtimeAarName) }).isEmpty() + } + assertThat(result.output).doesNotContain("Adding SQLite rewrite runtime dependency") + } + } + + @Test + fun `opted in without the helper AAR property fails the build and says how to fix it`( + @TempDir dir: File, + ) { + val message = failureOf { build(dir, ":app:printRuntimeDeps", runtimeAar = null) } + + assertThat(message).contains("no property '$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR' is set") + assertThat(message).contains("Remove the line from gradle.properties") + } + + @Test + fun `opted in with a missing helper AAR file fails the build and says how to fix it`( + @TempDir dir: File, + ) { + val message = failureOf { build(dir, ":app:printRuntimeDeps", runtimeAar = File(dir, "absent.aar")) } + + assertThat(message).contains("SQLite rewrite runtime AAR not found") + assertThat(message).contains( + "Remove '$PROPERTY_SQLITE_DEBUGGER_ENABLED' from gradle.properties to build without it, " + + "or retry in a few seconds if Code on the Go has just started.", + ) + } + + private fun jar( + dir: File, + name: String, + vararg entries: String, + ) = standIn(File(dir, "libs/$name"), *entries.map { it to ByteArray(0) }.toTypedArray()) + + private fun project( + dir: File, + script: String, + ) = copySampleProject(dir, script) + + @Test + fun `the check fails without requery on the runtime classpath and says how to add it`( + @TempDir dir: File, + ) { + val project = project(dir, "") + val message = failureOf { build(dir, ":app:checkDemoDebugSqliteRewriteClasspath", project = project) } + + assertThat(message).contains("variant 'demoDebug'") + assertThat(message).contains(requeryEntry) + // the sample project's Room and AppCompat bring the real AARs, which the check must find + assertThat(message).doesNotContain(androidxSqliteEntry) + assertThat(message).doesNotContain(androidxCoreEntry) + assertThat(message).contains("file dependency") + assertThat(message).contains("brings none of its own dependencies") + } + + @Test + fun `the check fails without androidx sqlite, which requery's file dependency does not bring`( + @TempDir dir: File, + ) { + val requery = jar(dir, "requery.jar", requeryEntry) + val project = + project( + dir, + """ + configurations.all { exclude(group = "androidx.sqlite") } + dependencies { implementation(files("${requery.invariantSeparatorsPath}")) } + """.trimIndent(), + ) + val message = failureOf { build(dir, ":app:checkDemoDebugSqliteRewriteClasspath", project = project) } + + assertThat(message).contains(androidxSqliteEntry) + assertThat(message).doesNotContain("requery's SQLiteDatabase") + assertThat(message).doesNotContain(androidxCoreEntry) + } + + @Test + fun `the check fails without androidx core, whose CancellationSignal requery's API takes`( + @TempDir dir: File, + ) { + val requery = jar(dir, "requery.jar", requeryEntry) + val project = + project( + dir, + """ + configurations.all { exclude(group = "androidx.core") } + dependencies { implementation(files("${requery.invariantSeparatorsPath}")) } + """.trimIndent(), + ) + val message = failureOf { build(dir, ":app:checkDemoDebugSqliteRewriteClasspath", project = project) } + + assertThat(message).contains(androidxCoreEntry) + assertThat(message).contains("androidx.core:core") + assertThat(message).doesNotContain("requery's SQLiteDatabase") + assertThat(message).doesNotContain(androidxSqliteEntry) + } + + @Test + fun `the check passes with stand-in jars that contain the three class entries, and is reused from the configuration cache`( + @TempDir dir: File, + ) { + val requery = jar(dir, "requery.jar", requeryEntry) + val sqlite = jar(dir, "sqlite.jar", androidxSqliteEntry) + val core = jar(dir, "core.jar", androidxCoreEntry) + val project = + project( + dir, + """ + configurations.all { + exclude(group = "androidx.sqlite") + exclude(group = "androidx.core") + } + dependencies { + implementation(files("${requery.invariantSeparatorsPath}")) + implementation(files("${sqlite.invariantSeparatorsPath}")) + implementation(files("${core.invariantSeparatorsPath}")) + } + """.trimIndent(), + ) + val first = + build(dir, ":app:checkDemoDebugSqliteRewriteClasspath", project = project, extraArgs = listOf("--configuration-cache")) + assertThat(first.task(":app:checkDemoDebugSqliteRewriteClasspath")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) + + val second = + build(dir, ":app:checkDemoDebugSqliteRewriteClasspath", project = project, extraArgs = listOf("--configuration-cache")) + assertThat(second.output).contains("Reusing configuration cache") + assertThat(second.task(":app:checkDemoDebugSqliteRewriteClasspath")!!.outcome).isEqualTo(TaskOutcome.UP_TO_DATE) + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt new file mode 100644 index 0000000000..d05439852e --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt @@ -0,0 +1,209 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.gradle.sqlite.Compiler +import com.itsaky.androidide.gradle.sqlite.JavaSource +import com.itsaky.androidide.gradle.sqlite.TestJars +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR +import com.itsaky.androidide.utils.FileProvider +import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.objectweb.asm.ClassReader +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.MethodInsnNode +import java.io.ByteArrayOutputStream +import java.io.File +import java.util.zip.ZipEntry +import java.util.zip.ZipFile +import java.util.zip.ZipOutputStream + +/** + * A real build, not a dry run: AGP runs the ASM transform of a variant over a class in a local AAR, and the + * transformed class refers to requery. The check of the classpath passes because stand-in jars hold the class + * entries it looks for; nothing loads them. + */ +class SqliteRewriteTransformTest { + private fun classesJar(classes: Map): ByteArray { + val bytes = ByteArrayOutputStream() + ZipOutputStream(bytes).use { zip -> + zip.putNextEntry(ZipEntry("META-INF/stand-in")) + zip.closeEntry() + for ((name, content) in classes) { + zip.putNextEntry(ZipEntry("$name.class")) + zip.write(content) + zip.closeEntry() + } + } + return bytes.toByteArray() + } + + private fun aar( + file: File, + namespace: String, + classes: Map, + ): File = + standIn( + file, + "AndroidManifest.xml" to + "".toByteArray(), + "R.txt" to ByteArray(0), + "classes.jar" to classesJar(classes), + ) + + /** + * AGP instruments the classes of dependencies with artifact transforms, which the dex builder triggers, not + * in the variant's transform task, so the output is in Gradle's transform cache, which every run of this test + * shares: the bytes of [read] in each jar that holds [marker]. [marker] names a class no earlier run used, so a + * hit is this run's. + */ + private fun transformedJarsWith( + marker: String, + read: String = marker, + ): List { + val cache = FileProvider.projectRoot().resolve("gradle-plugin/build/tmp/test/work/.gradle-test-kit/caches").toFile() + return cache + .walkTopDown() + .filter { it.extension == "jar" && "/transforms/" in it.path } + .mapNotNull { jar -> + runCatching { + ZipFile(jar).use { zip -> + if (zip.getEntry(marker) == null) null else zip.getEntry(read)?.let { zip.getInputStream(it).readBytes() } + } + }.getOrNull() + }.toList() + } + + @Test + fun `AGP's transform rewrites a class of a local AAR to call requery with Object arrays`( + @TempDir dir: File, + ) { + val daoName = "Dao${System.nanoTime()}" + val library = + Compiler + .compile( + listOf( + JavaSource( + "x.lib.$daoName", + "package x.lib; public class $daoName { public Object q(android.database.sqlite.SQLiteDatabase db, String[] a) { return db.rawQuery(\"select 1\", a); } " + + "public boolean probe(com.unknown.Row r) { return r.isString(0); } }", + ), + JavaSource( + "com.unknown.Row", + "package com.unknown; public class Row { public boolean isString(int column) { return false; } }", + ), + ), + ) + // left out of the AAR, as an optional class a library references and the app does not ship + .filterKeys { it != "com/unknown/Row" } + val helperSource = + FileProvider + .projectRoot() + .resolve("subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java") + .toFile() + // The helper's name is fixed, so its AAR also carries a class of this run's, to tell its transformed jar apart + val markerName = "Marker${System.nanoTime()}" + val helper = + Compiler.compile( + listOf( + JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText()), + JavaSource( + "org.appdevforall.sqliterewrite.$markerName", + "package org.appdevforall.sqliterewrite; class $markerName {}", + ), + ), + listOf(TestJars.androidJar, TestJars.androidxCoreJar), + ) + val libAar = aar(File(dir, "libs/x.aar"), "x.lib", library) + val runtimeAar = aar(File(dir, "sqlite-rewrite-runtime-release.aar"), "org.appdevforall.sqliterewrite", helper) + // real, empty classes: the dex builder reads them + val requery = + standIn( + File(dir, "libs/requery.jar"), + *Compiler + .compile( + listOf( + JavaSource( + "io.requery.android.database.sqlite.SQLiteDatabase", + "package io.requery.android.database.sqlite; public class SQLiteDatabase {}", + ), + ), + ).map { (name, bytes) -> "$name.class" to bytes } + .toTypedArray(), + ) + // the debugger goes into the debug variants with the rewrite, and AGP reads its manifest + val debuggerAar = aar(File(dir, "sqlite-debugger-release.aar"), "org.appdevforall.sqlitedebugger", emptyMap()) + val project = + copySampleProject( + dir, + """ + // the sample compiles against 33, which AGP's AAR metadata check rejects for its own view binding + android { compileSdk = 36 } + dependencies { + implementation(files("${libAar.invariantSeparatorsPath}")) + implementation(files("${requery.invariantSeparatorsPath}")) + } + """.trimIndent(), + ) + + val result = + buildProject( + task = ":app:dexBuilderDemoDebug", + projectDir = project, + configureArgs = { + it += "-P$PROPERTY_SQLITE_DEBUGGER_ENABLED=true" + it += "-P$PROPERTY_SQLITE_DEBUGGER_AAR=${debuggerAar.absolutePath}" + it += "-P$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR=${runtimeAar.absolutePath}" + }, + ) + + assertThat(result.task(":app:checkDemoDebugSqliteRewriteClasspath")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":app:transformDemoDebugClassesWithAsm")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) + // isString(I)Z names no SQLite type, so the owner that cannot be resolved is only reported through Gradle's logger, + // at the calling method and line (the library's source is one line) + val warning = + "x.lib.$daoName.probe (line 1): com.unknown.Row cannot be resolved, so the rewrite cannot tell whether its isString(I)Z is " + + "SQLiteCursor.isString, which it changes or rejects; the call is left as it is, and fails at run time if the owner " + + "extends that class" + assertThat(result.output).contains(warning) + println("WARNING LINE: " + result.output.lines().first { warning in it }) + + // the unmodified copy from extracting the AAR is in the cache too; the rewritten one is the other + val daos = transformedJarsWith("x/lib/$daoName.class").map { bytes -> ClassNode().also { ClassReader(bytes).accept(it, 0) } } + assertThat(daos).isNotEmpty() + val rewritten = + daos.filter { dao -> + dao.methods + .first { it.name == "q" } + .instructions + .filterIsInstance() + .any { it.owner.startsWith("io/requery/") } + } + assertThat(rewritten).isNotEmpty() + val q = rewritten.first().methods.first { it.name == "q" } + val call = q.instructions.filterIsInstance().first { it.name == "rawQuery" } + assertThat(call.owner).isEqualTo("io/requery/android/database/sqlite/SQLiteDatabase") + assertThat(call.desc).isEqualTo("(Ljava/lang/String;[Ljava/lang/Object;)Landroid/database/Cursor;") + assertThat(q.desc).isEqualTo("(Lio/requery/android/database/sqlite/SQLiteDatabase;[Ljava/lang/String;)Ljava/lang/Object;") + + // the injected helper AAR goes through the same transform: its class is rewritten to requery too + val helpers = + transformedJarsWith( + "org/appdevforall/sqliterewrite/$markerName.class", + read = "org/appdevforall/sqliterewrite/SqliteRewriteRuntime.class", + ).map { bytes -> ClassNode().also { ClassReader(bytes).accept(it, 0) } } + assertThat(helpers).isNotEmpty() + assertThat( + helpers.any { helper -> + helper.methods.any { method -> + "io/requery/" in method.desc || + method.instructions.filterIsInstance().any { it.owner.startsWith("io/requery/") } + } + }, + ).isTrue() + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolverTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolverTest.kt index f8e066361b..382f522096 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolverTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolverTest.kt @@ -78,6 +78,8 @@ class ComponentProxiabilityResolverTest { // library plumbing that must stay out of the component list and the restart rule. assertThat(resolver.resolve("com.google.firebase.components.ComponentDiscoveryService")) .isInstanceOf(ComponentProxiabilityResolver.Resolution.Skip::class.java) + assertThat(resolver.resolve("org.appdevforall.sqlitedebugger.DebuggerStartup")) + .isInstanceOf(ComponentProxiabilityResolver.Resolution.Skip::class.java) } @Test diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifier.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifier.kt new file mode 100644 index 0000000000..11f751032a --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifier.kt @@ -0,0 +1,98 @@ +package com.itsaky.androidide.gradle.sqlite + +import org.objectweb.asm.ClassReader +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.commons.ClassRemapper +import org.objectweb.asm.commons.Remapper +import java.io.PrintWriter +import java.io.StringWriter +import java.net.URLClassLoader + +/** + * Rewrites compiled samples the way the plugin does and verifies the result twice: with ASM's + * CheckClassAdapter (types, through a loader that sees requery, android.jar, androidx core and + * androidx sqlite) and by making the JVM link each class, which runs its verifier over the + * StackMapTable. asm-util is on the test runtime classpath through AGP only, hence the reflection. + */ +internal object BytecodeVerifier { + private val renamer = + object : Remapper() { + override fun map(internalName: String): String = SqliteRewriteRules.RENAMES[internalName] ?: internalName + } + + private val jars by lazy { + listOfNotNull( + TestJars.requeryJar, + TestJars.androidJar, + TestJars.androidxCoreJar, + TestJars.androidxSqliteJar, + TestJars.kotlinStdlibJar, + ) + } + + /** Original (Android-named) samples are added on top of the jars, renamed ones to [after]. */ + private class Universe( + val before: ClassIndex, + val after: ClassIndex, + ) + + private fun universe(originals: Map): Universe { + val before = ClassIndex(listOf(TestJars.androidJar, TestJars.androidxCoreJar)) + val after = ClassIndex(jars) + for (bytes in originals.values) { + before.addClass(bytes) + val renamed = ClassWriter(0) + ClassReader(bytes).accept(ClassRemapper(renamed, renamer), 0) + after.addClass(renamed.toByteArray()) + } + return Universe(before, after) + } + + /** Rewrites every class, then verifies all of them. Returns the rewritten bytes by original name. */ + fun rewriteAndVerify(originals: Map): Map { + val u = universe(originals) + val hierarchy = ClassHierarchy { name -> if (u.before.knows(name)) u.before.superTypes(name) else null } + val out = originals.mapValues { SqliteRewriter.rewrite(it.value, hierarchy) } + verify(out.values.associateBy { ClassReader(it).className }) + return out + } + + /** Fails with the verifier's output when any class in [classes] (by its final name) does not verify. */ + fun verify(classes: Map) { + val loader = SampleLoader(classes) + val checkClass = Class.forName("org.objectweb.asm.util.CheckClassAdapter") + val check = + checkClass.getMethod( + "verify", + ClassReader::class.java, + ClassLoader::class.java, + Boolean::class.javaPrimitiveType, + PrintWriter::class.java, + ) + val problems = StringBuilder() + for ((name, bytes) in classes) { + val sw = StringWriter() + check.invoke(null, ClassReader(bytes), loader, false, PrintWriter(sw)) + if (sw.toString().isNotBlank()) problems.append("CheckClassAdapter on $name:\n").append(sw).append('\n') + try { + // reflection links the class, which verifies it + Class.forName(name.replace('/', '.'), false, loader).declaredMethods + } catch (e: LinkageError) { + problems.append("JVM verification of $name failed: $e\n") + } + } + check(problems.isEmpty()) { "Rewritten bytecode does not verify:\n$problems" } + } + + /** A loader over the same classpath as [verify], defining [classes] (by final name). */ + fun loader(classes: Map): ClassLoader = SampleLoader(classes) + + private class SampleLoader( + private val classes: Map, + ) : URLClassLoader(jars.map { it.toURI().toURL() }.toTypedArray(), ClassLoader.getPlatformClassLoader()) { + override fun findClass(name: String): Class<*> { + val bytes = classes[name.replace('.', '/')] ?: return super.findClass(name) + return defineClass(name, bytes, 0, bytes.size) + } + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifierTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifierTest.kt new file mode 100644 index 0000000000..e3c6ba8da2 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifierTest.kt @@ -0,0 +1,58 @@ +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.Label +import org.objectweb.asm.Opcodes + +/** The verifier helper itself must catch a bad type and a stale frame, or its "OK" means nothing. */ +class BytecodeVerifierTest { + private fun clazz(build: (ClassWriter) -> Unit): Map { + val w = ClassWriter(0) + w.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "p/Bad", null, "java/lang/Object", null) + build(w) + w.visitEnd() + return mapOf("p/Bad" to w.toByteArray()) + } + + @Test + fun `rejects an operand of the wrong type`() { + val classes = + clazz { w -> + w.visitMethod(Opcodes.ACC_STATIC, "m", "(Ljava/lang/Object;)I", null, null).apply { + visitCode() + visitVarInsn(Opcodes.ALOAD, 0) + visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I", false) + visitInsn(Opcodes.IRETURN) + visitMaxs(1, 1) + visitEnd() + } + } + val e = assertThrows(IllegalStateException::class.java) { BytecodeVerifier.verify(classes) } + assertThat(e.message).contains("p/Bad") + } + + @Test + fun `rejects a stack map frame that disagrees with the code`() { + val classes = + clazz { w -> + w.visitMethod(Opcodes.ACC_STATIC, "m", "(Ljava/lang/Object;)I", null, null).apply { + visitCode() + val l = Label() + visitVarInsn(Opcodes.ALOAD, 0) + visitJumpInsn(Opcodes.IFNULL, l) + visitLabel(l) + // the frame claims a String where the parameter is an Object + visitFrame(Opcodes.F_NEW, 1, arrayOf("java/lang/String"), 0, arrayOf()) + visitInsn(Opcodes.ICONST_0) + visitInsn(Opcodes.IRETURN) + visitMaxs(1, 1) + visitEnd() + } + } + val e = assertThrows(IllegalStateException::class.java) { BytecodeVerifier.verify(classes) } + assertThat(e.message).contains("p/Bad") + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteResolutionTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteResolutionTest.kt new file mode 100644 index 0000000000..01bab6fa4a --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteResolutionTest.kt @@ -0,0 +1,348 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.utils.FileProvider +import org.junit.jupiter.api.Test +import org.objectweb.asm.ClassReader +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.FieldInsnNode +import org.objectweb.asm.tree.MethodInsnNode +import org.objectweb.asm.tree.MethodNode + +class SqliteRewriteResolutionTest { + private val helperSource = + FileProvider + .projectRoot() + .resolve("subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java") + .toFile() + + private class Ref( + val owner: String, + val name: String, + val desc: String, + val field: Boolean, + val isStatic: Boolean, + ) { + override fun toString() = "$owner.$name$desc" + } + + private fun ClassIndex.resolve( + ref: Ref, + caller: String? = null, + protectedOk: Boolean = false, + ) = resolve(ref.owner, ref.name, ref.desc, ref.field, ref.isStatic, caller, protectedOk) + + private fun refsOf(n: ClassNode): List = + n.methods.flatMap { m -> + m.instructions.mapNotNull { insn -> + when (insn) { + is MethodInsnNode -> { + Ref(insn.owner, insn.name, insn.desc, false, insn.opcode == Opcodes.INVOKESTATIC) + } + + is FieldInsnNode -> { + Ref( + insn.owner, + insn.name, + insn.desc, + true, + insn.opcode == Opcodes.GETSTATIC || insn.opcode == Opcodes.PUTSTATIC, + ) + } + + else -> { + null + } + } + } + } + + @Test + fun `the transformed helper only references what requery, android and androidx core define`() { + val classes = + Compiler.compile( + listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText())), + listOf(TestJars.androidJar, TestJars.androidxCoreJar), + ) + val index = ClassIndex(listOf(TestJars.requeryJar, TestJars.androidJar, TestJars.androidxCoreJar)) + val unresolved = mutableListOf() + var checked = 0 + for ((name, bytes) in classes) { + val n = node(SqliteRewriter.rewrite(bytes)) + assertThat(n.name).isEqualTo(name) + for (ref in refsOf(n)) { + if (ref.owner in classes.keys) continue + checked++ + if (index.resolve(ref, caller = n.name) == null) { + unresolved += ref.toString() + } + assertThat( + ref.owner, + ).doesNotMatch("android/database/sqlite/SQLite(Database|Open|Cursor|Query|Statement|Program|Closable).*") + } + } + assertThat(checked).isGreaterThan(10) + assertThat(unresolved).isEmpty() + } + + @Test + fun `every public or protected member of the 10 renamed android classes, inherited ones included, resolves in requery or fails the build`() { + val android = TestJars.androidJar + val index = ClassIndex(listOf(TestJars.requeryJar, TestJars.androidJar, TestJars.androidxCoreJar)) + val helper = + Compiler.compile( + listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText())), + listOf(android, TestJars.androidxCoreJar), + ) + helper.values.forEach { index.addClass(SqliteRewriter.rewrite(it)) } + + val unresolved = mutableListOf() + val failed = mutableListOf() + var total = 0 + var inheritedTotal = 0 + java.util.zip.ZipFile(android).use { zip -> + fun load(name: String) = + zip.getEntry("$name.class")?.let { e -> + ClassNode().also { ClassReader(zip.getInputStream(e)).accept(it, ClassReader.SKIP_CODE) } + } + + /** Android classes the class extends or implements and that stay android's: their members are inherited. */ + fun inheritedFrom(cls: ClassNode): List { + val result = mutableListOf() + val pending = ArrayDeque(listOfNotNull(cls.superName) + cls.interfaces) + val seen = HashSet() + while (pending.isNotEmpty()) { + val n = pending.removeFirst() + if (!seen.add(n) || n in SqliteRewriteRules.RENAMES) continue + val c = load(n) ?: continue + result += c + c.superName?.let(pending::add) + pending.addAll(c.interfaces) + } + return result + } + for (cls in SqliteRewriteRules.SQLITE_CLASSES) { + val owner = load(cls)!! + val isInterface = owner.access and Opcodes.ACC_INTERFACE != 0 + val visible = Opcodes.ACC_PUBLIC or Opcodes.ACC_PROTECTED + // the member through its declaring class and through every renamed subclass; inherited ones through the class + val callOwners = listOf(cls) + SqliteRewriteRules.RENAMED_SUPERS.filterValues { cls in it }.keys + val declarers = listOf(owner to callOwners) + inheritedFrom(owner).map { it to listOf(cls) } + for ((declarer, owners) in declarers) { + val inherited = declarer !== owner + // constants are copied into callers by javac, so no field access to them exists + val fields = declarer.fields.filter { it.access and visible != 0 && it.value == null } + val methods = declarer.methods.filter { it.access and visible != 0 && !(inherited && it.name == "") } + val members = + fields.map { Triple(true, it.name, it.desc) to it.access } + + methods.map { Triple(false, it.name, it.desc) to it.access } + for ((member, memberAccess) in members) { + val (field, name, desc) = member + for (callOwner in owners) { + if (callOwner != cls && name == "") continue + total++ + if (inherited) inheritedTotal++ + val synthetic = synthesize(declarer, callOwner, isInterface, field, name, desc, memberAccess) + val label = "${cls.substringAfterLast('/')}.$name:$desc" + val out = + try { + node(SqliteRewriter.rewrite(synthetic)) + } catch (e: SqliteRewriteException) { + failed += label + continue + } + val calls = refsOf(out).filter { it.owner != "p/Probe" } + // the probe makes exactly one member reference, plus a signal conversion when inserted + val main = calls.first { it.name == name } + if (index.resolve(main, protectedOk = memberAccess and Opcodes.ACC_PROTECTED != 0) == null) { + unresolved += "$label via ${callOwner.substringAfterLast('/')}" + } + } + } + } + } + } + assertThat(inheritedTotal).isGreaterThan(100) + println("members checked: $total, fail rule: ${failed.size}") + assertThat(unresolved).isEmpty() + // everything the rule table lists as missing must be hit, and nothing else may fail + val expected = + SqliteRewriteRules.MISSING_MEMBERS.flatMap { (key, descs) -> + val prefix = key.substringBeforeLast('.').substringAfterLast('/') + "." + key.substringAfterLast('.') + ":" + descs.map { prefix + it } + } + val unexpected = + failed.filterNot { f -> + expected.contains(f) || f.contains("OpenParams") || f.contains("android/database/CursorWindow") + } + assertThat(unexpected).isEmpty() + } + + @Test + fun `every other platform member that mentions a renamed class is redirected to a target that resolves or fails the build`() { + val index = ClassIndex(listOf(TestJars.requeryJar, TestJars.androidJar, TestJars.androidxCoreJar)) + val helper = + Compiler.compile( + listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText())), + listOf(TestJars.androidJar, TestJars.androidxCoreJar), + ) + helper.values.forEach { index.addClass(SqliteRewriter.rewrite(it)) } + val renamedTokens = SqliteRewriteRules.RENAMED_DESCRIPTOR_TOKENS + val unresolved = mutableListOf() + var probed = 0 + var redirected = 0 + java.util.zip.ZipFile(TestJars.androidJar).use { zip -> + for (entry in zip.entries()) { + if (!entry.name.startsWith("android/") || !entry.name.endsWith(".class")) continue + val owner = ClassNode().also { ClassReader(zip.getInputStream(entry)).accept(it, ClassReader.SKIP_CODE) } + if (owner.name in SqliteRewriteRules.RENAMES) continue + val isInterface = owner.access and Opcodes.ACC_INTERFACE != 0 + for (method in owner.methods) { + if (method.access and (Opcodes.ACC_PUBLIC or Opcodes.ACC_PROTECTED) == 0) continue + if (renamedTokens.none { method.desc.contains(it) }) continue + probed++ + val probe = synthesize(owner, owner.name, isInterface, false, method.name, method.desc, method.access) + val out = + try { + node(SqliteRewriter.rewrite(probe)) + } catch (e: SqliteRewriteException) { + continue + } + val call = refsOf(out).first { it.name == method.name } + redirected++ + if (call.owner != "org/appdevforall/sqliterewrite/SqliteRewriteRuntime" || index.resolve(call) == null) { + unresolved += "${owner.name}.${method.name}${method.desc}" + } + } + } + } + println("platform members probed: $probed, redirected: $redirected") + assertThat(probed).isGreaterThan(10) + assertThat(redirected).isGreaterThan(8) + assertThat(unresolved).isEmpty() + } + + @Test + fun `the real androidx framework classes transform, verify, and resolve in requery`() { + val framework = TestJars.androidxSqliteFrameworkJar + val sources = mutableMapOf() + java.util.zip.ZipFile(framework).use { zip -> + for (entry in zip.entries()) { + if (entry.name.startsWith(SqliteRewriteRules.FRAMEWORK_PACKAGE_PREFIX) && entry.name.endsWith(".class")) { + sources[entry.name.removeSuffix(".class")] = zip.getInputStream(entry).readBytes() + } + } + } + // FrameworkSQLiteOpenHelper and its OpenHelper, FrameworkSQLiteDatabase, -Statement, -Program, and the helpers + assertThat(sources.keys).containsAtLeast( + "androidx/sqlite/db/framework/FrameworkSQLiteOpenHelper", + "androidx/sqlite/db/framework/FrameworkSQLiteOpenHelper\$OpenHelper", + "androidx/sqlite/db/framework/FrameworkSQLiteDatabase", + "androidx/sqlite/db/framework/FrameworkSQLiteStatement", + "androidx/sqlite/db/framework/FrameworkSQLiteProgram", + "androidx/sqlite/db/framework/FrameworkSQLiteOpenHelperFactory", + ) + + val before = ClassIndex(listOf(TestJars.androidJar, TestJars.androidxCoreJar, TestJars.androidxSqliteJar, framework)) + val hierarchy = ClassHierarchy { name -> if (before.knows(name)) before.superTypes(name) else null } + val transformed = sources.mapValues { SqliteRewriter.rewrite(it.value, hierarchy) } + assertThat(transformed.keys).isEqualTo(sources.keys) + + val helper = + Compiler.compile( + listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText())), + listOf(TestJars.androidJar, TestJars.androidxCoreJar), + ) + // the framework jar also holds androidx.sqlite.util.ProcessLock, which is not rewritten + val index = + ClassIndex(listOf(TestJars.requeryJar, TestJars.androidJar, TestJars.androidxCoreJar, TestJars.androidxSqliteJar, framework)) + helper.values.forEach { index.addClass(SqliteRewriter.rewrite(it)) } + transformed.values.forEach { index.addClass(it) } + + val unresolved = mutableListOf() + var requeryRefs = 0 + for ((name, bytes) in transformed) { + for (ref in refsOf(node(bytes))) { + if (ref.owner.startsWith("kotlin/") || ref.owner.startsWith("[")) continue + if (ref.owner.startsWith("io/requery/")) requeryRefs++ + if (index.resolve(ref, caller = name) == null) unresolved += "$name -> $ref" + } + } + assertThat(requeryRefs).isGreaterThan(30) + assertThat(unresolved).isEmpty() + // requery has no execPerConnectionSQL of its own: the reference resolves to the interface's default, which throws + assertThat( + index.resolve( + "io/requery/android/database/sqlite/SQLiteDatabase", + "execPerConnectionSQL", + "(Ljava/lang/String;[Ljava/lang/Object;)V", + false, + ), + ).isEqualTo("androidx/sqlite/db/SupportSQLiteDatabase") + // every android.database.sqlite reference is gone, so nothing in the package links to the platform's SQLite + for (bytes in transformed.values) { + for (ref in refsOf(node(bytes))) { + assertThat( + ref.owner, + ).doesNotMatch("android/database/sqlite/SQLite(Database|Open|Cursor|Query|Statement|Program|Closable).*") + } + } + BytecodeVerifier.verify(transformed) + } + + /** A class `p/Probe` with one method that performs one member reference with a valid stack. */ + private fun synthesize( + owner: ClassNode, + callOwner: String, + isInterface: Boolean, + field: Boolean, + name: String, + desc: String, + access: Int, + ): ByteArray { + val cn = ClassNode() + cn.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "p/Probe", null, "java/lang/Object", null) + val m = MethodNode(Opcodes.ASM9, Opcodes.ACC_PUBLIC or Opcodes.ACC_STATIC, "probe", "()V", null, null) + val isStatic = access and Opcodes.ACC_STATIC != 0 + if (field) { + m.visitFieldInsn(if (isStatic) Opcodes.GETSTATIC else Opcodes.GETFIELD, callOwner, name, desc) + m.visitInsn(if (Type.getType(desc).size == 2) Opcodes.POP2 else Opcodes.POP) + } else { + if (!isStatic) m.visitInsn(Opcodes.ACONST_NULL) + for (arg in Type.getArgumentTypes(desc)) { + m.visitInsn( + when (arg.sort) { + Type.LONG -> Opcodes.LCONST_0 + Type.FLOAT -> Opcodes.FCONST_0 + Type.DOUBLE -> Opcodes.DCONST_0 + Type.OBJECT, Type.ARRAY -> Opcodes.ACONST_NULL + else -> Opcodes.ICONST_0 + }, + ) + } + val opcode = + when { + name == "" -> Opcodes.INVOKESPECIAL + isStatic -> Opcodes.INVOKESTATIC + isInterface -> Opcodes.INVOKEINTERFACE + else -> Opcodes.INVOKEVIRTUAL + } + m.visitMethodInsn(opcode, callOwner, name, desc, isInterface) + when (Type.getReturnType(desc).size) { + 2 -> m.visitInsn(Opcodes.POP2) + 1 -> m.visitInsn(Opcodes.POP) + } + } + m.visitInsn(Opcodes.RETURN) + m.visitMaxs(8, 0) + cn.methods.add(m) + val w = ClassWriter(0) + cn.accept(w) + return w.toByteArray() + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt new file mode 100644 index 0000000000..e1b5ac1b14 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt @@ -0,0 +1,54 @@ +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import org.junit.jupiter.api.Test +import org.objectweb.asm.Type + +class SqliteRewriteRulesTest { + @Test + fun `every missing member lists the exact descriptors requery lacks, each a well-formed descriptor`() { + // The rewriter fails a call or a field access only when its descriptor is in the entry's set, so an + // entry with no descriptor, or a blank one, would match nothing. + for ((member, descs) in SqliteRewriteRules.MISSING_MEMBERS) { + assertWithMessage(member).that(descs).isNotEmpty() + for (desc in descs) { + val parsed = runCatching { if (desc.startsWith("(")) Type.getMethodType(desc) else Type.getType(desc) }.getOrNull() + assertWithMessage("$member descriptor '$desc'").that(parsed?.descriptor).isEqualTo(desc) + } + } + } + + @Test + fun `the SQLite descriptor tokens, and which table matches fail on an unresolvable owner`() { + assertThat(SqliteRewriteRules.SQLITE_DESCRIPTOR_TOKENS) + .containsExactly( + "Landroid/database/sqlite/", + "Lio/requery/android/database/", + "Landroid/database/Cursor;", + "Landroid/os/CancellationSignal;", + "Landroidx/core/os/CancellationSignal;", + "Landroid/database/DatabaseErrorHandler;", + ).inOrder() + val tokens = SqliteRewriteRules.SQLITE_DESCRIPTOR_TOKENS + // a key is a name plus a descriptor, and no name holds a token + val (failing, warned) = SqliteRewriteRules.INHERITABLE_RULE_MEMBERS.keys.partition { key -> tokens.any { key.contains(it) } } + + assertThat(failing.map { it.substringBefore('(') }) + .containsExactly("setOpenParams", "getCursorFactory", "insert", "update", "delete", "query") + // CursorWindow is not an SQLite type: any windowed cursor has setWindow(CursorWindow) and mWindow + assertThat(warned).hasSize(28) + assertThat(warned).containsAtLeast( + "isDistinct()Z", + "isStrict()Z", + "getUniqueId()I", + "isString(I)Z", + "isLong(I)Z", + "isFloat(I)Z", + "isBlob(I)Z", + "getProjectionMap()Ljava/util/Map;", + "setWindow(Landroid/database/CursorWindow;)V", + "mWindowLandroid/database/CursorWindow;", + ) + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRuntimeTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRuntimeTest.kt new file mode 100644 index 0000000000..fec4064e9b --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRuntimeTest.kt @@ -0,0 +1,194 @@ +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import com.itsaky.androidide.utils.FileProvider +import org.junit.jupiter.api.Test +import java.lang.ref.WeakReference + +/** + * The runtime helper's functions that need no device, run on the JVM from its bytecode compiled against android.jar, + * with Android's CancellationSignal replaced by a copy of its listener logic (android.jar's methods only throw). + */ +class SqliteRewriteRuntimeTest { + private val loader: ClassLoader by lazy { + val source = + FileProvider + .projectRoot() + .resolve("subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java") + .toFile() + .readText() + val classes = + Compiler.compile( + listOf( + JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", source), + JavaSource("android.os.CancellationSignal", ANDROID_SIGNAL), + ), + listOf(TestJars.androidJar, TestJars.androidxCoreJar), + ) + BytecodeVerifier.loader(classes) + } + + private val runtime: Class<*> by lazy { loader.loadClass("org.appdevforall.sqliterewrite.SqliteRewriteRuntime") } + + private val androidSignal: Class<*> by lazy { loader.loadClass("android.os.CancellationSignal") } + + private fun newAndroidSignal(): Any = androidSignal.getConstructor().newInstance() + + private fun convert(signal: Any): Any = runtime.getMethod("toCancellationSignal", androidSignal).invoke(null, signal) + + private fun cancel(signal: Any) { + signal.javaClass.getMethod("cancel").invoke(signal) + } + + private fun isCanceled(signal: Any) = signal.javaClass.getMethod("isCanceled").invoke(signal) as Boolean + + /** The signal's listener, read from the signal itself: a call on FORWARDERS would expunge its stale entries. */ + private fun listenerOf(signal: Any): Any? = androidSignal.getDeclaredField("listener").apply { isAccessible = true }.get(signal) + + private fun setListener( + signal: Any, + listener: Any?, + ) { + val type = loader.loadClass("android.os.CancellationSignal\$OnCancelListener") + androidSignal.getMethod("setOnCancelListener", type).invoke(signal, listener) + } + + /** Converts a signal that nothing else holds, in its own frame, and returns a weak reference to its forwarder. */ + private fun forwarderOfDroppedSignal(): WeakReference { + val signal = newAndroidSignal() + convert(signal) + return WeakReference(listenerOf(signal)!!) + } + + /** + * Requests GC until [ref] clears, at most [rounds] times. Timing-dependent only in the passing direction: HotSpot's + * System.gc() is a full collection and clears a weak reference in the first round, and the bound only ends a run in + * which it never clears. Nothing calls FORWARDERS meanwhile, as any call on it expunges the stale entries. + */ + private fun clearedAfterGc( + ref: WeakReference<*>, + rounds: Int = 50, + ): Boolean { + repeat(rounds) { + if (ref.get() == null) return true + System.gc() + Thread.sleep(20) + } + return ref.get() == null + } + + private fun translate(flags: Int) = runtime.getMethod("translateOpenFlags", Int::class.javaPrimitiveType).invoke(null, flags) as Int + + private val readOnly = 1 + private val createIfNecessary = 0x10000000 + private val wal = 0x20000000 + + @Test + fun `read-write and create keep requery's values`() { + assertThat(translate(0)).isEqualTo(2) + assertThat(translate(createIfNecessary)).isEqualTo(6) + assertThat(translate(createIfNecessary or wal)).isEqualTo(6 or wal) + } + + @Test + fun `read-only never gets the create bit, which SQLite rejects`() { + assertThat(translate(readOnly)).isEqualTo(1) + assertThat(translate(readOnly or createIfNecessary)).isEqualTo(1) + } + + @Test + fun `other Android bits are dropped`() { + assertThat(translate(16)).isEqualTo(2) + assertThat(translate(8 or createIfNecessary)).isEqualTo(6) + } + + @Test + fun `two queries that share an Android signal are both cancelled with it`() { + val signal = newAndroidSignal() + // requery keeps each query's signal for its cursor's later reads + val first = convert(signal) + val second = convert(signal) + + cancel(signal) + + assertWithMessage("the first query's signal").that(isCanceled(first)).isTrue() + assertWithMessage("the second query's signal").that(isCanceled(second)).isTrue() + } + + @Test + fun `a query given a signal that is already cancelled gets a cancelled one`() { + val signal = newAndroidSignal() + convert(signal) + cancel(signal) + + assertThat(isCanceled(convert(signal))).isTrue() + } + + @Test + fun `the forwarder of a signal the app dropped is not kept`() { + val forwarder = forwarderOfDroppedSignal() + + assertWithMessage("CancelForwarder of a dropped signal, kept by the value of FORWARDERS' stale entry") + .that(clearedAfterGc(forwarder)) + .isTrue() + } + + @Test + fun `a live signal keeps its forwarder through a GC and still cancels its queries`() { + val signal = newAndroidSignal() + val converted = convert(signal) + val forwarder = WeakReference(listenerOf(signal)) + + assertWithMessage("forwarder collected while its signal lives").that(clearedAfterGc(forwarder, rounds = 3)).isFalse() + cancel(signal) + + assertThat(isCanceled(converted)).isTrue() + } + + @Test + fun `a signal whose forwarder was collected gets a new one`() { + val signal = newAndroidSignal() + convert(signal) + val first = WeakReference(listenerOf(signal)) + // the caller sets its own listener, so nothing holds the first forwarder any more + setListener(signal, null) + assertWithMessage("first forwarder collected").that(clearedAfterGc(first)).isTrue() + + val converted = convert(signal) + cancel(signal) + + assertWithMessage("a query converted after the forwarder was collected").that(isCanceled(converted)).isTrue() + } + + private companion object { + /** Android's listener rules: one listener, a new one runs at once on a cancelled signal, the same one does not. */ + val ANDROID_SIGNAL = + """ + package android.os; + public final class CancellationSignal { + private boolean canceled; + private OnCancelListener listener; + public boolean isCanceled() { synchronized (this) { return canceled; } } + public void cancel() { + OnCancelListener l; + synchronized (this) { + if (canceled) return; + canceled = true; + l = listener; + } + if (l != null) l.onCancel(); + } + public void setOnCancelListener(OnCancelListener l) { + synchronized (this) { + if (listener == l) return; + listener = l; + if (!canceled || l == null) return; + } + l.onCancel(); + } + public interface OnCancelListener { void onCancel(); } + } + """.trimIndent() + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriterTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriterTest.kt new file mode 100644 index 0000000000..f512ce5fe7 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriterTest.kt @@ -0,0 +1,1832 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import org.objectweb.asm.ClassReader +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.MethodInsnNode +import org.objectweb.asm.tree.MethodNode +import org.objectweb.asm.tree.TypeInsnNode + +private const val R = "io/requery/android/database/sqlite/" +private const val A = "android/database/sqlite/" +private const val HELPER = "org/appdevforall/sqliterewrite/SqliteRewriteRuntime" + +internal fun node(bytes: ByteArray) = ClassNode().also { ClassReader(bytes).accept(it, 0) } + +internal fun ClassNode.method(name: String): MethodNode = methods.first { it.name == name } + +internal fun MethodNode.calls(): List = instructions.filterIsInstance() + +internal fun MethodNode.call(name: String): MethodInsnNode = calls().first { it.name == name } + +internal fun src( + name: String, + body: String, +) = JavaSource(name, body) + +class SqliteRewriterTest { + private val hierarchy = + ClassHierarchy { name -> + when (name) { + "p/MyActivity" -> { + listOf( + "android/app/Activity", + "android/content/ContextWrapper", + "android/content/Context", + "java/lang/Object", + ) + } + + "p/MyBuilder" -> { + listOf(A + "SQLiteQueryBuilder", "java/lang/Object") + } + + else -> { + null + } + } + } + + private fun transform( + vararg sources: JavaSource, + extraClasspath: List = emptyList(), + ): Map = + BytecodeVerifier.rewriteAndVerify(Compiler.compile(sources.toList(), listOf(TestJars.androidJar) + extraClasspath)) + + @Test + fun `renames the shared classes, the error handlers and overrides, and leaves the kept ones`() { + val out = + transform( + src( + "p.Rename", + """ + package p; + import android.content.Context; + import android.database.*; + import android.database.sqlite.*; + public class Rename extends SQLiteOpenHelper { + public Rename(Context c) { super(c, "n", null, 1, new DefaultDatabaseErrorHandler()); } + @Override public void onCreate(SQLiteDatabase db) { db.execSQL("x"); } + @Override public void onUpgrade(SQLiteDatabase db, int a, int b) {} + public void use(SQLiteDatabase db, CursorWindow w, Cursor c, SQLiteDatabase.CursorFactory f, + SQLiteTransactionListener l) throws SQLiteException { + db.beginTransactionWithListener(l); + SQLiteStatement s = db.compileStatement("y"); + SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); + } + } + """, + ), + )["p/Rename"]!! + val n = node(out) + assertThat(n.superName).isEqualTo(R + "SQLiteOpenHelper") + assertThat(n.methods.first { it.name == "onCreate" }.desc).isEqualTo("(L${R}SQLiteDatabase;)V") + val init = n.method("") + assertThat(init.calls().first { it.name == "" && it.owner == R + "SQLiteOpenHelper" }.desc) + .isEqualTo( + "(Landroid/content/Context;Ljava/lang/String;L${R}SQLiteDatabase\$CursorFactory;IL${"io/requery/android/database/DatabaseErrorHandler"};)V", + ) + assertThat(init.instructions.filterIsInstance().map { it.desc }) + .contains("io/requery/android/database/DefaultDatabaseErrorHandler") + val use = n.method("use") + assertThat(use.desc).isEqualTo( + "(L${R}SQLiteDatabase;Landroid/database/CursorWindow;Landroid/database/Cursor;L${R}SQLiteDatabase\$CursorFactory;L${A}SQLiteTransactionListener;)V", + ) + assertThat(use.exceptions).containsExactly(A + "SQLiteException") + assertThat(use.call("beginTransactionWithListener").desc).isEqualTo("(L${A}SQLiteTransactionListener;)V") + assertThat(use.call("compileStatement").desc).isEqualTo("(Ljava/lang/String;)L${R}SQLiteStatement;") + assertThat(use.instructions.filterIsInstance().map { it.desc }).contains(R + "SQLiteQueryBuilder") + } + + @Test + fun `rewrites String array to Object array in calls and in the cursor driver override`() { + val out = + transform( + src( + "p.Args", + """ + package p; + import android.database.Cursor; + import android.database.sqlite.*; + public class Args implements SQLiteCursorDriver { + public Cursor query(SQLiteDatabase.CursorFactory f, String[] args) { return null; } + public void cursorDeactivated() {} + public void cursorRequeried(Cursor c) {} + public void cursorClosed() {} + public void setBindArguments(String[] a) {} + void calls(SQLiteDatabase db, SQLiteDatabase.CursorFactory f, SQLiteCursorDriver d, Args me, String[] a) { + db.rawQuery("s", a); + db.query("t", a, "w", a, null, null, null); + db.query("t", a, "w", a, null, null, null, "1"); + db.query(true, "t", a, "w", a, null, null, null, "1"); + db.rawQueryWithFactory(f, "s", a, "t"); + db.queryWithFactory(f, true, "t", a, "w", a, null, null, null, "1"); + d.query(f, a); + me.query(f, a); + } + } + """, + ), + )["p/Args"]!! + val n = node(out) + val decl = n.methods.first { it.name == "query" } + assertThat(decl.desc).isEqualTo("(L${R}SQLiteDatabase\$CursorFactory;[Ljava/lang/Object;)Landroid/database/Cursor;") + val calls = n.method("calls").calls() + assertThat(calls.first { it.name == "rawQuery" }.desc) + .isEqualTo("(Ljava/lang/String;[Ljava/lang/Object;)Landroid/database/Cursor;") + assertThat(calls.filter { it.name == "query" && it.owner == R + "SQLiteDatabase" }.map { it.desc }).containsExactly( + "(Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", + "(Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", + "(ZLjava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", + ) + assertThat(calls.first { it.name == "rawQueryWithFactory" }.desc).isEqualTo( + "(L${R}SQLiteDatabase\$CursorFactory;Ljava/lang/String;[Ljava/lang/Object;Ljava/lang/String;)Landroid/database/Cursor;", + ) + assertThat( + calls + .first { + it.name == "queryWithFactory" + }.desc, + ).contains("Ljava/lang/String;[Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)") + // the interface call, and the call on the implementing class itself + assertThat(calls.filter { it.name == "query" && it.owner != R + "SQLiteDatabase" }.map { it.desc }.toSet()) + .containsExactly("(L${R}SQLiteDatabase\$CursorFactory;[Ljava/lang/Object;)Landroid/database/Cursor;") + } + + @Test + fun `converts the cancellation signal right before the call`() { + val out = + transform( + src( + "p.Signal", + """ + package p; + import android.database.sqlite.*; + import android.os.CancellationSignal; + public class Signal { + void go(SQLiteDatabase db, SQLiteQueryBuilder qb, SQLiteDatabase.CursorFactory f, CancellationSignal s, String[] a) { + db.rawQuery("x", a, s); + db.validateSql("x", s); + qb.query(db, a, "w", a, null, null, null, "1", s); + db.rawQueryWithFactory(f, "x", a, "t", s); + } + } + """, + ), + )["p/Signal"]!! + val go = node(out).method("go") + val insns = go.instructions.toArray().toList() + for (name in listOf("rawQuery", "validateSql", "query", "rawQueryWithFactory")) { + val call = go.call(name) + assertThat(call.desc).contains("Landroidx/core/os/CancellationSignal;)") + val before = insns[insns.indexOf(call) - 1] as MethodInsnNode + assertThat(before.owner).isEqualTo(HELPER) + assertThat(before.name).isEqualTo("toCancellationSignal") + assertThat(before.desc).isEqualTo("(Landroid/os/CancellationSignal;)Landroidx/core/os/CancellationSignal;") + } + assertThat(go.call("rawQuery").desc).isEqualTo( + "(Ljava/lang/String;[Ljava/lang/Object;Landroidx/core/os/CancellationSignal;)Landroid/database/Cursor;", + ) + } + + @Test + fun `redirects open flags and the context calls (also on subclasses), and leaves androidx's factory alone`() { + val out = + transform( + src( + "androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory", + "package androidx.sqlite.db.framework; public class FrameworkSQLiteOpenHelperFactory { public FrameworkSQLiteOpenHelperFactory() {} }", + ), + src( + "p.MyActivity", + "package p; public class MyActivity extends android.app.Activity {" + + " void own(android.database.sqlite.SQLiteDatabase.CursorFactory f) {" + + " this.openOrCreateDatabase(\"n\", 0, f); deleteDatabase(\"n\"); } }", + ), + src( + "p.Redirect", + """ + package p; + import android.content.Context; + import android.database.DatabaseErrorHandler; + import android.database.sqlite.*; + public class Redirect { + Object go(Context c, SQLiteDatabase.CursorFactory f, DatabaseErrorHandler h, MyActivity a, Other o) { + SQLiteDatabase.openDatabase("p", f, SQLiteDatabase.OPEN_READONLY); + SQLiteDatabase.openDatabase("p", f, SQLiteDatabase.CREATE_IF_NECESSARY, h); + SQLiteDatabase.openOrCreateDatabase("p", f); + c.openOrCreateDatabase("n", 0, f); + c.openOrCreateDatabase("n", 0, f, h); + c.deleteDatabase("n"); + a.deleteDatabase("n"); + o.deleteDatabase("n"); + return new androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory(); + } + } + """, + ), + src("p.Other", "package p; public class Other { public boolean deleteDatabase(String n) { return true; } }"), + ) + val go = node(out["p/Redirect"]!!).method("go") + + fun redirected(name: String) = go.calls().filter { it.owner == HELPER && it.name == name } + + assertThat(redirected("openDatabase").map { it.desc }).containsExactly( + "(Ljava/lang/String;L${R}SQLiteDatabase\$CursorFactory;I)L${R}SQLiteDatabase;", + "(Ljava/lang/String;L${R}SQLiteDatabase\$CursorFactory;IL${"io/requery/android/database/DatabaseErrorHandler"};)L${R}SQLiteDatabase;", + ) + assertThat(redirected("openOrCreateDatabase").map { it.desc }).containsExactly( + "(Landroid/content/Context;Ljava/lang/String;IL${R}SQLiteDatabase\$CursorFactory;)L${R}SQLiteDatabase;", + "(Landroid/content/Context;Ljava/lang/String;IL${R}SQLiteDatabase\$CursorFactory;Lio/requery/android/database/DatabaseErrorHandler;)L${R}SQLiteDatabase;", + ) + // Context, the Activity subclass; not the unrelated class with the same method + assertThat(redirected("deleteDatabase")).hasSize(2) + assertThat(go.calls().any { it.owner == "p/Other" && it.name == "deleteDatabase" }).isTrue() + // without flags: stays a plain, renamed call + assertThat(go.calls().any { it.owner == R + "SQLiteDatabase" && it.name == "openOrCreateDatabase" }).isTrue() + // requery's factory ignores noBackupDirectory, allowDataLossOnRecovery and the framework's cross-process + // lock, so Room and WorkManager keep androidx's, which runs on requery through the renamed classes + assertThat(go.instructions.filterIsInstance().map { it.desc }) + .containsExactly("androidx/sqlite/db/framework/FrameworkSQLiteOpenHelperFactory") + assertThat( + go.calls().first { it.name == "" }.owner, + ).isEqualTo("androidx/sqlite/db/framework/FrameworkSQLiteOpenHelperFactory") + + val own = node(out["p/MyActivity"]!!).method("own") + assertThat(own.calls().map { it.owner + "." + it.name }).containsExactly("$HELPER.openOrCreateDatabase", "$HELPER.deleteDatabase") + } + + @Test + fun `branchy and looping samples of every rule kind verify, frames included`() { + val out = + transform( + src( + "p.Branchy", + """ + package p; + import android.content.Context; + import android.database.Cursor; + import android.database.DatabaseErrorHandler; + import android.database.sqlite.*; + import android.os.CancellationSignal; + public class Branchy implements SQLiteCursorDriver { + public Cursor query(SQLiteDatabase.CursorFactory f, String[] args) { + // four locals at once make javac write a full frame, which names the parameter's type + int n = 0, a = 1, b = 2, c = 3; + for (int i = 0; i < 3; i++) { + if (f == null) { + n++; + } else if (args == null) { + n--; + } + } + if (n + a + b + c > 1) { + return null; + } + return null; + } + public void cursorDeactivated() {} + public void cursorRequeried(Cursor c) {} + public void cursorClosed() {} + public void setBindArguments(String[] a) {} + + Cursor descriptors(SQLiteDatabase db, SQLiteDatabase.CursorFactory f, String[] a, boolean b, CancellationSignal s) { + Cursor c = null; + for (int i = 0; i < 3; i++) { + if (b) { + c = db.rawQuery("x", a); + } else if (s != null) { + c = db.rawQuery("y", a, s); + } else { + c = db.query("t", a, "w", a, null, null, null, "1"); + } + if (c != null && i == 1) { + c = db.rawQueryWithFactory(f, "z", a, "t"); + } + } + return c; + } + + Object redirects(Context ctx, SQLiteDatabase.CursorFactory f, DatabaseErrorHandler h, boolean b, String name) { + SQLiteDatabase db; + if (b) { + db = SQLiteDatabase.openDatabase(name, f, SQLiteDatabase.OPEN_READONLY); + } else { + db = ctx.openOrCreateDatabase(name, 0, f, h); + } + for (int i = 0; i < 2; i++) { + if (ctx.deleteDatabase(name + i)) { + db = SQLiteDatabase.openDatabase(name, f, SQLiteDatabase.CREATE_IF_NECESSARY, h); + } + } + return db; + } + + Object joins(SQLiteDatabase db, boolean b, SQLiteQueryBuilder qb) { + SQLiteClosable c = b ? db : db.compileStatement("x"); + SQLiteProgram p = b ? db.compileStatement("y") : null; + while (c != null && b) { + c.close(); + c = p; + b = false; + } + return qb != null ? c : p; + } + } + """, + ), + ) + assertThat(out["p/Branchy"]).isNotNull() + } + + @Test + fun `the androidx factory class is rewritten like the rest of its package, not excluded`() { + val name = "androidx/sqlite/db/framework/FrameworkSQLiteOpenHelperFactory" + val out = + transform( + src( + "androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory", + """ + package androidx.sqlite.db.framework; + import android.content.Context; + import android.database.sqlite.SQLiteOpenHelper; + public class FrameworkSQLiteOpenHelperFactory { + public SQLiteOpenHelper create(Context c, String n) { return null; } + } + """, + ), + ) + val n = node(out[name]!!) + assertThat(n.name).isEqualTo(name) + assertThat( + n.method("create").desc, + ).isEqualTo("(Landroid/content/Context;Ljava/lang/String;)L${R}SQLiteOpenHelper;") + assertThat(RewritePolicy.isExcluded(name)).isFalse() + } + + @Test + fun `fails with class, method and line for members requery lacks, collecting all of them`() { + val sources = + src( + "p.Bad", + """ + package p; + import android.database.sqlite.*; + public class Bad { + void one(SQLiteDatabase db) { + db.execPerConnectionSQL("x", null); + } + void two(SQLiteDatabase db, SQLiteDatabase.OpenParams params) { + db.setCustomScalarFunction("f", s -> s); + SQLiteDatabase.openDatabase(new java.io.File("f"), params); + new SQLiteDatabase.OpenParams.Builder().build(); + } + void three(MyBuilder b, SQLiteQueryBuilder q) { + b.isDistinct(); + q.setStrictGrammar(true); + } + } + """, + ) + val classes = + Compiler.compile( + listOf( + sources, + src("p.MyBuilder", "package p; public class MyBuilder extends android.database.sqlite.SQLiteQueryBuilder {}"), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Bad"]!!, hierarchy) } + val message = e.message!! + assertThat(message).contains("p.Bad") + assertThat(message).contains("one(Landroid/database/sqlite/SQLiteDatabase;)V (line 6)") + assertThat(message).contains("SQLiteDatabase.execPerConnectionSQL") + assertThat(message).contains("two(") + assertThat(message).contains("setCustomScalarFunction") + assertThat(message).contains("OpenParams") + assertThat(message).contains("(line 9)") + assertThat(message).contains("(line 10)") + assertThat(message).contains("(line 11)") + // a subclass of SQLiteQueryBuilder is resolved through the hierarchy + assertThat(message).contains("SQLiteQueryBuilder.isDistinct") + assertThat(message).contains("SQLiteQueryBuilder.setStrictGrammar") + } + + @Test + fun `the failure says how to build when the class is in a library`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Lib", + """ + package p; + public class Lib { + void one(android.database.sqlite.SQLiteDatabase db) { + db.execPerConnectionSQL("x", null); + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Lib"]!!, hierarchy) } + val lastLine = e.message!!.lines().last() + assertThat(lastLine).contains("in a library") + assertThat(lastLine).contains("comment out cogo.sqliteDebugger=true in gradle.properties") + } + + @Test + fun `a missing member is found through every renamed ancestor of the owner`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Ancestors", + """ + package p; + import android.database.sqlite.*; + public class Ancestors { + int program(SQLiteStatement stmt, SQLiteQuery query) { return stmt.getUniqueId() + query.getUniqueId(); } + void closable(SQLiteDatabase db) { db.releaseReferenceFromContainer(); } + void subclass(MyBuilder b) { b.isStrict(); b.close(); } + } + """, + ), + src( + "p.MyBuilder", + "package p; public class MyBuilder extends android.database.sqlite.SQLiteQueryBuilder { void close() {} }", + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Ancestors"]!!, hierarchy) } + val message = e.message!! + assertThat(message).contains("SQLiteProgram.getUniqueId") + assertThat(message).contains("SQLiteClosable.releaseReferenceFromContainer") + assertThat(message).contains("SQLiteQueryBuilder.isStrict") + assertThat(message).doesNotContain("close") + } + + @Test + fun `hierarchy lookups are made once per owner and not for classes that cannot be SQLite subclasses`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Chatty", + """ + package p; + import android.database.sqlite.*; + public class Chatty { + int go(String s, StringBuilder sb, java.util.List l, android.widget.TextView tv, Mine a, Mine b, SQLiteStatement st) { + sb.append(s.length()).append(s.trim()).append(l.size()); + tv.setText(s); tv.setText(s); + a.run(); a.run(); b.run(); + return st.getUniqueId() > 0 ? 1 : 0; + } + } + """, + ), + src("p.Mine", "package p; public class Mine { void run() {} }"), + ), + ) + val asked = mutableListOf() + val counting = + ClassHierarchy { name -> + asked += name + null + } + try { + SqliteRewriter.rewrite(classes["p/Chatty"]!!, counting) + } catch (e: SqliteRewriteException) { + // SQLiteStatement.getUniqueId fails the build (requery lacks it), which is not what this test is about + } + assertThat(asked.filter { it.startsWith("java/") || it.startsWith("android/") }).isEmpty() + assertThat(asked.groupingBy { it }.eachCount().filterValues { it > 1 }).isEmpty() + // ordinary calls on app classes need no lookup: no rule can match their name or descriptor + assertThat(asked).doesNotContain("p/Mine") + } + + @Test + fun `a call whose name a rule lists, or whose descriptor has a CursorWindow, still looks the owner up`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Named", + """ + package p; + public class Named { + void go(Mine a, Other b, Third c, android.database.CursorWindow w) { + a.getUniqueId(); + a.getUniqueId(); + b.fetch(w); + c.ordinary(); + } + } + """, + ), + src("p.Mine", "package p; public class Mine { int getUniqueId() { return 0; } }"), + src("p.Other", "package p; public class Other { void fetch(android.database.CursorWindow w) {} }"), + src("p.Third", "package p; public class Third { void ordinary() {} }"), + ), + ) + val asked = mutableListOf() + // getUniqueId()I names no SQLite type, so on an owner that cannot be resolved it only warns + SqliteRewriter.rewrite( + classes["p/Named"]!!, + ClassHierarchy { name -> + asked += name + null + }, + ) + assertThat(asked).containsExactly("p/Mine", "p/Other") + } + + @Test + fun `field accesses and constructors of app classes trigger no hierarchy lookup`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Fields", + """ + package p; + public class Fields { + int go(Holder h) { h.count++; return h.count + new Holder().count; } + } + """, + ), + src("p.Holder", "package p; public class Holder { int count; }"), + ), + ) + val asked = mutableListOf() + SqliteRewriter.rewrite( + classes["p/Fields"]!!, + ClassHierarchy { name -> + asked += name + null + }, + ) + assertThat(asked).isEmpty() + } + + @Test + fun `DatabaseUtils helpers that take SQLite types are redirected to the runtime`() { + val out = + transform( + src( + "p.Utils", + """ + package p; + import android.database.DatabaseUtils; + import android.database.sqlite.*; + public class Utils { + long go(SQLiteDatabase db, SQLiteStatement st, String[] a, boolean b) { + long n = 0; + if (b) { + n += DatabaseUtils.queryNumEntries(db, "t"); + n += DatabaseUtils.queryNumEntries(db, "t", "x"); + } else { + n += DatabaseUtils.queryNumEntries(db, "t", "x", a); + } + for (int i = 0; i < 2; i++) { + n += DatabaseUtils.longForQuery(db, "q", a) + DatabaseUtils.longForQuery(st, a); + n += DatabaseUtils.stringForQuery(db, "q", a).length() + DatabaseUtils.stringForQuery(st, a).length(); + } + return n; + } + } + """, + ), + )["p/Utils"]!! + val calls = node(out).method("go").calls() + assertThat(calls.filter { it.owner == "android/database/DatabaseUtils" }).isEmpty() + val redirected = calls.filter { it.owner == HELPER } + assertThat(redirected.map { it.name }.groupingBy { it }.eachCount()) + .containsExactly("queryNumEntries", 3, "longForQuery", 2, "stringForQuery", 2) + assertThat(redirected.map { it.opcode }.toSet()).containsExactly(org.objectweb.asm.Opcodes.INVOKESTATIC) + assertThat(redirected.first { it.name == "longForQuery" && it.desc.contains("Statement") }.desc) + .isEqualTo("(L${R}SQLiteStatement;[Ljava/lang/String;)J") + assertThat(redirected.first { it.name == "queryNumEntries" && it.desc.contains("Ljava/lang/String;Ljava/lang/String;)") }.desc) + .isEqualTo("(L${R}SQLiteDatabase;Ljava/lang/String;Ljava/lang/String;)J") + } + + @Test + fun `platform methods with SQLite types that have no requery counterpart fail the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.BadUtils", + """ + package p; + import android.database.DatabaseUtils; + import android.database.sqlite.*; + public class BadUtils { + void one(SQLiteDatabase db, SQLiteStatement st, String[] a, SQLiteProgram p) throws Exception { + DatabaseUtils.blobFileDescriptorForQuery(db, "q", a); + DatabaseUtils.blobFileDescriptorForQuery(st, a); + DatabaseUtils.bindObjectToProgram(p, 1, "x"); + new DatabaseUtils.InsertHelper(db, "t"); + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/BadUtils"]!!, hierarchy) } + val message = e.message!! + assertThat(message).contains("p.BadUtils") + assertThat(message).contains("DatabaseUtils.blobFileDescriptorForQuery") + assertThat(message).contains("DatabaseUtils.bindObjectToProgram") + assertThat(message).contains("DatabaseUtils\$InsertHelper.") + for (line in 7..10) assertThat(message).contains("(line $line)") + } + + @Test + fun `Context database calls that cannot be redirected fail the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.SuperCall", + """ + package p; + public class SuperCall extends android.app.Activity { + void go(android.database.sqlite.SQLiteDatabase.CursorFactory f) { + super.openOrCreateDatabase("n", 0, f); + } + boolean del() { + return super.deleteDatabase("n"); + } + } + """, + ), + src( + "p.OverridesBoth", + """ + package p; + import android.database.sqlite.*; + public class OverridesBoth extends android.content.ContextWrapper { + public OverridesBoth() { super(null); } + @Override public SQLiteDatabase openOrCreateDatabase(String n, int m, SQLiteDatabase.CursorFactory f) { return null; } + @Override public boolean deleteDatabase(String n) { return true; } + } + """, + ), + src( + "p.Unknown", + """ + package p; + public class Unknown { + boolean go(q.Lib lib, android.database.sqlite.SQLiteDatabase.CursorFactory f) { + lib.openOrCreateDatabase("n", 0, f); + return lib.deleteDatabase("n"); + } + } + """, + ), + src( + "q.Lib", + """ + package q; + public class Lib { + public android.database.sqlite.SQLiteDatabase openOrCreateDatabase(String n, int m, android.database.sqlite.SQLiteDatabase.CursorFactory f) { return null; } + public boolean deleteDatabase(String n) { return true; } + } + """, + ), + ), + ) + + fun failure(name: String) = + assertThrows(SqliteRewriteException::class.java) { + SqliteRewriter.rewrite(classes[name]!!, hierarchy) + }.message!! + failure("p/SuperCall").let { + assertThat(it).contains("go(") + assertThat(it).contains("del(") + assertThat(it).contains("super") + } + failure("p/OverridesBoth").let { + assertThat(it).contains("overrides Context.openOrCreateDatabase") + assertThat(it).contains("overrides Context.deleteDatabase") + } + failure("p/Unknown").let { + assertThat(it).contains("q/Lib") + assertThat(it).contains("(line 5)") + assertThat(it).contains("(line 6)") + } + } + + @Test + fun `method references get the same rules as calls, or fail asking for a lambda`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Refs", + """ + package p; + import android.content.Context; + import android.database.Cursor; + import android.database.sqlite.*; + public class Refs { + interface Open { SQLiteDatabase open(String n, int m, SQLiteDatabase.CursorFactory f); } + interface Opener { SQLiteDatabase open(String n, SQLiteDatabase.CursorFactory f, int flags); } + Object rawQuery(SQLiteDatabase db) { + java.util.function.BiFunction f = db::rawQuery; + return f; + } + Object delete(Context ctx) { + java.util.function.Predicate p = ctx::deleteDatabase; + return p; + } + Object open(Context ctx) { + Open o = ctx::openOrCreateDatabase; + return o; + } + Object staticOpen() { + Opener o = SQLiteDatabase::openDatabase; + return o; + } + Object missing(SQLiteDatabase db) { + Runnable r = db::releaseReferenceFromContainer; + return r; + } + } + """, + ), + ), + ) + val message = + assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Refs"]!!, hierarchy) }.message!! + for (method in listOf("rawQuery", "delete", "open", "staticOpen", "missing")) { + assertThat(message).contains("$method(") + } + assertThat(message).contains("method reference") + assertThat(message).contains("lambda") + } + + @Test + fun `method references and lambdas that need no rule are left alone`() { + val out = + transform( + src( + "p.FineRefs", + """ + package p; + import android.database.Cursor; + import android.database.sqlite.*; + public class FineRefs { + Object go(SQLiteDatabase db, java.util.List l) { + Runnable begin = db::beginTransaction; + java.util.function.Function len = String::length; + java.util.function.Supplier s = () -> db.compileStatement("x"); + java.util.function.Function q = a -> db.rawQuery(a, null); + l.forEach(x -> db.execSQL(x)); + return new Object[] {begin, len, s, q}; + } + } + """, + ), + )["p/FineRefs"]!! + val n = node(out) + assertThat( + n.methods + .flatMap { it.calls() } + .filter { it.name == "rawQuery" } + .map { it.desc }, + ).containsExactly("(Ljava/lang/String;[Ljava/lang/Object;)Landroid/database/Cursor;") + } + + @Test + fun `fails for a cast of an SQLite cursor to AbstractWindowedCursor but not for other values`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Casts", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Casts { + Object viaLocal(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + return (AbstractWindowedCursor) c; + } + Object viaSqliteCursor(SQLiteCursor c) { + Object o = c; + return (AbstractWindowedCursor) o; + } + } + """, + ), + src( + "p.Fine", + """ + package p; + import android.database.*; + public class Fine { + Object other(Cursor fromContentProvider) { + return (AbstractWindowedCursor) fromContentProvider; + } + boolean guarded(Cursor c) { + return c instanceof AbstractWindowedCursor; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Casts"]!!, hierarchy) } + assertThat(e.message).contains("viaLocal") + assertThat(e.message).contains("viaSqliteCursor") + assertThat(e.message).contains("AbstractWindowedCursor") + SqliteRewriter.rewrite(classes["p/Fine"]!!, hierarchy) + } + + @Test + fun `taint of an SQLite cursor survives a join point`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Joins", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Joins { + Object assignedOnOneBranch(Cursor other, SQLiteDatabase db, boolean b) { + Cursor c = other; + if (b) { + c = db.rawQuery("x", null); + } + return (AbstractWindowedCursor) c; + } + Object assignedInElse(Cursor other, SQLiteDatabase db, boolean b) { + Cursor c; + if (b) { + c = other; + } else { + c = db.rawQuery("x", null); + } + return (AbstractWindowedCursor) c; + } + Object assignedInLoop(Cursor other, SQLiteDatabase db, int n) { + Cursor c = other; + for (int i = 0; i < n; i++) { + c = db.rawQuery("x", null); + } + return (AbstractWindowedCursor) c; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Joins"]!!, hierarchy) } + assertThat(e.message).contains("assignedOnOneBranch") + assertThat(e.message).contains("assignedInElse") + assertThat(e.message).contains("assignedInLoop") + } + + @Test + fun `casts of an SQLite cursor to AbstractCursor and CrossProcessCursor fail too`() { + val classes = + Compiler.compile( + listOf( + src( + "p.MoreCasts", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class MoreCasts { + Object toAbstract(SQLiteDatabase db) { + return (AbstractCursor) db.rawQuery("x", null); + } + Object toCrossProcess(SQLiteCursor c) { + Object o = c; + return (CrossProcessCursor) o; + } + boolean guarded(SQLiteDatabase db) { + return db.rawQuery("x", null) instanceof CrossProcessCursor; + } + } + """, + ), + src( + "p.FineCasts", + """ + package p; + import android.database.*; + public class FineCasts { + Object other(Cursor fromContentProvider) { + return (AbstractCursor) fromContentProvider; + } + Object cross(Cursor fromContentProvider) { + return (CrossProcessCursor) fromContentProvider; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/MoreCasts"]!!, hierarchy) } + assertThat(e.message).contains("toAbstract") + assertThat(e.message).contains("android.database.AbstractCursor") + assertThat(e.message).contains("toCrossProcess") + assertThat(e.message).contains("android.database.CrossProcessCursor") + assertThat(e.message).doesNotContain("guarded") + SqliteRewriter.rewrite(classes["p/FineCasts"]!!, hierarchy) + } + + @Test + fun `a call through CursorWindow of a renamed class fails`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Window", + "package p; public class Window { Object go(android.database.sqlite.SQLiteCursor c) { return c.getWindow(); } }", + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Window"]!!, hierarchy) } + assertThat(e.message).contains("CursorWindow") + } + + @Test + fun `the helper package is renamed but neither redirected nor failed, and framework only skips the fail rule`() { + val classes = + Compiler.compile( + listOf( + src( + "org.appdevforall.sqliterewrite.SqliteRewriteRuntime", + """ + package org.appdevforall.sqliterewrite; + import android.database.sqlite.SQLiteDatabase; + public class SqliteRewriteRuntime { + public static SQLiteDatabase openDatabase(String p, SQLiteDatabase.CursorFactory f, int fl) { + return SQLiteDatabase.openDatabase(p, f, fl); + } + public static void bad(SQLiteDatabase db) { db.execPerConnectionSQL("x", null); } + } + """, + ), + src( + "androidx.sqlite.db.framework.FrameworkSQLiteDatabase", + """ + package androidx.sqlite.db.framework; + import android.database.sqlite.SQLiteDatabase; + public class FrameworkSQLiteDatabase { + void exec(SQLiteDatabase db, String[] a) { + db.execPerConnectionSQL("x", null); + db.rawQuery("x", a); + } + } + """, + ), + ), + ) + val helper = node(SqliteRewriter.rewrite(classes["org/appdevforall/sqliterewrite/SqliteRewriteRuntime"]!!, hierarchy)) + val inner = helper.method("openDatabase").call("openDatabase") + assertThat(inner.owner).isEqualTo(R + "SQLiteDatabase") + assertThat(inner.desc).isEqualTo("(Ljava/lang/String;L${R}SQLiteDatabase\$CursorFactory;I)L${R}SQLiteDatabase;") + + val framework = node(SqliteRewriter.rewrite(classes["androidx/sqlite/db/framework/FrameworkSQLiteDatabase"]!!, hierarchy)) + assertThat(framework.method("exec").call("rawQuery").desc).contains("[Ljava/lang/Object;") + } + + @Test + fun `a type test on OpenParams fails too`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Fields", + "package p; public class Fields { Object go(Object o) { return o instanceof android.database.sqlite.SQLiteDatabase.OpenParams; } }", + ), + ), + ) + assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Fields"]!!, hierarchy) } + } + + @Test + fun `androidx core's SQLiteCursorCompat helper is rewritten but never fails the build`() { + val classes = + Compiler.compile( + listOf( + src( + "androidx.core.database.sqlite.SQLiteCursorCompat", + """ + package androidx.core.database.sqlite; + public class SQLiteCursorCompat { + public static void setFillWindowForwardOnly(android.database.sqlite.SQLiteCursor c, boolean b) { + c.setFillWindowForwardOnly(b); + } + } + """, + ), + ), + ) + val out = node(SqliteRewriter.rewrite(classes["androidx/core/database/sqlite/SQLiteCursorCompat"]!!, hierarchy)) + assertThat(out.method("setFillWindowForwardOnly").desc).isEqualTo("(L${R}SQLiteCursor;Z)V") + assertThat(RewritePolicy.forClass("androidx/core/database/sqlite/SQLiteCursorCompat\$Api28Impl")).isEqualTo(RewritePolicy.NO_FAIL) + } + + @Test + fun `a cursor driver that uses its String array parameter verifies and links, with every kind of frame`() { + val out = + transform( + src( + "p.Driver", + """ + package p; + import android.database.Cursor; + import android.database.sqlite.*; + public class Driver implements SQLiteCursorDriver { + static int sink(String[] a) { return a.length; } + public Cursor query(SQLiteDatabase.CursorFactory f, String[] args) { + int x = args[0].length(); + if (f == null) { + x += sink(args); + } + int a = 1, b = 2, c = 3, d = 4; + for (int i = 0; i < x; i++) { + if (args.length > i) { + x += args[i].length(); + } else { + x += sink(args); + } + } + return x > a + b + c + d ? null : null; + } + public void cursorDeactivated() {} + public void cursorRequeried(Cursor c) {} + public void cursorClosed() {} + public void setBindArguments(String[] a) {} + } + """, + ), + src( + "p.LoopFirst", + """ + package p; + import android.database.Cursor; + import android.database.sqlite.*; + public abstract class LoopFirst implements SQLiteCursorDriver { + public Cursor query(SQLiteDatabase.CursorFactory f, String[] args) { + for (;;) { + if (args[0].length() > 1) { + return null; + } + Driver.sink(args); + } + } + } + """, + ), + ) + val query = node(out["p/Driver"]!!).method("query") + assertThat(query.desc).isEqualTo("(L${R}SQLiteDatabase\$CursorFactory;[Ljava/lang/Object;)Landroid/database/Cursor;") + val first = query.instructions.filter { it.opcode >= 0 }.take(3) + assertThat(first.map { it.opcode }).containsExactly(25, 192, 58).inOrder() // ALOAD, CHECKCAST, ASTORE + assertThat((first[1] as TypeInsnNode).desc).isEqualTo("[Ljava/lang/String;") + } + + @Test + fun `SQL text with COLLATE UNICODE fails the build, with class, method and line`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Dao", + """ + package p; + public class Dao { + static final String K = "SELECT * FROM t ORDER BY n Collate Unicode"; + String direct() { + return "SELECT * FROM t ORDER BY n COLLATE\n\tUNICODE".trim(); + } + String viaConstant() { return K; } + String fine() { return "SELECT * FROM t ORDER BY n COLLATE NOCASE; collateral unicode"; } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Dao"]!!) } + val message = e.message!! + assertThat(message).contains("p.Dao") + assertThat(message).contains("direct()Ljava/lang/String; (line ") + assertThat(message).contains("viaConstant()Ljava/lang/String; (line ") + assertThat(message).contains("COLLATE UNICODE") + assertThat(message).doesNotContain("fine") + } + + @Test + fun `COLLATE LOCALIZED only warns`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Sorted", + "package p; public class Sorted { String q() { return \"SELECT * FROM t ORDER BY n collate localized\"; } String n() { return \"x\"; } }", + ), + ), + ) + val warnings = mutableListOf() + SqliteRewriter.rewrite(classes["p/Sorted"]!!, warn = { warnings += it }) + assertThat(warnings).hasSize(1) + assertThat(warnings.single()).contains("p.Sorted.q") + assertThat(warnings.single()).contains("line ") + assertThat(warnings.single()).contains("byte") + } + + @Test + fun `a string naming a renamed class only warns, and one naming a kept class does not`() { + val classes = + Compiler.compile( + listOf( + src( + "p.ByName", + """ + package p; + public class ByName { + Class db() throws Exception { return Class.forName("android.database.sqlite.SQLiteDatabase"); } + String factory() { return "android/database/sqlite/SQLiteDatabase${'$'}CursorFactory"; } + String kept() { return "android.database.sqlite.SQLiteException android.database.sqlite.SQLiteDatabaseLockedException"; } + } + """.trimIndent(), + ), + ), + ) + val warnings = mutableListOf() + SqliteRewriter.rewrite(classes["p/ByName"]!!, warn = { warnings += it }) + assertThat(warnings).hasSize(2) + assertThat(warnings[0]).contains("p.ByName.db (line ") + assertThat(warnings[0]).contains("\"android.database.sqlite.SQLiteDatabase\"") + assertThat(warnings[1]).contains("p.ByName.factory (line ") + assertThat(warnings[1]).contains("\"android/database/sqlite/SQLiteDatabase\$CursorFactory\"") + } + + @Test + fun `a string that only contains a renamed class name inside a longer name does not warn`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Longer", + """ + package p; + public class Longer { + String longer() { return "xandroid.database.sqlite.SQLiteDatabase"; } + String named() { return "android.database.sqlite.SQLiteDatabase"; } + } + """.trimIndent(), + ), + ), + ) + val warnings = mutableListOf() + SqliteRewriter.rewrite(classes["p/Longer"]!!, warn = { warnings += it }) + assertThat(warnings).hasSize(1) + assertThat(warnings.single()).contains("p.Longer.named (line ") + } + + @Test + fun `a type descriptor or array class name naming a renamed class still warns`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Descriptors", + """ + package p; + public class Descriptors { + String array() { return "[Landroid.database.sqlite.SQLiteDatabase;"; } + String method() { return "(Landroid/database/sqlite/SQLiteDatabase;)V"; } + } + """.trimIndent(), + ), + ), + ) + val warnings = mutableListOf() + SqliteRewriter.rewrite(classes["p/Descriptors"]!!, warn = { warnings += it }) + assertThat(warnings).hasSize(2) + assertThat(warnings[0]).contains("p.Descriptors.array (line ") + assertThat(warnings[1]).contains("p.Descriptors.method (line ") + } + + @Test + fun `calls on android's CursorWindow are not reported even when the hierarchy says it extends SQLiteClosable`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Windows", + """ + package p; + import android.database.CursorWindow; + public class Windows { + void go(CursorWindow w) { w.releaseReferenceFromContainer(); w.close(); } + Object parcel(android.os.Parcel p) { return CursorWindow.newFromParcel(p); } + } + """, + ), + ), + ) + val withClosable = + ClassHierarchy { name -> + if (name == "android/database/CursorWindow") listOf(A + "SQLiteClosable", "java/lang/Object") else null + } + SqliteRewriter.rewrite(classes["p/Windows"]!!, withClosable) + } + + @Test + fun `inherited deprecated cursor members that requery lacks fail the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.CursorUser", + """ + package p; + import android.database.sqlite.SQLiteCursor; + public class CursorUser { + boolean types(SQLiteCursor c) { return c.isBlob(0) || c.isFloat(1) || c.isLong(2) || c.isString(3); } + Object uris(SQLiteCursor c) { return c.getNotificationUris(); } + } + """, + ), + src( + "p.MyCursor", + """ + package p; + import android.database.sqlite.*; + public class MyCursor extends SQLiteCursor { + public MyCursor(SQLiteCursorDriver d, String t, SQLiteQuery q) { super(d, t, q); } + boolean updated(int i) { return isFieldUpdated(i) || getUpdatedField(i) != null; } + Object window() { return mWindow; } + boolean fine() { return isClosed() && getCount() > 0; } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/CursorUser"]!!, hierarchy) } + for (name in listOf("isBlob", "isFloat", "isLong", "isString", "getNotificationUris")) { + assertThat(e.message).contains("SQLiteCursor.$name") + } + val viaSubclass = + ClassHierarchy { name -> + if (name == + "p/MyCursor" + ) { + listOf(A + "SQLiteCursor", "android/database/AbstractWindowedCursor", "java/lang/Object") + } else { + null + } + } + val e2 = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/MyCursor"]!!, viaSubclass) } + assertThat(e2.message).contains("SQLiteCursor.isFieldUpdated") + assertThat(e2.message).contains("SQLiteCursor.getUpdatedField") + assertThat(e2.message).contains("SQLiteCursor.mWindow") + assertThat(e2.message).doesNotContain("isClosed") + assertThat(e2.message).doesNotContain("getCount") + } + + @Test + fun `a cast guarded by an instanceof of the same type on the same value is not reported`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Guarded", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Guarded { + Object ifBlock(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (c instanceof AbstractWindowedCursor) { + AbstractWindowedCursor w = (AbstractWindowedCursor) c; + return w.getWindow(); + } + return null; + } + Object and(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + return c instanceof AbstractCursor && ((AbstractCursor) c).getCount() > 0; + } + Object ternary(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + return c instanceof CrossProcessCursor ? (CrossProcessCursor) c : null; + } + Object early(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (!(c instanceof AbstractWindowedCursor)) { + return null; + } + return (AbstractWindowedCursor) c; + } + } + """, + ), + src( + "p.Unguarded", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Unguarded { + Object otherType(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (c instanceof AbstractCursor) { + return (AbstractWindowedCursor) c; + } + return null; + } + Object otherValue(SQLiteDatabase db, Cursor d) { + Cursor c = db.rawQuery("x", null); + if (d instanceof AbstractWindowedCursor) { + return (AbstractWindowedCursor) c; + } + return null; + } + Object reassigned(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (c instanceof AbstractWindowedCursor) { + c = db.rawQuery("y", null); + return (AbstractWindowedCursor) c; + } + return null; + } + Object afterTheBlock(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (c instanceof AbstractWindowedCursor) { + c.close(); + } + return (AbstractWindowedCursor) c; + } + Object negatedWithoutExit(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (!(c instanceof AbstractWindowedCursor)) { + c.close(); + } + return (AbstractWindowedCursor) c; + } + } + """, + ), + ), + ) + SqliteRewriter.rewrite(classes["p/Guarded"]!!, hierarchy) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Unguarded"]!!, hierarchy) } + for (name in listOf("otherType", "otherValue", "reassigned", "afterTheBlock", "negatedWithoutExit")) { + assertThat(e.message).contains(name) + } + } + + @Test + fun `a query builder subclass that overrides the query taking a CancellationSignal fails the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.MyBuilder", + """ + package p; + import android.database.Cursor; + import android.database.sqlite.*; + import android.os.CancellationSignal; + public class MyBuilder extends SQLiteQueryBuilder { + @Override + public Cursor query(SQLiteDatabase db, String[] p, String s, String[] a, String g, String h, String o, String l, + CancellationSignal c) { + return super.query(db, p, s, a, g, h, o, l, c); + } + @Override + public Cursor query(SQLiteDatabase db, String[] p, String s, String[] a, String g, String h, String o) { + return null; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/MyBuilder"]!!, hierarchy) } + val overrides = e.message!!.lines().filter { "overrides SQLiteQueryBuilder.query" in it } + // the overload without a signal keeps its descriptor in requery, so overriding it still works + assertThat(overrides).hasSize(1) + assertThat(overrides.single()).contains("Landroid/os/CancellationSignal;)Landroid/database/Cursor;'") + assertThat(e.message).contains("p.MyBuilder") + } + + @Test + fun `a call a rule could match on an owner the hierarchy cannot resolve fails the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.UsesLost", + """ + package p; + import android.database.sqlite.*; + public class UsesLost { + void go(Lost l, SQLiteDatabase db, String[] a, android.os.CancellationSignal s) { + l.isStrict(); + l.query(db, a, "w", a, null, null, null, "1", s); + l.rawQuery("x", a); + l.delete("x"); + } + } + """, + ), + // left out of the hierarchy, as a class missing from the runtime classpath is + src( + "p.Lost", + """ + package p; + public class Lost extends android.database.sqlite.SQLiteQueryBuilder { + public android.database.Cursor rawQuery(String s, String[] a) { return null; } + public int delete(String s) { return 0; } + } + """, + ), + ), + ) + val warnings = mutableListOf() + val bytes = classes["p/UsesLost"]!! + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(bytes, hierarchy, warn = warnings::add) } + val unresolved = + e.message!! + .lines() + .filter { "p.Lost cannot be resolved" in it } + .map { it.substringAfter("whether its ").substringBefore(" is ") } + // SQLiteDatabase is final, so no class inherits its rawQuery, and no rule has a delete(String). + // isStrict()Z names no SQLite type, so it only warns; the query descriptor names SQLiteDatabase, so it fails. + assertThat(unresolved).hasSize(1) + assertThat(unresolved[0]).startsWith("query(Landroid/database/sqlite/SQLiteDatabase;") + assertThat(warnings).hasSize(1) + assertThat(warnings.single()).contains("p.Lost cannot be resolved") + assertThat(warnings.single()).contains("isStrict()Z") + } + + @Test + fun `an unresolvable owner whose member names no SQLite type only warns`() { + val classes = + Compiler.compile( + listOf( + src( + "p.UsesRow", + """ + package p; + public class UsesRow { + boolean go(com.unknown.Row r) { return r.isString(0); } + } + """, + ), + // left out of the hierarchy, as an optional class the app does not ship is + src("com.unknown.Row", "package com.unknown; public class Row { public boolean isString(int i) { return false; } }"), + ), + ) + val warnings = mutableListOf() + SqliteRewriter.rewrite(classes["p/UsesRow"]!!, hierarchy, warn = warnings::add) + assertThat(warnings).hasSize(1) + assertThat(warnings.single()).contains("p.UsesRow") + assertThat(warnings.single()).contains("com.unknown.Row cannot be resolved") + assertThat(warnings.single()).contains("isString(I)Z") + } + + @Test + fun `an unresolvable windowed cursor's setWindow(CursorWindow) and mWindow only warn`() { + val classes = + Compiler.compile( + listOf( + src( + "p.UsesWindowed", + """ + package p; + import android.database.CursorWindow; + public class UsesWindowed { + void reset(com.unknown.Windowed c, CursorWindow w) { c.setWindow(w); } + CursorWindow window(com.unknown.Windowed c) { return c.mWindow; } + } + """, + ), + // a library's own windowed cursor, as android's AbstractWindowedCursor declares both members; left + // out of the hierarchy, as an optional class the app does not ship is + src( + "com.unknown.Windowed", + """ + package com.unknown; + public class Windowed { + public android.database.CursorWindow mWindow; + public void setWindow(android.database.CursorWindow w) {} + } + """, + ), + ), + ) + val warnings = mutableListOf() + // CursorWindow is not an SQLite type: it carries every ContentProvider cursor across processes + SqliteRewriter.rewrite(classes["p/UsesWindowed"]!!, hierarchy, warn = warnings::add) + assertThat(warnings).hasSize(2) + assertThat(warnings[0]).contains("its setWindow(Landroid/database/CursorWindow;)V is SQLiteCursor.setWindow") + assertThat(warnings[0]).contains("the call is left as it is") + assertThat(warnings[1]).contains("its mWindowLandroid/database/CursorWindow; is SQLiteCursor.mWindow") + assertThat(warnings[1]).contains("the field access is left as it is") + } + + @Test + fun `an unresolved-owner warning names the method and line, once per place`() { + val source = + """ + package p; + public class Sites { + boolean one(com.unknown.Row r) { + return r.isString(0) && r.isString(1); + } + boolean two(com.unknown.Row r) { + return r.isString(2); + } + } + """ + val classes = + Compiler.compile( + listOf( + src("p.Sites", source), + src("com.unknown.Row", "package com.unknown; public class Row { public boolean isString(int i) { return false; } }"), + ), + ) + + fun lineOf(text: String) = source.lines().indexOfFirst { text in it } + 1 + val warnings = mutableListOf() + SqliteRewriter.rewrite(classes["p/Sites"]!!, hierarchy, warn = warnings::add) + // the two calls on one line make the same warning, logged once + assertThat(warnings.map { it.substringBefore(": ") }) + .containsExactly("p.Sites.one (line ${lineOf("r.isString(0)")})", "p.Sites.two (line ${lineOf("r.isString(2)")})") + .inOrder() + assertThat(warnings.map { it.substringAfter(": ") }.distinct().single()).startsWith("com.unknown.Row cannot be resolved") + } + + @Test + fun `an unresolvable owner whose member names an SQLite type still fails`() { + val classes = + Compiler.compile( + listOf( + src( + "p.UsesQb", + """ + package p; + import android.database.sqlite.*; + public class UsesQb { + void go(com.unknown.Qb q, SQLiteDatabase db, String[] a, android.os.CancellationSignal s) { + q.query(db, a, "w", a, null, null, null, "1", s); + } + } + """, + ), + src( + "com.unknown.Qb", + """ + package com.unknown; + public class Qb extends android.database.sqlite.SQLiteQueryBuilder {} + """, + ), + ), + ) + val warnings = mutableListOf() + val bytes = classes["p/UsesQb"]!! + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(bytes, hierarchy, warn = warnings::add) } + assertThat(e.message).contains("com.unknown.Qb cannot be resolved") + assertThat(warnings).isEmpty() + } + + @Test + fun `an SQLite cursor used as an Android cursor class without a cast fails the build, and would not verify`() { + fun up(pkg: String) = + src( + "$pkg.Up", + """ + package $pkg; + import android.database.*; + import android.database.sqlite.*; + public class Up { + AbstractCursor field; + AbstractCursor returned(SQLiteCursor c) { return c; } + void passed(SQLiteCursor c) { take(c); } + static void take(AbstractWindowedCursor w) {} + void stored(SQLiteCursor c) { field = c; } + int calledOn(SQLiteCursor c) { + AbstractCursor a = c; + return a.getCount(); + } + Object inFrame(SQLiteCursor c, boolean b) { + AbstractCursor a = c; + if (b) { + a = null; + } + return a; + } + Cursor fine(SQLiteCursor c, Cursor other) { + Cursor k = c; + Object o = c; + AbstractCursor a = (AbstractCursor) other; + return k; + } + } + """, + ) + val classes = Compiler.compile(listOf(up("p"))) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Up"]!!, hierarchy) } + for (name in listOf("returned", "passed", "stored", "calledOn", "inFrame")) { + assertThat(e.message).contains(" - $name(") + } + assertThat(e.message).contains("an SQLite cursor used as android.database.AbstractWindowedCursor without a cast") + assertThat(e.message).contains("an SQLite cursor used as android.database.AbstractCursor without a cast") + assertThat(e.message).doesNotContain("fine(") + // The failure mode: rewritten in a package the fail rule exempts, the same class does not verify + val unverified = assertThrows(IllegalStateException::class.java) { transform(up("androidx.sqlite.db.framework")) } + assertThat(unverified.message).contains("does not verify") + assertThat(unverified.message).contains("androidx/sqlite/db/framework/Up") + } + + @Test + fun `an SQLite cursor stored into an array of an Android cursor class fails the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Stores", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Stores { + void param(AbstractCursor[] a, SQLiteCursor c) { + a[0] = c; + } + AbstractWindowedCursor[] fresh(SQLiteCursor c) { + AbstractWindowedCursor[] a = new AbstractWindowedCursor[1]; + a[0] = c; + return a; + } + void crossProcess(CrossProcessCursor[] a, SQLiteCursor c) { + Object[] same = a; + same[0] = c; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Stores"]!!, hierarchy) } + assertThat(e.message).contains("'p.Stores'") + assertThat(e.message).contains( + " - param([Landroid/database/AbstractCursor;Landroid/database/sqlite/SQLiteCursor;)V (line 7): an SQLite cursor stored " + + "into an array of android.database.AbstractCursor; requery's SQLiteCursor is not one, so the store would throw ArrayStoreException", + ) + assertThat(e.message).contains( + " - fresh(Landroid/database/sqlite/SQLiteCursor;)[Landroid/database/AbstractWindowedCursor; (line 11): " + + "an SQLite cursor stored into an array of android.database.AbstractWindowedCursor", + ) + // the Object[] local holds the parameter's array, which is a CrossProcessCursor[] on every path + assertThat(e.message).contains( + " - crossProcess([Landroid/database/CrossProcessCursor;Landroid/database/sqlite/SQLiteCursor;)V (line 16): " + + "an SQLite cursor stored into an array of android.database.CrossProcessCursor", + ) + } + + @Test + fun `an SQLite cursor stored into a Cursor or Object array, or another value into an Android cursor array, passes`() { + val classes = + Compiler.compile( + listOf( + src( + "p.StoresFine", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class StoresFine { + void cursors(Cursor[] a, SQLiteCursor c) { + a[0] = c; + } + void objects(Object[] a, SQLiteCursor c) { + a[0] = c; + } + void otherValues(AbstractCursor[] a, MatrixCursor m) { + a[0] = m; + a[1] = null; + } + } + """, + ), + ), + ) + SqliteRewriter.rewrite(classes["p/StoresFine"]!!, hierarchy) + } + + @Test + fun `a store whose array is an Android cursor array on one path only is not checked`() { + val classes = + Compiler.compile( + listOf( + src( + "p.StoresMerged", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class StoresMerged { + void merged(AbstractCursor[] a, Cursor[] k, SQLiteCursor c, boolean b) { + Cursor[] t = b ? a : k; + t[0] = c; + } + } + """, + ), + ), + ) + // a known limit: when b is true, the store throws ArrayStoreException at run time + SqliteRewriter.rewrite(classes["p/StoresMerged"]!!, hierarchy) + } + + @Test + fun `an instanceof guard counts only when it covers every path to the cast`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Partly", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Partly { + Object or(SQLiteDatabase db, boolean b) { + Cursor c = db.rawQuery("x", null); + if (b || c instanceof AbstractCursor) { + return (AbstractCursor) c; + } + return null; + } + Object beforeTheLoop(SQLiteDatabase db, int n) { + Cursor c = db.rawQuery("x", null); + for (int i = 0; i < n; i++) { + if (!(c instanceof AbstractWindowedCursor)) { + return null; + } + c = db.rawQuery("y", null); + } + return (AbstractWindowedCursor) c; + } + } + """, + ), + src( + "p.Everywhere", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Everywhere { + Object andAfter(SQLiteDatabase db, boolean b) { + Cursor c = db.rawQuery("x", null); + if (c instanceof AbstractCursor && b) { + return (AbstractCursor) c; + } + return null; + } + Object onBothBranches(SQLiteDatabase db, boolean b) { + Cursor c = db.rawQuery("x", null); + if (b) { + if (!(c instanceof AbstractWindowedCursor)) return null; + } else { + if (!(c instanceof AbstractWindowedCursor)) return null; + } + Cursor copy = c; + return (AbstractWindowedCursor) copy; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Partly"]!!, hierarchy) } + // b alone reaches the cast in or; the loop may run zero times, or end on the unchecked cursor + assertThat(e.message).contains(" - or(") + assertThat(e.message).contains(" - beforeTheLoop(") + SqliteRewriter.rewrite(classes["p/Everywhere"]!!, hierarchy) + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupport.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupport.kt new file mode 100644 index 0000000000..782d613ffd --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupport.kt @@ -0,0 +1,335 @@ +package com.itsaky.androidide.gradle.sqlite + +import org.junit.jupiter.api.Assumptions.assumeTrue +import org.objectweb.asm.ClassReader +import org.objectweb.asm.Opcodes +import org.objectweb.asm.tree.ClassNode +import java.io.File +import java.net.URI +import java.nio.file.Files +import java.util.concurrent.TimeUnit +import java.util.zip.ZipFile +import javax.tools.JavaFileObject +import javax.tools.SimpleJavaFileObject +import javax.tools.ToolProvider + +/** + * Prints one line naming what the environment lacks. Then skips the test locally, where not everyone has + * every jar, but fails it when the environment variable CI is set: a CI run that skips these tests + * looks green while checking nothing. + */ +internal fun requireEnvironment( + present: Boolean, + missing: String, +) { + if (present) return + val onCi = !System.getenv("CI").isNullOrEmpty() + System.err.println("SQLITE REWRITE TESTS: missing $missing (${if (onCi) "failing, CI is set" else "skipping, set CI to fail instead"})") + if (onCi) throw AssertionError("The environment lacks $missing, and CI is set, so the SQLite rewrite tests do not skip") + assumeTrue(false, missing) +} + +/** + * The Gradle user home whose cache holds the jars: the one of the build that runs the tests, which the test tasks pass + * as cogo.test.gradleUserHome, then GRADLE_USER_HOME, then ~/.gradle, as from an IDE. + */ +internal fun gradleUserHome( + property: String? = System.getProperty("cogo.test.gradleUserHome"), + env: String? = System.getenv("GRADLE_USER_HOME"), + userHome: File = File(System.getProperty("user.home")), +): File = property?.takeIf { it.isNotEmpty() }?.let(::File) ?: env?.takeIf { it.isNotEmpty() }?.let(::File) ?: File(userHome, ".gradle") + +/** The module cache of [gradleHome]: group/artifact/version//file. */ +internal fun moduleCache(gradleHome: File = gradleUserHome()): File = File(gradleHome, "caches/modules-2/files-2.1") + +/** [file] of [group]:[artifact]:[version] in the module cache of [gradleHome], or null. */ +internal fun gradleCacheFile( + group: String, + artifact: String, + version: String, + file: String, + gradleHome: File = gradleUserHome(), +): File? = + File(moduleCache(gradleHome), "$group/$artifact/$version") + .walkTopDown() + .firstOrNull { it.name == file } + +/** requery's AAR: the file REQUERY_AAR names, else the one in the module cache of [gradleHome], or null. */ +internal fun requeryAar( + env: String? = System.getenv("REQUERY_AAR"), + gradleHome: File = gradleUserHome(), +): File? = + env?.let(::File)?.takeIf { it.isFile } + ?: gradleCacheFile("com.github.requery", "sqlite-android", "3.49.0", "sqlite-android-3.49.0.aar", gradleHome) + +/** Jars the tests compile and resolve against. Each lookup skips the test with a message, or fails it on CI. */ +internal object TestJars { + private val home = File(System.getProperty("user.home")) + private val modules by lazy { moduleCache() } + + val androidJar: File by lazy { + val sdkRoots = + listOfNotNull( + System.getenv("ANDROID_HOME"), + System.getenv("ANDROID_SDK_ROOT"), + generateSequence(File("").absoluteFile) { it.parentFile } + .map { File(it, "local.properties") } + .firstOrNull { it.isFile } + ?.readLines() + ?.firstOrNull { it.startsWith("sdk.dir=") } + ?.substringAfter("="), + File(home, "Android/Sdk").path, + ) + val jar = + sdkRoots + .flatMap { root -> listOf(36, 35, 34, 33).map { File(root, "platforms/android-$it/android.jar") } } + .firstOrNull { it.isFile } + requireEnvironment(jar != null, "No android.jar found: set ANDROID_HOME or sdk.dir in local.properties") + jar!! + } + + private fun classesJarOf( + aar: File, + name: String, + ): File { + val out = Files.createTempDirectory("sqlite-rewrite-test").toFile().also { it.deleteOnExit() } + val jar = File(out, "$name.jar") + ZipFile(aar).use { zip -> + zip.getInputStream(zip.getEntry("classes.jar")).use { input -> jar.outputStream().use { input.copyTo(it) } } + } + return jar + } + + val requeryJar: File by lazy { + val aar = requeryAar() + requireEnvironment(aar != null, "requery 3.49.0 AAR not found: set REQUERY_AAR or resolve it into the Gradle cache") + classesJarOf(aar!!, "requery") + } + + val androidxSqliteJar: File by lazy { + val aar = gradleCacheFile("androidx.sqlite", "sqlite-android", "2.6.2", "sqlite.aar") + requireEnvironment(aar != null, "androidx.sqlite 2.6.2 AAR not found in the Gradle cache") + classesJarOf(aar!!, "androidx-sqlite") + } + + val androidxSqliteFrameworkJar: File by lazy { + val aar = gradleCacheFile("androidx.sqlite", "sqlite-framework-android", "2.6.2", "sqlite-framework.aar") + requireEnvironment(aar != null, "androidx.sqlite framework 2.6.2 AAR not found in the Gradle cache") + classesJarOf(aar!!, "androidx-sqlite-framework") + } + + /** The Kotlin classes the framework classes call; null when the Gradle cache has none. */ + val kotlinStdlibJar: File? by lazy { + File(modules, "org.jetbrains.kotlin/kotlin-stdlib") + .walkTopDown() + .filter { it.name.startsWith("kotlin-stdlib-") && it.name.endsWith(".jar") && "sources" !in it.name } + .maxByOrNull { it.name } + } + + val androidxCoreJar: File by lazy { + val aar = gradleCacheFile("androidx.core", "core", "1.13.1", "core-1.13.1.aar") + requireEnvironment(aar != null, "androidx.core 1.13.1 AAR not found in the Gradle cache") + classesJarOf(aar!!, "androidx-core") + } +} + +/** A finished process: its exit code, and its standard output and error together. */ +internal class ProcessResult( + val exitCode: Int, + val output: String, +) + +/** + * Runs [command] with its output going to a temp file, so the time limit holds even for a process, or a child it + * leaves behind, that keeps the output open: reading a pipe to its end would wait for that. On timeout it destroys + * the process and fails with what it printed. + */ +internal fun runBounded( + command: List, + timeoutSeconds: Long, +): ProcessResult { + val log = Files.createTempFile("sqlite-test-process", ".log").toFile() + try { + val process = + ProcessBuilder(command) + .redirectErrorStream(true) + .redirectOutput(log) + .start() + if (!process.waitFor(timeoutSeconds, TimeUnit.SECONDS)) { + process.destroyForcibly().waitFor(10, TimeUnit.SECONDS) + throw AssertionError("${command.joinToString(" ")} did not finish in $timeoutSeconds s; it printed:\n${log.readText()}") + } + return ProcessResult(process.exitValue(), log.readText()) + } finally { + log.delete() + } +} + +internal class JavaSource( + val className: String, + val code: String, +) : SimpleJavaFileObject(URI.create("string:///${className.replace('.', '/')}.java"), JavaFileObject.Kind.SOURCE) { + override fun getCharContent(ignoreEncodingErrors: Boolean) = code +} + +internal object Compiler { + /** Class bytes by internal name, compiled with debug info (line numbers) against [classpath]. */ + fun compile( + sources: List, + classpath: List = listOf(TestJars.androidJar), + ): Map { + val compiler = ToolProvider.getSystemJavaCompiler() ?: error("No system Java compiler: run the tests on a JDK") + val out = Files.createTempDirectory("sqlite-rewrite-classes").toFile().also { it.deleteOnExit() } + val diagnostics = StringBuilder() + val task = + compiler.getTask( + null, + null, + { diagnostics.appendLine(it.toString()) }, + listOf( + "-g", + "-proc:none", + "-source", + "8", + "-target", + "8", + "-Xlint:-options", + "-d", + out.path, + "-classpath", + classpath.joinToString(File.pathSeparator), + ), + null, + sources, + ) + check(task.call()) { "Sample failed to compile:\n$diagnostics" } + return out + .walkTopDown() + .filter { it.extension == "class" } + .associate { it.relativeTo(out).path.removeSuffix(".class") to it.readBytes() } + } +} + +/** Class members of jars, for resolving a reference the way the JVM does. */ +internal class ClassIndex( + jars: List, +) { + private val classes = HashMap() + + init { + for (jar in jars) { + ZipFile(jar).use { zip -> + for (entry in zip.entries()) { + if (entry.name.endsWith(".class")) { + classes[entry.name.removeSuffix(".class")] = + ClassNode().also { ClassReader(zip.getInputStream(entry)).accept(it, ClassReader.SKIP_CODE) } + } + } + } + } + } + + fun addClass(bytes: ByteArray) { + val n = ClassNode().also { ClassReader(bytes).accept(it, ClassReader.SKIP_CODE) } + classes[n.name] = n + } + + private fun load(name: String): ClassNode? = + classes[name] + ?: ClassLoader.getSystemResourceAsStream("$name.class")?.use { s -> + ClassNode().also { ClassReader(s).accept(it, ClassReader.SKIP_CODE) } + } + + fun knows(name: String) = load(name) != null + + fun superClass(name: String): String? = load(name)?.superName + + fun superTypes(name: String): List { + val result = mutableListOf() + val pending = ArrayDeque(listOf(name)) + while (pending.isNotEmpty()) { + val n = pending.removeFirst() + val c = load(n) ?: continue + if (n != name) result += n + c.superName?.let(pending::add) + pending.addAll(c.interfaces) + } + return result + } + + private fun packageOf(name: String) = name.substringBeforeLast('/', "") + + private fun accessible( + access: Int, + declaring: String, + caller: String?, + protectedOk: Boolean, + ): Boolean = + when { + access and Opcodes.ACC_PUBLIC != 0 -> { + true + } + + access and Opcodes.ACC_PRIVATE != 0 -> { + caller == declaring + } + + access and Opcodes.ACC_PROTECTED != 0 -> { + protectedOk || ( + caller != null && ( + caller == declaring || declaring in + superTypes( + caller, + ) || packageOf(caller) == packageOf(declaring) + ) + ) + } + + else -> { + caller != null && packageOf(caller) == packageOf(declaring) + } + } + + /** + * The class declaring the member, or null when the JVM would not link the reference: the member is + * missing, static where the call is not (or the reverse), or not accessible from [caller], a class + * in [classes] (null: an unrelated class in another package). [protectedOk] accepts protected members + * for a caller that is not a subclass, as a probe that stands in for one. A constructor is looked up + * in [owner] only: it is not inherited. + */ + fun resolve( + owner: String, + name: String, + desc: String, + field: Boolean, + isStatic: Boolean? = null, + caller: String? = null, + protectedOk: Boolean = false, + ): String? { + val ownerNode = load(owner) ?: return null + if (!accessible(ownerNode.access, owner, caller, false)) return null + val pending = ArrayDeque(listOf(owner)) + val seen = HashSet() + while (pending.isNotEmpty()) { + val n = pending.removeFirst() + if (!seen.add(n)) continue + val c = load(n) ?: continue + val access = + if (field) { + c.fields.firstOrNull { it.name == name && it.desc == desc }?.access + } else { + c.methods.firstOrNull { it.name == name && it.desc == desc }?.access + } + if (access != null) { + if (isStatic != null && (access and Opcodes.ACC_STATIC != 0) != isStatic) return null + return if (accessible(access, n, caller, protectedOk)) n else null + } + if (!field && name == "") return null + c.superName?.let(pending::add) + pending.addAll(c.interfaces) + } + return null + } + + fun isInterface(name: String) = load(name)?.let { it.access and Opcodes.ACC_INTERFACE != 0 } ?: false +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt new file mode 100644 index 0000000000..e3290bc65c --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt @@ -0,0 +1,83 @@ +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File +import java.time.Duration + +/** + * The test support's own logic, which the rewrite tests rely on to find their jars and to tell a linking reference + * from a broken one. + */ +class SqliteTestSupportTest { + @Test + fun `the jars are looked up in the Gradle user home of the build that runs the tests`( + @TempDir dir: File, + ) { + val home = File(dir, "home") + val active = File(dir, "active-gradle-home") + val env = File(dir, "env-gradle-home") + + assertThat(gradleUserHome(active.path, env.path, home)).isEqualTo(active) + // run from an IDE, without the property the test tasks pass + assertThat(gradleUserHome(null, env.path, home)).isEqualTo(env) + assertThat(gradleUserHome(null, null, home)).isEqualTo(File(home, ".gradle")) + } + + @Test + fun `the requery AAR is found when only a Gradle user home other than the default one has it`( + @TempDir dir: File, + ) { + val home = File(dir, "home") + // the default home's cache exists, without requery + File(home, ".gradle/caches/modules-2/files-2.1").mkdirs() + val active = File(dir, "active-gradle-home") + val aar = File(active, "caches/modules-2/files-2.1/com.github.requery/sqlite-android/3.49.0/0123abcd/sqlite-android-3.49.0.aar") + aar.parentFile.mkdirs() + aar.writeText("aar") + + assertThat(requeryAar(env = null, gradleHome = gradleUserHome(active.path, null, home))).isEqualTo(aar) + // GRADLE_USER_HOME, as when the tests run from an IDE + assertThat(requeryAar(env = null, gradleHome = gradleUserHome(null, active.path, home))).isEqualTo(aar) + } + + @Test + fun `a constructor resolves in the named class only, while methods are still inherited`() { + val classes = + Compiler.compile( + listOf( + JavaSource("p.Parent", "package p; public class Parent { public Parent() {} public void m() {} }"), + JavaSource("p.Child", "package p; public class Child extends Parent { public Child(int i) {} }"), + ), + classpath = emptyList(), + ) + val index = ClassIndex(emptyList()) + classes.values.forEach(index::addClass) + + assertThat(index.resolve("p/Child", "", "(I)V", field = false)).isEqualTo("p/Child") + // the JVM throws NoSuchMethodError for `new Child()`: Parent's constructor is not Child's + assertThat(index.resolve("p/Child", "", "()V", field = false)).isNull() + assertThat(index.resolve("p/Child", "m", "()V", field = false)).isEqualTo("p/Parent") + } + + @Test + fun `a process that keeps its output open fails at its time limit, with what it printed`() { + val started = System.nanoTime() + + val e = assertThrows(AssertionError::class.java) { runBounded(listOf("bash", "-c", "echo started; sleep 20"), timeoutSeconds = 1) } + + assertThat(e.message).contains("did not finish in 1 s") + assertThat(e.message).contains("started") + assertThat(Duration.ofNanos(System.nanoTime() - started)).isLessThan(Duration.ofSeconds(15)) + } + + @Test + fun `a process that finishes gives its exit code and output`() { + val result = runBounded(listOf("bash", "-c", "echo out; echo err >&2; exit 3"), timeoutSeconds = 30) + + assertThat(result.exitCode).isEqualTo(3) + assertThat(result.output.lines()).containsAtLeast("out", "err") + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index cdc27caaf2..0e023731a4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -291,6 +291,9 @@ composeRuntimeDaemonTests = { module = "androidx.compose.runtime:runtime-android # Only :quickbuild:daemon uses this, and it takes the jar from the on-device Gradle # distribution rather than the APK - so the version is the distribution's, not ours. ow2-asm = { module = "org.ow2.asm:asm", version = "9.9" } +ow2-asmAnalysis = { module = "org.ow2.asm:asm-analysis", version = "9.9" } +ow2-asmCommons = { module = "org.ow2.asm:asm-commons", version = "9.9" } +ow2-asmTree = { module = "org.ow2.asm:asm-tree", version = "9.9" } # Tests tests-junit = { module = "junit:junit", version = "4.13.2" } @@ -299,6 +302,8 @@ tests-junit-platformLauncher = { module = "org.junit.platform:junit-platform-lau tests-junit-vintageEngine = { module = "org.junit.vintage:junit-vintage-engine", version.ref = "junit-jupiter" } core-tests-anroidx-arch = { module = "androidx.arch.core:core-testing", version.ref = "anroidx-test-core" } tests-google-truth = { module = "com.google.truth:truth", version = "1.4.1" } +# The requery the ADFA-7082 template ships in libs/ (JitPack); :gradle-plugin's sqliteReleaseApkTest builds against it +tests-requery-sqliteAndroid = { module = "com.github.requery:sqlite-android", version = "3.49.0" } tests-robolectric = { module = "org.robolectric:robolectric", version = "4.11.1" } tests-androidx-test-core = { module = "androidx.test:core", version = "1.5.0" } tests-androidx-test-monitor = { module = "androidx.test:monitor", version = "1.8.0" } diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt index f0dcf22769..454e570131 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt @@ -29,6 +29,8 @@ import java.io.File * @property libraryResourceFlats pre-compiled `.flat` resource units from the proxy app build, * passed to `aapt2 link` as `-R` overlays so a relink can resolve a resource only a dependency * AAR declares (e.g. Material3's `Theme.Material3.DayNight.NoActionBar`). + * @property gradleUserHome the Gradle user home, whose `gradle.properties` is watched with the + * project's own; null when there is none to watch. */ class QuickBuildProjectLayout( val projectRoot: File, @@ -37,6 +39,7 @@ class QuickBuildProjectLayout( private val extraSourceRoots: List = emptyList(), private val stableIdsFile: File? = null, private val libraryResourceFlats: List = emptyList(), + private val gradleUserHome: File? = null, ) { private val mainDir = File(appModuleDir, "src/main") @@ -107,18 +110,25 @@ class QuickBuildProjectLayout( /** * Exact files watched outside the roots (gradle config; changes invalidate). * - * @return the root's settings/properties/version-catalog files plus both build-script - * spellings for every module, listed unconditionally - only the existing ones are polled. + * @return the root's settings/properties/version-catalog files, both build-script spellings + * and a `gradle.properties` for every module [moduleDirs] returns (the app module Quick Build + * builds, and each module the scan finds up to [MODULE_SCAN_MAX_DEPTH] levels below the root), + * and the Gradle user home's `gradle.properties`, listed unconditionally - only the existing + * ones are polled. So turning `QuickBuildProjectSupport.isSqliteDebuggerEnabled`'s opt-in on + * in one of those files invalidates a live session. Another app module nested deeper is read + * at the next proxy app build (a provision or a rebaseline), but its `gradle.properties` is + * not watched, so turning the opt-in on there does not end a running session. */ fun watchedFiles(): List = - listOf( + listOfNotNull( File(projectRoot, "settings.gradle"), File(projectRoot, "settings.gradle.kts"), File(projectRoot, "gradle.properties"), File(projectRoot, "gradle/libs.versions.toml"), + gradleUserHome?.let { File(it, "gradle.properties") }, ) + moduleDirs().flatMap { - listOf(File(it, "build.gradle"), File(it, "build.gradle.kts")) + listOf(File(it, "build.gradle"), File(it, "build.gradle.kts"), File(it, "gradle.properties")) } /** diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildMessage.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildMessage.kt index 325344d2fe..7964fcaf8c 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildMessage.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildMessage.kt @@ -20,6 +20,13 @@ sealed interface QuickBuildMessage { val text: String, ) : QuickBuildMessage + /** + * Quick Build is deliberately off for this project because the SQLite debugger is on + * (`cogo.sqliteDebugger=true`). Nothing failed and a retry cannot help, so the session records + * it apart from a failed start ([QuickBuildSessionState.Idle.startRefused]). + */ + data object DisabledBySqliteDebugger : QuickBuildMessage + /** * The OS wants the reinstall confirmed but no dialog could be shown, because CoGo was not * in the foreground to host it. Returning to CoGo is what re-prompts. diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildSessionState.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildSessionState.kt index 522e896738..bd45278231 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildSessionState.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildSessionState.kt @@ -19,9 +19,14 @@ sealed interface QuickBuildSessionState { * failure flash. Cleared by the next tap (which starts a fresh provision anyway) or the next * save ([SessionEvent.FileSaved]) - the save clears the tone only and never retries the * start; a retry stays a tap. + * @property startRefused the failed start was a deliberate refusal + * ([QuickBuildMessage.DisabledBySqliteDebugger]) rather than a fault, so the status line says + * Quick Build is off instead of offering a retry that cannot help. Only meaningful with + * [lastStartFailed]; cleared by the same gestures. */ data class Idle( val lastStartFailed: Boolean = false, + val startRefused: Boolean = false, ) : QuickBuildSessionState /** @@ -33,10 +38,12 @@ sealed interface QuickBuildSessionState { * @property lastStartFailed carried from [Idle.lastStartFailed] so the silent warm build does * not clear the failed-start error tone on its way through; a queued tap clears it, and a * tapless finish hands it back to [Idle]. + * @property startRefused carried from [Idle.startRefused] alongside [lastStartFailed]. */ data class Prebuilding( val tapQueued: Boolean = false, val lastStartFailed: Boolean = false, + val startRefused: Boolean = false, ) : QuickBuildSessionState /** diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildStatus.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildStatus.kt index 78480dcd52..c53d6b3723 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildStatus.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildStatus.kt @@ -19,9 +19,12 @@ sealed interface QuickBuildStatus { * ([QuickBuildSessionState.Idle.lastStartFailed]), so the bolt keeps the error tone until * the next tap or save; carried here because a failed start rests in Hidden and the tone * is derived from status alone. + * @property startRefused that failed start was Quick Build deliberately declining the project + * ([QuickBuildSessionState.Idle.startRefused]), not a fault; surfaces word it differently. */ data class Hidden( val lastStartFailed: Boolean = false, + val startRefused: Boolean = false, ) : QuickBuildStatus /** @@ -120,7 +123,7 @@ sealed interface QuickBuildStatus { fun from(state: QuickBuildSessionState): QuickBuildStatus = when (state) { is QuickBuildSessionState.Idle -> { - Hidden(state.lastStartFailed) + Hidden(state.lastStartFailed, state.startRefused) } // A warm-up the user never asked for stays invisible - but it must not clear a @@ -128,7 +131,7 @@ sealed interface QuickBuildStatus { is QuickBuildSessionState.Prebuilding -> { // A warm build has no baseline to replace, so a tap that queues on one is // always a session's first provision. - if (state.tapQueued) Provisioning() else Hidden(state.lastStartFailed) + if (state.tapQueued) Provisioning() else Hidden(state.lastStartFailed, state.startRefused) } is QuickBuildSessionState.Provisioning -> { diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/SessionReducer.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/SessionReducer.kt index 04870a25ab..59fcf0a523 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/SessionReducer.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/SessionReducer.kt @@ -99,7 +99,10 @@ class SessionReducer { // The flag rides along so the silent warm build cannot clear a failed-start // tone: only a tap or a save is a user gesture. SessionTransition( - QuickBuildSessionState.Prebuilding(lastStartFailed = state.lastStartFailed), + QuickBuildSessionState.Prebuilding( + lastStartFailed = state.lastStartFailed, + startRefused = state.startRefused, + ), listOf(SessionEffect.StartProxyAppPrebuild), ) } @@ -164,13 +167,16 @@ class SessionReducer { // the tooling server); it queues and fires on PrebuildFinished. The tap is also // the retry gesture, so it clears a carried failed-start tone. is SessionEvent.QuickBuildTapped -> { - SessionTransition(state.copy(tapQueued = true, lastStartFailed = false), listOf(SessionEffect.RecordAsk)) + SessionTransition( + state.copy(tapQueued = true, lastStartFailed = false, startRefused = false), + listOf(SessionEffect.RecordAsk), + ) } SessionEvent.FileSaved -> { // Same clearing gesture as in Idle; the warm build itself is not one. if (state.lastStartFailed) { - SessionTransition(state.copy(lastStartFailed = false)) + SessionTransition(state.copy(lastStartFailed = false, startRefused = false)) } else { SessionTransition(state) } @@ -186,7 +192,12 @@ class SessionReducer { } else { // A carried failed-start tone goes back to Idle uncleared: the warm build's // outcome is silent either way, and only a tap or a save clears the tone. - SessionTransition(QuickBuildSessionState.Idle(lastStartFailed = state.lastStartFailed)) + SessionTransition( + QuickBuildSessionState.Idle( + lastStartFailed = state.lastStartFailed, + startRefused = state.startRefused, + ), + ) } } @@ -300,7 +311,10 @@ class SessionReducer { // lastStartFailed keeps the error tone on the bolt after the failure flash // fades - a plain Idle here read READY right after a failed start (Q8). SessionTransition( - QuickBuildSessionState.Idle(lastStartFailed = true), + QuickBuildSessionState.Idle( + lastStartFailed = true, + startRefused = event.message == QuickBuildMessage.DisabledBySqliteDebugger, + ), listOf(SessionEffect.WithdrawAsk, SessionEffect.SurfaceProvisioningError(event.message)), ) } diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManager.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManager.kt index e9965a25e1..abf3b76a7f 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManager.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManager.kt @@ -1362,7 +1362,13 @@ class QuickBuildSessionManager( // event, so a still-broken build file does not loop. session.orchestrator.onProxyAppRebuildFailed() val park = rebuildPark - if (park != null) { + if (result.message == QuickBuildMessage.DisabledBySqliteDebugger) { + // Not a build failure: the user turned the SQLite debugger on mid-session, so + // Quick Build is off for this project. A park would retry on every save and be + // refused each time; end like a refused start (Idle, "off" status) instead. + log.info("Proxy app rebuild refused: the SQLite debugger is on; ending the session") + dispatch(SessionEvent.ProvisioningFailed(result.message)) + } else if (park != null) { // Park recoverable instead of dying to Idle: the session and the running proxy // app are both fine; what failed is the user's build files. The message is // surfaced here rather than through SurfaceProvisioningError, whose effect diff --git a/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayoutTest.kt b/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayoutTest.kt index cdb7410ea8..9342209127 100644 --- a/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayoutTest.kt +++ b/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayoutTest.kt @@ -1,6 +1,7 @@ package org.appdevforall.cotg.quickbuild.data import com.google.common.truth.Truth.assertThat +import org.appdevforall.cotg.quickbuild.domain.watch.WatchFilter import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir import java.io.File @@ -142,6 +143,27 @@ class QuickBuildProjectLayoutTest { ) } + @Test + fun `watchedFiles includes every gradle properties file the sqlite debugger opt-in reads`() { + write("app/build.gradle.kts") + write("feature-login/build.gradle") + val home = File(root, "gradle-user-home") + + val layout = QuickBuildProjectLayout(root, gradleUserHome = home) + val watched = layout.watchedFiles() + + assertThat(watched).containsAtLeast( + File(root, "gradle.properties"), + File(root, "app/gradle.properties"), + File(root, "feature-login/gradle.properties"), + File(home, "gradle.properties"), + ) + // The session's filter must pass a change to each of them, or the opt-in goes unseen. + val filter = WatchFilter(layout.watchedRoots(), watched) + assertThat(filter.isRelevant(File(home, "gradle.properties"))).isTrue() + assertThat(filter.isRelevant(File(root, "feature-login/gradle.properties"))).isTrue() + } + @Test fun `module discovery skips build intermediates and hidden dirs`() { write("app/build.gradle.kts") diff --git a/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManagerTest.kt b/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManagerTest.kt index 61da2de072..abe7ec2d22 100644 --- a/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManagerTest.kt +++ b/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManagerTest.kt @@ -1682,6 +1682,28 @@ class QuickBuildSessionManagerTest { assertThat(userMessages).contains(QuickBuildMessage.Literal("manifest does not build")) } + @Test + fun `a rebuild refused for the SQLite debugger ends in the refused state with no retry`() = + runTest { + proxyAppRebuildOutcome = { ProxyAppRebuildOutcome.Failure(QuickBuildMessage.DisabledBySqliteDebugger) } + val manager = createManager() + manager.onQuickBuildTapped() + advanceUntilIdle() + + manager.save(gradleFile) + advanceUntilIdle() + + // Same state as a refused start, not a parked failed rebuild. + assertThat(manager.state.value) + .isEqualTo(QuickBuildSessionState.Idle(lastStartFailed = true, startRefused = true)) + assertThat(userMessages).contains(QuickBuildMessage.DisabledBySqliteDebugger) + assertThat(proxyAppRebuildCount).isEqualTo(1) + + manager.save(gradleFile) + advanceUntilIdle() + assertThat(proxyAppRebuildCount).isEqualTo(1) + } + // Review gap (2026-07-26 #69): pin the failed proxy app rebuild's DAEMON state and the // recovery - the session must stay recoverable, not linger wedged and daemon-less. @Test diff --git a/resources/src/main/res/values/strings.xml b/resources/src/main/res/values/strings.xml index ce99d1624d..9ec6aa932f 100644 --- a/resources/src/main/res/values/strings.xml +++ b/resources/src/main/res/values/strings.xml @@ -1218,6 +1218,9 @@ Quick Build: starting a full build… Quick Build failed - see Build Output Quick Build: setup failed - tap to retry + Quick Build: off while the SQLite debugger is on - tap for details + + show why Quick Build is off Quick Build failed. See the Build Output panel for more details. Quick Build successful Quick Build compiler restarting… @@ -1233,6 +1236,7 @@ Quick Build needs an Android app module, and this project has none. Quick Build needs a debuggable build variant, and \"%1$s\" is a release variant. Open Build Variants in the sidebar and select a debug variant. Quick Build could not set up \"%1$s\". If that variant is not debuggable, open Build Variants in the sidebar and select a debug variant. + Quick Build is off while the SQLite debugger is on (cogo.sqliteDebugger=true in gradle.properties), because it would compile your source without the move to requery. Use Run to build the app, or remove that line to use Quick Build. Quick Build setup failed. Check the Build Output for what went wrong. Another build is running. Wait for it to finish, then start Quick Build again. Quick Build could not rebuild your app. Check the Build Output for what went wrong. diff --git a/scripts/check-release-apk-no-sqlite-debugger.sh b/scripts/check-release-apk-no-sqlite-debugger.sh new file mode 100755 index 0000000000..53a6cf42f9 --- /dev/null +++ b/scripts/check-release-apk-no-sqlite-debugger.sh @@ -0,0 +1,98 @@ +#!/usr/bin/env bash +# Usage: check-release-apk-no-sqlite-debugger.sh +# +# Fails (exit 1, one line per finding) when an APK carries the SQLite debugger (ADFA-7131): +# - a lib//libsqlitedbg.so entry, +# - the provider org.appdevforall.sqlitedebugger.DebuggerStartup in the manifest, +# - a package com.itsaky.androidide in the manifest, +# - the class descriptor prefix Lorg/appdevforall/sqlitedebugger/ in any classes*.dex. +# INTERNET is only a WARNING line, not a failure: the app may declare it for itself, and the +# manifest cannot say which declaration it came from. Without the provider above, treat the +# warning as the app's own. +# Exit 2 on bad usage or when aapt2, unzip or the APK cannot be found. +# +# aapt2 comes from $AAPT2, else the newest build-tools under $ANDROID_HOME / $ANDROID_SDK_ROOT +# / ~/Android/Sdk. + +set -u +set -o pipefail + +if [ $# -ne 1 ] || [ ! -f "$1" ]; then + echo "usage: $0 " >&2 + exit 2 +fi +apk=$1 + +if ! command -v unzip >/dev/null 2>&1; then + echo "unzip not found: install it to inspect the APK" >&2 + exit 2 +fi + +aapt2=${AAPT2:-} +if [ -z "$aapt2" ]; then + for sdk in "${ANDROID_HOME:-}" "${ANDROID_SDK_ROOT:-}" "$HOME/Android/Sdk"; do + [ -n "$sdk" ] || continue + candidate=$(ls -d "$sdk"/build-tools/*/aapt2 2>/dev/null | sort -V | tail -n 1) + if [ -n "$candidate" ]; then + aapt2=$candidate + break + fi + done +fi +if [ -z "$aapt2" ] || [ ! -x "$aapt2" ]; then + echo "aapt2 not found: set AAPT2 or ANDROID_HOME" >&2 + exit 2 +fi + +manifest=$("$aapt2" dump xmltree --file AndroidManifest.xml "$apk" 2>&1) || { + echo "cannot read the manifest of $apk: $manifest" >&2 + exit 2 +} + +failed=0 +fail() { + echo "FAIL: $1" + failed=1 +} + +libs=$(unzip -Z1 "$apk" | grep -E '^lib/[^/]+/libsqlitedbg\.so$' || true) +for entry in $libs; do + fail "native library $entry" +done + +if grep -q 'org\.appdevforall\.sqlitedebugger\.DebuggerStartup' <<<"$manifest"; then + fail "manifest declares provider org.appdevforall.sqlitedebugger.DebuggerStartup" +fi + +# xmltree indents children deeper than their parent: collect the lines under , then look +# at the name of its children. +if awk ' + { match($0, /^ */); indent = RLENGTH } + /E: queries / { inq = 1; qindent = indent; next } + inq && indent <= qindent { inq = 0 } + inq && /E: package / { pkg = 1; next } + inq && /E: / { pkg = 0 } + inq && pkg && /:name\(.*"com\.itsaky\.androidide"/ { found = 1 } + END { exit !found } +' <<<"$manifest"; then + fail "manifest lists package com.itsaky.androidide" +fi + +# A dex stores type descriptors as plain strings, so a debugger class that got in shows up here even when +# the manifest is clean (a library that registers nothing). R8 can rename or drop classes in a minified release, +# so this check can miss them: the manifest and native-library checks above are the ones that hold. +# grep -c reads all its input: with pipefail, grep -q exiting early would make unzip die of SIGPIPE +# and the pipeline report failure. +dex_hits=$(unzip -p "$apk" 'classes*.dex' 2>/dev/null | grep -ac 'Lorg/appdevforall/sqlitedebugger/' || true) +if [ "${dex_hits:-0}" -gt 0 ]; then + fail "a classes*.dex contains org.appdevforall.sqlitedebugger classes" +fi + +if grep -q 'android.permission.INTERNET' <<<"$manifest"; then + echo "WARN: manifest has android.permission.INTERNET. It comes from the debugger only if the provider DebuggerStartup is also present (that is a FAIL above); otherwise it is the app's own." +fi + +if [ "$failed" -eq 0 ]; then + echo "OK: no SQLite debugger found in $apk" +fi +exit "$failed" diff --git a/settings.gradle.kts b/settings.gradle.kts index 2d60f4d01c..183ca036d9 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -172,6 +172,7 @@ include( ":subprojects:shizuku-starter", ":subprojects:sqlite-debugger", ":subprojects:sqlite-js", + ":subprojects:sqlite-rewrite-runtime", ":subprojects:tooling-api", ":subprojects:tooling-api-events", ":subprojects:tooling-api-impl", diff --git a/subprojects/sqlite-debugger/README.md b/subprojects/sqlite-debugger/README.md index dcbfad6e66..7511de7cf2 100644 --- a/subprojects/sqlite-debugger/README.md +++ b/subprojects/sqlite-debugger/README.md @@ -4,6 +4,19 @@ A debug-only SQLite debugger and MCP server that runs inside apps built with Cod The module builds an AAR with `libsqlitedbg.so` for arm64-v8a and armeabi-v7a in one artifact, so it has no v7/v8 flavors (`AndroidModuleConf`). Its minSdk is 21, requery's, because the debugger runs on requery's SQLite. The unit test task is `:subprojects:sqlite-debugger:testDebugUnitTest`. +## Turning it on in an app + +A project made from the JavaScript in SQLite template has it on already. For any other project: + +1. Add `cogo.sqliteDebugger=true` to the project's `gradle.properties` ([where else it can go](../../gradle-plugin/docs/sqlite-rewrite.md#turning-it-on)). +2. Copy `app/libs/sqlite-android.aar` (requery) from a project made from that template into the app's `libs/`, and add `implementation(files("libs/sqlite-android.aar"))`. +3. Check that the app depends on androidx.sqlite (Room and WorkManager bring it) and androidx.core (AppCompat brings it). requery needs both, and a file dependency brings neither. +4. Build and run the debug variant. The app starts the server and sends its port and token to Code on the Go (ADFA-7137), and AI-Agent-MCP connects to it with no setup (ADFA-7139). In phase 1 the Agent asks before each call (ADFA-7147). + +The app's code does not change: the build moves the app and its libraries from Android's SQLite to requery in every variant, release included, so debug and release run the same SQLite. Release builds never contain the debugger. While the line is on, Quick Build is off for the project. The opt-in is decided per project from all app modules: Code on the Go reads the Gradle user home, every app module and the root, so the first of those files that defines the line decides for the whole project, whatever the other modules say, even though Quick Build builds only the first app module. Commenting the line out turns all of this off. + +Not covered: Room's `BundledSQLiteDriver`, which brings its own SQLite. When the app or one of its libraries uses something requery lacks, the build fails, names the class, method and line, and says how to build without the debugger; the list is under [Rules](../../gradle-plugin/docs/sqlite-rewrite.md#rules). + ## How it fits together ``` @@ -22,6 +35,7 @@ MCP client --HTTP, 127.0.0.1--> McpHttp -> McpProtocol -> DatabaseTools -> Debug - `engine/`: `DebugEngine` is what the server needs from the extension, `NativeDebugEngine` implements it over JNI, and `EventDecoder` turns the event buffer's bytes into `Event`s. - `mcp/`: `McpHttp` is the HTTP/1.1 listener, `McpProtocol` is JSON-RPC and the MCP session handling, `DatabaseTools` is the tools: `list_databases`, `list_connections`, `get_schema`, `query`, `explain_query_plan`, `get_stats`, `integrity_check`, `get_events`. - `DebuggerStartup` is a `ContentProvider` with the highest `initOrder`, declared in the AAR's manifest. It runs before the app's own providers and `Application.onCreate`, so the extension is registered before the app opens a database. It installs the extension synchronously in `onCreate` (so an app that opens its database in `Application.onCreate` is still seen) and starts the server on a background thread. Everything after the debuggable check is wrapped in `catch (Throwable)`: a missing requery library (`UnsatisfiedLinkError`) means the debugger is disabled and logged, never a crash of the host app. +- **Gradle plugin wiring (ADFA-7138).** An app opts in with `cogo.sqliteDebugger=true` in its `gradle.properties`; commenting the line out turns everything off. With it, Code on the Go's Gradle plugin (`AndroidIDEGradlePlugin`) applies two plugins. `SqliteRewritePlugin` registers a class transform on every variant that moves the app's and its libraries' classes from `android.database.sqlite` to requery (see `gradle-plugin/docs/sqlite-rewrite.md`) and adds the rewrite's helper AAR (`:subprojects:sqlite-rewrite-runtime`) to every variant. `SqliteDebuggerPlugin` adds the debugger AAR and kotlin-stdlib to debuggable variants only. Code on the Go passes both AAR paths to Gradle as properties. Quick Build is not applied to an opted-in project, because its payload would compile the project's source without the rewrite. The apps' minSdk must be at least 21, the floor of requery, the helper AAR and the debugger. ### The event record @@ -50,6 +64,5 @@ The layout of `sqlitedbg_event_record` lives in `sqlitedbg.h`. The JNI layer han ## Not done yet - Broadcasting the port and token to Code on the Go (ADFA-7137). `DebuggerStartup` only keeps them in `DebuggerStartup.port` and `endpointToken` and logs the port, never the token. -- Gradle plugin wiring and the build-time rewrite that adds the library to debug builds (ADFA-7138). - The AI-Agent-MCP side that connects to the server (ADFA-7139). - A device run of the current code. The device results on ADFA-7135 (statements captured on both ABIs, the overhead) predate the review fixes. diff --git a/subprojects/sqlite-rewrite-runtime/build.gradle.kts b/subprojects/sqlite-rewrite-runtime/build.gradle.kts new file mode 100644 index 0000000000..1ca7d55699 --- /dev/null +++ b/subprojects/sqlite-rewrite-runtime/build.gradle.kts @@ -0,0 +1,30 @@ +plugins { + id("com.android.library") +} + +description = "Runtime helpers for classes rewritten from android.database.sqlite to requery (ADFA-7138)" + +android { + namespace = "org.appdevforall.sqliterewrite" + + defaultConfig { + // requery's own minSdk (its AAR manifest says 21); apps rewritten to use it need no more + minSdk = 21 + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + + buildFeatures { + viewBinding = false + buildConfig = false + } +} + +dependencies { + // Not packaged: requery's AAR is a file dependency, which brings none of its own dependencies, so + // androidx.core comes from the app (AppCompat and most androidx libraries depend on it) + compileOnly(libs.androidx.core) +} diff --git a/subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java b/subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java new file mode 100644 index 0000000000..7917a9c3bb --- /dev/null +++ b/subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java @@ -0,0 +1,222 @@ +package org.appdevforall.sqliterewrite; + +import android.content.Context; +import android.database.DatabaseErrorHandler; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteStatement; +import android.os.CancellationSignal; +import android.text.TextUtils; + +import java.io.File; +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; + +/** + * Targets of the calls that CoGo's Gradle plugin redirects when it rewrites an app from android.database.sqlite to requery (ADFA-7138). + * + *

+ * The methods are written against Android's classes on purpose: the plugin rewrites this library like every other class of the app, so {@code SQLiteDatabase} and {@code DatabaseErrorHandler} below become requery's in the final APK. The plugin leaves this package's own calls alone, otherwise a redirect would call itself. + * + *

+ * Flag mapping, Android -> requery 3.49.0 (read from its classes.jar): + * + *

+ * OPEN_READWRITE (0)                           -> OPEN_READWRITE (2)
+ * OPEN_READONLY (1)                            -> OPEN_READONLY (1)
+ * CREATE_IF_NECESSARY (0x10000000)             -> OPEN_CREATE (4), so 6 with read-write; dropped with OPEN_READONLY
+ * ENABLE_WRITE_AHEAD_LOGGING (0x20000000)      -> same value in requery, kept
+ * NO_LOCALIZED_COLLATORS (16)                  -> dropped, requery has no collator support
+ * any other bit                                -> dropped
+ * 
+ * + * Other bits are dropped because requery hands them to sqlite3_open_v2, where 16 and 8 mean SQLITE_OPEN_EXCLUSIVE and SQLITE_OPEN_DELETEONCLOSE: an Android flag must never turn into one of those. + */ +public final class SqliteRewriteRuntime { + private static final int ANDROID_OPEN_READONLY = 1; + private static final int ANDROID_CREATE_IF_NECESSARY = 0x10000000; + private static final int ANDROID_ENABLE_WRITE_AHEAD_LOGGING = 0x20000000; + + private static final int REQUERY_OPEN_READONLY = 1; + private static final int REQUERY_OPEN_READWRITE = 2; + private static final int REQUERY_OPEN_CREATE = 4; + private static final int REQUERY_ENABLE_WRITE_AHEAD_LOGGING = 0x20000000; + + private static final int CONTEXT_MODE_ENABLE_WRITE_AHEAD_LOGGING = 0x8; + + /** + * The listener each Android signal forwards cancellation through. Weak keys, so a signal the app dropped goes, and weak values: a WeakHashMap keeps the value of a cleared key until a later call on the map, so a strong value kept a dropped signal's forwarder alive until the next conversion. The signal's own listener field keeps its forwarder alive while the signal lives. + */ + private static final Map> FORWARDERS = new WeakHashMap<>(); + + /** + * Same as Context.deleteDatabase, which also returns false on any exception. requery's deleteDatabase removes the -journal, -wal, -shm and -mj files next to the file, like Android's. + */ + public static boolean deleteDatabase(Context context, String name) { + try { + return SQLiteDatabase.deleteDatabase(context.getDatabasePath(name)); + } catch (Exception e) { + return false; + } + } + + public static long longForQuery(SQLiteDatabase db, String query, String[] selectionArgs) { + SQLiteStatement prog = db.compileStatement(query); + try { + return longForQuery(prog, selectionArgs); + } finally { + prog.close(); + } + } + + public static long longForQuery(SQLiteStatement prog, String[] selectionArgs) { + prog.bindAllArgsAsStrings(selectionArgs); + return prog.simpleQueryForLong(); + } + + public static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, + int flags) { + return SQLiteDatabase.openDatabase(path, factory, translateOpenFlags(flags)); + } + + public static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, + int flags, DatabaseErrorHandler errorHandler) { + return SQLiteDatabase.openDatabase(path, factory, translateOpenFlags(flags), + errorHandler); + } + + public static SQLiteDatabase openOrCreateDatabase(Context context, String name, int mode, + SQLiteDatabase.CursorFactory factory) { + return openOrCreateDatabase(context, name, mode, factory, null); + } + + /** + * Context.openOrCreateDatabase without the file permission bits of {@code mode}: Android applies them with a hidden API, and the modes other than private were deprecated in API 17. + */ + public static SQLiteDatabase openOrCreateDatabase(Context context, String name, int mode, + SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) { + int flags = ANDROID_CREATE_IF_NECESSARY; + if ((mode & CONTEXT_MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { + flags |= ANDROID_ENABLE_WRITE_AHEAD_LOGGING; + } + File file = context.getDatabasePath(name); + File parent = file.getParentFile(); + if (parent != null && !parent.isDirectory()) { + // getDatabasePath does this for the default directory only + parent.mkdirs(); + } + return openDatabase(file.getPath(), factory, flags, errorHandler); + } + + /** + * The DatabaseUtils methods that take SQLiteDatabase or SQLiteStatement, as Android implements them (requery has no DatabaseUtils). They call only members requery has, so they stay on this class. + */ + public static long queryNumEntries(SQLiteDatabase db, String table) { + return queryNumEntries(db, table, null, null); + } + + public static long queryNumEntries(SQLiteDatabase db, String table, String selection) { + return queryNumEntries(db, table, selection, null); + } + + public static long queryNumEntries(SQLiteDatabase db, String table, String selection, + String[] selectionArgs) { + String s = (!TextUtils.isEmpty(selection)) ? " where " + selection : ""; + return longForQuery(db, "select count(*) from " + table + s, selectionArgs); + } + + public static String stringForQuery(SQLiteDatabase db, String query, String[] selectionArgs) { + SQLiteStatement prog = db.compileStatement(query); + try { + return stringForQuery(prog, selectionArgs); + } finally { + prog.close(); + } + } + + public static String stringForQuery(SQLiteStatement prog, String[] selectionArgs) { + prog.bindAllArgsAsStrings(selectionArgs); + return prog.simpleQueryForString(); + } + + /** + * Android's signal for requery's. Each call returns a new requery signal, and cancelling Android's cancels every one of them still in use: requery keeps a query's signal for the cursor's later reads, so two lazy queries that share an Android signal must both stop. Android's signal holds one listener, so this replaces any listener the caller set on it (Android's setOnCancelListener documents that it replaces the previous one). The forwarder lives only as long as it is that listener: if the caller sets its own listener after a query, the forwarder can be collected, the next call starts a new one, and queries converted before the caller's listener no longer stop with the signal. + */ + public static androidx.core.os.CancellationSignal toCancellationSignal( + CancellationSignal signal) { + if (signal == null) { + return null; + } + final androidx.core.os.CancellationSignal converted = new androidx.core.os.CancellationSignal(); + CancelForwarder forwarder; + synchronized (FORWARDERS) { + WeakReference ref = FORWARDERS.get(signal); + forwarder = ref == null ? null : ref.get(); + if (forwarder == null) { + forwarder = new CancelForwarder(); + FORWARDERS.put(signal, new WeakReference<>(forwarder)); + } + } + // the local keeps the new forwarder alive until setOnCancelListener stores it in the signal + forwarder.add(converted); + signal.setOnCancelListener(forwarder); + // setOnCancelListener does not call a listener that is already set, so a signal cancelled before this call + // has to be checked + if (signal.isCanceled()) { + converted.cancel(); + } + return converted; + } + + /** Converts Android's open flags to requery's. */ + public static int translateOpenFlags(int androidFlags) { + boolean readOnly = (androidFlags & ANDROID_OPEN_READONLY) != 0; + int flags = readOnly ? REQUERY_OPEN_READONLY : REQUERY_OPEN_READWRITE; + // SQLite accepts READONLY, READWRITE and READWRITE|CREATE, and rejects READONLY|CREATE + if (!readOnly && (androidFlags & ANDROID_CREATE_IF_NECESSARY) != 0) { + flags |= REQUERY_OPEN_CREATE; + } + if ((androidFlags & ANDROID_ENABLE_WRITE_AHEAD_LOGGING) != 0) { + flags |= REQUERY_ENABLE_WRITE_AHEAD_LOGGING; + } + return flags; + } + + private SqliteRewriteRuntime() {} + + /** + * Cancels every requery signal converted from one Android signal. It holds them weakly: a query's signal lives as long as its cursor, and a closed and dropped cursor must not be kept for a signal the app reuses. + */ + private static final class CancelForwarder implements CancellationSignal.OnCancelListener { + private final List> converted = new ArrayList<>(); + + @Override + public void onCancel() { + List live = new ArrayList<>(); + synchronized (this) { + for (WeakReference ref : converted) { + androidx.core.os.CancellationSignal signal = ref.get(); + if (signal != null) { + live.add(signal); + } + } + converted.clear(); + } + // outside the lock: requery's listener interrupts the native query + for (androidx.core.os.CancellationSignal signal : live) { + signal.cancel(); + } + } + + synchronized void add(androidx.core.os.CancellationSignal signal) { + for (int i = converted.size() - 1; i >= 0; i--) { + if (converted.get(i).get() == null) { + converted.remove(i); + } + } + converted.add(new WeakReference<>(signal)); + } + } +}