Skip to content

Commit

Permalink
More JNI cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Fredy Wijaya <[email protected]>
  • Loading branch information
fredyw committed May 15, 2024
1 parent ec7c0a3 commit 10fe281
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 291 deletions.
3 changes: 0 additions & 3 deletions mobile/library/jni/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ envoy_cc_library(
"//library/common/types:c_types_lib",
"//library/common/types:managed_types_lib",
"//library/common/types:matcher_data_lib",
"//library/jni/types:jni_exception_lib",
"@envoy//source/common/buffer:buffer_lib",
"@envoy//source/common/common:assert_lib",
"@envoy//source/common/http:header_map_lib",
Expand Down Expand Up @@ -65,7 +64,6 @@ envoy_cc_library(
"//library/common/extensions/cert_validator/platform_bridge:c_types_lib",
"//library/common/extensions/key_value/platform:config",
"//library/common/types:managed_types_lib",
"//library/jni/types:jni_exception_lib",
"@envoy//source/common/protobuf",
],
# We need this to ensure that we link this into the .so even though there are no code references.
Expand All @@ -88,7 +86,6 @@ envoy_cc_library(
"//library/common/bridge:utility_lib",
"//library/common/extensions/cert_validator/platform_bridge:c_types_lib",
"//library/common/types:c_types_lib",
"//library/jni/types:jni_exception_lib",
"@envoy//bazel:boringssl",
],
)
Expand Down
6 changes: 4 additions & 2 deletions mobile/library/jni/android_network_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ static void jvmVerifyX509CertChain(const std::vector<std::string>& cert_chain,
JniHelper jni_helper(JniHelper::getThreadLocalEnv());
LocalRefUniquePtr<jobject> result =
callJvmVerifyX509CertChain(jni_helper, cert_chain, auth_type, hostname);
if (Exception::checkAndClear()) {
if (jni_helper.exceptionOccurred() != nullptr) {
*status = CertVerifyStatus::NotYetValid;
jni_helper.exceptionCleared();
} else {
extractCertVerifyResult(jni_helper, result.get(), status, is_issued_by_known_root,
verified_chain);
if (Exception::checkAndClear()) {
if (jni_helper.exceptionOccurred() != nullptr) {
*status = CertVerifyStatus::Failed;
jni_helper.exceptionCleared();
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions mobile/library/jni/jni_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ LocalRefUniquePtr<jthrowable> JniHelper::exceptionOccurred() {
return {env_->ExceptionOccurred(), LocalRefDeleter(env_)};
}

void JniHelper::exceptionCleared() { env_->ExceptionClear(); }

GlobalRefUniquePtr<jobject> JniHelper::newGlobalRef(jobject object) {
GlobalRefUniquePtr<jobject> result(env_->NewGlobalRef(object), GlobalRefDeleter(env_));
return result;
Expand Down
10 changes: 9 additions & 1 deletion mobile/library/jni/jni_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,20 @@ class JniHelper {
void throwNew(const char* java_class_name, const char* message);

/**
* Determines if an exception is being thrown.
* Determines if an exception is being thrown. Returns a `nullptr` if there is no exception.
*
* https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#exceptionoccurred
*/
[[nodiscard]] LocalRefUniquePtr<jthrowable> exceptionOccurred();

/**
* Clears any exception that is currently being thrown. If no exception is currently being thrown,
* this function has no effect.
*
* https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#exceptionclear
*/
void exceptionCleared();

/**
* Creates a new global reference to the object referred to by the `object` argument.
*
Expand Down
6 changes: 2 additions & 4 deletions mobile/library/jni/jni_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,9 @@ jvm_on_headers(const char* method, const Envoy::Types::ManagedEnvoyHeaders& head
Envoy::JNI::LocalRefUniquePtr<jobjectArray> result = jni_helper.callObjectMethod<jobjectArray>(
j_context, jmid_onHeaders, static_cast<jlong>(headers.get().length),
end_stream ? JNI_TRUE : JNI_FALSE, j_stream_intel.get());
// TODO(Augustyniak): Pass the name of the filter in here so that we can instrument the origin of
// the JNI exception better.
bool exception_cleared = Envoy::JNI::Exception::checkAndClear(method);

if (!exception_cleared) {
if (jni_helper.exceptionOccurred() != nullptr) {
jni_helper.exceptionCleared();
return result;
}

Expand Down
36 changes: 0 additions & 36 deletions mobile/library/jni/types/BUILD

This file was deleted.

155 changes: 0 additions & 155 deletions mobile/library/jni/types/exception.cc

This file was deleted.

42 changes: 0 additions & 42 deletions mobile/library/jni/types/exception.h

This file was deleted.

48 changes: 0 additions & 48 deletions mobile/library/jni/types/string.h

This file was deleted.

Loading

0 comments on commit 10fe281

Please sign in to comment.