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
2 changes: 2 additions & 0 deletions .github/actions/test-natives/jni-check-report.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ 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 '
# The JVM on Windows ends some lines with CRLF.
{ sub(/\r$/, "") }
function flush() {
if (kind != "") {
key = kind " | " (frame != "" ? frame : (native ? "<thread without Java frames>" : "<no stack>"))
Expand Down
31 changes: 23 additions & 8 deletions .github/workflows/platforms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,29 @@ jobs:
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.
# The tests once more under -Xcheck:jni, once per OS: the JNI glue is shared,
# but the device modules, capturers and hardware codecs that call back into
# Java are not, so each OS has JNI code of its own. One architecture each is
# enough, since that code is the same across them. It fails on any JNI error
# or warning, see the test-natives action.
jni-check:
name: jni-check (linux_x86-64)
name: jni-check (${{ matrix.platform.name }})
needs: build
timeout-minutes: 30
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
platform:
- name: linux_x86-64
runs-on: ubuntu-22.04
classifier: linux-x86_64
- name: macos_arm64
runs-on: macos-15
classifier: macos-aarch64
- name: windows_x86_64
runs-on: windows-2022
classifier: windows-x86_64
runs-on: ${{ matrix.platform.runs-on }}
steps:
- name: Checkout code
uses: actions/checkout@v7
Expand All @@ -175,9 +190,9 @@ jobs:
- 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
platform-name: ${{ matrix.platform.name }}
classifier: ${{ matrix.platform.classifier }}
profile: ${{ matrix.platform.classifier }}
java-distribution: temurin
version: ${{ inputs.version }}
jni-check: 'true'
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target_sources(${PROJECT_NAME}
include/JavaArray.h
include/JavaArrayList.h
include/JavaBigInteger.h
include/JavaBoundary.h
include/JavaClasses.h
include/JavaClass.h
include/JavaClassLoader.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2026, Alex Andres. All rights reserved.
*
* Use of this source code is governed by the 3-Clause BSD license that can be
* found in the LICENSE file in the root of the source tree.
*/

#ifndef JNI_JAVA_BOUNDARY_H_
#define JNI_JAVA_BOUNDARY_H_

#include "JavaUtils.h"

#include <jni.h>

namespace jni
{
namespace detail
{
// What a native method returns when its body failed: null, zero or
// false, which Java never sees, since an exception is pending then.
template <typename R>
struct FailedReturn
{
static R value()
{
return R();
}
};

template <>
struct FailedReturn<void>
{
static void value()
{
}
};
}

// Runs the body of a JNI entry point. A C++ exception that reaches the VM
// ends the process, and the helpers of this library throw them: a Java
// exception they ran into as JavaWrappedException, a failed allocation as
// std::bad_alloc. Here every C++ exception becomes a Java one, with
// ThrowCxxJavaException(), and the entry point returns null, zero or
// false. What the body holds is released before that, so a destructor may
// still call into Java. The body's own returns, also those of
// CHECK_HANDLE, return from the entry point as before:
//
// JNIEXPORT jobject JNICALL Java_..._getFoo(JNIEnv * env, jobject caller)
// {
// return jni::CallFromJava(env, [&]() -> jobject {
// ...
// });
// }
//
// A body that catches its own exceptions, such as one with a try block of
// its own around everything that may throw, needs none of this.
template <typename Body>
auto CallFromJava(JNIEnv * env, Body && body) noexcept -> decltype(body())
{
try {
return body();
}
catch (...) {
ThrowCxxJavaException(env);
}

return detail::FailedReturn<decltype(body())>::value();
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ namespace jni
explicit JavaClassLoader(JNIEnv * env, const JavaGlobalRef<jobject> & classLoader) :
classLoader(classLoader)
{
classLoaderClass_ = reinterpret_cast<jclass>(env->NewGlobalRef(env->FindClass("java/lang/ClassLoader")));
JavaLocalRef<jclass> cls(env, env->FindClass("java/lang/ClassLoader"));

if (ExceptionCheck(env)) {
return;
}

classLoaderClass_ = reinterpret_cast<jclass>(env->NewGlobalRef(cls.get()));

loadClass = env->GetMethodID(classLoaderClass_, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");

ExceptionCheck(env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ namespace jni
{
JavaClassUtils::JavaClassUtils(JNIEnv * env)
{
jclass cls = env->FindClass("java/lang/Class");
JavaLocalRef<jclass> cls(env, env->FindClass("java/lang/Class"));

getClassName = GetMethod(env, cls, "getName", "()Ljava/lang/String;");
getClassName = GetMethod(env, cls.get(), "getName", "()Ljava/lang/String;");
}

std::string JavaClassUtils::toNativeClassName(JNIEnv * env, const JavaLocalRef<jobject> & javaRef)
{
const auto classUtils = JavaClasses::get<JavaClassUtils>(env);

jclass cls = env->GetObjectClass(javaRef.get());
jstring clsName = static_cast<jstring>(env->CallObjectMethod(cls, classUtils->getClassName));
JavaLocalRef<jclass> cls(env, env->GetObjectClass(javaRef.get()));
jstring clsName = static_cast<jstring>(env->CallObjectMethod(cls.get(), 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 @@ -173,14 +173,16 @@ jfieldID GetHandleField(JNIEnv * env, jobject obj, const std::string & fieldName

jfieldID GetFieldID(JNIEnv * env, jobject obj, const std::string & fieldName, const char * type)
{
jclass cls = env->GetObjectClass(obj);
// Deleted on return: on a native thread attached for a callback, nothing
// else frees local references until the thread detaches.
jni::JavaLocalRef<jclass> cls(env, env->GetObjectClass(obj));

if (cls == nullptr) {
if (cls.get() == nullptr) {
ExceptionCheck(env);
return nullptr;
}

return GetFieldID(env, cls, fieldName, type);
return GetFieldID(env, cls.get(), fieldName, type);
}

jfieldID GetFieldID(JNIEnv * env, jclass cls, const std::string & fieldName, const char * type)
Expand Down
89 changes: 47 additions & 42 deletions webrtc-jni/src/main/cpp/src/JNI_AudioConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "JNI_AudioConverter.h"
#include "JavaBoundary.h"
#include "NativeHandle.h"
#include "api/audio/audio_processing.h"
#include "media/audio/AudioConverter.h"
Expand All @@ -26,62 +27,66 @@
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioConverter_convertInternal
(JNIEnv * env, jobject caller, jbyteArray src, jint nSrcSamples, jbyteArray dst, jint nDstSamples)
{
jni::NativeLease<jni::AudioConverter> converter(env, caller);
CHECK_HANDLE(converter);
jni::CallFromJava(env, [&]() {
jni::NativeLease<jni::AudioConverter> converter(env, caller);
CHECK_HANDLE(converter);

if (src == nullptr || dst == nullptr) {
ThrowJavaException(env, "java/lang/NullPointerException", "Sample arrays must not be null");
return;
}

// The counts are samples over all channels. The converter checks them against its format, this the arrays.
const jlong bytesPerSample = sizeof(int16_t);

if (nSrcSamples < 0 || nDstSamples < 0 ||
env->GetArrayLength(src) < nSrcSamples * bytesPerSample || env->GetArrayLength(dst) < nDstSamples * bytesPerSample) {
ThrowJavaException(env, "java/lang/IllegalArgumentException", "Sample arrays too short for the requested samples");
return;
}

try {
auto releaseSrc = [env, src](jbyte * bytes) {
env->ReleaseByteArrayElements(src, bytes, JNI_ABORT);
};
std::unique_ptr<jbyte, decltype(releaseSrc)> srcPtr(env->GetByteArrayElements(src, nullptr), releaseSrc);
if (!srcPtr) {
if (src == nullptr || dst == nullptr) {
ThrowJavaException(env, "java/lang/NullPointerException", "Sample arrays must not be null");
return;
}

// Mode 0 writes the samples back when the VM handed out a copy.
auto releaseDst = [env, dst](jbyte * bytes) {
env->ReleaseByteArrayElements(dst, bytes, 0);
};
std::unique_ptr<jbyte, decltype(releaseDst)> dstPtr(env->GetByteArrayElements(dst, nullptr), releaseDst);
if (!dstPtr) {
// The counts are samples over all channels. The converter checks them against its format, this the arrays.
const jlong bytesPerSample = sizeof(int16_t);

if (nSrcSamples < 0 || nDstSamples < 0 ||
env->GetArrayLength(src) < nSrcSamples * bytesPerSample || env->GetArrayLength(dst) < nDstSamples * bytesPerSample) {
ThrowJavaException(env, "java/lang/IllegalArgumentException", "Sample arrays too short for the requested samples");
return;
}

converter->convert(reinterpret_cast<const int16_t *>(srcPtr.get()), nSrcSamples, reinterpret_cast<int16_t *>(dstPtr.get()), nDstSamples);
}
catch (const std::invalid_argument & e) {
ThrowJavaException(env, "java/lang/IllegalArgumentException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
try {
auto releaseSrc = [env, src](jbyte * bytes) {
env->ReleaseByteArrayElements(src, bytes, JNI_ABORT);
};
std::unique_ptr<jbyte, decltype(releaseSrc)> srcPtr(env->GetByteArrayElements(src, nullptr), releaseSrc);
if (!srcPtr) {
return;
}

// Mode 0 writes the samples back when the VM handed out a copy.
auto releaseDst = [env, dst](jbyte * bytes) {
env->ReleaseByteArrayElements(dst, bytes, 0);
};
std::unique_ptr<jbyte, decltype(releaseDst)> dstPtr(env->GetByteArrayElements(dst, nullptr), releaseDst);
if (!dstPtr) {
return;
}

converter->convert(reinterpret_cast<const int16_t *>(srcPtr.get()), nSrcSamples, reinterpret_cast<int16_t *>(dstPtr.get()), nDstSamples);
}
catch (const std::invalid_argument & e) {
ThrowJavaException(env, "java/lang/IllegalArgumentException", e.what());
}
catch (...) {
ThrowCxxJavaException(env);
}
});
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_AudioConverter_dispose
(JNIEnv * env, jobject caller)
{
jni::AudioConverter * converter = jni::TakeHandle<jni::AudioConverter>(env, caller);
jni::CallFromJava(env, [&]() {
jni::AudioConverter * converter = jni::TakeHandle<jni::AudioConverter>(env, caller);

if (converter == nullptr) {
return;
}
if (converter == nullptr) {
return;
}

jni::ReleaseHandle(env, converter, [converter](bool) {
delete converter;
jni::ReleaseHandle(env, converter, [converter](bool) {
delete converter;
});
});
}

Expand Down
Loading
Loading