Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion .github/actions/test-natives/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ inputs:
required: false
default: ''

jni-check:
description: 'Run the tests under -Xcheck:jni instead, and fail on any JNI error or warning it reports.'
required: false
default: 'false'

runs:
using: "composite"
steps:
Expand Down Expand Up @@ -144,17 +149,43 @@ runs:
- name: Test
run: mvn -B -pl webrtc,webrtc-media test -DskipNativeBuild -P${{ inputs.profile }} -Dsurefire.timeout=1800
shell: bash
if: inputs.jni-check != 'true'

# The same tests under -Xcheck:jni, which checks every JNI call of the
# natives: a fatal error ends the test JVM, and a call made without
# checking for the exception the previous one may have thrown is a
# warning. The JVM writes them to native stdout, so the log goes to a file:
# one warning comes with a whole stack, and many of them would make an
# unreadable job log. The report counts them, sums them up by where they
# come from and fails on any, so that a missing check cannot creep back in.
- name: Test under -Xcheck:jni
if: inputs.jni-check == 'true'
run: |
status=0
mvn -B -pl webrtc,webrtc-media test -DskipNativeBuild -P${{ inputs.profile }} -Pjni-check \
-Dsurefire.timeout=1800 > jni-check.log 2>&1 || status=$?

# What Maven reported about the tests, which went to the file as well.
grep -E '^\[(INFO|WARNING|ERROR)\] (Tests run:|BUILD|Reactor Summary)|^\[ERROR\]' jni-check.log | tail -n 200 || true

bash "$GITHUB_ACTION_PATH/jni-check-report.sh" jni-check.log 0 || status=1

exit $status
shell: bash

# What failed, for runners that cannot be reproduced locally.
- name: Upload test reports
if: failure()
uses: actions/upload-artifact@v7
with:
name: test-reports-native-${{ inputs.platform-name }}
name: test-reports-native-${{ inputs.platform-name }}${{ inputs.jni-check == 'true' && '-jni-check' || '' }}
# A native crash ends the test JVM with a report of its own beside the
# module, which names the thread that crashed and what it ran.
# Under -Xcheck:jni, the first of its diagnostics with their stacks,
# rather than the whole log.
path: |
**/target/surefire-reports/
**/hs_err_pid*.log
jni-check-excerpt.txt
if-no-files-found: ignore
retention-days: 7
85 changes: 85 additions & 0 deletions .github/actions/test-natives/jni-check-report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env bash
#
# Evaluates the output of a test run under -Xcheck:jni. The JVM writes its
# diagnostics straight to native stdout, one block each: the message, then the
# Java stack of the thread (or its native frames on a thread without Java
# frames). Fails on any fatal error, and on more warnings than allowed.
#
# Usage: jni-check-report.sh <log> [max-warnings]
#
# A summary groups the warnings by the JNI function whose exception was left
# unchecked and by the native method the warning was raised in, which is where
# to look for the missing check. On failure, the first diagnostics with their
# stacks go to jni-check-excerpt.txt, since the whole log can grow to hundreds
# of megabytes.

set -euo pipefail

log="$1"
max_warnings="${2:-0}"

fatal=$(grep -c 'FATAL ERROR in native method' "$log" || true)
warnings=$(grep -cE 'WARNING in native method|WARNING: JNI local refs' "$log" || true)

summary=$(awk '
function flush() {
if (kind != "") {
key = kind " | " (frame != "" ? frame : (native ? "<thread without Java frames>" : "<no stack>"))
count[key]++
}
kind = ""
}
/WARNING in native method|WARNING: JNI local refs/ {
flush()
kind = $0
sub(/.*when required to from /, "", kind)
sub(/.*WARNING in native method: /, "", kind)
sub(/.*WARNING: /, "", kind)
gsub(/[0-9]+/, "N", kind)
frame = ""
native = 0
next
}
kind != "" && /^\tat / {
if (frame == "" && index($0, "Native Method)") > 0) {
frame = $0
sub(/^\tat /, "", frame)
sub(/\(.*$/, "", frame)
}
next
}
kind != "" && /^\t- / { next }
kind != "" && (/^Native frames/ || /^[VCvjJ] /) { native = 1; next }
kind != "" { flush() }
END {
flush()
for (key in count) printf "%8d %s\n", count[key], key
}
' "$log" | sort -rn | head -n 25)

echo "-Xcheck:jni: $fatal fatal errors, $warnings warnings (at most $max_warnings allowed)"

if [ -n "$summary" ]; then
echo "Warnings by unchecked JNI function and native method:"
echo "$summary"
fi

if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
{
echo "### -Xcheck:jni"
echo
echo "$fatal fatal errors, $warnings warnings (at most $max_warnings allowed)."
if [ -n "$summary" ]; then
echo
echo '```'
echo "$summary"
echo '```'
fi
} >> "$GITHUB_STEP_SUMMARY"
fi

if [ "$fatal" -gt 0 ] || [ "$warnings" -gt "$max_warnings" ]; then
grep -n -A 25 -m 100 -E 'FATAL ERROR in native method|WARNING in native method|WARNING: JNI local refs' "$log" \
> jni-check-excerpt.txt || true
exit 1
fi
24 changes: 24 additions & 0 deletions .github/workflows/platforms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,27 @@ jobs:
java-distribution: ${{ matrix.platform.java-distribution }}
java-architecture: ${{ matrix.platform.java-architecture }}
version: ${{ inputs.version }}

# The tests once more under -Xcheck:jni, on one platform only: the JNI code
# is the same on all of them, and its exception checks are what this is
# about. It fails on any JNI error or warning, see the test-natives action.
jni-check:
name: jni-check (linux_x86-64)
needs: build
timeout-minutes: 30
runs-on: ubuntu-22.04
steps:
- name: Checkout code
uses: actions/checkout@v7
with:
ref: ${{ github.sha }}

- name: Test natives under -Xcheck:jni
uses: ./.github/actions/test-natives
with:
platform-name: linux_x86-64
classifier: linux-x86_64
profile: linux-x86_64
java-distribution: temurin
version: ${{ inputs.version }}
jni-check: 'true'
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace jni
jmethodID enumValues = GetStaticMethod(env, enumClass, "values", valuesSig.c_str());

jobject val = env->CallStaticObjectMethod(enumClass, enumValues);
ExceptionCheck(env);

values = JavaGlobalRef<jobjectArray>(env, static_cast<jobjectArray>(val));
valuesLength = env->GetArrayLength(values.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class className \
static retType getValue(JNIEnv * env, jobject obj) \
{ \
const auto javaClass = JavaClasses::get<JAVA_PRIMITIVE_CLASS(className)>(env); \
return env->mCall(obj, javaClass->value); \
const retType value = env->mCall(obj, javaClass->value); \
ExceptionCheck(env); \
return value; \
} \
\
private: \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,26 @@ namespace jni

bool JavaArrayList::add(const JavaRef<jobject> & obj)
{
return env->CallBooleanMethod(list, classDef->add, obj.get());
const bool added = env->CallBooleanMethod(list, classDef->add, obj.get());
ExceptionCheck(env);

return added;
}

JavaLocalRef<jobject> JavaArrayList::get(int index)
{
return JavaLocalRef<jobject>(env, env->CallObjectMethod(list, classDef->get, index));
JavaLocalRef<jobject> element(env, env->CallObjectMethod(list, classDef->get, index));
ExceptionCheck(env);

return element;
}

int JavaArrayList::size()
{
return env->CallIntMethod(list, classDef->size);
const int size = env->CallIntMethod(list, classDef->size);
ExceptionCheck(env);

return size;
}

JavaLocalRef<jobject> JavaArrayList::listObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace jni

jclass cls = env->GetObjectClass(javaRef.get());
jstring clsName = static_cast<jstring>(env->CallObjectMethod(cls, classUtils->getClassName));
ExceptionCheck(env);

return JavaString::toNative(env, JavaLocalRef<jstring>(env, clsName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace jni
JavaMapIterator JavaHashMap::begin()
{
JavaLocalRef<jobject> entrySet = JavaLocalRef<jobject>(env, env->CallObjectMethod(jMap, mapClass->entrySet));
ExceptionCheck(env);

return JavaMapIterator(env, entrySet);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace jni
}

iterator = JavaLocalRef<jobject>(env, env->CallObjectMethod(iterable, iterableClass->iterator));
ExceptionCheck(env);

// Start at the first element.
++(*this);
Expand All @@ -50,6 +51,7 @@ namespace jni
}

bool hasNext = env->CallBooleanMethod(iterator, iterableClass->iteratorHasNext);
ExceptionCheck(env);

if (!hasNext) {
iterator = nullptr;
Expand All @@ -58,6 +60,7 @@ namespace jni
}

value = JavaLocalRef<jobject>(env, env->CallObjectMethod(iterator, iterableClass->iteratorNext));
ExceptionCheck(env);

return *this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace jni
}

iterator = JavaLocalRef<jobject>(env, env->CallObjectMethod(iterable, mapIteratorClass->iterator));
ExceptionCheck(env);

// Start at the first element.
++(*this);
Expand All @@ -49,6 +50,7 @@ namespace jni
}

bool hasNext = env->CallBooleanMethod(iterator, mapIteratorClass->iteratorHasNext);
ExceptionCheck(env);

if (!hasNext) {
iterator = nullptr;
Expand All @@ -57,8 +59,11 @@ namespace jni
}

auto mapEntry = JavaLocalRef<jobject>(env, env->CallObjectMethod(iterator, mapIteratorClass->iteratorNext));
ExceptionCheck(env);
auto key = JavaLocalRef<jobject>(env, env->CallObjectMethod(mapEntry, mapIteratorClass->getKey));
ExceptionCheck(env);
auto value = JavaLocalRef<jobject>(env, env->CallObjectMethod(mapEntry, mapIteratorClass->getValue));
ExceptionCheck(env);

entry = std::make_pair(key, value);

Expand Down
5 changes: 4 additions & 1 deletion webrtc-jni/src/main/cpp/src/WebRTCContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ namespace jni

auto javaGet = GetStaticMethod(env, javaClass, "getClassLoader", "()Ljava/lang/ClassLoader;");

auto loaderRef = jni::JavaGlobalRef<jobject>(env, env->CallStaticObjectMethod(javaClass, javaGet));
jni::JavaLocalRef<jobject> loader(env, env->CallStaticObjectMethod(javaClass, javaGet));
ExceptionCheck(env);

auto loaderRef = jni::JavaGlobalRef<jobject>(env, loader.get());

InitClassLoader(env, loaderRef);
}
Expand Down
31 changes: 21 additions & 10 deletions webrtc-jni/src/main/cpp/src/api/VideoFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,28 @@ namespace jni
return webrtc::scoped_refptr<webrtc::VideoFrameBuffer>(buffer);
}

const jint width = env->CallIntMethod(i420, javaClass->getWidth);
const jint height = env->CallIntMethod(i420, javaClass->getHeight);
const jint strideY = env->CallIntMethod(i420, javaClass->getStrideY);
const jint strideU = env->CallIntMethod(i420, javaClass->getStrideU);
const jint strideV = env->CallIntMethod(i420, javaClass->getStrideV);
ExceptionCheck(env);
// Each call is checked before the next one: a buffer's getter may
// throw, and no JNI call may follow with its exception pending.
auto callInt = [&](jmethodID method) {
const jint value = env->CallIntMethod(i420, method);
ExceptionCheck(env);
return value;
};
auto callObject = [&](jmethodID method) {
JavaLocalRef<jobject> value(env, env->CallObjectMethod(i420, method));
ExceptionCheck(env);
return value;
};

JavaLocalRef<jobject> dataY(env, env->CallObjectMethod(i420, javaClass->getDataY));
JavaLocalRef<jobject> dataU(env, env->CallObjectMethod(i420, javaClass->getDataU));
JavaLocalRef<jobject> dataV(env, env->CallObjectMethod(i420, javaClass->getDataV));
ExceptionCheck(env);
const jint width = callInt(javaClass->getWidth);
const jint height = callInt(javaClass->getHeight);
const jint strideY = callInt(javaClass->getStrideY);
const jint strideU = callInt(javaClass->getStrideU);
const jint strideV = callInt(javaClass->getStrideV);

JavaLocalRef<jobject> dataY = callObject(javaClass->getDataY);
JavaLocalRef<jobject> dataU = callObject(javaClass->getDataU);
JavaLocalRef<jobject> dataV = callObject(javaClass->getDataV);

if (dataY.get() == nullptr || dataU.get() == nullptr || dataV.get() == nullptr) {
return nullptr;
Expand Down
5 changes: 5 additions & 0 deletions webrtc-jni/src/main/cpp/src/api/WebRTCUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ namespace jni
}

JavaLocalRef<jobject> thread(env, env->CallStaticObjectMethod(threadClass.get(), currentThread));
if (env->ExceptionCheck()) {
env->ExceptionClear();
return;
}

JavaLocalRef<jobject> handler(env, env->CallObjectMethod(thread.get(), getHandler));
if (!env->ExceptionCheck() && handler.get()) {
env->CallVoidMethod(handler.get(), uncaughtException, thread.get(), exception);
Expand Down
6 changes: 4 additions & 2 deletions webrtc-media/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,10 @@
</profile>

<!-- Reruns the tests under -Xcheck:jni, which turns a JNI reference
mistake into a hard error instead of a crash somewhere later. Not
part of the default build: see the note in the root CLAUDE.md. -->
mistake into a hard error instead of a crash somewhere later, and
warns about a JNI call made with the previous one's exception left
unchecked. CI's jni-check job fails on either; see the profile of
the same name in webrtc/pom.xml. -->
<profile>
<id>jni-check</id>
<build>
Expand Down
6 changes: 6 additions & 0 deletions webrtc-media/src/main/cpp/src/media/JniBoundary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ namespace ffmpeg
}

LocalRef<jobject> thread(env, env->CallStaticObjectMethod(thread_class.get(), current_thread));

if (env->ExceptionCheck() == JNI_TRUE) {
env->ExceptionClear();
return;
}

LocalRef<jobject> handler(env, env->CallObjectMethod(thread.get(), get_handler));

if (env->ExceptionCheck() == JNI_FALSE && handler.get() != nullptr) {
Expand Down
Loading
Loading