Skip to content

Replace JNI string conversion workarounds with direct NewStringUTF and GetStringUTFChars to fix FindClass crash on background threads. - #3566

Merged
shlzxjp merged 1 commit into
mainfrom
bugfix/thunderllei_safestring_jni
Jul 13, 2026
Merged

Replace JNI string conversion workarounds with direct NewStringUTF and GetStringUTFChars to fix FindClass crash on background threads.#3566
shlzxjp merged 1 commit into
mainfrom
bugfix/thunderllei_safestring_jni

Conversation

@leiyue123

Copy link
Copy Markdown
Collaborator

问题

SafeConvertToJStringSafeConvertToStdString 使用 static 懒初始化缓存 java/lang/String 类,通过 FindClass 查找。如果首次调用发生在后台线程(如 ThreadPoolExecutor),FindClass 可能因为线程的 ClassLoader 上下文不完整而返回 null,触发 CheckJNI abort(java_class == null)。

相关 issue:#3104#3175

根因

原实现为了规避 Android 4.x Dalvik 时代 NewStringUTF 对 4 字节 UTF-8 字符的崩溃问题,采用了绕路方案:通过 FindClassGetMethodIDNewObject 来构造 Java String。但 FindClass 在不同线程上行为不可靠。

libpag 当前 minSdk 为 21(Android 5.0+),运行时已切换为 ART,该 NewStringUTF bug 已在 ART 中修复。

修复

  • SafeConvertToJString:直接用 env->NewStringUTF() 替代 Java 构造器绕路
  • SafeConvertToStdString:直接用 GetStringUTFChars/ReleaseStringUTFChars 替代 Java getBytes() 回调

改动量:2 文件,+5 -40 行。消除所有 static FindClass 依赖,在任何线程调用都安全。

…d GetStringUTFChars to fix FindClass crash on background threads.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.33%. Comparing base (417f38d) to head (84f81bf).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3566      +/-   ##
==========================================
- Coverage   81.34%   81.33%   -0.02%     
==========================================
  Files         653      653              
  Lines       77243    77243              
  Branches    21919    21919              
==========================================
- Hits        62837    62825      -12     
- Misses       9975     9981       +6     
- Partials     4431     4437       +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@shlzxjp
shlzxjp merged commit 4d30d01 into main Jul 13, 2026
9 checks passed
@shlzxjp
shlzxjp deleted the bugfix/thunderllei_safestring_jni branch July 13, 2026 11:51
leiyue123 added a commit that referenced this pull request Jul 14, 2026
…d GetStringUTFChars to fix FindClass crash on background threads. (#3566)
leiyue123 added a commit that referenced this pull request Jul 14, 2026
…d GetStringUTFChars to fix FindClass crash on background threads. (#3566)
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.

3 participants