square / leakcanary

A memory leak detection library for Android.
https://square.github.io/leakcanary
Apache License 2.0
29.44k stars 3.97k forks source link

NPE on invalid hprof #1606

Open youuupeng opened 5 years ago

youuupeng commented 5 years ago

Description

Threre is a NPE when using shark to analyze hprof

Steps to Reproduce

2019-10-21_16-51-51_598.zip

  1. copy hprof file from "InternalStorage/Download/leakcanary-" to my computer
  2. open git bash in shark directory like this: image

Version Information

Additional Information

kotlin.KotlinNullPointerException
        at shark.AndroidBuildMirror$Companion$fromHeapGraph$1.invoke(AndroidBuildMirror.kt:23)
        at shark.AndroidBuildMirror$Companion$fromHeapGraph$1.invoke(AndroidBuildMirror.kt:17)
        at shark.GraphContext.getOrPut(GraphContext.kt:23)
        at shark.AndroidBuildMirror$Companion.fromHeapGraph(AndroidBuildMirror.kt:22)
        at shark.AndroidReferenceMatchers$Companion$libraryLeak$1.invoke(AndroidReferenceMatchers.kt:1192)
        at shark.AndroidReferenceMatchers$Companion$libraryLeak$1.invoke(AndroidReferenceMatchers.kt:1109)
        at shark.internal.PathFinder.<init>(PathFinder.kt:116)
        at shark.HeapAnalyzer.findLeaks(HeapAnalyzer.kt:124)
        at shark.HeapAnalyzer.analyze(HeapAnalyzer.kt:105)
        at shark.HeapAnalyzer.analyze$default(HeapAnalyzer.kt:83)
        at shark.MainKt.analyze(Main.kt:146)
        at shark.MainKt.main(Main.kt:17)
github-actions[bot] commented 5 years ago

🙏Thank you for opening an issue! LeakCanary is maintained by @pyricau with help from the community. Please be kind and remember that LeakCanary isn't anyone's main job 😘.

Armaxis commented 5 years ago

Thank you for the detailed report!

It looks like it cannot find android.os.Build in the heap dump, which is very odd, as this class should always be present. Is this heap dump coming from an Android app? Can you please tell what device / Android OS version are you running?

youuupeng commented 5 years ago

@Armaxis

Yes,it's from an Android app. device:MI PLAY android os version:8.1.0 MIUI version:10.3.4

As you say,it seems like NPE happens in here : val buildClass = graph.findClassByName("android.os.Build")!!

Strange things is,only two hprof files have NPE,while the other files can be analyzed normally. image NPE hprof file:2019-10-21_16-46-57_979.hprof,2019-10-21_16-51-51_598.hprof

Obviously,this two files is smaller than other files,i guess it lacks android.os.Build,but how that happens?

Armaxis commented 5 years ago

One thing that is definitely suspicious here is the size of the hprof files that crashes - only 220KB and 312KB. This is very small for an Android app heap dump - it means that the whole memory footprint of the app was only 220kb, which is pretty much impossible. Seeing that you also have other memory dumps that are 20-30MB in size, I would make a guess that the crashing hprof is simply incomplete and is not a valid heap dump. How did you obtain it? Did it come from the LeakCanary or from Android Profiler? Cause, if it's coming from LeakCanary then we need to investigate if it is sometimes dumps incomplete heap dump.

youuupeng commented 5 years ago

It's from LeakCanary.I pull it from my phone.Folder directory is"InternalStorage/Download/leakcanary- < packagename > "

Armaxis commented 5 years ago

Thanks! We will take a look if there's something wrong with the heap dumper

pyricau commented 5 years ago

Fascinating! @Armaxis maybe next steps here might be opening the hprof with both Android Studio profiler and also YourKit, see if it works for those. Could also look at the file content in terms of bytes, see if it's a valid hprof (looks like it is though)

Armaxis commented 5 years ago

Good idea, I'll give it a look and post back once I find anything.

Armaxis commented 5 years ago

Both Android Studio and YourKit refused to open the file claiming that it's not a heap dump.

So I went ahead and opened it with Sublime with UTF-8 encoding. Beginning of file shows that it is indeed a heap dump image

But the file ends pretty abruptly.

image

Looks like it just failed to flush the heap dump into the file.

pyricau commented 5 years ago

Thanks! What's also interesting is that shark doesnt fail the initial parsing. I wonder if there's content that valid hprofs have that we could look for, so that we can report this invalid without proceeding further.

pyricau commented 3 years ago

Next steps: check which blocks are missing from that heap dump and use those to detect "bad" heap dumps and fail early.