Skip to content
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

Detect minified classes and skip instrumentation to avoid build problems #362

Merged

Conversation

adinauer
Copy link
Member

@adinauer adinauer commented Aug 1, 2022

📜 Description

Detect minified classes using a Regex and skip visiting it to avoid ArrayIndexOutOfBoundsException due to empty dstFrame.inputStack Array in org.objectweb.asm.Frame:1233.

💡 Motivation and Context

Fixes #359 and #360

💚 How did you test it?

Local build and added tests including a minimal reproduction JAR

📝 Checklist

  • I reviewed the submitted code
  • I added tests to verify the changes
  • I updated the docs if needed
  • No breaking changes

🔮 Next steps

@adinauer adinauer requested a review from romtsn as a code owner August 1, 2022 20:50
Comment on lines 53 to 56
@Test
fun `tests that something happens`() {
"""^\w(\\${'$'}(\w))*${'$'}""".toRegex().matches("a${'$'}a")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name here is not clear.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops that was just there for me to figure out why the regex failed to match.

@@ -23,6 +23,7 @@ class SentryPluginWithMinifiedLibsTest :
implementation 'com.google.android.gms:play-services-mlkit-text-recognition:18.0.0'
implementation 'com.adcolony:sdk:4.7.1'
implementation 'com.appboy:android-sdk-ui:19.0.0'
implementation files("testlib/stripe-problematic.jar")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we actually add a dependency directly to the stripe lib that is problematic?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, can update

"""^(([\w\${'$'}]\/[\w\${'$'}]{1,2})|([\w\${'$'}]{2}\/[\w\${'$'}]\/[\w\${'$'}]))(\/|${'$'})""".toRegex()
/* ktlint-enable max-line-length */

fun classNameLooksMinified(simpleClassName: String, fullClassName: String): Boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun classNameLooksMinified(simpleClassName: String, fullClassName: String): Boolean {
private fun classNameLooksMinified(simpleClassName: String, fullClassName: String): Boolean {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't access it directly from test if I do that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we add the annotation that is only visible for the test then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they're available in SAGP (yet).

/**
* Looks at the constant pool entries and searches for R8 markers
*/
internal fun ClassReader.isMinifiedClass(): Boolean {
val charBuffer = CharArray(maxStringLength)
return isR8Minified(this) || classNameLooksMinified(this.getSimpleClassName(), this.className)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there are many different ways of checking it now, I am wondering if we'd not have false positives.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we call our logger in case isMinifiedClass returns true? Let's do it if not, so people know why something isn't being instrumented.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log statement is in place:

SentryPlugin.logger.info {
                "$simpleClassName skipped from instrumentation because it's a minified class."
            }

I guess it's better to skip instrumentation for too many classes rather than break the build. But yes, we'll surely have some false positives due to the regex.

@marandaneto
Copy link
Contributor

Let's upgrade the Gradle and AGP testing matrix with the latest version to be sure that we are not breaking something new either.
You can check the latest version of AGP here
Gradle here
Also the Gradle vs AGP compatibility here
It can be a separate PR on top of this one but I'd do it before merging/releasing.

@adinauer
Copy link
Member Author

adinauer commented Aug 3, 2022

Created #363 for the build matrix.

@marandaneto
Copy link
Contributor

Thanks for digging into the dark side of bytecode man. @adinauer .

@adinauer adinauer merged commit 19a4772 into main Aug 3, 2022
@adinauer adinauer deleted the feat/fix-build-with-file-instr-for-minified-dependencies branch August 3, 2022 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Index 0 out of bounds for length 0
2 participants