diff --git a/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt b/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt index 8c3c29860e..f3d8477a5e 100644 --- a/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt +++ b/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt @@ -3,6 +3,7 @@ package com.itsaky.androidide.repositories import android.util.Log import com.itsaky.androidide.plugins.PluginInfo import com.itsaky.androidide.plugins.PluginMetadata +import com.itsaky.androidide.plugins.manager.core.PluginBuildFiles import com.itsaky.androidide.plugins.manager.core.PluginManager import com.itsaky.androidide.plugins.manager.loaders.toPluginMetadata import kotlinx.coroutines.Dispatchers @@ -144,7 +145,9 @@ class PluginRepositoryImpl( val fileExtension = if (pluginFile.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true)) ".$PLUGIN_ARCHIVE_EXTENSION" else ".apk" - val finalFileName = "${pluginId}$fileExtension" + // A name of its own per build: Android caches a plugin's resources, assets and native + // libraries by path, so the new build would otherwise run against the old one's. + val finalFileName = "${PluginBuildFiles.fileStem(pluginId, pluginFile)}$fileExtension" if (!pluginsDir.exists()) { pluginsDir.mkdirs() diff --git a/app/src/test/java/com/itsaky/androidide/repositories/PluginRepositoryImplInstallTest.kt b/app/src/test/java/com/itsaky/androidide/repositories/PluginRepositoryImplInstallTest.kt new file mode 100644 index 0000000000..f7794cf82e --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/repositories/PluginRepositoryImplInstallTest.kt @@ -0,0 +1,55 @@ +package com.itsaky.androidide.repositories + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.plugins.manager.core.PluginBuildFiles +import com.itsaky.androidide.plugins.manager.core.PluginManager +import com.itsaky.androidide.plugins.manager.core.PluginValidation +import com.itsaky.androidide.plugins.manager.loaders.PluginManifest +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.test.runTest +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +/** + * Each plugin build is installed under a name of its own. Android caches a plugin's resources and + * assets by path, so a build installed at the previous build's path read the previous build's + * assets until the app restarted (ADFA-7160). + */ +@RunWith(RobolectricTestRunner::class) +class PluginRepositoryImplInstallTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun aBuildIsInstalledUnderItsContentHash() = + runTest { + val pluginsDir = temp.newFolder("plugins") + val build = temp.newFile("incoming.cgp").apply { writeText("build A") } + val expected = "${PluginBuildFiles.fileStem(ID, build)}.cgp" + val manager = + mockk(relaxed = true) { + every { getPluginValidation(any()) } returns + Result.success( + PluginValidation( + manifest = PluginManifest(ID, "Example", "1.0", "", "", "Main", "1.0"), + isDebug = false, + iconDayEntryExists = false, + iconNightEntryExists = false, + ), + ) + every { getPlugin(ID) } returns mockk() + } + + PluginRepositoryImpl({ manager }, pluginsDir).installPluginFromFile(build).getOrThrow() + + assertThat(pluginsDir.list()!!.toList()).containsExactly(expected) + } + + private companion object { + const val ID = "org.example.plugin" + } +} diff --git a/docs/PLUGIN_AUTHORING.md b/docs/PLUGIN_AUTHORING.md index eff39136fe..85e18dffb1 100644 --- a/docs/PLUGIN_AUTHORING.md +++ b/docs/PLUGIN_AUTHORING.md @@ -112,7 +112,7 @@ these in `PluginLoader.getPluginMetadata()` | Key | Type | Required | Notes | |---------------------------|-------------------------|----------|-----------------------------------------------------------------------| -| `plugin.id` | string | yes\* | Unique plugin identifier. Falls back to the APK's package name. | +| `plugin.id` | string | yes\* | Unique plugin identifier. Falls back to the APK's package name. It names files, so it can't be empty, `.` or `..`, or contain `/`, `\` or NUL; CoGo refuses such a plugin. | | `plugin.name` | string | yes\* | Display name. | | `plugin.version` | string | yes\* | Defaults to `"1.0.0"`. Prefer `${pluginVersion}` from `pluginBuilder`. | | `plugin.description` | string | no | Shown in the plugin list. | diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt new file mode 100644 index 0000000000..53fab2b13e --- /dev/null +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -0,0 +1,257 @@ +package com.itsaky.androidide.plugins.manager.core + +import org.adfa.constants.PLUGIN_ARCHIVE_EXTENSION +import org.slf4j.LoggerFactory +import java.io.File +import java.io.IOException +import java.security.MessageDigest +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicBoolean +import java.util.zip.ZipFile + +/** + * The files of installed plugin builds: the plugin files in [pluginsDir], and the native libraries + * extracted from them under [nativeLibsDir]. + * + * Every build gets paths of its own. Android caches a plugin's resources and assets by file path, + * and refuses to open a native library at a path another class loader already opened, so a build + * installed at the previous build's paths runs against the previous build's files until the app + * restarts. Plugin files are named `-.cgp`, after their content. A file installed + * before that, `.cgp`, keeps loading until its plugin is next updated. An older CoGo + * build updates that file in place, so its libraries' folder is named after its size and time. + * + * [legacyNativeLibsDir] is where CoGo builds before per-build folders extracted libraries. It is + * deleted, never read. + */ +class PluginBuildFiles internal constructor( + private val pluginsDir: File, + private val nativeLibsDir: File, + private val legacyNativeLibsDir: File? = null, + private val readPluginId: (File) -> String?, +) { + private val legacyDeleted = AtomicBoolean(legacyNativeLibsDir == null) + + // One extraction or deletion per plugin at a time: loads can overlap, and two extractions of a + // build would share its scratch folder. + private val locks = ConcurrentHashMap() + + // Native-library folders handed to a class loader in this process. A folder deleted by an + // uninstall is never handed out again under the same path: the runtime would refuse to open a + // library there for the reinstalled build's class loader while the old one still holds it. + private val handedOut: MutableSet = ConcurrentHashMap.newKeySet() + + /** Whether [file] is a plugin file, which only its manifest ties to a plugin. */ + private fun isPluginFile(file: File): Boolean = + file.isFile && file.parentFile == pluginsDir && file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) + + /** Every plugin file whose manifest has [pluginId]. Its name can't tell: it names the build. */ + fun filesOf(pluginId: String): List = + pluginsDir + .listFiles(::isPluginFile) + .orEmpty() + .filter { file -> + runCatching { readPluginId(file) } + .onFailure { log.warn("Could not read the plugin ID of {}", file.name, it) } + .getOrNull() == pluginId + } + + /** + * Deletes every file of [pluginId], not just the first, since two would both load. True when + * there was one and all were deleted: a file left behind loads again on the next start. The + * plugin's native libraries go even when it has no file left: an install whose load failed + * deletes its file but not the libraries it extracted. They don't count toward the result: with + * its files gone the plugin is gone, and its next install deletes the folders of other builds. + */ + fun delete(pluginId: String): Boolean = + synchronized(lockFor(pluginId)) { + deleteLegacyNativeLibs() + if (!pluginDirOf(pluginId).deleteRecursively()) { + log.error("Could not delete all native libraries of {}", pluginId) + } + val files = filesOf(pluginId) + val undeleted = files.filterNot(File::delete) + undeleted.forEach { log.error("Could not delete plugin file {}", it.absolutePath) } + files.isNotEmpty() && undeleted.isEmpty() + } + + /** + * Extracts [build]'s native libraries for [abi] into a folder of that build's own, and deletes + * [pluginId]'s folders that belong to no installed build. Null when [build] has none for [abi]. + */ + fun extractNativeLibs( + pluginId: String, + build: File, + abi: String, + ): File? = + synchronized(lockFor(pluginId)) { + deleteLegacyNativeLibs() + val pluginDir = pluginDirOf(pluginId) + val key = buildKey(pluginId, build) + val buildDir = folderFor(pluginDir, key) + val extracted = buildDir.isDirectory || extractNativeLibs(build, "lib/$abi/", buildDir) + // Another installed build of this plugin keeps its folder: it may be loading from it. + val otherBuilds = installedKeys(pluginId) - key + pluginDir + .listFiles { file -> file != buildDir && !(file.isDirectory && file.name.substringBefore('~') in otherBuilds) } + ?.forEach(File::deleteRecursively) + pluginDir.delete() // only when empty + if (extracted) buildDir.also { handedOut += it.absolutePath } else null + } + + private fun lockFor(pluginId: String): Any = locks.computeIfAbsent(pluginId) { Any() } + + private fun deleteLegacyNativeLibs() { + if (legacyDeleted.compareAndSet(false, true) && legacyNativeLibsDir?.deleteRecursively() == false) { + log.warn("Could not delete {}", legacyNativeLibsDir) + } + } + + /** + * [pluginId]'s folder of native libraries. Throws unless that is a folder of [nativeLibsDir] + * named [pluginId]: an ID that is a path, or a link, would have it delete a folder elsewhere. + */ + private fun pluginDirOf(pluginId: String): File { + val dir = File(nativeLibsDir, pluginId) + val canonical = dir.canonicalFile + require(canonical.parentFile == nativeLibsDir.canonicalFile && canonical.name == pluginId) { + "Plugin ID \"$pluginId\" names no folder of ${nativeLibsDir.absolutePath}" + } + return dir + } + + /** + * The build's folder: ``, or `~` when an uninstall in this process deleted the + * folders it handed out before. An existing one is reused. + */ + private fun folderFor( + pluginDir: File, + key: String, + ): File { + val existing = pluginDir.listFiles { file -> file.isDirectory && (file.name == key || file.name.startsWith("$key~")) } + existing?.minByOrNull { it.name.length }?.let { return it } + return generateSequence(0) { it + 1 } + .map { n -> File(pluginDir, if (n == 0) key else "$key~$n") } + .first { it.absolutePath !in handedOut } + } + + /** + * The build keys of [pluginId]'s files, by name, which is cheap enough for every load. A file of + * another plugin whose ID starts with `-` can match too; that only keeps a folder. + */ + private fun installedKeys(pluginId: String): Set = + pluginsDir + .listFiles { file -> + file.isFile && + file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) && + (file.nameWithoutExtension == pluginId || file.nameWithoutExtension.startsWith("$pluginId-")) + }.orEmpty() + .mapTo(HashSet()) { buildKey(pluginId, it) } + + /** Extracts into a scratch folder renamed when complete, so an interrupted extraction is never reused. */ + private fun extractNativeLibs( + build: File, + libPrefix: String, + buildDir: File, + ): Boolean { + val scratch = File(buildDir.parentFile, ".${buildDir.name}.partial") + scratch.deleteRecursively() + scratch.mkdirs() + val targetPath = scratch.toPath().toAbsolutePath().normalize() + + var found = false + ZipFile(build).use { zip -> + zip + .entries() + .asSequence() + .filter { !it.isDirectory && it.name.startsWith(libPrefix) && it.name.endsWith(".so") } + .forEach { entry -> + val outPath = targetPath.resolve(entry.name.substringAfterLast("/")).normalize() + if (outPath.startsWith(targetPath)) { + zip.getInputStream(entry).use { input -> + outPath.toFile().outputStream().use { output -> + input.copyTo(output) + } + } + outPath.toFile().setExecutable(true, false) + found = true + } + } + } + + if (!found) { + scratch.deleteRecursively() + return false + } + if (!scratch.renameTo(buildDir)) { + scratch.deleteRecursively() + throw IOException("Could not move native libraries into ${buildDir.absolutePath}") + } + return true + } + + companion object { + private val log = LoggerFactory.getLogger(PluginBuildFiles::class.java) + + private const val HASH_CHARS = 16 + + // Not app_plugin_native_libs: an older CoGo build reuses its plugin's folder there whatever it + // holds, so it would find these per-build folders and no libraries. Folder names as getDir() + // makes them. + private const val NATIVE_LIBS_DIR = "app_plugin_build_libs" + private const val LEGACY_NATIVE_LIBS_DIR = "app_plugin_native_libs" + + /** The plugin build files of the app whose data folder is [dataDir]. */ + internal fun inDataDir( + pluginsDir: File, + dataDir: File, + readPluginId: (File) -> String?, + ) = PluginBuildFiles(pluginsDir, File(dataDir, NATIVE_LIBS_DIR), File(dataDir, LEGACY_NATIVE_LIBS_DIR), readPluginId) + + /** The name, without extension, to install [build] under: `-`. */ + fun fileStem( + pluginId: String, + build: File, + ): String = "$pluginId-${sha256Hex(build).take(HASH_CHARS)}" + + /** + * The name of [installed]'s library folder: the hash in its name. A `.cgp` installed + * before hashes gets `legacy--`, since an older CoGo build rewrites it in + * place. Its name alone would keep the previous content's libraries. + */ + internal fun buildKey( + pluginId: String, + installed: File, + ): String = + if (installed.nameWithoutExtension == pluginId) { + "legacy-${installed.length()}-${installed.lastModified()}" + } else { + installed.nameWithoutExtension.removePrefix("$pluginId-") + } + + /** + * Whether [file] is ART's compiled code for a file of [pluginId]: `.odex`, `.vdex` or + * `.art`, or `.cgp.prof` or `.cgp.cur.prof`. Matched exactly, since another plugin's ID + * or content hash can contain this ID. + */ + internal fun isCompiledCodeOf( + pluginId: String, + file: File, + ): Boolean = + file.isFile && + Regex("${Regex.escape(pluginId)}(-[0-9a-f]{$HASH_CHARS})?\\.(odex|vdex|art|$PLUGIN_ARCHIVE_EXTENSION\\.(cur\\.)?prof)") + .matches(file.name) + + private fun sha256Hex(file: File): String { + val digest = MessageDigest.getInstance("SHA-256") + file.inputStream().use { input -> + val buffer = ByteArray(DEFAULT_BUFFER_SIZE) + while (true) { + val read = input.read(buffer) + if (read < 0) break + digest.update(buffer, 0, read) + } + } + return digest.digest().joinToString("") { "%02x".format(it) } + } + } +} diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt new file mode 100644 index 0000000000..6c77fbc323 --- /dev/null +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt @@ -0,0 +1,13 @@ +package com.itsaky.androidide.plugins.manager.core + +/** + * Fails when [id] can't be a plugin ID. The ID names the plugin's files and folders, so one that + * is a path reaches outside them: installing `../files` would delete the app's whole files folder. + * Nullable because a `plugin.json` manifest without an ID parses to a null one. + */ +internal fun checkPluginId(id: String?): Result = + if (id.isNullOrEmpty() || id == "." || id == ".." || id.any { it == '/' || it == '\\' || it == '\u0000' }) { + Result.failure(IllegalArgumentException("Invalid plugin ID \"$id\": it must be a name, not a path")) + } else { + Result.success(id) + } diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index 03e42340e7..be3d0d1fcd 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -4,6 +4,7 @@ package com.itsaky.androidide.plugins.manager.core import android.app.Activity import android.content.Context +import android.os.Build import com.itsaky.androidide.actions.SidebarSlotManager import com.itsaky.androidide.plugins.IPlugin import com.itsaky.androidide.plugins.PluginContext @@ -331,6 +332,10 @@ class PluginManager private constructor( private val lifecycleDispatcher = PluginLifecycleDispatcher() private val pluginsDir = File(context.filesDir, "plugins") + private val buildFiles = + PluginBuildFiles.inDataDir(pluginsDir, context.dataDir) { file -> + PluginLoader(context, file).getPluginMetadata()?.id + } private val documentationManager = PluginDocumentationManager(context) private var templateReloadListener: (() -> Unit)? = null private var snippetRefreshListener: ((String) -> Unit)? = null @@ -479,17 +484,25 @@ class PluginManager private constructor( return Result.success(manifest to loader) } + /** + * The manifest, its ID unchecked: the Add-ons Manager reports this failing as an unreadable file, + * so a bad ID is left for [getPluginValidation] to report. Don't build a path from it. + */ fun getPluginMetadataOnly(pluginFile: File): Result = loadAndValidate(pluginFile).map { it.first } - fun getPluginValidation(pluginFile: File): Result = - loadAndValidate(pluginFile).map { (manifest, loader) -> + /** Fails for a plugin ID that is a path: install deletes and creates folders named after it. */ + fun getPluginValidation(pluginFile: File): Result { + val (manifest, loader) = loadAndValidate(pluginFile).getOrElse { return Result.failure(it) } + checkPluginId(manifest.id).onFailure { return Result.failure(it) } + return Result.success( PluginValidation( manifest = manifest, isDebug = loader.isDebuggable(), iconDayEntryExists = manifest.iconDay?.let(loader::hasEntry) ?: false, iconNightEntryExists = manifest.iconNight?.let(loader::hasEntry) ?: false, - ) - } + ), + ) + } /** * Load plugin and return both the plugin instance and its metadata @@ -562,6 +575,7 @@ class PluginManager private constructor( if (manifest == null) { return Result.failure(IllegalArgumentException("Plugin manifest not found in: ${file.name}")) } + checkPluginId(manifest.id).onFailure { return Result.failure(it) } logger.debug("Parsed manifest for plugin: ${manifest.name} (${manifest.id})") @@ -588,7 +602,7 @@ class PluginManager private constructor( var nativeLibPath: String? = try { - pluginLoader.extractNativeLibs(manifest.id)?.absolutePath + buildFiles.extractNativeLibs(manifest.id, file, Build.SUPPORTED_ABIS[0])?.absolutePath } catch (e: Exception) { logger.warn("Failed to extract native libs for plugin: ${manifest.id}", e) null @@ -685,6 +699,7 @@ class PluginManager private constructor( isEnabled, iconDayPath = iconDayPath, iconNightPath = iconNightPath, + nativeLibraryDir = nativeLibPath?.let(::File), ) loadedPlugins[manifest.id] = loadedPlugin @@ -842,10 +857,6 @@ class PluginManager private constructor( // Drop lifecycle listeners this plugin registered; its classloader is going away. lifecycleDispatcher.removeAllFrom(pluginId) - File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId).let { dir -> - if (dir.exists()) dir.deleteRecursively() - } - notifyLanguageContributionsChanged(loadedPlugin.plugin) logger.info("Unloaded plugin: $pluginId") return true @@ -859,7 +870,13 @@ class PluginManager private constructor( incomingFile: File, existingPluginId: String, ): Boolean { - val existingFile = File(pluginsDir, "$existingPluginId.$PLUGIN_ARCHIVE_EXTENSION") + val existingFile = + loadedPlugins[existingPluginId]?.apkPath?.let(::File)?.takeIf(File::isFile) + ?: buildFiles.filesOf(existingPluginId).firstOrNull() + if (existingFile == null) { + logger.warn("No installed file found for $existingPluginId; treating as mismatch") + return false + } val incomingSig = PluginLoader(context, incomingFile).getSignatureHash() val existingSig = PluginLoader(context, existingFile).getSignatureHash() if (incomingSig == null || existingSig == null) { @@ -886,41 +903,7 @@ class PluginManager private constructor( logger.info("Successfully unloaded plugin from memory: $pluginId") } - // Find and delete the plugin file (CGP) - val pluginFiles = - pluginsDir.listFiles { file -> - file.isFile && file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) - } - - if (pluginFiles == null || pluginFiles.isEmpty()) { - logger.error("No plugin files found in plugins directory") - return false - } - - logger.info("Found ${pluginFiles.size} plugin files to check") - var deleted = false - for (pluginFile in pluginFiles) { - try { - // Check if this contains the plugin we want to delete - val loader = PluginLoader(context, pluginFile) - val manifest = loader.getPluginMetadata() - - if (manifest != null) { - if (manifest.id == pluginId) { - if (pluginFile.delete()) { - deleted = true - break // Found and deleted the right file - } else { - logger.error("File exists: ${pluginFile.exists()}, Can write: ${pluginFile.canWrite()}") - } - } - } else { - logger.warn("Could not read manifest from ${pluginFile.name}") - } - } catch (e: Exception) { - logger.error("Error checking plugin file ${pluginFile.name}: ${e.message}", e) - } - } + val deleted = buildFiles.delete(pluginId) // Remove plugin state and cleanup contributions if (deleted) { @@ -1032,8 +1015,7 @@ class PluginManager private constructor( logger.error("No resource context registered for plugin $pluginId; ignoring its languages") return@flatMap emptyList() } - val nativeLibraryDir = - File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId).takeIf { it.isDirectory } + val nativeLibraryDir = loaded.nativeLibraryDir?.takeIf { it.isDirectory } definitions .filter { definition -> isLanguageAllowed(pluginId, loaded.manifest, definition, nativeLibraryDir) } .map { definition -> PluginLanguageContribution(pluginId, definition, assets, nativeLibraryDir) } @@ -1902,10 +1884,6 @@ class PluginManager private constructor( logger.debug("Deleted plugin directory: ${pluginDir.absolutePath} (success: $deleted)") } - File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId).let { dir -> - if (dir.exists()) dir.deleteRecursively() - } - File(context.getDir("plugin_icons", Context.MODE_PRIVATE), pluginId).let { dir -> if (dir.exists()) dir.deleteRecursively() } @@ -1915,7 +1893,7 @@ class PluginManager private constructor( val oatDir = File(pluginsDir, "oat") if (oatDir.exists() && oatDir.isDirectory) { oatDir.walkTopDown().forEach { file -> - if (file.name.contains(pluginId)) { + if (PluginBuildFiles.isCompiledCodeOf(pluginId, file)) { val deleted = file.deleteRecursively() logger.debug("Deleted ART cache: ${file.absolutePath} (success: $deleted)") } @@ -1925,18 +1903,8 @@ class PluginManager private constructor( logger.warn("Failed to cleanup ART cache files for: $pluginId", e) } - // Clean up any other directories or files that contain the plugin ID - try { - pluginsDir.walkTopDown().forEach { file -> - if (file != pluginsDir && file.name.contains(pluginId)) { - val deleted = file.deleteRecursively() - logger.debug("Deleted plugin-related item: ${file.absolutePath} (success: $deleted)") - } - } - } catch (e: Exception) { - logger.warn("Failed to cleanup plugin-related files for: $pluginId", e) - } - + // Nothing is swept by a name containing the ID: another plugin's ID, content hash or data + // can contain it. The plugin's files went by manifest ID in buildFiles.delete(). logger.debug("Complete plugin cleanup finished for: $pluginId") } } @@ -1985,6 +1953,7 @@ data class LoadedPlugin( var isEnabled: Boolean = true, val iconDayPath: String? = null, val iconNightPath: String? = null, + val nativeLibraryDir: File? = null, ) fun LoadedPlugin.toPluginMetadata(): PluginMetadata = diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt index 6fc6f51e2b..3d23471adc 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt @@ -6,7 +6,6 @@ import android.content.pm.PackageInfo import android.content.pm.PackageManager import android.content.res.AssetManager import android.content.res.Resources -import android.os.Build import android.os.Bundle import dalvik.system.DexClassLoader import org.slf4j.LoggerFactory @@ -28,7 +27,6 @@ class PluginLoader( private var pluginResources: Resources? = null private var pluginPackageInfo: PackageInfo? = null private var pluginClassLoader: DexClassLoader? = null - private var nativeLibDir: File? = null /** * Load plugin resources from APK @@ -149,46 +147,6 @@ class PluginLoader( ) } - fun extractNativeLibs(pluginId: String): File? { - val pluginNativeDir = File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId) - if (pluginNativeDir.exists()) { - nativeLibDir = pluginNativeDir - return pluginNativeDir - } - - val libPrefix = "lib/${Build.SUPPORTED_ABIS[0]}/" - val targetPath = pluginNativeDir.toPath().toAbsolutePath().normalize() - pluginNativeDir.mkdirs() - - var found = false - ZipFile(pluginApk).use { zip -> - zip - .entries() - .asSequence() - .filter { !it.isDirectory && it.name.startsWith(libPrefix) && it.name.endsWith(".so") } - .forEach { entry -> - val outPath = targetPath.resolve(entry.name.substringAfterLast("/")).normalize() - if (outPath.startsWith(targetPath)) { - zip.getInputStream(entry).use { input -> - outPath.toFile().outputStream().use { output -> - input.copyTo(output) - } - } - outPath.toFile().setExecutable(true, false) - found = true - } - } - } - - if (!found) { - pluginNativeDir.deleteRecursively() - return null - } - - nativeLibDir = pluginNativeDir - return pluginNativeDir - } - fun isDebuggable(): Boolean { val packageInfo = pluginPackageInfo diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt new file mode 100644 index 0000000000..b5baf6080c --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt @@ -0,0 +1,268 @@ +package com.itsaky.androidide.plugins.manager.core + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File +import java.nio.file.Files +import java.util.concurrent.CountDownLatch +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream +import kotlin.random.Random + +/** + * Each installed plugin build has paths of its own. Android caches resources, assets and native + * libraries by path, so a build at the previous build's paths ran against the previous build's + * files until the app restarted. + */ +class PluginBuildFilesTest { + @get:Rule + val temp = TemporaryFolder() + + private val pluginsDir by lazy { temp.newFolder("plugins") } + private val nativeLibsDir by lazy { temp.newFolder("plugin_native_libs") } + + // Uninstall tests write the plugin ID as the file's text; a real file has it in its manifest. + private val files by lazy { newFiles() } + + private fun newFiles() = PluginBuildFiles(pluginsDir, nativeLibsDir) { it.readText() } + + @Test + fun eachBuildIsNamedAfterItsContent() { + val a = build("a.cgp", "libfoo.so" to "A") + val b = build("b.cgp", "libfoo.so" to "B") + val aAgain = a.copyTo(File(temp.root, "a-again.cgp")) + + val stemA = PluginBuildFiles.fileStem(ID, a) + + assertThat(stemA).matches("${Regex.escape(ID)}-[0-9a-f]{16}") + assertWithMessage("two builds got one name").that(PluginBuildFiles.fileStem(ID, b)).isNotEqualTo(stemA) + assertThat(PluginBuildFiles.fileStem(ID, aAgain)).isEqualTo(stemA) + } + + @Test + fun aNewBuildLoadsItsLibrariesFromANewFolder() { + val a = installed(build("a.cgp", "libfoo.so" to "A")) + val dirA = files.extractNativeLibs(ID, a, ABI) + a.delete() + + val dirB = files.extractNativeLibs(ID, installed(build("b.cgp", "libfoo.so" to "B")), ABI) + + assertWithMessage("the new build reuses the old build's library path").that(dirB).isNotEqualTo(dirA) + assertThat(File(dirB, "libfoo.so").readText()).isEqualTo("B") + assertWithMessage("the replaced build's folder is left behind").that(dirA!!.exists()).isFalse() + } + + @Test + fun anotherInstalledBuildKeepsItsFolder() { + // Two files with one ID both load, in parallel: neither may delete the other's libraries. + val dirA = files.extractNativeLibs(ID, installed(build("a.cgp", "libfoo.so" to "A")), ABI) + files.extractNativeLibs(ID, installed(build("b.cgp", "libfoo.so" to "B")), ABI) + + assertThat(File(dirA, "libfoo.so").readText()).isEqualTo("A") + } + + @Test + fun anOlderCoGoBuildFindsNoFolderToReuse() { + val dataDir = temp.newFolder("data") + val build = installed(build("a.cgp", "libfoo.so" to "A")) + + val dir = PluginBuildFiles.inDataDir(pluginsDir, dataDir) { ID }.extractNativeLibs(ID, build, ABI) + + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("A") + // Before per-build folders, CoGo loaded a plugin's libraries from getDir("plugin_native_libs")/ + // whenever that folder existed, whatever it held. + assertWithMessage("an older CoGo build would find this build's folder and no libraries in it") + .that(File(dataDir, "app_plugin_native_libs/$ID").exists()) + .isFalse() + } + + @Test + fun librariesAnOlderCoGoBuildExtractedAreDeleted() { + val dataDir = temp.newFolder("data") + File(dataDir, "app_plugin_native_libs/$ID").mkdirs() + File(dataDir, "app_plugin_native_libs/$ID/libfoo.so").writeText("old") + val legacy = build("$ID.cgp", "libfoo.so" to "new").copyTo(File(pluginsDir, "$ID.cgp")) + + val dir = PluginBuildFiles.inDataDir(pluginsDir, dataDir) { ID }.extractNativeLibs(ID, legacy, ABI) + + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("new") + assertWithMessage("an older CoGo build's libraries are left behind") + .that(File(dataDir, "app_plugin_native_libs").exists()) + .isFalse() + } + + @Test + fun aFileUnderTheOldNameRewrittenInPlaceGetsItsOwnLibraries() { + val legacy = build("x.cgp", "libfoo.so" to "X").copyTo(File(pluginsDir, "$ID.cgp")) + legacy.setLastModified(1_000_000_000_000) + files.extractNativeLibs(ID, legacy, ABI) + // An older CoGo build updates the plugin under the same name. + build("y.cgp", "libfoo.so" to "Y").copyTo(legacy, overwrite = true) + legacy.setLastModified(1_000_000_001_000) + + val dir = newFiles().extractNativeLibs(ID, legacy, ABI) + + assertWithMessage("the rewritten file ran against the previous content's libraries") + .that(File(dir, "libfoo.so").readText()) + .isEqualTo("Y") + } + + @Test + fun anInstalledBuildUnderTheOldNameOfAnIdWithATildeKeepsItsFolder() { + val id = "org.example~plugin" + val legacy = build("old.cgp", "libfoo.so" to "old").copyTo(File(pluginsDir, "$id.cgp")) + val legacyDir = files.extractNativeLibs(id, legacy, ABI) + val hashed = build("new.cgp", "libfoo.so" to "new") + + files.extractNativeLibs(id, hashed.copyTo(File(pluginsDir, "${PluginBuildFiles.fileStem(id, hashed)}.cgp")), ABI) + + assertWithMessage("loading one build deleted the folder of another installed build") + .that(File(legacyDir, "libfoo.so").exists()) + .isTrue() + } + + @Test + fun aLeftoverScratchFolderIsNotReused() { + val build = installed(build("a.cgp", "libfoo.so" to "A")) + val scratch = File(nativeLibsDir, "$ID/.${PluginBuildFiles.buildKey(ID, build)}.partial") + scratch.mkdirs() + File(scratch, "libstale.so").writeText("from an interrupted extraction") + + val dir = files.extractNativeLibs(ID, build, ABI) + + assertThat(dir!!.list()!!.toList()).containsExactly("libfoo.so") + assertThat(scratch.exists()).isFalse() + } + + @Test + fun aBuildReinstalledInTheSameProcessGetsANewFolder() { + val build = build("a.cgp", "libfoo.so" to "A") + val first = files.extractNativeLibs(ID, installed(build), ABI) + files.delete(ID) + + val second = files.extractNativeLibs(ID, installed(build), ABI) + + // The old class loader may still hold the first path, and the runtime refuses to open it again. + assertWithMessage("the reinstalled build got the path the old class loader opened").that(second).isNotEqualTo(first) + assertThat(File(second, "libfoo.so").readText()).isEqualTo("A") + assertWithMessage("the next start extracts again instead of reusing the folder") + .that(newFiles().extractNativeLibs(ID, File(pluginsDir, "${PluginBuildFiles.fileStem(ID, build)}.cgp"), ABI)) + .isEqualTo(second) + } + + @Test + fun overlappingExtractionsOfABuildAllGetItsLibraries() { + val content = Random(7).nextBytes(4 shl 20) + repeat(ROUNDS) { round -> + val id = "$ID.r$round" + val build = build("r$round.cgp", "libbig.so" to content) + val installed = build.copyTo(File(pluginsDir, "${PluginBuildFiles.fileStem(id, build)}.cgp")) + val start = CountDownLatch(1) + val pool = Executors.newFixedThreadPool(THREADS) + val results = + (1..THREADS).map { + pool.submit { + start.await() + files.extractNativeLibs(id, installed, ABI) + } + } + start.countDown() + val dirs = results.map { it.get(60, TimeUnit.SECONDS) } + pool.shutdown() + + assertWithMessage("round $round: loads got different folders").that(dirs.toSet()).hasSize(1) + assertWithMessage("round $round: a library is incomplete") + .that(File(dirs.first(), "libbig.so").readBytes().contentEquals(content)) + .isTrue() + } + } + + @Test + fun aBuildWithoutLibrariesForTheAbiGetsNoFolder() { + File(nativeLibsDir, "$ID/0123456789abcdef").mkdirs() + val otherAbi = build("x.cgp", "libfoo.so" to "x", abi = "x86_64") + + assertThat(files.extractNativeLibs(ID, installed(otherAbi), ABI)).isNull() + assertThat(File(nativeLibsDir, ID).exists()).isFalse() + } + + @Test + fun uninstallDeletesTheLibrariesOfAPluginWithNoFileLeft() { + File(nativeLibsDir, "$ID/0123456789abcdef").mkdirs() + File(nativeLibsDir, "$ID/0123456789abcdef/libfoo.so").writeText("A") + + assertThat(files.delete(ID)).isFalse() + assertThat(File(nativeLibsDir, ID).exists()).isFalse() + } + + @Test + fun uninstallDeletesEveryFileOfThePluginAndNoOther() { + val legacy = File(pluginsDir, "$ID.cgp").apply { writeText(ID) } + val hashed = File(pluginsDir, "$ID-0123456789abcdef.cgp").apply { writeText(ID) } + // An ID that starts with this one, so only the manifest tells the files apart. + val other = File(pluginsDir, "$ID.extra-0123456789abcdef.cgp").apply { writeText("$ID.extra") } + + assertThat(files.filesOf(ID)).containsExactly(legacy, hashed) + assertThat(files.delete(ID)).isTrue() + assertWithMessage("a second file of the plugin survived").that(pluginsDir.listFiles()!!.toList()).containsExactly(other) + } + + @Test + fun aPluginIdThatIsAPathDeletesNothingOutsideTheLibrariesFolder() { + val victim = File(nativeLibsDir.parentFile, "victim").apply { mkdirs() } + File(victim, "keep.txt").writeText("not a plugin's") + val build = installed(build("a.cgp", "libfoo.so" to "A")) + + val deleted = runCatching { files.delete("../victim") } + val extracted = runCatching { files.extractNativeLibs("../victim", build, ABI) } + + assertWithMessage("a folder outside the libraries folder lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(deleted.exceptionOrNull()).isInstanceOf(IllegalArgumentException::class.java) + assertThat(extracted.exceptionOrNull()).isInstanceOf(IllegalArgumentException::class.java) + } + + @Test + fun aPluginFolderThatLinksElsewhereIsNotFollowed() { + val victim = File(nativeLibsDir.parentFile, "victim").apply { mkdirs() } + File(victim, "keep.txt").writeText("not a plugin's") + Files.createSymbolicLink(File(nativeLibsDir, ID).toPath(), victim.toPath()) + + val deleted = runCatching { files.delete(ID) } + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(deleted.exceptionOrNull()).isInstanceOf(IllegalArgumentException::class.java) + } + + // As PluginRepositoryImpl.installPluginFromFile names and copies it. + private fun installed(build: File): File { + val name = "${PluginBuildFiles.fileStem(ID, build)}.cgp" + return build.copyTo(File(pluginsDir, name), overwrite = true) + } + + private fun build( + name: String, + vararg libs: Pair, + abi: String = ABI, + ): File = + File(temp.newFolder(), name).also { file -> + ZipOutputStream(file.outputStream()).use { zip -> + for ((lib, content) in libs) { + zip.putNextEntry(ZipEntry("lib/$abi/$lib")) + zip.write(if (content is ByteArray) content else content.toString().toByteArray()) + zip.closeEntry() + } + } + } + + private companion object { + const val ID = "org.example.plugin" + const val ABI = "arm64-v8a" + const val ROUNDS = 10 + const val THREADS = 6 + } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt new file mode 100644 index 0000000000..d259cd1950 --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt @@ -0,0 +1,69 @@ +package com.itsaky.androidide.plugins.manager.core + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import org.junit.After +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +/** + * A plugin ID names the plugin's files and folders, so the installer refuses one that is a path: + * installing a plugin whose ID was `../files` deleted the app's whole files folder. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class PluginIdTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun theInstallerRefusesAPluginIdThatIsAPath() { + for (id in listOf("../files", "..", ".", "", "a/b", "/data", "a\\b", "a\u0000b")) { + val result = pluginManager().getPluginValidation(plugin(id)) + + assertWithMessage("the installer accepted \"$id\"").that(result.isFailure).isTrue() + assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"$id\": it must be a name, not a path") + } + } + + @Test + fun theInstallerAcceptsAPluginIdThatIsAName() { + for (id in listOf("com.example.myplugin", "org.adfa.shell-hooks", "my_plugin", "a..b", "...")) { + assertThat( + pluginManager() + .getPluginValidation(plugin(id)) + .getOrThrow() + .manifest.id, + ).isEqualTo(id) + } + } + + @Test + fun loadingRefusesAPluginIdThatIsAPath() { + val files = RuntimeEnvironment.getApplication().filesDir + File(files, "keep.txt").writeText("the app's") + + val result = pluginManager().loadPlugin(plugin("../files")) + + assertWithMessage("loading deleted the app's files").that(files.list()?.toList()).contains("keep.txt") + assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"../files\": it must be a name, not a path") + } + + @Test + fun aManifestWithoutAnIdIsRefused() { + assertThat(checkPluginId(null).exceptionOrNull()) + .hasMessageThat() + .isEqualTo("Invalid plugin ID \"null\": it must be a name, not a path") + } + + @After + fun forgetManager() = forgetPluginManager() + + private fun plugin(id: String): File = fakePlugin(File(temp.newFolder(), "plugin.cgp"), id) +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt new file mode 100644 index 0000000000..5c3b86065a --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt @@ -0,0 +1,97 @@ +package com.itsaky.androidide.plugins.manager.core + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import org.junit.After +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +/** + * Uninstalling a plugin deletes its own files only. Update uninstalls first, so the same goes for + * every update. The plugin uninstalled here is "a", which occurs in the other plugins' content + * hashes, IDs and file names. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class PluginUninstallTest { + private val pluginsDir by lazy { File(RuntimeEnvironment.getApplication().filesDir, "plugins") } + private val oatDir by lazy { File(pluginsDir, "oat") } + + @After + fun forgetManager() = forgetPluginManager() + + @Test + fun anotherPluginsFileWhoseNameContainsTheIdIsKept() { + val uninstalled = fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val others = + listOf( + fakePlugin(File(pluginsDir, "org.demo.tool-0123456789abcdef.cgp"), "org.demo.tool"), + fakePlugin(File(pluginsDir, "a.extra-1111111111111111.cgp"), "a.extra"), + ) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertThat(uninstalled.exists()).isFalse() + assertWithMessage("uninstalling \"a\" deleted another plugin's file") + .that(pluginsDir.listFiles()!!.toList()) + .containsAtLeastElementsIn(others) + } + + @Test + fun anotherPluginsDataIsKept() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val others = listOf(file(pluginsDir, "a.extra/state.json"), file(pluginsDir, "org.demo.tool/cache.bin")) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted another plugin's data") + .that(others.filterNot(File::exists)) + .isEmpty() + } + + @Test + fun anotherPluginsCompiledCodeIsKept() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val others = + listOf( + file(oatDir, "arm64/org.demo.tool-0123456789abcdef.odex"), + file(oatDir, "arm64/a.extra-1111111111111111.vdex"), + file(oatDir, "org.demo.tool-0123456789abcdef.cgp.cur.prof"), + ) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted another plugin's compiled code") + .that(others.filterNot(File::exists)) + .isEmpty() + } + + @Test + fun thePluginsOwnDataAndCompiledCodeAreDeleted() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val own = + listOf( + file(pluginsDir, "a/state.json"), + file(oatDir, "arm64/a-0123456789012345.odex"), + file(oatDir, "arm64/a-0123456789012345.vdex"), + file(oatDir, "arm64/a-0123456789012345.art"), + file(oatDir, "a-0123456789012345.cgp.cur.prof"), + file(oatDir, "a-0123456789012345.cgp.prof"), + // From a file installed before hashes. + file(oatDir, "arm64/a.odex"), + ) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" left its own files").that(own.filter(File::exists)).isEmpty() + } + + private fun file( + dir: File, + path: String, + ): File = File(dir, path).apply { parentFile!!.mkdirs() }.apply { writeText(path) } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt new file mode 100644 index 0000000000..5188d120b3 --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt @@ -0,0 +1,79 @@ +package com.itsaky.androidide.plugins.manager.core + +import android.content.pm.ApplicationInfo +import android.content.pm.PackageInfo +import android.os.Bundle +import com.itsaky.androidide.plugins.PluginLogger +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import java.io.File +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + +/** The plugin manager of the test's application. */ +internal fun pluginManager() = PluginManager.getInstance(RuntimeEnvironment.getApplication(), Any(), NoLogger) + +/** Forgets the singleton plugin manager: each test has an application of its own. */ +internal fun forgetPluginManager() { + PluginManager::class.java + .getDeclaredField("INSTANCE") + .apply { isAccessible = true } + .set(null, null) +} + +/** Writes [file] as a plugin whose AndroidManifest states [id]: Robolectric can't read one from an APK. */ +internal fun fakePlugin( + file: File, + id: String, +): File { + file.parentFile?.mkdirs() + ZipOutputStream(file.outputStream()).use { it.putNextEntry(ZipEntry("classes.dex")) } + val info = + PackageInfo().apply { + packageName = "org.example.plugin" + applicationInfo = + ApplicationInfo().apply { + metaData = + Bundle().apply { + putString("plugin.id", id) + putString("plugin.main_class", "org.example.plugin.Main") + // Loading deletes and recreates the plugin's icon folder, named after its ID. + putString("plugin.icon_day", "icon.png") + } + } + } + shadowOf(RuntimeEnvironment.getApplication().packageManager).setPackageArchiveInfo(file.absolutePath, info) + return file +} + +internal object NoLogger : PluginLogger { + override val pluginId = "test" + + override fun debug(message: String) = Unit + + override fun debug( + message: String, + error: Throwable, + ) = Unit + + override fun info(message: String) = Unit + + override fun info( + message: String, + error: Throwable, + ) = Unit + + override fun warn(message: String) = Unit + + override fun warn( + message: String, + error: Throwable, + ) = Unit + + override fun error(message: String) = Unit + + override fun error( + message: String, + error: Throwable, + ) = Unit +}