-
-
Notifications
You must be signed in to change notification settings - Fork 39
feat(plugin): Auto-instrument SQLiteDriver for Room users (GRADLE-107) #1285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xadam-brown
wants to merge
2
commits into
main
Choose a base branch
from
feat/wrap-sqlite-driver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...n/io/sentry/android/gradle/instrumentation/androidx/sqlite/driver/AndroidXSQLiteDriver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package io.sentry.android.gradle.instrumentation.androidx.sqlite.driver | ||
|
|
||
| import com.android.build.api.instrumentation.ClassContext | ||
| import io.sentry.android.gradle.instrumentation.ClassInstrumentable | ||
| import io.sentry.android.gradle.instrumentation.CommonClassVisitor | ||
| import io.sentry.android.gradle.instrumentation.MethodContext | ||
| import io.sentry.android.gradle.instrumentation.MethodInstrumentable | ||
| import io.sentry.android.gradle.instrumentation.SpanAddingClassVisitorFactory | ||
| import io.sentry.android.gradle.instrumentation.androidx.sqlite.driver.visitor.SetDriverMethodVisitor | ||
| import org.objectweb.asm.ClassVisitor | ||
| import org.objectweb.asm.MethodVisitor | ||
|
|
||
| /** JVM type descriptor for `androidx.sqlite.SQLiteDriver`. */ | ||
| internal const val SQLITE_DRIVER_TYPE_DESCRIPTOR = "Landroidx/sqlite/SQLiteDriver;" | ||
|
|
||
| /** | ||
| * Auto-instruments `SQLiteDriver` for all Room users by wrapping any driver passed to | ||
| * `RoomDatabase.Builder.setDriver(SQLiteDriver)`. | ||
| * | ||
| * In other words, this: | ||
| * ```kotlin | ||
| * val database = Room.databaseBuilder(context, MyDatabase::class.java, "dbName") | ||
| * .setDriver(AndroidSQLiteDriver()) | ||
| * .build() | ||
| * ``` | ||
| * | ||
| * becomes: | ||
| * ```kotlin | ||
| * val database = Room.databaseBuilder(context, MyDatabase::class.java, "dbName") | ||
| * .setDriver(SentrySQLiteDriver.create(AndroidSQLiteDriver())) | ||
| * .build() | ||
| * ``` | ||
| * | ||
| * `SentrySQLiteDriver` protects against duplicate wrappings, allowing the visitor to wrap | ||
| * `SQLiteDriver` unconditionally. | ||
| * | ||
| * Coverage is limited to Room because SQLDelight | ||
| * [doesn't support `SQLiteDriver`](https://github.com/sqldelight/sqldelight/issues/6072) (it uses | ||
| * `SupportSQLiteOpenHelper`, which we auto-instrument via | ||
| * [AndroidXSQLiteOpenHelper][io.sentry.android.gradle.instrumentation.androidx.sqlite.AndroidXSQLiteOpenHelper]). | ||
| * To keep our implementation simple and build times fast, developers who use `SQLiteDriver` | ||
| * directly are expected to wrap it themselves. | ||
| */ | ||
| class AndroidXSQLiteDriver : ClassInstrumentable { | ||
|
|
||
| override fun getVisitor( | ||
| instrumentableContext: ClassContext, | ||
| apiVersion: Int, | ||
| originalVisitor: ClassVisitor, | ||
| parameters: SpanAddingClassVisitorFactory.SpanAddingParameters, | ||
| ): ClassVisitor { | ||
| val currentClassName = instrumentableContext.currentClassData.className | ||
|
|
||
| return CommonClassVisitor( | ||
| apiVersion = apiVersion, | ||
| classVisitor = originalVisitor, | ||
| className = currentClassName.substringAfterLast('.'), | ||
| methodInstrumentables = listOf(SetDriverMethodInstrumentable()), | ||
| parameters = parameters, | ||
| ) | ||
| } | ||
|
|
||
| override fun isInstrumentable(data: ClassContext): Boolean = | ||
| data.currentClassData.className in TARGET_CLASSES | ||
|
|
||
| companion object { | ||
|
|
||
| // Currently covers Room 2 and Room 3 packages. Update as needed. | ||
| internal val TARGET_CLASSES = | ||
| setOf("androidx.room.RoomDatabase\$Builder", "androidx.room3.RoomDatabase\$Builder") | ||
| } | ||
| } | ||
|
|
||
| class SetDriverMethodInstrumentable : MethodInstrumentable { | ||
|
|
||
| override fun getVisitor( | ||
| instrumentableContext: MethodContext, | ||
| apiVersion: Int, | ||
| originalVisitor: MethodVisitor, | ||
| parameters: SpanAddingClassVisitorFactory.SpanAddingParameters, | ||
| ): MethodVisitor = SetDriverMethodVisitor(apiVersion, originalVisitor, instrumentableContext) | ||
|
|
||
| override fun isInstrumentable(data: MethodContext): Boolean = | ||
| data.name == SET_DRIVER && data.descriptor?.startsWith(SET_DRIVER_DESCRIPTOR_PREFIX) == true | ||
|
|
||
| companion object { | ||
| internal const val SET_DRIVER = "setDriver" | ||
| internal const val SET_DRIVER_DESCRIPTOR_PREFIX = "($SQLITE_DRIVER_TYPE_DESCRIPTOR)" | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
...y/android/gradle/instrumentation/androidx/sqlite/driver/visitor/SetDriverMethodVisitor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package io.sentry.android.gradle.instrumentation.androidx.sqlite.driver.visitor | ||
|
|
||
| import io.sentry.android.gradle.instrumentation.MethodContext | ||
| import io.sentry.android.gradle.instrumentation.androidx.sqlite.driver.SQLITE_DRIVER_TYPE_DESCRIPTOR | ||
| import org.objectweb.asm.MethodVisitor | ||
| import org.objectweb.asm.Type | ||
| import org.objectweb.asm.commons.AdviceAdapter | ||
| import org.objectweb.asm.commons.Method | ||
|
|
||
| class SetDriverMethodVisitor( | ||
| apiVersion: Int, | ||
| originalVisitor: MethodVisitor, | ||
| instrumentableContext: MethodContext, | ||
| ) : | ||
| AdviceAdapter( | ||
| apiVersion, | ||
| originalVisitor, | ||
| instrumentableContext.access, | ||
| instrumentableContext.name, | ||
| instrumentableContext.descriptor, | ||
| ) { | ||
|
|
||
| override fun onMethodEnter() { | ||
| loadArg(0) | ||
| invokeStatic(Type.getType(SENTRY_SQLITE_DRIVER_TYPE), Method(CREATE, SENTRY_CREATE_DESCRIPTOR)) | ||
| storeArg(0) | ||
| } | ||
|
|
||
| companion object { | ||
| internal const val CREATE = "create" | ||
| internal const val SENTRY_CREATE_DESCRIPTOR = | ||
| "($SQLITE_DRIVER_TYPE_DESCRIPTOR)$SQLITE_DRIVER_TYPE_DESCRIPTOR" | ||
| internal const val SENTRY_SQLITE_DRIVER_TYPE = "Lio/sentry/sqlite/SentrySQLiteDriver;" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
plugin-build/src/test/kotlin/androidx/sqlite/SQLiteDriver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package androidx.sqlite | ||
|
|
||
| /** | ||
| * Minimal stub of `androidx.sqlite.SQLiteDriver` so ASM can resolve the type referenced by the | ||
| * instrumented bytecode. | ||
| * | ||
| * Must not coexist with androidx.sqlite >= 2.5 on the test classpath: that version introduces the | ||
| * real `SQLiteDriver`, which would collide with this stub. Safe today because `libs.sqlite` is | ||
| * pinned below 2.5 and `testImplementationAar` does not pull transitive dependencies (Room 2.7+ | ||
| * depends on androidx.sqlite 2.5+, but `Aar2JarPlugin` sets `isTransitive = false`). | ||
| */ | ||
| interface SQLiteDriver |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
.../sentry/android/gradle/instrumentation/androidx/sqlite/driver/AndroidXSQLiteDriverTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| @file:Suppress("UnstableApiUsage") | ||
|
|
||
| package io.sentry.android.gradle.instrumentation.androidx.sqlite.driver | ||
|
|
||
| import io.sentry.android.gradle.instrumentation.ChainedInstrumentable | ||
| import io.sentry.android.gradle.instrumentation.ClassInstrumentable | ||
| import io.sentry.android.gradle.instrumentation.fakes.TestClassContext | ||
| import io.sentry.android.gradle.instrumentation.fakes.TestSpanAddingParameters | ||
| import java.io.FileInputStream | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.rules.TemporaryFolder | ||
| import org.objectweb.asm.ClassReader | ||
| import org.objectweb.asm.ClassWriter | ||
| import org.objectweb.asm.Opcodes | ||
|
|
||
| class AndroidXSQLiteDriverTest { | ||
|
|
||
| @get:Rule val tmpDir = TemporaryFolder() | ||
|
|
||
| private val instrumentable = AndroidXSQLiteDriver() | ||
|
|
||
| @Test | ||
| fun `isInstrumentable returns true for RoomDatabase Builder classes`() { | ||
| assertTrue( | ||
| instrumentable.isInstrumentable(TestClassContext("androidx.room.RoomDatabase\$Builder")) | ||
| ) | ||
| assertTrue( | ||
| instrumentable.isInstrumentable(TestClassContext("androidx.room3.RoomDatabase\$Builder")) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `isInstrumentable returns false for unrelated classes`() { | ||
| assertFalse(instrumentable.isInstrumentable(TestClassContext("com.example.RoomConfig"))) | ||
| assertFalse(instrumentable.isInstrumentable(TestClassContext("io.sentry.Sentry"))) | ||
| assertFalse(instrumentable.isInstrumentable(TestClassContext("com.example.FakeSetDriver"))) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ChainedInstrumentable does not instrument unrelated classes`() { | ||
| val className = "com.example.NoSetDriver" | ||
| val originalBytes = loadNoSetDriverFixtureBytes() | ||
| val instrumentedBytes = instrumentThroughChain(className, originalBytes) | ||
|
|
||
| assertEquals(0, SQLiteDriverBytecodeTestUtil.countWrapCalls(instrumentedBytes)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `does not wrap when a visited class has no setDriver method`() { | ||
| // The Room < 2.7 production path: RoomDatabase$Builder matches the allowlist (so getVisitor | ||
| // runs), but the class has no setDriver(SQLiteDriver) and must pass through without a wrap. | ||
| val instrumentedBytes = | ||
| instrumentDirectly("com.example.NoSetDriver", loadNoSetDriverFixtureBytes()) | ||
|
|
||
| assertEquals(0, SQLiteDriverBytecodeTestUtil.countWrapCalls(instrumentedBytes)) | ||
| } | ||
|
|
||
| private fun instrumentThroughChain(className: String, bytes: ByteArray): ByteArray = | ||
| instrument(ChainedInstrumentable(listOf(instrumentable)), className, bytes) | ||
|
|
||
| private fun instrumentDirectly(className: String, bytes: ByteArray): ByteArray = | ||
| instrument(instrumentable, className, bytes) | ||
|
|
||
| private fun instrument( | ||
| classInstrumentable: ClassInstrumentable, | ||
| className: String, | ||
| bytes: ByteArray, | ||
| ): ByteArray { | ||
| val classReader = ClassReader(bytes) | ||
| val classWriter = ClassWriter(classReader, ClassWriter.COMPUTE_MAXS) | ||
| val classVisitor = | ||
| classInstrumentable.getVisitor( | ||
| TestClassContext(className), | ||
| Opcodes.ASM9, | ||
| classWriter, | ||
| parameters = TestSpanAddingParameters(inMemoryDir = tmpDir.root), | ||
| ) | ||
| classReader.accept(classVisitor, ClassReader.SKIP_FRAMES) | ||
| return classWriter.toByteArray() | ||
| } | ||
|
|
||
| /** | ||
| * `NoSetDriver.class`: hand-compiled `public class NoSetDriver { int unrelated(int) }`. Shape is | ||
| * irrelevant / any class without `setDriver(SQLiteDriver)` works. | ||
| */ | ||
| private fun loadNoSetDriverFixtureBytes(): ByteArray = | ||
| FileInputStream( | ||
| "src/test/resources/testFixtures/instrumentation/androidxSqliteDriver/NoSetDriver.class" | ||
| ) | ||
| .use { it.readBytes() } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Update version number once we ship the SentrySQLiteDriver in sentry-android.