Skip to content

Commit 8b5dc4a

Browse files
authored
fix: keep C++ exceptions and string pins inside the media JNI layer (#439)
Every media entry point now runs behind a boundary that turns C++ exceptions into Java ones, Java strings are held by an RAII wrapper, and a recorder track that fails to be added releases its frames.
1 parent da4b832 commit 8b5dc4a

13 files changed

Lines changed: 726 additions & 178 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <jni.h>
18+
/* Header for class dev_onvoid_webrtc_media_player_AllocationFault */
19+
20+
#ifndef _Included_dev_onvoid_webrtc_media_player_AllocationFault
21+
#define _Included_dev_onvoid_webrtc_media_player_AllocationFault
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
/*
26+
* Class: dev_onvoid_webrtc_media_player_AllocationFault
27+
* Method: failNextStringCopy
28+
* Signature: ()V
29+
*/
30+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_failNextStringCopy
31+
(JNIEnv *, jclass);
32+
33+
/*
34+
* Class: dev_onvoid_webrtc_media_player_AllocationFault
35+
* Method: disarm
36+
* Signature: ()V
37+
*/
38+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_disarm
39+
(JNIEnv *, jclass);
40+
41+
/*
42+
* Class: dev_onvoid_webrtc_media_player_AllocationFault
43+
* Method: heldStrings
44+
* Signature: ()I
45+
*/
46+
JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_heldStrings
47+
(JNIEnv *, jclass);
48+
49+
#ifdef __cplusplus
50+
}
51+
#endif
52+
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef WEBRTC_JAVA_MEDIA_ALLOCATION_FAULT_H_
18+
#define WEBRTC_JAVA_MEDIA_ALLOCATION_FAULT_H_
19+
20+
namespace ffmpeg
21+
{
22+
// Makes the copy of a Java string fail as an exhausted heap does, so that
23+
// the tests can see what an entry point does then: the C++ heap does not
24+
// run out on demand.
25+
//
26+
// Only the tests arm it, through JNI_AllocationFault. A copy that is not
27+
// armed pays a single atomic load.
28+
class AllocationFault
29+
{
30+
public:
31+
// Fails the next copy of a Java string, once.
32+
static void FailNextStringCopy();
33+
// Forgets a fault that has not fired.
34+
static void Disarm();
35+
// Throws std::bad_alloc if a fault is armed, and disarms it.
36+
static void CheckStringCopy();
37+
};
38+
}
39+
40+
#endif
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef WEBRTC_JAVA_MEDIA_JNI_BOUNDARY_H_
18+
#define WEBRTC_JAVA_MEDIA_JNI_BOUNDARY_H_
19+
20+
#include <jni.h>
21+
22+
#include <string>
23+
24+
namespace ffmpeg
25+
{
26+
// Throws a Java exception of the given class, unless one is pending
27+
// already, which then stays the one Java sees. Allocates nothing on the
28+
// C++ heap, so that it can also report that an allocation failed.
29+
void ThrowJavaException(JNIEnv * env, const char * class_name, const char * message) noexcept;
30+
31+
void ThrowIOException(JNIEnv * env, const std::string & message) noexcept;
32+
33+
// Turns the C++ exception being handled into a Java one: a failed
34+
// allocation into an OutOfMemoryError, anything else into a
35+
// RuntimeException. Only to be called from a catch block.
36+
void ThrowCurrentException(JNIEnv * env) noexcept;
37+
38+
// Runs the body of a JNI entry point, which must not let a C++ exception
39+
// reach the VM: one that does ends the process. A C++ exception becomes a
40+
// Java one, and the entry point returns `on_error`. Whatever the body
41+
// holds is released before the Java exception is thrown, so a destructor
42+
// may still call into Java.
43+
template <typename R, typename Body>
44+
R CallFromJava(JNIEnv * env, R on_error, Body && body) noexcept
45+
{
46+
try {
47+
return body();
48+
}
49+
catch (...) {
50+
ThrowCurrentException(env);
51+
}
52+
53+
return on_error;
54+
}
55+
56+
template <typename Body>
57+
void CallFromJava(JNIEnv * env, Body && body) noexcept
58+
{
59+
try {
60+
body();
61+
}
62+
catch (...) {
63+
ThrowCurrentException(env);
64+
}
65+
}
66+
67+
// A local reference, deleted when this goes out of scope.
68+
template <typename T>
69+
class LocalRef
70+
{
71+
public:
72+
LocalRef(JNIEnv * env, T ref) noexcept : env_(env), ref_(ref)
73+
{
74+
}
75+
76+
~LocalRef()
77+
{
78+
if (ref_ != nullptr) {
79+
env_->DeleteLocalRef(ref_);
80+
}
81+
}
82+
83+
LocalRef(const LocalRef &) = delete;
84+
LocalRef & operator=(const LocalRef &) = delete;
85+
86+
T get() const noexcept
87+
{
88+
return ref_;
89+
}
90+
91+
// Gives the reference to the caller, such as the VM it is returned to.
92+
T release() noexcept
93+
{
94+
T ref = ref_;
95+
ref_ = nullptr;
96+
97+
return ref;
98+
}
99+
100+
private:
101+
JNIEnv * env_;
102+
T ref_;
103+
};
104+
105+
// The modified UTF-8 of a Java string, released when this goes out of
106+
// scope, also when a copy of it fails on the way out.
107+
class JavaStringChars
108+
{
109+
public:
110+
// get() is null when the string is null or the VM could not
111+
// provide the characters, and a Java exception is pending then.
112+
JavaStringChars(JNIEnv * env, jstring string) noexcept;
113+
~JavaStringChars();
114+
115+
JavaStringChars(const JavaStringChars &) = delete;
116+
JavaStringChars & operator=(const JavaStringChars &) = delete;
117+
118+
const char * get() const noexcept
119+
{
120+
return chars_;
121+
}
122+
123+
// A copy that outlives this object. Throws std::bad_alloc.
124+
std::string str() const;
125+
126+
// How many strings are held right now, for the tests to see that
127+
// none is left behind.
128+
static int Held() noexcept;
129+
130+
private:
131+
JNIEnv * env_;
132+
jstring string_;
133+
const char * chars_;
134+
};
135+
}
136+
137+
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "JNI_AllocationFault.h"
18+
#include "media/AllocationFault.h"
19+
#include "media/JniBoundary.h"
20+
21+
// The Java class this belongs to exists in the tests only, so that nothing but
22+
// them can make an allocation fail.
23+
24+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_failNextStringCopy
25+
(JNIEnv * env, jclass caller)
26+
{
27+
ffmpeg::AllocationFault::FailNextStringCopy();
28+
}
29+
30+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_disarm
31+
(JNIEnv * env, jclass caller)
32+
{
33+
ffmpeg::AllocationFault::Disarm();
34+
}
35+
36+
JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_heldStrings
37+
(JNIEnv * env, jclass caller)
38+
{
39+
return ffmpeg::JavaStringChars::Held();
40+
}

0 commit comments

Comments
 (0)