Skip to content

[Android] Harden JNI resource ownership - #1525

Open
bmehta001 wants to merge 2 commits into
microsoft:mainfrom
bmehta001:bhamehta/jni-resource-safety
Open

[Android] Harden JNI resource ownership#1525
bmehta001 wants to merge 2 commits into
microsoft:mainfrom
bmehta001:bhamehta/jni-resource-safety

Conversation

@bmehta001

Copy link
Copy Markdown
Contributor

Description

Harden normal Android JNI resource ownership and failure handling found during the repository-wide follow-up to #1523.

  • Stop using JNI strings after acquisition fails, and stop common-context conversion immediately while a Java exception is pending.
  • Pass UUID context as GUID_t; the previous code converted the UUID string but accidentally passed the Java jstring, selecting the boolean SetContext overload.
  • Retain debug-event classes/listeners with checked global references, bound callback-local references, and attach/detach native callback threads correctly.
  • Fix zero-based listener identities, reuse one callback when a Java listener observes multiple event types, and release global references after its final explicit removal.
  • Keep Privacy Guard custom event-name storage alive for as long as any shared guard instance and avoid publishing partially initialized singleton state.

The listener changes follow the existing explicit lifecycle: callers remove debug listeners before closing their manager. This PR does not add support for concurrent or reentrant listener mutation.

Validation

  • Built the Android arm64 native library with NDK 29 and MATSDK_WARNINGS_AS_ERRORS=ON, exempting only the pre-existing pessimizing-move, unused-variable, and sign-compare warning categories.
  • Compiled the MAE SDK Java sources and Android instrumentation sources.
  • Assembled the debug instrumentation APK and native libraries for arm64-v8a, armeabi-v7a, x86, and x86_64.
  • Added instrumentation coverage for zero-based listener identity, callback reuse across event types, and identity release after final removal.

No Android device was connected, so the instrumentation APK was assembled but not executed.

Fix normal Android JNI paths that could retain invalid pointers, leak JNI references, dispatch UUID context through the wrong overload, or publish partially initialized Privacy Guard state.

Files changed:
- lib/jni/LogManager_jni.cpp: validate string conversion, correct UUID context, and safely own debug callbacks.
- lib/jni/JniConvertors.cpp: stop array conversion when JNI raises an exception.
- lib/jni/PrivacyGuard_jni.cpp: co-own borrowed event names and fail before singleton publication.
- lib/android_build/maesdk/src/main/java/com/microsoft/applications/events/LogManagerProvider.java: reset listener identities after final removal.
- lib/android_build/app/src/androidTest/java/com/microsoft/applications/events/maesdktest/LogManagerDDVUnitTest.java: cover listener identity reuse and release.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d7d2f27a-7339-4585-ad02-9f89ce20ef40
@bmehta001
bmehta001 requested a review from a team as a code owner August 24, 2026 04:39
Pin JNI callbacks across reentrant dispatch, make add/remove registration transitions transactional, and atomically snapshot Privacy Guard state so teardown cannot race active users or commit partial JNI conversions.

Files changed:
- lib/callbacks/DebugSource.cpp: snapshot dispatch listeners and track pending callback lifetimes.
- lib/callbacks/DebugSourceInternal.hpp: expose internal pending-listener cleanup hooks.
- lib/jni/LogManager_jni.cpp: serialize listener state transitions without lock inversion and retain callbacks through dispatch.
- lib/jni/PrivacyGuard_jni.cpp: publish atomic shared snapshots and reject partial conversion results.
- tests/unittests/DebugEventSourceTests.cpp: cover reentrant removal during dispatch.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: de6b1234-574e-4b12-9f45-88ff809818ab
@bmehta001 bmehta001 self-assigned this Aug 24, 2026
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.

1 participant