| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one or more | |
| * contributor license agreements. See the NOTICE file distributed with | |
| * this work for additional information regarding copyright ownership. | |
| * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| * (the "License"); you may not use this file except in compliance with | |
| * the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #include "JavaException.h" | |
| #include <glog/logging.h> | |
| #include <stdexcept> | |
| #include <string> | |
| namespace gluten { | |
| JniEntryGuard::JniEntryGuard() noexcept { | |
| ++depth_; | |
| } | |
| JniEntryGuard::~JniEntryGuard() noexcept { | |
| --depth_; | |
| } | |
| bool JniEntryGuard::isActive() noexcept { | |
| return depth_ != 0; | |
| } | |
| namespace { | |
| // The describer class and result need two locals; reserve one spare slot. | |
| constexpr jint kDescriptionLocalCapacity = 3; | |
| class LocalFrame { | |
| public: | |
| explicit LocalFrame(JNIEnv* env) : env_(env), pushed_(env->PushLocalFrame(kDescriptionLocalCapacity) == JNI_OK) {} | |
| ~LocalFrame() { | |
| if (pushed_) { | |
| env_->PopLocalFrame(nullptr); | |
| } | |
| } | |
| bool pushed() const { | |
| return pushed_; | |
| } | |
| private: | |
| JNIEnv* env_; | |
| bool pushed_; | |
| }; | |
| // Lossy native wrappers and destructor logging may have only what(), not the carrier. | |
| std::string describe(JNIEnv* env, jthrowable throwable) { | |
| const std::string prefix = "Error during calling Java code from native code: "; | |
| const auto unavailable = [&](const char* reason) { | |
| // Describing an exception must not replace the throwable we are transporting. | |
| env->ExceptionClear(); | |
| return prefix + "Java stack trace unavailable (" + reason + ")"; | |
| }; | |
| LocalFrame frame(env); | |
| if (!frame.pushed()) { | |
| return unavailable("PushLocalFrame failed"); | |
| } | |
| auto describer = env->FindClass("org/apache/gluten/exception/JniExceptionDescriber"); | |
| if (describer == nullptr) { | |
| return unavailable("JniExceptionDescriber lookup failed"); | |
| } | |
| auto method = env->GetStaticMethodID(describer, "describe", "(Ljava/lang/Throwable;)Ljava/lang/String;"); | |
| if (method == nullptr) { | |
| return unavailable("describe method lookup failed"); | |
| } | |
| auto description = static_cast<jstring>(env->CallStaticObjectMethod(describer, method, throwable)); | |
| if (env->ExceptionCheck() || description == nullptr) { | |
| return unavailable("describe method failed"); | |
| } | |
| const auto release = [&](const char* chars) { env->ReleaseStringUTFChars(description, chars); }; | |
| std::unique_ptr<const char, decltype(release)> chars(env->GetStringUTFChars(description, nullptr), release); | |
| if (!chars) { | |
| return unavailable("GetStringUTFChars failed"); | |
| } | |
| return prefix + chars.get(); | |
| } | |
| } // namespace | |
| struct JavaException::State { | |
| State(JNIEnv* env, jthrowable throwable) { | |
| if (throwable == nullptr) { | |
| throw std::invalid_argument("JavaException requires a non-null throwable"); | |
| } | |
| if (env->GetJavaVM(&vm) != JNI_OK) { | |
| throw std::runtime_error("Unable to get JavaVM for Java exception"); | |
| } | |
| ref = static_cast<jthrowable>(env->NewGlobalRef(throwable)); | |
| if (ref == nullptr) { | |
| throw std::runtime_error("Unable to create global reference for Java exception"); | |
| } | |
| } | |
| ~State() { | |
| JNIEnv* env = nullptr; | |
| auto status = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_8); | |
| const bool detached = status == JNI_EDETACHED; | |
| if (detached) { | |
| status = vm->AttachCurrentThreadAsDaemon(reinterpret_cast<void**>(&env), nullptr); | |
| } | |
| if (status != JNI_OK) { | |
| LOG(ERROR) << "Unable to attach thread to release Java exception global reference: " << status; | |
| return; | |
| } | |
| env->DeleteGlobalRef(ref); | |
| // Only detach if this destructor attached the thread; never detach a caller's JNI/HDFS thread. | |
| if (detached && vm->DetachCurrentThread() != JNI_OK) { | |
| LOG(ERROR) << "Unable to detach thread after releasing Java exception global reference"; | |
| } | |
| } | |
| JavaVM* vm{nullptr}; | |
| jthrowable ref{nullptr}; | |
| std::string message; | |
| }; | |
| JavaException::JavaException(JNIEnv* env, jthrowable throwable) : state_(std::make_shared<State>(env, throwable)) { | |
| try { | |
| state_->message = describe(env, throwable); | |
| } catch (const std::exception&) { | |
| // Keep the captured throwable even if diagnostics fail. what() must not allocate a fallback. | |
| env->ExceptionClear(); | |
| } | |
| } | |
| const char* JavaException::what() const noexcept { | |
| return state_->message.empty() | |
| ? "Error during calling Java code from native code: Java stack trace unavailable (native diagnostic failure)" | |
| : state_->message.c_str(); | |
| } | |
| void JavaException::throwToJava(JNIEnv* env) const noexcept { | |
| if (env->Throw(state_->ref) != JNI_OK) { | |
| LOG(ERROR) << "Unable to restore original Java exception"; | |
| } | |
| } | |
| bool JavaException::throwNestedToJava(JNIEnv* env, const std::exception& error) noexcept { | |
| const auto* nested = dynamic_cast<const std::nested_exception*>(&error); | |
| if (nested == nullptr || nested->nested_ptr() == nullptr) { | |
| return false; | |
| } | |
| try { | |
| std::rethrow_exception(nested->nested_ptr()); | |
| } catch (const JavaException& original) { | |
| original.throwToJava(env); | |
| return true; | |
| } catch (const std::exception& cause) { | |
| return throwNestedToJava(env, cause); | |
| } catch (...) { | |
| // Not a Java exception. Leave the enclosing native error to the existing JNI fallback. | |
| return false; | |
| } | |
| } | |
| void JavaException::check(JNIEnv* env) { | |
| if (!env->ExceptionCheck()) { | |
| return; | |
| } | |
| auto throwable = env->ExceptionOccurred(); | |
| env->ExceptionClear(); | |
| const auto release = [env](jthrowable ref) { env->DeleteLocalRef(ref); }; | |
| std::unique_ptr<_jthrowable, decltype(release)> local(throwable, release); | |
| // Guard construction only: successful carrier propagation must leave JNI clear. | |
| auto error = [env, throwable]() { | |
| try { | |
| return JavaException(env, throwable); | |
| } catch (...) { | |
| if (!JniEntryGuard::isActive()) { | |
| // An attached pool worker has no Java return boundary to consume pending state. | |
| env->ExceptionClear(); | |
| } else if (!env->ExceptionCheck() && env->Throw(throwable) != JNI_OK) { | |
| LOG(ERROR) << "Unable to restore original Java exception"; | |
| } | |
| throw; | |
| } | |
| }(); | |
| throw error; | |
| } | |
| } // namespace gluten |