Fix R8 stripping JNI callback methods causing SIGABRT#6247
Open
stevenelliottjr wants to merge 1 commit intogoogle-ai-edge:masterfrom
Open
Fix R8 stripping JNI callback methods causing SIGABRT#6247stevenelliottjr wants to merge 1 commit intogoogle-ai-edge:masterfrom
stevenelliottjr wants to merge 1 commit intogoogle-ai-edge:masterfrom
Conversation
…#6045) R8 tree-shakes process() implementations on lambda-desugared classes implementing PacketCallback, PacketListCallback, and PacketWithHeaderCallback because it cannot see that these methods are invoked from native JNI code in graph.cc (CallbackToJava). When GetMethodID fails to find the stripped process() method at runtime, the JNI layer triggers a fatal SIGABRT that cannot be caught by try/catch. The existing -keep rule on the interfaces preserves the interface definitions but does not prevent R8 from removing method implementations on anonymous classes. Add explicit -keep rules for all classes implementing the three callback interfaces to retain their process() methods. Fixes: google-ai-edge#6045
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #6045 — Android apps using MediaPipe tasks with R8 minification (
isMinifyEnabled = true) crash with a fatalSIGABRTwhen running inference (e.g.ImageSegmenter.segment()).Root cause: R8 tree-shakes
process()method implementations on lambda-desugared anonymous classes that implementPacketCallback,PacketListCallback, orPacketWithHeaderCallback. R8 cannot see that these methods are invoked from native JNI code (graph.ccCallbackToJava→GetMethodID→CallVoidMethod), so it considers them dead code and strips them. WhenGetMethodIDfails to find the method at runtime, the JNI layer triggers a hardSIGABRTthat bypasses try/catch.The existing ProGuard rule
-keep public interface com.google.mediapipe.framework.* { public *; }preserves the interface definitions but does not prevent R8 from removing method implementations on anonymous/synthetic classes.Fix: Add explicit
-keep class * implements <Interface>rules for the three callback interfaces so R8 retainsprocess()on all implementing classes, including lambda-desugared synthetics.Changes
mediapipe/java/com/google/mediapipe/framework/proguard.pgcfg— Added keep rules for:PacketCallback.process(Packet)PacketListCallback.process(List)PacketWithHeaderCallback.process(Packet, Packet)Reproduction
Minimal repro from the issue reporter: https://github.com/rvp-diconium/mediapipe-bug-report
Logcat shows:
Test plan
mediapipe_aartarget and verify the updatedproguard.pgcfgis bundledisMinifyEnabled = trueagainst the patched AARImageSegmenter.segment()completes without crashPacketCallback(single-stream) andPacketWithHeaderCallbackcallbacks