Skip to content

Make exceptions thrown by JNI calls to Image.getHardwareBuffer non-fatal#185125

Merged
auto-submit[bot] merged 3 commits into
flutter:masterfrom
jason-simmons:bug_175267
Apr 29, 2026
Merged

Make exceptions thrown by JNI calls to Image.getHardwareBuffer non-fatal#185125
auto-submit[bot] merged 3 commits into
flutter:masterfrom
jason-simmons:bug_175267

Conversation

@jason-simmons
Copy link
Copy Markdown
Member

If a call to Android's Image.getHardwareBuffer API made by PlatformViewAndroidJNIImpl::ImageGetHardwareBuffer throws an exception, then the ImageExternalTexture can skip rendering of this frame. It is excessive to make this a fatal exception that terminates the process.

Fixes #175267
Fixes #180804

@jason-simmons jason-simmons requested a review from reidbaker April 16, 2026 00:17
@jason-simmons jason-simmons requested a review from a team as a code owner April 16, 2026 00:17
@github-actions github-actions Bot added platform-android Android applications specifically engine flutter/engine related. See also e: labels. team-android Owned by Android platform team labels Apr 16, 2026
@jason-simmons jason-simmons added the CICD Run CI/CD label Apr 16, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a JNI mocking framework for Android platform views and adds unit tests for PlatformViewAndroidJNIImpl. It updates ImageGetHardwareBuffer to gracefully handle JNI exceptions by clearing them and returning an empty reference, and adds null checks for hardware buffers in the OpenGL and Vulkan external texture implementations. Review feedback identifies a dangling pointer risk in the test suite's JVM setup and suggests completing the JNI function table assignments in the mock environment.

Comment on lines +42 to +43
MockJNIEnv mock_env;
SetJNIEnv(&mock_env);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

mock_env is a local variable within SetUpJVM. Since SetJNIEnv stores its address in the static jvm_ instance, this pointer becomes dangling as soon as SetUpJVM returns. This can lead to undefined behavior or crashes if any code attempts to access the JNI environment via the VM before the next test sets a valid environment.

Suggested change
MockJNIEnv mock_env;
SetJNIEnv(&mock_env);
static MockJNIEnv mock_env;
SetJNIEnv(&mock_env);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added an RAII wrapper for SetJNIEnv that nulls the mock JVM's environment when the test exits

// Replace the JNIEnv's function table with wrappers that invoke the
// mockable virtual methods in this class.
functions = &jni_;
jni_.CallObjectMethodV = WrapCallObjectMethodV;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The WrapCallObjectMethod function is defined (lines 90-99) but not assigned to the jni_ function table in the constructor. While C++ JNIEnv helpers often delegate to CallObjectMethodV, it is safer and more complete to hook up both to ensure the mock behaves correctly regardless of how the JNI call is invoked.

Suggested change
jni_.CallObjectMethodV = WrapCallObjectMethodV;
jni_.CallObjectMethod = WrapCallObjectMethod;
jni_.CallObjectMethodV = WrapCallObjectMethodV;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

@github-actions github-actions Bot removed the CICD Run CI/CD label Apr 16, 2026
@jason-simmons jason-simmons added the CICD Run CI/CD label Apr 16, 2026
Copy link
Copy Markdown
Member

@gaaclarke gaaclarke left a comment

Choose a reason for hiding this comment

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

lgtm

If a call to Android's Image.getHardwareBuffer API made by PlatformViewAndroidJNIImpl::ImageGetHardwareBuffer throws an exception, then the ImageExternalTexture can skip rendering of this frame.  It is excessive to make this a fatal exception that terminates the process.

Fixes flutter#175267
Fixes flutter#180804
@github-actions github-actions Bot removed the CICD Run CI/CD label Apr 28, 2026
@jason-simmons jason-simmons added CICD Run CI/CD autosubmit Merge PR when tree becomes green via auto submit App labels Apr 28, 2026
@auto-submit auto-submit Bot added this pull request to the merge queue Apr 29, 2026
Merged via the queue into flutter:master with commit 88c9eee Apr 29, 2026
200 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Apr 29, 2026
@ZS-James2026
Copy link
Copy Markdown

If a call to Android's Image.getHardwareBuffer API made by PlatformViewAndroidJNIImpl::ImageGetHardwareBuffer throws an exception, then the ImageExternalTexture can skip rendering of this frame. It is excessive to make this a fatal exception that terminates the process.

Fixes #175267 Fixes #180804
@jason-simmons

Thanks a lot for resolving this issue promptly. I’d like to inquire when this fix is expected to be merged into the stable release and rolled out via the upgrade update.

@reidbaker
Copy link
Copy Markdown
Contributor

To determine what versions of Flutter contain the fix, please see where’s my commit. If the commit has no release tag, it is currently only available in the master channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-android Android applications specifically team-android Owned by Android platform team

Projects

None yet

4 participants